OpenACC acc_on_device.
[official-gcc.git] / gcc / cp / parser.c
blob82613b27dde9173463f5447e34c4d115f0379593
1 /* C++ Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 } non_integral_constant;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 } name_lookup_error;
130 /* The various kinds of required token */
131 typedef enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_INTERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
186 /* Prototypes. */
188 static cp_lexer *cp_lexer_new_main
189 (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191 (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193 (cp_lexer *);
194 static int cp_lexer_saving_tokens
195 (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197 (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201 (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203 (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211 (cp_lexer *);
212 static void cp_lexer_purge_token
213 (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217 (cp_lexer *);
218 static void cp_lexer_commit_tokens
219 (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221 (cp_lexer *);
222 static void cp_lexer_print_token
223 (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225 (cp_lexer *);
226 static void cp_lexer_start_debugging
227 (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229 (cp_lexer *) ATTRIBUTE_UNUSED;
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 static tree cp_literal_operator_id
238 (const char *);
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static tree cp_parser_cilk_for
243 (cp_parser *, tree);
244 static bool cp_parser_omp_declare_reduction_exprs
245 (tree, cp_parser *);
246 static tree cp_parser_cilk_simd_vectorlength
247 (cp_parser *, tree, bool);
249 /* Manifest constants. */
250 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
251 #define CP_SAVED_TOKEN_STACK 5
253 /* Variables. */
255 /* The stream to which debugging output should be written. */
256 static FILE *cp_lexer_debug_stream;
258 /* Nonzero if we are parsing an unevaluated operand: an operand to
259 sizeof, typeof, or alignof. */
260 int cp_unevaluated_operand;
262 /* Dump up to NUM tokens in BUFFER to FILE starting with token
263 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
264 first token in BUFFER. If NUM is 0, dump all the tokens. If
265 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
266 highlighted by surrounding it in [[ ]]. */
268 static void
269 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
270 cp_token *start_token, unsigned num,
271 cp_token *curr_token)
273 unsigned i, nprinted;
274 cp_token *token;
275 bool do_print;
277 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
279 if (buffer == NULL)
280 return;
282 if (num == 0)
283 num = buffer->length ();
285 if (start_token == NULL)
286 start_token = buffer->address ();
288 if (start_token > buffer->address ())
290 cp_lexer_print_token (file, &(*buffer)[0]);
291 fprintf (file, " ... ");
294 do_print = false;
295 nprinted = 0;
296 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
298 if (token == start_token)
299 do_print = true;
301 if (!do_print)
302 continue;
304 nprinted++;
305 if (token == curr_token)
306 fprintf (file, "[[");
308 cp_lexer_print_token (file, token);
310 if (token == curr_token)
311 fprintf (file, "]]");
313 switch (token->type)
315 case CPP_SEMICOLON:
316 case CPP_OPEN_BRACE:
317 case CPP_CLOSE_BRACE:
318 case CPP_EOF:
319 fputc ('\n', file);
320 break;
322 default:
323 fputc (' ', file);
327 if (i == num && i < buffer->length ())
329 fprintf (file, " ... ");
330 cp_lexer_print_token (file, &buffer->last ());
333 fprintf (file, "\n");
337 /* Dump all tokens in BUFFER to stderr. */
339 void
340 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
342 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
345 DEBUG_FUNCTION void
346 debug (vec<cp_token, va_gc> &ref)
348 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
351 DEBUG_FUNCTION void
352 debug (vec<cp_token, va_gc> *ptr)
354 if (ptr)
355 debug (*ptr);
356 else
357 fprintf (stderr, "<nil>\n");
361 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
362 description for T. */
364 static void
365 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
367 if (t)
369 fprintf (file, "%s: ", desc);
370 print_node_brief (file, "", t, 0);
375 /* Dump parser context C to FILE. */
377 static void
378 cp_debug_print_context (FILE *file, cp_parser_context *c)
380 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
381 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
382 print_node_brief (file, "", c->object_type, 0);
383 fprintf (file, "}\n");
387 /* Print the stack of parsing contexts to FILE starting with FIRST. */
389 static void
390 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
392 unsigned i;
393 cp_parser_context *c;
395 fprintf (file, "Parsing context stack:\n");
396 for (i = 0, c = first; c; c = c->next, i++)
398 fprintf (file, "\t#%u: ", i);
399 cp_debug_print_context (file, c);
404 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
406 static void
407 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
409 if (flag)
410 fprintf (file, "%s: true\n", desc);
414 /* Print an unparsed function entry UF to FILE. */
416 static void
417 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
419 unsigned i;
420 cp_default_arg_entry *default_arg_fn;
421 tree fn;
423 fprintf (file, "\tFunctions with default args:\n");
424 for (i = 0;
425 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
426 i++)
428 fprintf (file, "\t\tClass type: ");
429 print_node_brief (file, "", default_arg_fn->class_type, 0);
430 fprintf (file, "\t\tDeclaration: ");
431 print_node_brief (file, "", default_arg_fn->decl, 0);
432 fprintf (file, "\n");
435 fprintf (file, "\n\tFunctions with definitions that require "
436 "post-processing\n\t\t");
437 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
439 print_node_brief (file, "", fn, 0);
440 fprintf (file, " ");
442 fprintf (file, "\n");
444 fprintf (file, "\n\tNon-static data members with initializers that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
455 /* Print the stack of unparsed member functions S to FILE. */
457 static void
458 cp_debug_print_unparsed_queues (FILE *file,
459 vec<cp_unparsed_functions_entry, va_gc> *s)
461 unsigned i;
462 cp_unparsed_functions_entry *uf;
464 fprintf (file, "Unparsed functions\n");
465 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
467 fprintf (file, "#%u:\n", i);
468 cp_debug_print_unparsed_function (file, uf);
473 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
474 the given PARSER. If FILE is NULL, the output is printed on stderr. */
476 static void
477 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
479 cp_token *next_token, *first_token, *start_token;
481 if (file == NULL)
482 file = stderr;
484 next_token = parser->lexer->next_token;
485 first_token = parser->lexer->buffer->address ();
486 start_token = (next_token > first_token + window_size / 2)
487 ? next_token - window_size / 2
488 : first_token;
489 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
490 next_token);
494 /* Dump debugging information for the given PARSER. If FILE is NULL,
495 the output is printed on stderr. */
497 void
498 cp_debug_parser (FILE *file, cp_parser *parser)
500 const size_t window_size = 20;
501 cp_token *token;
502 expanded_location eloc;
504 if (file == NULL)
505 file = stderr;
507 fprintf (file, "Parser state\n\n");
508 fprintf (file, "Number of tokens: %u\n",
509 vec_safe_length (parser->lexer->buffer));
510 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
511 cp_debug_print_tree_if_set (file, "Object scope",
512 parser->object_scope);
513 cp_debug_print_tree_if_set (file, "Qualifying scope",
514 parser->qualifying_scope);
515 cp_debug_print_context_stack (file, parser->context);
516 cp_debug_print_flag (file, "Allow GNU extensions",
517 parser->allow_gnu_extensions_p);
518 cp_debug_print_flag (file, "'>' token is greater-than",
519 parser->greater_than_is_operator_p);
520 cp_debug_print_flag (file, "Default args allowed in current "
521 "parameter list", parser->default_arg_ok_p);
522 cp_debug_print_flag (file, "Parsing integral constant-expression",
523 parser->integral_constant_expression_p);
524 cp_debug_print_flag (file, "Allow non-constant expression in current "
525 "constant-expression",
526 parser->allow_non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Seen non-constant expression",
528 parser->non_integral_constant_expression_p);
529 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
530 "current context",
531 parser->local_variables_forbidden_p);
532 cp_debug_print_flag (file, "In unbraced linkage specification",
533 parser->in_unbraced_linkage_specification_p);
534 cp_debug_print_flag (file, "Parsing a declarator",
535 parser->in_declarator_p);
536 cp_debug_print_flag (file, "In template argument list",
537 parser->in_template_argument_list_p);
538 cp_debug_print_flag (file, "Parsing an iteration statement",
539 parser->in_statement & IN_ITERATION_STMT);
540 cp_debug_print_flag (file, "Parsing a switch statement",
541 parser->in_statement & IN_SWITCH_STMT);
542 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
543 parser->in_statement & IN_OMP_BLOCK);
544 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
545 parser->in_statement & IN_CILK_SIMD_FOR);
546 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
547 parser->in_statement & IN_OMP_FOR);
548 cp_debug_print_flag (file, "Parsing an if statement",
549 parser->in_statement & IN_IF_STMT);
550 cp_debug_print_flag (file, "Parsing a type-id in an expression "
551 "context", parser->in_type_id_in_expr_p);
552 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
553 parser->implicit_extern_c);
554 cp_debug_print_flag (file, "String expressions should be translated "
555 "to execution character set",
556 parser->translate_strings_p);
557 cp_debug_print_flag (file, "Parsing function body outside of a "
558 "local class", parser->in_function_body);
559 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
560 parser->colon_corrects_to_scope_p);
561 cp_debug_print_flag (file, "Colon doesn't start a class definition",
562 parser->colon_doesnt_start_class_def_p);
563 if (parser->type_definition_forbidden_message)
564 fprintf (file, "Error message for forbidden type definitions: %s\n",
565 parser->type_definition_forbidden_message);
566 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
567 fprintf (file, "Number of class definitions in progress: %u\n",
568 parser->num_classes_being_defined);
569 fprintf (file, "Number of template parameter lists for the current "
570 "declaration: %u\n", parser->num_template_parameter_lists);
571 cp_debug_parser_tokens (file, parser, window_size);
572 token = parser->lexer->next_token;
573 fprintf (file, "Next token to parse:\n");
574 fprintf (file, "\tToken: ");
575 cp_lexer_print_token (file, token);
576 eloc = expand_location (token->location);
577 fprintf (file, "\n\tFile: %s\n", eloc.file);
578 fprintf (file, "\tLine: %d\n", eloc.line);
579 fprintf (file, "\tColumn: %d\n", eloc.column);
582 DEBUG_FUNCTION void
583 debug (cp_parser &ref)
585 cp_debug_parser (stderr, &ref);
588 DEBUG_FUNCTION void
589 debug (cp_parser *ptr)
591 if (ptr)
592 debug (*ptr);
593 else
594 fprintf (stderr, "<nil>\n");
597 /* Allocate memory for a new lexer object and return it. */
599 static cp_lexer *
600 cp_lexer_alloc (void)
602 cp_lexer *lexer;
604 c_common_no_more_pch ();
606 /* Allocate the memory. */
607 lexer = ggc_cleared_alloc<cp_lexer> ();
609 /* Initially we are not debugging. */
610 lexer->debugging_p = false;
612 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
614 /* Create the buffer. */
615 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
617 return lexer;
621 /* Create a new main C++ lexer, the lexer that gets tokens from the
622 preprocessor. */
624 static cp_lexer *
625 cp_lexer_new_main (void)
627 cp_lexer *lexer;
628 cp_token token;
630 /* It's possible that parsing the first pragma will load a PCH file,
631 which is a GC collection point. So we have to do that before
632 allocating any memory. */
633 cp_parser_initial_pragma (&token);
635 lexer = cp_lexer_alloc ();
637 /* Put the first token in the buffer. */
638 lexer->buffer->quick_push (token);
640 /* Get the remaining tokens from the preprocessor. */
641 while (token.type != CPP_EOF)
643 cp_lexer_get_preprocessor_token (lexer, &token);
644 vec_safe_push (lexer->buffer, token);
647 lexer->last_token = lexer->buffer->address ()
648 + lexer->buffer->length ()
649 - 1;
650 lexer->next_token = lexer->buffer->length ()
651 ? lexer->buffer->address ()
652 : &eof_token;
654 /* Subsequent preprocessor diagnostics should use compiler
655 diagnostic functions to get the compiler source location. */
656 done_lexing = true;
658 gcc_assert (!lexer->next_token->purged_p);
659 return lexer;
662 /* Create a new lexer whose token stream is primed with the tokens in
663 CACHE. When these tokens are exhausted, no new tokens will be read. */
665 static cp_lexer *
666 cp_lexer_new_from_tokens (cp_token_cache *cache)
668 cp_token *first = cache->first;
669 cp_token *last = cache->last;
670 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
672 /* We do not own the buffer. */
673 lexer->buffer = NULL;
674 lexer->next_token = first == last ? &eof_token : first;
675 lexer->last_token = last;
677 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
679 /* Initially we are not debugging. */
680 lexer->debugging_p = false;
682 gcc_assert (!lexer->next_token->purged_p);
683 return lexer;
686 /* Frees all resources associated with LEXER. */
688 static void
689 cp_lexer_destroy (cp_lexer *lexer)
691 vec_free (lexer->buffer);
692 lexer->saved_tokens.release ();
693 ggc_free (lexer);
696 /* Returns nonzero if debugging information should be output. */
698 static inline bool
699 cp_lexer_debugging_p (cp_lexer *lexer)
701 return lexer->debugging_p;
705 static inline cp_token_position
706 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
708 gcc_assert (!previous_p || lexer->next_token != &eof_token);
710 return lexer->next_token - previous_p;
713 static inline cp_token *
714 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
716 return pos;
719 static inline void
720 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
722 lexer->next_token = cp_lexer_token_at (lexer, pos);
725 static inline cp_token_position
726 cp_lexer_previous_token_position (cp_lexer *lexer)
728 if (lexer->next_token == &eof_token)
729 return lexer->last_token - 1;
730 else
731 return cp_lexer_token_position (lexer, true);
734 static inline cp_token *
735 cp_lexer_previous_token (cp_lexer *lexer)
737 cp_token_position tp = cp_lexer_previous_token_position (lexer);
739 return cp_lexer_token_at (lexer, tp);
742 /* nonzero if we are presently saving tokens. */
744 static inline int
745 cp_lexer_saving_tokens (const cp_lexer* lexer)
747 return lexer->saved_tokens.length () != 0;
750 /* Store the next token from the preprocessor in *TOKEN. Return true
751 if we reach EOF. If LEXER is NULL, assume we are handling an
752 initial #pragma pch_preprocess, and thus want the lexer to return
753 processed strings. */
755 static void
756 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
758 static int is_extern_c = 0;
760 /* Get a new token from the preprocessor. */
761 token->type
762 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
763 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
764 token->keyword = RID_MAX;
765 token->pragma_kind = PRAGMA_NONE;
766 token->purged_p = false;
767 token->error_reported = false;
769 /* On some systems, some header files are surrounded by an
770 implicit extern "C" block. Set a flag in the token if it
771 comes from such a header. */
772 is_extern_c += pending_lang_change;
773 pending_lang_change = 0;
774 token->implicit_extern_c = is_extern_c > 0;
776 /* Check to see if this token is a keyword. */
777 if (token->type == CPP_NAME)
779 if (C_IS_RESERVED_WORD (token->u.value))
781 /* Mark this token as a keyword. */
782 token->type = CPP_KEYWORD;
783 /* Record which keyword. */
784 token->keyword = C_RID_CODE (token->u.value);
786 else
788 if (warn_cxx0x_compat
789 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
790 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
792 /* Warn about the C++0x keyword (but still treat it as
793 an identifier). */
794 warning (OPT_Wc__0x_compat,
795 "identifier %qE is a keyword in C++11",
796 token->u.value);
798 /* Clear out the C_RID_CODE so we don't warn about this
799 particular identifier-turned-keyword again. */
800 C_SET_RID_CODE (token->u.value, RID_MAX);
803 token->keyword = RID_MAX;
806 else if (token->type == CPP_AT_NAME)
808 /* This only happens in Objective-C++; it must be a keyword. */
809 token->type = CPP_KEYWORD;
810 switch (C_RID_CODE (token->u.value))
812 /* Replace 'class' with '@class', 'private' with '@private',
813 etc. This prevents confusion with the C++ keyword
814 'class', and makes the tokens consistent with other
815 Objective-C 'AT' keywords. For example '@class' is
816 reported as RID_AT_CLASS which is consistent with
817 '@synchronized', which is reported as
818 RID_AT_SYNCHRONIZED.
820 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
821 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
822 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
823 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
824 case RID_THROW: token->keyword = RID_AT_THROW; break;
825 case RID_TRY: token->keyword = RID_AT_TRY; break;
826 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
827 default: token->keyword = C_RID_CODE (token->u.value);
830 else if (token->type == CPP_PRAGMA)
832 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
833 token->pragma_kind = ((enum pragma_kind)
834 TREE_INT_CST_LOW (token->u.value));
835 token->u.value = NULL_TREE;
839 /* Update the globals input_location and the input file stack from TOKEN. */
840 static inline void
841 cp_lexer_set_source_position_from_token (cp_token *token)
843 if (token->type != CPP_EOF)
845 input_location = token->location;
849 /* Update the globals input_location and the input file stack from LEXER. */
850 static inline void
851 cp_lexer_set_source_position (cp_lexer *lexer)
853 cp_token *token = cp_lexer_peek_token (lexer);
854 cp_lexer_set_source_position_from_token (token);
857 /* Return a pointer to the next token in the token stream, but do not
858 consume it. */
860 static inline cp_token *
861 cp_lexer_peek_token (cp_lexer *lexer)
863 if (cp_lexer_debugging_p (lexer))
865 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
866 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
867 putc ('\n', cp_lexer_debug_stream);
869 return lexer->next_token;
872 /* Return true if the next token has the indicated TYPE. */
874 static inline bool
875 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
877 return cp_lexer_peek_token (lexer)->type == type;
880 /* Return true if the next token does not have the indicated TYPE. */
882 static inline bool
883 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
885 return !cp_lexer_next_token_is (lexer, type);
888 /* Return true if the next token is the indicated KEYWORD. */
890 static inline bool
891 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
893 return cp_lexer_peek_token (lexer)->keyword == keyword;
896 static inline bool
897 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
899 return cp_lexer_peek_nth_token (lexer, n)->type == type;
902 static inline bool
903 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
905 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
908 /* Return true if the next token is not the indicated KEYWORD. */
910 static inline bool
911 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
913 return cp_lexer_peek_token (lexer)->keyword != keyword;
916 /* Return true if the next token is a keyword for a decl-specifier. */
918 static bool
919 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
921 cp_token *token;
923 token = cp_lexer_peek_token (lexer);
924 switch (token->keyword)
926 /* auto specifier: storage-class-specifier in C++,
927 simple-type-specifier in C++0x. */
928 case RID_AUTO:
929 /* Storage classes. */
930 case RID_REGISTER:
931 case RID_STATIC:
932 case RID_EXTERN:
933 case RID_MUTABLE:
934 case RID_THREAD:
935 /* Elaborated type specifiers. */
936 case RID_ENUM:
937 case RID_CLASS:
938 case RID_STRUCT:
939 case RID_UNION:
940 case RID_TYPENAME:
941 /* Simple type specifiers. */
942 case RID_CHAR:
943 case RID_CHAR16:
944 case RID_CHAR32:
945 case RID_WCHAR:
946 case RID_BOOL:
947 case RID_SHORT:
948 case RID_INT:
949 case RID_LONG:
950 case RID_INT128:
951 case RID_SIGNED:
952 case RID_UNSIGNED:
953 case RID_FLOAT:
954 case RID_DOUBLE:
955 case RID_VOID:
956 /* GNU extensions. */
957 case RID_ATTRIBUTE:
958 case RID_TYPEOF:
959 /* C++0x extensions. */
960 case RID_DECLTYPE:
961 case RID_UNDERLYING_TYPE:
962 return true;
964 default:
965 return false;
969 /* Returns TRUE iff the token T begins a decltype type. */
971 static bool
972 token_is_decltype (cp_token *t)
974 return (t->keyword == RID_DECLTYPE
975 || t->type == CPP_DECLTYPE);
978 /* Returns TRUE iff the next token begins a decltype type. */
980 static bool
981 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
983 cp_token *t = cp_lexer_peek_token (lexer);
984 return token_is_decltype (t);
987 /* Return a pointer to the Nth token in the token stream. If N is 1,
988 then this is precisely equivalent to cp_lexer_peek_token (except
989 that it is not inline). One would like to disallow that case, but
990 there is one case (cp_parser_nth_token_starts_template_id) where
991 the caller passes a variable for N and it might be 1. */
993 static cp_token *
994 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
996 cp_token *token;
998 /* N is 1-based, not zero-based. */
999 gcc_assert (n > 0);
1001 if (cp_lexer_debugging_p (lexer))
1002 fprintf (cp_lexer_debug_stream,
1003 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1005 --n;
1006 token = lexer->next_token;
1007 gcc_assert (!n || token != &eof_token);
1008 while (n != 0)
1010 ++token;
1011 if (token == lexer->last_token)
1013 token = &eof_token;
1014 break;
1017 if (!token->purged_p)
1018 --n;
1021 if (cp_lexer_debugging_p (lexer))
1023 cp_lexer_print_token (cp_lexer_debug_stream, token);
1024 putc ('\n', cp_lexer_debug_stream);
1027 return token;
1030 /* Return the next token, and advance the lexer's next_token pointer
1031 to point to the next non-purged token. */
1033 static cp_token *
1034 cp_lexer_consume_token (cp_lexer* lexer)
1036 cp_token *token = lexer->next_token;
1038 gcc_assert (token != &eof_token);
1039 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1043 lexer->next_token++;
1044 if (lexer->next_token == lexer->last_token)
1046 lexer->next_token = &eof_token;
1047 break;
1051 while (lexer->next_token->purged_p);
1053 cp_lexer_set_source_position_from_token (token);
1055 /* Provide debugging output. */
1056 if (cp_lexer_debugging_p (lexer))
1058 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1059 cp_lexer_print_token (cp_lexer_debug_stream, token);
1060 putc ('\n', cp_lexer_debug_stream);
1063 return token;
1066 /* Permanently remove the next token from the token stream, and
1067 advance the next_token pointer to refer to the next non-purged
1068 token. */
1070 static void
1071 cp_lexer_purge_token (cp_lexer *lexer)
1073 cp_token *tok = lexer->next_token;
1075 gcc_assert (tok != &eof_token);
1076 tok->purged_p = true;
1077 tok->location = UNKNOWN_LOCATION;
1078 tok->u.value = NULL_TREE;
1079 tok->keyword = RID_MAX;
1083 tok++;
1084 if (tok == lexer->last_token)
1086 tok = &eof_token;
1087 break;
1090 while (tok->purged_p);
1091 lexer->next_token = tok;
1094 /* Permanently remove all tokens after TOK, up to, but not
1095 including, the token that will be returned next by
1096 cp_lexer_peek_token. */
1098 static void
1099 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1101 cp_token *peek = lexer->next_token;
1103 if (peek == &eof_token)
1104 peek = lexer->last_token;
1106 gcc_assert (tok < peek);
1108 for ( tok += 1; tok != peek; tok += 1)
1110 tok->purged_p = true;
1111 tok->location = UNKNOWN_LOCATION;
1112 tok->u.value = NULL_TREE;
1113 tok->keyword = RID_MAX;
1117 /* Begin saving tokens. All tokens consumed after this point will be
1118 preserved. */
1120 static void
1121 cp_lexer_save_tokens (cp_lexer* lexer)
1123 /* Provide debugging output. */
1124 if (cp_lexer_debugging_p (lexer))
1125 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1127 lexer->saved_tokens.safe_push (lexer->next_token);
1130 /* Commit to the portion of the token stream most recently saved. */
1132 static void
1133 cp_lexer_commit_tokens (cp_lexer* lexer)
1135 /* Provide debugging output. */
1136 if (cp_lexer_debugging_p (lexer))
1137 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1139 lexer->saved_tokens.pop ();
1142 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1143 to the token stream. Stop saving tokens. */
1145 static void
1146 cp_lexer_rollback_tokens (cp_lexer* lexer)
1148 /* Provide debugging output. */
1149 if (cp_lexer_debugging_p (lexer))
1150 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1152 lexer->next_token = lexer->saved_tokens.pop ();
1155 /* Print a representation of the TOKEN on the STREAM. */
1157 static void
1158 cp_lexer_print_token (FILE * stream, cp_token *token)
1160 /* We don't use cpp_type2name here because the parser defines
1161 a few tokens of its own. */
1162 static const char *const token_names[] = {
1163 /* cpplib-defined token types */
1164 #define OP(e, s) #e,
1165 #define TK(e, s) #e,
1166 TTYPE_TABLE
1167 #undef OP
1168 #undef TK
1169 /* C++ parser token types - see "Manifest constants", above. */
1170 "KEYWORD",
1171 "TEMPLATE_ID",
1172 "NESTED_NAME_SPECIFIER",
1175 /* For some tokens, print the associated data. */
1176 switch (token->type)
1178 case CPP_KEYWORD:
1179 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1180 For example, `struct' is mapped to an INTEGER_CST. */
1181 if (!identifier_p (token->u.value))
1182 break;
1183 /* else fall through */
1184 case CPP_NAME:
1185 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1186 break;
1188 case CPP_STRING:
1189 case CPP_STRING16:
1190 case CPP_STRING32:
1191 case CPP_WSTRING:
1192 case CPP_UTF8STRING:
1193 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1194 break;
1196 case CPP_NUMBER:
1197 print_generic_expr (stream, token->u.value, 0);
1198 break;
1200 default:
1201 /* If we have a name for the token, print it out. Otherwise, we
1202 simply give the numeric code. */
1203 if (token->type < ARRAY_SIZE(token_names))
1204 fputs (token_names[token->type], stream);
1205 else
1206 fprintf (stream, "[%d]", token->type);
1207 break;
1211 DEBUG_FUNCTION void
1212 debug (cp_token &ref)
1214 cp_lexer_print_token (stderr, &ref);
1215 fprintf (stderr, "\n");
1218 DEBUG_FUNCTION void
1219 debug (cp_token *ptr)
1221 if (ptr)
1222 debug (*ptr);
1223 else
1224 fprintf (stderr, "<nil>\n");
1228 /* Start emitting debugging information. */
1230 static void
1231 cp_lexer_start_debugging (cp_lexer* lexer)
1233 lexer->debugging_p = true;
1234 cp_lexer_debug_stream = stderr;
1237 /* Stop emitting debugging information. */
1239 static void
1240 cp_lexer_stop_debugging (cp_lexer* lexer)
1242 lexer->debugging_p = false;
1243 cp_lexer_debug_stream = NULL;
1246 /* Create a new cp_token_cache, representing a range of tokens. */
1248 static cp_token_cache *
1249 cp_token_cache_new (cp_token *first, cp_token *last)
1251 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1252 cache->first = first;
1253 cache->last = last;
1254 return cache;
1257 /* Diagnose if #pragma omp declare simd isn't followed immediately
1258 by function declaration or definition. */
1260 static inline void
1261 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1263 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1265 error ("%<#pragma omp declare simd%> not immediately followed by "
1266 "function declaration or definition");
1267 parser->omp_declare_simd = NULL;
1271 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1272 and put that into "omp declare simd" attribute. */
1274 static inline void
1275 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1277 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1279 if (fndecl == error_mark_node)
1281 parser->omp_declare_simd = NULL;
1282 return;
1284 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1286 cp_ensure_no_omp_declare_simd (parser);
1287 return;
1292 /* Decl-specifiers. */
1294 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1296 static void
1297 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1299 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1302 /* Declarators. */
1304 /* Nothing other than the parser should be creating declarators;
1305 declarators are a semi-syntactic representation of C++ entities.
1306 Other parts of the front end that need to create entities (like
1307 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1309 static cp_declarator *make_call_declarator
1310 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1311 static cp_declarator *make_array_declarator
1312 (cp_declarator *, tree);
1313 static cp_declarator *make_pointer_declarator
1314 (cp_cv_quals, cp_declarator *, tree);
1315 static cp_declarator *make_reference_declarator
1316 (cp_cv_quals, cp_declarator *, bool, tree);
1317 static cp_parameter_declarator *make_parameter_declarator
1318 (cp_decl_specifier_seq *, cp_declarator *, tree);
1319 static cp_declarator *make_ptrmem_declarator
1320 (cp_cv_quals, tree, cp_declarator *, tree);
1322 /* An erroneous declarator. */
1323 static cp_declarator *cp_error_declarator;
1325 /* The obstack on which declarators and related data structures are
1326 allocated. */
1327 static struct obstack declarator_obstack;
1329 /* Alloc BYTES from the declarator memory pool. */
1331 static inline void *
1332 alloc_declarator (size_t bytes)
1334 return obstack_alloc (&declarator_obstack, bytes);
1337 /* Allocate a declarator of the indicated KIND. Clear fields that are
1338 common to all declarators. */
1340 static cp_declarator *
1341 make_declarator (cp_declarator_kind kind)
1343 cp_declarator *declarator;
1345 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1346 declarator->kind = kind;
1347 declarator->attributes = NULL_TREE;
1348 declarator->std_attributes = NULL_TREE;
1349 declarator->declarator = NULL;
1350 declarator->parameter_pack_p = false;
1351 declarator->id_loc = UNKNOWN_LOCATION;
1353 return declarator;
1356 /* Make a declarator for a generalized identifier. If
1357 QUALIFYING_SCOPE is non-NULL, the identifier is
1358 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1359 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1360 is, if any. */
1362 static cp_declarator *
1363 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1364 special_function_kind sfk)
1366 cp_declarator *declarator;
1368 /* It is valid to write:
1370 class C { void f(); };
1371 typedef C D;
1372 void D::f();
1374 The standard is not clear about whether `typedef const C D' is
1375 legal; as of 2002-09-15 the committee is considering that
1376 question. EDG 3.0 allows that syntax. Therefore, we do as
1377 well. */
1378 if (qualifying_scope && TYPE_P (qualifying_scope))
1379 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1381 gcc_assert (identifier_p (unqualified_name)
1382 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1383 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1385 declarator = make_declarator (cdk_id);
1386 declarator->u.id.qualifying_scope = qualifying_scope;
1387 declarator->u.id.unqualified_name = unqualified_name;
1388 declarator->u.id.sfk = sfk;
1390 return declarator;
1393 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1394 of modifiers such as const or volatile to apply to the pointer
1395 type, represented as identifiers. ATTRIBUTES represent the attributes that
1396 appertain to the pointer or reference. */
1398 cp_declarator *
1399 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1400 tree attributes)
1402 cp_declarator *declarator;
1404 declarator = make_declarator (cdk_pointer);
1405 declarator->declarator = target;
1406 declarator->u.pointer.qualifiers = cv_qualifiers;
1407 declarator->u.pointer.class_type = NULL_TREE;
1408 if (target)
1410 declarator->id_loc = target->id_loc;
1411 declarator->parameter_pack_p = target->parameter_pack_p;
1412 target->parameter_pack_p = false;
1414 else
1415 declarator->parameter_pack_p = false;
1417 declarator->std_attributes = attributes;
1419 return declarator;
1422 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1423 represent the attributes that appertain to the pointer or
1424 reference. */
1426 cp_declarator *
1427 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1428 bool rvalue_ref, tree attributes)
1430 cp_declarator *declarator;
1432 declarator = make_declarator (cdk_reference);
1433 declarator->declarator = target;
1434 declarator->u.reference.qualifiers = cv_qualifiers;
1435 declarator->u.reference.rvalue_ref = rvalue_ref;
1436 if (target)
1438 declarator->id_loc = target->id_loc;
1439 declarator->parameter_pack_p = target->parameter_pack_p;
1440 target->parameter_pack_p = false;
1442 else
1443 declarator->parameter_pack_p = false;
1445 declarator->std_attributes = attributes;
1447 return declarator;
1450 /* Like make_pointer_declarator -- but for a pointer to a non-static
1451 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1452 appertain to the pointer or reference. */
1454 cp_declarator *
1455 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1456 cp_declarator *pointee,
1457 tree attributes)
1459 cp_declarator *declarator;
1461 declarator = make_declarator (cdk_ptrmem);
1462 declarator->declarator = pointee;
1463 declarator->u.pointer.qualifiers = cv_qualifiers;
1464 declarator->u.pointer.class_type = class_type;
1466 if (pointee)
1468 declarator->parameter_pack_p = pointee->parameter_pack_p;
1469 pointee->parameter_pack_p = false;
1471 else
1472 declarator->parameter_pack_p = false;
1474 declarator->std_attributes = attributes;
1476 return declarator;
1479 /* Make a declarator for the function given by TARGET, with the
1480 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1481 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1482 indicates what exceptions can be thrown. */
1484 cp_declarator *
1485 make_call_declarator (cp_declarator *target,
1486 tree parms,
1487 cp_cv_quals cv_qualifiers,
1488 cp_virt_specifiers virt_specifiers,
1489 cp_ref_qualifier ref_qualifier,
1490 tree exception_specification,
1491 tree late_return_type)
1493 cp_declarator *declarator;
1495 declarator = make_declarator (cdk_function);
1496 declarator->declarator = target;
1497 declarator->u.function.parameters = parms;
1498 declarator->u.function.qualifiers = cv_qualifiers;
1499 declarator->u.function.virt_specifiers = virt_specifiers;
1500 declarator->u.function.ref_qualifier = ref_qualifier;
1501 declarator->u.function.exception_specification = exception_specification;
1502 declarator->u.function.late_return_type = late_return_type;
1503 if (target)
1505 declarator->id_loc = target->id_loc;
1506 declarator->parameter_pack_p = target->parameter_pack_p;
1507 target->parameter_pack_p = false;
1509 else
1510 declarator->parameter_pack_p = false;
1512 return declarator;
1515 /* Make a declarator for an array of BOUNDS elements, each of which is
1516 defined by ELEMENT. */
1518 cp_declarator *
1519 make_array_declarator (cp_declarator *element, tree bounds)
1521 cp_declarator *declarator;
1523 declarator = make_declarator (cdk_array);
1524 declarator->declarator = element;
1525 declarator->u.array.bounds = bounds;
1526 if (element)
1528 declarator->id_loc = element->id_loc;
1529 declarator->parameter_pack_p = element->parameter_pack_p;
1530 element->parameter_pack_p = false;
1532 else
1533 declarator->parameter_pack_p = false;
1535 return declarator;
1538 /* Determine whether the declarator we've seen so far can be a
1539 parameter pack, when followed by an ellipsis. */
1540 static bool
1541 declarator_can_be_parameter_pack (cp_declarator *declarator)
1543 /* Search for a declarator name, or any other declarator that goes
1544 after the point where the ellipsis could appear in a parameter
1545 pack. If we find any of these, then this declarator can not be
1546 made into a parameter pack. */
1547 bool found = false;
1548 while (declarator && !found)
1550 switch ((int)declarator->kind)
1552 case cdk_id:
1553 case cdk_array:
1554 found = true;
1555 break;
1557 case cdk_error:
1558 return true;
1560 default:
1561 declarator = declarator->declarator;
1562 break;
1566 return !found;
1569 cp_parameter_declarator *no_parameters;
1571 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1572 DECLARATOR and DEFAULT_ARGUMENT. */
1574 cp_parameter_declarator *
1575 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1576 cp_declarator *declarator,
1577 tree default_argument)
1579 cp_parameter_declarator *parameter;
1581 parameter = ((cp_parameter_declarator *)
1582 alloc_declarator (sizeof (cp_parameter_declarator)));
1583 parameter->next = NULL;
1584 if (decl_specifiers)
1585 parameter->decl_specifiers = *decl_specifiers;
1586 else
1587 clear_decl_specs (&parameter->decl_specifiers);
1588 parameter->declarator = declarator;
1589 parameter->default_argument = default_argument;
1590 parameter->ellipsis_p = false;
1592 return parameter;
1595 /* Returns true iff DECLARATOR is a declaration for a function. */
1597 static bool
1598 function_declarator_p (const cp_declarator *declarator)
1600 while (declarator)
1602 if (declarator->kind == cdk_function
1603 && declarator->declarator->kind == cdk_id)
1604 return true;
1605 if (declarator->kind == cdk_id
1606 || declarator->kind == cdk_error)
1607 return false;
1608 declarator = declarator->declarator;
1610 return false;
1613 /* The parser. */
1615 /* Overview
1616 --------
1618 A cp_parser parses the token stream as specified by the C++
1619 grammar. Its job is purely parsing, not semantic analysis. For
1620 example, the parser breaks the token stream into declarators,
1621 expressions, statements, and other similar syntactic constructs.
1622 It does not check that the types of the expressions on either side
1623 of an assignment-statement are compatible, or that a function is
1624 not declared with a parameter of type `void'.
1626 The parser invokes routines elsewhere in the compiler to perform
1627 semantic analysis and to build up the abstract syntax tree for the
1628 code processed.
1630 The parser (and the template instantiation code, which is, in a
1631 way, a close relative of parsing) are the only parts of the
1632 compiler that should be calling push_scope and pop_scope, or
1633 related functions. The parser (and template instantiation code)
1634 keeps track of what scope is presently active; everything else
1635 should simply honor that. (The code that generates static
1636 initializers may also need to set the scope, in order to check
1637 access control correctly when emitting the initializers.)
1639 Methodology
1640 -----------
1642 The parser is of the standard recursive-descent variety. Upcoming
1643 tokens in the token stream are examined in order to determine which
1644 production to use when parsing a non-terminal. Some C++ constructs
1645 require arbitrary look ahead to disambiguate. For example, it is
1646 impossible, in the general case, to tell whether a statement is an
1647 expression or declaration without scanning the entire statement.
1648 Therefore, the parser is capable of "parsing tentatively." When the
1649 parser is not sure what construct comes next, it enters this mode.
1650 Then, while we attempt to parse the construct, the parser queues up
1651 error messages, rather than issuing them immediately, and saves the
1652 tokens it consumes. If the construct is parsed successfully, the
1653 parser "commits", i.e., it issues any queued error messages and
1654 the tokens that were being preserved are permanently discarded.
1655 If, however, the construct is not parsed successfully, the parser
1656 rolls back its state completely so that it can resume parsing using
1657 a different alternative.
1659 Future Improvements
1660 -------------------
1662 The performance of the parser could probably be improved substantially.
1663 We could often eliminate the need to parse tentatively by looking ahead
1664 a little bit. In some places, this approach might not entirely eliminate
1665 the need to parse tentatively, but it might still speed up the average
1666 case. */
1668 /* Flags that are passed to some parsing functions. These values can
1669 be bitwise-ored together. */
1671 enum
1673 /* No flags. */
1674 CP_PARSER_FLAGS_NONE = 0x0,
1675 /* The construct is optional. If it is not present, then no error
1676 should be issued. */
1677 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1678 /* When parsing a type-specifier, treat user-defined type-names
1679 as non-type identifiers. */
1680 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1681 /* When parsing a type-specifier, do not try to parse a class-specifier
1682 or enum-specifier. */
1683 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1684 /* When parsing a decl-specifier-seq, only allow type-specifier or
1685 constexpr. */
1686 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1689 /* This type is used for parameters and variables which hold
1690 combinations of the above flags. */
1691 typedef int cp_parser_flags;
1693 /* The different kinds of declarators we want to parse. */
1695 typedef enum cp_parser_declarator_kind
1697 /* We want an abstract declarator. */
1698 CP_PARSER_DECLARATOR_ABSTRACT,
1699 /* We want a named declarator. */
1700 CP_PARSER_DECLARATOR_NAMED,
1701 /* We don't mind, but the name must be an unqualified-id. */
1702 CP_PARSER_DECLARATOR_EITHER
1703 } cp_parser_declarator_kind;
1705 /* The precedence values used to parse binary expressions. The minimum value
1706 of PREC must be 1, because zero is reserved to quickly discriminate
1707 binary operators from other tokens. */
1709 enum cp_parser_prec
1711 PREC_NOT_OPERATOR,
1712 PREC_LOGICAL_OR_EXPRESSION,
1713 PREC_LOGICAL_AND_EXPRESSION,
1714 PREC_INCLUSIVE_OR_EXPRESSION,
1715 PREC_EXCLUSIVE_OR_EXPRESSION,
1716 PREC_AND_EXPRESSION,
1717 PREC_EQUALITY_EXPRESSION,
1718 PREC_RELATIONAL_EXPRESSION,
1719 PREC_SHIFT_EXPRESSION,
1720 PREC_ADDITIVE_EXPRESSION,
1721 PREC_MULTIPLICATIVE_EXPRESSION,
1722 PREC_PM_EXPRESSION,
1723 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1726 /* A mapping from a token type to a corresponding tree node type, with a
1727 precedence value. */
1729 typedef struct cp_parser_binary_operations_map_node
1731 /* The token type. */
1732 enum cpp_ttype token_type;
1733 /* The corresponding tree code. */
1734 enum tree_code tree_type;
1735 /* The precedence of this operator. */
1736 enum cp_parser_prec prec;
1737 } cp_parser_binary_operations_map_node;
1739 typedef struct cp_parser_expression_stack_entry
1741 /* Left hand side of the binary operation we are currently
1742 parsing. */
1743 tree lhs;
1744 /* Original tree code for left hand side, if it was a binary
1745 expression itself (used for -Wparentheses). */
1746 enum tree_code lhs_type;
1747 /* Tree code for the binary operation we are parsing. */
1748 enum tree_code tree_type;
1749 /* Precedence of the binary operation we are parsing. */
1750 enum cp_parser_prec prec;
1751 /* Location of the binary operation we are parsing. */
1752 location_t loc;
1753 } cp_parser_expression_stack_entry;
1755 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1756 entries because precedence levels on the stack are monotonically
1757 increasing. */
1758 typedef struct cp_parser_expression_stack_entry
1759 cp_parser_expression_stack[NUM_PREC_VALUES];
1761 /* Prototypes. */
1763 /* Constructors and destructors. */
1765 static cp_parser_context *cp_parser_context_new
1766 (cp_parser_context *);
1768 /* Class variables. */
1770 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1772 /* The operator-precedence table used by cp_parser_binary_expression.
1773 Transformed into an associative array (binops_by_token) by
1774 cp_parser_new. */
1776 static const cp_parser_binary_operations_map_node binops[] = {
1777 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1778 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1780 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1781 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1782 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1784 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1785 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1787 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1788 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1790 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1791 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1792 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1793 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1795 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1796 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1798 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1800 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1802 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1804 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1806 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1809 /* The same as binops, but initialized by cp_parser_new so that
1810 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1811 for speed. */
1812 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1814 /* Constructors and destructors. */
1816 /* Construct a new context. The context below this one on the stack
1817 is given by NEXT. */
1819 static cp_parser_context *
1820 cp_parser_context_new (cp_parser_context* next)
1822 cp_parser_context *context;
1824 /* Allocate the storage. */
1825 if (cp_parser_context_free_list != NULL)
1827 /* Pull the first entry from the free list. */
1828 context = cp_parser_context_free_list;
1829 cp_parser_context_free_list = context->next;
1830 memset (context, 0, sizeof (*context));
1832 else
1833 context = ggc_cleared_alloc<cp_parser_context> ();
1835 /* No errors have occurred yet in this context. */
1836 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1837 /* If this is not the bottommost context, copy information that we
1838 need from the previous context. */
1839 if (next)
1841 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1842 expression, then we are parsing one in this context, too. */
1843 context->object_type = next->object_type;
1844 /* Thread the stack. */
1845 context->next = next;
1848 return context;
1851 /* Managing the unparsed function queues. */
1853 #define unparsed_funs_with_default_args \
1854 parser->unparsed_queues->last ().funs_with_default_args
1855 #define unparsed_funs_with_definitions \
1856 parser->unparsed_queues->last ().funs_with_definitions
1857 #define unparsed_nsdmis \
1858 parser->unparsed_queues->last ().nsdmis
1859 #define unparsed_classes \
1860 parser->unparsed_queues->last ().classes
1862 static void
1863 push_unparsed_function_queues (cp_parser *parser)
1865 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1866 vec_safe_push (parser->unparsed_queues, e);
1869 static void
1870 pop_unparsed_function_queues (cp_parser *parser)
1872 release_tree_vector (unparsed_funs_with_definitions);
1873 parser->unparsed_queues->pop ();
1876 /* Prototypes. */
1878 /* Constructors and destructors. */
1880 static cp_parser *cp_parser_new
1881 (void);
1883 /* Routines to parse various constructs.
1885 Those that return `tree' will return the error_mark_node (rather
1886 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1887 Sometimes, they will return an ordinary node if error-recovery was
1888 attempted, even though a parse error occurred. So, to check
1889 whether or not a parse error occurred, you should always use
1890 cp_parser_error_occurred. If the construct is optional (indicated
1891 either by an `_opt' in the name of the function that does the
1892 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1893 the construct is not present. */
1895 /* Lexical conventions [gram.lex] */
1897 static tree cp_parser_identifier
1898 (cp_parser *);
1899 static tree cp_parser_string_literal
1900 (cp_parser *, bool, bool, bool);
1901 static tree cp_parser_userdef_char_literal
1902 (cp_parser *);
1903 static tree cp_parser_userdef_string_literal
1904 (tree);
1905 static tree cp_parser_userdef_numeric_literal
1906 (cp_parser *);
1908 /* Basic concepts [gram.basic] */
1910 static bool cp_parser_translation_unit
1911 (cp_parser *);
1913 /* Expressions [gram.expr] */
1915 static tree cp_parser_primary_expression
1916 (cp_parser *, bool, bool, bool, cp_id_kind *);
1917 static tree cp_parser_id_expression
1918 (cp_parser *, bool, bool, bool *, bool, bool);
1919 static tree cp_parser_unqualified_id
1920 (cp_parser *, bool, bool, bool, bool);
1921 static tree cp_parser_nested_name_specifier_opt
1922 (cp_parser *, bool, bool, bool, bool);
1923 static tree cp_parser_nested_name_specifier
1924 (cp_parser *, bool, bool, bool, bool);
1925 static tree cp_parser_qualifying_entity
1926 (cp_parser *, bool, bool, bool, bool, bool);
1927 static tree cp_parser_postfix_expression
1928 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1929 static tree cp_parser_postfix_open_square_expression
1930 (cp_parser *, tree, bool, bool);
1931 static tree cp_parser_postfix_dot_deref_expression
1932 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1933 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1934 (cp_parser *, int, bool, bool, bool *, bool = false);
1935 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1936 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1937 static void cp_parser_pseudo_destructor_name
1938 (cp_parser *, tree, tree *, tree *);
1939 static tree cp_parser_unary_expression
1940 (cp_parser *, bool, bool, cp_id_kind *);
1941 static enum tree_code cp_parser_unary_operator
1942 (cp_token *);
1943 static tree cp_parser_new_expression
1944 (cp_parser *);
1945 static vec<tree, va_gc> *cp_parser_new_placement
1946 (cp_parser *);
1947 static tree cp_parser_new_type_id
1948 (cp_parser *, tree *);
1949 static cp_declarator *cp_parser_new_declarator_opt
1950 (cp_parser *);
1951 static cp_declarator *cp_parser_direct_new_declarator
1952 (cp_parser *);
1953 static vec<tree, va_gc> *cp_parser_new_initializer
1954 (cp_parser *);
1955 static tree cp_parser_delete_expression
1956 (cp_parser *);
1957 static tree cp_parser_cast_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_binary_expression
1960 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1961 static tree cp_parser_question_colon_clause
1962 (cp_parser *, tree);
1963 static tree cp_parser_assignment_expression
1964 (cp_parser *, bool, cp_id_kind *);
1965 static enum tree_code cp_parser_assignment_operator_opt
1966 (cp_parser *);
1967 static tree cp_parser_expression
1968 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
1969 static tree cp_parser_constant_expression
1970 (cp_parser *, bool, bool *);
1971 static tree cp_parser_builtin_offsetof
1972 (cp_parser *);
1973 static tree cp_parser_lambda_expression
1974 (cp_parser *);
1975 static void cp_parser_lambda_introducer
1976 (cp_parser *, tree);
1977 static bool cp_parser_lambda_declarator_opt
1978 (cp_parser *, tree);
1979 static void cp_parser_lambda_body
1980 (cp_parser *, tree);
1982 /* Statements [gram.stmt.stmt] */
1984 static void cp_parser_statement
1985 (cp_parser *, tree, bool, bool *);
1986 static void cp_parser_label_for_labeled_statement
1987 (cp_parser *, tree);
1988 static tree cp_parser_expression_statement
1989 (cp_parser *, tree);
1990 static tree cp_parser_compound_statement
1991 (cp_parser *, tree, bool, bool);
1992 static void cp_parser_statement_seq_opt
1993 (cp_parser *, tree);
1994 static tree cp_parser_selection_statement
1995 (cp_parser *, bool *);
1996 static tree cp_parser_condition
1997 (cp_parser *);
1998 static tree cp_parser_iteration_statement
1999 (cp_parser *, bool);
2000 static bool cp_parser_for_init_statement
2001 (cp_parser *, tree *decl);
2002 static tree cp_parser_for
2003 (cp_parser *, bool);
2004 static tree cp_parser_c_for
2005 (cp_parser *, tree, tree, bool);
2006 static tree cp_parser_range_for
2007 (cp_parser *, tree, tree, tree, bool);
2008 static void do_range_for_auto_deduction
2009 (tree, tree);
2010 static tree cp_parser_perform_range_for_lookup
2011 (tree, tree *, tree *);
2012 static tree cp_parser_range_for_member_function
2013 (tree, tree);
2014 static tree cp_parser_jump_statement
2015 (cp_parser *);
2016 static void cp_parser_declaration_statement
2017 (cp_parser *);
2019 static tree cp_parser_implicitly_scoped_statement
2020 (cp_parser *, bool *);
2021 static void cp_parser_already_scoped_statement
2022 (cp_parser *);
2024 /* Declarations [gram.dcl.dcl] */
2026 static void cp_parser_declaration_seq_opt
2027 (cp_parser *);
2028 static void cp_parser_declaration
2029 (cp_parser *);
2030 static void cp_parser_block_declaration
2031 (cp_parser *, bool);
2032 static void cp_parser_simple_declaration
2033 (cp_parser *, bool, tree *);
2034 static void cp_parser_decl_specifier_seq
2035 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2036 static tree cp_parser_storage_class_specifier_opt
2037 (cp_parser *);
2038 static tree cp_parser_function_specifier_opt
2039 (cp_parser *, cp_decl_specifier_seq *);
2040 static tree cp_parser_type_specifier
2041 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2042 int *, bool *);
2043 static tree cp_parser_simple_type_specifier
2044 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2045 static tree cp_parser_type_name
2046 (cp_parser *);
2047 static tree cp_parser_nonclass_name
2048 (cp_parser* parser);
2049 static tree cp_parser_elaborated_type_specifier
2050 (cp_parser *, bool, bool);
2051 static tree cp_parser_enum_specifier
2052 (cp_parser *);
2053 static void cp_parser_enumerator_list
2054 (cp_parser *, tree);
2055 static void cp_parser_enumerator_definition
2056 (cp_parser *, tree);
2057 static tree cp_parser_namespace_name
2058 (cp_parser *);
2059 static void cp_parser_namespace_definition
2060 (cp_parser *);
2061 static void cp_parser_namespace_body
2062 (cp_parser *);
2063 static tree cp_parser_qualified_namespace_specifier
2064 (cp_parser *);
2065 static void cp_parser_namespace_alias_definition
2066 (cp_parser *);
2067 static bool cp_parser_using_declaration
2068 (cp_parser *, bool);
2069 static void cp_parser_using_directive
2070 (cp_parser *);
2071 static tree cp_parser_alias_declaration
2072 (cp_parser *);
2073 static void cp_parser_asm_definition
2074 (cp_parser *);
2075 static void cp_parser_linkage_specification
2076 (cp_parser *);
2077 static void cp_parser_static_assert
2078 (cp_parser *, bool);
2079 static tree cp_parser_decltype
2080 (cp_parser *);
2082 /* Declarators [gram.dcl.decl] */
2084 static tree cp_parser_init_declarator
2085 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2086 static cp_declarator *cp_parser_declarator
2087 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2088 static cp_declarator *cp_parser_direct_declarator
2089 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2090 static enum tree_code cp_parser_ptr_operator
2091 (cp_parser *, tree *, cp_cv_quals *, tree *);
2092 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2093 (cp_parser *);
2094 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2095 (cp_parser *);
2096 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2097 (cp_parser *);
2098 static tree cp_parser_late_return_type_opt
2099 (cp_parser *, cp_declarator *, cp_cv_quals);
2100 static tree cp_parser_declarator_id
2101 (cp_parser *, bool);
2102 static tree cp_parser_type_id
2103 (cp_parser *);
2104 static tree cp_parser_template_type_arg
2105 (cp_parser *);
2106 static tree cp_parser_trailing_type_id (cp_parser *);
2107 static tree cp_parser_type_id_1
2108 (cp_parser *, bool, bool);
2109 static void cp_parser_type_specifier_seq
2110 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2111 static tree cp_parser_parameter_declaration_clause
2112 (cp_parser *);
2113 static tree cp_parser_parameter_declaration_list
2114 (cp_parser *, bool *);
2115 static cp_parameter_declarator *cp_parser_parameter_declaration
2116 (cp_parser *, bool, bool *);
2117 static tree cp_parser_default_argument
2118 (cp_parser *, bool);
2119 static void cp_parser_function_body
2120 (cp_parser *, bool);
2121 static tree cp_parser_initializer
2122 (cp_parser *, bool *, bool *);
2123 static tree cp_parser_initializer_clause
2124 (cp_parser *, bool *);
2125 static tree cp_parser_braced_list
2126 (cp_parser*, bool*);
2127 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2128 (cp_parser *, bool *);
2130 static bool cp_parser_ctor_initializer_opt_and_function_body
2131 (cp_parser *, bool);
2133 static tree cp_parser_late_parsing_omp_declare_simd
2134 (cp_parser *, tree);
2136 static tree cp_parser_late_parsing_cilk_simd_fn_info
2137 (cp_parser *, tree);
2139 static tree synthesize_implicit_template_parm
2140 (cp_parser *);
2141 static tree finish_fully_implicit_template
2142 (cp_parser *, tree);
2144 /* Classes [gram.class] */
2146 static tree cp_parser_class_name
2147 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2148 static tree cp_parser_class_specifier
2149 (cp_parser *);
2150 static tree cp_parser_class_head
2151 (cp_parser *, bool *);
2152 static enum tag_types cp_parser_class_key
2153 (cp_parser *);
2154 static void cp_parser_type_parameter_key
2155 (cp_parser* parser);
2156 static void cp_parser_member_specification_opt
2157 (cp_parser *);
2158 static void cp_parser_member_declaration
2159 (cp_parser *);
2160 static tree cp_parser_pure_specifier
2161 (cp_parser *);
2162 static tree cp_parser_constant_initializer
2163 (cp_parser *);
2165 /* Derived classes [gram.class.derived] */
2167 static tree cp_parser_base_clause
2168 (cp_parser *);
2169 static tree cp_parser_base_specifier
2170 (cp_parser *);
2172 /* Special member functions [gram.special] */
2174 static tree cp_parser_conversion_function_id
2175 (cp_parser *);
2176 static tree cp_parser_conversion_type_id
2177 (cp_parser *);
2178 static cp_declarator *cp_parser_conversion_declarator_opt
2179 (cp_parser *);
2180 static bool cp_parser_ctor_initializer_opt
2181 (cp_parser *);
2182 static void cp_parser_mem_initializer_list
2183 (cp_parser *);
2184 static tree cp_parser_mem_initializer
2185 (cp_parser *);
2186 static tree cp_parser_mem_initializer_id
2187 (cp_parser *);
2189 /* Overloading [gram.over] */
2191 static tree cp_parser_operator_function_id
2192 (cp_parser *);
2193 static tree cp_parser_operator
2194 (cp_parser *);
2196 /* Templates [gram.temp] */
2198 static void cp_parser_template_declaration
2199 (cp_parser *, bool);
2200 static tree cp_parser_template_parameter_list
2201 (cp_parser *);
2202 static tree cp_parser_template_parameter
2203 (cp_parser *, bool *, bool *);
2204 static tree cp_parser_type_parameter
2205 (cp_parser *, bool *);
2206 static tree cp_parser_template_id
2207 (cp_parser *, bool, bool, enum tag_types, bool);
2208 static tree cp_parser_template_name
2209 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2210 static tree cp_parser_template_argument_list
2211 (cp_parser *);
2212 static tree cp_parser_template_argument
2213 (cp_parser *);
2214 static void cp_parser_explicit_instantiation
2215 (cp_parser *);
2216 static void cp_parser_explicit_specialization
2217 (cp_parser *);
2219 /* Exception handling [gram.exception] */
2221 static tree cp_parser_try_block
2222 (cp_parser *);
2223 static bool cp_parser_function_try_block
2224 (cp_parser *);
2225 static void cp_parser_handler_seq
2226 (cp_parser *);
2227 static void cp_parser_handler
2228 (cp_parser *);
2229 static tree cp_parser_exception_declaration
2230 (cp_parser *);
2231 static tree cp_parser_throw_expression
2232 (cp_parser *);
2233 static tree cp_parser_exception_specification_opt
2234 (cp_parser *);
2235 static tree cp_parser_type_id_list
2236 (cp_parser *);
2238 /* GNU Extensions */
2240 static tree cp_parser_asm_specification_opt
2241 (cp_parser *);
2242 static tree cp_parser_asm_operand_list
2243 (cp_parser *);
2244 static tree cp_parser_asm_clobber_list
2245 (cp_parser *);
2246 static tree cp_parser_asm_label_list
2247 (cp_parser *);
2248 static bool cp_next_tokens_can_be_attribute_p
2249 (cp_parser *);
2250 static bool cp_next_tokens_can_be_gnu_attribute_p
2251 (cp_parser *);
2252 static bool cp_next_tokens_can_be_std_attribute_p
2253 (cp_parser *);
2254 static bool cp_nth_tokens_can_be_std_attribute_p
2255 (cp_parser *, size_t);
2256 static bool cp_nth_tokens_can_be_gnu_attribute_p
2257 (cp_parser *, size_t);
2258 static bool cp_nth_tokens_can_be_attribute_p
2259 (cp_parser *, size_t);
2260 static tree cp_parser_attributes_opt
2261 (cp_parser *);
2262 static tree cp_parser_gnu_attributes_opt
2263 (cp_parser *);
2264 static tree cp_parser_gnu_attribute_list
2265 (cp_parser *);
2266 static tree cp_parser_std_attribute
2267 (cp_parser *);
2268 static tree cp_parser_std_attribute_spec
2269 (cp_parser *);
2270 static tree cp_parser_std_attribute_spec_seq
2271 (cp_parser *);
2272 static bool cp_parser_extension_opt
2273 (cp_parser *, int *);
2274 static void cp_parser_label_declaration
2275 (cp_parser *);
2277 /* Transactional Memory Extensions */
2279 static tree cp_parser_transaction
2280 (cp_parser *, enum rid);
2281 static tree cp_parser_transaction_expression
2282 (cp_parser *, enum rid);
2283 static bool cp_parser_function_transaction
2284 (cp_parser *, enum rid);
2285 static tree cp_parser_transaction_cancel
2286 (cp_parser *);
2288 enum pragma_context {
2289 pragma_external,
2290 pragma_member,
2291 pragma_objc_icode,
2292 pragma_stmt,
2293 pragma_compound
2295 static bool cp_parser_pragma
2296 (cp_parser *, enum pragma_context);
2298 /* Objective-C++ Productions */
2300 static tree cp_parser_objc_message_receiver
2301 (cp_parser *);
2302 static tree cp_parser_objc_message_args
2303 (cp_parser *);
2304 static tree cp_parser_objc_message_expression
2305 (cp_parser *);
2306 static tree cp_parser_objc_encode_expression
2307 (cp_parser *);
2308 static tree cp_parser_objc_defs_expression
2309 (cp_parser *);
2310 static tree cp_parser_objc_protocol_expression
2311 (cp_parser *);
2312 static tree cp_parser_objc_selector_expression
2313 (cp_parser *);
2314 static tree cp_parser_objc_expression
2315 (cp_parser *);
2316 static bool cp_parser_objc_selector_p
2317 (enum cpp_ttype);
2318 static tree cp_parser_objc_selector
2319 (cp_parser *);
2320 static tree cp_parser_objc_protocol_refs_opt
2321 (cp_parser *);
2322 static void cp_parser_objc_declaration
2323 (cp_parser *, tree);
2324 static tree cp_parser_objc_statement
2325 (cp_parser *);
2326 static bool cp_parser_objc_valid_prefix_attributes
2327 (cp_parser *, tree *);
2328 static void cp_parser_objc_at_property_declaration
2329 (cp_parser *) ;
2330 static void cp_parser_objc_at_synthesize_declaration
2331 (cp_parser *) ;
2332 static void cp_parser_objc_at_dynamic_declaration
2333 (cp_parser *) ;
2334 static tree cp_parser_objc_struct_declaration
2335 (cp_parser *) ;
2337 /* Utility Routines */
2339 static tree cp_parser_lookup_name
2340 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2341 static tree cp_parser_lookup_name_simple
2342 (cp_parser *, tree, location_t);
2343 static tree cp_parser_maybe_treat_template_as_class
2344 (tree, bool);
2345 static bool cp_parser_check_declarator_template_parameters
2346 (cp_parser *, cp_declarator *, location_t);
2347 static bool cp_parser_check_template_parameters
2348 (cp_parser *, unsigned, location_t, cp_declarator *);
2349 static tree cp_parser_simple_cast_expression
2350 (cp_parser *);
2351 static tree cp_parser_global_scope_opt
2352 (cp_parser *, bool);
2353 static bool cp_parser_constructor_declarator_p
2354 (cp_parser *, bool);
2355 static tree cp_parser_function_definition_from_specifiers_and_declarator
2356 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2357 static tree cp_parser_function_definition_after_declarator
2358 (cp_parser *, bool);
2359 static void cp_parser_template_declaration_after_export
2360 (cp_parser *, bool);
2361 static void cp_parser_perform_template_parameter_access_checks
2362 (vec<deferred_access_check, va_gc> *);
2363 static tree cp_parser_single_declaration
2364 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2365 static tree cp_parser_functional_cast
2366 (cp_parser *, tree);
2367 static tree cp_parser_save_member_function_body
2368 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2369 static tree cp_parser_save_nsdmi
2370 (cp_parser *);
2371 static tree cp_parser_enclosed_template_argument_list
2372 (cp_parser *);
2373 static void cp_parser_save_default_args
2374 (cp_parser *, tree);
2375 static void cp_parser_late_parsing_for_member
2376 (cp_parser *, tree);
2377 static tree cp_parser_late_parse_one_default_arg
2378 (cp_parser *, tree, tree, tree);
2379 static void cp_parser_late_parsing_nsdmi
2380 (cp_parser *, tree);
2381 static void cp_parser_late_parsing_default_args
2382 (cp_parser *, tree);
2383 static tree cp_parser_sizeof_operand
2384 (cp_parser *, enum rid);
2385 static tree cp_parser_trait_expr
2386 (cp_parser *, enum rid);
2387 static bool cp_parser_declares_only_class_p
2388 (cp_parser *);
2389 static void cp_parser_set_storage_class
2390 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2391 static void cp_parser_set_decl_spec_type
2392 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2393 static void set_and_check_decl_spec_loc
2394 (cp_decl_specifier_seq *decl_specs,
2395 cp_decl_spec ds, cp_token *);
2396 static bool cp_parser_friend_p
2397 (const cp_decl_specifier_seq *);
2398 static void cp_parser_required_error
2399 (cp_parser *, required_token, bool);
2400 static cp_token *cp_parser_require
2401 (cp_parser *, enum cpp_ttype, required_token);
2402 static cp_token *cp_parser_require_keyword
2403 (cp_parser *, enum rid, required_token);
2404 static bool cp_parser_token_starts_function_definition_p
2405 (cp_token *);
2406 static bool cp_parser_next_token_starts_class_definition_p
2407 (cp_parser *);
2408 static bool cp_parser_next_token_ends_template_argument_p
2409 (cp_parser *);
2410 static bool cp_parser_nth_token_starts_template_argument_list_p
2411 (cp_parser *, size_t);
2412 static enum tag_types cp_parser_token_is_class_key
2413 (cp_token *);
2414 static enum tag_types cp_parser_token_is_type_parameter_key
2415 (cp_token *);
2416 static void cp_parser_check_class_key
2417 (enum tag_types, tree type);
2418 static void cp_parser_check_access_in_redeclaration
2419 (tree type, location_t location);
2420 static bool cp_parser_optional_template_keyword
2421 (cp_parser *);
2422 static void cp_parser_pre_parsed_nested_name_specifier
2423 (cp_parser *);
2424 static bool cp_parser_cache_group
2425 (cp_parser *, enum cpp_ttype, unsigned);
2426 static tree cp_parser_cache_defarg
2427 (cp_parser *parser, bool nsdmi);
2428 static void cp_parser_parse_tentatively
2429 (cp_parser *);
2430 static void cp_parser_commit_to_tentative_parse
2431 (cp_parser *);
2432 static void cp_parser_commit_to_topmost_tentative_parse
2433 (cp_parser *);
2434 static void cp_parser_abort_tentative_parse
2435 (cp_parser *);
2436 static bool cp_parser_parse_definitely
2437 (cp_parser *);
2438 static inline bool cp_parser_parsing_tentatively
2439 (cp_parser *);
2440 static bool cp_parser_uncommitted_to_tentative_parse_p
2441 (cp_parser *);
2442 static void cp_parser_error
2443 (cp_parser *, const char *);
2444 static void cp_parser_name_lookup_error
2445 (cp_parser *, tree, tree, name_lookup_error, location_t);
2446 static bool cp_parser_simulate_error
2447 (cp_parser *);
2448 static bool cp_parser_check_type_definition
2449 (cp_parser *);
2450 static void cp_parser_check_for_definition_in_return_type
2451 (cp_declarator *, tree, location_t type_location);
2452 static void cp_parser_check_for_invalid_template_id
2453 (cp_parser *, tree, enum tag_types, location_t location);
2454 static bool cp_parser_non_integral_constant_expression
2455 (cp_parser *, non_integral_constant);
2456 static void cp_parser_diagnose_invalid_type_name
2457 (cp_parser *, tree, location_t);
2458 static bool cp_parser_parse_and_diagnose_invalid_type_name
2459 (cp_parser *);
2460 static int cp_parser_skip_to_closing_parenthesis
2461 (cp_parser *, bool, bool, bool);
2462 static void cp_parser_skip_to_end_of_statement
2463 (cp_parser *);
2464 static void cp_parser_consume_semicolon_at_end_of_statement
2465 (cp_parser *);
2466 static void cp_parser_skip_to_end_of_block_or_statement
2467 (cp_parser *);
2468 static bool cp_parser_skip_to_closing_brace
2469 (cp_parser *);
2470 static void cp_parser_skip_to_end_of_template_parameter_list
2471 (cp_parser *);
2472 static void cp_parser_skip_to_pragma_eol
2473 (cp_parser*, cp_token *);
2474 static bool cp_parser_error_occurred
2475 (cp_parser *);
2476 static bool cp_parser_allow_gnu_extensions_p
2477 (cp_parser *);
2478 static bool cp_parser_is_pure_string_literal
2479 (cp_token *);
2480 static bool cp_parser_is_string_literal
2481 (cp_token *);
2482 static bool cp_parser_is_keyword
2483 (cp_token *, enum rid);
2484 static tree cp_parser_make_typename_type
2485 (cp_parser *, tree, location_t location);
2486 static cp_declarator * cp_parser_make_indirect_declarator
2487 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2488 static bool cp_parser_compound_literal_p
2489 (cp_parser *);
2491 /* Returns nonzero if we are parsing tentatively. */
2493 static inline bool
2494 cp_parser_parsing_tentatively (cp_parser* parser)
2496 return parser->context->next != NULL;
2499 /* Returns nonzero if TOKEN is a string literal. */
2501 static bool
2502 cp_parser_is_pure_string_literal (cp_token* token)
2504 return (token->type == CPP_STRING ||
2505 token->type == CPP_STRING16 ||
2506 token->type == CPP_STRING32 ||
2507 token->type == CPP_WSTRING ||
2508 token->type == CPP_UTF8STRING);
2511 /* Returns nonzero if TOKEN is a string literal
2512 of a user-defined string literal. */
2514 static bool
2515 cp_parser_is_string_literal (cp_token* token)
2517 return (cp_parser_is_pure_string_literal (token) ||
2518 token->type == CPP_STRING_USERDEF ||
2519 token->type == CPP_STRING16_USERDEF ||
2520 token->type == CPP_STRING32_USERDEF ||
2521 token->type == CPP_WSTRING_USERDEF ||
2522 token->type == CPP_UTF8STRING_USERDEF);
2525 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2527 static bool
2528 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2530 return token->keyword == keyword;
2533 /* If not parsing tentatively, issue a diagnostic of the form
2534 FILE:LINE: MESSAGE before TOKEN
2535 where TOKEN is the next token in the input stream. MESSAGE
2536 (specified by the caller) is usually of the form "expected
2537 OTHER-TOKEN". */
2539 static void
2540 cp_parser_error (cp_parser* parser, const char* gmsgid)
2542 if (!cp_parser_simulate_error (parser))
2544 cp_token *token = cp_lexer_peek_token (parser->lexer);
2545 /* This diagnostic makes more sense if it is tagged to the line
2546 of the token we just peeked at. */
2547 cp_lexer_set_source_position_from_token (token);
2549 if (token->type == CPP_PRAGMA)
2551 error_at (token->location,
2552 "%<#pragma%> is not allowed here");
2553 cp_parser_skip_to_pragma_eol (parser, token);
2554 return;
2557 c_parse_error (gmsgid,
2558 /* Because c_parser_error does not understand
2559 CPP_KEYWORD, keywords are treated like
2560 identifiers. */
2561 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2562 token->u.value, token->flags);
2566 /* Issue an error about name-lookup failing. NAME is the
2567 IDENTIFIER_NODE DECL is the result of
2568 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2569 the thing that we hoped to find. */
2571 static void
2572 cp_parser_name_lookup_error (cp_parser* parser,
2573 tree name,
2574 tree decl,
2575 name_lookup_error desired,
2576 location_t location)
2578 /* If name lookup completely failed, tell the user that NAME was not
2579 declared. */
2580 if (decl == error_mark_node)
2582 if (parser->scope && parser->scope != global_namespace)
2583 error_at (location, "%<%E::%E%> has not been declared",
2584 parser->scope, name);
2585 else if (parser->scope == global_namespace)
2586 error_at (location, "%<::%E%> has not been declared", name);
2587 else if (parser->object_scope
2588 && !CLASS_TYPE_P (parser->object_scope))
2589 error_at (location, "request for member %qE in non-class type %qT",
2590 name, parser->object_scope);
2591 else if (parser->object_scope)
2592 error_at (location, "%<%T::%E%> has not been declared",
2593 parser->object_scope, name);
2594 else
2595 error_at (location, "%qE has not been declared", name);
2597 else if (parser->scope && parser->scope != global_namespace)
2599 switch (desired)
2601 case NLE_TYPE:
2602 error_at (location, "%<%E::%E%> is not a type",
2603 parser->scope, name);
2604 break;
2605 case NLE_CXX98:
2606 error_at (location, "%<%E::%E%> is not a class or namespace",
2607 parser->scope, name);
2608 break;
2609 case NLE_NOT_CXX98:
2610 error_at (location,
2611 "%<%E::%E%> is not a class, namespace, or enumeration",
2612 parser->scope, name);
2613 break;
2614 default:
2615 gcc_unreachable ();
2619 else if (parser->scope == global_namespace)
2621 switch (desired)
2623 case NLE_TYPE:
2624 error_at (location, "%<::%E%> is not a type", name);
2625 break;
2626 case NLE_CXX98:
2627 error_at (location, "%<::%E%> is not a class or namespace", name);
2628 break;
2629 case NLE_NOT_CXX98:
2630 error_at (location,
2631 "%<::%E%> is not a class, namespace, or enumeration",
2632 name);
2633 break;
2634 default:
2635 gcc_unreachable ();
2638 else
2640 switch (desired)
2642 case NLE_TYPE:
2643 error_at (location, "%qE is not a type", name);
2644 break;
2645 case NLE_CXX98:
2646 error_at (location, "%qE is not a class or namespace", name);
2647 break;
2648 case NLE_NOT_CXX98:
2649 error_at (location,
2650 "%qE is not a class, namespace, or enumeration", name);
2651 break;
2652 default:
2653 gcc_unreachable ();
2658 /* If we are parsing tentatively, remember that an error has occurred
2659 during this tentative parse. Returns true if the error was
2660 simulated; false if a message should be issued by the caller. */
2662 static bool
2663 cp_parser_simulate_error (cp_parser* parser)
2665 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2667 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2668 return true;
2670 return false;
2673 /* This function is called when a type is defined. If type
2674 definitions are forbidden at this point, an error message is
2675 issued. */
2677 static bool
2678 cp_parser_check_type_definition (cp_parser* parser)
2680 /* If types are forbidden here, issue a message. */
2681 if (parser->type_definition_forbidden_message)
2683 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2684 in the message need to be interpreted. */
2685 error (parser->type_definition_forbidden_message);
2686 return false;
2688 return true;
2691 /* This function is called when the DECLARATOR is processed. The TYPE
2692 was a type defined in the decl-specifiers. If it is invalid to
2693 define a type in the decl-specifiers for DECLARATOR, an error is
2694 issued. TYPE_LOCATION is the location of TYPE and is used
2695 for error reporting. */
2697 static void
2698 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2699 tree type, location_t type_location)
2701 /* [dcl.fct] forbids type definitions in return types.
2702 Unfortunately, it's not easy to know whether or not we are
2703 processing a return type until after the fact. */
2704 while (declarator
2705 && (declarator->kind == cdk_pointer
2706 || declarator->kind == cdk_reference
2707 || declarator->kind == cdk_ptrmem))
2708 declarator = declarator->declarator;
2709 if (declarator
2710 && declarator->kind == cdk_function)
2712 error_at (type_location,
2713 "new types may not be defined in a return type");
2714 inform (type_location,
2715 "(perhaps a semicolon is missing after the definition of %qT)",
2716 type);
2720 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2721 "<" in any valid C++ program. If the next token is indeed "<",
2722 issue a message warning the user about what appears to be an
2723 invalid attempt to form a template-id. LOCATION is the location
2724 of the type-specifier (TYPE) */
2726 static void
2727 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2728 tree type,
2729 enum tag_types tag_type,
2730 location_t location)
2732 cp_token_position start = 0;
2734 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2736 if (TYPE_P (type))
2737 error_at (location, "%qT is not a template", type);
2738 else if (identifier_p (type))
2740 if (tag_type != none_type)
2741 error_at (location, "%qE is not a class template", type);
2742 else
2743 error_at (location, "%qE is not a template", type);
2745 else
2746 error_at (location, "invalid template-id");
2747 /* Remember the location of the invalid "<". */
2748 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2749 start = cp_lexer_token_position (parser->lexer, true);
2750 /* Consume the "<". */
2751 cp_lexer_consume_token (parser->lexer);
2752 /* Parse the template arguments. */
2753 cp_parser_enclosed_template_argument_list (parser);
2754 /* Permanently remove the invalid template arguments so that
2755 this error message is not issued again. */
2756 if (start)
2757 cp_lexer_purge_tokens_after (parser->lexer, start);
2761 /* If parsing an integral constant-expression, issue an error message
2762 about the fact that THING appeared and return true. Otherwise,
2763 return false. In either case, set
2764 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2766 static bool
2767 cp_parser_non_integral_constant_expression (cp_parser *parser,
2768 non_integral_constant thing)
2770 parser->non_integral_constant_expression_p = true;
2771 if (parser->integral_constant_expression_p)
2773 if (!parser->allow_non_integral_constant_expression_p)
2775 const char *msg = NULL;
2776 switch (thing)
2778 case NIC_FLOAT:
2779 error ("floating-point literal "
2780 "cannot appear in a constant-expression");
2781 return true;
2782 case NIC_CAST:
2783 error ("a cast to a type other than an integral or "
2784 "enumeration type cannot appear in a "
2785 "constant-expression");
2786 return true;
2787 case NIC_TYPEID:
2788 error ("%<typeid%> operator "
2789 "cannot appear in a constant-expression");
2790 return true;
2791 case NIC_NCC:
2792 error ("non-constant compound literals "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_FUNC_CALL:
2796 error ("a function call "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_INC:
2800 error ("an increment "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_DEC:
2804 error ("an decrement "
2805 "cannot appear in a constant-expression");
2806 return true;
2807 case NIC_ARRAY_REF:
2808 error ("an array reference "
2809 "cannot appear in a constant-expression");
2810 return true;
2811 case NIC_ADDR_LABEL:
2812 error ("the address of a label "
2813 "cannot appear in a constant-expression");
2814 return true;
2815 case NIC_OVERLOADED:
2816 error ("calls to overloaded operators "
2817 "cannot appear in a constant-expression");
2818 return true;
2819 case NIC_ASSIGNMENT:
2820 error ("an assignment cannot appear in a constant-expression");
2821 return true;
2822 case NIC_COMMA:
2823 error ("a comma operator "
2824 "cannot appear in a constant-expression");
2825 return true;
2826 case NIC_CONSTRUCTOR:
2827 error ("a call to a constructor "
2828 "cannot appear in a constant-expression");
2829 return true;
2830 case NIC_TRANSACTION:
2831 error ("a transaction expression "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_THIS:
2835 msg = "this";
2836 break;
2837 case NIC_FUNC_NAME:
2838 msg = "__FUNCTION__";
2839 break;
2840 case NIC_PRETTY_FUNC:
2841 msg = "__PRETTY_FUNCTION__";
2842 break;
2843 case NIC_C99_FUNC:
2844 msg = "__func__";
2845 break;
2846 case NIC_VA_ARG:
2847 msg = "va_arg";
2848 break;
2849 case NIC_ARROW:
2850 msg = "->";
2851 break;
2852 case NIC_POINT:
2853 msg = ".";
2854 break;
2855 case NIC_STAR:
2856 msg = "*";
2857 break;
2858 case NIC_ADDR:
2859 msg = "&";
2860 break;
2861 case NIC_PREINCREMENT:
2862 msg = "++";
2863 break;
2864 case NIC_PREDECREMENT:
2865 msg = "--";
2866 break;
2867 case NIC_NEW:
2868 msg = "new";
2869 break;
2870 case NIC_DEL:
2871 msg = "delete";
2872 break;
2873 default:
2874 gcc_unreachable ();
2876 if (msg)
2877 error ("%qs cannot appear in a constant-expression", msg);
2878 return true;
2881 return false;
2884 /* Emit a diagnostic for an invalid type name. This function commits
2885 to the current active tentative parse, if any. (Otherwise, the
2886 problematic construct might be encountered again later, resulting
2887 in duplicate error messages.) LOCATION is the location of ID. */
2889 static void
2890 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2891 location_t location)
2893 tree decl, ambiguous_decls;
2894 cp_parser_commit_to_tentative_parse (parser);
2895 /* Try to lookup the identifier. */
2896 decl = cp_parser_lookup_name (parser, id, none_type,
2897 /*is_template=*/false,
2898 /*is_namespace=*/false,
2899 /*check_dependency=*/true,
2900 &ambiguous_decls, location);
2901 if (ambiguous_decls)
2902 /* If the lookup was ambiguous, an error will already have
2903 been issued. */
2904 return;
2905 /* If the lookup found a template-name, it means that the user forgot
2906 to specify an argument list. Emit a useful error message. */
2907 if (TREE_CODE (decl) == TEMPLATE_DECL)
2908 error_at (location,
2909 "invalid use of template-name %qE without an argument list",
2910 decl);
2911 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2912 error_at (location, "invalid use of destructor %qD as a type", id);
2913 else if (TREE_CODE (decl) == TYPE_DECL)
2914 /* Something like 'unsigned A a;' */
2915 error_at (location, "invalid combination of multiple type-specifiers");
2916 else if (!parser->scope)
2918 /* Issue an error message. */
2919 error_at (location, "%qE does not name a type", id);
2920 /* If we're in a template class, it's possible that the user was
2921 referring to a type from a base class. For example:
2923 template <typename T> struct A { typedef T X; };
2924 template <typename T> struct B : public A<T> { X x; };
2926 The user should have said "typename A<T>::X". */
2927 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2928 inform (location, "C++11 %<constexpr%> only available with "
2929 "-std=c++11 or -std=gnu++11");
2930 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2931 inform (location, "C++11 %<noexcept%> only available with "
2932 "-std=c++11 or -std=gnu++11");
2933 else if (cxx_dialect < cxx11
2934 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2935 inform (location, "C++11 %<thread_local%> only available with "
2936 "-std=c++11 or -std=gnu++11");
2937 else if (processing_template_decl && current_class_type
2938 && TYPE_BINFO (current_class_type))
2940 tree b;
2942 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2944 b = TREE_CHAIN (b))
2946 tree base_type = BINFO_TYPE (b);
2947 if (CLASS_TYPE_P (base_type)
2948 && dependent_type_p (base_type))
2950 tree field;
2951 /* Go from a particular instantiation of the
2952 template (which will have an empty TYPE_FIELDs),
2953 to the main version. */
2954 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2955 for (field = TYPE_FIELDS (base_type);
2956 field;
2957 field = DECL_CHAIN (field))
2958 if (TREE_CODE (field) == TYPE_DECL
2959 && DECL_NAME (field) == id)
2961 inform (location,
2962 "(perhaps %<typename %T::%E%> was intended)",
2963 BINFO_TYPE (b), id);
2964 break;
2966 if (field)
2967 break;
2972 /* Here we diagnose qualified-ids where the scope is actually correct,
2973 but the identifier does not resolve to a valid type name. */
2974 else if (parser->scope != error_mark_node)
2976 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2978 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2979 error_at (location_of (id),
2980 "%qE in namespace %qE does not name a template type",
2981 id, parser->scope);
2982 else
2983 error_at (location_of (id),
2984 "%qE in namespace %qE does not name a type",
2985 id, parser->scope);
2987 else if (CLASS_TYPE_P (parser->scope)
2988 && constructor_name_p (id, parser->scope))
2990 /* A<T>::A<T>() */
2991 error_at (location, "%<%T::%E%> names the constructor, not"
2992 " the type", parser->scope, id);
2993 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2994 error_at (location, "and %qT has no template constructors",
2995 parser->scope);
2997 else if (TYPE_P (parser->scope)
2998 && dependent_scope_p (parser->scope))
2999 error_at (location, "need %<typename%> before %<%T::%E%> because "
3000 "%qT is a dependent scope",
3001 parser->scope, id, parser->scope);
3002 else if (TYPE_P (parser->scope))
3004 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3005 error_at (location_of (id),
3006 "%qE in %q#T does not name a template type",
3007 id, parser->scope);
3008 else
3009 error_at (location_of (id),
3010 "%qE in %q#T does not name a type",
3011 id, parser->scope);
3013 else
3014 gcc_unreachable ();
3018 /* Check for a common situation where a type-name should be present,
3019 but is not, and issue a sensible error message. Returns true if an
3020 invalid type-name was detected.
3022 The situation handled by this function are variable declarations of the
3023 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3024 Usually, `ID' should name a type, but if we got here it means that it
3025 does not. We try to emit the best possible error message depending on
3026 how exactly the id-expression looks like. */
3028 static bool
3029 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3031 tree id;
3032 cp_token *token = cp_lexer_peek_token (parser->lexer);
3034 /* Avoid duplicate error about ambiguous lookup. */
3035 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3037 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3038 if (next->type == CPP_NAME && next->error_reported)
3039 goto out;
3042 cp_parser_parse_tentatively (parser);
3043 id = cp_parser_id_expression (parser,
3044 /*template_keyword_p=*/false,
3045 /*check_dependency_p=*/true,
3046 /*template_p=*/NULL,
3047 /*declarator_p=*/true,
3048 /*optional_p=*/false);
3049 /* If the next token is a (, this is a function with no explicit return
3050 type, i.e. constructor, destructor or conversion op. */
3051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3052 || TREE_CODE (id) == TYPE_DECL)
3054 cp_parser_abort_tentative_parse (parser);
3055 return false;
3057 if (!cp_parser_parse_definitely (parser))
3058 return false;
3060 /* Emit a diagnostic for the invalid type. */
3061 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3062 out:
3063 /* If we aren't in the middle of a declarator (i.e. in a
3064 parameter-declaration-clause), skip to the end of the declaration;
3065 there's no point in trying to process it. */
3066 if (!parser->in_declarator_p)
3067 cp_parser_skip_to_end_of_block_or_statement (parser);
3068 return true;
3071 /* Consume tokens up to, and including, the next non-nested closing `)'.
3072 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3073 are doing error recovery. Returns -1 if OR_COMMA is true and we
3074 found an unnested comma. */
3076 static int
3077 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3078 bool recovering,
3079 bool or_comma,
3080 bool consume_paren)
3082 unsigned paren_depth = 0;
3083 unsigned brace_depth = 0;
3084 unsigned square_depth = 0;
3086 if (recovering && !or_comma
3087 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3088 return 0;
3090 while (true)
3092 cp_token * token = cp_lexer_peek_token (parser->lexer);
3094 switch (token->type)
3096 case CPP_EOF:
3097 case CPP_PRAGMA_EOL:
3098 /* If we've run out of tokens, then there is no closing `)'. */
3099 return 0;
3101 /* This is good for lambda expression capture-lists. */
3102 case CPP_OPEN_SQUARE:
3103 ++square_depth;
3104 break;
3105 case CPP_CLOSE_SQUARE:
3106 if (!square_depth--)
3107 return 0;
3108 break;
3110 case CPP_SEMICOLON:
3111 /* This matches the processing in skip_to_end_of_statement. */
3112 if (!brace_depth)
3113 return 0;
3114 break;
3116 case CPP_OPEN_BRACE:
3117 ++brace_depth;
3118 break;
3119 case CPP_CLOSE_BRACE:
3120 if (!brace_depth--)
3121 return 0;
3122 break;
3124 case CPP_COMMA:
3125 if (recovering && or_comma && !brace_depth && !paren_depth
3126 && !square_depth)
3127 return -1;
3128 break;
3130 case CPP_OPEN_PAREN:
3131 if (!brace_depth)
3132 ++paren_depth;
3133 break;
3135 case CPP_CLOSE_PAREN:
3136 if (!brace_depth && !paren_depth--)
3138 if (consume_paren)
3139 cp_lexer_consume_token (parser->lexer);
3140 return 1;
3142 break;
3144 default:
3145 break;
3148 /* Consume the token. */
3149 cp_lexer_consume_token (parser->lexer);
3153 /* Consume tokens until we reach the end of the current statement.
3154 Normally, that will be just before consuming a `;'. However, if a
3155 non-nested `}' comes first, then we stop before consuming that. */
3157 static void
3158 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3160 unsigned nesting_depth = 0;
3162 /* Unwind generic function template scope if necessary. */
3163 if (parser->fully_implicit_function_template_p)
3164 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3166 while (true)
3168 cp_token *token = cp_lexer_peek_token (parser->lexer);
3170 switch (token->type)
3172 case CPP_EOF:
3173 case CPP_PRAGMA_EOL:
3174 /* If we've run out of tokens, stop. */
3175 return;
3177 case CPP_SEMICOLON:
3178 /* If the next token is a `;', we have reached the end of the
3179 statement. */
3180 if (!nesting_depth)
3181 return;
3182 break;
3184 case CPP_CLOSE_BRACE:
3185 /* If this is a non-nested '}', stop before consuming it.
3186 That way, when confronted with something like:
3188 { 3 + }
3190 we stop before consuming the closing '}', even though we
3191 have not yet reached a `;'. */
3192 if (nesting_depth == 0)
3193 return;
3195 /* If it is the closing '}' for a block that we have
3196 scanned, stop -- but only after consuming the token.
3197 That way given:
3199 void f g () { ... }
3200 typedef int I;
3202 we will stop after the body of the erroneously declared
3203 function, but before consuming the following `typedef'
3204 declaration. */
3205 if (--nesting_depth == 0)
3207 cp_lexer_consume_token (parser->lexer);
3208 return;
3211 case CPP_OPEN_BRACE:
3212 ++nesting_depth;
3213 break;
3215 default:
3216 break;
3219 /* Consume the token. */
3220 cp_lexer_consume_token (parser->lexer);
3224 /* This function is called at the end of a statement or declaration.
3225 If the next token is a semicolon, it is consumed; otherwise, error
3226 recovery is attempted. */
3228 static void
3229 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3231 /* Look for the trailing `;'. */
3232 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3234 /* If there is additional (erroneous) input, skip to the end of
3235 the statement. */
3236 cp_parser_skip_to_end_of_statement (parser);
3237 /* If the next token is now a `;', consume it. */
3238 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3239 cp_lexer_consume_token (parser->lexer);
3243 /* Skip tokens until we have consumed an entire block, or until we
3244 have consumed a non-nested `;'. */
3246 static void
3247 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3249 int nesting_depth = 0;
3251 /* Unwind generic function template scope if necessary. */
3252 if (parser->fully_implicit_function_template_p)
3253 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3255 while (nesting_depth >= 0)
3257 cp_token *token = cp_lexer_peek_token (parser->lexer);
3259 switch (token->type)
3261 case CPP_EOF:
3262 case CPP_PRAGMA_EOL:
3263 /* If we've run out of tokens, stop. */
3264 return;
3266 case CPP_SEMICOLON:
3267 /* Stop if this is an unnested ';'. */
3268 if (!nesting_depth)
3269 nesting_depth = -1;
3270 break;
3272 case CPP_CLOSE_BRACE:
3273 /* Stop if this is an unnested '}', or closes the outermost
3274 nesting level. */
3275 nesting_depth--;
3276 if (nesting_depth < 0)
3277 return;
3278 if (!nesting_depth)
3279 nesting_depth = -1;
3280 break;
3282 case CPP_OPEN_BRACE:
3283 /* Nest. */
3284 nesting_depth++;
3285 break;
3287 default:
3288 break;
3291 /* Consume the token. */
3292 cp_lexer_consume_token (parser->lexer);
3296 /* Skip tokens until a non-nested closing curly brace is the next
3297 token, or there are no more tokens. Return true in the first case,
3298 false otherwise. */
3300 static bool
3301 cp_parser_skip_to_closing_brace (cp_parser *parser)
3303 unsigned nesting_depth = 0;
3305 while (true)
3307 cp_token *token = cp_lexer_peek_token (parser->lexer);
3309 switch (token->type)
3311 case CPP_EOF:
3312 case CPP_PRAGMA_EOL:
3313 /* If we've run out of tokens, stop. */
3314 return false;
3316 case CPP_CLOSE_BRACE:
3317 /* If the next token is a non-nested `}', then we have reached
3318 the end of the current block. */
3319 if (nesting_depth-- == 0)
3320 return true;
3321 break;
3323 case CPP_OPEN_BRACE:
3324 /* If it the next token is a `{', then we are entering a new
3325 block. Consume the entire block. */
3326 ++nesting_depth;
3327 break;
3329 default:
3330 break;
3333 /* Consume the token. */
3334 cp_lexer_consume_token (parser->lexer);
3338 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3339 parameter is the PRAGMA token, allowing us to purge the entire pragma
3340 sequence. */
3342 static void
3343 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3345 cp_token *token;
3347 parser->lexer->in_pragma = false;
3350 token = cp_lexer_consume_token (parser->lexer);
3351 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3353 /* Ensure that the pragma is not parsed again. */
3354 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3357 /* Require pragma end of line, resyncing with it as necessary. The
3358 arguments are as for cp_parser_skip_to_pragma_eol. */
3360 static void
3361 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3363 parser->lexer->in_pragma = false;
3364 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3365 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3368 /* This is a simple wrapper around make_typename_type. When the id is
3369 an unresolved identifier node, we can provide a superior diagnostic
3370 using cp_parser_diagnose_invalid_type_name. */
3372 static tree
3373 cp_parser_make_typename_type (cp_parser *parser, tree id,
3374 location_t id_location)
3376 tree result;
3377 if (identifier_p (id))
3379 result = make_typename_type (parser->scope, id, typename_type,
3380 /*complain=*/tf_none);
3381 if (result == error_mark_node)
3382 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3383 return result;
3385 return make_typename_type (parser->scope, id, typename_type, tf_error);
3388 /* This is a wrapper around the
3389 make_{pointer,ptrmem,reference}_declarator functions that decides
3390 which one to call based on the CODE and CLASS_TYPE arguments. The
3391 CODE argument should be one of the values returned by
3392 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3393 appertain to the pointer or reference. */
3395 static cp_declarator *
3396 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3397 cp_cv_quals cv_qualifiers,
3398 cp_declarator *target,
3399 tree attributes)
3401 if (code == ERROR_MARK)
3402 return cp_error_declarator;
3404 if (code == INDIRECT_REF)
3405 if (class_type == NULL_TREE)
3406 return make_pointer_declarator (cv_qualifiers, target, attributes);
3407 else
3408 return make_ptrmem_declarator (cv_qualifiers, class_type,
3409 target, attributes);
3410 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3411 return make_reference_declarator (cv_qualifiers, target,
3412 false, attributes);
3413 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3414 return make_reference_declarator (cv_qualifiers, target,
3415 true, attributes);
3416 gcc_unreachable ();
3419 /* Create a new C++ parser. */
3421 static cp_parser *
3422 cp_parser_new (void)
3424 cp_parser *parser;
3425 cp_lexer *lexer;
3426 unsigned i;
3428 /* cp_lexer_new_main is called before doing GC allocation because
3429 cp_lexer_new_main might load a PCH file. */
3430 lexer = cp_lexer_new_main ();
3432 /* Initialize the binops_by_token so that we can get the tree
3433 directly from the token. */
3434 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3435 binops_by_token[binops[i].token_type] = binops[i];
3437 parser = ggc_cleared_alloc<cp_parser> ();
3438 parser->lexer = lexer;
3439 parser->context = cp_parser_context_new (NULL);
3441 /* For now, we always accept GNU extensions. */
3442 parser->allow_gnu_extensions_p = 1;
3444 /* The `>' token is a greater-than operator, not the end of a
3445 template-id. */
3446 parser->greater_than_is_operator_p = true;
3448 parser->default_arg_ok_p = true;
3450 /* We are not parsing a constant-expression. */
3451 parser->integral_constant_expression_p = false;
3452 parser->allow_non_integral_constant_expression_p = false;
3453 parser->non_integral_constant_expression_p = false;
3455 /* Local variable names are not forbidden. */
3456 parser->local_variables_forbidden_p = false;
3458 /* We are not processing an `extern "C"' declaration. */
3459 parser->in_unbraced_linkage_specification_p = false;
3461 /* We are not processing a declarator. */
3462 parser->in_declarator_p = false;
3464 /* We are not processing a template-argument-list. */
3465 parser->in_template_argument_list_p = false;
3467 /* We are not in an iteration statement. */
3468 parser->in_statement = 0;
3470 /* We are not in a switch statement. */
3471 parser->in_switch_statement_p = false;
3473 /* We are not parsing a type-id inside an expression. */
3474 parser->in_type_id_in_expr_p = false;
3476 /* Declarations aren't implicitly extern "C". */
3477 parser->implicit_extern_c = false;
3479 /* String literals should be translated to the execution character set. */
3480 parser->translate_strings_p = true;
3482 /* We are not parsing a function body. */
3483 parser->in_function_body = false;
3485 /* We can correct until told otherwise. */
3486 parser->colon_corrects_to_scope_p = true;
3488 /* The unparsed function queue is empty. */
3489 push_unparsed_function_queues (parser);
3491 /* There are no classes being defined. */
3492 parser->num_classes_being_defined = 0;
3494 /* No template parameters apply. */
3495 parser->num_template_parameter_lists = 0;
3497 /* Not declaring an implicit function template. */
3498 parser->auto_is_implicit_function_template_parm_p = false;
3499 parser->fully_implicit_function_template_p = false;
3500 parser->implicit_template_parms = 0;
3501 parser->implicit_template_scope = 0;
3503 return parser;
3506 /* Create a cp_lexer structure which will emit the tokens in CACHE
3507 and push it onto the parser's lexer stack. This is used for delayed
3508 parsing of in-class method bodies and default arguments, and should
3509 not be confused with tentative parsing. */
3510 static void
3511 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3513 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3514 lexer->next = parser->lexer;
3515 parser->lexer = lexer;
3517 /* Move the current source position to that of the first token in the
3518 new lexer. */
3519 cp_lexer_set_source_position_from_token (lexer->next_token);
3522 /* Pop the top lexer off the parser stack. This is never used for the
3523 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3524 static void
3525 cp_parser_pop_lexer (cp_parser *parser)
3527 cp_lexer *lexer = parser->lexer;
3528 parser->lexer = lexer->next;
3529 cp_lexer_destroy (lexer);
3531 /* Put the current source position back where it was before this
3532 lexer was pushed. */
3533 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3536 /* Lexical conventions [gram.lex] */
3538 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3539 identifier. */
3541 static tree
3542 cp_parser_identifier (cp_parser* parser)
3544 cp_token *token;
3546 /* Look for the identifier. */
3547 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3548 /* Return the value. */
3549 return token ? token->u.value : error_mark_node;
3552 /* Parse a sequence of adjacent string constants. Returns a
3553 TREE_STRING representing the combined, nul-terminated string
3554 constant. If TRANSLATE is true, translate the string to the
3555 execution character set. If WIDE_OK is true, a wide string is
3556 invalid here.
3558 C++98 [lex.string] says that if a narrow string literal token is
3559 adjacent to a wide string literal token, the behavior is undefined.
3560 However, C99 6.4.5p4 says that this results in a wide string literal.
3561 We follow C99 here, for consistency with the C front end.
3563 This code is largely lifted from lex_string() in c-lex.c.
3565 FUTURE: ObjC++ will need to handle @-strings here. */
3566 static tree
3567 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3568 bool lookup_udlit = true)
3570 tree value;
3571 size_t count;
3572 struct obstack str_ob;
3573 cpp_string str, istr, *strs;
3574 cp_token *tok;
3575 enum cpp_ttype type, curr_type;
3576 int have_suffix_p = 0;
3577 tree string_tree;
3578 tree suffix_id = NULL_TREE;
3579 bool curr_tok_is_userdef_p = false;
3581 tok = cp_lexer_peek_token (parser->lexer);
3582 if (!cp_parser_is_string_literal (tok))
3584 cp_parser_error (parser, "expected string-literal");
3585 return error_mark_node;
3588 if (cpp_userdef_string_p (tok->type))
3590 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3591 curr_type = cpp_userdef_string_remove_type (tok->type);
3592 curr_tok_is_userdef_p = true;
3594 else
3596 string_tree = tok->u.value;
3597 curr_type = tok->type;
3599 type = curr_type;
3601 /* Try to avoid the overhead of creating and destroying an obstack
3602 for the common case of just one string. */
3603 if (!cp_parser_is_string_literal
3604 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3606 cp_lexer_consume_token (parser->lexer);
3608 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3609 str.len = TREE_STRING_LENGTH (string_tree);
3610 count = 1;
3612 if (curr_tok_is_userdef_p)
3614 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3615 have_suffix_p = 1;
3616 curr_type = cpp_userdef_string_remove_type (tok->type);
3618 else
3619 curr_type = tok->type;
3621 strs = &str;
3623 else
3625 gcc_obstack_init (&str_ob);
3626 count = 0;
3630 cp_lexer_consume_token (parser->lexer);
3631 count++;
3632 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3633 str.len = TREE_STRING_LENGTH (string_tree);
3635 if (curr_tok_is_userdef_p)
3637 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3638 if (have_suffix_p == 0)
3640 suffix_id = curr_suffix_id;
3641 have_suffix_p = 1;
3643 else if (have_suffix_p == 1
3644 && curr_suffix_id != suffix_id)
3646 error ("inconsistent user-defined literal suffixes"
3647 " %qD and %qD in string literal",
3648 suffix_id, curr_suffix_id);
3649 have_suffix_p = -1;
3651 curr_type = cpp_userdef_string_remove_type (tok->type);
3653 else
3654 curr_type = tok->type;
3656 if (type != curr_type)
3658 if (type == CPP_STRING)
3659 type = curr_type;
3660 else if (curr_type != CPP_STRING)
3661 error_at (tok->location,
3662 "unsupported non-standard concatenation "
3663 "of string literals");
3666 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3668 tok = cp_lexer_peek_token (parser->lexer);
3669 if (cpp_userdef_string_p (tok->type))
3671 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3672 curr_type = cpp_userdef_string_remove_type (tok->type);
3673 curr_tok_is_userdef_p = true;
3675 else
3677 string_tree = tok->u.value;
3678 curr_type = tok->type;
3679 curr_tok_is_userdef_p = false;
3682 while (cp_parser_is_string_literal (tok));
3684 strs = (cpp_string *) obstack_finish (&str_ob);
3687 if (type != CPP_STRING && !wide_ok)
3689 cp_parser_error (parser, "a wide string is invalid in this context");
3690 type = CPP_STRING;
3693 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3694 (parse_in, strs, count, &istr, type))
3696 value = build_string (istr.len, (const char *)istr.text);
3697 free (CONST_CAST (unsigned char *, istr.text));
3699 switch (type)
3701 default:
3702 case CPP_STRING:
3703 case CPP_UTF8STRING:
3704 TREE_TYPE (value) = char_array_type_node;
3705 break;
3706 case CPP_STRING16:
3707 TREE_TYPE (value) = char16_array_type_node;
3708 break;
3709 case CPP_STRING32:
3710 TREE_TYPE (value) = char32_array_type_node;
3711 break;
3712 case CPP_WSTRING:
3713 TREE_TYPE (value) = wchar_array_type_node;
3714 break;
3717 value = fix_string_type (value);
3719 if (have_suffix_p)
3721 tree literal = build_userdef_literal (suffix_id, value,
3722 OT_NONE, NULL_TREE);
3723 if (lookup_udlit)
3724 value = cp_parser_userdef_string_literal (literal);
3725 else
3726 value = literal;
3729 else
3730 /* cpp_interpret_string has issued an error. */
3731 value = error_mark_node;
3733 if (count > 1)
3734 obstack_free (&str_ob, 0);
3736 return value;
3739 /* Look up a literal operator with the name and the exact arguments. */
3741 static tree
3742 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3744 tree decl, fns;
3745 decl = lookup_name (name);
3746 if (!decl || !is_overloaded_fn (decl))
3747 return error_mark_node;
3749 for (fns = decl; fns; fns = OVL_NEXT (fns))
3751 unsigned int ix;
3752 bool found = true;
3753 tree fn = OVL_CURRENT (fns);
3754 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3755 if (parmtypes != NULL_TREE)
3757 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3758 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3760 tree tparm = TREE_VALUE (parmtypes);
3761 tree targ = TREE_TYPE ((*args)[ix]);
3762 bool ptr = TYPE_PTR_P (tparm);
3763 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3764 if ((ptr || arr || !same_type_p (tparm, targ))
3765 && (!ptr || !arr
3766 || !same_type_p (TREE_TYPE (tparm),
3767 TREE_TYPE (targ))))
3768 found = false;
3770 if (found
3771 && ix == vec_safe_length (args)
3772 /* May be this should be sufficient_parms_p instead,
3773 depending on how exactly should user-defined literals
3774 work in presence of default arguments on the literal
3775 operator parameters. */
3776 && parmtypes == void_list_node)
3777 return fn;
3781 return error_mark_node;
3784 /* Parse a user-defined char constant. Returns a call to a user-defined
3785 literal operator taking the character as an argument. */
3787 static tree
3788 cp_parser_userdef_char_literal (cp_parser *parser)
3790 cp_token *token = cp_lexer_consume_token (parser->lexer);
3791 tree literal = token->u.value;
3792 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3793 tree value = USERDEF_LITERAL_VALUE (literal);
3794 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3795 tree decl, result;
3797 /* Build up a call to the user-defined operator */
3798 /* Lookup the name we got back from the id-expression. */
3799 vec<tree, va_gc> *args = make_tree_vector ();
3800 vec_safe_push (args, value);
3801 decl = lookup_literal_operator (name, args);
3802 if (!decl || decl == error_mark_node)
3804 error ("unable to find character literal operator %qD with %qT argument",
3805 name, TREE_TYPE (value));
3806 release_tree_vector (args);
3807 return error_mark_node;
3809 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3810 release_tree_vector (args);
3811 if (result != error_mark_node)
3812 return result;
3814 error ("unable to find character literal operator %qD with %qT argument",
3815 name, TREE_TYPE (value));
3816 return error_mark_node;
3819 /* A subroutine of cp_parser_userdef_numeric_literal to
3820 create a char... template parameter pack from a string node. */
3822 static tree
3823 make_char_string_pack (tree value)
3825 tree charvec;
3826 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3827 const char *str = TREE_STRING_POINTER (value);
3828 int i, len = TREE_STRING_LENGTH (value) - 1;
3829 tree argvec = make_tree_vec (1);
3831 /* Fill in CHARVEC with all of the parameters. */
3832 charvec = make_tree_vec (len);
3833 for (i = 0; i < len; ++i)
3834 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3836 /* Build the argument packs. */
3837 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3838 TREE_TYPE (argpack) = char_type_node;
3840 TREE_VEC_ELT (argvec, 0) = argpack;
3842 return argvec;
3845 /* A subroutine of cp_parser_userdef_numeric_literal to
3846 create a char... template parameter pack from a string node. */
3848 static tree
3849 make_string_pack (tree value)
3851 tree charvec;
3852 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3853 const unsigned char *str
3854 = (const unsigned char *) TREE_STRING_POINTER (value);
3855 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3856 int len = TREE_STRING_LENGTH (value) / sz - 1;
3857 tree argvec = make_tree_vec (2);
3859 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3860 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3862 /* First template parm is character type. */
3863 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3865 /* Fill in CHARVEC with all of the parameters. */
3866 charvec = make_tree_vec (len);
3867 for (int i = 0; i < len; ++i)
3868 TREE_VEC_ELT (charvec, i)
3869 = double_int_to_tree (str_char_type_node,
3870 double_int::from_buffer (str + i * sz, sz));
3872 /* Build the argument packs. */
3873 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3874 TREE_TYPE (argpack) = str_char_type_node;
3876 TREE_VEC_ELT (argvec, 1) = argpack;
3878 return argvec;
3881 /* Parse a user-defined numeric constant. returns a call to a user-defined
3882 literal operator. */
3884 static tree
3885 cp_parser_userdef_numeric_literal (cp_parser *parser)
3887 cp_token *token = cp_lexer_consume_token (parser->lexer);
3888 tree literal = token->u.value;
3889 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3890 tree value = USERDEF_LITERAL_VALUE (literal);
3891 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3892 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3893 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3894 tree decl, result;
3895 vec<tree, va_gc> *args;
3897 /* Look for a literal operator taking the exact type of numeric argument
3898 as the literal value. */
3899 args = make_tree_vector ();
3900 vec_safe_push (args, value);
3901 decl = lookup_literal_operator (name, args);
3902 if (decl && decl != error_mark_node)
3904 result = finish_call_expr (decl, &args, false, true, tf_none);
3905 if (result != error_mark_node)
3907 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3908 warning_at (token->location, OPT_Woverflow,
3909 "integer literal exceeds range of %qT type",
3910 long_long_unsigned_type_node);
3911 else
3913 if (overflow > 0)
3914 warning_at (token->location, OPT_Woverflow,
3915 "floating literal exceeds range of %qT type",
3916 long_double_type_node);
3917 else if (overflow < 0)
3918 warning_at (token->location, OPT_Woverflow,
3919 "floating literal truncated to zero");
3921 release_tree_vector (args);
3922 return result;
3925 release_tree_vector (args);
3927 /* If the numeric argument didn't work, look for a raw literal
3928 operator taking a const char* argument consisting of the number
3929 in string format. */
3930 args = make_tree_vector ();
3931 vec_safe_push (args, num_string);
3932 decl = lookup_literal_operator (name, args);
3933 if (decl && decl != error_mark_node)
3935 result = finish_call_expr (decl, &args, false, true, tf_none);
3936 if (result != error_mark_node)
3938 release_tree_vector (args);
3939 return result;
3942 release_tree_vector (args);
3944 /* If the raw literal didn't work, look for a non-type template
3945 function with parameter pack char.... Call the function with
3946 template parameter characters representing the number. */
3947 args = make_tree_vector ();
3948 decl = lookup_literal_operator (name, args);
3949 if (decl && decl != error_mark_node)
3951 tree tmpl_args = make_char_string_pack (num_string);
3952 decl = lookup_template_function (decl, tmpl_args);
3953 result = finish_call_expr (decl, &args, false, true, tf_none);
3954 if (result != error_mark_node)
3956 release_tree_vector (args);
3957 return result;
3960 release_tree_vector (args);
3962 error ("unable to find numeric literal operator %qD", name);
3963 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3964 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3965 "to enable more built-in suffixes");
3966 return error_mark_node;
3969 /* Parse a user-defined string constant. Returns a call to a user-defined
3970 literal operator taking a character pointer and the length of the string
3971 as arguments. */
3973 static tree
3974 cp_parser_userdef_string_literal (tree literal)
3976 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3977 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3978 tree value = USERDEF_LITERAL_VALUE (literal);
3979 int len = TREE_STRING_LENGTH (value)
3980 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3981 tree decl, result;
3982 vec<tree, va_gc> *args;
3984 /* Look for a template function with typename parameter CharT
3985 and parameter pack CharT... Call the function with
3986 template parameter characters representing the string. */
3987 args = make_tree_vector ();
3988 decl = lookup_literal_operator (name, args);
3989 if (decl && decl != error_mark_node)
3991 tree tmpl_args = make_string_pack (value);
3992 decl = lookup_template_function (decl, tmpl_args);
3993 result = finish_call_expr (decl, &args, false, true, tf_none);
3994 if (result != error_mark_node)
3996 release_tree_vector (args);
3997 return result;
4000 release_tree_vector (args);
4002 /* Build up a call to the user-defined operator */
4003 /* Lookup the name we got back from the id-expression. */
4004 args = make_tree_vector ();
4005 vec_safe_push (args, value);
4006 vec_safe_push (args, build_int_cst (size_type_node, len));
4007 decl = lookup_name (name);
4008 if (!decl || decl == error_mark_node)
4010 error ("unable to find string literal operator %qD", name);
4011 release_tree_vector (args);
4012 return error_mark_node;
4014 result = finish_call_expr (decl, &args, false, true, tf_none);
4015 release_tree_vector (args);
4016 if (result != error_mark_node)
4017 return result;
4019 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4020 name, TREE_TYPE (value), size_type_node);
4021 return error_mark_node;
4025 /* Basic concepts [gram.basic] */
4027 /* Parse a translation-unit.
4029 translation-unit:
4030 declaration-seq [opt]
4032 Returns TRUE if all went well. */
4034 static bool
4035 cp_parser_translation_unit (cp_parser* parser)
4037 /* The address of the first non-permanent object on the declarator
4038 obstack. */
4039 static void *declarator_obstack_base;
4041 bool success;
4043 /* Create the declarator obstack, if necessary. */
4044 if (!cp_error_declarator)
4046 gcc_obstack_init (&declarator_obstack);
4047 /* Create the error declarator. */
4048 cp_error_declarator = make_declarator (cdk_error);
4049 /* Create the empty parameter list. */
4050 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4051 /* Remember where the base of the declarator obstack lies. */
4052 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4055 cp_parser_declaration_seq_opt (parser);
4057 /* If there are no tokens left then all went well. */
4058 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4060 /* Get rid of the token array; we don't need it any more. */
4061 cp_lexer_destroy (parser->lexer);
4062 parser->lexer = NULL;
4064 /* This file might have been a context that's implicitly extern
4065 "C". If so, pop the lang context. (Only relevant for PCH.) */
4066 if (parser->implicit_extern_c)
4068 pop_lang_context ();
4069 parser->implicit_extern_c = false;
4072 /* Finish up. */
4073 finish_translation_unit ();
4075 success = true;
4077 else
4079 cp_parser_error (parser, "expected declaration");
4080 success = false;
4083 /* Make sure the declarator obstack was fully cleaned up. */
4084 gcc_assert (obstack_next_free (&declarator_obstack)
4085 == declarator_obstack_base);
4087 /* All went well. */
4088 return success;
4091 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4092 decltype context. */
4094 static inline tsubst_flags_t
4095 complain_flags (bool decltype_p)
4097 tsubst_flags_t complain = tf_warning_or_error;
4098 if (decltype_p)
4099 complain |= tf_decltype;
4100 return complain;
4103 /* Expressions [gram.expr] */
4105 /* Parse a primary-expression.
4107 primary-expression:
4108 literal
4109 this
4110 ( expression )
4111 id-expression
4112 lambda-expression (C++11)
4114 GNU Extensions:
4116 primary-expression:
4117 ( compound-statement )
4118 __builtin_va_arg ( assignment-expression , type-id )
4119 __builtin_offsetof ( type-id , offsetof-expression )
4121 C++ Extensions:
4122 __has_nothrow_assign ( type-id )
4123 __has_nothrow_constructor ( type-id )
4124 __has_nothrow_copy ( type-id )
4125 __has_trivial_assign ( type-id )
4126 __has_trivial_constructor ( type-id )
4127 __has_trivial_copy ( type-id )
4128 __has_trivial_destructor ( type-id )
4129 __has_virtual_destructor ( type-id )
4130 __is_abstract ( type-id )
4131 __is_base_of ( type-id , type-id )
4132 __is_class ( type-id )
4133 __is_convertible_to ( type-id , type-id )
4134 __is_empty ( type-id )
4135 __is_enum ( type-id )
4136 __is_final ( type-id )
4137 __is_literal_type ( type-id )
4138 __is_pod ( type-id )
4139 __is_polymorphic ( type-id )
4140 __is_std_layout ( type-id )
4141 __is_trivial ( type-id )
4142 __is_union ( type-id )
4144 Objective-C++ Extension:
4146 primary-expression:
4147 objc-expression
4149 literal:
4150 __null
4152 ADDRESS_P is true iff this expression was immediately preceded by
4153 "&" and therefore might denote a pointer-to-member. CAST_P is true
4154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4155 true iff this expression is a template argument.
4157 Returns a representation of the expression. Upon return, *IDK
4158 indicates what kind of id-expression (if any) was present. */
4160 static tree
4161 cp_parser_primary_expression (cp_parser *parser,
4162 bool address_p,
4163 bool cast_p,
4164 bool template_arg_p,
4165 bool decltype_p,
4166 cp_id_kind *idk)
4168 cp_token *token = NULL;
4170 /* Assume the primary expression is not an id-expression. */
4171 *idk = CP_ID_KIND_NONE;
4173 /* Peek at the next token. */
4174 token = cp_lexer_peek_token (parser->lexer);
4175 switch (token->type)
4177 /* literal:
4178 integer-literal
4179 character-literal
4180 floating-literal
4181 string-literal
4182 boolean-literal
4183 pointer-literal
4184 user-defined-literal */
4185 case CPP_CHAR:
4186 case CPP_CHAR16:
4187 case CPP_CHAR32:
4188 case CPP_WCHAR:
4189 case CPP_NUMBER:
4190 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4191 return cp_parser_userdef_numeric_literal (parser);
4192 token = cp_lexer_consume_token (parser->lexer);
4193 if (TREE_CODE (token->u.value) == FIXED_CST)
4195 error_at (token->location,
4196 "fixed-point types not supported in C++");
4197 return error_mark_node;
4199 /* Floating-point literals are only allowed in an integral
4200 constant expression if they are cast to an integral or
4201 enumeration type. */
4202 if (TREE_CODE (token->u.value) == REAL_CST
4203 && parser->integral_constant_expression_p
4204 && pedantic)
4206 /* CAST_P will be set even in invalid code like "int(2.7 +
4207 ...)". Therefore, we have to check that the next token
4208 is sure to end the cast. */
4209 if (cast_p)
4211 cp_token *next_token;
4213 next_token = cp_lexer_peek_token (parser->lexer);
4214 if (/* The comma at the end of an
4215 enumerator-definition. */
4216 next_token->type != CPP_COMMA
4217 /* The curly brace at the end of an enum-specifier. */
4218 && next_token->type != CPP_CLOSE_BRACE
4219 /* The end of a statement. */
4220 && next_token->type != CPP_SEMICOLON
4221 /* The end of the cast-expression. */
4222 && next_token->type != CPP_CLOSE_PAREN
4223 /* The end of an array bound. */
4224 && next_token->type != CPP_CLOSE_SQUARE
4225 /* The closing ">" in a template-argument-list. */
4226 && (next_token->type != CPP_GREATER
4227 || parser->greater_than_is_operator_p)
4228 /* C++0x only: A ">>" treated like two ">" tokens,
4229 in a template-argument-list. */
4230 && (next_token->type != CPP_RSHIFT
4231 || (cxx_dialect == cxx98)
4232 || parser->greater_than_is_operator_p))
4233 cast_p = false;
4236 /* If we are within a cast, then the constraint that the
4237 cast is to an integral or enumeration type will be
4238 checked at that point. If we are not within a cast, then
4239 this code is invalid. */
4240 if (!cast_p)
4241 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4243 return token->u.value;
4245 case CPP_CHAR_USERDEF:
4246 case CPP_CHAR16_USERDEF:
4247 case CPP_CHAR32_USERDEF:
4248 case CPP_WCHAR_USERDEF:
4249 return cp_parser_userdef_char_literal (parser);
4251 case CPP_STRING:
4252 case CPP_STRING16:
4253 case CPP_STRING32:
4254 case CPP_WSTRING:
4255 case CPP_UTF8STRING:
4256 case CPP_STRING_USERDEF:
4257 case CPP_STRING16_USERDEF:
4258 case CPP_STRING32_USERDEF:
4259 case CPP_WSTRING_USERDEF:
4260 case CPP_UTF8STRING_USERDEF:
4261 /* ??? Should wide strings be allowed when parser->translate_strings_p
4262 is false (i.e. in attributes)? If not, we can kill the third
4263 argument to cp_parser_string_literal. */
4264 return cp_parser_string_literal (parser,
4265 parser->translate_strings_p,
4266 true);
4268 case CPP_OPEN_PAREN:
4270 tree expr;
4271 bool saved_greater_than_is_operator_p;
4273 /* Consume the `('. */
4274 cp_lexer_consume_token (parser->lexer);
4275 /* Within a parenthesized expression, a `>' token is always
4276 the greater-than operator. */
4277 saved_greater_than_is_operator_p
4278 = parser->greater_than_is_operator_p;
4279 parser->greater_than_is_operator_p = true;
4280 /* If we see `( { ' then we are looking at the beginning of
4281 a GNU statement-expression. */
4282 if (cp_parser_allow_gnu_extensions_p (parser)
4283 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4285 /* Statement-expressions are not allowed by the standard. */
4286 pedwarn (token->location, OPT_Wpedantic,
4287 "ISO C++ forbids braced-groups within expressions");
4289 /* And they're not allowed outside of a function-body; you
4290 cannot, for example, write:
4292 int i = ({ int j = 3; j + 1; });
4294 at class or namespace scope. */
4295 if (!parser->in_function_body
4296 || parser->in_template_argument_list_p)
4298 error_at (token->location,
4299 "statement-expressions are not allowed outside "
4300 "functions nor in template-argument lists");
4301 cp_parser_skip_to_end_of_block_or_statement (parser);
4302 expr = error_mark_node;
4304 else
4306 /* Start the statement-expression. */
4307 expr = begin_stmt_expr ();
4308 /* Parse the compound-statement. */
4309 cp_parser_compound_statement (parser, expr, false, false);
4310 /* Finish up. */
4311 expr = finish_stmt_expr (expr, false);
4314 else
4316 /* Parse the parenthesized expression. */
4317 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4318 /* Let the front end know that this expression was
4319 enclosed in parentheses. This matters in case, for
4320 example, the expression is of the form `A::B', since
4321 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4322 not. */
4323 expr = finish_parenthesized_expr (expr);
4324 /* DR 705: Wrapping an unqualified name in parentheses
4325 suppresses arg-dependent lookup. We want to pass back
4326 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4327 (c++/37862), but none of the others. */
4328 if (*idk != CP_ID_KIND_QUALIFIED)
4329 *idk = CP_ID_KIND_NONE;
4331 /* The `>' token might be the end of a template-id or
4332 template-parameter-list now. */
4333 parser->greater_than_is_operator_p
4334 = saved_greater_than_is_operator_p;
4335 /* Consume the `)'. */
4336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4337 cp_parser_skip_to_end_of_statement (parser);
4339 return expr;
4342 case CPP_OPEN_SQUARE:
4343 if (c_dialect_objc ())
4344 /* We have an Objective-C++ message. */
4345 return cp_parser_objc_expression (parser);
4347 tree lam = cp_parser_lambda_expression (parser);
4348 /* Don't warn about a failed tentative parse. */
4349 if (cp_parser_error_occurred (parser))
4350 return error_mark_node;
4351 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4352 return lam;
4355 case CPP_OBJC_STRING:
4356 if (c_dialect_objc ())
4357 /* We have an Objective-C++ string literal. */
4358 return cp_parser_objc_expression (parser);
4359 cp_parser_error (parser, "expected primary-expression");
4360 return error_mark_node;
4362 case CPP_KEYWORD:
4363 switch (token->keyword)
4365 /* These two are the boolean literals. */
4366 case RID_TRUE:
4367 cp_lexer_consume_token (parser->lexer);
4368 return boolean_true_node;
4369 case RID_FALSE:
4370 cp_lexer_consume_token (parser->lexer);
4371 return boolean_false_node;
4373 /* The `__null' literal. */
4374 case RID_NULL:
4375 cp_lexer_consume_token (parser->lexer);
4376 return null_node;
4378 /* The `nullptr' literal. */
4379 case RID_NULLPTR:
4380 cp_lexer_consume_token (parser->lexer);
4381 return nullptr_node;
4383 /* Recognize the `this' keyword. */
4384 case RID_THIS:
4385 cp_lexer_consume_token (parser->lexer);
4386 if (parser->local_variables_forbidden_p)
4388 error_at (token->location,
4389 "%<this%> may not be used in this context");
4390 return error_mark_node;
4392 /* Pointers cannot appear in constant-expressions. */
4393 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4394 return error_mark_node;
4395 return finish_this_expr ();
4397 /* The `operator' keyword can be the beginning of an
4398 id-expression. */
4399 case RID_OPERATOR:
4400 goto id_expression;
4402 case RID_FUNCTION_NAME:
4403 case RID_PRETTY_FUNCTION_NAME:
4404 case RID_C99_FUNCTION_NAME:
4406 non_integral_constant name;
4408 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4409 __func__ are the names of variables -- but they are
4410 treated specially. Therefore, they are handled here,
4411 rather than relying on the generic id-expression logic
4412 below. Grammatically, these names are id-expressions.
4414 Consume the token. */
4415 token = cp_lexer_consume_token (parser->lexer);
4417 switch (token->keyword)
4419 case RID_FUNCTION_NAME:
4420 name = NIC_FUNC_NAME;
4421 break;
4422 case RID_PRETTY_FUNCTION_NAME:
4423 name = NIC_PRETTY_FUNC;
4424 break;
4425 case RID_C99_FUNCTION_NAME:
4426 name = NIC_C99_FUNC;
4427 break;
4428 default:
4429 gcc_unreachable ();
4432 if (cp_parser_non_integral_constant_expression (parser, name))
4433 return error_mark_node;
4435 /* Look up the name. */
4436 return finish_fname (token->u.value);
4439 case RID_VA_ARG:
4441 tree expression;
4442 tree type;
4443 source_location type_location;
4445 /* The `__builtin_va_arg' construct is used to handle
4446 `va_arg'. Consume the `__builtin_va_arg' token. */
4447 cp_lexer_consume_token (parser->lexer);
4448 /* Look for the opening `('. */
4449 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4450 /* Now, parse the assignment-expression. */
4451 expression = cp_parser_assignment_expression (parser,
4452 /*cast_p=*/false, NULL);
4453 /* Look for the `,'. */
4454 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4455 type_location = cp_lexer_peek_token (parser->lexer)->location;
4456 /* Parse the type-id. */
4457 type = cp_parser_type_id (parser);
4458 /* Look for the closing `)'. */
4459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4460 /* Using `va_arg' in a constant-expression is not
4461 allowed. */
4462 if (cp_parser_non_integral_constant_expression (parser,
4463 NIC_VA_ARG))
4464 return error_mark_node;
4465 return build_x_va_arg (type_location, expression, type);
4468 case RID_OFFSETOF:
4469 return cp_parser_builtin_offsetof (parser);
4471 case RID_HAS_NOTHROW_ASSIGN:
4472 case RID_HAS_NOTHROW_CONSTRUCTOR:
4473 case RID_HAS_NOTHROW_COPY:
4474 case RID_HAS_TRIVIAL_ASSIGN:
4475 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4476 case RID_HAS_TRIVIAL_COPY:
4477 case RID_HAS_TRIVIAL_DESTRUCTOR:
4478 case RID_HAS_VIRTUAL_DESTRUCTOR:
4479 case RID_IS_ABSTRACT:
4480 case RID_IS_BASE_OF:
4481 case RID_IS_CLASS:
4482 case RID_IS_CONVERTIBLE_TO:
4483 case RID_IS_EMPTY:
4484 case RID_IS_ENUM:
4485 case RID_IS_FINAL:
4486 case RID_IS_LITERAL_TYPE:
4487 case RID_IS_POD:
4488 case RID_IS_POLYMORPHIC:
4489 case RID_IS_STD_LAYOUT:
4490 case RID_IS_TRIVIAL:
4491 case RID_IS_UNION:
4492 return cp_parser_trait_expr (parser, token->keyword);
4494 /* Objective-C++ expressions. */
4495 case RID_AT_ENCODE:
4496 case RID_AT_PROTOCOL:
4497 case RID_AT_SELECTOR:
4498 return cp_parser_objc_expression (parser);
4500 case RID_TEMPLATE:
4501 if (parser->in_function_body
4502 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4503 == CPP_LESS))
4505 error_at (token->location,
4506 "a template declaration cannot appear at block scope");
4507 cp_parser_skip_to_end_of_block_or_statement (parser);
4508 return error_mark_node;
4510 default:
4511 cp_parser_error (parser, "expected primary-expression");
4512 return error_mark_node;
4515 /* An id-expression can start with either an identifier, a
4516 `::' as the beginning of a qualified-id, or the "operator"
4517 keyword. */
4518 case CPP_NAME:
4519 case CPP_SCOPE:
4520 case CPP_TEMPLATE_ID:
4521 case CPP_NESTED_NAME_SPECIFIER:
4523 tree id_expression;
4524 tree decl;
4525 const char *error_msg;
4526 bool template_p;
4527 bool done;
4528 cp_token *id_expr_token;
4530 id_expression:
4531 /* Parse the id-expression. */
4532 id_expression
4533 = cp_parser_id_expression (parser,
4534 /*template_keyword_p=*/false,
4535 /*check_dependency_p=*/true,
4536 &template_p,
4537 /*declarator_p=*/false,
4538 /*optional_p=*/false);
4539 if (id_expression == error_mark_node)
4540 return error_mark_node;
4541 id_expr_token = token;
4542 token = cp_lexer_peek_token (parser->lexer);
4543 done = (token->type != CPP_OPEN_SQUARE
4544 && token->type != CPP_OPEN_PAREN
4545 && token->type != CPP_DOT
4546 && token->type != CPP_DEREF
4547 && token->type != CPP_PLUS_PLUS
4548 && token->type != CPP_MINUS_MINUS);
4549 /* If we have a template-id, then no further lookup is
4550 required. If the template-id was for a template-class, we
4551 will sometimes have a TYPE_DECL at this point. */
4552 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4553 || TREE_CODE (id_expression) == TYPE_DECL)
4554 decl = id_expression;
4555 /* Look up the name. */
4556 else
4558 tree ambiguous_decls;
4560 /* If we already know that this lookup is ambiguous, then
4561 we've already issued an error message; there's no reason
4562 to check again. */
4563 if (id_expr_token->type == CPP_NAME
4564 && id_expr_token->error_reported)
4566 cp_parser_simulate_error (parser);
4567 return error_mark_node;
4570 decl = cp_parser_lookup_name (parser, id_expression,
4571 none_type,
4572 template_p,
4573 /*is_namespace=*/false,
4574 /*check_dependency=*/true,
4575 &ambiguous_decls,
4576 id_expr_token->location);
4577 /* If the lookup was ambiguous, an error will already have
4578 been issued. */
4579 if (ambiguous_decls)
4580 return error_mark_node;
4582 /* In Objective-C++, we may have an Objective-C 2.0
4583 dot-syntax for classes here. */
4584 if (c_dialect_objc ()
4585 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4586 && TREE_CODE (decl) == TYPE_DECL
4587 && objc_is_class_name (decl))
4589 tree component;
4590 cp_lexer_consume_token (parser->lexer);
4591 component = cp_parser_identifier (parser);
4592 if (component == error_mark_node)
4593 return error_mark_node;
4595 return objc_build_class_component_ref (id_expression, component);
4598 /* In Objective-C++, an instance variable (ivar) may be preferred
4599 to whatever cp_parser_lookup_name() found. */
4600 decl = objc_lookup_ivar (decl, id_expression);
4602 /* If name lookup gives us a SCOPE_REF, then the
4603 qualifying scope was dependent. */
4604 if (TREE_CODE (decl) == SCOPE_REF)
4606 /* At this point, we do not know if DECL is a valid
4607 integral constant expression. We assume that it is
4608 in fact such an expression, so that code like:
4610 template <int N> struct A {
4611 int a[B<N>::i];
4614 is accepted. At template-instantiation time, we
4615 will check that B<N>::i is actually a constant. */
4616 return decl;
4618 /* Check to see if DECL is a local variable in a context
4619 where that is forbidden. */
4620 if (parser->local_variables_forbidden_p
4621 && local_variable_p (decl))
4623 /* It might be that we only found DECL because we are
4624 trying to be generous with pre-ISO scoping rules.
4625 For example, consider:
4627 int i;
4628 void g() {
4629 for (int i = 0; i < 10; ++i) {}
4630 extern void f(int j = i);
4633 Here, name look up will originally find the out
4634 of scope `i'. We need to issue a warning message,
4635 but then use the global `i'. */
4636 decl = check_for_out_of_scope_variable (decl);
4637 if (local_variable_p (decl))
4639 error_at (id_expr_token->location,
4640 "local variable %qD may not appear in this context",
4641 decl);
4642 return error_mark_node;
4647 decl = (finish_id_expression
4648 (id_expression, decl, parser->scope,
4649 idk,
4650 parser->integral_constant_expression_p,
4651 parser->allow_non_integral_constant_expression_p,
4652 &parser->non_integral_constant_expression_p,
4653 template_p, done, address_p,
4654 template_arg_p,
4655 &error_msg,
4656 id_expr_token->location));
4657 if (error_msg)
4658 cp_parser_error (parser, error_msg);
4659 return decl;
4662 /* Anything else is an error. */
4663 default:
4664 cp_parser_error (parser, "expected primary-expression");
4665 return error_mark_node;
4669 static inline tree
4670 cp_parser_primary_expression (cp_parser *parser,
4671 bool address_p,
4672 bool cast_p,
4673 bool template_arg_p,
4674 cp_id_kind *idk)
4676 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4677 /*decltype*/false, idk);
4680 /* Parse an id-expression.
4682 id-expression:
4683 unqualified-id
4684 qualified-id
4686 qualified-id:
4687 :: [opt] nested-name-specifier template [opt] unqualified-id
4688 :: identifier
4689 :: operator-function-id
4690 :: template-id
4692 Return a representation of the unqualified portion of the
4693 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4694 a `::' or nested-name-specifier.
4696 Often, if the id-expression was a qualified-id, the caller will
4697 want to make a SCOPE_REF to represent the qualified-id. This
4698 function does not do this in order to avoid wastefully creating
4699 SCOPE_REFs when they are not required.
4701 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4702 `template' keyword.
4704 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4705 uninstantiated templates.
4707 If *TEMPLATE_P is non-NULL, it is set to true iff the
4708 `template' keyword is used to explicitly indicate that the entity
4709 named is a template.
4711 If DECLARATOR_P is true, the id-expression is appearing as part of
4712 a declarator, rather than as part of an expression. */
4714 static tree
4715 cp_parser_id_expression (cp_parser *parser,
4716 bool template_keyword_p,
4717 bool check_dependency_p,
4718 bool *template_p,
4719 bool declarator_p,
4720 bool optional_p)
4722 bool global_scope_p;
4723 bool nested_name_specifier_p;
4725 /* Assume the `template' keyword was not used. */
4726 if (template_p)
4727 *template_p = template_keyword_p;
4729 /* Look for the optional `::' operator. */
4730 global_scope_p
4731 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4732 != NULL_TREE);
4733 /* Look for the optional nested-name-specifier. */
4734 nested_name_specifier_p
4735 = (cp_parser_nested_name_specifier_opt (parser,
4736 /*typename_keyword_p=*/false,
4737 check_dependency_p,
4738 /*type_p=*/false,
4739 declarator_p)
4740 != NULL_TREE);
4741 /* If there is a nested-name-specifier, then we are looking at
4742 the first qualified-id production. */
4743 if (nested_name_specifier_p)
4745 tree saved_scope;
4746 tree saved_object_scope;
4747 tree saved_qualifying_scope;
4748 tree unqualified_id;
4749 bool is_template;
4751 /* See if the next token is the `template' keyword. */
4752 if (!template_p)
4753 template_p = &is_template;
4754 *template_p = cp_parser_optional_template_keyword (parser);
4755 /* Name lookup we do during the processing of the
4756 unqualified-id might obliterate SCOPE. */
4757 saved_scope = parser->scope;
4758 saved_object_scope = parser->object_scope;
4759 saved_qualifying_scope = parser->qualifying_scope;
4760 /* Process the final unqualified-id. */
4761 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4762 check_dependency_p,
4763 declarator_p,
4764 /*optional_p=*/false);
4765 /* Restore the SAVED_SCOPE for our caller. */
4766 parser->scope = saved_scope;
4767 parser->object_scope = saved_object_scope;
4768 parser->qualifying_scope = saved_qualifying_scope;
4770 return unqualified_id;
4772 /* Otherwise, if we are in global scope, then we are looking at one
4773 of the other qualified-id productions. */
4774 else if (global_scope_p)
4776 cp_token *token;
4777 tree id;
4779 /* Peek at the next token. */
4780 token = cp_lexer_peek_token (parser->lexer);
4782 /* If it's an identifier, and the next token is not a "<", then
4783 we can avoid the template-id case. This is an optimization
4784 for this common case. */
4785 if (token->type == CPP_NAME
4786 && !cp_parser_nth_token_starts_template_argument_list_p
4787 (parser, 2))
4788 return cp_parser_identifier (parser);
4790 cp_parser_parse_tentatively (parser);
4791 /* Try a template-id. */
4792 id = cp_parser_template_id (parser,
4793 /*template_keyword_p=*/false,
4794 /*check_dependency_p=*/true,
4795 none_type,
4796 declarator_p);
4797 /* If that worked, we're done. */
4798 if (cp_parser_parse_definitely (parser))
4799 return id;
4801 /* Peek at the next token. (Changes in the token buffer may
4802 have invalidated the pointer obtained above.) */
4803 token = cp_lexer_peek_token (parser->lexer);
4805 switch (token->type)
4807 case CPP_NAME:
4808 return cp_parser_identifier (parser);
4810 case CPP_KEYWORD:
4811 if (token->keyword == RID_OPERATOR)
4812 return cp_parser_operator_function_id (parser);
4813 /* Fall through. */
4815 default:
4816 cp_parser_error (parser, "expected id-expression");
4817 return error_mark_node;
4820 else
4821 return cp_parser_unqualified_id (parser, template_keyword_p,
4822 /*check_dependency_p=*/true,
4823 declarator_p,
4824 optional_p);
4827 /* Parse an unqualified-id.
4829 unqualified-id:
4830 identifier
4831 operator-function-id
4832 conversion-function-id
4833 ~ class-name
4834 template-id
4836 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4837 keyword, in a construct like `A::template ...'.
4839 Returns a representation of unqualified-id. For the `identifier'
4840 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4841 production a BIT_NOT_EXPR is returned; the operand of the
4842 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4843 other productions, see the documentation accompanying the
4844 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4845 names are looked up in uninstantiated templates. If DECLARATOR_P
4846 is true, the unqualified-id is appearing as part of a declarator,
4847 rather than as part of an expression. */
4849 static tree
4850 cp_parser_unqualified_id (cp_parser* parser,
4851 bool template_keyword_p,
4852 bool check_dependency_p,
4853 bool declarator_p,
4854 bool optional_p)
4856 cp_token *token;
4858 /* Peek at the next token. */
4859 token = cp_lexer_peek_token (parser->lexer);
4861 switch (token->type)
4863 case CPP_NAME:
4865 tree id;
4867 /* We don't know yet whether or not this will be a
4868 template-id. */
4869 cp_parser_parse_tentatively (parser);
4870 /* Try a template-id. */
4871 id = cp_parser_template_id (parser, template_keyword_p,
4872 check_dependency_p,
4873 none_type,
4874 declarator_p);
4875 /* If it worked, we're done. */
4876 if (cp_parser_parse_definitely (parser))
4877 return id;
4878 /* Otherwise, it's an ordinary identifier. */
4879 return cp_parser_identifier (parser);
4882 case CPP_TEMPLATE_ID:
4883 return cp_parser_template_id (parser, template_keyword_p,
4884 check_dependency_p,
4885 none_type,
4886 declarator_p);
4888 case CPP_COMPL:
4890 tree type_decl;
4891 tree qualifying_scope;
4892 tree object_scope;
4893 tree scope;
4894 bool done;
4896 /* Consume the `~' token. */
4897 cp_lexer_consume_token (parser->lexer);
4898 /* Parse the class-name. The standard, as written, seems to
4899 say that:
4901 template <typename T> struct S { ~S (); };
4902 template <typename T> S<T>::~S() {}
4904 is invalid, since `~' must be followed by a class-name, but
4905 `S<T>' is dependent, and so not known to be a class.
4906 That's not right; we need to look in uninstantiated
4907 templates. A further complication arises from:
4909 template <typename T> void f(T t) {
4910 t.T::~T();
4913 Here, it is not possible to look up `T' in the scope of `T'
4914 itself. We must look in both the current scope, and the
4915 scope of the containing complete expression.
4917 Yet another issue is:
4919 struct S {
4920 int S;
4921 ~S();
4924 S::~S() {}
4926 The standard does not seem to say that the `S' in `~S'
4927 should refer to the type `S' and not the data member
4928 `S::S'. */
4930 /* DR 244 says that we look up the name after the "~" in the
4931 same scope as we looked up the qualifying name. That idea
4932 isn't fully worked out; it's more complicated than that. */
4933 scope = parser->scope;
4934 object_scope = parser->object_scope;
4935 qualifying_scope = parser->qualifying_scope;
4937 /* Check for invalid scopes. */
4938 if (scope == error_mark_node)
4940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4941 cp_lexer_consume_token (parser->lexer);
4942 return error_mark_node;
4944 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4946 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4947 error_at (token->location,
4948 "scope %qT before %<~%> is not a class-name",
4949 scope);
4950 cp_parser_simulate_error (parser);
4951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4952 cp_lexer_consume_token (parser->lexer);
4953 return error_mark_node;
4955 gcc_assert (!scope || TYPE_P (scope));
4957 /* If the name is of the form "X::~X" it's OK even if X is a
4958 typedef. */
4959 token = cp_lexer_peek_token (parser->lexer);
4960 if (scope
4961 && token->type == CPP_NAME
4962 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4963 != CPP_LESS)
4964 && (token->u.value == TYPE_IDENTIFIER (scope)
4965 || (CLASS_TYPE_P (scope)
4966 && constructor_name_p (token->u.value, scope))))
4968 cp_lexer_consume_token (parser->lexer);
4969 return build_nt (BIT_NOT_EXPR, scope);
4972 /* ~auto means the destructor of whatever the object is. */
4973 if (cp_parser_is_keyword (token, RID_AUTO))
4975 if (cxx_dialect < cxx14)
4976 pedwarn (input_location, 0,
4977 "%<~auto%> only available with "
4978 "-std=c++14 or -std=gnu++14");
4979 cp_lexer_consume_token (parser->lexer);
4980 return build_nt (BIT_NOT_EXPR, make_auto ());
4983 /* If there was an explicit qualification (S::~T), first look
4984 in the scope given by the qualification (i.e., S).
4986 Note: in the calls to cp_parser_class_name below we pass
4987 typename_type so that lookup finds the injected-class-name
4988 rather than the constructor. */
4989 done = false;
4990 type_decl = NULL_TREE;
4991 if (scope)
4993 cp_parser_parse_tentatively (parser);
4994 type_decl = cp_parser_class_name (parser,
4995 /*typename_keyword_p=*/false,
4996 /*template_keyword_p=*/false,
4997 typename_type,
4998 /*check_dependency=*/false,
4999 /*class_head_p=*/false,
5000 declarator_p);
5001 if (cp_parser_parse_definitely (parser))
5002 done = true;
5004 /* In "N::S::~S", look in "N" as well. */
5005 if (!done && scope && qualifying_scope)
5007 cp_parser_parse_tentatively (parser);
5008 parser->scope = qualifying_scope;
5009 parser->object_scope = NULL_TREE;
5010 parser->qualifying_scope = NULL_TREE;
5011 type_decl
5012 = cp_parser_class_name (parser,
5013 /*typename_keyword_p=*/false,
5014 /*template_keyword_p=*/false,
5015 typename_type,
5016 /*check_dependency=*/false,
5017 /*class_head_p=*/false,
5018 declarator_p);
5019 if (cp_parser_parse_definitely (parser))
5020 done = true;
5022 /* In "p->S::~T", look in the scope given by "*p" as well. */
5023 else if (!done && object_scope)
5025 cp_parser_parse_tentatively (parser);
5026 parser->scope = object_scope;
5027 parser->object_scope = NULL_TREE;
5028 parser->qualifying_scope = NULL_TREE;
5029 type_decl
5030 = cp_parser_class_name (parser,
5031 /*typename_keyword_p=*/false,
5032 /*template_keyword_p=*/false,
5033 typename_type,
5034 /*check_dependency=*/false,
5035 /*class_head_p=*/false,
5036 declarator_p);
5037 if (cp_parser_parse_definitely (parser))
5038 done = true;
5040 /* Look in the surrounding context. */
5041 if (!done)
5043 parser->scope = NULL_TREE;
5044 parser->object_scope = NULL_TREE;
5045 parser->qualifying_scope = NULL_TREE;
5046 if (processing_template_decl)
5047 cp_parser_parse_tentatively (parser);
5048 type_decl
5049 = cp_parser_class_name (parser,
5050 /*typename_keyword_p=*/false,
5051 /*template_keyword_p=*/false,
5052 typename_type,
5053 /*check_dependency=*/false,
5054 /*class_head_p=*/false,
5055 declarator_p);
5056 if (processing_template_decl
5057 && ! cp_parser_parse_definitely (parser))
5059 /* We couldn't find a type with this name, so just accept
5060 it and check for a match at instantiation time. */
5061 type_decl = cp_parser_identifier (parser);
5062 if (type_decl != error_mark_node)
5063 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5064 return type_decl;
5067 /* If an error occurred, assume that the name of the
5068 destructor is the same as the name of the qualifying
5069 class. That allows us to keep parsing after running
5070 into ill-formed destructor names. */
5071 if (type_decl == error_mark_node && scope)
5072 return build_nt (BIT_NOT_EXPR, scope);
5073 else if (type_decl == error_mark_node)
5074 return error_mark_node;
5076 /* Check that destructor name and scope match. */
5077 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5079 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5080 error_at (token->location,
5081 "declaration of %<~%T%> as member of %qT",
5082 type_decl, scope);
5083 cp_parser_simulate_error (parser);
5084 return error_mark_node;
5087 /* [class.dtor]
5089 A typedef-name that names a class shall not be used as the
5090 identifier in the declarator for a destructor declaration. */
5091 if (declarator_p
5092 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5093 && !DECL_SELF_REFERENCE_P (type_decl)
5094 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5095 error_at (token->location,
5096 "typedef-name %qD used as destructor declarator",
5097 type_decl);
5099 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5102 case CPP_KEYWORD:
5103 if (token->keyword == RID_OPERATOR)
5105 tree id;
5107 /* This could be a template-id, so we try that first. */
5108 cp_parser_parse_tentatively (parser);
5109 /* Try a template-id. */
5110 id = cp_parser_template_id (parser, template_keyword_p,
5111 /*check_dependency_p=*/true,
5112 none_type,
5113 declarator_p);
5114 /* If that worked, we're done. */
5115 if (cp_parser_parse_definitely (parser))
5116 return id;
5117 /* We still don't know whether we're looking at an
5118 operator-function-id or a conversion-function-id. */
5119 cp_parser_parse_tentatively (parser);
5120 /* Try an operator-function-id. */
5121 id = cp_parser_operator_function_id (parser);
5122 /* If that didn't work, try a conversion-function-id. */
5123 if (!cp_parser_parse_definitely (parser))
5124 id = cp_parser_conversion_function_id (parser);
5125 else if (UDLIT_OPER_P (id))
5127 /* 17.6.3.3.5 */
5128 const char *name = UDLIT_OP_SUFFIX (id);
5129 if (name[0] != '_' && !in_system_header_at (input_location)
5130 && declarator_p)
5131 warning (0, "literal operator suffixes not preceded by %<_%>"
5132 " are reserved for future standardization");
5135 return id;
5137 /* Fall through. */
5139 default:
5140 if (optional_p)
5141 return NULL_TREE;
5142 cp_parser_error (parser, "expected unqualified-id");
5143 return error_mark_node;
5147 /* Parse an (optional) nested-name-specifier.
5149 nested-name-specifier: [C++98]
5150 class-or-namespace-name :: nested-name-specifier [opt]
5151 class-or-namespace-name :: template nested-name-specifier [opt]
5153 nested-name-specifier: [C++0x]
5154 type-name ::
5155 namespace-name ::
5156 nested-name-specifier identifier ::
5157 nested-name-specifier template [opt] simple-template-id ::
5159 PARSER->SCOPE should be set appropriately before this function is
5160 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5161 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5162 in name lookups.
5164 Sets PARSER->SCOPE to the class (TYPE) or namespace
5165 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5166 it unchanged if there is no nested-name-specifier. Returns the new
5167 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5169 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5170 part of a declaration and/or decl-specifier. */
5172 static tree
5173 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5174 bool typename_keyword_p,
5175 bool check_dependency_p,
5176 bool type_p,
5177 bool is_declaration)
5179 bool success = false;
5180 cp_token_position start = 0;
5181 cp_token *token;
5183 /* Remember where the nested-name-specifier starts. */
5184 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5186 start = cp_lexer_token_position (parser->lexer, false);
5187 push_deferring_access_checks (dk_deferred);
5190 while (true)
5192 tree new_scope;
5193 tree old_scope;
5194 tree saved_qualifying_scope;
5195 bool template_keyword_p;
5197 /* Spot cases that cannot be the beginning of a
5198 nested-name-specifier. */
5199 token = cp_lexer_peek_token (parser->lexer);
5201 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5202 the already parsed nested-name-specifier. */
5203 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5205 /* Grab the nested-name-specifier and continue the loop. */
5206 cp_parser_pre_parsed_nested_name_specifier (parser);
5207 /* If we originally encountered this nested-name-specifier
5208 with IS_DECLARATION set to false, we will not have
5209 resolved TYPENAME_TYPEs, so we must do so here. */
5210 if (is_declaration
5211 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5213 new_scope = resolve_typename_type (parser->scope,
5214 /*only_current_p=*/false);
5215 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5216 parser->scope = new_scope;
5218 success = true;
5219 continue;
5222 /* Spot cases that cannot be the beginning of a
5223 nested-name-specifier. On the second and subsequent times
5224 through the loop, we look for the `template' keyword. */
5225 if (success && token->keyword == RID_TEMPLATE)
5227 /* A template-id can start a nested-name-specifier. */
5228 else if (token->type == CPP_TEMPLATE_ID)
5230 /* DR 743: decltype can be used in a nested-name-specifier. */
5231 else if (token_is_decltype (token))
5233 else
5235 /* If the next token is not an identifier, then it is
5236 definitely not a type-name or namespace-name. */
5237 if (token->type != CPP_NAME)
5238 break;
5239 /* If the following token is neither a `<' (to begin a
5240 template-id), nor a `::', then we are not looking at a
5241 nested-name-specifier. */
5242 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5244 if (token->type == CPP_COLON
5245 && parser->colon_corrects_to_scope_p
5246 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5248 error_at (token->location,
5249 "found %<:%> in nested-name-specifier, expected %<::%>");
5250 token->type = CPP_SCOPE;
5253 if (token->type != CPP_SCOPE
5254 && !cp_parser_nth_token_starts_template_argument_list_p
5255 (parser, 2))
5256 break;
5259 /* The nested-name-specifier is optional, so we parse
5260 tentatively. */
5261 cp_parser_parse_tentatively (parser);
5263 /* Look for the optional `template' keyword, if this isn't the
5264 first time through the loop. */
5265 if (success)
5266 template_keyword_p = cp_parser_optional_template_keyword (parser);
5267 else
5268 template_keyword_p = false;
5270 /* Save the old scope since the name lookup we are about to do
5271 might destroy it. */
5272 old_scope = parser->scope;
5273 saved_qualifying_scope = parser->qualifying_scope;
5274 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5275 look up names in "X<T>::I" in order to determine that "Y" is
5276 a template. So, if we have a typename at this point, we make
5277 an effort to look through it. */
5278 if (is_declaration
5279 && !typename_keyword_p
5280 && parser->scope
5281 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5282 parser->scope = resolve_typename_type (parser->scope,
5283 /*only_current_p=*/false);
5284 /* Parse the qualifying entity. */
5285 new_scope
5286 = cp_parser_qualifying_entity (parser,
5287 typename_keyword_p,
5288 template_keyword_p,
5289 check_dependency_p,
5290 type_p,
5291 is_declaration);
5292 /* Look for the `::' token. */
5293 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5295 /* If we found what we wanted, we keep going; otherwise, we're
5296 done. */
5297 if (!cp_parser_parse_definitely (parser))
5299 bool error_p = false;
5301 /* Restore the OLD_SCOPE since it was valid before the
5302 failed attempt at finding the last
5303 class-or-namespace-name. */
5304 parser->scope = old_scope;
5305 parser->qualifying_scope = saved_qualifying_scope;
5307 /* If the next token is a decltype, and the one after that is a
5308 `::', then the decltype has failed to resolve to a class or
5309 enumeration type. Give this error even when parsing
5310 tentatively since it can't possibly be valid--and we're going
5311 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5312 won't get another chance.*/
5313 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5314 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5315 == CPP_SCOPE))
5317 token = cp_lexer_consume_token (parser->lexer);
5318 error_at (token->location, "decltype evaluates to %qT, "
5319 "which is not a class or enumeration type",
5320 token->u.value);
5321 parser->scope = error_mark_node;
5322 error_p = true;
5323 /* As below. */
5324 success = true;
5325 cp_lexer_consume_token (parser->lexer);
5328 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5329 break;
5330 /* If the next token is an identifier, and the one after
5331 that is a `::', then any valid interpretation would have
5332 found a class-or-namespace-name. */
5333 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5334 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5335 == CPP_SCOPE)
5336 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5337 != CPP_COMPL))
5339 token = cp_lexer_consume_token (parser->lexer);
5340 if (!error_p)
5342 if (!token->error_reported)
5344 tree decl;
5345 tree ambiguous_decls;
5347 decl = cp_parser_lookup_name (parser, token->u.value,
5348 none_type,
5349 /*is_template=*/false,
5350 /*is_namespace=*/false,
5351 /*check_dependency=*/true,
5352 &ambiguous_decls,
5353 token->location);
5354 if (TREE_CODE (decl) == TEMPLATE_DECL)
5355 error_at (token->location,
5356 "%qD used without template parameters",
5357 decl);
5358 else if (ambiguous_decls)
5360 // cp_parser_lookup_name has the same diagnostic,
5361 // thus make sure to emit it at most once.
5362 if (cp_parser_uncommitted_to_tentative_parse_p
5363 (parser))
5365 error_at (token->location,
5366 "reference to %qD is ambiguous",
5367 token->u.value);
5368 print_candidates (ambiguous_decls);
5370 decl = error_mark_node;
5372 else
5374 if (cxx_dialect != cxx98)
5375 cp_parser_name_lookup_error
5376 (parser, token->u.value, decl, NLE_NOT_CXX98,
5377 token->location);
5378 else
5379 cp_parser_name_lookup_error
5380 (parser, token->u.value, decl, NLE_CXX98,
5381 token->location);
5384 parser->scope = error_mark_node;
5385 error_p = true;
5386 /* Treat this as a successful nested-name-specifier
5387 due to:
5389 [basic.lookup.qual]
5391 If the name found is not a class-name (clause
5392 _class_) or namespace-name (_namespace.def_), the
5393 program is ill-formed. */
5394 success = true;
5396 cp_lexer_consume_token (parser->lexer);
5398 break;
5400 /* We've found one valid nested-name-specifier. */
5401 success = true;
5402 /* Name lookup always gives us a DECL. */
5403 if (TREE_CODE (new_scope) == TYPE_DECL)
5404 new_scope = TREE_TYPE (new_scope);
5405 /* Uses of "template" must be followed by actual templates. */
5406 if (template_keyword_p
5407 && !(CLASS_TYPE_P (new_scope)
5408 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5409 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5410 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5411 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5412 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5413 == TEMPLATE_ID_EXPR)))
5414 permerror (input_location, TYPE_P (new_scope)
5415 ? G_("%qT is not a template")
5416 : G_("%qD is not a template"),
5417 new_scope);
5418 /* If it is a class scope, try to complete it; we are about to
5419 be looking up names inside the class. */
5420 if (TYPE_P (new_scope)
5421 /* Since checking types for dependency can be expensive,
5422 avoid doing it if the type is already complete. */
5423 && !COMPLETE_TYPE_P (new_scope)
5424 /* Do not try to complete dependent types. */
5425 && !dependent_type_p (new_scope))
5427 new_scope = complete_type (new_scope);
5428 /* If it is a typedef to current class, use the current
5429 class instead, as the typedef won't have any names inside
5430 it yet. */
5431 if (!COMPLETE_TYPE_P (new_scope)
5432 && currently_open_class (new_scope))
5433 new_scope = TYPE_MAIN_VARIANT (new_scope);
5435 /* Make sure we look in the right scope the next time through
5436 the loop. */
5437 parser->scope = new_scope;
5440 /* If parsing tentatively, replace the sequence of tokens that makes
5441 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5442 token. That way, should we re-parse the token stream, we will
5443 not have to repeat the effort required to do the parse, nor will
5444 we issue duplicate error messages. */
5445 if (success && start)
5447 cp_token *token;
5449 token = cp_lexer_token_at (parser->lexer, start);
5450 /* Reset the contents of the START token. */
5451 token->type = CPP_NESTED_NAME_SPECIFIER;
5452 /* Retrieve any deferred checks. Do not pop this access checks yet
5453 so the memory will not be reclaimed during token replacing below. */
5454 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5455 token->u.tree_check_value->value = parser->scope;
5456 token->u.tree_check_value->checks = get_deferred_access_checks ();
5457 token->u.tree_check_value->qualifying_scope =
5458 parser->qualifying_scope;
5459 token->keyword = RID_MAX;
5461 /* Purge all subsequent tokens. */
5462 cp_lexer_purge_tokens_after (parser->lexer, start);
5465 if (start)
5466 pop_to_parent_deferring_access_checks ();
5468 return success ? parser->scope : NULL_TREE;
5471 /* Parse a nested-name-specifier. See
5472 cp_parser_nested_name_specifier_opt for details. This function
5473 behaves identically, except that it will an issue an error if no
5474 nested-name-specifier is present. */
5476 static tree
5477 cp_parser_nested_name_specifier (cp_parser *parser,
5478 bool typename_keyword_p,
5479 bool check_dependency_p,
5480 bool type_p,
5481 bool is_declaration)
5483 tree scope;
5485 /* Look for the nested-name-specifier. */
5486 scope = cp_parser_nested_name_specifier_opt (parser,
5487 typename_keyword_p,
5488 check_dependency_p,
5489 type_p,
5490 is_declaration);
5491 /* If it was not present, issue an error message. */
5492 if (!scope)
5494 cp_parser_error (parser, "expected nested-name-specifier");
5495 parser->scope = NULL_TREE;
5498 return scope;
5501 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5502 this is either a class-name or a namespace-name (which corresponds
5503 to the class-or-namespace-name production in the grammar). For
5504 C++0x, it can also be a type-name that refers to an enumeration
5505 type or a simple-template-id.
5507 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5508 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5509 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5510 TYPE_P is TRUE iff the next name should be taken as a class-name,
5511 even the same name is declared to be another entity in the same
5512 scope.
5514 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5515 specified by the class-or-namespace-name. If neither is found the
5516 ERROR_MARK_NODE is returned. */
5518 static tree
5519 cp_parser_qualifying_entity (cp_parser *parser,
5520 bool typename_keyword_p,
5521 bool template_keyword_p,
5522 bool check_dependency_p,
5523 bool type_p,
5524 bool is_declaration)
5526 tree saved_scope;
5527 tree saved_qualifying_scope;
5528 tree saved_object_scope;
5529 tree scope;
5530 bool only_class_p;
5531 bool successful_parse_p;
5533 /* DR 743: decltype can appear in a nested-name-specifier. */
5534 if (cp_lexer_next_token_is_decltype (parser->lexer))
5536 scope = cp_parser_decltype (parser);
5537 if (TREE_CODE (scope) != ENUMERAL_TYPE
5538 && !MAYBE_CLASS_TYPE_P (scope))
5540 cp_parser_simulate_error (parser);
5541 return error_mark_node;
5543 if (TYPE_NAME (scope))
5544 scope = TYPE_NAME (scope);
5545 return scope;
5548 /* Before we try to parse the class-name, we must save away the
5549 current PARSER->SCOPE since cp_parser_class_name will destroy
5550 it. */
5551 saved_scope = parser->scope;
5552 saved_qualifying_scope = parser->qualifying_scope;
5553 saved_object_scope = parser->object_scope;
5554 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5555 there is no need to look for a namespace-name. */
5556 only_class_p = template_keyword_p
5557 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5558 if (!only_class_p)
5559 cp_parser_parse_tentatively (parser);
5560 scope = cp_parser_class_name (parser,
5561 typename_keyword_p,
5562 template_keyword_p,
5563 type_p ? class_type : none_type,
5564 check_dependency_p,
5565 /*class_head_p=*/false,
5566 is_declaration);
5567 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5568 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5569 if (!only_class_p
5570 && cxx_dialect != cxx98
5571 && !successful_parse_p)
5573 /* Restore the saved scope. */
5574 parser->scope = saved_scope;
5575 parser->qualifying_scope = saved_qualifying_scope;
5576 parser->object_scope = saved_object_scope;
5578 /* Parse tentatively. */
5579 cp_parser_parse_tentatively (parser);
5581 /* Parse a type-name */
5582 scope = cp_parser_type_name (parser);
5584 /* "If the name found does not designate a namespace or a class,
5585 enumeration, or dependent type, the program is ill-formed."
5587 We cover classes and dependent types above and namespaces below,
5588 so this code is only looking for enums. */
5589 if (!scope || TREE_CODE (scope) != TYPE_DECL
5590 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5591 cp_parser_simulate_error (parser);
5593 successful_parse_p = cp_parser_parse_definitely (parser);
5595 /* If that didn't work, try for a namespace-name. */
5596 if (!only_class_p && !successful_parse_p)
5598 /* Restore the saved scope. */
5599 parser->scope = saved_scope;
5600 parser->qualifying_scope = saved_qualifying_scope;
5601 parser->object_scope = saved_object_scope;
5602 /* If we are not looking at an identifier followed by the scope
5603 resolution operator, then this is not part of a
5604 nested-name-specifier. (Note that this function is only used
5605 to parse the components of a nested-name-specifier.) */
5606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5607 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5608 return error_mark_node;
5609 scope = cp_parser_namespace_name (parser);
5612 return scope;
5615 /* Return true if we are looking at a compound-literal, false otherwise. */
5617 static bool
5618 cp_parser_compound_literal_p (cp_parser *parser)
5620 /* Consume the `('. */
5621 cp_lexer_consume_token (parser->lexer);
5623 cp_lexer_save_tokens (parser->lexer);
5625 /* Skip tokens until the next token is a closing parenthesis.
5626 If we find the closing `)', and the next token is a `{', then
5627 we are looking at a compound-literal. */
5628 bool compound_literal_p
5629 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5630 /*consume_paren=*/true)
5631 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5633 /* Roll back the tokens we skipped. */
5634 cp_lexer_rollback_tokens (parser->lexer);
5636 return compound_literal_p;
5639 /* Parse a postfix-expression.
5641 postfix-expression:
5642 primary-expression
5643 postfix-expression [ expression ]
5644 postfix-expression ( expression-list [opt] )
5645 simple-type-specifier ( expression-list [opt] )
5646 typename :: [opt] nested-name-specifier identifier
5647 ( expression-list [opt] )
5648 typename :: [opt] nested-name-specifier template [opt] template-id
5649 ( expression-list [opt] )
5650 postfix-expression . template [opt] id-expression
5651 postfix-expression -> template [opt] id-expression
5652 postfix-expression . pseudo-destructor-name
5653 postfix-expression -> pseudo-destructor-name
5654 postfix-expression ++
5655 postfix-expression --
5656 dynamic_cast < type-id > ( expression )
5657 static_cast < type-id > ( expression )
5658 reinterpret_cast < type-id > ( expression )
5659 const_cast < type-id > ( expression )
5660 typeid ( expression )
5661 typeid ( type-id )
5663 GNU Extension:
5665 postfix-expression:
5666 ( type-id ) { initializer-list , [opt] }
5668 This extension is a GNU version of the C99 compound-literal
5669 construct. (The C99 grammar uses `type-name' instead of `type-id',
5670 but they are essentially the same concept.)
5672 If ADDRESS_P is true, the postfix expression is the operand of the
5673 `&' operator. CAST_P is true if this expression is the target of a
5674 cast.
5676 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5677 class member access expressions [expr.ref].
5679 Returns a representation of the expression. */
5681 static tree
5682 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5683 bool member_access_only_p, bool decltype_p,
5684 cp_id_kind * pidk_return)
5686 cp_token *token;
5687 location_t loc;
5688 enum rid keyword;
5689 cp_id_kind idk = CP_ID_KIND_NONE;
5690 tree postfix_expression = NULL_TREE;
5691 bool is_member_access = false;
5692 int saved_in_statement = -1;
5694 /* Peek at the next token. */
5695 token = cp_lexer_peek_token (parser->lexer);
5696 loc = token->location;
5697 /* Some of the productions are determined by keywords. */
5698 keyword = token->keyword;
5699 switch (keyword)
5701 case RID_DYNCAST:
5702 case RID_STATCAST:
5703 case RID_REINTCAST:
5704 case RID_CONSTCAST:
5706 tree type;
5707 tree expression;
5708 const char *saved_message;
5709 bool saved_in_type_id_in_expr_p;
5711 /* All of these can be handled in the same way from the point
5712 of view of parsing. Begin by consuming the token
5713 identifying the cast. */
5714 cp_lexer_consume_token (parser->lexer);
5716 /* New types cannot be defined in the cast. */
5717 saved_message = parser->type_definition_forbidden_message;
5718 parser->type_definition_forbidden_message
5719 = G_("types may not be defined in casts");
5721 /* Look for the opening `<'. */
5722 cp_parser_require (parser, CPP_LESS, RT_LESS);
5723 /* Parse the type to which we are casting. */
5724 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5725 parser->in_type_id_in_expr_p = true;
5726 type = cp_parser_type_id (parser);
5727 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5728 /* Look for the closing `>'. */
5729 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5730 /* Restore the old message. */
5731 parser->type_definition_forbidden_message = saved_message;
5733 bool saved_greater_than_is_operator_p
5734 = parser->greater_than_is_operator_p;
5735 parser->greater_than_is_operator_p = true;
5737 /* And the expression which is being cast. */
5738 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5739 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5742 parser->greater_than_is_operator_p
5743 = saved_greater_than_is_operator_p;
5745 /* Only type conversions to integral or enumeration types
5746 can be used in constant-expressions. */
5747 if (!cast_valid_in_integral_constant_expression_p (type)
5748 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5749 return error_mark_node;
5751 switch (keyword)
5753 case RID_DYNCAST:
5754 postfix_expression
5755 = build_dynamic_cast (type, expression, tf_warning_or_error);
5756 break;
5757 case RID_STATCAST:
5758 postfix_expression
5759 = build_static_cast (type, expression, tf_warning_or_error);
5760 break;
5761 case RID_REINTCAST:
5762 postfix_expression
5763 = build_reinterpret_cast (type, expression,
5764 tf_warning_or_error);
5765 break;
5766 case RID_CONSTCAST:
5767 postfix_expression
5768 = build_const_cast (type, expression, tf_warning_or_error);
5769 break;
5770 default:
5771 gcc_unreachable ();
5774 break;
5776 case RID_TYPEID:
5778 tree type;
5779 const char *saved_message;
5780 bool saved_in_type_id_in_expr_p;
5782 /* Consume the `typeid' token. */
5783 cp_lexer_consume_token (parser->lexer);
5784 /* Look for the `(' token. */
5785 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5786 /* Types cannot be defined in a `typeid' expression. */
5787 saved_message = parser->type_definition_forbidden_message;
5788 parser->type_definition_forbidden_message
5789 = G_("types may not be defined in a %<typeid%> expression");
5790 /* We can't be sure yet whether we're looking at a type-id or an
5791 expression. */
5792 cp_parser_parse_tentatively (parser);
5793 /* Try a type-id first. */
5794 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5795 parser->in_type_id_in_expr_p = true;
5796 type = cp_parser_type_id (parser);
5797 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5798 /* Look for the `)' token. Otherwise, we can't be sure that
5799 we're not looking at an expression: consider `typeid (int
5800 (3))', for example. */
5801 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5802 /* If all went well, simply lookup the type-id. */
5803 if (cp_parser_parse_definitely (parser))
5804 postfix_expression = get_typeid (type, tf_warning_or_error);
5805 /* Otherwise, fall back to the expression variant. */
5806 else
5808 tree expression;
5810 /* Look for an expression. */
5811 expression = cp_parser_expression (parser, & idk);
5812 /* Compute its typeid. */
5813 postfix_expression = build_typeid (expression, tf_warning_or_error);
5814 /* Look for the `)' token. */
5815 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5817 /* Restore the saved message. */
5818 parser->type_definition_forbidden_message = saved_message;
5819 /* `typeid' may not appear in an integral constant expression. */
5820 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5821 return error_mark_node;
5823 break;
5825 case RID_TYPENAME:
5827 tree type;
5828 /* The syntax permitted here is the same permitted for an
5829 elaborated-type-specifier. */
5830 type = cp_parser_elaborated_type_specifier (parser,
5831 /*is_friend=*/false,
5832 /*is_declaration=*/false);
5833 postfix_expression = cp_parser_functional_cast (parser, type);
5835 break;
5837 case RID_CILK_SPAWN:
5839 cp_lexer_consume_token (parser->lexer);
5840 token = cp_lexer_peek_token (parser->lexer);
5841 if (token->type == CPP_SEMICOLON)
5843 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5844 "an expression");
5845 postfix_expression = error_mark_node;
5846 break;
5848 else if (!current_function_decl)
5850 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5851 "inside a function");
5852 postfix_expression = error_mark_node;
5853 break;
5855 else
5857 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5858 saved_in_statement = parser->in_statement;
5859 parser->in_statement |= IN_CILK_SPAWN;
5861 cfun->calls_cilk_spawn = 1;
5862 postfix_expression =
5863 cp_parser_postfix_expression (parser, false, false,
5864 false, false, &idk);
5865 if (!flag_cilkplus)
5867 error_at (token->location, "-fcilkplus must be enabled to use"
5868 " %<_Cilk_spawn%>");
5869 cfun->calls_cilk_spawn = 0;
5871 else if (saved_in_statement & IN_CILK_SPAWN)
5873 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5874 "are not permitted");
5875 postfix_expression = error_mark_node;
5876 cfun->calls_cilk_spawn = 0;
5878 else
5880 postfix_expression = build_cilk_spawn (token->location,
5881 postfix_expression);
5882 if (postfix_expression != error_mark_node)
5883 SET_EXPR_LOCATION (postfix_expression, input_location);
5884 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5886 break;
5889 case RID_BUILTIN_SHUFFLE:
5891 vec<tree, va_gc> *vec;
5892 unsigned int i;
5893 tree p;
5895 cp_lexer_consume_token (parser->lexer);
5896 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5897 /*cast_p=*/false, /*allow_expansion_p=*/true,
5898 /*non_constant_p=*/NULL);
5899 if (vec == NULL)
5900 return error_mark_node;
5902 FOR_EACH_VEC_ELT (*vec, i, p)
5903 mark_exp_read (p);
5905 if (vec->length () == 2)
5906 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5907 tf_warning_or_error);
5908 else if (vec->length () == 3)
5909 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5910 tf_warning_or_error);
5911 else
5913 error_at (loc, "wrong number of arguments to "
5914 "%<__builtin_shuffle%>");
5915 return error_mark_node;
5917 break;
5920 default:
5922 tree type;
5924 /* If the next thing is a simple-type-specifier, we may be
5925 looking at a functional cast. We could also be looking at
5926 an id-expression. So, we try the functional cast, and if
5927 that doesn't work we fall back to the primary-expression. */
5928 cp_parser_parse_tentatively (parser);
5929 /* Look for the simple-type-specifier. */
5930 type = cp_parser_simple_type_specifier (parser,
5931 /*decl_specs=*/NULL,
5932 CP_PARSER_FLAGS_NONE);
5933 /* Parse the cast itself. */
5934 if (!cp_parser_error_occurred (parser))
5935 postfix_expression
5936 = cp_parser_functional_cast (parser, type);
5937 /* If that worked, we're done. */
5938 if (cp_parser_parse_definitely (parser))
5939 break;
5941 /* If the functional-cast didn't work out, try a
5942 compound-literal. */
5943 if (cp_parser_allow_gnu_extensions_p (parser)
5944 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5946 tree initializer = NULL_TREE;
5948 cp_parser_parse_tentatively (parser);
5950 /* Avoid calling cp_parser_type_id pointlessly, see comment
5951 in cp_parser_cast_expression about c++/29234. */
5952 if (!cp_parser_compound_literal_p (parser))
5953 cp_parser_simulate_error (parser);
5954 else
5956 /* Parse the type. */
5957 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5958 parser->in_type_id_in_expr_p = true;
5959 type = cp_parser_type_id (parser);
5960 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5961 /* Look for the `)'. */
5962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5965 /* If things aren't going well, there's no need to
5966 keep going. */
5967 if (!cp_parser_error_occurred (parser))
5969 bool non_constant_p;
5970 /* Parse the brace-enclosed initializer list. */
5971 initializer = cp_parser_braced_list (parser,
5972 &non_constant_p);
5974 /* If that worked, we're definitely looking at a
5975 compound-literal expression. */
5976 if (cp_parser_parse_definitely (parser))
5978 /* Warn the user that a compound literal is not
5979 allowed in standard C++. */
5980 pedwarn (input_location, OPT_Wpedantic,
5981 "ISO C++ forbids compound-literals");
5982 /* For simplicity, we disallow compound literals in
5983 constant-expressions. We could
5984 allow compound literals of integer type, whose
5985 initializer was a constant, in constant
5986 expressions. Permitting that usage, as a further
5987 extension, would not change the meaning of any
5988 currently accepted programs. (Of course, as
5989 compound literals are not part of ISO C++, the
5990 standard has nothing to say.) */
5991 if (cp_parser_non_integral_constant_expression (parser,
5992 NIC_NCC))
5994 postfix_expression = error_mark_node;
5995 break;
5997 /* Form the representation of the compound-literal. */
5998 postfix_expression
5999 = finish_compound_literal (type, initializer,
6000 tf_warning_or_error);
6001 break;
6005 /* It must be a primary-expression. */
6006 postfix_expression
6007 = cp_parser_primary_expression (parser, address_p, cast_p,
6008 /*template_arg_p=*/false,
6009 decltype_p,
6010 &idk);
6012 break;
6015 /* Note that we don't need to worry about calling build_cplus_new on a
6016 class-valued CALL_EXPR in decltype when it isn't the end of the
6017 postfix-expression; unary_complex_lvalue will take care of that for
6018 all these cases. */
6020 /* Keep looping until the postfix-expression is complete. */
6021 while (true)
6023 if (idk == CP_ID_KIND_UNQUALIFIED
6024 && identifier_p (postfix_expression)
6025 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6026 /* It is not a Koenig lookup function call. */
6027 postfix_expression
6028 = unqualified_name_lookup_error (postfix_expression);
6030 /* Peek at the next token. */
6031 token = cp_lexer_peek_token (parser->lexer);
6033 switch (token->type)
6035 case CPP_OPEN_SQUARE:
6036 if (cp_next_tokens_can_be_std_attribute_p (parser))
6038 cp_parser_error (parser,
6039 "two consecutive %<[%> shall "
6040 "only introduce an attribute");
6041 return error_mark_node;
6043 postfix_expression
6044 = cp_parser_postfix_open_square_expression (parser,
6045 postfix_expression,
6046 false,
6047 decltype_p);
6048 idk = CP_ID_KIND_NONE;
6049 is_member_access = false;
6050 break;
6052 case CPP_OPEN_PAREN:
6053 /* postfix-expression ( expression-list [opt] ) */
6055 bool koenig_p;
6056 bool is_builtin_constant_p;
6057 bool saved_integral_constant_expression_p = false;
6058 bool saved_non_integral_constant_expression_p = false;
6059 tsubst_flags_t complain = complain_flags (decltype_p);
6060 vec<tree, va_gc> *args;
6062 is_member_access = false;
6064 is_builtin_constant_p
6065 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6066 if (is_builtin_constant_p)
6068 /* The whole point of __builtin_constant_p is to allow
6069 non-constant expressions to appear as arguments. */
6070 saved_integral_constant_expression_p
6071 = parser->integral_constant_expression_p;
6072 saved_non_integral_constant_expression_p
6073 = parser->non_integral_constant_expression_p;
6074 parser->integral_constant_expression_p = false;
6076 args = (cp_parser_parenthesized_expression_list
6077 (parser, non_attr,
6078 /*cast_p=*/false, /*allow_expansion_p=*/true,
6079 /*non_constant_p=*/NULL,
6080 /*want_literal_zero_p=*/warn_memset_transposed_args));
6081 if (is_builtin_constant_p)
6083 parser->integral_constant_expression_p
6084 = saved_integral_constant_expression_p;
6085 parser->non_integral_constant_expression_p
6086 = saved_non_integral_constant_expression_p;
6089 if (args == NULL)
6091 postfix_expression = error_mark_node;
6092 break;
6095 /* Function calls are not permitted in
6096 constant-expressions. */
6097 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6098 && cp_parser_non_integral_constant_expression (parser,
6099 NIC_FUNC_CALL))
6101 postfix_expression = error_mark_node;
6102 release_tree_vector (args);
6103 break;
6106 koenig_p = false;
6107 if (idk == CP_ID_KIND_UNQUALIFIED
6108 || idk == CP_ID_KIND_TEMPLATE_ID)
6110 if (identifier_p (postfix_expression))
6112 if (!args->is_empty ())
6114 koenig_p = true;
6115 if (!any_type_dependent_arguments_p (args))
6116 postfix_expression
6117 = perform_koenig_lookup (postfix_expression, args,
6118 complain);
6120 else
6121 postfix_expression
6122 = unqualified_fn_lookup_error (postfix_expression);
6124 /* We do not perform argument-dependent lookup if
6125 normal lookup finds a non-function, in accordance
6126 with the expected resolution of DR 218. */
6127 else if (!args->is_empty ()
6128 && is_overloaded_fn (postfix_expression))
6130 tree fn = get_first_fn (postfix_expression);
6131 fn = STRIP_TEMPLATE (fn);
6133 /* Do not do argument dependent lookup if regular
6134 lookup finds a member function or a block-scope
6135 function declaration. [basic.lookup.argdep]/3 */
6136 if (!DECL_FUNCTION_MEMBER_P (fn)
6137 && !DECL_LOCAL_FUNCTION_P (fn))
6139 koenig_p = true;
6140 if (!any_type_dependent_arguments_p (args))
6141 postfix_expression
6142 = perform_koenig_lookup (postfix_expression, args,
6143 complain);
6148 if (warn_memset_transposed_args)
6150 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6151 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6152 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6153 && vec_safe_length (args) == 3
6154 && integer_zerop ((*args)[2])
6155 && LITERAL_ZERO_P ((*args)[2])
6156 && !(integer_zerop ((*args)[1])
6157 && LITERAL_ZERO_P ((*args)[1])))
6158 warning (OPT_Wmemset_transposed_args,
6159 "%<memset%> used with constant zero length "
6160 "parameter; this could be due to transposed "
6161 "parameters");
6163 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6164 to avoid leaking those into folder and middle-end. */
6165 unsigned int i;
6166 tree arg;
6167 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6168 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6169 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6172 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6174 tree instance = TREE_OPERAND (postfix_expression, 0);
6175 tree fn = TREE_OPERAND (postfix_expression, 1);
6177 if (processing_template_decl
6178 && (type_dependent_expression_p (instance)
6179 || (!BASELINK_P (fn)
6180 && TREE_CODE (fn) != FIELD_DECL)
6181 || type_dependent_expression_p (fn)
6182 || any_type_dependent_arguments_p (args)))
6184 postfix_expression
6185 = build_nt_call_vec (postfix_expression, args);
6186 release_tree_vector (args);
6187 break;
6190 if (BASELINK_P (fn))
6192 postfix_expression
6193 = (build_new_method_call
6194 (instance, fn, &args, NULL_TREE,
6195 (idk == CP_ID_KIND_QUALIFIED
6196 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6197 : LOOKUP_NORMAL),
6198 /*fn_p=*/NULL,
6199 complain));
6201 else
6202 postfix_expression
6203 = finish_call_expr (postfix_expression, &args,
6204 /*disallow_virtual=*/false,
6205 /*koenig_p=*/false,
6206 complain);
6208 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6209 || TREE_CODE (postfix_expression) == MEMBER_REF
6210 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6211 postfix_expression = (build_offset_ref_call_from_tree
6212 (postfix_expression, &args,
6213 complain));
6214 else if (idk == CP_ID_KIND_QUALIFIED)
6215 /* A call to a static class member, or a namespace-scope
6216 function. */
6217 postfix_expression
6218 = finish_call_expr (postfix_expression, &args,
6219 /*disallow_virtual=*/true,
6220 koenig_p,
6221 complain);
6222 else
6223 /* All other function calls. */
6224 postfix_expression
6225 = finish_call_expr (postfix_expression, &args,
6226 /*disallow_virtual=*/false,
6227 koenig_p,
6228 complain);
6230 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6231 idk = CP_ID_KIND_NONE;
6233 release_tree_vector (args);
6235 break;
6237 case CPP_DOT:
6238 case CPP_DEREF:
6239 /* postfix-expression . template [opt] id-expression
6240 postfix-expression . pseudo-destructor-name
6241 postfix-expression -> template [opt] id-expression
6242 postfix-expression -> pseudo-destructor-name */
6244 /* Consume the `.' or `->' operator. */
6245 cp_lexer_consume_token (parser->lexer);
6247 postfix_expression
6248 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6249 postfix_expression,
6250 false, &idk, loc);
6252 is_member_access = true;
6253 break;
6255 case CPP_PLUS_PLUS:
6256 /* postfix-expression ++ */
6257 /* Consume the `++' token. */
6258 cp_lexer_consume_token (parser->lexer);
6259 /* Generate a representation for the complete expression. */
6260 postfix_expression
6261 = finish_increment_expr (postfix_expression,
6262 POSTINCREMENT_EXPR);
6263 /* Increments may not appear in constant-expressions. */
6264 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6265 postfix_expression = error_mark_node;
6266 idk = CP_ID_KIND_NONE;
6267 is_member_access = false;
6268 break;
6270 case CPP_MINUS_MINUS:
6271 /* postfix-expression -- */
6272 /* Consume the `--' token. */
6273 cp_lexer_consume_token (parser->lexer);
6274 /* Generate a representation for the complete expression. */
6275 postfix_expression
6276 = finish_increment_expr (postfix_expression,
6277 POSTDECREMENT_EXPR);
6278 /* Decrements may not appear in constant-expressions. */
6279 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6280 postfix_expression = error_mark_node;
6281 idk = CP_ID_KIND_NONE;
6282 is_member_access = false;
6283 break;
6285 default:
6286 if (pidk_return != NULL)
6287 * pidk_return = idk;
6288 if (member_access_only_p)
6289 return is_member_access? postfix_expression : error_mark_node;
6290 else
6291 return postfix_expression;
6295 /* We should never get here. */
6296 gcc_unreachable ();
6297 return error_mark_node;
6300 /* This function parses Cilk Plus array notations. If a normal array expr. is
6301 parsed then the array index is passed back to the caller through *INIT_INDEX
6302 and the function returns a NULL_TREE. If array notation expr. is parsed,
6303 then *INIT_INDEX is ignored by the caller and the function returns
6304 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6305 error_mark_node. */
6307 static tree
6308 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6309 tree array_value)
6311 cp_token *token = NULL;
6312 tree length_index, stride = NULL_TREE, value_tree, array_type;
6313 if (!array_value || array_value == error_mark_node)
6315 cp_parser_skip_to_end_of_statement (parser);
6316 return error_mark_node;
6319 array_type = TREE_TYPE (array_value);
6321 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6322 parser->colon_corrects_to_scope_p = false;
6323 token = cp_lexer_peek_token (parser->lexer);
6325 if (!token)
6327 cp_parser_error (parser, "expected %<:%> or numeral");
6328 return error_mark_node;
6330 else if (token->type == CPP_COLON)
6332 /* Consume the ':'. */
6333 cp_lexer_consume_token (parser->lexer);
6335 /* If we are here, then we have a case like this A[:]. */
6336 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6338 cp_parser_error (parser, "expected %<]%>");
6339 cp_parser_skip_to_end_of_statement (parser);
6340 return error_mark_node;
6342 *init_index = NULL_TREE;
6343 stride = NULL_TREE;
6344 length_index = NULL_TREE;
6346 else
6348 /* If we are here, then there are three valid possibilities:
6349 1. ARRAY [ EXP ]
6350 2. ARRAY [ EXP : EXP ]
6351 3. ARRAY [ EXP : EXP : EXP ] */
6353 *init_index = cp_parser_expression (parser);
6354 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6356 /* This indicates that we have a normal array expression. */
6357 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6358 return NULL_TREE;
6361 /* Consume the ':'. */
6362 cp_lexer_consume_token (parser->lexer);
6363 length_index = cp_parser_expression (parser);
6364 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6366 cp_lexer_consume_token (parser->lexer);
6367 stride = cp_parser_expression (parser);
6370 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6372 if (*init_index == error_mark_node || length_index == error_mark_node
6373 || stride == error_mark_node || array_type == error_mark_node)
6375 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6376 cp_lexer_consume_token (parser->lexer);
6377 return error_mark_node;
6379 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6381 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6382 length_index, stride, array_type);
6383 return value_tree;
6386 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6387 by cp_parser_builtin_offsetof. We're looking for
6389 postfix-expression [ expression ]
6390 postfix-expression [ braced-init-list ] (C++11)
6392 FOR_OFFSETOF is set if we're being called in that context, which
6393 changes how we deal with integer constant expressions. */
6395 static tree
6396 cp_parser_postfix_open_square_expression (cp_parser *parser,
6397 tree postfix_expression,
6398 bool for_offsetof,
6399 bool decltype_p)
6401 tree index = NULL_TREE;
6402 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6403 bool saved_greater_than_is_operator_p;
6405 /* Consume the `[' token. */
6406 cp_lexer_consume_token (parser->lexer);
6408 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6409 parser->greater_than_is_operator_p = true;
6411 /* Parse the index expression. */
6412 /* ??? For offsetof, there is a question of what to allow here. If
6413 offsetof is not being used in an integral constant expression context,
6414 then we *could* get the right answer by computing the value at runtime.
6415 If we are in an integral constant expression context, then we might
6416 could accept any constant expression; hard to say without analysis.
6417 Rather than open the barn door too wide right away, allow only integer
6418 constant expressions here. */
6419 if (for_offsetof)
6420 index = cp_parser_constant_expression (parser, false, NULL);
6421 else
6423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6425 bool expr_nonconst_p;
6426 cp_lexer_set_source_position (parser->lexer);
6427 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6428 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6429 if (flag_cilkplus
6430 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6432 error_at (cp_lexer_peek_token (parser->lexer)->location,
6433 "braced list index is not allowed with array "
6434 "notation");
6435 cp_parser_skip_to_end_of_statement (parser);
6436 return error_mark_node;
6439 else if (flag_cilkplus)
6441 /* Here are have these two options:
6442 ARRAY[EXP : EXP] - Array notation expr with default
6443 stride of 1.
6444 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6445 stride. */
6446 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6447 postfix_expression);
6448 if (an_exp)
6449 return an_exp;
6451 else
6452 index = cp_parser_expression (parser);
6455 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6457 /* Look for the closing `]'. */
6458 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6460 /* Build the ARRAY_REF. */
6461 postfix_expression = grok_array_decl (loc, postfix_expression,
6462 index, decltype_p);
6464 /* When not doing offsetof, array references are not permitted in
6465 constant-expressions. */
6466 if (!for_offsetof
6467 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6468 postfix_expression = error_mark_node;
6470 return postfix_expression;
6473 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6474 by cp_parser_builtin_offsetof. We're looking for
6476 postfix-expression . template [opt] id-expression
6477 postfix-expression . pseudo-destructor-name
6478 postfix-expression -> template [opt] id-expression
6479 postfix-expression -> pseudo-destructor-name
6481 FOR_OFFSETOF is set if we're being called in that context. That sorta
6482 limits what of the above we'll actually accept, but nevermind.
6483 TOKEN_TYPE is the "." or "->" token, which will already have been
6484 removed from the stream. */
6486 static tree
6487 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6488 enum cpp_ttype token_type,
6489 tree postfix_expression,
6490 bool for_offsetof, cp_id_kind *idk,
6491 location_t location)
6493 tree name;
6494 bool dependent_p;
6495 bool pseudo_destructor_p;
6496 tree scope = NULL_TREE;
6498 /* If this is a `->' operator, dereference the pointer. */
6499 if (token_type == CPP_DEREF)
6500 postfix_expression = build_x_arrow (location, postfix_expression,
6501 tf_warning_or_error);
6502 /* Check to see whether or not the expression is type-dependent. */
6503 dependent_p = type_dependent_expression_p (postfix_expression);
6504 /* The identifier following the `->' or `.' is not qualified. */
6505 parser->scope = NULL_TREE;
6506 parser->qualifying_scope = NULL_TREE;
6507 parser->object_scope = NULL_TREE;
6508 *idk = CP_ID_KIND_NONE;
6510 /* Enter the scope corresponding to the type of the object
6511 given by the POSTFIX_EXPRESSION. */
6512 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6514 scope = TREE_TYPE (postfix_expression);
6515 /* According to the standard, no expression should ever have
6516 reference type. Unfortunately, we do not currently match
6517 the standard in this respect in that our internal representation
6518 of an expression may have reference type even when the standard
6519 says it does not. Therefore, we have to manually obtain the
6520 underlying type here. */
6521 scope = non_reference (scope);
6522 /* The type of the POSTFIX_EXPRESSION must be complete. */
6523 if (scope == unknown_type_node)
6525 error_at (location, "%qE does not have class type",
6526 postfix_expression);
6527 scope = NULL_TREE;
6529 /* Unlike the object expression in other contexts, *this is not
6530 required to be of complete type for purposes of class member
6531 access (5.2.5) outside the member function body. */
6532 else if (postfix_expression != current_class_ref
6533 && !(processing_template_decl && scope == current_class_type))
6534 scope = complete_type_or_else (scope, NULL_TREE);
6535 /* Let the name lookup machinery know that we are processing a
6536 class member access expression. */
6537 parser->context->object_type = scope;
6538 /* If something went wrong, we want to be able to discern that case,
6539 as opposed to the case where there was no SCOPE due to the type
6540 of expression being dependent. */
6541 if (!scope)
6542 scope = error_mark_node;
6543 /* If the SCOPE was erroneous, make the various semantic analysis
6544 functions exit quickly -- and without issuing additional error
6545 messages. */
6546 if (scope == error_mark_node)
6547 postfix_expression = error_mark_node;
6550 /* Assume this expression is not a pseudo-destructor access. */
6551 pseudo_destructor_p = false;
6553 /* If the SCOPE is a scalar type, then, if this is a valid program,
6554 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6555 is type dependent, it can be pseudo-destructor-name or something else.
6556 Try to parse it as pseudo-destructor-name first. */
6557 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6559 tree s;
6560 tree type;
6562 cp_parser_parse_tentatively (parser);
6563 /* Parse the pseudo-destructor-name. */
6564 s = NULL_TREE;
6565 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6566 &s, &type);
6567 if (dependent_p
6568 && (cp_parser_error_occurred (parser)
6569 || !SCALAR_TYPE_P (type)))
6570 cp_parser_abort_tentative_parse (parser);
6571 else if (cp_parser_parse_definitely (parser))
6573 pseudo_destructor_p = true;
6574 postfix_expression
6575 = finish_pseudo_destructor_expr (postfix_expression,
6576 s, type, location);
6580 if (!pseudo_destructor_p)
6582 /* If the SCOPE is not a scalar type, we are looking at an
6583 ordinary class member access expression, rather than a
6584 pseudo-destructor-name. */
6585 bool template_p;
6586 cp_token *token = cp_lexer_peek_token (parser->lexer);
6587 /* Parse the id-expression. */
6588 name = (cp_parser_id_expression
6589 (parser,
6590 cp_parser_optional_template_keyword (parser),
6591 /*check_dependency_p=*/true,
6592 &template_p,
6593 /*declarator_p=*/false,
6594 /*optional_p=*/false));
6595 /* In general, build a SCOPE_REF if the member name is qualified.
6596 However, if the name was not dependent and has already been
6597 resolved; there is no need to build the SCOPE_REF. For example;
6599 struct X { void f(); };
6600 template <typename T> void f(T* t) { t->X::f(); }
6602 Even though "t" is dependent, "X::f" is not and has been resolved
6603 to a BASELINK; there is no need to include scope information. */
6605 /* But we do need to remember that there was an explicit scope for
6606 virtual function calls. */
6607 if (parser->scope)
6608 *idk = CP_ID_KIND_QUALIFIED;
6610 /* If the name is a template-id that names a type, we will get a
6611 TYPE_DECL here. That is invalid code. */
6612 if (TREE_CODE (name) == TYPE_DECL)
6614 error_at (token->location, "invalid use of %qD", name);
6615 postfix_expression = error_mark_node;
6617 else
6619 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6621 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6623 error_at (token->location, "%<%D::%D%> is not a class member",
6624 parser->scope, name);
6625 postfix_expression = error_mark_node;
6627 else
6628 name = build_qualified_name (/*type=*/NULL_TREE,
6629 parser->scope,
6630 name,
6631 template_p);
6632 parser->scope = NULL_TREE;
6633 parser->qualifying_scope = NULL_TREE;
6634 parser->object_scope = NULL_TREE;
6636 if (parser->scope && name && BASELINK_P (name))
6637 adjust_result_of_qualified_name_lookup
6638 (name, parser->scope, scope);
6639 postfix_expression
6640 = finish_class_member_access_expr (postfix_expression, name,
6641 template_p,
6642 tf_warning_or_error);
6646 /* We no longer need to look up names in the scope of the object on
6647 the left-hand side of the `.' or `->' operator. */
6648 parser->context->object_type = NULL_TREE;
6650 /* Outside of offsetof, these operators may not appear in
6651 constant-expressions. */
6652 if (!for_offsetof
6653 && (cp_parser_non_integral_constant_expression
6654 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6655 postfix_expression = error_mark_node;
6657 return postfix_expression;
6660 /* Cache of LITERAL_ZERO_P constants. */
6662 static GTY(()) tree literal_zeros[itk_none];
6664 /* Parse a parenthesized expression-list.
6666 expression-list:
6667 assignment-expression
6668 expression-list, assignment-expression
6670 attribute-list:
6671 expression-list
6672 identifier
6673 identifier, expression-list
6675 CAST_P is true if this expression is the target of a cast.
6677 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6678 argument pack.
6680 Returns a vector of trees. Each element is a representation of an
6681 assignment-expression. NULL is returned if the ( and or ) are
6682 missing. An empty, but allocated, vector is returned on no
6683 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6684 if we are parsing an attribute list for an attribute that wants a
6685 plain identifier argument, normal_attr for an attribute that wants
6686 an expression, or non_attr if we aren't parsing an attribute list. If
6687 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6688 not all of the expressions in the list were constant.
6689 WANT_LITERAL_ZERO_P is true if the caller is interested in
6690 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6691 immediately, this can be removed. */
6693 static vec<tree, va_gc> *
6694 cp_parser_parenthesized_expression_list (cp_parser* parser,
6695 int is_attribute_list,
6696 bool cast_p,
6697 bool allow_expansion_p,
6698 bool *non_constant_p,
6699 bool want_literal_zero_p)
6701 vec<tree, va_gc> *expression_list;
6702 bool fold_expr_p = is_attribute_list != non_attr;
6703 tree identifier = NULL_TREE;
6704 bool saved_greater_than_is_operator_p;
6706 /* Assume all the expressions will be constant. */
6707 if (non_constant_p)
6708 *non_constant_p = false;
6710 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6711 return NULL;
6713 expression_list = make_tree_vector ();
6715 /* Within a parenthesized expression, a `>' token is always
6716 the greater-than operator. */
6717 saved_greater_than_is_operator_p
6718 = parser->greater_than_is_operator_p;
6719 parser->greater_than_is_operator_p = true;
6721 /* Consume expressions until there are no more. */
6722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6723 while (true)
6725 tree expr;
6727 /* At the beginning of attribute lists, check to see if the
6728 next token is an identifier. */
6729 if (is_attribute_list == id_attr
6730 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6732 cp_token *token;
6734 /* Consume the identifier. */
6735 token = cp_lexer_consume_token (parser->lexer);
6736 /* Save the identifier. */
6737 identifier = token->u.value;
6739 else
6741 bool expr_non_constant_p;
6743 /* Parse the next assignment-expression. */
6744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6746 /* A braced-init-list. */
6747 cp_lexer_set_source_position (parser->lexer);
6748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6749 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6750 if (non_constant_p && expr_non_constant_p)
6751 *non_constant_p = true;
6753 else if (non_constant_p)
6755 expr = (cp_parser_constant_expression
6756 (parser, /*allow_non_constant_p=*/true,
6757 &expr_non_constant_p));
6758 if (expr_non_constant_p)
6759 *non_constant_p = true;
6761 else
6763 expr = NULL_TREE;
6764 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6765 switch (tok->type)
6767 case CPP_NUMBER:
6768 case CPP_CHAR:
6769 case CPP_WCHAR:
6770 case CPP_CHAR16:
6771 case CPP_CHAR32:
6772 /* If a parameter is literal zero alone, remember it
6773 for -Wmemset-transposed-args warning. */
6774 if (integer_zerop (tok->u.value)
6775 && !TREE_OVERFLOW (tok->u.value)
6776 && want_literal_zero_p
6777 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6778 == CPP_COMMA
6779 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6780 == CPP_CLOSE_PAREN))
6782 unsigned int i;
6783 for (i = 0; i < itk_none; ++i)
6784 if (TREE_TYPE (tok->u.value) == integer_types[i])
6785 break;
6786 if (i < itk_none && literal_zeros[i])
6787 expr = literal_zeros[i];
6788 else
6790 expr = copy_node (tok->u.value);
6791 LITERAL_ZERO_P (expr) = 1;
6792 if (i < itk_none)
6793 literal_zeros[i] = expr;
6795 /* Consume the 0 token (or '\0', 0LL etc.). */
6796 cp_lexer_consume_token (parser->lexer);
6798 break;
6799 default:
6800 break;
6802 if (expr == NULL_TREE)
6803 expr = cp_parser_assignment_expression (parser, cast_p,
6804 NULL);
6807 if (fold_expr_p)
6808 expr = fold_non_dependent_expr (expr);
6810 /* If we have an ellipsis, then this is an expression
6811 expansion. */
6812 if (allow_expansion_p
6813 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6815 /* Consume the `...'. */
6816 cp_lexer_consume_token (parser->lexer);
6818 /* Build the argument pack. */
6819 expr = make_pack_expansion (expr);
6822 /* Add it to the list. We add error_mark_node
6823 expressions to the list, so that we can still tell if
6824 the correct form for a parenthesized expression-list
6825 is found. That gives better errors. */
6826 vec_safe_push (expression_list, expr);
6828 if (expr == error_mark_node)
6829 goto skip_comma;
6832 /* After the first item, attribute lists look the same as
6833 expression lists. */
6834 is_attribute_list = non_attr;
6836 get_comma:;
6837 /* If the next token isn't a `,', then we are done. */
6838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6839 break;
6841 /* Otherwise, consume the `,' and keep going. */
6842 cp_lexer_consume_token (parser->lexer);
6845 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6847 int ending;
6849 skip_comma:;
6850 /* We try and resync to an unnested comma, as that will give the
6851 user better diagnostics. */
6852 ending = cp_parser_skip_to_closing_parenthesis (parser,
6853 /*recovering=*/true,
6854 /*or_comma=*/true,
6855 /*consume_paren=*/true);
6856 if (ending < 0)
6857 goto get_comma;
6858 if (!ending)
6860 parser->greater_than_is_operator_p
6861 = saved_greater_than_is_operator_p;
6862 return NULL;
6866 parser->greater_than_is_operator_p
6867 = saved_greater_than_is_operator_p;
6869 if (identifier)
6870 vec_safe_insert (expression_list, 0, identifier);
6872 return expression_list;
6875 /* Parse a pseudo-destructor-name.
6877 pseudo-destructor-name:
6878 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6879 :: [opt] nested-name-specifier template template-id :: ~ type-name
6880 :: [opt] nested-name-specifier [opt] ~ type-name
6882 If either of the first two productions is used, sets *SCOPE to the
6883 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6884 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6885 or ERROR_MARK_NODE if the parse fails. */
6887 static void
6888 cp_parser_pseudo_destructor_name (cp_parser* parser,
6889 tree object,
6890 tree* scope,
6891 tree* type)
6893 bool nested_name_specifier_p;
6895 /* Handle ~auto. */
6896 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6897 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6898 && !type_dependent_expression_p (object))
6900 if (cxx_dialect < cxx14)
6901 pedwarn (input_location, 0,
6902 "%<~auto%> only available with "
6903 "-std=c++14 or -std=gnu++14");
6904 cp_lexer_consume_token (parser->lexer);
6905 cp_lexer_consume_token (parser->lexer);
6906 *scope = NULL_TREE;
6907 *type = TREE_TYPE (object);
6908 return;
6911 /* Assume that things will not work out. */
6912 *type = error_mark_node;
6914 /* Look for the optional `::' operator. */
6915 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6916 /* Look for the optional nested-name-specifier. */
6917 nested_name_specifier_p
6918 = (cp_parser_nested_name_specifier_opt (parser,
6919 /*typename_keyword_p=*/false,
6920 /*check_dependency_p=*/true,
6921 /*type_p=*/false,
6922 /*is_declaration=*/false)
6923 != NULL_TREE);
6924 /* Now, if we saw a nested-name-specifier, we might be doing the
6925 second production. */
6926 if (nested_name_specifier_p
6927 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6929 /* Consume the `template' keyword. */
6930 cp_lexer_consume_token (parser->lexer);
6931 /* Parse the template-id. */
6932 cp_parser_template_id (parser,
6933 /*template_keyword_p=*/true,
6934 /*check_dependency_p=*/false,
6935 class_type,
6936 /*is_declaration=*/true);
6937 /* Look for the `::' token. */
6938 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6940 /* If the next token is not a `~', then there might be some
6941 additional qualification. */
6942 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6944 /* At this point, we're looking for "type-name :: ~". The type-name
6945 must not be a class-name, since this is a pseudo-destructor. So,
6946 it must be either an enum-name, or a typedef-name -- both of which
6947 are just identifiers. So, we peek ahead to check that the "::"
6948 and "~" tokens are present; if they are not, then we can avoid
6949 calling type_name. */
6950 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6951 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6952 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6954 cp_parser_error (parser, "non-scalar type");
6955 return;
6958 /* Look for the type-name. */
6959 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6960 if (*scope == error_mark_node)
6961 return;
6963 /* Look for the `::' token. */
6964 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6966 else
6967 *scope = NULL_TREE;
6969 /* Look for the `~'. */
6970 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6972 /* Once we see the ~, this has to be a pseudo-destructor. */
6973 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6974 cp_parser_commit_to_topmost_tentative_parse (parser);
6976 /* Look for the type-name again. We are not responsible for
6977 checking that it matches the first type-name. */
6978 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6981 /* Parse a unary-expression.
6983 unary-expression:
6984 postfix-expression
6985 ++ cast-expression
6986 -- cast-expression
6987 unary-operator cast-expression
6988 sizeof unary-expression
6989 sizeof ( type-id )
6990 alignof ( type-id ) [C++0x]
6991 new-expression
6992 delete-expression
6994 GNU Extensions:
6996 unary-expression:
6997 __extension__ cast-expression
6998 __alignof__ unary-expression
6999 __alignof__ ( type-id )
7000 alignof unary-expression [C++0x]
7001 __real__ cast-expression
7002 __imag__ cast-expression
7003 && identifier
7004 sizeof ( type-id ) { initializer-list , [opt] }
7005 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7006 __alignof__ ( type-id ) { initializer-list , [opt] }
7008 ADDRESS_P is true iff the unary-expression is appearing as the
7009 operand of the `&' operator. CAST_P is true if this expression is
7010 the target of a cast.
7012 Returns a representation of the expression. */
7014 static tree
7015 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7016 bool decltype_p, cp_id_kind * pidk)
7018 cp_token *token;
7019 enum tree_code unary_operator;
7021 /* Peek at the next token. */
7022 token = cp_lexer_peek_token (parser->lexer);
7023 /* Some keywords give away the kind of expression. */
7024 if (token->type == CPP_KEYWORD)
7026 enum rid keyword = token->keyword;
7028 switch (keyword)
7030 case RID_ALIGNOF:
7031 case RID_SIZEOF:
7033 tree operand, ret;
7034 enum tree_code op;
7035 location_t first_loc;
7037 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7038 /* Consume the token. */
7039 cp_lexer_consume_token (parser->lexer);
7040 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7041 /* Parse the operand. */
7042 operand = cp_parser_sizeof_operand (parser, keyword);
7044 if (TYPE_P (operand))
7045 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7046 else
7048 /* ISO C++ defines alignof only with types, not with
7049 expressions. So pedwarn if alignof is used with a non-
7050 type expression. However, __alignof__ is ok. */
7051 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7052 pedwarn (token->location, OPT_Wpedantic,
7053 "ISO C++ does not allow %<alignof%> "
7054 "with a non-type");
7056 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7058 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7059 SIZEOF_EXPR with the original operand. */
7060 if (op == SIZEOF_EXPR && ret != error_mark_node)
7062 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7064 if (!processing_template_decl && TYPE_P (operand))
7066 ret = build_min (SIZEOF_EXPR, size_type_node,
7067 build1 (NOP_EXPR, operand,
7068 error_mark_node));
7069 SIZEOF_EXPR_TYPE_P (ret) = 1;
7071 else
7072 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7073 TREE_SIDE_EFFECTS (ret) = 0;
7074 TREE_READONLY (ret) = 1;
7076 SET_EXPR_LOCATION (ret, first_loc);
7078 return ret;
7081 case RID_NEW:
7082 return cp_parser_new_expression (parser);
7084 case RID_DELETE:
7085 return cp_parser_delete_expression (parser);
7087 case RID_EXTENSION:
7089 /* The saved value of the PEDANTIC flag. */
7090 int saved_pedantic;
7091 tree expr;
7093 /* Save away the PEDANTIC flag. */
7094 cp_parser_extension_opt (parser, &saved_pedantic);
7095 /* Parse the cast-expression. */
7096 expr = cp_parser_simple_cast_expression (parser);
7097 /* Restore the PEDANTIC flag. */
7098 pedantic = saved_pedantic;
7100 return expr;
7103 case RID_REALPART:
7104 case RID_IMAGPART:
7106 tree expression;
7108 /* Consume the `__real__' or `__imag__' token. */
7109 cp_lexer_consume_token (parser->lexer);
7110 /* Parse the cast-expression. */
7111 expression = cp_parser_simple_cast_expression (parser);
7112 /* Create the complete representation. */
7113 return build_x_unary_op (token->location,
7114 (keyword == RID_REALPART
7115 ? REALPART_EXPR : IMAGPART_EXPR),
7116 expression,
7117 tf_warning_or_error);
7119 break;
7121 case RID_TRANSACTION_ATOMIC:
7122 case RID_TRANSACTION_RELAXED:
7123 return cp_parser_transaction_expression (parser, keyword);
7125 case RID_NOEXCEPT:
7127 tree expr;
7128 const char *saved_message;
7129 bool saved_integral_constant_expression_p;
7130 bool saved_non_integral_constant_expression_p;
7131 bool saved_greater_than_is_operator_p;
7133 cp_lexer_consume_token (parser->lexer);
7134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7136 saved_message = parser->type_definition_forbidden_message;
7137 parser->type_definition_forbidden_message
7138 = G_("types may not be defined in %<noexcept%> expressions");
7140 saved_integral_constant_expression_p
7141 = parser->integral_constant_expression_p;
7142 saved_non_integral_constant_expression_p
7143 = parser->non_integral_constant_expression_p;
7144 parser->integral_constant_expression_p = false;
7146 saved_greater_than_is_operator_p
7147 = parser->greater_than_is_operator_p;
7148 parser->greater_than_is_operator_p = true;
7150 ++cp_unevaluated_operand;
7151 ++c_inhibit_evaluation_warnings;
7152 expr = cp_parser_expression (parser);
7153 --c_inhibit_evaluation_warnings;
7154 --cp_unevaluated_operand;
7156 parser->greater_than_is_operator_p
7157 = saved_greater_than_is_operator_p;
7159 parser->integral_constant_expression_p
7160 = saved_integral_constant_expression_p;
7161 parser->non_integral_constant_expression_p
7162 = saved_non_integral_constant_expression_p;
7164 parser->type_definition_forbidden_message = saved_message;
7166 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7167 return finish_noexcept_expr (expr, tf_warning_or_error);
7170 default:
7171 break;
7175 /* Look for the `:: new' and `:: delete', which also signal the
7176 beginning of a new-expression, or delete-expression,
7177 respectively. If the next token is `::', then it might be one of
7178 these. */
7179 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7181 enum rid keyword;
7183 /* See if the token after the `::' is one of the keywords in
7184 which we're interested. */
7185 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7186 /* If it's `new', we have a new-expression. */
7187 if (keyword == RID_NEW)
7188 return cp_parser_new_expression (parser);
7189 /* Similarly, for `delete'. */
7190 else if (keyword == RID_DELETE)
7191 return cp_parser_delete_expression (parser);
7194 /* Look for a unary operator. */
7195 unary_operator = cp_parser_unary_operator (token);
7196 /* The `++' and `--' operators can be handled similarly, even though
7197 they are not technically unary-operators in the grammar. */
7198 if (unary_operator == ERROR_MARK)
7200 if (token->type == CPP_PLUS_PLUS)
7201 unary_operator = PREINCREMENT_EXPR;
7202 else if (token->type == CPP_MINUS_MINUS)
7203 unary_operator = PREDECREMENT_EXPR;
7204 /* Handle the GNU address-of-label extension. */
7205 else if (cp_parser_allow_gnu_extensions_p (parser)
7206 && token->type == CPP_AND_AND)
7208 tree identifier;
7209 tree expression;
7210 location_t loc = token->location;
7212 /* Consume the '&&' token. */
7213 cp_lexer_consume_token (parser->lexer);
7214 /* Look for the identifier. */
7215 identifier = cp_parser_identifier (parser);
7216 /* Create an expression representing the address. */
7217 expression = finish_label_address_expr (identifier, loc);
7218 if (cp_parser_non_integral_constant_expression (parser,
7219 NIC_ADDR_LABEL))
7220 expression = error_mark_node;
7221 return expression;
7224 if (unary_operator != ERROR_MARK)
7226 tree cast_expression;
7227 tree expression = error_mark_node;
7228 non_integral_constant non_constant_p = NIC_NONE;
7229 location_t loc = token->location;
7230 tsubst_flags_t complain = complain_flags (decltype_p);
7232 /* Consume the operator token. */
7233 token = cp_lexer_consume_token (parser->lexer);
7234 /* Parse the cast-expression. */
7235 cast_expression
7236 = cp_parser_cast_expression (parser,
7237 unary_operator == ADDR_EXPR,
7238 /*cast_p=*/false,
7239 /*decltype*/false,
7240 pidk);
7241 /* Now, build an appropriate representation. */
7242 switch (unary_operator)
7244 case INDIRECT_REF:
7245 non_constant_p = NIC_STAR;
7246 expression = build_x_indirect_ref (loc, cast_expression,
7247 RO_UNARY_STAR,
7248 complain);
7249 break;
7251 case ADDR_EXPR:
7252 non_constant_p = NIC_ADDR;
7253 /* Fall through. */
7254 case BIT_NOT_EXPR:
7255 expression = build_x_unary_op (loc, unary_operator,
7256 cast_expression,
7257 complain);
7258 break;
7260 case PREINCREMENT_EXPR:
7261 case PREDECREMENT_EXPR:
7262 non_constant_p = unary_operator == PREINCREMENT_EXPR
7263 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7264 /* Fall through. */
7265 case UNARY_PLUS_EXPR:
7266 case NEGATE_EXPR:
7267 case TRUTH_NOT_EXPR:
7268 expression = finish_unary_op_expr (loc, unary_operator,
7269 cast_expression, complain);
7270 break;
7272 default:
7273 gcc_unreachable ();
7276 if (non_constant_p != NIC_NONE
7277 && cp_parser_non_integral_constant_expression (parser,
7278 non_constant_p))
7279 expression = error_mark_node;
7281 return expression;
7284 return cp_parser_postfix_expression (parser, address_p, cast_p,
7285 /*member_access_only_p=*/false,
7286 decltype_p,
7287 pidk);
7290 static inline tree
7291 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7292 cp_id_kind * pidk)
7294 return cp_parser_unary_expression (parser, address_p, cast_p,
7295 /*decltype*/false, pidk);
7298 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7299 unary-operator, the corresponding tree code is returned. */
7301 static enum tree_code
7302 cp_parser_unary_operator (cp_token* token)
7304 switch (token->type)
7306 case CPP_MULT:
7307 return INDIRECT_REF;
7309 case CPP_AND:
7310 return ADDR_EXPR;
7312 case CPP_PLUS:
7313 return UNARY_PLUS_EXPR;
7315 case CPP_MINUS:
7316 return NEGATE_EXPR;
7318 case CPP_NOT:
7319 return TRUTH_NOT_EXPR;
7321 case CPP_COMPL:
7322 return BIT_NOT_EXPR;
7324 default:
7325 return ERROR_MARK;
7329 /* Parse a new-expression.
7331 new-expression:
7332 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7333 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7335 Returns a representation of the expression. */
7337 static tree
7338 cp_parser_new_expression (cp_parser* parser)
7340 bool global_scope_p;
7341 vec<tree, va_gc> *placement;
7342 tree type;
7343 vec<tree, va_gc> *initializer;
7344 tree nelts = NULL_TREE;
7345 tree ret;
7347 /* Look for the optional `::' operator. */
7348 global_scope_p
7349 = (cp_parser_global_scope_opt (parser,
7350 /*current_scope_valid_p=*/false)
7351 != NULL_TREE);
7352 /* Look for the `new' operator. */
7353 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7354 /* There's no easy way to tell a new-placement from the
7355 `( type-id )' construct. */
7356 cp_parser_parse_tentatively (parser);
7357 /* Look for a new-placement. */
7358 placement = cp_parser_new_placement (parser);
7359 /* If that didn't work out, there's no new-placement. */
7360 if (!cp_parser_parse_definitely (parser))
7362 if (placement != NULL)
7363 release_tree_vector (placement);
7364 placement = NULL;
7367 /* If the next token is a `(', then we have a parenthesized
7368 type-id. */
7369 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7371 cp_token *token;
7372 const char *saved_message = parser->type_definition_forbidden_message;
7374 /* Consume the `('. */
7375 cp_lexer_consume_token (parser->lexer);
7377 /* Parse the type-id. */
7378 parser->type_definition_forbidden_message
7379 = G_("types may not be defined in a new-expression");
7380 type = cp_parser_type_id (parser);
7381 parser->type_definition_forbidden_message = saved_message;
7383 /* Look for the closing `)'. */
7384 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7385 token = cp_lexer_peek_token (parser->lexer);
7386 /* There should not be a direct-new-declarator in this production,
7387 but GCC used to allowed this, so we check and emit a sensible error
7388 message for this case. */
7389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7391 error_at (token->location,
7392 "array bound forbidden after parenthesized type-id");
7393 inform (token->location,
7394 "try removing the parentheses around the type-id");
7395 cp_parser_direct_new_declarator (parser);
7398 /* Otherwise, there must be a new-type-id. */
7399 else
7400 type = cp_parser_new_type_id (parser, &nelts);
7402 /* If the next token is a `(' or '{', then we have a new-initializer. */
7403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7404 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7405 initializer = cp_parser_new_initializer (parser);
7406 else
7407 initializer = NULL;
7409 /* A new-expression may not appear in an integral constant
7410 expression. */
7411 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7412 ret = error_mark_node;
7413 else
7415 /* Create a representation of the new-expression. */
7416 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7417 tf_warning_or_error);
7420 if (placement != NULL)
7421 release_tree_vector (placement);
7422 if (initializer != NULL)
7423 release_tree_vector (initializer);
7425 return ret;
7428 /* Parse a new-placement.
7430 new-placement:
7431 ( expression-list )
7433 Returns the same representation as for an expression-list. */
7435 static vec<tree, va_gc> *
7436 cp_parser_new_placement (cp_parser* parser)
7438 vec<tree, va_gc> *expression_list;
7440 /* Parse the expression-list. */
7441 expression_list = (cp_parser_parenthesized_expression_list
7442 (parser, non_attr, /*cast_p=*/false,
7443 /*allow_expansion_p=*/true,
7444 /*non_constant_p=*/NULL));
7446 return expression_list;
7449 /* Parse a new-type-id.
7451 new-type-id:
7452 type-specifier-seq new-declarator [opt]
7454 Returns the TYPE allocated. If the new-type-id indicates an array
7455 type, *NELTS is set to the number of elements in the last array
7456 bound; the TYPE will not include the last array bound. */
7458 static tree
7459 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7461 cp_decl_specifier_seq type_specifier_seq;
7462 cp_declarator *new_declarator;
7463 cp_declarator *declarator;
7464 cp_declarator *outer_declarator;
7465 const char *saved_message;
7467 /* The type-specifier sequence must not contain type definitions.
7468 (It cannot contain declarations of new types either, but if they
7469 are not definitions we will catch that because they are not
7470 complete.) */
7471 saved_message = parser->type_definition_forbidden_message;
7472 parser->type_definition_forbidden_message
7473 = G_("types may not be defined in a new-type-id");
7474 /* Parse the type-specifier-seq. */
7475 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7476 /*is_trailing_return=*/false,
7477 &type_specifier_seq);
7478 /* Restore the old message. */
7479 parser->type_definition_forbidden_message = saved_message;
7481 if (type_specifier_seq.type == error_mark_node)
7482 return error_mark_node;
7484 /* Parse the new-declarator. */
7485 new_declarator = cp_parser_new_declarator_opt (parser);
7487 /* Determine the number of elements in the last array dimension, if
7488 any. */
7489 *nelts = NULL_TREE;
7490 /* Skip down to the last array dimension. */
7491 declarator = new_declarator;
7492 outer_declarator = NULL;
7493 while (declarator && (declarator->kind == cdk_pointer
7494 || declarator->kind == cdk_ptrmem))
7496 outer_declarator = declarator;
7497 declarator = declarator->declarator;
7499 while (declarator
7500 && declarator->kind == cdk_array
7501 && declarator->declarator
7502 && declarator->declarator->kind == cdk_array)
7504 outer_declarator = declarator;
7505 declarator = declarator->declarator;
7508 if (declarator && declarator->kind == cdk_array)
7510 *nelts = declarator->u.array.bounds;
7511 if (*nelts == error_mark_node)
7512 *nelts = integer_one_node;
7514 if (outer_declarator)
7515 outer_declarator->declarator = declarator->declarator;
7516 else
7517 new_declarator = NULL;
7520 return groktypename (&type_specifier_seq, new_declarator, false);
7523 /* Parse an (optional) new-declarator.
7525 new-declarator:
7526 ptr-operator new-declarator [opt]
7527 direct-new-declarator
7529 Returns the declarator. */
7531 static cp_declarator *
7532 cp_parser_new_declarator_opt (cp_parser* parser)
7534 enum tree_code code;
7535 tree type, std_attributes = NULL_TREE;
7536 cp_cv_quals cv_quals;
7538 /* We don't know if there's a ptr-operator next, or not. */
7539 cp_parser_parse_tentatively (parser);
7540 /* Look for a ptr-operator. */
7541 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7542 /* If that worked, look for more new-declarators. */
7543 if (cp_parser_parse_definitely (parser))
7545 cp_declarator *declarator;
7547 /* Parse another optional declarator. */
7548 declarator = cp_parser_new_declarator_opt (parser);
7550 declarator = cp_parser_make_indirect_declarator
7551 (code, type, cv_quals, declarator, std_attributes);
7553 return declarator;
7556 /* If the next token is a `[', there is a direct-new-declarator. */
7557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7558 return cp_parser_direct_new_declarator (parser);
7560 return NULL;
7563 /* Parse a direct-new-declarator.
7565 direct-new-declarator:
7566 [ expression ]
7567 direct-new-declarator [constant-expression]
7571 static cp_declarator *
7572 cp_parser_direct_new_declarator (cp_parser* parser)
7574 cp_declarator *declarator = NULL;
7576 while (true)
7578 tree expression;
7579 cp_token *token;
7581 /* Look for the opening `['. */
7582 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7584 token = cp_lexer_peek_token (parser->lexer);
7585 expression = cp_parser_expression (parser);
7586 /* The standard requires that the expression have integral
7587 type. DR 74 adds enumeration types. We believe that the
7588 real intent is that these expressions be handled like the
7589 expression in a `switch' condition, which also allows
7590 classes with a single conversion to integral or
7591 enumeration type. */
7592 if (!processing_template_decl)
7594 expression
7595 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7596 expression,
7597 /*complain=*/true);
7598 if (!expression)
7600 error_at (token->location,
7601 "expression in new-declarator must have integral "
7602 "or enumeration type");
7603 expression = error_mark_node;
7607 /* Look for the closing `]'. */
7608 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7610 /* Add this bound to the declarator. */
7611 declarator = make_array_declarator (declarator, expression);
7613 /* If the next token is not a `[', then there are no more
7614 bounds. */
7615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7616 break;
7619 return declarator;
7622 /* Parse a new-initializer.
7624 new-initializer:
7625 ( expression-list [opt] )
7626 braced-init-list
7628 Returns a representation of the expression-list. */
7630 static vec<tree, va_gc> *
7631 cp_parser_new_initializer (cp_parser* parser)
7633 vec<tree, va_gc> *expression_list;
7635 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7637 tree t;
7638 bool expr_non_constant_p;
7639 cp_lexer_set_source_position (parser->lexer);
7640 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7641 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7642 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7643 expression_list = make_tree_vector_single (t);
7645 else
7646 expression_list = (cp_parser_parenthesized_expression_list
7647 (parser, non_attr, /*cast_p=*/false,
7648 /*allow_expansion_p=*/true,
7649 /*non_constant_p=*/NULL));
7651 return expression_list;
7654 /* Parse a delete-expression.
7656 delete-expression:
7657 :: [opt] delete cast-expression
7658 :: [opt] delete [ ] cast-expression
7660 Returns a representation of the expression. */
7662 static tree
7663 cp_parser_delete_expression (cp_parser* parser)
7665 bool global_scope_p;
7666 bool array_p;
7667 tree expression;
7669 /* Look for the optional `::' operator. */
7670 global_scope_p
7671 = (cp_parser_global_scope_opt (parser,
7672 /*current_scope_valid_p=*/false)
7673 != NULL_TREE);
7674 /* Look for the `delete' keyword. */
7675 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7676 /* See if the array syntax is in use. */
7677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7679 /* Consume the `[' token. */
7680 cp_lexer_consume_token (parser->lexer);
7681 /* Look for the `]' token. */
7682 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7683 /* Remember that this is the `[]' construct. */
7684 array_p = true;
7686 else
7687 array_p = false;
7689 /* Parse the cast-expression. */
7690 expression = cp_parser_simple_cast_expression (parser);
7692 /* A delete-expression may not appear in an integral constant
7693 expression. */
7694 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7695 return error_mark_node;
7697 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7698 tf_warning_or_error);
7701 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7702 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7703 0 otherwise. */
7705 static int
7706 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7708 cp_token *token = cp_lexer_peek_token (parser->lexer);
7709 switch (token->type)
7711 case CPP_COMMA:
7712 case CPP_SEMICOLON:
7713 case CPP_QUERY:
7714 case CPP_COLON:
7715 case CPP_CLOSE_SQUARE:
7716 case CPP_CLOSE_PAREN:
7717 case CPP_CLOSE_BRACE:
7718 case CPP_OPEN_BRACE:
7719 case CPP_DOT:
7720 case CPP_DOT_STAR:
7721 case CPP_DEREF:
7722 case CPP_DEREF_STAR:
7723 case CPP_DIV:
7724 case CPP_MOD:
7725 case CPP_LSHIFT:
7726 case CPP_RSHIFT:
7727 case CPP_LESS:
7728 case CPP_GREATER:
7729 case CPP_LESS_EQ:
7730 case CPP_GREATER_EQ:
7731 case CPP_EQ_EQ:
7732 case CPP_NOT_EQ:
7733 case CPP_EQ:
7734 case CPP_MULT_EQ:
7735 case CPP_DIV_EQ:
7736 case CPP_MOD_EQ:
7737 case CPP_PLUS_EQ:
7738 case CPP_MINUS_EQ:
7739 case CPP_RSHIFT_EQ:
7740 case CPP_LSHIFT_EQ:
7741 case CPP_AND_EQ:
7742 case CPP_XOR_EQ:
7743 case CPP_OR_EQ:
7744 case CPP_XOR:
7745 case CPP_OR:
7746 case CPP_OR_OR:
7747 case CPP_EOF:
7748 case CPP_ELLIPSIS:
7749 return 0;
7751 case CPP_OPEN_PAREN:
7752 /* In ((type ()) () the last () isn't a valid cast-expression,
7753 so the whole must be parsed as postfix-expression. */
7754 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7755 != CPP_CLOSE_PAREN;
7757 case CPP_OPEN_SQUARE:
7758 /* '[' may start a primary-expression in obj-c++ and in C++11,
7759 as a lambda-expression, eg, '(void)[]{}'. */
7760 if (cxx_dialect >= cxx11)
7761 return -1;
7762 return c_dialect_objc ();
7764 case CPP_PLUS_PLUS:
7765 case CPP_MINUS_MINUS:
7766 /* '++' and '--' may or may not start a cast-expression:
7768 struct T { void operator++(int); };
7769 void f() { (T())++; }
7773 int a;
7774 (int)++a; */
7775 return -1;
7777 default:
7778 return 1;
7782 /* Parse a cast-expression.
7784 cast-expression:
7785 unary-expression
7786 ( type-id ) cast-expression
7788 ADDRESS_P is true iff the unary-expression is appearing as the
7789 operand of the `&' operator. CAST_P is true if this expression is
7790 the target of a cast.
7792 Returns a representation of the expression. */
7794 static tree
7795 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7796 bool decltype_p, cp_id_kind * pidk)
7798 /* If it's a `(', then we might be looking at a cast. */
7799 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7801 tree type = NULL_TREE;
7802 tree expr = NULL_TREE;
7803 int cast_expression = 0;
7804 const char *saved_message;
7806 /* There's no way to know yet whether or not this is a cast.
7807 For example, `(int (3))' is a unary-expression, while `(int)
7808 3' is a cast. So, we resort to parsing tentatively. */
7809 cp_parser_parse_tentatively (parser);
7810 /* Types may not be defined in a cast. */
7811 saved_message = parser->type_definition_forbidden_message;
7812 parser->type_definition_forbidden_message
7813 = G_("types may not be defined in casts");
7814 /* Consume the `('. */
7815 cp_lexer_consume_token (parser->lexer);
7816 /* A very tricky bit is that `(struct S) { 3 }' is a
7817 compound-literal (which we permit in C++ as an extension).
7818 But, that construct is not a cast-expression -- it is a
7819 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7820 is legal; if the compound-literal were a cast-expression,
7821 you'd need an extra set of parentheses.) But, if we parse
7822 the type-id, and it happens to be a class-specifier, then we
7823 will commit to the parse at that point, because we cannot
7824 undo the action that is done when creating a new class. So,
7825 then we cannot back up and do a postfix-expression.
7827 Another tricky case is the following (c++/29234):
7829 struct S { void operator () (); };
7831 void foo ()
7833 ( S()() );
7836 As a type-id we parse the parenthesized S()() as a function
7837 returning a function, groktypename complains and we cannot
7838 back up in this case either.
7840 Therefore, we scan ahead to the closing `)', and check to see
7841 if the tokens after the `)' can start a cast-expression. Otherwise
7842 we are dealing with an unary-expression, a postfix-expression
7843 or something else.
7845 Yet another tricky case, in C++11, is the following (c++/54891):
7847 (void)[]{};
7849 The issue is that usually, besides the case of lambda-expressions,
7850 the parenthesized type-id cannot be followed by '[', and, eg, we
7851 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7852 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7853 we don't commit, we try a cast-expression, then an unary-expression.
7855 Save tokens so that we can put them back. */
7856 cp_lexer_save_tokens (parser->lexer);
7858 /* We may be looking at a cast-expression. */
7859 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7860 /*consume_paren=*/true))
7861 cast_expression
7862 = cp_parser_tokens_start_cast_expression (parser);
7864 /* Roll back the tokens we skipped. */
7865 cp_lexer_rollback_tokens (parser->lexer);
7866 /* If we aren't looking at a cast-expression, simulate an error so
7867 that the call to cp_parser_error_occurred below returns true. */
7868 if (!cast_expression)
7869 cp_parser_simulate_error (parser);
7870 else
7872 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7873 parser->in_type_id_in_expr_p = true;
7874 /* Look for the type-id. */
7875 type = cp_parser_type_id (parser);
7876 /* Look for the closing `)'. */
7877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7878 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7881 /* Restore the saved message. */
7882 parser->type_definition_forbidden_message = saved_message;
7884 /* At this point this can only be either a cast or a
7885 parenthesized ctor such as `(T ())' that looks like a cast to
7886 function returning T. */
7887 if (!cp_parser_error_occurred (parser))
7889 /* Only commit if the cast-expression doesn't start with
7890 '++', '--', or '[' in C++11. */
7891 if (cast_expression > 0)
7892 cp_parser_commit_to_topmost_tentative_parse (parser);
7894 expr = cp_parser_cast_expression (parser,
7895 /*address_p=*/false,
7896 /*cast_p=*/true,
7897 /*decltype_p=*/false,
7898 pidk);
7900 if (cp_parser_parse_definitely (parser))
7902 /* Warn about old-style casts, if so requested. */
7903 if (warn_old_style_cast
7904 && !in_system_header_at (input_location)
7905 && !VOID_TYPE_P (type)
7906 && current_lang_name != lang_name_c)
7907 warning (OPT_Wold_style_cast, "use of old-style cast");
7909 /* Only type conversions to integral or enumeration types
7910 can be used in constant-expressions. */
7911 if (!cast_valid_in_integral_constant_expression_p (type)
7912 && cp_parser_non_integral_constant_expression (parser,
7913 NIC_CAST))
7914 return error_mark_node;
7916 /* Perform the cast. */
7917 expr = build_c_cast (input_location, type, expr);
7918 return expr;
7921 else
7922 cp_parser_abort_tentative_parse (parser);
7925 /* If we get here, then it's not a cast, so it must be a
7926 unary-expression. */
7927 return cp_parser_unary_expression (parser, address_p, cast_p,
7928 decltype_p, pidk);
7931 /* Parse a binary expression of the general form:
7933 pm-expression:
7934 cast-expression
7935 pm-expression .* cast-expression
7936 pm-expression ->* cast-expression
7938 multiplicative-expression:
7939 pm-expression
7940 multiplicative-expression * pm-expression
7941 multiplicative-expression / pm-expression
7942 multiplicative-expression % pm-expression
7944 additive-expression:
7945 multiplicative-expression
7946 additive-expression + multiplicative-expression
7947 additive-expression - multiplicative-expression
7949 shift-expression:
7950 additive-expression
7951 shift-expression << additive-expression
7952 shift-expression >> additive-expression
7954 relational-expression:
7955 shift-expression
7956 relational-expression < shift-expression
7957 relational-expression > shift-expression
7958 relational-expression <= shift-expression
7959 relational-expression >= shift-expression
7961 GNU Extension:
7963 relational-expression:
7964 relational-expression <? shift-expression
7965 relational-expression >? shift-expression
7967 equality-expression:
7968 relational-expression
7969 equality-expression == relational-expression
7970 equality-expression != relational-expression
7972 and-expression:
7973 equality-expression
7974 and-expression & equality-expression
7976 exclusive-or-expression:
7977 and-expression
7978 exclusive-or-expression ^ and-expression
7980 inclusive-or-expression:
7981 exclusive-or-expression
7982 inclusive-or-expression | exclusive-or-expression
7984 logical-and-expression:
7985 inclusive-or-expression
7986 logical-and-expression && inclusive-or-expression
7988 logical-or-expression:
7989 logical-and-expression
7990 logical-or-expression || logical-and-expression
7992 All these are implemented with a single function like:
7994 binary-expression:
7995 simple-cast-expression
7996 binary-expression <token> binary-expression
7998 CAST_P is true if this expression is the target of a cast.
8000 The binops_by_token map is used to get the tree codes for each <token> type.
8001 binary-expressions are associated according to a precedence table. */
8003 #define TOKEN_PRECEDENCE(token) \
8004 (((token->type == CPP_GREATER \
8005 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8006 && !parser->greater_than_is_operator_p) \
8007 ? PREC_NOT_OPERATOR \
8008 : binops_by_token[token->type].prec)
8010 static tree
8011 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8012 bool no_toplevel_fold_p,
8013 bool decltype_p,
8014 enum cp_parser_prec prec,
8015 cp_id_kind * pidk)
8017 cp_parser_expression_stack stack;
8018 cp_parser_expression_stack_entry *sp = &stack[0];
8019 cp_parser_expression_stack_entry current;
8020 tree rhs;
8021 cp_token *token;
8022 enum tree_code rhs_type;
8023 enum cp_parser_prec new_prec, lookahead_prec;
8024 tree overload;
8026 /* Parse the first expression. */
8027 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8028 ? TRUTH_NOT_EXPR : ERROR_MARK);
8029 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8030 cast_p, decltype_p, pidk);
8031 current.prec = prec;
8033 if (cp_parser_error_occurred (parser))
8034 return error_mark_node;
8036 for (;;)
8038 /* Get an operator token. */
8039 token = cp_lexer_peek_token (parser->lexer);
8041 if (warn_cxx0x_compat
8042 && token->type == CPP_RSHIFT
8043 && !parser->greater_than_is_operator_p)
8045 if (warning_at (token->location, OPT_Wc__0x_compat,
8046 "%<>>%> operator is treated"
8047 " as two right angle brackets in C++11"))
8048 inform (token->location,
8049 "suggest parentheses around %<>>%> expression");
8052 new_prec = TOKEN_PRECEDENCE (token);
8054 /* Popping an entry off the stack means we completed a subexpression:
8055 - either we found a token which is not an operator (`>' where it is not
8056 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8057 will happen repeatedly;
8058 - or, we found an operator which has lower priority. This is the case
8059 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8060 parsing `3 * 4'. */
8061 if (new_prec <= current.prec)
8063 if (sp == stack)
8064 break;
8065 else
8066 goto pop;
8069 get_rhs:
8070 current.tree_type = binops_by_token[token->type].tree_type;
8071 current.loc = token->location;
8073 /* We used the operator token. */
8074 cp_lexer_consume_token (parser->lexer);
8076 /* For "false && x" or "true || x", x will never be executed;
8077 disable warnings while evaluating it. */
8078 if (current.tree_type == TRUTH_ANDIF_EXPR)
8079 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8080 else if (current.tree_type == TRUTH_ORIF_EXPR)
8081 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8083 /* Extract another operand. It may be the RHS of this expression
8084 or the LHS of a new, higher priority expression. */
8085 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8086 ? TRUTH_NOT_EXPR : ERROR_MARK);
8087 rhs = cp_parser_simple_cast_expression (parser);
8089 /* Get another operator token. Look up its precedence to avoid
8090 building a useless (immediately popped) stack entry for common
8091 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8092 token = cp_lexer_peek_token (parser->lexer);
8093 lookahead_prec = TOKEN_PRECEDENCE (token);
8094 if (lookahead_prec > new_prec)
8096 /* ... and prepare to parse the RHS of the new, higher priority
8097 expression. Since precedence levels on the stack are
8098 monotonically increasing, we do not have to care about
8099 stack overflows. */
8100 *sp = current;
8101 ++sp;
8102 current.lhs = rhs;
8103 current.lhs_type = rhs_type;
8104 current.prec = new_prec;
8105 new_prec = lookahead_prec;
8106 goto get_rhs;
8108 pop:
8109 lookahead_prec = new_prec;
8110 /* If the stack is not empty, we have parsed into LHS the right side
8111 (`4' in the example above) of an expression we had suspended.
8112 We can use the information on the stack to recover the LHS (`3')
8113 from the stack together with the tree code (`MULT_EXPR'), and
8114 the precedence of the higher level subexpression
8115 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8116 which will be used to actually build the additive expression. */
8117 rhs = current.lhs;
8118 rhs_type = current.lhs_type;
8119 --sp;
8120 current = *sp;
8123 /* Undo the disabling of warnings done above. */
8124 if (current.tree_type == TRUTH_ANDIF_EXPR)
8125 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8126 else if (current.tree_type == TRUTH_ORIF_EXPR)
8127 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8129 if (warn_logical_not_paren
8130 && current.lhs_type == TRUTH_NOT_EXPR)
8131 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8133 overload = NULL;
8134 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8135 ERROR_MARK for everything that is not a binary expression.
8136 This makes warn_about_parentheses miss some warnings that
8137 involve unary operators. For unary expressions we should
8138 pass the correct tree_code unless the unary expression was
8139 surrounded by parentheses.
8141 if (no_toplevel_fold_p
8142 && lookahead_prec <= current.prec
8143 && sp == stack)
8144 current.lhs = build2 (current.tree_type,
8145 TREE_CODE_CLASS (current.tree_type)
8146 == tcc_comparison
8147 ? boolean_type_node : TREE_TYPE (current.lhs),
8148 current.lhs, rhs);
8149 else
8150 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8151 current.lhs, current.lhs_type,
8152 rhs, rhs_type, &overload,
8153 complain_flags (decltype_p));
8154 current.lhs_type = current.tree_type;
8155 if (EXPR_P (current.lhs))
8156 SET_EXPR_LOCATION (current.lhs, current.loc);
8158 /* If the binary operator required the use of an overloaded operator,
8159 then this expression cannot be an integral constant-expression.
8160 An overloaded operator can be used even if both operands are
8161 otherwise permissible in an integral constant-expression if at
8162 least one of the operands is of enumeration type. */
8164 if (overload
8165 && cp_parser_non_integral_constant_expression (parser,
8166 NIC_OVERLOADED))
8167 return error_mark_node;
8170 return current.lhs;
8173 static tree
8174 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8175 bool no_toplevel_fold_p,
8176 enum cp_parser_prec prec,
8177 cp_id_kind * pidk)
8179 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8180 /*decltype*/false, prec, pidk);
8183 /* Parse the `? expression : assignment-expression' part of a
8184 conditional-expression. The LOGICAL_OR_EXPR is the
8185 logical-or-expression that started the conditional-expression.
8186 Returns a representation of the entire conditional-expression.
8188 This routine is used by cp_parser_assignment_expression.
8190 ? expression : assignment-expression
8192 GNU Extensions:
8194 ? : assignment-expression */
8196 static tree
8197 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8199 tree expr;
8200 tree assignment_expr;
8201 struct cp_token *token;
8202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8204 /* Consume the `?' token. */
8205 cp_lexer_consume_token (parser->lexer);
8206 token = cp_lexer_peek_token (parser->lexer);
8207 if (cp_parser_allow_gnu_extensions_p (parser)
8208 && token->type == CPP_COLON)
8210 pedwarn (token->location, OPT_Wpedantic,
8211 "ISO C++ does not allow ?: with omitted middle operand");
8212 /* Implicit true clause. */
8213 expr = NULL_TREE;
8214 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8215 warn_for_omitted_condop (token->location, logical_or_expr);
8217 else
8219 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8220 parser->colon_corrects_to_scope_p = false;
8221 /* Parse the expression. */
8222 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8223 expr = cp_parser_expression (parser);
8224 c_inhibit_evaluation_warnings +=
8225 ((logical_or_expr == truthvalue_true_node)
8226 - (logical_or_expr == truthvalue_false_node));
8227 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8230 /* The next token should be a `:'. */
8231 cp_parser_require (parser, CPP_COLON, RT_COLON);
8232 /* Parse the assignment-expression. */
8233 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8234 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8236 /* Build the conditional-expression. */
8237 return build_x_conditional_expr (loc, logical_or_expr,
8238 expr,
8239 assignment_expr,
8240 tf_warning_or_error);
8243 /* Parse an assignment-expression.
8245 assignment-expression:
8246 conditional-expression
8247 logical-or-expression assignment-operator assignment_expression
8248 throw-expression
8250 CAST_P is true if this expression is the target of a cast.
8251 DECLTYPE_P is true if this expression is the operand of decltype.
8253 Returns a representation for the expression. */
8255 static tree
8256 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8257 bool decltype_p, cp_id_kind * pidk)
8259 tree expr;
8261 /* If the next token is the `throw' keyword, then we're looking at
8262 a throw-expression. */
8263 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8264 expr = cp_parser_throw_expression (parser);
8265 /* Otherwise, it must be that we are looking at a
8266 logical-or-expression. */
8267 else
8269 /* Parse the binary expressions (logical-or-expression). */
8270 expr = cp_parser_binary_expression (parser, cast_p, false,
8271 decltype_p,
8272 PREC_NOT_OPERATOR, pidk);
8273 /* If the next token is a `?' then we're actually looking at a
8274 conditional-expression. */
8275 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8276 return cp_parser_question_colon_clause (parser, expr);
8277 else
8279 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8281 /* If it's an assignment-operator, we're using the second
8282 production. */
8283 enum tree_code assignment_operator
8284 = cp_parser_assignment_operator_opt (parser);
8285 if (assignment_operator != ERROR_MARK)
8287 bool non_constant_p;
8288 location_t saved_input_location;
8290 /* Parse the right-hand side of the assignment. */
8291 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8293 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8294 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8296 /* An assignment may not appear in a
8297 constant-expression. */
8298 if (cp_parser_non_integral_constant_expression (parser,
8299 NIC_ASSIGNMENT))
8300 return error_mark_node;
8301 /* Build the assignment expression. Its default
8302 location is the location of the '=' token. */
8303 saved_input_location = input_location;
8304 input_location = loc;
8305 expr = build_x_modify_expr (loc, expr,
8306 assignment_operator,
8307 rhs,
8308 complain_flags (decltype_p));
8309 input_location = saved_input_location;
8314 return expr;
8317 static tree
8318 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8319 cp_id_kind * pidk)
8321 return cp_parser_assignment_expression (parser, cast_p,
8322 /*decltype*/false, pidk);
8325 /* Parse an (optional) assignment-operator.
8327 assignment-operator: one of
8328 = *= /= %= += -= >>= <<= &= ^= |=
8330 GNU Extension:
8332 assignment-operator: one of
8333 <?= >?=
8335 If the next token is an assignment operator, the corresponding tree
8336 code is returned, and the token is consumed. For example, for
8337 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8338 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8339 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8340 operator, ERROR_MARK is returned. */
8342 static enum tree_code
8343 cp_parser_assignment_operator_opt (cp_parser* parser)
8345 enum tree_code op;
8346 cp_token *token;
8348 /* Peek at the next token. */
8349 token = cp_lexer_peek_token (parser->lexer);
8351 switch (token->type)
8353 case CPP_EQ:
8354 op = NOP_EXPR;
8355 break;
8357 case CPP_MULT_EQ:
8358 op = MULT_EXPR;
8359 break;
8361 case CPP_DIV_EQ:
8362 op = TRUNC_DIV_EXPR;
8363 break;
8365 case CPP_MOD_EQ:
8366 op = TRUNC_MOD_EXPR;
8367 break;
8369 case CPP_PLUS_EQ:
8370 op = PLUS_EXPR;
8371 break;
8373 case CPP_MINUS_EQ:
8374 op = MINUS_EXPR;
8375 break;
8377 case CPP_RSHIFT_EQ:
8378 op = RSHIFT_EXPR;
8379 break;
8381 case CPP_LSHIFT_EQ:
8382 op = LSHIFT_EXPR;
8383 break;
8385 case CPP_AND_EQ:
8386 op = BIT_AND_EXPR;
8387 break;
8389 case CPP_XOR_EQ:
8390 op = BIT_XOR_EXPR;
8391 break;
8393 case CPP_OR_EQ:
8394 op = BIT_IOR_EXPR;
8395 break;
8397 default:
8398 /* Nothing else is an assignment operator. */
8399 op = ERROR_MARK;
8402 /* If it was an assignment operator, consume it. */
8403 if (op != ERROR_MARK)
8404 cp_lexer_consume_token (parser->lexer);
8406 return op;
8409 /* Parse an expression.
8411 expression:
8412 assignment-expression
8413 expression , assignment-expression
8415 CAST_P is true if this expression is the target of a cast.
8416 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8417 except possibly parenthesized or on the RHS of a comma (N3276).
8419 Returns a representation of the expression. */
8421 static tree
8422 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8423 bool cast_p, bool decltype_p)
8425 tree expression = NULL_TREE;
8426 location_t loc = UNKNOWN_LOCATION;
8428 while (true)
8430 tree assignment_expression;
8432 /* Parse the next assignment-expression. */
8433 assignment_expression
8434 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8436 /* We don't create a temporary for a call that is the immediate operand
8437 of decltype or on the RHS of a comma. But when we see a comma, we
8438 need to create a temporary for a call on the LHS. */
8439 if (decltype_p && !processing_template_decl
8440 && TREE_CODE (assignment_expression) == CALL_EXPR
8441 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8442 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8443 assignment_expression
8444 = build_cplus_new (TREE_TYPE (assignment_expression),
8445 assignment_expression, tf_warning_or_error);
8447 /* If this is the first assignment-expression, we can just
8448 save it away. */
8449 if (!expression)
8450 expression = assignment_expression;
8451 else
8452 expression = build_x_compound_expr (loc, expression,
8453 assignment_expression,
8454 complain_flags (decltype_p));
8455 /* If the next token is not a comma, then we are done with the
8456 expression. */
8457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8458 break;
8459 /* Consume the `,'. */
8460 loc = cp_lexer_peek_token (parser->lexer)->location;
8461 cp_lexer_consume_token (parser->lexer);
8462 /* A comma operator cannot appear in a constant-expression. */
8463 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8464 expression = error_mark_node;
8467 return expression;
8470 /* Parse a constant-expression.
8472 constant-expression:
8473 conditional-expression
8475 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8476 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8477 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8478 is false, NON_CONSTANT_P should be NULL. */
8480 static tree
8481 cp_parser_constant_expression (cp_parser* parser,
8482 bool allow_non_constant_p,
8483 bool *non_constant_p)
8485 bool saved_integral_constant_expression_p;
8486 bool saved_allow_non_integral_constant_expression_p;
8487 bool saved_non_integral_constant_expression_p;
8488 tree expression;
8490 /* It might seem that we could simply parse the
8491 conditional-expression, and then check to see if it were
8492 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8493 one that the compiler can figure out is constant, possibly after
8494 doing some simplifications or optimizations. The standard has a
8495 precise definition of constant-expression, and we must honor
8496 that, even though it is somewhat more restrictive.
8498 For example:
8500 int i[(2, 3)];
8502 is not a legal declaration, because `(2, 3)' is not a
8503 constant-expression. The `,' operator is forbidden in a
8504 constant-expression. However, GCC's constant-folding machinery
8505 will fold this operation to an INTEGER_CST for `3'. */
8507 /* Save the old settings. */
8508 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8509 saved_allow_non_integral_constant_expression_p
8510 = parser->allow_non_integral_constant_expression_p;
8511 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8512 /* We are now parsing a constant-expression. */
8513 parser->integral_constant_expression_p = true;
8514 parser->allow_non_integral_constant_expression_p
8515 = (allow_non_constant_p || cxx_dialect >= cxx11);
8516 parser->non_integral_constant_expression_p = false;
8517 /* Although the grammar says "conditional-expression", we parse an
8518 "assignment-expression", which also permits "throw-expression"
8519 and the use of assignment operators. In the case that
8520 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8521 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8522 actually essential that we look for an assignment-expression.
8523 For example, cp_parser_initializer_clauses uses this function to
8524 determine whether a particular assignment-expression is in fact
8525 constant. */
8526 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8527 /* Restore the old settings. */
8528 parser->integral_constant_expression_p
8529 = saved_integral_constant_expression_p;
8530 parser->allow_non_integral_constant_expression_p
8531 = saved_allow_non_integral_constant_expression_p;
8532 if (cxx_dialect >= cxx11)
8534 /* Require an rvalue constant expression here; that's what our
8535 callers expect. Reference constant expressions are handled
8536 separately in e.g. cp_parser_template_argument. */
8537 bool is_const = potential_rvalue_constant_expression (expression);
8538 parser->non_integral_constant_expression_p = !is_const;
8539 if (!is_const && !allow_non_constant_p)
8540 require_potential_rvalue_constant_expression (expression);
8542 if (allow_non_constant_p)
8543 *non_constant_p = parser->non_integral_constant_expression_p;
8544 parser->non_integral_constant_expression_p
8545 = saved_non_integral_constant_expression_p;
8547 return expression;
8550 /* Parse __builtin_offsetof.
8552 offsetof-expression:
8553 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8555 offsetof-member-designator:
8556 id-expression
8557 | offsetof-member-designator "." id-expression
8558 | offsetof-member-designator "[" expression "]"
8559 | offsetof-member-designator "->" id-expression */
8561 static tree
8562 cp_parser_builtin_offsetof (cp_parser *parser)
8564 int save_ice_p, save_non_ice_p;
8565 tree type, expr;
8566 cp_id_kind dummy;
8567 cp_token *token;
8569 /* We're about to accept non-integral-constant things, but will
8570 definitely yield an integral constant expression. Save and
8571 restore these values around our local parsing. */
8572 save_ice_p = parser->integral_constant_expression_p;
8573 save_non_ice_p = parser->non_integral_constant_expression_p;
8575 /* Consume the "__builtin_offsetof" token. */
8576 cp_lexer_consume_token (parser->lexer);
8577 /* Consume the opening `('. */
8578 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8579 /* Parse the type-id. */
8580 type = cp_parser_type_id (parser);
8581 /* Look for the `,'. */
8582 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8583 token = cp_lexer_peek_token (parser->lexer);
8585 /* Build the (type *)null that begins the traditional offsetof macro. */
8586 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8587 tf_warning_or_error);
8589 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8590 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8591 true, &dummy, token->location);
8592 while (true)
8594 token = cp_lexer_peek_token (parser->lexer);
8595 switch (token->type)
8597 case CPP_OPEN_SQUARE:
8598 /* offsetof-member-designator "[" expression "]" */
8599 expr = cp_parser_postfix_open_square_expression (parser, expr,
8600 true, false);
8601 break;
8603 case CPP_DEREF:
8604 /* offsetof-member-designator "->" identifier */
8605 expr = grok_array_decl (token->location, expr,
8606 integer_zero_node, false);
8607 /* FALLTHRU */
8609 case CPP_DOT:
8610 /* offsetof-member-designator "." identifier */
8611 cp_lexer_consume_token (parser->lexer);
8612 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8613 expr, true, &dummy,
8614 token->location);
8615 break;
8617 case CPP_CLOSE_PAREN:
8618 /* Consume the ")" token. */
8619 cp_lexer_consume_token (parser->lexer);
8620 goto success;
8622 default:
8623 /* Error. We know the following require will fail, but
8624 that gives the proper error message. */
8625 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8626 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8627 expr = error_mark_node;
8628 goto failure;
8632 success:
8633 /* If we're processing a template, we can't finish the semantics yet.
8634 Otherwise we can fold the entire expression now. */
8635 if (processing_template_decl)
8636 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8637 else
8638 expr = finish_offsetof (expr);
8640 failure:
8641 parser->integral_constant_expression_p = save_ice_p;
8642 parser->non_integral_constant_expression_p = save_non_ice_p;
8644 return expr;
8647 /* Parse a trait expression.
8649 Returns a representation of the expression, the underlying type
8650 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8652 static tree
8653 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8655 cp_trait_kind kind;
8656 tree type1, type2 = NULL_TREE;
8657 bool binary = false;
8658 cp_decl_specifier_seq decl_specs;
8660 switch (keyword)
8662 case RID_HAS_NOTHROW_ASSIGN:
8663 kind = CPTK_HAS_NOTHROW_ASSIGN;
8664 break;
8665 case RID_HAS_NOTHROW_CONSTRUCTOR:
8666 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8667 break;
8668 case RID_HAS_NOTHROW_COPY:
8669 kind = CPTK_HAS_NOTHROW_COPY;
8670 break;
8671 case RID_HAS_TRIVIAL_ASSIGN:
8672 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8673 break;
8674 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8675 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8676 break;
8677 case RID_HAS_TRIVIAL_COPY:
8678 kind = CPTK_HAS_TRIVIAL_COPY;
8679 break;
8680 case RID_HAS_TRIVIAL_DESTRUCTOR:
8681 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8682 break;
8683 case RID_HAS_VIRTUAL_DESTRUCTOR:
8684 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8685 break;
8686 case RID_IS_ABSTRACT:
8687 kind = CPTK_IS_ABSTRACT;
8688 break;
8689 case RID_IS_BASE_OF:
8690 kind = CPTK_IS_BASE_OF;
8691 binary = true;
8692 break;
8693 case RID_IS_CLASS:
8694 kind = CPTK_IS_CLASS;
8695 break;
8696 case RID_IS_CONVERTIBLE_TO:
8697 kind = CPTK_IS_CONVERTIBLE_TO;
8698 binary = true;
8699 break;
8700 case RID_IS_EMPTY:
8701 kind = CPTK_IS_EMPTY;
8702 break;
8703 case RID_IS_ENUM:
8704 kind = CPTK_IS_ENUM;
8705 break;
8706 case RID_IS_FINAL:
8707 kind = CPTK_IS_FINAL;
8708 break;
8709 case RID_IS_LITERAL_TYPE:
8710 kind = CPTK_IS_LITERAL_TYPE;
8711 break;
8712 case RID_IS_POD:
8713 kind = CPTK_IS_POD;
8714 break;
8715 case RID_IS_POLYMORPHIC:
8716 kind = CPTK_IS_POLYMORPHIC;
8717 break;
8718 case RID_IS_STD_LAYOUT:
8719 kind = CPTK_IS_STD_LAYOUT;
8720 break;
8721 case RID_IS_TRIVIAL:
8722 kind = CPTK_IS_TRIVIAL;
8723 break;
8724 case RID_IS_UNION:
8725 kind = CPTK_IS_UNION;
8726 break;
8727 case RID_UNDERLYING_TYPE:
8728 kind = CPTK_UNDERLYING_TYPE;
8729 break;
8730 case RID_BASES:
8731 kind = CPTK_BASES;
8732 break;
8733 case RID_DIRECT_BASES:
8734 kind = CPTK_DIRECT_BASES;
8735 break;
8736 default:
8737 gcc_unreachable ();
8740 /* Consume the token. */
8741 cp_lexer_consume_token (parser->lexer);
8743 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8745 type1 = cp_parser_type_id (parser);
8747 if (type1 == error_mark_node)
8748 return error_mark_node;
8750 /* Build a trivial decl-specifier-seq. */
8751 clear_decl_specs (&decl_specs);
8752 decl_specs.type = type1;
8754 /* Call grokdeclarator to figure out what type this is. */
8755 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8756 /*initialized=*/0, /*attrlist=*/NULL);
8758 if (binary)
8760 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8762 type2 = cp_parser_type_id (parser);
8764 if (type2 == error_mark_node)
8765 return error_mark_node;
8767 /* Build a trivial decl-specifier-seq. */
8768 clear_decl_specs (&decl_specs);
8769 decl_specs.type = type2;
8771 /* Call grokdeclarator to figure out what type this is. */
8772 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8773 /*initialized=*/0, /*attrlist=*/NULL);
8776 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8778 /* Complete the trait expression, which may mean either processing
8779 the trait expr now or saving it for template instantiation. */
8780 switch(kind)
8782 case CPTK_UNDERLYING_TYPE:
8783 return finish_underlying_type (type1);
8784 case CPTK_BASES:
8785 return finish_bases (type1, false);
8786 case CPTK_DIRECT_BASES:
8787 return finish_bases (type1, true);
8788 default:
8789 return finish_trait_expr (kind, type1, type2);
8793 /* Lambdas that appear in variable initializer or default argument scope
8794 get that in their mangling, so we need to record it. We might as well
8795 use the count for function and namespace scopes as well. */
8796 static GTY(()) tree lambda_scope;
8797 static GTY(()) int lambda_count;
8798 typedef struct GTY(()) tree_int
8800 tree t;
8801 int i;
8802 } tree_int;
8803 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8805 static void
8806 start_lambda_scope (tree decl)
8808 tree_int ti;
8809 gcc_assert (decl);
8810 /* Once we're inside a function, we ignore other scopes and just push
8811 the function again so that popping works properly. */
8812 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8813 decl = current_function_decl;
8814 ti.t = lambda_scope;
8815 ti.i = lambda_count;
8816 vec_safe_push (lambda_scope_stack, ti);
8817 if (lambda_scope != decl)
8819 /* Don't reset the count if we're still in the same function. */
8820 lambda_scope = decl;
8821 lambda_count = 0;
8825 static void
8826 record_lambda_scope (tree lambda)
8828 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8829 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8832 static void
8833 finish_lambda_scope (void)
8835 tree_int *p = &lambda_scope_stack->last ();
8836 if (lambda_scope != p->t)
8838 lambda_scope = p->t;
8839 lambda_count = p->i;
8841 lambda_scope_stack->pop ();
8844 /* Parse a lambda expression.
8846 lambda-expression:
8847 lambda-introducer lambda-declarator [opt] compound-statement
8849 Returns a representation of the expression. */
8851 static tree
8852 cp_parser_lambda_expression (cp_parser* parser)
8854 tree lambda_expr = build_lambda_expr ();
8855 tree type;
8856 bool ok = true;
8857 cp_token *token = cp_lexer_peek_token (parser->lexer);
8859 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8861 if (cp_unevaluated_operand)
8863 if (!token->error_reported)
8865 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8866 "lambda-expression in unevaluated context");
8867 token->error_reported = true;
8869 ok = false;
8872 /* We may be in the middle of deferred access check. Disable
8873 it now. */
8874 push_deferring_access_checks (dk_no_deferred);
8876 cp_parser_lambda_introducer (parser, lambda_expr);
8878 type = begin_lambda_type (lambda_expr);
8879 if (type == error_mark_node)
8880 return error_mark_node;
8882 record_lambda_scope (lambda_expr);
8884 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8885 determine_visibility (TYPE_NAME (type));
8887 /* Now that we've started the type, add the capture fields for any
8888 explicit captures. */
8889 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8892 /* Inside the class, surrounding template-parameter-lists do not apply. */
8893 unsigned int saved_num_template_parameter_lists
8894 = parser->num_template_parameter_lists;
8895 unsigned char in_statement = parser->in_statement;
8896 bool in_switch_statement_p = parser->in_switch_statement_p;
8897 bool fully_implicit_function_template_p
8898 = parser->fully_implicit_function_template_p;
8899 tree implicit_template_parms = parser->implicit_template_parms;
8900 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8901 bool auto_is_implicit_function_template_parm_p
8902 = parser->auto_is_implicit_function_template_parm_p;
8904 parser->num_template_parameter_lists = 0;
8905 parser->in_statement = 0;
8906 parser->in_switch_statement_p = false;
8907 parser->fully_implicit_function_template_p = false;
8908 parser->implicit_template_parms = 0;
8909 parser->implicit_template_scope = 0;
8910 parser->auto_is_implicit_function_template_parm_p = false;
8912 /* By virtue of defining a local class, a lambda expression has access to
8913 the private variables of enclosing classes. */
8915 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8917 if (ok)
8918 cp_parser_lambda_body (parser, lambda_expr);
8919 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8921 if (cp_parser_skip_to_closing_brace (parser))
8922 cp_lexer_consume_token (parser->lexer);
8925 /* The capture list was built up in reverse order; fix that now. */
8926 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8927 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8929 if (ok)
8930 maybe_add_lambda_conv_op (type);
8932 type = finish_struct (type, /*attributes=*/NULL_TREE);
8934 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8935 parser->in_statement = in_statement;
8936 parser->in_switch_statement_p = in_switch_statement_p;
8937 parser->fully_implicit_function_template_p
8938 = fully_implicit_function_template_p;
8939 parser->implicit_template_parms = implicit_template_parms;
8940 parser->implicit_template_scope = implicit_template_scope;
8941 parser->auto_is_implicit_function_template_parm_p
8942 = auto_is_implicit_function_template_parm_p;
8945 pop_deferring_access_checks ();
8947 /* This field is only used during parsing of the lambda. */
8948 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8950 /* This lambda shouldn't have any proxies left at this point. */
8951 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8952 /* And now that we're done, push proxies for an enclosing lambda. */
8953 insert_pending_capture_proxies ();
8955 if (ok)
8956 return build_lambda_object (lambda_expr);
8957 else
8958 return error_mark_node;
8961 /* Parse the beginning of a lambda expression.
8963 lambda-introducer:
8964 [ lambda-capture [opt] ]
8966 LAMBDA_EXPR is the current representation of the lambda expression. */
8968 static void
8969 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8971 /* Need commas after the first capture. */
8972 bool first = true;
8974 /* Eat the leading `['. */
8975 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8977 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8978 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8979 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8980 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8981 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8982 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8984 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8986 cp_lexer_consume_token (parser->lexer);
8987 first = false;
8990 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8992 cp_token* capture_token;
8993 tree capture_id;
8994 tree capture_init_expr;
8995 cp_id_kind idk = CP_ID_KIND_NONE;
8996 bool explicit_init_p = false;
8998 enum capture_kind_type
9000 BY_COPY,
9001 BY_REFERENCE
9003 enum capture_kind_type capture_kind = BY_COPY;
9005 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9007 error ("expected end of capture-list");
9008 return;
9011 if (first)
9012 first = false;
9013 else
9014 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9016 /* Possibly capture `this'. */
9017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9019 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9020 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9021 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9022 "with by-copy capture default");
9023 cp_lexer_consume_token (parser->lexer);
9024 add_capture (lambda_expr,
9025 /*id=*/this_identifier,
9026 /*initializer=*/finish_this_expr(),
9027 /*by_reference_p=*/false,
9028 explicit_init_p);
9029 continue;
9032 /* Remember whether we want to capture as a reference or not. */
9033 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9035 capture_kind = BY_REFERENCE;
9036 cp_lexer_consume_token (parser->lexer);
9039 /* Get the identifier. */
9040 capture_token = cp_lexer_peek_token (parser->lexer);
9041 capture_id = cp_parser_identifier (parser);
9043 if (capture_id == error_mark_node)
9044 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9045 delimiters, but I modified this to stop on unnested ']' as well. It
9046 was already changed to stop on unnested '}', so the
9047 "closing_parenthesis" name is no more misleading with my change. */
9049 cp_parser_skip_to_closing_parenthesis (parser,
9050 /*recovering=*/true,
9051 /*or_comma=*/true,
9052 /*consume_paren=*/true);
9053 break;
9056 /* Find the initializer for this capture. */
9057 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9058 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9059 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9061 bool direct, non_constant;
9062 /* An explicit initializer exists. */
9063 if (cxx_dialect < cxx14)
9064 pedwarn (input_location, 0,
9065 "lambda capture initializers "
9066 "only available with -std=c++14 or -std=gnu++14");
9067 capture_init_expr = cp_parser_initializer (parser, &direct,
9068 &non_constant);
9069 explicit_init_p = true;
9070 if (capture_init_expr == NULL_TREE)
9072 error ("empty initializer for lambda init-capture");
9073 capture_init_expr = error_mark_node;
9076 else
9078 const char* error_msg;
9080 /* Turn the identifier into an id-expression. */
9081 capture_init_expr
9082 = cp_parser_lookup_name_simple (parser, capture_id,
9083 capture_token->location);
9085 if (capture_init_expr == error_mark_node)
9087 unqualified_name_lookup_error (capture_id);
9088 continue;
9090 else if (DECL_P (capture_init_expr)
9091 && (!VAR_P (capture_init_expr)
9092 && TREE_CODE (capture_init_expr) != PARM_DECL))
9094 error_at (capture_token->location,
9095 "capture of non-variable %qD ",
9096 capture_init_expr);
9097 inform (0, "%q+#D declared here", capture_init_expr);
9098 continue;
9100 if (VAR_P (capture_init_expr)
9101 && decl_storage_duration (capture_init_expr) != dk_auto)
9103 if (pedwarn (capture_token->location, 0, "capture of variable "
9104 "%qD with non-automatic storage duration",
9105 capture_init_expr))
9106 inform (0, "%q+#D declared here", capture_init_expr);
9107 continue;
9110 capture_init_expr
9111 = finish_id_expression
9112 (capture_id,
9113 capture_init_expr,
9114 parser->scope,
9115 &idk,
9116 /*integral_constant_expression_p=*/false,
9117 /*allow_non_integral_constant_expression_p=*/false,
9118 /*non_integral_constant_expression_p=*/NULL,
9119 /*template_p=*/false,
9120 /*done=*/true,
9121 /*address_p=*/false,
9122 /*template_arg_p=*/false,
9123 &error_msg,
9124 capture_token->location);
9126 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9128 cp_lexer_consume_token (parser->lexer);
9129 capture_init_expr = make_pack_expansion (capture_init_expr);
9131 else
9132 check_for_bare_parameter_packs (capture_init_expr);
9135 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9136 && !explicit_init_p)
9138 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9139 && capture_kind == BY_COPY)
9140 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9141 "of %qD redundant with by-copy capture default",
9142 capture_id);
9143 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9144 && capture_kind == BY_REFERENCE)
9145 pedwarn (capture_token->location, 0, "explicit by-reference "
9146 "capture of %qD redundant with by-reference capture "
9147 "default", capture_id);
9150 add_capture (lambda_expr,
9151 capture_id,
9152 capture_init_expr,
9153 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9154 explicit_init_p);
9157 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9160 /* Parse the (optional) middle of a lambda expression.
9162 lambda-declarator:
9163 < template-parameter-list [opt] >
9164 ( parameter-declaration-clause [opt] )
9165 attribute-specifier [opt]
9166 mutable [opt]
9167 exception-specification [opt]
9168 lambda-return-type-clause [opt]
9170 LAMBDA_EXPR is the current representation of the lambda expression. */
9172 static bool
9173 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9175 /* 5.1.1.4 of the standard says:
9176 If a lambda-expression does not include a lambda-declarator, it is as if
9177 the lambda-declarator were ().
9178 This means an empty parameter list, no attributes, and no exception
9179 specification. */
9180 tree param_list = void_list_node;
9181 tree attributes = NULL_TREE;
9182 tree exception_spec = NULL_TREE;
9183 tree template_param_list = NULL_TREE;
9185 /* The template-parameter-list is optional, but must begin with
9186 an opening angle if present. */
9187 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9189 if (cxx_dialect < cxx14)
9190 pedwarn (parser->lexer->next_token->location, 0,
9191 "lambda templates are only available with "
9192 "-std=c++14 or -std=gnu++14");
9194 cp_lexer_consume_token (parser->lexer);
9196 template_param_list = cp_parser_template_parameter_list (parser);
9198 cp_parser_skip_to_end_of_template_parameter_list (parser);
9200 /* We just processed one more parameter list. */
9201 ++parser->num_template_parameter_lists;
9204 /* The parameter-declaration-clause is optional (unless
9205 template-parameter-list was given), but must begin with an
9206 opening parenthesis if present. */
9207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9209 cp_lexer_consume_token (parser->lexer);
9211 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9213 /* Parse parameters. */
9214 param_list = cp_parser_parameter_declaration_clause (parser);
9216 /* Default arguments shall not be specified in the
9217 parameter-declaration-clause of a lambda-declarator. */
9218 for (tree t = param_list; t; t = TREE_CHAIN (t))
9219 if (TREE_PURPOSE (t))
9220 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9221 "default argument specified for lambda parameter");
9223 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9225 attributes = cp_parser_attributes_opt (parser);
9227 /* Parse optional `mutable' keyword. */
9228 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9230 cp_lexer_consume_token (parser->lexer);
9231 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9234 /* Parse optional exception specification. */
9235 exception_spec = cp_parser_exception_specification_opt (parser);
9237 /* Parse optional trailing return type. */
9238 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9240 cp_lexer_consume_token (parser->lexer);
9241 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9242 = cp_parser_trailing_type_id (parser);
9245 /* The function parameters must be in scope all the way until after the
9246 trailing-return-type in case of decltype. */
9247 pop_bindings_and_leave_scope ();
9249 else if (template_param_list != NULL_TREE) // generate diagnostic
9250 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9252 /* Create the function call operator.
9254 Messing with declarators like this is no uglier than building up the
9255 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9256 other code. */
9258 cp_decl_specifier_seq return_type_specs;
9259 cp_declarator* declarator;
9260 tree fco;
9261 int quals;
9262 void *p;
9264 clear_decl_specs (&return_type_specs);
9265 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9266 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9267 else
9268 /* Maybe we will deduce the return type later. */
9269 return_type_specs.type = make_auto ();
9271 p = obstack_alloc (&declarator_obstack, 0);
9273 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9274 sfk_none);
9276 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9277 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9278 declarator = make_call_declarator (declarator, param_list, quals,
9279 VIRT_SPEC_UNSPECIFIED,
9280 REF_QUAL_NONE,
9281 exception_spec,
9282 /*late_return_type=*/NULL_TREE);
9283 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9285 fco = grokmethod (&return_type_specs,
9286 declarator,
9287 attributes);
9288 if (fco != error_mark_node)
9290 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9291 DECL_ARTIFICIAL (fco) = 1;
9292 /* Give the object parameter a different name. */
9293 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9294 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9295 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9297 if (template_param_list)
9299 fco = finish_member_template_decl (fco);
9300 finish_template_decl (template_param_list);
9301 --parser->num_template_parameter_lists;
9303 else if (parser->fully_implicit_function_template_p)
9304 fco = finish_fully_implicit_template (parser, fco);
9306 finish_member_declaration (fco);
9308 obstack_free (&declarator_obstack, p);
9310 return (fco != error_mark_node);
9314 /* Parse the body of a lambda expression, which is simply
9316 compound-statement
9318 but which requires special handling.
9319 LAMBDA_EXPR is the current representation of the lambda expression. */
9321 static void
9322 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9324 bool nested = (current_function_decl != NULL_TREE);
9325 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9326 if (nested)
9327 push_function_context ();
9328 else
9329 /* Still increment function_depth so that we don't GC in the
9330 middle of an expression. */
9331 ++function_depth;
9332 /* Clear this in case we're in the middle of a default argument. */
9333 parser->local_variables_forbidden_p = false;
9335 /* Finish the function call operator
9336 - class_specifier
9337 + late_parsing_for_member
9338 + function_definition_after_declarator
9339 + ctor_initializer_opt_and_function_body */
9341 tree fco = lambda_function (lambda_expr);
9342 tree body;
9343 bool done = false;
9344 tree compound_stmt;
9345 tree cap;
9347 /* Let the front end know that we are going to be defining this
9348 function. */
9349 start_preparsed_function (fco,
9350 NULL_TREE,
9351 SF_PRE_PARSED | SF_INCLASS_INLINE);
9353 start_lambda_scope (fco);
9354 body = begin_function_body ();
9356 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9357 goto out;
9359 /* Push the proxies for any explicit captures. */
9360 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9361 cap = TREE_CHAIN (cap))
9362 build_capture_proxy (TREE_PURPOSE (cap));
9364 compound_stmt = begin_compound_stmt (0);
9366 /* 5.1.1.4 of the standard says:
9367 If a lambda-expression does not include a trailing-return-type, it
9368 is as if the trailing-return-type denotes the following type:
9369 * if the compound-statement is of the form
9370 { return attribute-specifier [opt] expression ; }
9371 the type of the returned expression after lvalue-to-rvalue
9372 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9373 (_conv.array_ 4.2), and function-to-pointer conversion
9374 (_conv.func_ 4.3);
9375 * otherwise, void. */
9377 /* In a lambda that has neither a lambda-return-type-clause
9378 nor a deducible form, errors should be reported for return statements
9379 in the body. Since we used void as the placeholder return type, parsing
9380 the body as usual will give such desired behavior. */
9381 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9382 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9383 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9385 tree expr = NULL_TREE;
9386 cp_id_kind idk = CP_ID_KIND_NONE;
9388 /* Parse tentatively in case there's more after the initial return
9389 statement. */
9390 cp_parser_parse_tentatively (parser);
9392 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9394 expr = cp_parser_expression (parser, &idk);
9396 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9397 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9399 if (cp_parser_parse_definitely (parser))
9401 if (!processing_template_decl)
9402 apply_deduced_return_type (fco, lambda_return_type (expr));
9404 /* Will get error here if type not deduced yet. */
9405 finish_return_stmt (expr);
9407 done = true;
9411 if (!done)
9413 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9414 cp_parser_label_declaration (parser);
9415 cp_parser_statement_seq_opt (parser, NULL_TREE);
9416 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9419 finish_compound_stmt (compound_stmt);
9421 out:
9422 finish_function_body (body);
9423 finish_lambda_scope ();
9425 /* Finish the function and generate code for it if necessary. */
9426 tree fn = finish_function (/*inline*/2);
9428 /* Only expand if the call op is not a template. */
9429 if (!DECL_TEMPLATE_INFO (fco))
9430 expand_or_defer_fn (fn);
9433 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9434 if (nested)
9435 pop_function_context();
9436 else
9437 --function_depth;
9440 /* Statements [gram.stmt.stmt] */
9442 /* Parse a statement.
9444 statement:
9445 labeled-statement
9446 expression-statement
9447 compound-statement
9448 selection-statement
9449 iteration-statement
9450 jump-statement
9451 declaration-statement
9452 try-block
9454 C++11:
9456 statement:
9457 labeled-statement
9458 attribute-specifier-seq (opt) expression-statement
9459 attribute-specifier-seq (opt) compound-statement
9460 attribute-specifier-seq (opt) selection-statement
9461 attribute-specifier-seq (opt) iteration-statement
9462 attribute-specifier-seq (opt) jump-statement
9463 declaration-statement
9464 attribute-specifier-seq (opt) try-block
9466 TM Extension:
9468 statement:
9469 atomic-statement
9471 IN_COMPOUND is true when the statement is nested inside a
9472 cp_parser_compound_statement; this matters for certain pragmas.
9474 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9475 is a (possibly labeled) if statement which is not enclosed in braces
9476 and has an else clause. This is used to implement -Wparentheses. */
9478 static void
9479 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9480 bool in_compound, bool *if_p)
9482 tree statement, std_attrs = NULL_TREE;
9483 cp_token *token;
9484 location_t statement_location, attrs_location;
9486 restart:
9487 if (if_p != NULL)
9488 *if_p = false;
9489 /* There is no statement yet. */
9490 statement = NULL_TREE;
9492 cp_lexer_save_tokens (parser->lexer);
9493 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9494 if (c_dialect_objc ())
9495 /* In obj-c++, seeing '[[' might be the either the beginning of
9496 c++11 attributes, or a nested objc-message-expression. So
9497 let's parse the c++11 attributes tentatively. */
9498 cp_parser_parse_tentatively (parser);
9499 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9500 if (c_dialect_objc ())
9502 if (!cp_parser_parse_definitely (parser))
9503 std_attrs = NULL_TREE;
9506 /* Peek at the next token. */
9507 token = cp_lexer_peek_token (parser->lexer);
9508 /* Remember the location of the first token in the statement. */
9509 statement_location = token->location;
9510 /* If this is a keyword, then that will often determine what kind of
9511 statement we have. */
9512 if (token->type == CPP_KEYWORD)
9514 enum rid keyword = token->keyword;
9516 switch (keyword)
9518 case RID_CASE:
9519 case RID_DEFAULT:
9520 /* Looks like a labeled-statement with a case label.
9521 Parse the label, and then use tail recursion to parse
9522 the statement. */
9523 cp_parser_label_for_labeled_statement (parser, std_attrs);
9524 goto restart;
9526 case RID_IF:
9527 case RID_SWITCH:
9528 statement = cp_parser_selection_statement (parser, if_p);
9529 break;
9531 case RID_WHILE:
9532 case RID_DO:
9533 case RID_FOR:
9534 statement = cp_parser_iteration_statement (parser, false);
9535 break;
9537 case RID_CILK_FOR:
9538 if (!flag_cilkplus)
9540 error_at (cp_lexer_peek_token (parser->lexer)->location,
9541 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9542 cp_lexer_consume_token (parser->lexer);
9543 statement = error_mark_node;
9545 else
9546 statement = cp_parser_cilk_for (parser, integer_zero_node);
9547 break;
9549 case RID_BREAK:
9550 case RID_CONTINUE:
9551 case RID_RETURN:
9552 case RID_GOTO:
9553 statement = cp_parser_jump_statement (parser);
9554 break;
9556 case RID_CILK_SYNC:
9557 cp_lexer_consume_token (parser->lexer);
9558 if (flag_cilkplus)
9560 tree sync_expr = build_cilk_sync ();
9561 SET_EXPR_LOCATION (sync_expr,
9562 token->location);
9563 statement = finish_expr_stmt (sync_expr);
9565 else
9567 error_at (token->location, "-fcilkplus must be enabled to use"
9568 " %<_Cilk_sync%>");
9569 statement = error_mark_node;
9571 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9572 break;
9574 /* Objective-C++ exception-handling constructs. */
9575 case RID_AT_TRY:
9576 case RID_AT_CATCH:
9577 case RID_AT_FINALLY:
9578 case RID_AT_SYNCHRONIZED:
9579 case RID_AT_THROW:
9580 statement = cp_parser_objc_statement (parser);
9581 break;
9583 case RID_TRY:
9584 statement = cp_parser_try_block (parser);
9585 break;
9587 case RID_NAMESPACE:
9588 /* This must be a namespace alias definition. */
9589 cp_parser_declaration_statement (parser);
9590 return;
9592 case RID_TRANSACTION_ATOMIC:
9593 case RID_TRANSACTION_RELAXED:
9594 statement = cp_parser_transaction (parser, keyword);
9595 break;
9596 case RID_TRANSACTION_CANCEL:
9597 statement = cp_parser_transaction_cancel (parser);
9598 break;
9600 default:
9601 /* It might be a keyword like `int' that can start a
9602 declaration-statement. */
9603 break;
9606 else if (token->type == CPP_NAME)
9608 /* If the next token is a `:', then we are looking at a
9609 labeled-statement. */
9610 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9611 if (token->type == CPP_COLON)
9613 /* Looks like a labeled-statement with an ordinary label.
9614 Parse the label, and then use tail recursion to parse
9615 the statement. */
9617 cp_parser_label_for_labeled_statement (parser, std_attrs);
9618 goto restart;
9621 /* Anything that starts with a `{' must be a compound-statement. */
9622 else if (token->type == CPP_OPEN_BRACE)
9623 statement = cp_parser_compound_statement (parser, NULL, false, false);
9624 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9625 a statement all its own. */
9626 else if (token->type == CPP_PRAGMA)
9628 /* Only certain OpenMP pragmas are attached to statements, and thus
9629 are considered statements themselves. All others are not. In
9630 the context of a compound, accept the pragma as a "statement" and
9631 return so that we can check for a close brace. Otherwise we
9632 require a real statement and must go back and read one. */
9633 if (in_compound)
9634 cp_parser_pragma (parser, pragma_compound);
9635 else if (!cp_parser_pragma (parser, pragma_stmt))
9636 goto restart;
9637 return;
9639 else if (token->type == CPP_EOF)
9641 cp_parser_error (parser, "expected statement");
9642 return;
9645 /* Everything else must be a declaration-statement or an
9646 expression-statement. Try for the declaration-statement
9647 first, unless we are looking at a `;', in which case we know that
9648 we have an expression-statement. */
9649 if (!statement)
9651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9653 if (std_attrs != NULL_TREE)
9655 /* Attributes should be parsed as part of the the
9656 declaration, so let's un-parse them. */
9657 cp_lexer_rollback_tokens (parser->lexer);
9658 std_attrs = NULL_TREE;
9661 cp_parser_parse_tentatively (parser);
9662 /* Try to parse the declaration-statement. */
9663 cp_parser_declaration_statement (parser);
9664 /* If that worked, we're done. */
9665 if (cp_parser_parse_definitely (parser))
9666 return;
9668 /* Look for an expression-statement instead. */
9669 statement = cp_parser_expression_statement (parser, in_statement_expr);
9672 /* Set the line number for the statement. */
9673 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9674 SET_EXPR_LOCATION (statement, statement_location);
9676 /* Note that for now, we don't do anything with c++11 statements
9677 parsed at this level. */
9678 if (std_attrs != NULL_TREE)
9679 warning_at (attrs_location,
9680 OPT_Wattributes,
9681 "attributes at the beginning of statement are ignored");
9684 /* Parse the label for a labeled-statement, i.e.
9686 identifier :
9687 case constant-expression :
9688 default :
9690 GNU Extension:
9691 case constant-expression ... constant-expression : statement
9693 When a label is parsed without errors, the label is added to the
9694 parse tree by the finish_* functions, so this function doesn't
9695 have to return the label. */
9697 static void
9698 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9700 cp_token *token;
9701 tree label = NULL_TREE;
9702 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9704 /* The next token should be an identifier. */
9705 token = cp_lexer_peek_token (parser->lexer);
9706 if (token->type != CPP_NAME
9707 && token->type != CPP_KEYWORD)
9709 cp_parser_error (parser, "expected labeled-statement");
9710 return;
9713 parser->colon_corrects_to_scope_p = false;
9714 switch (token->keyword)
9716 case RID_CASE:
9718 tree expr, expr_hi;
9719 cp_token *ellipsis;
9721 /* Consume the `case' token. */
9722 cp_lexer_consume_token (parser->lexer);
9723 /* Parse the constant-expression. */
9724 expr = cp_parser_constant_expression (parser,
9725 /*allow_non_constant_p=*/false,
9726 NULL);
9728 ellipsis = cp_lexer_peek_token (parser->lexer);
9729 if (ellipsis->type == CPP_ELLIPSIS)
9731 /* Consume the `...' token. */
9732 cp_lexer_consume_token (parser->lexer);
9733 expr_hi =
9734 cp_parser_constant_expression (parser,
9735 /*allow_non_constant_p=*/false,
9736 NULL);
9737 /* We don't need to emit warnings here, as the common code
9738 will do this for us. */
9740 else
9741 expr_hi = NULL_TREE;
9743 if (parser->in_switch_statement_p)
9744 finish_case_label (token->location, expr, expr_hi);
9745 else
9746 error_at (token->location,
9747 "case label %qE not within a switch statement",
9748 expr);
9750 break;
9752 case RID_DEFAULT:
9753 /* Consume the `default' token. */
9754 cp_lexer_consume_token (parser->lexer);
9756 if (parser->in_switch_statement_p)
9757 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9758 else
9759 error_at (token->location, "case label not within a switch statement");
9760 break;
9762 default:
9763 /* Anything else must be an ordinary label. */
9764 label = finish_label_stmt (cp_parser_identifier (parser));
9765 break;
9768 /* Require the `:' token. */
9769 cp_parser_require (parser, CPP_COLON, RT_COLON);
9771 /* An ordinary label may optionally be followed by attributes.
9772 However, this is only permitted if the attributes are then
9773 followed by a semicolon. This is because, for backward
9774 compatibility, when parsing
9775 lab: __attribute__ ((unused)) int i;
9776 we want the attribute to attach to "i", not "lab". */
9777 if (label != NULL_TREE
9778 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9780 tree attrs;
9781 cp_parser_parse_tentatively (parser);
9782 attrs = cp_parser_gnu_attributes_opt (parser);
9783 if (attrs == NULL_TREE
9784 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9785 cp_parser_abort_tentative_parse (parser);
9786 else if (!cp_parser_parse_definitely (parser))
9788 else
9789 attributes = chainon (attributes, attrs);
9792 if (attributes != NULL_TREE)
9793 cplus_decl_attributes (&label, attributes, 0);
9795 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9798 /* Parse an expression-statement.
9800 expression-statement:
9801 expression [opt] ;
9803 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9804 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9805 indicates whether this expression-statement is part of an
9806 expression statement. */
9808 static tree
9809 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9811 tree statement = NULL_TREE;
9812 cp_token *token = cp_lexer_peek_token (parser->lexer);
9814 /* If the next token is a ';', then there is no expression
9815 statement. */
9816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9818 statement = cp_parser_expression (parser);
9819 if (statement == error_mark_node
9820 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9822 cp_parser_skip_to_end_of_block_or_statement (parser);
9823 return error_mark_node;
9827 /* Give a helpful message for "A<T>::type t;" and the like. */
9828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9829 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9831 if (TREE_CODE (statement) == SCOPE_REF)
9832 error_at (token->location, "need %<typename%> before %qE because "
9833 "%qT is a dependent scope",
9834 statement, TREE_OPERAND (statement, 0));
9835 else if (is_overloaded_fn (statement)
9836 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9838 /* A::A a; */
9839 tree fn = get_first_fn (statement);
9840 error_at (token->location,
9841 "%<%T::%D%> names the constructor, not the type",
9842 DECL_CONTEXT (fn), DECL_NAME (fn));
9846 /* Consume the final `;'. */
9847 cp_parser_consume_semicolon_at_end_of_statement (parser);
9849 if (in_statement_expr
9850 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9851 /* This is the final expression statement of a statement
9852 expression. */
9853 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9854 else if (statement)
9855 statement = finish_expr_stmt (statement);
9857 return statement;
9860 /* Parse a compound-statement.
9862 compound-statement:
9863 { statement-seq [opt] }
9865 GNU extension:
9867 compound-statement:
9868 { label-declaration-seq [opt] statement-seq [opt] }
9870 label-declaration-seq:
9871 label-declaration
9872 label-declaration-seq label-declaration
9874 Returns a tree representing the statement. */
9876 static tree
9877 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9878 bool in_try, bool function_body)
9880 tree compound_stmt;
9882 /* Consume the `{'. */
9883 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9884 return error_mark_node;
9885 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9886 && !function_body)
9887 pedwarn (input_location, OPT_Wpedantic,
9888 "compound-statement in constexpr function");
9889 /* Begin the compound-statement. */
9890 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9891 /* If the next keyword is `__label__' we have a label declaration. */
9892 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9893 cp_parser_label_declaration (parser);
9894 /* Parse an (optional) statement-seq. */
9895 cp_parser_statement_seq_opt (parser, in_statement_expr);
9896 /* Finish the compound-statement. */
9897 finish_compound_stmt (compound_stmt);
9898 /* Consume the `}'. */
9899 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9901 return compound_stmt;
9904 /* Parse an (optional) statement-seq.
9906 statement-seq:
9907 statement
9908 statement-seq [opt] statement */
9910 static void
9911 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9913 /* Scan statements until there aren't any more. */
9914 while (true)
9916 cp_token *token = cp_lexer_peek_token (parser->lexer);
9918 /* If we are looking at a `}', then we have run out of
9919 statements; the same is true if we have reached the end
9920 of file, or have stumbled upon a stray '@end'. */
9921 if (token->type == CPP_CLOSE_BRACE
9922 || token->type == CPP_EOF
9923 || token->type == CPP_PRAGMA_EOL
9924 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9925 break;
9927 /* If we are in a compound statement and find 'else' then
9928 something went wrong. */
9929 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9931 if (parser->in_statement & IN_IF_STMT)
9932 break;
9933 else
9935 token = cp_lexer_consume_token (parser->lexer);
9936 error_at (token->location, "%<else%> without a previous %<if%>");
9940 /* Parse the statement. */
9941 cp_parser_statement (parser, in_statement_expr, true, NULL);
9945 /* Parse a selection-statement.
9947 selection-statement:
9948 if ( condition ) statement
9949 if ( condition ) statement else statement
9950 switch ( condition ) statement
9952 Returns the new IF_STMT or SWITCH_STMT.
9954 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9955 is a (possibly labeled) if statement which is not enclosed in
9956 braces and has an else clause. This is used to implement
9957 -Wparentheses. */
9959 static tree
9960 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9962 cp_token *token;
9963 enum rid keyword;
9965 if (if_p != NULL)
9966 *if_p = false;
9968 /* Peek at the next token. */
9969 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9971 /* See what kind of keyword it is. */
9972 keyword = token->keyword;
9973 switch (keyword)
9975 case RID_IF:
9976 case RID_SWITCH:
9978 tree statement;
9979 tree condition;
9981 /* Look for the `('. */
9982 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9984 cp_parser_skip_to_end_of_statement (parser);
9985 return error_mark_node;
9988 /* Begin the selection-statement. */
9989 if (keyword == RID_IF)
9990 statement = begin_if_stmt ();
9991 else
9992 statement = begin_switch_stmt ();
9994 /* Parse the condition. */
9995 condition = cp_parser_condition (parser);
9996 /* Look for the `)'. */
9997 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9998 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9999 /*consume_paren=*/true);
10001 if (keyword == RID_IF)
10003 bool nested_if;
10004 unsigned char in_statement;
10006 /* Add the condition. */
10007 finish_if_stmt_cond (condition, statement);
10009 /* Parse the then-clause. */
10010 in_statement = parser->in_statement;
10011 parser->in_statement |= IN_IF_STMT;
10012 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10014 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10015 add_stmt (build_empty_stmt (loc));
10016 cp_lexer_consume_token (parser->lexer);
10017 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10018 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10019 "empty body in an %<if%> statement");
10020 nested_if = false;
10022 else
10023 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10024 parser->in_statement = in_statement;
10026 finish_then_clause (statement);
10028 /* If the next token is `else', parse the else-clause. */
10029 if (cp_lexer_next_token_is_keyword (parser->lexer,
10030 RID_ELSE))
10032 /* Consume the `else' keyword. */
10033 cp_lexer_consume_token (parser->lexer);
10034 begin_else_clause (statement);
10035 /* Parse the else-clause. */
10036 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10038 location_t loc;
10039 loc = cp_lexer_peek_token (parser->lexer)->location;
10040 warning_at (loc,
10041 OPT_Wempty_body, "suggest braces around "
10042 "empty body in an %<else%> statement");
10043 add_stmt (build_empty_stmt (loc));
10044 cp_lexer_consume_token (parser->lexer);
10046 else
10047 cp_parser_implicitly_scoped_statement (parser, NULL);
10049 finish_else_clause (statement);
10051 /* If we are currently parsing a then-clause, then
10052 IF_P will not be NULL. We set it to true to
10053 indicate that this if statement has an else clause.
10054 This may trigger the Wparentheses warning below
10055 when we get back up to the parent if statement. */
10056 if (if_p != NULL)
10057 *if_p = true;
10059 else
10061 /* This if statement does not have an else clause. If
10062 NESTED_IF is true, then the then-clause is an if
10063 statement which does have an else clause. We warn
10064 about the potential ambiguity. */
10065 if (nested_if)
10066 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10067 "suggest explicit braces to avoid ambiguous"
10068 " %<else%>");
10071 /* Now we're all done with the if-statement. */
10072 finish_if_stmt (statement);
10074 else
10076 bool in_switch_statement_p;
10077 unsigned char in_statement;
10079 /* Add the condition. */
10080 finish_switch_cond (condition, statement);
10082 /* Parse the body of the switch-statement. */
10083 in_switch_statement_p = parser->in_switch_statement_p;
10084 in_statement = parser->in_statement;
10085 parser->in_switch_statement_p = true;
10086 parser->in_statement |= IN_SWITCH_STMT;
10087 cp_parser_implicitly_scoped_statement (parser, NULL);
10088 parser->in_switch_statement_p = in_switch_statement_p;
10089 parser->in_statement = in_statement;
10091 /* Now we're all done with the switch-statement. */
10092 finish_switch_stmt (statement);
10095 return statement;
10097 break;
10099 default:
10100 cp_parser_error (parser, "expected selection-statement");
10101 return error_mark_node;
10105 /* Parse a condition.
10107 condition:
10108 expression
10109 type-specifier-seq declarator = initializer-clause
10110 type-specifier-seq declarator braced-init-list
10112 GNU Extension:
10114 condition:
10115 type-specifier-seq declarator asm-specification [opt]
10116 attributes [opt] = assignment-expression
10118 Returns the expression that should be tested. */
10120 static tree
10121 cp_parser_condition (cp_parser* parser)
10123 cp_decl_specifier_seq type_specifiers;
10124 const char *saved_message;
10125 int declares_class_or_enum;
10127 /* Try the declaration first. */
10128 cp_parser_parse_tentatively (parser);
10129 /* New types are not allowed in the type-specifier-seq for a
10130 condition. */
10131 saved_message = parser->type_definition_forbidden_message;
10132 parser->type_definition_forbidden_message
10133 = G_("types may not be defined in conditions");
10134 /* Parse the type-specifier-seq. */
10135 cp_parser_decl_specifier_seq (parser,
10136 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10137 &type_specifiers,
10138 &declares_class_or_enum);
10139 /* Restore the saved message. */
10140 parser->type_definition_forbidden_message = saved_message;
10141 /* If all is well, we might be looking at a declaration. */
10142 if (!cp_parser_error_occurred (parser))
10144 tree decl;
10145 tree asm_specification;
10146 tree attributes;
10147 cp_declarator *declarator;
10148 tree initializer = NULL_TREE;
10150 /* Parse the declarator. */
10151 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10152 /*ctor_dtor_or_conv_p=*/NULL,
10153 /*parenthesized_p=*/NULL,
10154 /*member_p=*/false,
10155 /*friend_p=*/false);
10156 /* Parse the attributes. */
10157 attributes = cp_parser_attributes_opt (parser);
10158 /* Parse the asm-specification. */
10159 asm_specification = cp_parser_asm_specification_opt (parser);
10160 /* If the next token is not an `=' or '{', then we might still be
10161 looking at an expression. For example:
10163 if (A(a).x)
10165 looks like a decl-specifier-seq and a declarator -- but then
10166 there is no `=', so this is an expression. */
10167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10168 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10169 cp_parser_simulate_error (parser);
10171 /* If we did see an `=' or '{', then we are looking at a declaration
10172 for sure. */
10173 if (cp_parser_parse_definitely (parser))
10175 tree pushed_scope;
10176 bool non_constant_p;
10177 bool flags = LOOKUP_ONLYCONVERTING;
10179 /* Create the declaration. */
10180 decl = start_decl (declarator, &type_specifiers,
10181 /*initialized_p=*/true,
10182 attributes, /*prefix_attributes=*/NULL_TREE,
10183 &pushed_scope);
10185 /* Parse the initializer. */
10186 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10188 initializer = cp_parser_braced_list (parser, &non_constant_p);
10189 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10190 flags = 0;
10192 else
10194 /* Consume the `='. */
10195 cp_parser_require (parser, CPP_EQ, RT_EQ);
10196 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10198 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10199 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10201 /* Process the initializer. */
10202 cp_finish_decl (decl,
10203 initializer, !non_constant_p,
10204 asm_specification,
10205 flags);
10207 if (pushed_scope)
10208 pop_scope (pushed_scope);
10210 return convert_from_reference (decl);
10213 /* If we didn't even get past the declarator successfully, we are
10214 definitely not looking at a declaration. */
10215 else
10216 cp_parser_abort_tentative_parse (parser);
10218 /* Otherwise, we are looking at an expression. */
10219 return cp_parser_expression (parser);
10222 /* Parses a for-statement or range-for-statement until the closing ')',
10223 not included. */
10225 static tree
10226 cp_parser_for (cp_parser *parser, bool ivdep)
10228 tree init, scope, decl;
10229 bool is_range_for;
10231 /* Begin the for-statement. */
10232 scope = begin_for_scope (&init);
10234 /* Parse the initialization. */
10235 is_range_for = cp_parser_for_init_statement (parser, &decl);
10237 if (is_range_for)
10238 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10239 else
10240 return cp_parser_c_for (parser, scope, init, ivdep);
10243 static tree
10244 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10246 /* Normal for loop */
10247 tree condition = NULL_TREE;
10248 tree expression = NULL_TREE;
10249 tree stmt;
10251 stmt = begin_for_stmt (scope, init);
10252 /* The for-init-statement has already been parsed in
10253 cp_parser_for_init_statement, so no work is needed here. */
10254 finish_for_init_stmt (stmt);
10256 /* If there's a condition, process it. */
10257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10258 condition = cp_parser_condition (parser);
10259 else if (ivdep)
10261 cp_parser_error (parser, "missing loop condition in loop with "
10262 "%<GCC ivdep%> pragma");
10263 condition = error_mark_node;
10265 finish_for_cond (condition, stmt, ivdep);
10266 /* Look for the `;'. */
10267 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10269 /* If there's an expression, process it. */
10270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10271 expression = cp_parser_expression (parser);
10272 finish_for_expr (expression, stmt);
10274 return stmt;
10277 /* Tries to parse a range-based for-statement:
10279 range-based-for:
10280 decl-specifier-seq declarator : expression
10282 The decl-specifier-seq declarator and the `:' are already parsed by
10283 cp_parser_for_init_statement. If processing_template_decl it returns a
10284 newly created RANGE_FOR_STMT; if not, it is converted to a
10285 regular FOR_STMT. */
10287 static tree
10288 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10289 bool ivdep)
10291 tree stmt, range_expr;
10293 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10295 bool expr_non_constant_p;
10296 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10298 else
10299 range_expr = cp_parser_expression (parser);
10301 /* If in template, STMT is converted to a normal for-statement
10302 at instantiation. If not, it is done just ahead. */
10303 if (processing_template_decl)
10305 if (check_for_bare_parameter_packs (range_expr))
10306 range_expr = error_mark_node;
10307 stmt = begin_range_for_stmt (scope, init);
10308 if (ivdep)
10309 RANGE_FOR_IVDEP (stmt) = 1;
10310 finish_range_for_decl (stmt, range_decl, range_expr);
10311 if (!type_dependent_expression_p (range_expr)
10312 /* do_auto_deduction doesn't mess with template init-lists. */
10313 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10314 do_range_for_auto_deduction (range_decl, range_expr);
10316 else
10318 stmt = begin_for_stmt (scope, init);
10319 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10321 return stmt;
10324 /* Subroutine of cp_convert_range_for: given the initializer expression,
10325 builds up the range temporary. */
10327 static tree
10328 build_range_temp (tree range_expr)
10330 tree range_type, range_temp;
10332 /* Find out the type deduced by the declaration
10333 `auto &&__range = range_expr'. */
10334 range_type = cp_build_reference_type (make_auto (), true);
10335 range_type = do_auto_deduction (range_type, range_expr,
10336 type_uses_auto (range_type));
10338 /* Create the __range variable. */
10339 range_temp = build_decl (input_location, VAR_DECL,
10340 get_identifier ("__for_range"), range_type);
10341 TREE_USED (range_temp) = 1;
10342 DECL_ARTIFICIAL (range_temp) = 1;
10344 return range_temp;
10347 /* Used by cp_parser_range_for in template context: we aren't going to
10348 do a full conversion yet, but we still need to resolve auto in the
10349 type of the for-range-declaration if present. This is basically
10350 a shortcut version of cp_convert_range_for. */
10352 static void
10353 do_range_for_auto_deduction (tree decl, tree range_expr)
10355 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10356 if (auto_node)
10358 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10359 range_temp = convert_from_reference (build_range_temp (range_expr));
10360 iter_type = (cp_parser_perform_range_for_lookup
10361 (range_temp, &begin_dummy, &end_dummy));
10362 if (iter_type)
10364 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10365 iter_type);
10366 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10367 tf_warning_or_error);
10368 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10369 iter_decl, auto_node);
10374 /* Converts a range-based for-statement into a normal
10375 for-statement, as per the definition.
10377 for (RANGE_DECL : RANGE_EXPR)
10378 BLOCK
10380 should be equivalent to:
10383 auto &&__range = RANGE_EXPR;
10384 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10385 __begin != __end;
10386 ++__begin)
10388 RANGE_DECL = *__begin;
10389 BLOCK
10393 If RANGE_EXPR is an array:
10394 BEGIN_EXPR = __range
10395 END_EXPR = __range + ARRAY_SIZE(__range)
10396 Else if RANGE_EXPR has a member 'begin' or 'end':
10397 BEGIN_EXPR = __range.begin()
10398 END_EXPR = __range.end()
10399 Else:
10400 BEGIN_EXPR = begin(__range)
10401 END_EXPR = end(__range);
10403 If __range has a member 'begin' but not 'end', or vice versa, we must
10404 still use the second alternative (it will surely fail, however).
10405 When calling begin()/end() in the third alternative we must use
10406 argument dependent lookup, but always considering 'std' as an associated
10407 namespace. */
10409 tree
10410 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10411 bool ivdep)
10413 tree begin, end;
10414 tree iter_type, begin_expr, end_expr;
10415 tree condition, expression;
10417 if (range_decl == error_mark_node || range_expr == error_mark_node)
10418 /* If an error happened previously do nothing or else a lot of
10419 unhelpful errors would be issued. */
10420 begin_expr = end_expr = iter_type = error_mark_node;
10421 else
10423 tree range_temp;
10425 if (TREE_CODE (range_expr) == VAR_DECL
10426 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10427 /* Can't bind a reference to an array of runtime bound. */
10428 range_temp = range_expr;
10429 else
10431 range_temp = build_range_temp (range_expr);
10432 pushdecl (range_temp);
10433 cp_finish_decl (range_temp, range_expr,
10434 /*is_constant_init*/false, NULL_TREE,
10435 LOOKUP_ONLYCONVERTING);
10436 range_temp = convert_from_reference (range_temp);
10438 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10439 &begin_expr, &end_expr);
10442 /* The new for initialization statement. */
10443 begin = build_decl (input_location, VAR_DECL,
10444 get_identifier ("__for_begin"), iter_type);
10445 TREE_USED (begin) = 1;
10446 DECL_ARTIFICIAL (begin) = 1;
10447 pushdecl (begin);
10448 cp_finish_decl (begin, begin_expr,
10449 /*is_constant_init*/false, NULL_TREE,
10450 LOOKUP_ONLYCONVERTING);
10452 end = build_decl (input_location, VAR_DECL,
10453 get_identifier ("__for_end"), iter_type);
10454 TREE_USED (end) = 1;
10455 DECL_ARTIFICIAL (end) = 1;
10456 pushdecl (end);
10457 cp_finish_decl (end, end_expr,
10458 /*is_constant_init*/false, NULL_TREE,
10459 LOOKUP_ONLYCONVERTING);
10461 finish_for_init_stmt (statement);
10463 /* The new for condition. */
10464 condition = build_x_binary_op (input_location, NE_EXPR,
10465 begin, ERROR_MARK,
10466 end, ERROR_MARK,
10467 NULL, tf_warning_or_error);
10468 finish_for_cond (condition, statement, ivdep);
10470 /* The new increment expression. */
10471 expression = finish_unary_op_expr (input_location,
10472 PREINCREMENT_EXPR, begin,
10473 tf_warning_or_error);
10474 finish_for_expr (expression, statement);
10476 /* The declaration is initialized with *__begin inside the loop body. */
10477 cp_finish_decl (range_decl,
10478 build_x_indirect_ref (input_location, begin, RO_NULL,
10479 tf_warning_or_error),
10480 /*is_constant_init*/false, NULL_TREE,
10481 LOOKUP_ONLYCONVERTING);
10483 return statement;
10486 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10487 We need to solve both at the same time because the method used
10488 depends on the existence of members begin or end.
10489 Returns the type deduced for the iterator expression. */
10491 static tree
10492 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10494 if (error_operand_p (range))
10496 *begin = *end = error_mark_node;
10497 return error_mark_node;
10500 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10502 error ("range-based %<for%> expression of type %qT "
10503 "has incomplete type", TREE_TYPE (range));
10504 *begin = *end = error_mark_node;
10505 return error_mark_node;
10507 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10509 /* If RANGE is an array, we will use pointer arithmetic. */
10510 *begin = range;
10511 *end = build_binary_op (input_location, PLUS_EXPR,
10512 range,
10513 array_type_nelts_top (TREE_TYPE (range)),
10515 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10517 else
10519 /* If it is not an array, we must do a bit of magic. */
10520 tree id_begin, id_end;
10521 tree member_begin, member_end;
10523 *begin = *end = error_mark_node;
10525 id_begin = get_identifier ("begin");
10526 id_end = get_identifier ("end");
10527 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10528 /*protect=*/2, /*want_type=*/false,
10529 tf_warning_or_error);
10530 member_end = lookup_member (TREE_TYPE (range), id_end,
10531 /*protect=*/2, /*want_type=*/false,
10532 tf_warning_or_error);
10534 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10536 /* Use the member functions. */
10537 if (member_begin != NULL_TREE)
10538 *begin = cp_parser_range_for_member_function (range, id_begin);
10539 else
10540 error ("range-based %<for%> expression of type %qT has an "
10541 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10543 if (member_end != NULL_TREE)
10544 *end = cp_parser_range_for_member_function (range, id_end);
10545 else
10546 error ("range-based %<for%> expression of type %qT has a "
10547 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10549 else
10551 /* Use global functions with ADL. */
10552 vec<tree, va_gc> *vec;
10553 vec = make_tree_vector ();
10555 vec_safe_push (vec, range);
10557 member_begin = perform_koenig_lookup (id_begin, vec,
10558 tf_warning_or_error);
10559 *begin = finish_call_expr (member_begin, &vec, false, true,
10560 tf_warning_or_error);
10561 member_end = perform_koenig_lookup (id_end, vec,
10562 tf_warning_or_error);
10563 *end = finish_call_expr (member_end, &vec, false, true,
10564 tf_warning_or_error);
10566 release_tree_vector (vec);
10569 /* Last common checks. */
10570 if (*begin == error_mark_node || *end == error_mark_node)
10572 /* If one of the expressions is an error do no more checks. */
10573 *begin = *end = error_mark_node;
10574 return error_mark_node;
10576 else if (type_dependent_expression_p (*begin)
10577 || type_dependent_expression_p (*end))
10578 /* Can happen, when, eg, in a template context, Koenig lookup
10579 can't resolve begin/end (c++/58503). */
10580 return NULL_TREE;
10581 else
10583 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10584 /* The unqualified type of the __begin and __end temporaries should
10585 be the same, as required by the multiple auto declaration. */
10586 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10587 error ("inconsistent begin/end types in range-based %<for%> "
10588 "statement: %qT and %qT",
10589 TREE_TYPE (*begin), TREE_TYPE (*end));
10590 return iter_type;
10595 /* Helper function for cp_parser_perform_range_for_lookup.
10596 Builds a tree for RANGE.IDENTIFIER(). */
10598 static tree
10599 cp_parser_range_for_member_function (tree range, tree identifier)
10601 tree member, res;
10602 vec<tree, va_gc> *vec;
10604 member = finish_class_member_access_expr (range, identifier,
10605 false, tf_warning_or_error);
10606 if (member == error_mark_node)
10607 return error_mark_node;
10609 vec = make_tree_vector ();
10610 res = finish_call_expr (member, &vec,
10611 /*disallow_virtual=*/false,
10612 /*koenig_p=*/false,
10613 tf_warning_or_error);
10614 release_tree_vector (vec);
10615 return res;
10618 /* Parse an iteration-statement.
10620 iteration-statement:
10621 while ( condition ) statement
10622 do statement while ( expression ) ;
10623 for ( for-init-statement condition [opt] ; expression [opt] )
10624 statement
10626 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10628 static tree
10629 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10631 cp_token *token;
10632 enum rid keyword;
10633 tree statement;
10634 unsigned char in_statement;
10636 /* Peek at the next token. */
10637 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10638 if (!token)
10639 return error_mark_node;
10641 /* Remember whether or not we are already within an iteration
10642 statement. */
10643 in_statement = parser->in_statement;
10645 /* See what kind of keyword it is. */
10646 keyword = token->keyword;
10647 switch (keyword)
10649 case RID_WHILE:
10651 tree condition;
10653 /* Begin the while-statement. */
10654 statement = begin_while_stmt ();
10655 /* Look for the `('. */
10656 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10657 /* Parse the condition. */
10658 condition = cp_parser_condition (parser);
10659 finish_while_stmt_cond (condition, statement, ivdep);
10660 /* Look for the `)'. */
10661 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10662 /* Parse the dependent statement. */
10663 parser->in_statement = IN_ITERATION_STMT;
10664 cp_parser_already_scoped_statement (parser);
10665 parser->in_statement = in_statement;
10666 /* We're done with the while-statement. */
10667 finish_while_stmt (statement);
10669 break;
10671 case RID_DO:
10673 tree expression;
10675 /* Begin the do-statement. */
10676 statement = begin_do_stmt ();
10677 /* Parse the body of the do-statement. */
10678 parser->in_statement = IN_ITERATION_STMT;
10679 cp_parser_implicitly_scoped_statement (parser, NULL);
10680 parser->in_statement = in_statement;
10681 finish_do_body (statement);
10682 /* Look for the `while' keyword. */
10683 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10684 /* Look for the `('. */
10685 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10686 /* Parse the expression. */
10687 expression = cp_parser_expression (parser);
10688 /* We're done with the do-statement. */
10689 finish_do_stmt (expression, statement, ivdep);
10690 /* Look for the `)'. */
10691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10692 /* Look for the `;'. */
10693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10695 break;
10697 case RID_FOR:
10699 /* Look for the `('. */
10700 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10702 statement = cp_parser_for (parser, ivdep);
10704 /* Look for the `)'. */
10705 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10707 /* Parse the body of the for-statement. */
10708 parser->in_statement = IN_ITERATION_STMT;
10709 cp_parser_already_scoped_statement (parser);
10710 parser->in_statement = in_statement;
10712 /* We're done with the for-statement. */
10713 finish_for_stmt (statement);
10715 break;
10717 default:
10718 cp_parser_error (parser, "expected iteration-statement");
10719 statement = error_mark_node;
10720 break;
10723 return statement;
10726 /* Parse a for-init-statement or the declarator of a range-based-for.
10727 Returns true if a range-based-for declaration is seen.
10729 for-init-statement:
10730 expression-statement
10731 simple-declaration */
10733 static bool
10734 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10736 /* If the next token is a `;', then we have an empty
10737 expression-statement. Grammatically, this is also a
10738 simple-declaration, but an invalid one, because it does not
10739 declare anything. Therefore, if we did not handle this case
10740 specially, we would issue an error message about an invalid
10741 declaration. */
10742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10744 bool is_range_for = false;
10745 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10748 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10750 /* N3994 -- for (id : init) ... */
10751 if (cxx_dialect < cxx1z)
10752 pedwarn (input_location, 0, "range-based for loop without a "
10753 "type-specifier only available with "
10754 "-std=c++1z or -std=gnu++1z");
10755 tree name = cp_parser_identifier (parser);
10756 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10757 *decl = build_decl (input_location, VAR_DECL, name, type);
10758 pushdecl (*decl);
10759 cp_lexer_consume_token (parser->lexer);
10760 return true;
10763 /* A colon is used in range-based for. */
10764 parser->colon_corrects_to_scope_p = false;
10766 /* We're going to speculatively look for a declaration, falling back
10767 to an expression, if necessary. */
10768 cp_parser_parse_tentatively (parser);
10769 /* Parse the declaration. */
10770 cp_parser_simple_declaration (parser,
10771 /*function_definition_allowed_p=*/false,
10772 decl);
10773 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10774 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10776 /* It is a range-for, consume the ':' */
10777 cp_lexer_consume_token (parser->lexer);
10778 is_range_for = true;
10779 if (cxx_dialect < cxx11)
10781 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10782 "range-based %<for%> loops only available with "
10783 "-std=c++11 or -std=gnu++11");
10784 *decl = error_mark_node;
10787 else
10788 /* The ';' is not consumed yet because we told
10789 cp_parser_simple_declaration not to. */
10790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10792 if (cp_parser_parse_definitely (parser))
10793 return is_range_for;
10794 /* If the tentative parse failed, then we shall need to look for an
10795 expression-statement. */
10797 /* If we are here, it is an expression-statement. */
10798 cp_parser_expression_statement (parser, NULL_TREE);
10799 return false;
10802 /* Parse a jump-statement.
10804 jump-statement:
10805 break ;
10806 continue ;
10807 return expression [opt] ;
10808 return braced-init-list ;
10809 goto identifier ;
10811 GNU extension:
10813 jump-statement:
10814 goto * expression ;
10816 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10818 static tree
10819 cp_parser_jump_statement (cp_parser* parser)
10821 tree statement = error_mark_node;
10822 cp_token *token;
10823 enum rid keyword;
10824 unsigned char in_statement;
10826 /* Peek at the next token. */
10827 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10828 if (!token)
10829 return error_mark_node;
10831 /* See what kind of keyword it is. */
10832 keyword = token->keyword;
10833 switch (keyword)
10835 case RID_BREAK:
10836 in_statement = parser->in_statement & ~IN_IF_STMT;
10837 switch (in_statement)
10839 case 0:
10840 error_at (token->location, "break statement not within loop or switch");
10841 break;
10842 default:
10843 gcc_assert ((in_statement & IN_SWITCH_STMT)
10844 || in_statement == IN_ITERATION_STMT);
10845 statement = finish_break_stmt ();
10846 if (in_statement == IN_ITERATION_STMT)
10847 break_maybe_infinite_loop ();
10848 break;
10849 case IN_OMP_BLOCK:
10850 error_at (token->location, "invalid exit from OpenMP structured block");
10851 break;
10852 case IN_OMP_FOR:
10853 error_at (token->location, "break statement used with OpenMP for loop");
10854 break;
10855 case IN_CILK_SIMD_FOR:
10856 error_at (token->location, "break statement used with Cilk Plus for loop");
10857 break;
10859 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10860 break;
10862 case RID_CONTINUE:
10863 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10865 case 0:
10866 error_at (token->location, "continue statement not within a loop");
10867 break;
10868 case IN_CILK_SIMD_FOR:
10869 error_at (token->location,
10870 "continue statement within %<#pragma simd%> loop body");
10871 /* Fall through. */
10872 case IN_ITERATION_STMT:
10873 case IN_OMP_FOR:
10874 statement = finish_continue_stmt ();
10875 break;
10876 case IN_OMP_BLOCK:
10877 error_at (token->location, "invalid exit from OpenMP structured block");
10878 break;
10879 default:
10880 gcc_unreachable ();
10882 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10883 break;
10885 case RID_RETURN:
10887 tree expr;
10888 bool expr_non_constant_p;
10890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10892 cp_lexer_set_source_position (parser->lexer);
10893 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10894 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10896 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10897 expr = cp_parser_expression (parser);
10898 else
10899 /* If the next token is a `;', then there is no
10900 expression. */
10901 expr = NULL_TREE;
10902 /* Build the return-statement. */
10903 statement = finish_return_stmt (expr);
10904 /* Look for the final `;'. */
10905 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10907 break;
10909 case RID_GOTO:
10910 /* Create the goto-statement. */
10911 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10913 /* Issue a warning about this use of a GNU extension. */
10914 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10915 /* Consume the '*' token. */
10916 cp_lexer_consume_token (parser->lexer);
10917 /* Parse the dependent expression. */
10918 finish_goto_stmt (cp_parser_expression (parser));
10920 else
10921 finish_goto_stmt (cp_parser_identifier (parser));
10922 /* Look for the final `;'. */
10923 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10924 break;
10926 default:
10927 cp_parser_error (parser, "expected jump-statement");
10928 break;
10931 return statement;
10934 /* Parse a declaration-statement.
10936 declaration-statement:
10937 block-declaration */
10939 static void
10940 cp_parser_declaration_statement (cp_parser* parser)
10942 void *p;
10944 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10945 p = obstack_alloc (&declarator_obstack, 0);
10947 /* Parse the block-declaration. */
10948 cp_parser_block_declaration (parser, /*statement_p=*/true);
10950 /* Free any declarators allocated. */
10951 obstack_free (&declarator_obstack, p);
10954 /* Some dependent statements (like `if (cond) statement'), are
10955 implicitly in their own scope. In other words, if the statement is
10956 a single statement (as opposed to a compound-statement), it is
10957 none-the-less treated as if it were enclosed in braces. Any
10958 declarations appearing in the dependent statement are out of scope
10959 after control passes that point. This function parses a statement,
10960 but ensures that is in its own scope, even if it is not a
10961 compound-statement.
10963 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10964 is a (possibly labeled) if statement which is not enclosed in
10965 braces and has an else clause. This is used to implement
10966 -Wparentheses.
10968 Returns the new statement. */
10970 static tree
10971 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10973 tree statement;
10975 if (if_p != NULL)
10976 *if_p = false;
10978 /* Mark if () ; with a special NOP_EXPR. */
10979 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10981 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10982 cp_lexer_consume_token (parser->lexer);
10983 statement = add_stmt (build_empty_stmt (loc));
10985 /* if a compound is opened, we simply parse the statement directly. */
10986 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10987 statement = cp_parser_compound_statement (parser, NULL, false, false);
10988 /* If the token is not a `{', then we must take special action. */
10989 else
10991 /* Create a compound-statement. */
10992 statement = begin_compound_stmt (0);
10993 /* Parse the dependent-statement. */
10994 cp_parser_statement (parser, NULL_TREE, false, if_p);
10995 /* Finish the dummy compound-statement. */
10996 finish_compound_stmt (statement);
10999 /* Return the statement. */
11000 return statement;
11003 /* For some dependent statements (like `while (cond) statement'), we
11004 have already created a scope. Therefore, even if the dependent
11005 statement is a compound-statement, we do not want to create another
11006 scope. */
11008 static void
11009 cp_parser_already_scoped_statement (cp_parser* parser)
11011 /* If the token is a `{', then we must take special action. */
11012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11013 cp_parser_statement (parser, NULL_TREE, false, NULL);
11014 else
11016 /* Avoid calling cp_parser_compound_statement, so that we
11017 don't create a new scope. Do everything else by hand. */
11018 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11019 /* If the next keyword is `__label__' we have a label declaration. */
11020 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11021 cp_parser_label_declaration (parser);
11022 /* Parse an (optional) statement-seq. */
11023 cp_parser_statement_seq_opt (parser, NULL_TREE);
11024 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11028 /* Declarations [gram.dcl.dcl] */
11030 /* Parse an optional declaration-sequence.
11032 declaration-seq:
11033 declaration
11034 declaration-seq declaration */
11036 static void
11037 cp_parser_declaration_seq_opt (cp_parser* parser)
11039 while (true)
11041 cp_token *token;
11043 token = cp_lexer_peek_token (parser->lexer);
11045 if (token->type == CPP_CLOSE_BRACE
11046 || token->type == CPP_EOF
11047 || token->type == CPP_PRAGMA_EOL)
11048 break;
11050 if (token->type == CPP_SEMICOLON)
11052 /* A declaration consisting of a single semicolon is
11053 invalid. Allow it unless we're being pedantic. */
11054 cp_lexer_consume_token (parser->lexer);
11055 if (!in_system_header_at (input_location))
11056 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11057 continue;
11060 /* If we're entering or exiting a region that's implicitly
11061 extern "C", modify the lang context appropriately. */
11062 if (!parser->implicit_extern_c && token->implicit_extern_c)
11064 push_lang_context (lang_name_c);
11065 parser->implicit_extern_c = true;
11067 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11069 pop_lang_context ();
11070 parser->implicit_extern_c = false;
11073 if (token->type == CPP_PRAGMA)
11075 /* A top-level declaration can consist solely of a #pragma.
11076 A nested declaration cannot, so this is done here and not
11077 in cp_parser_declaration. (A #pragma at block scope is
11078 handled in cp_parser_statement.) */
11079 cp_parser_pragma (parser, pragma_external);
11080 continue;
11083 /* Parse the declaration itself. */
11084 cp_parser_declaration (parser);
11088 /* Parse a declaration.
11090 declaration:
11091 block-declaration
11092 function-definition
11093 template-declaration
11094 explicit-instantiation
11095 explicit-specialization
11096 linkage-specification
11097 namespace-definition
11099 GNU extension:
11101 declaration:
11102 __extension__ declaration */
11104 static void
11105 cp_parser_declaration (cp_parser* parser)
11107 cp_token token1;
11108 cp_token token2;
11109 int saved_pedantic;
11110 void *p;
11111 tree attributes = NULL_TREE;
11113 /* Check for the `__extension__' keyword. */
11114 if (cp_parser_extension_opt (parser, &saved_pedantic))
11116 /* Parse the qualified declaration. */
11117 cp_parser_declaration (parser);
11118 /* Restore the PEDANTIC flag. */
11119 pedantic = saved_pedantic;
11121 return;
11124 /* Try to figure out what kind of declaration is present. */
11125 token1 = *cp_lexer_peek_token (parser->lexer);
11127 if (token1.type != CPP_EOF)
11128 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11129 else
11131 token2.type = CPP_EOF;
11132 token2.keyword = RID_MAX;
11135 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11136 p = obstack_alloc (&declarator_obstack, 0);
11138 /* If the next token is `extern' and the following token is a string
11139 literal, then we have a linkage specification. */
11140 if (token1.keyword == RID_EXTERN
11141 && cp_parser_is_pure_string_literal (&token2))
11142 cp_parser_linkage_specification (parser);
11143 /* If the next token is `template', then we have either a template
11144 declaration, an explicit instantiation, or an explicit
11145 specialization. */
11146 else if (token1.keyword == RID_TEMPLATE)
11148 /* `template <>' indicates a template specialization. */
11149 if (token2.type == CPP_LESS
11150 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11151 cp_parser_explicit_specialization (parser);
11152 /* `template <' indicates a template declaration. */
11153 else if (token2.type == CPP_LESS)
11154 cp_parser_template_declaration (parser, /*member_p=*/false);
11155 /* Anything else must be an explicit instantiation. */
11156 else
11157 cp_parser_explicit_instantiation (parser);
11159 /* If the next token is `export', then we have a template
11160 declaration. */
11161 else if (token1.keyword == RID_EXPORT)
11162 cp_parser_template_declaration (parser, /*member_p=*/false);
11163 /* If the next token is `extern', 'static' or 'inline' and the one
11164 after that is `template', we have a GNU extended explicit
11165 instantiation directive. */
11166 else if (cp_parser_allow_gnu_extensions_p (parser)
11167 && (token1.keyword == RID_EXTERN
11168 || token1.keyword == RID_STATIC
11169 || token1.keyword == RID_INLINE)
11170 && token2.keyword == RID_TEMPLATE)
11171 cp_parser_explicit_instantiation (parser);
11172 /* If the next token is `namespace', check for a named or unnamed
11173 namespace definition. */
11174 else if (token1.keyword == RID_NAMESPACE
11175 && (/* A named namespace definition. */
11176 (token2.type == CPP_NAME
11177 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11178 != CPP_EQ))
11179 /* An unnamed namespace definition. */
11180 || token2.type == CPP_OPEN_BRACE
11181 || token2.keyword == RID_ATTRIBUTE))
11182 cp_parser_namespace_definition (parser);
11183 /* An inline (associated) namespace definition. */
11184 else if (token1.keyword == RID_INLINE
11185 && token2.keyword == RID_NAMESPACE)
11186 cp_parser_namespace_definition (parser);
11187 /* Objective-C++ declaration/definition. */
11188 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11189 cp_parser_objc_declaration (parser, NULL_TREE);
11190 else if (c_dialect_objc ()
11191 && token1.keyword == RID_ATTRIBUTE
11192 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11193 cp_parser_objc_declaration (parser, attributes);
11194 /* We must have either a block declaration or a function
11195 definition. */
11196 else
11197 /* Try to parse a block-declaration, or a function-definition. */
11198 cp_parser_block_declaration (parser, /*statement_p=*/false);
11200 /* Free any declarators allocated. */
11201 obstack_free (&declarator_obstack, p);
11204 /* Parse a block-declaration.
11206 block-declaration:
11207 simple-declaration
11208 asm-definition
11209 namespace-alias-definition
11210 using-declaration
11211 using-directive
11213 GNU Extension:
11215 block-declaration:
11216 __extension__ block-declaration
11218 C++0x Extension:
11220 block-declaration:
11221 static_assert-declaration
11223 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11224 part of a declaration-statement. */
11226 static void
11227 cp_parser_block_declaration (cp_parser *parser,
11228 bool statement_p)
11230 cp_token *token1;
11231 int saved_pedantic;
11233 /* Check for the `__extension__' keyword. */
11234 if (cp_parser_extension_opt (parser, &saved_pedantic))
11236 /* Parse the qualified declaration. */
11237 cp_parser_block_declaration (parser, statement_p);
11238 /* Restore the PEDANTIC flag. */
11239 pedantic = saved_pedantic;
11241 return;
11244 /* Peek at the next token to figure out which kind of declaration is
11245 present. */
11246 token1 = cp_lexer_peek_token (parser->lexer);
11248 /* If the next keyword is `asm', we have an asm-definition. */
11249 if (token1->keyword == RID_ASM)
11251 if (statement_p)
11252 cp_parser_commit_to_tentative_parse (parser);
11253 cp_parser_asm_definition (parser);
11255 /* If the next keyword is `namespace', we have a
11256 namespace-alias-definition. */
11257 else if (token1->keyword == RID_NAMESPACE)
11258 cp_parser_namespace_alias_definition (parser);
11259 /* If the next keyword is `using', we have a
11260 using-declaration, a using-directive, or an alias-declaration. */
11261 else if (token1->keyword == RID_USING)
11263 cp_token *token2;
11265 if (statement_p)
11266 cp_parser_commit_to_tentative_parse (parser);
11267 /* If the token after `using' is `namespace', then we have a
11268 using-directive. */
11269 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11270 if (token2->keyword == RID_NAMESPACE)
11271 cp_parser_using_directive (parser);
11272 /* If the second token after 'using' is '=', then we have an
11273 alias-declaration. */
11274 else if (cxx_dialect >= cxx11
11275 && token2->type == CPP_NAME
11276 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11277 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11278 cp_parser_alias_declaration (parser);
11279 /* Otherwise, it's a using-declaration. */
11280 else
11281 cp_parser_using_declaration (parser,
11282 /*access_declaration_p=*/false);
11284 /* If the next keyword is `__label__' we have a misplaced label
11285 declaration. */
11286 else if (token1->keyword == RID_LABEL)
11288 cp_lexer_consume_token (parser->lexer);
11289 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11290 cp_parser_skip_to_end_of_statement (parser);
11291 /* If the next token is now a `;', consume it. */
11292 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11293 cp_lexer_consume_token (parser->lexer);
11295 /* If the next token is `static_assert' we have a static assertion. */
11296 else if (token1->keyword == RID_STATIC_ASSERT)
11297 cp_parser_static_assert (parser, /*member_p=*/false);
11298 /* Anything else must be a simple-declaration. */
11299 else
11300 cp_parser_simple_declaration (parser, !statement_p,
11301 /*maybe_range_for_decl*/NULL);
11304 /* Parse a simple-declaration.
11306 simple-declaration:
11307 decl-specifier-seq [opt] init-declarator-list [opt] ;
11309 init-declarator-list:
11310 init-declarator
11311 init-declarator-list , init-declarator
11313 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11314 function-definition as a simple-declaration.
11316 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11317 parsed declaration if it is an uninitialized single declarator not followed
11318 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11319 if present, will not be consumed. */
11321 static void
11322 cp_parser_simple_declaration (cp_parser* parser,
11323 bool function_definition_allowed_p,
11324 tree *maybe_range_for_decl)
11326 cp_decl_specifier_seq decl_specifiers;
11327 int declares_class_or_enum;
11328 bool saw_declarator;
11330 if (maybe_range_for_decl)
11331 *maybe_range_for_decl = NULL_TREE;
11333 /* Defer access checks until we know what is being declared; the
11334 checks for names appearing in the decl-specifier-seq should be
11335 done as if we were in the scope of the thing being declared. */
11336 push_deferring_access_checks (dk_deferred);
11338 /* Parse the decl-specifier-seq. We have to keep track of whether
11339 or not the decl-specifier-seq declares a named class or
11340 enumeration type, since that is the only case in which the
11341 init-declarator-list is allowed to be empty.
11343 [dcl.dcl]
11345 In a simple-declaration, the optional init-declarator-list can be
11346 omitted only when declaring a class or enumeration, that is when
11347 the decl-specifier-seq contains either a class-specifier, an
11348 elaborated-type-specifier, or an enum-specifier. */
11349 cp_parser_decl_specifier_seq (parser,
11350 CP_PARSER_FLAGS_OPTIONAL,
11351 &decl_specifiers,
11352 &declares_class_or_enum);
11353 /* We no longer need to defer access checks. */
11354 stop_deferring_access_checks ();
11356 /* In a block scope, a valid declaration must always have a
11357 decl-specifier-seq. By not trying to parse declarators, we can
11358 resolve the declaration/expression ambiguity more quickly. */
11359 if (!function_definition_allowed_p
11360 && !decl_specifiers.any_specifiers_p)
11362 cp_parser_error (parser, "expected declaration");
11363 goto done;
11366 /* If the next two tokens are both identifiers, the code is
11367 erroneous. The usual cause of this situation is code like:
11369 T t;
11371 where "T" should name a type -- but does not. */
11372 if (!decl_specifiers.any_type_specifiers_p
11373 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11375 /* If parsing tentatively, we should commit; we really are
11376 looking at a declaration. */
11377 cp_parser_commit_to_tentative_parse (parser);
11378 /* Give up. */
11379 goto done;
11382 /* If we have seen at least one decl-specifier, and the next token
11383 is not a parenthesis, then we must be looking at a declaration.
11384 (After "int (" we might be looking at a functional cast.) */
11385 if (decl_specifiers.any_specifiers_p
11386 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11387 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11388 && !cp_parser_error_occurred (parser))
11389 cp_parser_commit_to_tentative_parse (parser);
11391 /* Keep going until we hit the `;' at the end of the simple
11392 declaration. */
11393 saw_declarator = false;
11394 while (cp_lexer_next_token_is_not (parser->lexer,
11395 CPP_SEMICOLON))
11397 cp_token *token;
11398 bool function_definition_p;
11399 tree decl;
11401 if (saw_declarator)
11403 /* If we are processing next declarator, coma is expected */
11404 token = cp_lexer_peek_token (parser->lexer);
11405 gcc_assert (token->type == CPP_COMMA);
11406 cp_lexer_consume_token (parser->lexer);
11407 if (maybe_range_for_decl)
11408 *maybe_range_for_decl = error_mark_node;
11410 else
11411 saw_declarator = true;
11413 /* Parse the init-declarator. */
11414 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11415 /*checks=*/NULL,
11416 function_definition_allowed_p,
11417 /*member_p=*/false,
11418 declares_class_or_enum,
11419 &function_definition_p,
11420 maybe_range_for_decl);
11421 /* If an error occurred while parsing tentatively, exit quickly.
11422 (That usually happens when in the body of a function; each
11423 statement is treated as a declaration-statement until proven
11424 otherwise.) */
11425 if (cp_parser_error_occurred (parser))
11426 goto done;
11427 /* Handle function definitions specially. */
11428 if (function_definition_p)
11430 /* If the next token is a `,', then we are probably
11431 processing something like:
11433 void f() {}, *p;
11435 which is erroneous. */
11436 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11438 cp_token *token = cp_lexer_peek_token (parser->lexer);
11439 error_at (token->location,
11440 "mixing"
11441 " declarations and function-definitions is forbidden");
11443 /* Otherwise, we're done with the list of declarators. */
11444 else
11446 pop_deferring_access_checks ();
11447 return;
11450 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11451 *maybe_range_for_decl = decl;
11452 /* The next token should be either a `,' or a `;'. */
11453 token = cp_lexer_peek_token (parser->lexer);
11454 /* If it's a `,', there are more declarators to come. */
11455 if (token->type == CPP_COMMA)
11456 /* will be consumed next time around */;
11457 /* If it's a `;', we are done. */
11458 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11459 break;
11460 /* Anything else is an error. */
11461 else
11463 /* If we have already issued an error message we don't need
11464 to issue another one. */
11465 if (decl != error_mark_node
11466 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11467 cp_parser_error (parser, "expected %<,%> or %<;%>");
11468 /* Skip tokens until we reach the end of the statement. */
11469 cp_parser_skip_to_end_of_statement (parser);
11470 /* If the next token is now a `;', consume it. */
11471 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11472 cp_lexer_consume_token (parser->lexer);
11473 goto done;
11475 /* After the first time around, a function-definition is not
11476 allowed -- even if it was OK at first. For example:
11478 int i, f() {}
11480 is not valid. */
11481 function_definition_allowed_p = false;
11484 /* Issue an error message if no declarators are present, and the
11485 decl-specifier-seq does not itself declare a class or
11486 enumeration: [dcl.dcl]/3. */
11487 if (!saw_declarator)
11489 if (cp_parser_declares_only_class_p (parser))
11491 if (!declares_class_or_enum
11492 && decl_specifiers.type
11493 && OVERLOAD_TYPE_P (decl_specifiers.type))
11494 /* Ensure an error is issued anyway when finish_decltype_type,
11495 called via cp_parser_decl_specifier_seq, returns a class or
11496 an enumeration (c++/51786). */
11497 decl_specifiers.type = NULL_TREE;
11498 shadow_tag (&decl_specifiers);
11500 /* Perform any deferred access checks. */
11501 perform_deferred_access_checks (tf_warning_or_error);
11504 /* Consume the `;'. */
11505 if (!maybe_range_for_decl)
11506 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11508 done:
11509 pop_deferring_access_checks ();
11512 /* Parse a decl-specifier-seq.
11514 decl-specifier-seq:
11515 decl-specifier-seq [opt] decl-specifier
11516 decl-specifier attribute-specifier-seq [opt] (C++11)
11518 decl-specifier:
11519 storage-class-specifier
11520 type-specifier
11521 function-specifier
11522 friend
11523 typedef
11525 GNU Extension:
11527 decl-specifier:
11528 attributes
11530 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11532 The parser flags FLAGS is used to control type-specifier parsing.
11534 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11535 flags:
11537 1: one of the decl-specifiers is an elaborated-type-specifier
11538 (i.e., a type declaration)
11539 2: one of the decl-specifiers is an enum-specifier or a
11540 class-specifier (i.e., a type definition)
11544 static void
11545 cp_parser_decl_specifier_seq (cp_parser* parser,
11546 cp_parser_flags flags,
11547 cp_decl_specifier_seq *decl_specs,
11548 int* declares_class_or_enum)
11550 bool constructor_possible_p = !parser->in_declarator_p;
11551 bool found_decl_spec = false;
11552 cp_token *start_token = NULL;
11553 cp_decl_spec ds;
11555 /* Clear DECL_SPECS. */
11556 clear_decl_specs (decl_specs);
11558 /* Assume no class or enumeration type is declared. */
11559 *declares_class_or_enum = 0;
11561 /* Keep reading specifiers until there are no more to read. */
11562 while (true)
11564 bool constructor_p;
11565 cp_token *token;
11566 ds = ds_last;
11568 /* Peek at the next token. */
11569 token = cp_lexer_peek_token (parser->lexer);
11571 /* Save the first token of the decl spec list for error
11572 reporting. */
11573 if (!start_token)
11574 start_token = token;
11575 /* Handle attributes. */
11576 if (cp_next_tokens_can_be_attribute_p (parser))
11578 /* Parse the attributes. */
11579 tree attrs = cp_parser_attributes_opt (parser);
11581 /* In a sequence of declaration specifiers, c++11 attributes
11582 appertain to the type that precede them. In that case
11583 [dcl.spec]/1 says:
11585 The attribute-specifier-seq affects the type only for
11586 the declaration it appears in, not other declarations
11587 involving the same type.
11589 But for now let's force the user to position the
11590 attribute either at the beginning of the declaration or
11591 after the declarator-id, which would clearly mean that it
11592 applies to the declarator. */
11593 if (cxx11_attribute_p (attrs))
11595 if (!found_decl_spec)
11596 /* The c++11 attribute is at the beginning of the
11597 declaration. It appertains to the entity being
11598 declared. */;
11599 else
11601 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11603 /* This is an attribute following a
11604 class-specifier. */
11605 if (decl_specs->type_definition_p)
11606 warn_misplaced_attr_for_class_type (token->location,
11607 decl_specs->type);
11608 attrs = NULL_TREE;
11610 else
11612 decl_specs->std_attributes
11613 = chainon (decl_specs->std_attributes,
11614 attrs);
11615 if (decl_specs->locations[ds_std_attribute] == 0)
11616 decl_specs->locations[ds_std_attribute] = token->location;
11618 continue;
11622 decl_specs->attributes
11623 = chainon (decl_specs->attributes,
11624 attrs);
11625 if (decl_specs->locations[ds_attribute] == 0)
11626 decl_specs->locations[ds_attribute] = token->location;
11627 continue;
11629 /* Assume we will find a decl-specifier keyword. */
11630 found_decl_spec = true;
11631 /* If the next token is an appropriate keyword, we can simply
11632 add it to the list. */
11633 switch (token->keyword)
11635 /* decl-specifier:
11636 friend
11637 constexpr */
11638 case RID_FRIEND:
11639 if (!at_class_scope_p ())
11641 error_at (token->location, "%<friend%> used outside of class");
11642 cp_lexer_purge_token (parser->lexer);
11644 else
11646 ds = ds_friend;
11647 /* Consume the token. */
11648 cp_lexer_consume_token (parser->lexer);
11650 break;
11652 case RID_CONSTEXPR:
11653 ds = ds_constexpr;
11654 cp_lexer_consume_token (parser->lexer);
11655 break;
11657 /* function-specifier:
11658 inline
11659 virtual
11660 explicit */
11661 case RID_INLINE:
11662 case RID_VIRTUAL:
11663 case RID_EXPLICIT:
11664 cp_parser_function_specifier_opt (parser, decl_specs);
11665 break;
11667 /* decl-specifier:
11668 typedef */
11669 case RID_TYPEDEF:
11670 ds = ds_typedef;
11671 /* Consume the token. */
11672 cp_lexer_consume_token (parser->lexer);
11673 /* A constructor declarator cannot appear in a typedef. */
11674 constructor_possible_p = false;
11675 /* The "typedef" keyword can only occur in a declaration; we
11676 may as well commit at this point. */
11677 cp_parser_commit_to_tentative_parse (parser);
11679 if (decl_specs->storage_class != sc_none)
11680 decl_specs->conflicting_specifiers_p = true;
11681 break;
11683 /* storage-class-specifier:
11684 auto
11685 register
11686 static
11687 extern
11688 mutable
11690 GNU Extension:
11691 thread */
11692 case RID_AUTO:
11693 if (cxx_dialect == cxx98)
11695 /* Consume the token. */
11696 cp_lexer_consume_token (parser->lexer);
11698 /* Complain about `auto' as a storage specifier, if
11699 we're complaining about C++0x compatibility. */
11700 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11701 " changes meaning in C++11; please remove it");
11703 /* Set the storage class anyway. */
11704 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11705 token);
11707 else
11708 /* C++0x auto type-specifier. */
11709 found_decl_spec = false;
11710 break;
11712 case RID_REGISTER:
11713 case RID_STATIC:
11714 case RID_EXTERN:
11715 case RID_MUTABLE:
11716 /* Consume the token. */
11717 cp_lexer_consume_token (parser->lexer);
11718 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11719 token);
11720 break;
11721 case RID_THREAD:
11722 /* Consume the token. */
11723 ds = ds_thread;
11724 cp_lexer_consume_token (parser->lexer);
11725 break;
11727 default:
11728 /* We did not yet find a decl-specifier yet. */
11729 found_decl_spec = false;
11730 break;
11733 if (found_decl_spec
11734 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11735 && token->keyword != RID_CONSTEXPR)
11736 error ("decl-specifier invalid in condition");
11738 if (ds != ds_last)
11739 set_and_check_decl_spec_loc (decl_specs, ds, token);
11741 /* Constructors are a special case. The `S' in `S()' is not a
11742 decl-specifier; it is the beginning of the declarator. */
11743 constructor_p
11744 = (!found_decl_spec
11745 && constructor_possible_p
11746 && (cp_parser_constructor_declarator_p
11747 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11749 /* If we don't have a DECL_SPEC yet, then we must be looking at
11750 a type-specifier. */
11751 if (!found_decl_spec && !constructor_p)
11753 int decl_spec_declares_class_or_enum;
11754 bool is_cv_qualifier;
11755 tree type_spec;
11757 type_spec
11758 = cp_parser_type_specifier (parser, flags,
11759 decl_specs,
11760 /*is_declaration=*/true,
11761 &decl_spec_declares_class_or_enum,
11762 &is_cv_qualifier);
11763 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11765 /* If this type-specifier referenced a user-defined type
11766 (a typedef, class-name, etc.), then we can't allow any
11767 more such type-specifiers henceforth.
11769 [dcl.spec]
11771 The longest sequence of decl-specifiers that could
11772 possibly be a type name is taken as the
11773 decl-specifier-seq of a declaration. The sequence shall
11774 be self-consistent as described below.
11776 [dcl.type]
11778 As a general rule, at most one type-specifier is allowed
11779 in the complete decl-specifier-seq of a declaration. The
11780 only exceptions are the following:
11782 -- const or volatile can be combined with any other
11783 type-specifier.
11785 -- signed or unsigned can be combined with char, long,
11786 short, or int.
11788 -- ..
11790 Example:
11792 typedef char* Pc;
11793 void g (const int Pc);
11795 Here, Pc is *not* part of the decl-specifier seq; it's
11796 the declarator. Therefore, once we see a type-specifier
11797 (other than a cv-qualifier), we forbid any additional
11798 user-defined types. We *do* still allow things like `int
11799 int' to be considered a decl-specifier-seq, and issue the
11800 error message later. */
11801 if (type_spec && !is_cv_qualifier)
11802 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11803 /* A constructor declarator cannot follow a type-specifier. */
11804 if (type_spec)
11806 constructor_possible_p = false;
11807 found_decl_spec = true;
11808 if (!is_cv_qualifier)
11809 decl_specs->any_type_specifiers_p = true;
11813 /* If we still do not have a DECL_SPEC, then there are no more
11814 decl-specifiers. */
11815 if (!found_decl_spec)
11816 break;
11818 decl_specs->any_specifiers_p = true;
11819 /* After we see one decl-specifier, further decl-specifiers are
11820 always optional. */
11821 flags |= CP_PARSER_FLAGS_OPTIONAL;
11824 /* Don't allow a friend specifier with a class definition. */
11825 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11826 && (*declares_class_or_enum & 2))
11827 error_at (decl_specs->locations[ds_friend],
11828 "class definition may not be declared a friend");
11831 /* Parse an (optional) storage-class-specifier.
11833 storage-class-specifier:
11834 auto
11835 register
11836 static
11837 extern
11838 mutable
11840 GNU Extension:
11842 storage-class-specifier:
11843 thread
11845 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11847 static tree
11848 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11850 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11852 case RID_AUTO:
11853 if (cxx_dialect != cxx98)
11854 return NULL_TREE;
11855 /* Fall through for C++98. */
11857 case RID_REGISTER:
11858 case RID_STATIC:
11859 case RID_EXTERN:
11860 case RID_MUTABLE:
11861 case RID_THREAD:
11862 /* Consume the token. */
11863 return cp_lexer_consume_token (parser->lexer)->u.value;
11865 default:
11866 return NULL_TREE;
11870 /* Parse an (optional) function-specifier.
11872 function-specifier:
11873 inline
11874 virtual
11875 explicit
11877 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11878 Updates DECL_SPECS, if it is non-NULL. */
11880 static tree
11881 cp_parser_function_specifier_opt (cp_parser* parser,
11882 cp_decl_specifier_seq *decl_specs)
11884 cp_token *token = cp_lexer_peek_token (parser->lexer);
11885 switch (token->keyword)
11887 case RID_INLINE:
11888 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11889 break;
11891 case RID_VIRTUAL:
11892 /* 14.5.2.3 [temp.mem]
11894 A member function template shall not be virtual. */
11895 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11896 error_at (token->location, "templates may not be %<virtual%>");
11897 else
11898 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11899 break;
11901 case RID_EXPLICIT:
11902 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11903 break;
11905 default:
11906 return NULL_TREE;
11909 /* Consume the token. */
11910 return cp_lexer_consume_token (parser->lexer)->u.value;
11913 /* Parse a linkage-specification.
11915 linkage-specification:
11916 extern string-literal { declaration-seq [opt] }
11917 extern string-literal declaration */
11919 static void
11920 cp_parser_linkage_specification (cp_parser* parser)
11922 tree linkage;
11924 /* Look for the `extern' keyword. */
11925 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11927 /* Look for the string-literal. */
11928 linkage = cp_parser_string_literal (parser, false, false);
11930 /* Transform the literal into an identifier. If the literal is a
11931 wide-character string, or contains embedded NULs, then we can't
11932 handle it as the user wants. */
11933 if (strlen (TREE_STRING_POINTER (linkage))
11934 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11936 cp_parser_error (parser, "invalid linkage-specification");
11937 /* Assume C++ linkage. */
11938 linkage = lang_name_cplusplus;
11940 else
11941 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11943 /* We're now using the new linkage. */
11944 push_lang_context (linkage);
11946 /* If the next token is a `{', then we're using the first
11947 production. */
11948 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11950 cp_ensure_no_omp_declare_simd (parser);
11952 /* Consume the `{' token. */
11953 cp_lexer_consume_token (parser->lexer);
11954 /* Parse the declarations. */
11955 cp_parser_declaration_seq_opt (parser);
11956 /* Look for the closing `}'. */
11957 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11959 /* Otherwise, there's just one declaration. */
11960 else
11962 bool saved_in_unbraced_linkage_specification_p;
11964 saved_in_unbraced_linkage_specification_p
11965 = parser->in_unbraced_linkage_specification_p;
11966 parser->in_unbraced_linkage_specification_p = true;
11967 cp_parser_declaration (parser);
11968 parser->in_unbraced_linkage_specification_p
11969 = saved_in_unbraced_linkage_specification_p;
11972 /* We're done with the linkage-specification. */
11973 pop_lang_context ();
11976 /* Parse a static_assert-declaration.
11978 static_assert-declaration:
11979 static_assert ( constant-expression , string-literal ) ;
11981 If MEMBER_P, this static_assert is a class member. */
11983 static void
11984 cp_parser_static_assert(cp_parser *parser, bool member_p)
11986 tree condition;
11987 tree message;
11988 cp_token *token;
11989 location_t saved_loc;
11990 bool dummy;
11992 /* Peek at the `static_assert' token so we can keep track of exactly
11993 where the static assertion started. */
11994 token = cp_lexer_peek_token (parser->lexer);
11995 saved_loc = token->location;
11997 /* Look for the `static_assert' keyword. */
11998 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11999 RT_STATIC_ASSERT))
12000 return;
12002 /* We know we are in a static assertion; commit to any tentative
12003 parse. */
12004 if (cp_parser_parsing_tentatively (parser))
12005 cp_parser_commit_to_tentative_parse (parser);
12007 /* Parse the `(' starting the static assertion condition. */
12008 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12010 /* Parse the constant-expression. Allow a non-constant expression
12011 here in order to give better diagnostics in finish_static_assert. */
12012 condition =
12013 cp_parser_constant_expression (parser,
12014 /*allow_non_constant_p=*/true,
12015 /*non_constant_p=*/&dummy);
12017 /* Parse the separating `,'. */
12018 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12020 /* Parse the string-literal message. */
12021 message = cp_parser_string_literal (parser,
12022 /*translate=*/false,
12023 /*wide_ok=*/true);
12025 /* A `)' completes the static assertion. */
12026 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12027 cp_parser_skip_to_closing_parenthesis (parser,
12028 /*recovering=*/true,
12029 /*or_comma=*/false,
12030 /*consume_paren=*/true);
12032 /* A semicolon terminates the declaration. */
12033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12035 /* Complete the static assertion, which may mean either processing
12036 the static assert now or saving it for template instantiation. */
12037 finish_static_assert (condition, message, saved_loc, member_p);
12040 /* Parse the expression in decltype ( expression ). */
12042 static tree
12043 cp_parser_decltype_expr (cp_parser *parser,
12044 bool &id_expression_or_member_access_p)
12046 cp_token *id_expr_start_token;
12047 tree expr;
12049 /* First, try parsing an id-expression. */
12050 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12051 cp_parser_parse_tentatively (parser);
12052 expr = cp_parser_id_expression (parser,
12053 /*template_keyword_p=*/false,
12054 /*check_dependency_p=*/true,
12055 /*template_p=*/NULL,
12056 /*declarator_p=*/false,
12057 /*optional_p=*/false);
12059 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12061 bool non_integral_constant_expression_p = false;
12062 tree id_expression = expr;
12063 cp_id_kind idk;
12064 const char *error_msg;
12066 if (identifier_p (expr))
12067 /* Lookup the name we got back from the id-expression. */
12068 expr = cp_parser_lookup_name_simple (parser, expr,
12069 id_expr_start_token->location);
12071 if (expr
12072 && expr != error_mark_node
12073 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12074 && TREE_CODE (expr) != TYPE_DECL
12075 && (TREE_CODE (expr) != BIT_NOT_EXPR
12076 || !TYPE_P (TREE_OPERAND (expr, 0)))
12077 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12079 /* Complete lookup of the id-expression. */
12080 expr = (finish_id_expression
12081 (id_expression, expr, parser->scope, &idk,
12082 /*integral_constant_expression_p=*/false,
12083 /*allow_non_integral_constant_expression_p=*/true,
12084 &non_integral_constant_expression_p,
12085 /*template_p=*/false,
12086 /*done=*/true,
12087 /*address_p=*/false,
12088 /*template_arg_p=*/false,
12089 &error_msg,
12090 id_expr_start_token->location));
12092 if (expr == error_mark_node)
12093 /* We found an id-expression, but it was something that we
12094 should not have found. This is an error, not something
12095 we can recover from, so note that we found an
12096 id-expression and we'll recover as gracefully as
12097 possible. */
12098 id_expression_or_member_access_p = true;
12101 if (expr
12102 && expr != error_mark_node
12103 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12104 /* We have an id-expression. */
12105 id_expression_or_member_access_p = true;
12108 if (!id_expression_or_member_access_p)
12110 /* Abort the id-expression parse. */
12111 cp_parser_abort_tentative_parse (parser);
12113 /* Parsing tentatively, again. */
12114 cp_parser_parse_tentatively (parser);
12116 /* Parse a class member access. */
12117 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12118 /*cast_p=*/false, /*decltype*/true,
12119 /*member_access_only_p=*/true, NULL);
12121 if (expr
12122 && expr != error_mark_node
12123 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12124 /* We have an id-expression. */
12125 id_expression_or_member_access_p = true;
12128 if (id_expression_or_member_access_p)
12129 /* We have parsed the complete id-expression or member access. */
12130 cp_parser_parse_definitely (parser);
12131 else
12133 /* Abort our attempt to parse an id-expression or member access
12134 expression. */
12135 cp_parser_abort_tentative_parse (parser);
12137 /* Parse a full expression. */
12138 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12139 /*decltype_p=*/true);
12142 return expr;
12145 /* Parse a `decltype' type. Returns the type.
12147 simple-type-specifier:
12148 decltype ( expression )
12149 C++14 proposal:
12150 decltype ( auto ) */
12152 static tree
12153 cp_parser_decltype (cp_parser *parser)
12155 tree expr;
12156 bool id_expression_or_member_access_p = false;
12157 const char *saved_message;
12158 bool saved_integral_constant_expression_p;
12159 bool saved_non_integral_constant_expression_p;
12160 bool saved_greater_than_is_operator_p;
12161 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12163 if (start_token->type == CPP_DECLTYPE)
12165 /* Already parsed. */
12166 cp_lexer_consume_token (parser->lexer);
12167 return start_token->u.value;
12170 /* Look for the `decltype' token. */
12171 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12172 return error_mark_node;
12174 /* Parse the opening `('. */
12175 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12176 return error_mark_node;
12178 /* decltype (auto) */
12179 if (cxx_dialect >= cxx14
12180 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12182 cp_lexer_consume_token (parser->lexer);
12183 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12184 return error_mark_node;
12185 expr = make_decltype_auto ();
12186 AUTO_IS_DECLTYPE (expr) = true;
12187 goto rewrite;
12190 /* Types cannot be defined in a `decltype' expression. Save away the
12191 old message. */
12192 saved_message = parser->type_definition_forbidden_message;
12194 /* And create the new one. */
12195 parser->type_definition_forbidden_message
12196 = G_("types may not be defined in %<decltype%> expressions");
12198 /* The restrictions on constant-expressions do not apply inside
12199 decltype expressions. */
12200 saved_integral_constant_expression_p
12201 = parser->integral_constant_expression_p;
12202 saved_non_integral_constant_expression_p
12203 = parser->non_integral_constant_expression_p;
12204 parser->integral_constant_expression_p = false;
12206 /* Within a parenthesized expression, a `>' token is always
12207 the greater-than operator. */
12208 saved_greater_than_is_operator_p
12209 = parser->greater_than_is_operator_p;
12210 parser->greater_than_is_operator_p = true;
12212 /* Do not actually evaluate the expression. */
12213 ++cp_unevaluated_operand;
12215 /* Do not warn about problems with the expression. */
12216 ++c_inhibit_evaluation_warnings;
12218 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12220 /* Go back to evaluating expressions. */
12221 --cp_unevaluated_operand;
12222 --c_inhibit_evaluation_warnings;
12224 /* The `>' token might be the end of a template-id or
12225 template-parameter-list now. */
12226 parser->greater_than_is_operator_p
12227 = saved_greater_than_is_operator_p;
12229 /* Restore the old message and the integral constant expression
12230 flags. */
12231 parser->type_definition_forbidden_message = saved_message;
12232 parser->integral_constant_expression_p
12233 = saved_integral_constant_expression_p;
12234 parser->non_integral_constant_expression_p
12235 = saved_non_integral_constant_expression_p;
12237 /* Parse to the closing `)'. */
12238 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12240 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12241 /*consume_paren=*/true);
12242 return error_mark_node;
12245 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12246 tf_warning_or_error);
12248 rewrite:
12249 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12250 it again. */
12251 start_token->type = CPP_DECLTYPE;
12252 start_token->u.value = expr;
12253 start_token->keyword = RID_MAX;
12254 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12256 return expr;
12259 /* Special member functions [gram.special] */
12261 /* Parse a conversion-function-id.
12263 conversion-function-id:
12264 operator conversion-type-id
12266 Returns an IDENTIFIER_NODE representing the operator. */
12268 static tree
12269 cp_parser_conversion_function_id (cp_parser* parser)
12271 tree type;
12272 tree saved_scope;
12273 tree saved_qualifying_scope;
12274 tree saved_object_scope;
12275 tree pushed_scope = NULL_TREE;
12277 /* Look for the `operator' token. */
12278 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12279 return error_mark_node;
12280 /* When we parse the conversion-type-id, the current scope will be
12281 reset. However, we need that information in able to look up the
12282 conversion function later, so we save it here. */
12283 saved_scope = parser->scope;
12284 saved_qualifying_scope = parser->qualifying_scope;
12285 saved_object_scope = parser->object_scope;
12286 /* We must enter the scope of the class so that the names of
12287 entities declared within the class are available in the
12288 conversion-type-id. For example, consider:
12290 struct S {
12291 typedef int I;
12292 operator I();
12295 S::operator I() { ... }
12297 In order to see that `I' is a type-name in the definition, we
12298 must be in the scope of `S'. */
12299 if (saved_scope)
12300 pushed_scope = push_scope (saved_scope);
12301 /* Parse the conversion-type-id. */
12302 type = cp_parser_conversion_type_id (parser);
12303 /* Leave the scope of the class, if any. */
12304 if (pushed_scope)
12305 pop_scope (pushed_scope);
12306 /* Restore the saved scope. */
12307 parser->scope = saved_scope;
12308 parser->qualifying_scope = saved_qualifying_scope;
12309 parser->object_scope = saved_object_scope;
12310 /* If the TYPE is invalid, indicate failure. */
12311 if (type == error_mark_node)
12312 return error_mark_node;
12313 return mangle_conv_op_name_for_type (type);
12316 /* Parse a conversion-type-id:
12318 conversion-type-id:
12319 type-specifier-seq conversion-declarator [opt]
12321 Returns the TYPE specified. */
12323 static tree
12324 cp_parser_conversion_type_id (cp_parser* parser)
12326 tree attributes;
12327 cp_decl_specifier_seq type_specifiers;
12328 cp_declarator *declarator;
12329 tree type_specified;
12330 const char *saved_message;
12332 /* Parse the attributes. */
12333 attributes = cp_parser_attributes_opt (parser);
12335 saved_message = parser->type_definition_forbidden_message;
12336 parser->type_definition_forbidden_message
12337 = G_("types may not be defined in a conversion-type-id");
12339 /* Parse the type-specifiers. */
12340 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12341 /*is_trailing_return=*/false,
12342 &type_specifiers);
12344 parser->type_definition_forbidden_message = saved_message;
12346 /* If that didn't work, stop. */
12347 if (type_specifiers.type == error_mark_node)
12348 return error_mark_node;
12349 /* Parse the conversion-declarator. */
12350 declarator = cp_parser_conversion_declarator_opt (parser);
12352 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12353 /*initialized=*/0, &attributes);
12354 if (attributes)
12355 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12357 /* Don't give this error when parsing tentatively. This happens to
12358 work because we always parse this definitively once. */
12359 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12360 && type_uses_auto (type_specified))
12362 if (cxx_dialect < cxx14)
12364 error ("invalid use of %<auto%> in conversion operator");
12365 return error_mark_node;
12367 else if (template_parm_scope_p ())
12368 warning (0, "use of %<auto%> in member template "
12369 "conversion operator can never be deduced");
12372 return type_specified;
12375 /* Parse an (optional) conversion-declarator.
12377 conversion-declarator:
12378 ptr-operator conversion-declarator [opt]
12382 static cp_declarator *
12383 cp_parser_conversion_declarator_opt (cp_parser* parser)
12385 enum tree_code code;
12386 tree class_type, std_attributes = NULL_TREE;
12387 cp_cv_quals cv_quals;
12389 /* We don't know if there's a ptr-operator next, or not. */
12390 cp_parser_parse_tentatively (parser);
12391 /* Try the ptr-operator. */
12392 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12393 &std_attributes);
12394 /* If it worked, look for more conversion-declarators. */
12395 if (cp_parser_parse_definitely (parser))
12397 cp_declarator *declarator;
12399 /* Parse another optional declarator. */
12400 declarator = cp_parser_conversion_declarator_opt (parser);
12402 declarator = cp_parser_make_indirect_declarator
12403 (code, class_type, cv_quals, declarator, std_attributes);
12405 return declarator;
12408 return NULL;
12411 /* Parse an (optional) ctor-initializer.
12413 ctor-initializer:
12414 : mem-initializer-list
12416 Returns TRUE iff the ctor-initializer was actually present. */
12418 static bool
12419 cp_parser_ctor_initializer_opt (cp_parser* parser)
12421 /* If the next token is not a `:', then there is no
12422 ctor-initializer. */
12423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12425 /* Do default initialization of any bases and members. */
12426 if (DECL_CONSTRUCTOR_P (current_function_decl))
12427 finish_mem_initializers (NULL_TREE);
12429 return false;
12432 /* Consume the `:' token. */
12433 cp_lexer_consume_token (parser->lexer);
12434 /* And the mem-initializer-list. */
12435 cp_parser_mem_initializer_list (parser);
12437 return true;
12440 /* Parse a mem-initializer-list.
12442 mem-initializer-list:
12443 mem-initializer ... [opt]
12444 mem-initializer ... [opt] , mem-initializer-list */
12446 static void
12447 cp_parser_mem_initializer_list (cp_parser* parser)
12449 tree mem_initializer_list = NULL_TREE;
12450 tree target_ctor = error_mark_node;
12451 cp_token *token = cp_lexer_peek_token (parser->lexer);
12453 /* Let the semantic analysis code know that we are starting the
12454 mem-initializer-list. */
12455 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12456 error_at (token->location,
12457 "only constructors take member initializers");
12459 /* Loop through the list. */
12460 while (true)
12462 tree mem_initializer;
12464 token = cp_lexer_peek_token (parser->lexer);
12465 /* Parse the mem-initializer. */
12466 mem_initializer = cp_parser_mem_initializer (parser);
12467 /* If the next token is a `...', we're expanding member initializers. */
12468 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12470 /* Consume the `...'. */
12471 cp_lexer_consume_token (parser->lexer);
12473 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12474 can be expanded but members cannot. */
12475 if (mem_initializer != error_mark_node
12476 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12478 error_at (token->location,
12479 "cannot expand initializer for member %<%D%>",
12480 TREE_PURPOSE (mem_initializer));
12481 mem_initializer = error_mark_node;
12484 /* Construct the pack expansion type. */
12485 if (mem_initializer != error_mark_node)
12486 mem_initializer = make_pack_expansion (mem_initializer);
12488 if (target_ctor != error_mark_node
12489 && mem_initializer != error_mark_node)
12491 error ("mem-initializer for %qD follows constructor delegation",
12492 TREE_PURPOSE (mem_initializer));
12493 mem_initializer = error_mark_node;
12495 /* Look for a target constructor. */
12496 if (mem_initializer != error_mark_node
12497 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12498 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12500 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12501 if (mem_initializer_list)
12503 error ("constructor delegation follows mem-initializer for %qD",
12504 TREE_PURPOSE (mem_initializer_list));
12505 mem_initializer = error_mark_node;
12507 target_ctor = mem_initializer;
12509 /* Add it to the list, unless it was erroneous. */
12510 if (mem_initializer != error_mark_node)
12512 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12513 mem_initializer_list = mem_initializer;
12515 /* If the next token is not a `,', we're done. */
12516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12517 break;
12518 /* Consume the `,' token. */
12519 cp_lexer_consume_token (parser->lexer);
12522 /* Perform semantic analysis. */
12523 if (DECL_CONSTRUCTOR_P (current_function_decl))
12524 finish_mem_initializers (mem_initializer_list);
12527 /* Parse a mem-initializer.
12529 mem-initializer:
12530 mem-initializer-id ( expression-list [opt] )
12531 mem-initializer-id braced-init-list
12533 GNU extension:
12535 mem-initializer:
12536 ( expression-list [opt] )
12538 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12539 class) or FIELD_DECL (for a non-static data member) to initialize;
12540 the TREE_VALUE is the expression-list. An empty initialization
12541 list is represented by void_list_node. */
12543 static tree
12544 cp_parser_mem_initializer (cp_parser* parser)
12546 tree mem_initializer_id;
12547 tree expression_list;
12548 tree member;
12549 cp_token *token = cp_lexer_peek_token (parser->lexer);
12551 /* Find out what is being initialized. */
12552 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12554 permerror (token->location,
12555 "anachronistic old-style base class initializer");
12556 mem_initializer_id = NULL_TREE;
12558 else
12560 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12561 if (mem_initializer_id == error_mark_node)
12562 return mem_initializer_id;
12564 member = expand_member_init (mem_initializer_id);
12565 if (member && !DECL_P (member))
12566 in_base_initializer = 1;
12568 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12570 bool expr_non_constant_p;
12571 cp_lexer_set_source_position (parser->lexer);
12572 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12573 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12574 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12575 expression_list = build_tree_list (NULL_TREE, expression_list);
12577 else
12579 vec<tree, va_gc> *vec;
12580 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12581 /*cast_p=*/false,
12582 /*allow_expansion_p=*/true,
12583 /*non_constant_p=*/NULL);
12584 if (vec == NULL)
12585 return error_mark_node;
12586 expression_list = build_tree_list_vec (vec);
12587 release_tree_vector (vec);
12590 if (expression_list == error_mark_node)
12591 return error_mark_node;
12592 if (!expression_list)
12593 expression_list = void_type_node;
12595 in_base_initializer = 0;
12597 return member ? build_tree_list (member, expression_list) : error_mark_node;
12600 /* Parse a mem-initializer-id.
12602 mem-initializer-id:
12603 :: [opt] nested-name-specifier [opt] class-name
12604 identifier
12606 Returns a TYPE indicating the class to be initializer for the first
12607 production. Returns an IDENTIFIER_NODE indicating the data member
12608 to be initialized for the second production. */
12610 static tree
12611 cp_parser_mem_initializer_id (cp_parser* parser)
12613 bool global_scope_p;
12614 bool nested_name_specifier_p;
12615 bool template_p = false;
12616 tree id;
12618 cp_token *token = cp_lexer_peek_token (parser->lexer);
12620 /* `typename' is not allowed in this context ([temp.res]). */
12621 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12623 error_at (token->location,
12624 "keyword %<typename%> not allowed in this context (a qualified "
12625 "member initializer is implicitly a type)");
12626 cp_lexer_consume_token (parser->lexer);
12628 /* Look for the optional `::' operator. */
12629 global_scope_p
12630 = (cp_parser_global_scope_opt (parser,
12631 /*current_scope_valid_p=*/false)
12632 != NULL_TREE);
12633 /* Look for the optional nested-name-specifier. The simplest way to
12634 implement:
12636 [temp.res]
12638 The keyword `typename' is not permitted in a base-specifier or
12639 mem-initializer; in these contexts a qualified name that
12640 depends on a template-parameter is implicitly assumed to be a
12641 type name.
12643 is to assume that we have seen the `typename' keyword at this
12644 point. */
12645 nested_name_specifier_p
12646 = (cp_parser_nested_name_specifier_opt (parser,
12647 /*typename_keyword_p=*/true,
12648 /*check_dependency_p=*/true,
12649 /*type_p=*/true,
12650 /*is_declaration=*/true)
12651 != NULL_TREE);
12652 if (nested_name_specifier_p)
12653 template_p = cp_parser_optional_template_keyword (parser);
12654 /* If there is a `::' operator or a nested-name-specifier, then we
12655 are definitely looking for a class-name. */
12656 if (global_scope_p || nested_name_specifier_p)
12657 return cp_parser_class_name (parser,
12658 /*typename_keyword_p=*/true,
12659 /*template_keyword_p=*/template_p,
12660 typename_type,
12661 /*check_dependency_p=*/true,
12662 /*class_head_p=*/false,
12663 /*is_declaration=*/true);
12664 /* Otherwise, we could also be looking for an ordinary identifier. */
12665 cp_parser_parse_tentatively (parser);
12666 /* Try a class-name. */
12667 id = cp_parser_class_name (parser,
12668 /*typename_keyword_p=*/true,
12669 /*template_keyword_p=*/false,
12670 none_type,
12671 /*check_dependency_p=*/true,
12672 /*class_head_p=*/false,
12673 /*is_declaration=*/true);
12674 /* If we found one, we're done. */
12675 if (cp_parser_parse_definitely (parser))
12676 return id;
12677 /* Otherwise, look for an ordinary identifier. */
12678 return cp_parser_identifier (parser);
12681 /* Overloading [gram.over] */
12683 /* Parse an operator-function-id.
12685 operator-function-id:
12686 operator operator
12688 Returns an IDENTIFIER_NODE for the operator which is a
12689 human-readable spelling of the identifier, e.g., `operator +'. */
12691 static tree
12692 cp_parser_operator_function_id (cp_parser* parser)
12694 /* Look for the `operator' keyword. */
12695 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12696 return error_mark_node;
12697 /* And then the name of the operator itself. */
12698 return cp_parser_operator (parser);
12701 /* Return an identifier node for a user-defined literal operator.
12702 The suffix identifier is chained to the operator name identifier. */
12704 static tree
12705 cp_literal_operator_id (const char* name)
12707 tree identifier;
12708 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12709 + strlen (name) + 10);
12710 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12711 identifier = get_identifier (buffer);
12713 return identifier;
12716 /* Parse an operator.
12718 operator:
12719 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12720 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12721 || ++ -- , ->* -> () []
12723 GNU Extensions:
12725 operator:
12726 <? >? <?= >?=
12728 Returns an IDENTIFIER_NODE for the operator which is a
12729 human-readable spelling of the identifier, e.g., `operator +'. */
12731 static tree
12732 cp_parser_operator (cp_parser* parser)
12734 tree id = NULL_TREE;
12735 cp_token *token;
12736 bool utf8 = false;
12738 /* Peek at the next token. */
12739 token = cp_lexer_peek_token (parser->lexer);
12740 /* Figure out which operator we have. */
12741 switch (token->type)
12743 case CPP_KEYWORD:
12745 enum tree_code op;
12747 /* The keyword should be either `new' or `delete'. */
12748 if (token->keyword == RID_NEW)
12749 op = NEW_EXPR;
12750 else if (token->keyword == RID_DELETE)
12751 op = DELETE_EXPR;
12752 else
12753 break;
12755 /* Consume the `new' or `delete' token. */
12756 cp_lexer_consume_token (parser->lexer);
12758 /* Peek at the next token. */
12759 token = cp_lexer_peek_token (parser->lexer);
12760 /* If it's a `[' token then this is the array variant of the
12761 operator. */
12762 if (token->type == CPP_OPEN_SQUARE)
12764 /* Consume the `[' token. */
12765 cp_lexer_consume_token (parser->lexer);
12766 /* Look for the `]' token. */
12767 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12768 id = ansi_opname (op == NEW_EXPR
12769 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12771 /* Otherwise, we have the non-array variant. */
12772 else
12773 id = ansi_opname (op);
12775 return id;
12778 case CPP_PLUS:
12779 id = ansi_opname (PLUS_EXPR);
12780 break;
12782 case CPP_MINUS:
12783 id = ansi_opname (MINUS_EXPR);
12784 break;
12786 case CPP_MULT:
12787 id = ansi_opname (MULT_EXPR);
12788 break;
12790 case CPP_DIV:
12791 id = ansi_opname (TRUNC_DIV_EXPR);
12792 break;
12794 case CPP_MOD:
12795 id = ansi_opname (TRUNC_MOD_EXPR);
12796 break;
12798 case CPP_XOR:
12799 id = ansi_opname (BIT_XOR_EXPR);
12800 break;
12802 case CPP_AND:
12803 id = ansi_opname (BIT_AND_EXPR);
12804 break;
12806 case CPP_OR:
12807 id = ansi_opname (BIT_IOR_EXPR);
12808 break;
12810 case CPP_COMPL:
12811 id = ansi_opname (BIT_NOT_EXPR);
12812 break;
12814 case CPP_NOT:
12815 id = ansi_opname (TRUTH_NOT_EXPR);
12816 break;
12818 case CPP_EQ:
12819 id = ansi_assopname (NOP_EXPR);
12820 break;
12822 case CPP_LESS:
12823 id = ansi_opname (LT_EXPR);
12824 break;
12826 case CPP_GREATER:
12827 id = ansi_opname (GT_EXPR);
12828 break;
12830 case CPP_PLUS_EQ:
12831 id = ansi_assopname (PLUS_EXPR);
12832 break;
12834 case CPP_MINUS_EQ:
12835 id = ansi_assopname (MINUS_EXPR);
12836 break;
12838 case CPP_MULT_EQ:
12839 id = ansi_assopname (MULT_EXPR);
12840 break;
12842 case CPP_DIV_EQ:
12843 id = ansi_assopname (TRUNC_DIV_EXPR);
12844 break;
12846 case CPP_MOD_EQ:
12847 id = ansi_assopname (TRUNC_MOD_EXPR);
12848 break;
12850 case CPP_XOR_EQ:
12851 id = ansi_assopname (BIT_XOR_EXPR);
12852 break;
12854 case CPP_AND_EQ:
12855 id = ansi_assopname (BIT_AND_EXPR);
12856 break;
12858 case CPP_OR_EQ:
12859 id = ansi_assopname (BIT_IOR_EXPR);
12860 break;
12862 case CPP_LSHIFT:
12863 id = ansi_opname (LSHIFT_EXPR);
12864 break;
12866 case CPP_RSHIFT:
12867 id = ansi_opname (RSHIFT_EXPR);
12868 break;
12870 case CPP_LSHIFT_EQ:
12871 id = ansi_assopname (LSHIFT_EXPR);
12872 break;
12874 case CPP_RSHIFT_EQ:
12875 id = ansi_assopname (RSHIFT_EXPR);
12876 break;
12878 case CPP_EQ_EQ:
12879 id = ansi_opname (EQ_EXPR);
12880 break;
12882 case CPP_NOT_EQ:
12883 id = ansi_opname (NE_EXPR);
12884 break;
12886 case CPP_LESS_EQ:
12887 id = ansi_opname (LE_EXPR);
12888 break;
12890 case CPP_GREATER_EQ:
12891 id = ansi_opname (GE_EXPR);
12892 break;
12894 case CPP_AND_AND:
12895 id = ansi_opname (TRUTH_ANDIF_EXPR);
12896 break;
12898 case CPP_OR_OR:
12899 id = ansi_opname (TRUTH_ORIF_EXPR);
12900 break;
12902 case CPP_PLUS_PLUS:
12903 id = ansi_opname (POSTINCREMENT_EXPR);
12904 break;
12906 case CPP_MINUS_MINUS:
12907 id = ansi_opname (PREDECREMENT_EXPR);
12908 break;
12910 case CPP_COMMA:
12911 id = ansi_opname (COMPOUND_EXPR);
12912 break;
12914 case CPP_DEREF_STAR:
12915 id = ansi_opname (MEMBER_REF);
12916 break;
12918 case CPP_DEREF:
12919 id = ansi_opname (COMPONENT_REF);
12920 break;
12922 case CPP_OPEN_PAREN:
12923 /* Consume the `('. */
12924 cp_lexer_consume_token (parser->lexer);
12925 /* Look for the matching `)'. */
12926 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12927 return ansi_opname (CALL_EXPR);
12929 case CPP_OPEN_SQUARE:
12930 /* Consume the `['. */
12931 cp_lexer_consume_token (parser->lexer);
12932 /* Look for the matching `]'. */
12933 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12934 return ansi_opname (ARRAY_REF);
12936 case CPP_UTF8STRING:
12937 case CPP_UTF8STRING_USERDEF:
12938 utf8 = true;
12939 case CPP_STRING:
12940 case CPP_WSTRING:
12941 case CPP_STRING16:
12942 case CPP_STRING32:
12943 case CPP_STRING_USERDEF:
12944 case CPP_WSTRING_USERDEF:
12945 case CPP_STRING16_USERDEF:
12946 case CPP_STRING32_USERDEF:
12948 tree str, string_tree;
12949 int sz, len;
12951 if (cxx_dialect == cxx98)
12952 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12954 /* Consume the string. */
12955 str = cp_parser_string_literal (parser, /*translate=*/true,
12956 /*wide_ok=*/true, /*lookup_udlit=*/false);
12957 if (str == error_mark_node)
12958 return error_mark_node;
12959 else if (TREE_CODE (str) == USERDEF_LITERAL)
12961 string_tree = USERDEF_LITERAL_VALUE (str);
12962 id = USERDEF_LITERAL_SUFFIX_ID (str);
12964 else
12966 string_tree = str;
12967 /* Look for the suffix identifier. */
12968 token = cp_lexer_peek_token (parser->lexer);
12969 if (token->type == CPP_NAME)
12970 id = cp_parser_identifier (parser);
12971 else if (token->type == CPP_KEYWORD)
12973 error ("unexpected keyword;"
12974 " remove space between quotes and suffix identifier");
12975 return error_mark_node;
12977 else
12979 error ("expected suffix identifier");
12980 return error_mark_node;
12983 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
12984 (TREE_TYPE (TREE_TYPE (string_tree))));
12985 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
12986 if (len != 0)
12988 error ("expected empty string after %<operator%> keyword");
12989 return error_mark_node;
12991 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
12992 != char_type_node)
12994 error ("invalid encoding prefix in literal operator");
12995 return error_mark_node;
12997 if (id != error_mark_node)
12999 const char *name = IDENTIFIER_POINTER (id);
13000 id = cp_literal_operator_id (name);
13002 return id;
13005 default:
13006 /* Anything else is an error. */
13007 break;
13010 /* If we have selected an identifier, we need to consume the
13011 operator token. */
13012 if (id)
13013 cp_lexer_consume_token (parser->lexer);
13014 /* Otherwise, no valid operator name was present. */
13015 else
13017 cp_parser_error (parser, "expected operator");
13018 id = error_mark_node;
13021 return id;
13024 /* Parse a template-declaration.
13026 template-declaration:
13027 export [opt] template < template-parameter-list > declaration
13029 If MEMBER_P is TRUE, this template-declaration occurs within a
13030 class-specifier.
13032 The grammar rule given by the standard isn't correct. What
13033 is really meant is:
13035 template-declaration:
13036 export [opt] template-parameter-list-seq
13037 decl-specifier-seq [opt] init-declarator [opt] ;
13038 export [opt] template-parameter-list-seq
13039 function-definition
13041 template-parameter-list-seq:
13042 template-parameter-list-seq [opt]
13043 template < template-parameter-list > */
13045 static void
13046 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13048 /* Check for `export'. */
13049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13051 /* Consume the `export' token. */
13052 cp_lexer_consume_token (parser->lexer);
13053 /* Warn that we do not support `export'. */
13054 warning (0, "keyword %<export%> not implemented, and will be ignored");
13057 cp_parser_template_declaration_after_export (parser, member_p);
13060 /* Parse a template-parameter-list.
13062 template-parameter-list:
13063 template-parameter
13064 template-parameter-list , template-parameter
13066 Returns a TREE_LIST. Each node represents a template parameter.
13067 The nodes are connected via their TREE_CHAINs. */
13069 static tree
13070 cp_parser_template_parameter_list (cp_parser* parser)
13072 tree parameter_list = NULL_TREE;
13074 begin_template_parm_list ();
13076 /* The loop below parses the template parms. We first need to know
13077 the total number of template parms to be able to compute proper
13078 canonical types of each dependent type. So after the loop, when
13079 we know the total number of template parms,
13080 end_template_parm_list computes the proper canonical types and
13081 fixes up the dependent types accordingly. */
13082 while (true)
13084 tree parameter;
13085 bool is_non_type;
13086 bool is_parameter_pack;
13087 location_t parm_loc;
13089 /* Parse the template-parameter. */
13090 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13091 parameter = cp_parser_template_parameter (parser,
13092 &is_non_type,
13093 &is_parameter_pack);
13094 /* Add it to the list. */
13095 if (parameter != error_mark_node)
13096 parameter_list = process_template_parm (parameter_list,
13097 parm_loc,
13098 parameter,
13099 is_non_type,
13100 is_parameter_pack);
13101 else
13103 tree err_parm = build_tree_list (parameter, parameter);
13104 parameter_list = chainon (parameter_list, err_parm);
13107 /* If the next token is not a `,', we're done. */
13108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13109 break;
13110 /* Otherwise, consume the `,' token. */
13111 cp_lexer_consume_token (parser->lexer);
13114 return end_template_parm_list (parameter_list);
13117 /* Parse a template-parameter.
13119 template-parameter:
13120 type-parameter
13121 parameter-declaration
13123 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13124 the parameter. The TREE_PURPOSE is the default value, if any.
13125 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13126 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13127 set to true iff this parameter is a parameter pack. */
13129 static tree
13130 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13131 bool *is_parameter_pack)
13133 cp_token *token;
13134 cp_parameter_declarator *parameter_declarator;
13135 cp_declarator *id_declarator;
13136 tree parm;
13138 /* Assume it is a type parameter or a template parameter. */
13139 *is_non_type = false;
13140 /* Assume it not a parameter pack. */
13141 *is_parameter_pack = false;
13142 /* Peek at the next token. */
13143 token = cp_lexer_peek_token (parser->lexer);
13144 /* If it is `class' or `template', we have a type-parameter. */
13145 if (token->keyword == RID_TEMPLATE)
13146 return cp_parser_type_parameter (parser, is_parameter_pack);
13147 /* If it is `class' or `typename' we do not know yet whether it is a
13148 type parameter or a non-type parameter. Consider:
13150 template <typename T, typename T::X X> ...
13154 template <class C, class D*> ...
13156 Here, the first parameter is a type parameter, and the second is
13157 a non-type parameter. We can tell by looking at the token after
13158 the identifier -- if it is a `,', `=', or `>' then we have a type
13159 parameter. */
13160 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13162 /* Peek at the token after `class' or `typename'. */
13163 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13164 /* If it's an ellipsis, we have a template type parameter
13165 pack. */
13166 if (token->type == CPP_ELLIPSIS)
13167 return cp_parser_type_parameter (parser, is_parameter_pack);
13168 /* If it's an identifier, skip it. */
13169 if (token->type == CPP_NAME)
13170 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13171 /* Now, see if the token looks like the end of a template
13172 parameter. */
13173 if (token->type == CPP_COMMA
13174 || token->type == CPP_EQ
13175 || token->type == CPP_GREATER)
13176 return cp_parser_type_parameter (parser, is_parameter_pack);
13179 /* Otherwise, it is a non-type parameter.
13181 [temp.param]
13183 When parsing a default template-argument for a non-type
13184 template-parameter, the first non-nested `>' is taken as the end
13185 of the template parameter-list rather than a greater-than
13186 operator. */
13187 *is_non_type = true;
13188 parameter_declarator
13189 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13190 /*parenthesized_p=*/NULL);
13192 if (!parameter_declarator)
13193 return error_mark_node;
13195 /* If the parameter declaration is marked as a parameter pack, set
13196 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13197 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13198 grokdeclarator. */
13199 if (parameter_declarator->declarator
13200 && parameter_declarator->declarator->parameter_pack_p)
13202 *is_parameter_pack = true;
13203 parameter_declarator->declarator->parameter_pack_p = false;
13206 if (parameter_declarator->default_argument)
13208 /* Can happen in some cases of erroneous input (c++/34892). */
13209 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13210 /* Consume the `...' for better error recovery. */
13211 cp_lexer_consume_token (parser->lexer);
13213 /* If the next token is an ellipsis, and we don't already have it
13214 marked as a parameter pack, then we have a parameter pack (that
13215 has no declarator). */
13216 else if (!*is_parameter_pack
13217 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13218 && (declarator_can_be_parameter_pack
13219 (parameter_declarator->declarator)))
13221 /* Consume the `...'. */
13222 cp_lexer_consume_token (parser->lexer);
13223 maybe_warn_variadic_templates ();
13225 *is_parameter_pack = true;
13227 /* We might end up with a pack expansion as the type of the non-type
13228 template parameter, in which case this is a non-type template
13229 parameter pack. */
13230 else if (parameter_declarator->decl_specifiers.type
13231 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13233 *is_parameter_pack = true;
13234 parameter_declarator->decl_specifiers.type =
13235 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13238 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13240 /* Parameter packs cannot have default arguments. However, a
13241 user may try to do so, so we'll parse them and give an
13242 appropriate diagnostic here. */
13244 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13246 /* Find the name of the parameter pack. */
13247 id_declarator = parameter_declarator->declarator;
13248 while (id_declarator && id_declarator->kind != cdk_id)
13249 id_declarator = id_declarator->declarator;
13251 if (id_declarator && id_declarator->kind == cdk_id)
13252 error_at (start_token->location,
13253 "template parameter pack %qD cannot have a default argument",
13254 id_declarator->u.id.unqualified_name);
13255 else
13256 error_at (start_token->location,
13257 "template parameter pack cannot have a default argument");
13259 /* Parse the default argument, but throw away the result. */
13260 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13263 parm = grokdeclarator (parameter_declarator->declarator,
13264 &parameter_declarator->decl_specifiers,
13265 TPARM, /*initialized=*/0,
13266 /*attrlist=*/NULL);
13267 if (parm == error_mark_node)
13268 return error_mark_node;
13270 return build_tree_list (parameter_declarator->default_argument, parm);
13273 /* Parse a type-parameter.
13275 type-parameter:
13276 class identifier [opt]
13277 class identifier [opt] = type-id
13278 typename identifier [opt]
13279 typename identifier [opt] = type-id
13280 template < template-parameter-list > class identifier [opt]
13281 template < template-parameter-list > class identifier [opt]
13282 = id-expression
13284 GNU Extension (variadic templates):
13286 type-parameter:
13287 class ... identifier [opt]
13288 typename ... identifier [opt]
13290 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13291 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13292 the declaration of the parameter.
13294 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13296 static tree
13297 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13299 cp_token *token;
13300 tree parameter;
13302 /* Look for a keyword to tell us what kind of parameter this is. */
13303 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13304 if (!token)
13305 return error_mark_node;
13307 switch (token->keyword)
13309 case RID_CLASS:
13310 case RID_TYPENAME:
13312 tree identifier;
13313 tree default_argument;
13315 /* If the next token is an ellipsis, we have a template
13316 argument pack. */
13317 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13319 /* Consume the `...' token. */
13320 cp_lexer_consume_token (parser->lexer);
13321 maybe_warn_variadic_templates ();
13323 *is_parameter_pack = true;
13326 /* If the next token is an identifier, then it names the
13327 parameter. */
13328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13329 identifier = cp_parser_identifier (parser);
13330 else
13331 identifier = NULL_TREE;
13333 /* Create the parameter. */
13334 parameter = finish_template_type_parm (class_type_node, identifier);
13336 /* If the next token is an `=', we have a default argument. */
13337 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13339 /* Consume the `=' token. */
13340 cp_lexer_consume_token (parser->lexer);
13341 /* Parse the default-argument. */
13342 push_deferring_access_checks (dk_no_deferred);
13343 default_argument = cp_parser_type_id (parser);
13345 /* Template parameter packs cannot have default
13346 arguments. */
13347 if (*is_parameter_pack)
13349 if (identifier)
13350 error_at (token->location,
13351 "template parameter pack %qD cannot have a "
13352 "default argument", identifier);
13353 else
13354 error_at (token->location,
13355 "template parameter packs cannot have "
13356 "default arguments");
13357 default_argument = NULL_TREE;
13359 pop_deferring_access_checks ();
13361 else
13362 default_argument = NULL_TREE;
13364 /* Create the combined representation of the parameter and the
13365 default argument. */
13366 parameter = build_tree_list (default_argument, parameter);
13368 break;
13370 case RID_TEMPLATE:
13372 tree identifier;
13373 tree default_argument;
13375 /* Look for the `<'. */
13376 cp_parser_require (parser, CPP_LESS, RT_LESS);
13377 /* Parse the template-parameter-list. */
13378 cp_parser_template_parameter_list (parser);
13379 /* Look for the `>'. */
13380 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13381 /* Look for the `class' or 'typename' keywords. */
13382 cp_parser_type_parameter_key (parser);
13383 /* If the next token is an ellipsis, we have a template
13384 argument pack. */
13385 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13387 /* Consume the `...' token. */
13388 cp_lexer_consume_token (parser->lexer);
13389 maybe_warn_variadic_templates ();
13391 *is_parameter_pack = true;
13393 /* If the next token is an `=', then there is a
13394 default-argument. If the next token is a `>', we are at
13395 the end of the parameter-list. If the next token is a `,',
13396 then we are at the end of this parameter. */
13397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13398 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13399 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13401 identifier = cp_parser_identifier (parser);
13402 /* Treat invalid names as if the parameter were nameless. */
13403 if (identifier == error_mark_node)
13404 identifier = NULL_TREE;
13406 else
13407 identifier = NULL_TREE;
13409 /* Create the template parameter. */
13410 parameter = finish_template_template_parm (class_type_node,
13411 identifier);
13413 /* If the next token is an `=', then there is a
13414 default-argument. */
13415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13417 bool is_template;
13419 /* Consume the `='. */
13420 cp_lexer_consume_token (parser->lexer);
13421 /* Parse the id-expression. */
13422 push_deferring_access_checks (dk_no_deferred);
13423 /* save token before parsing the id-expression, for error
13424 reporting */
13425 token = cp_lexer_peek_token (parser->lexer);
13426 default_argument
13427 = cp_parser_id_expression (parser,
13428 /*template_keyword_p=*/false,
13429 /*check_dependency_p=*/true,
13430 /*template_p=*/&is_template,
13431 /*declarator_p=*/false,
13432 /*optional_p=*/false);
13433 if (TREE_CODE (default_argument) == TYPE_DECL)
13434 /* If the id-expression was a template-id that refers to
13435 a template-class, we already have the declaration here,
13436 so no further lookup is needed. */
13438 else
13439 /* Look up the name. */
13440 default_argument
13441 = cp_parser_lookup_name (parser, default_argument,
13442 none_type,
13443 /*is_template=*/is_template,
13444 /*is_namespace=*/false,
13445 /*check_dependency=*/true,
13446 /*ambiguous_decls=*/NULL,
13447 token->location);
13448 /* See if the default argument is valid. */
13449 default_argument
13450 = check_template_template_default_arg (default_argument);
13452 /* Template parameter packs cannot have default
13453 arguments. */
13454 if (*is_parameter_pack)
13456 if (identifier)
13457 error_at (token->location,
13458 "template parameter pack %qD cannot "
13459 "have a default argument",
13460 identifier);
13461 else
13462 error_at (token->location, "template parameter packs cannot "
13463 "have default arguments");
13464 default_argument = NULL_TREE;
13466 pop_deferring_access_checks ();
13468 else
13469 default_argument = NULL_TREE;
13471 /* Create the combined representation of the parameter and the
13472 default argument. */
13473 parameter = build_tree_list (default_argument, parameter);
13475 break;
13477 default:
13478 gcc_unreachable ();
13479 break;
13482 return parameter;
13485 /* Parse a template-id.
13487 template-id:
13488 template-name < template-argument-list [opt] >
13490 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13491 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13492 returned. Otherwise, if the template-name names a function, or set
13493 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13494 names a class, returns a TYPE_DECL for the specialization.
13496 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13497 uninstantiated templates. */
13499 static tree
13500 cp_parser_template_id (cp_parser *parser,
13501 bool template_keyword_p,
13502 bool check_dependency_p,
13503 enum tag_types tag_type,
13504 bool is_declaration)
13506 int i;
13507 tree templ;
13508 tree arguments;
13509 tree template_id;
13510 cp_token_position start_of_id = 0;
13511 deferred_access_check *chk;
13512 vec<deferred_access_check, va_gc> *access_check;
13513 cp_token *next_token = NULL, *next_token_2 = NULL;
13514 bool is_identifier;
13516 /* If the next token corresponds to a template-id, there is no need
13517 to reparse it. */
13518 next_token = cp_lexer_peek_token (parser->lexer);
13519 if (next_token->type == CPP_TEMPLATE_ID)
13521 struct tree_check *check_value;
13523 /* Get the stored value. */
13524 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13525 /* Perform any access checks that were deferred. */
13526 access_check = check_value->checks;
13527 if (access_check)
13529 FOR_EACH_VEC_ELT (*access_check, i, chk)
13530 perform_or_defer_access_check (chk->binfo,
13531 chk->decl,
13532 chk->diag_decl,
13533 tf_warning_or_error);
13535 /* Return the stored value. */
13536 return check_value->value;
13539 /* Avoid performing name lookup if there is no possibility of
13540 finding a template-id. */
13541 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13542 || (next_token->type == CPP_NAME
13543 && !cp_parser_nth_token_starts_template_argument_list_p
13544 (parser, 2)))
13546 cp_parser_error (parser, "expected template-id");
13547 return error_mark_node;
13550 /* Remember where the template-id starts. */
13551 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13552 start_of_id = cp_lexer_token_position (parser->lexer, false);
13554 push_deferring_access_checks (dk_deferred);
13556 /* Parse the template-name. */
13557 is_identifier = false;
13558 templ = cp_parser_template_name (parser, template_keyword_p,
13559 check_dependency_p,
13560 is_declaration,
13561 tag_type,
13562 &is_identifier);
13563 if (templ == error_mark_node || is_identifier)
13565 pop_deferring_access_checks ();
13566 return templ;
13569 /* If we find the sequence `[:' after a template-name, it's probably
13570 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13571 parse correctly the argument list. */
13572 next_token = cp_lexer_peek_token (parser->lexer);
13573 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13574 if (next_token->type == CPP_OPEN_SQUARE
13575 && next_token->flags & DIGRAPH
13576 && next_token_2->type == CPP_COLON
13577 && !(next_token_2->flags & PREV_WHITE))
13579 cp_parser_parse_tentatively (parser);
13580 /* Change `:' into `::'. */
13581 next_token_2->type = CPP_SCOPE;
13582 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13583 CPP_LESS. */
13584 cp_lexer_consume_token (parser->lexer);
13586 /* Parse the arguments. */
13587 arguments = cp_parser_enclosed_template_argument_list (parser);
13588 if (!cp_parser_parse_definitely (parser))
13590 /* If we couldn't parse an argument list, then we revert our changes
13591 and return simply an error. Maybe this is not a template-id
13592 after all. */
13593 next_token_2->type = CPP_COLON;
13594 cp_parser_error (parser, "expected %<<%>");
13595 pop_deferring_access_checks ();
13596 return error_mark_node;
13598 /* Otherwise, emit an error about the invalid digraph, but continue
13599 parsing because we got our argument list. */
13600 if (permerror (next_token->location,
13601 "%<<::%> cannot begin a template-argument list"))
13603 static bool hint = false;
13604 inform (next_token->location,
13605 "%<<:%> is an alternate spelling for %<[%>."
13606 " Insert whitespace between %<<%> and %<::%>");
13607 if (!hint && !flag_permissive)
13609 inform (next_token->location, "(if you use %<-fpermissive%> "
13610 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13611 "accept your code)");
13612 hint = true;
13616 else
13618 /* Look for the `<' that starts the template-argument-list. */
13619 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13621 pop_deferring_access_checks ();
13622 return error_mark_node;
13624 /* Parse the arguments. */
13625 arguments = cp_parser_enclosed_template_argument_list (parser);
13628 /* Build a representation of the specialization. */
13629 if (identifier_p (templ))
13630 template_id = build_min_nt_loc (next_token->location,
13631 TEMPLATE_ID_EXPR,
13632 templ, arguments);
13633 else if (DECL_TYPE_TEMPLATE_P (templ)
13634 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13636 bool entering_scope;
13637 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13638 template (rather than some instantiation thereof) only if
13639 is not nested within some other construct. For example, in
13640 "template <typename T> void f(T) { A<T>::", A<T> is just an
13641 instantiation of A. */
13642 entering_scope = (template_parm_scope_p ()
13643 && cp_lexer_next_token_is (parser->lexer,
13644 CPP_SCOPE));
13645 template_id
13646 = finish_template_type (templ, arguments, entering_scope);
13648 else if (variable_template_p (templ))
13650 template_id = lookup_template_variable (templ, arguments);
13652 else
13654 /* If it's not a class-template or a template-template, it should be
13655 a function-template. */
13656 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13657 || TREE_CODE (templ) == OVERLOAD
13658 || BASELINK_P (templ)));
13660 template_id = lookup_template_function (templ, arguments);
13663 /* If parsing tentatively, replace the sequence of tokens that makes
13664 up the template-id with a CPP_TEMPLATE_ID token. That way,
13665 should we re-parse the token stream, we will not have to repeat
13666 the effort required to do the parse, nor will we issue duplicate
13667 error messages about problems during instantiation of the
13668 template. */
13669 if (start_of_id
13670 /* Don't do this if we had a parse error in a declarator; re-parsing
13671 might succeed if a name changes meaning (60361). */
13672 && !(cp_parser_error_occurred (parser)
13673 && cp_parser_parsing_tentatively (parser)
13674 && parser->in_declarator_p))
13676 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13678 /* Reset the contents of the START_OF_ID token. */
13679 token->type = CPP_TEMPLATE_ID;
13680 /* Retrieve any deferred checks. Do not pop this access checks yet
13681 so the memory will not be reclaimed during token replacing below. */
13682 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13683 token->u.tree_check_value->value = template_id;
13684 token->u.tree_check_value->checks = get_deferred_access_checks ();
13685 token->keyword = RID_MAX;
13687 /* Purge all subsequent tokens. */
13688 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13690 /* ??? Can we actually assume that, if template_id ==
13691 error_mark_node, we will have issued a diagnostic to the
13692 user, as opposed to simply marking the tentative parse as
13693 failed? */
13694 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13695 error_at (token->location, "parse error in template argument list");
13698 pop_to_parent_deferring_access_checks ();
13699 return template_id;
13702 /* Parse a template-name.
13704 template-name:
13705 identifier
13707 The standard should actually say:
13709 template-name:
13710 identifier
13711 operator-function-id
13713 A defect report has been filed about this issue.
13715 A conversion-function-id cannot be a template name because they cannot
13716 be part of a template-id. In fact, looking at this code:
13718 a.operator K<int>()
13720 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13721 It is impossible to call a templated conversion-function-id with an
13722 explicit argument list, since the only allowed template parameter is
13723 the type to which it is converting.
13725 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13726 `template' keyword, in a construction like:
13728 T::template f<3>()
13730 In that case `f' is taken to be a template-name, even though there
13731 is no way of knowing for sure.
13733 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13734 name refers to a set of overloaded functions, at least one of which
13735 is a template, or an IDENTIFIER_NODE with the name of the template,
13736 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13737 names are looked up inside uninstantiated templates. */
13739 static tree
13740 cp_parser_template_name (cp_parser* parser,
13741 bool template_keyword_p,
13742 bool check_dependency_p,
13743 bool is_declaration,
13744 enum tag_types tag_type,
13745 bool *is_identifier)
13747 tree identifier;
13748 tree decl;
13749 tree fns;
13750 cp_token *token = cp_lexer_peek_token (parser->lexer);
13752 /* If the next token is `operator', then we have either an
13753 operator-function-id or a conversion-function-id. */
13754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13756 /* We don't know whether we're looking at an
13757 operator-function-id or a conversion-function-id. */
13758 cp_parser_parse_tentatively (parser);
13759 /* Try an operator-function-id. */
13760 identifier = cp_parser_operator_function_id (parser);
13761 /* If that didn't work, try a conversion-function-id. */
13762 if (!cp_parser_parse_definitely (parser))
13764 cp_parser_error (parser, "expected template-name");
13765 return error_mark_node;
13768 /* Look for the identifier. */
13769 else
13770 identifier = cp_parser_identifier (parser);
13772 /* If we didn't find an identifier, we don't have a template-id. */
13773 if (identifier == error_mark_node)
13774 return error_mark_node;
13776 /* If the name immediately followed the `template' keyword, then it
13777 is a template-name. However, if the next token is not `<', then
13778 we do not treat it as a template-name, since it is not being used
13779 as part of a template-id. This enables us to handle constructs
13780 like:
13782 template <typename T> struct S { S(); };
13783 template <typename T> S<T>::S();
13785 correctly. We would treat `S' as a template -- if it were `S<T>'
13786 -- but we do not if there is no `<'. */
13788 if (processing_template_decl
13789 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13791 /* In a declaration, in a dependent context, we pretend that the
13792 "template" keyword was present in order to improve error
13793 recovery. For example, given:
13795 template <typename T> void f(T::X<int>);
13797 we want to treat "X<int>" as a template-id. */
13798 if (is_declaration
13799 && !template_keyword_p
13800 && parser->scope && TYPE_P (parser->scope)
13801 && check_dependency_p
13802 && dependent_scope_p (parser->scope)
13803 /* Do not do this for dtors (or ctors), since they never
13804 need the template keyword before their name. */
13805 && !constructor_name_p (identifier, parser->scope))
13807 cp_token_position start = 0;
13809 /* Explain what went wrong. */
13810 error_at (token->location, "non-template %qD used as template",
13811 identifier);
13812 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13813 parser->scope, identifier);
13814 /* If parsing tentatively, find the location of the "<" token. */
13815 if (cp_parser_simulate_error (parser))
13816 start = cp_lexer_token_position (parser->lexer, true);
13817 /* Parse the template arguments so that we can issue error
13818 messages about them. */
13819 cp_lexer_consume_token (parser->lexer);
13820 cp_parser_enclosed_template_argument_list (parser);
13821 /* Skip tokens until we find a good place from which to
13822 continue parsing. */
13823 cp_parser_skip_to_closing_parenthesis (parser,
13824 /*recovering=*/true,
13825 /*or_comma=*/true,
13826 /*consume_paren=*/false);
13827 /* If parsing tentatively, permanently remove the
13828 template argument list. That will prevent duplicate
13829 error messages from being issued about the missing
13830 "template" keyword. */
13831 if (start)
13832 cp_lexer_purge_tokens_after (parser->lexer, start);
13833 if (is_identifier)
13834 *is_identifier = true;
13835 return identifier;
13838 /* If the "template" keyword is present, then there is generally
13839 no point in doing name-lookup, so we just return IDENTIFIER.
13840 But, if the qualifying scope is non-dependent then we can
13841 (and must) do name-lookup normally. */
13842 if (template_keyword_p
13843 && (!parser->scope
13844 || (TYPE_P (parser->scope)
13845 && dependent_type_p (parser->scope))))
13846 return identifier;
13849 /* Look up the name. */
13850 decl = cp_parser_lookup_name (parser, identifier,
13851 tag_type,
13852 /*is_template=*/true,
13853 /*is_namespace=*/false,
13854 check_dependency_p,
13855 /*ambiguous_decls=*/NULL,
13856 token->location);
13858 /* If DECL is a template, then the name was a template-name. */
13859 if (TREE_CODE (decl) == TEMPLATE_DECL)
13861 else
13863 tree fn = NULL_TREE;
13865 /* The standard does not explicitly indicate whether a name that
13866 names a set of overloaded declarations, some of which are
13867 templates, is a template-name. However, such a name should
13868 be a template-name; otherwise, there is no way to form a
13869 template-id for the overloaded templates. */
13870 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13871 if (TREE_CODE (fns) == OVERLOAD)
13872 for (fn = fns; fn; fn = OVL_NEXT (fn))
13873 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13874 break;
13876 if (!fn)
13878 /* The name does not name a template. */
13879 cp_parser_error (parser, "expected template-name");
13880 return error_mark_node;
13884 /* If DECL is dependent, and refers to a function, then just return
13885 its name; we will look it up again during template instantiation. */
13886 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13888 tree scope = ovl_scope (decl);
13889 if (TYPE_P (scope) && dependent_type_p (scope))
13890 return identifier;
13893 return decl;
13896 /* Parse a template-argument-list.
13898 template-argument-list:
13899 template-argument ... [opt]
13900 template-argument-list , template-argument ... [opt]
13902 Returns a TREE_VEC containing the arguments. */
13904 static tree
13905 cp_parser_template_argument_list (cp_parser* parser)
13907 tree fixed_args[10];
13908 unsigned n_args = 0;
13909 unsigned alloced = 10;
13910 tree *arg_ary = fixed_args;
13911 tree vec;
13912 bool saved_in_template_argument_list_p;
13913 bool saved_ice_p;
13914 bool saved_non_ice_p;
13916 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13917 parser->in_template_argument_list_p = true;
13918 /* Even if the template-id appears in an integral
13919 constant-expression, the contents of the argument list do
13920 not. */
13921 saved_ice_p = parser->integral_constant_expression_p;
13922 parser->integral_constant_expression_p = false;
13923 saved_non_ice_p = parser->non_integral_constant_expression_p;
13924 parser->non_integral_constant_expression_p = false;
13926 /* Parse the arguments. */
13929 tree argument;
13931 if (n_args)
13932 /* Consume the comma. */
13933 cp_lexer_consume_token (parser->lexer);
13935 /* Parse the template-argument. */
13936 argument = cp_parser_template_argument (parser);
13938 /* If the next token is an ellipsis, we're expanding a template
13939 argument pack. */
13940 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13942 if (argument == error_mark_node)
13944 cp_token *token = cp_lexer_peek_token (parser->lexer);
13945 error_at (token->location,
13946 "expected parameter pack before %<...%>");
13948 /* Consume the `...' token. */
13949 cp_lexer_consume_token (parser->lexer);
13951 /* Make the argument into a TYPE_PACK_EXPANSION or
13952 EXPR_PACK_EXPANSION. */
13953 argument = make_pack_expansion (argument);
13956 if (n_args == alloced)
13958 alloced *= 2;
13960 if (arg_ary == fixed_args)
13962 arg_ary = XNEWVEC (tree, alloced);
13963 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13965 else
13966 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13968 arg_ary[n_args++] = argument;
13970 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13972 vec = make_tree_vec (n_args);
13974 while (n_args--)
13975 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13977 if (arg_ary != fixed_args)
13978 free (arg_ary);
13979 parser->non_integral_constant_expression_p = saved_non_ice_p;
13980 parser->integral_constant_expression_p = saved_ice_p;
13981 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13982 #ifdef ENABLE_CHECKING
13983 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13984 #endif
13985 return vec;
13988 /* Parse a template-argument.
13990 template-argument:
13991 assignment-expression
13992 type-id
13993 id-expression
13995 The representation is that of an assignment-expression, type-id, or
13996 id-expression -- except that the qualified id-expression is
13997 evaluated, so that the value returned is either a DECL or an
13998 OVERLOAD.
14000 Although the standard says "assignment-expression", it forbids
14001 throw-expressions or assignments in the template argument.
14002 Therefore, we use "conditional-expression" instead. */
14004 static tree
14005 cp_parser_template_argument (cp_parser* parser)
14007 tree argument;
14008 bool template_p;
14009 bool address_p;
14010 bool maybe_type_id = false;
14011 cp_token *token = NULL, *argument_start_token = NULL;
14012 location_t loc = 0;
14013 cp_id_kind idk;
14015 /* There's really no way to know what we're looking at, so we just
14016 try each alternative in order.
14018 [temp.arg]
14020 In a template-argument, an ambiguity between a type-id and an
14021 expression is resolved to a type-id, regardless of the form of
14022 the corresponding template-parameter.
14024 Therefore, we try a type-id first. */
14025 cp_parser_parse_tentatively (parser);
14026 argument = cp_parser_template_type_arg (parser);
14027 /* If there was no error parsing the type-id but the next token is a
14028 '>>', our behavior depends on which dialect of C++ we're
14029 parsing. In C++98, we probably found a typo for '> >'. But there
14030 are type-id which are also valid expressions. For instance:
14032 struct X { int operator >> (int); };
14033 template <int V> struct Foo {};
14034 Foo<X () >> 5> r;
14036 Here 'X()' is a valid type-id of a function type, but the user just
14037 wanted to write the expression "X() >> 5". Thus, we remember that we
14038 found a valid type-id, but we still try to parse the argument as an
14039 expression to see what happens.
14041 In C++0x, the '>>' will be considered two separate '>'
14042 tokens. */
14043 if (!cp_parser_error_occurred (parser)
14044 && cxx_dialect == cxx98
14045 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14047 maybe_type_id = true;
14048 cp_parser_abort_tentative_parse (parser);
14050 else
14052 /* If the next token isn't a `,' or a `>', then this argument wasn't
14053 really finished. This means that the argument is not a valid
14054 type-id. */
14055 if (!cp_parser_next_token_ends_template_argument_p (parser))
14056 cp_parser_error (parser, "expected template-argument");
14057 /* If that worked, we're done. */
14058 if (cp_parser_parse_definitely (parser))
14059 return argument;
14061 /* We're still not sure what the argument will be. */
14062 cp_parser_parse_tentatively (parser);
14063 /* Try a template. */
14064 argument_start_token = cp_lexer_peek_token (parser->lexer);
14065 argument = cp_parser_id_expression (parser,
14066 /*template_keyword_p=*/false,
14067 /*check_dependency_p=*/true,
14068 &template_p,
14069 /*declarator_p=*/false,
14070 /*optional_p=*/false);
14071 /* If the next token isn't a `,' or a `>', then this argument wasn't
14072 really finished. */
14073 if (!cp_parser_next_token_ends_template_argument_p (parser))
14074 cp_parser_error (parser, "expected template-argument");
14075 if (!cp_parser_error_occurred (parser))
14077 /* Figure out what is being referred to. If the id-expression
14078 was for a class template specialization, then we will have a
14079 TYPE_DECL at this point. There is no need to do name lookup
14080 at this point in that case. */
14081 if (TREE_CODE (argument) != TYPE_DECL)
14082 argument = cp_parser_lookup_name (parser, argument,
14083 none_type,
14084 /*is_template=*/template_p,
14085 /*is_namespace=*/false,
14086 /*check_dependency=*/true,
14087 /*ambiguous_decls=*/NULL,
14088 argument_start_token->location);
14089 if (TREE_CODE (argument) != TEMPLATE_DECL
14090 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14091 cp_parser_error (parser, "expected template-name");
14093 if (cp_parser_parse_definitely (parser))
14094 return argument;
14095 /* It must be a non-type argument. There permitted cases are given
14096 in [temp.arg.nontype]:
14098 -- an integral constant-expression of integral or enumeration
14099 type; or
14101 -- the name of a non-type template-parameter; or
14103 -- the name of an object or function with external linkage...
14105 -- the address of an object or function with external linkage...
14107 -- a pointer to member... */
14108 /* Look for a non-type template parameter. */
14109 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14111 cp_parser_parse_tentatively (parser);
14112 argument = cp_parser_primary_expression (parser,
14113 /*address_p=*/false,
14114 /*cast_p=*/false,
14115 /*template_arg_p=*/true,
14116 &idk);
14117 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14118 || !cp_parser_next_token_ends_template_argument_p (parser))
14119 cp_parser_simulate_error (parser);
14120 if (cp_parser_parse_definitely (parser))
14121 return argument;
14124 /* If the next token is "&", the argument must be the address of an
14125 object or function with external linkage. */
14126 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14127 if (address_p)
14129 loc = cp_lexer_peek_token (parser->lexer)->location;
14130 cp_lexer_consume_token (parser->lexer);
14132 /* See if we might have an id-expression. */
14133 token = cp_lexer_peek_token (parser->lexer);
14134 if (token->type == CPP_NAME
14135 || token->keyword == RID_OPERATOR
14136 || token->type == CPP_SCOPE
14137 || token->type == CPP_TEMPLATE_ID
14138 || token->type == CPP_NESTED_NAME_SPECIFIER)
14140 cp_parser_parse_tentatively (parser);
14141 argument = cp_parser_primary_expression (parser,
14142 address_p,
14143 /*cast_p=*/false,
14144 /*template_arg_p=*/true,
14145 &idk);
14146 if (cp_parser_error_occurred (parser)
14147 || !cp_parser_next_token_ends_template_argument_p (parser))
14148 cp_parser_abort_tentative_parse (parser);
14149 else
14151 tree probe;
14153 if (INDIRECT_REF_P (argument))
14155 /* Strip the dereference temporarily. */
14156 gcc_assert (REFERENCE_REF_P (argument));
14157 argument = TREE_OPERAND (argument, 0);
14160 /* If we're in a template, we represent a qualified-id referring
14161 to a static data member as a SCOPE_REF even if the scope isn't
14162 dependent so that we can check access control later. */
14163 probe = argument;
14164 if (TREE_CODE (probe) == SCOPE_REF)
14165 probe = TREE_OPERAND (probe, 1);
14166 if (VAR_P (probe))
14168 /* A variable without external linkage might still be a
14169 valid constant-expression, so no error is issued here
14170 if the external-linkage check fails. */
14171 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14172 cp_parser_simulate_error (parser);
14174 else if (is_overloaded_fn (argument))
14175 /* All overloaded functions are allowed; if the external
14176 linkage test does not pass, an error will be issued
14177 later. */
14179 else if (address_p
14180 && (TREE_CODE (argument) == OFFSET_REF
14181 || TREE_CODE (argument) == SCOPE_REF))
14182 /* A pointer-to-member. */
14184 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14186 else
14187 cp_parser_simulate_error (parser);
14189 if (cp_parser_parse_definitely (parser))
14191 if (address_p)
14192 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14193 tf_warning_or_error);
14194 else
14195 argument = convert_from_reference (argument);
14196 return argument;
14200 /* If the argument started with "&", there are no other valid
14201 alternatives at this point. */
14202 if (address_p)
14204 cp_parser_error (parser, "invalid non-type template argument");
14205 return error_mark_node;
14208 /* If the argument wasn't successfully parsed as a type-id followed
14209 by '>>', the argument can only be a constant expression now.
14210 Otherwise, we try parsing the constant-expression tentatively,
14211 because the argument could really be a type-id. */
14212 if (maybe_type_id)
14213 cp_parser_parse_tentatively (parser);
14214 argument = cp_parser_constant_expression (parser,
14215 /*allow_non_constant_p=*/false,
14216 /*non_constant_p=*/NULL);
14217 if (!maybe_type_id)
14218 return argument;
14219 if (!cp_parser_next_token_ends_template_argument_p (parser))
14220 cp_parser_error (parser, "expected template-argument");
14221 if (cp_parser_parse_definitely (parser))
14222 return argument;
14223 /* We did our best to parse the argument as a non type-id, but that
14224 was the only alternative that matched (albeit with a '>' after
14225 it). We can assume it's just a typo from the user, and a
14226 diagnostic will then be issued. */
14227 return cp_parser_template_type_arg (parser);
14230 /* Parse an explicit-instantiation.
14232 explicit-instantiation:
14233 template declaration
14235 Although the standard says `declaration', what it really means is:
14237 explicit-instantiation:
14238 template decl-specifier-seq [opt] declarator [opt] ;
14240 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14241 supposed to be allowed. A defect report has been filed about this
14242 issue.
14244 GNU Extension:
14246 explicit-instantiation:
14247 storage-class-specifier template
14248 decl-specifier-seq [opt] declarator [opt] ;
14249 function-specifier template
14250 decl-specifier-seq [opt] declarator [opt] ; */
14252 static void
14253 cp_parser_explicit_instantiation (cp_parser* parser)
14255 int declares_class_or_enum;
14256 cp_decl_specifier_seq decl_specifiers;
14257 tree extension_specifier = NULL_TREE;
14259 timevar_push (TV_TEMPLATE_INST);
14261 /* Look for an (optional) storage-class-specifier or
14262 function-specifier. */
14263 if (cp_parser_allow_gnu_extensions_p (parser))
14265 extension_specifier
14266 = cp_parser_storage_class_specifier_opt (parser);
14267 if (!extension_specifier)
14268 extension_specifier
14269 = cp_parser_function_specifier_opt (parser,
14270 /*decl_specs=*/NULL);
14273 /* Look for the `template' keyword. */
14274 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14275 /* Let the front end know that we are processing an explicit
14276 instantiation. */
14277 begin_explicit_instantiation ();
14278 /* [temp.explicit] says that we are supposed to ignore access
14279 control while processing explicit instantiation directives. */
14280 push_deferring_access_checks (dk_no_check);
14281 /* Parse a decl-specifier-seq. */
14282 cp_parser_decl_specifier_seq (parser,
14283 CP_PARSER_FLAGS_OPTIONAL,
14284 &decl_specifiers,
14285 &declares_class_or_enum);
14286 /* If there was exactly one decl-specifier, and it declared a class,
14287 and there's no declarator, then we have an explicit type
14288 instantiation. */
14289 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14291 tree type;
14293 type = check_tag_decl (&decl_specifiers,
14294 /*explicit_type_instantiation_p=*/true);
14295 /* Turn access control back on for names used during
14296 template instantiation. */
14297 pop_deferring_access_checks ();
14298 if (type)
14299 do_type_instantiation (type, extension_specifier,
14300 /*complain=*/tf_error);
14302 else
14304 cp_declarator *declarator;
14305 tree decl;
14307 /* Parse the declarator. */
14308 declarator
14309 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14310 /*ctor_dtor_or_conv_p=*/NULL,
14311 /*parenthesized_p=*/NULL,
14312 /*member_p=*/false,
14313 /*friend_p=*/false);
14314 if (declares_class_or_enum & 2)
14315 cp_parser_check_for_definition_in_return_type (declarator,
14316 decl_specifiers.type,
14317 decl_specifiers.locations[ds_type_spec]);
14318 if (declarator != cp_error_declarator)
14320 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14321 permerror (decl_specifiers.locations[ds_inline],
14322 "explicit instantiation shall not use"
14323 " %<inline%> specifier");
14324 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14325 permerror (decl_specifiers.locations[ds_constexpr],
14326 "explicit instantiation shall not use"
14327 " %<constexpr%> specifier");
14329 decl = grokdeclarator (declarator, &decl_specifiers,
14330 NORMAL, 0, &decl_specifiers.attributes);
14331 /* Turn access control back on for names used during
14332 template instantiation. */
14333 pop_deferring_access_checks ();
14334 /* Do the explicit instantiation. */
14335 do_decl_instantiation (decl, extension_specifier);
14337 else
14339 pop_deferring_access_checks ();
14340 /* Skip the body of the explicit instantiation. */
14341 cp_parser_skip_to_end_of_statement (parser);
14344 /* We're done with the instantiation. */
14345 end_explicit_instantiation ();
14347 cp_parser_consume_semicolon_at_end_of_statement (parser);
14349 timevar_pop (TV_TEMPLATE_INST);
14352 /* Parse an explicit-specialization.
14354 explicit-specialization:
14355 template < > declaration
14357 Although the standard says `declaration', what it really means is:
14359 explicit-specialization:
14360 template <> decl-specifier [opt] init-declarator [opt] ;
14361 template <> function-definition
14362 template <> explicit-specialization
14363 template <> template-declaration */
14365 static void
14366 cp_parser_explicit_specialization (cp_parser* parser)
14368 bool need_lang_pop;
14369 cp_token *token = cp_lexer_peek_token (parser->lexer);
14371 /* Look for the `template' keyword. */
14372 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14373 /* Look for the `<'. */
14374 cp_parser_require (parser, CPP_LESS, RT_LESS);
14375 /* Look for the `>'. */
14376 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14377 /* We have processed another parameter list. */
14378 ++parser->num_template_parameter_lists;
14379 /* [temp]
14381 A template ... explicit specialization ... shall not have C
14382 linkage. */
14383 if (current_lang_name == lang_name_c)
14385 error_at (token->location, "template specialization with C linkage");
14386 /* Give it C++ linkage to avoid confusing other parts of the
14387 front end. */
14388 push_lang_context (lang_name_cplusplus);
14389 need_lang_pop = true;
14391 else
14392 need_lang_pop = false;
14393 /* Let the front end know that we are beginning a specialization. */
14394 if (!begin_specialization ())
14396 end_specialization ();
14397 return;
14400 /* If the next keyword is `template', we need to figure out whether
14401 or not we're looking a template-declaration. */
14402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14404 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14405 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14406 cp_parser_template_declaration_after_export (parser,
14407 /*member_p=*/false);
14408 else
14409 cp_parser_explicit_specialization (parser);
14411 else
14412 /* Parse the dependent declaration. */
14413 cp_parser_single_declaration (parser,
14414 /*checks=*/NULL,
14415 /*member_p=*/false,
14416 /*explicit_specialization_p=*/true,
14417 /*friend_p=*/NULL);
14418 /* We're done with the specialization. */
14419 end_specialization ();
14420 /* For the erroneous case of a template with C linkage, we pushed an
14421 implicit C++ linkage scope; exit that scope now. */
14422 if (need_lang_pop)
14423 pop_lang_context ();
14424 /* We're done with this parameter list. */
14425 --parser->num_template_parameter_lists;
14428 /* Parse a type-specifier.
14430 type-specifier:
14431 simple-type-specifier
14432 class-specifier
14433 enum-specifier
14434 elaborated-type-specifier
14435 cv-qualifier
14437 GNU Extension:
14439 type-specifier:
14440 __complex__
14442 Returns a representation of the type-specifier. For a
14443 class-specifier, enum-specifier, or elaborated-type-specifier, a
14444 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14446 The parser flags FLAGS is used to control type-specifier parsing.
14448 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14449 in a decl-specifier-seq.
14451 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14452 class-specifier, enum-specifier, or elaborated-type-specifier, then
14453 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14454 if a type is declared; 2 if it is defined. Otherwise, it is set to
14455 zero.
14457 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14458 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14459 is set to FALSE. */
14461 static tree
14462 cp_parser_type_specifier (cp_parser* parser,
14463 cp_parser_flags flags,
14464 cp_decl_specifier_seq *decl_specs,
14465 bool is_declaration,
14466 int* declares_class_or_enum,
14467 bool* is_cv_qualifier)
14469 tree type_spec = NULL_TREE;
14470 cp_token *token;
14471 enum rid keyword;
14472 cp_decl_spec ds = ds_last;
14474 /* Assume this type-specifier does not declare a new type. */
14475 if (declares_class_or_enum)
14476 *declares_class_or_enum = 0;
14477 /* And that it does not specify a cv-qualifier. */
14478 if (is_cv_qualifier)
14479 *is_cv_qualifier = false;
14480 /* Peek at the next token. */
14481 token = cp_lexer_peek_token (parser->lexer);
14483 /* If we're looking at a keyword, we can use that to guide the
14484 production we choose. */
14485 keyword = token->keyword;
14486 switch (keyword)
14488 case RID_ENUM:
14489 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14490 goto elaborated_type_specifier;
14492 /* Look for the enum-specifier. */
14493 type_spec = cp_parser_enum_specifier (parser);
14494 /* If that worked, we're done. */
14495 if (type_spec)
14497 if (declares_class_or_enum)
14498 *declares_class_or_enum = 2;
14499 if (decl_specs)
14500 cp_parser_set_decl_spec_type (decl_specs,
14501 type_spec,
14502 token,
14503 /*type_definition_p=*/true);
14504 return type_spec;
14506 else
14507 goto elaborated_type_specifier;
14509 /* Any of these indicate either a class-specifier, or an
14510 elaborated-type-specifier. */
14511 case RID_CLASS:
14512 case RID_STRUCT:
14513 case RID_UNION:
14514 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14515 goto elaborated_type_specifier;
14517 /* Parse tentatively so that we can back up if we don't find a
14518 class-specifier. */
14519 cp_parser_parse_tentatively (parser);
14520 /* Look for the class-specifier. */
14521 type_spec = cp_parser_class_specifier (parser);
14522 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14523 /* If that worked, we're done. */
14524 if (cp_parser_parse_definitely (parser))
14526 if (declares_class_or_enum)
14527 *declares_class_or_enum = 2;
14528 if (decl_specs)
14529 cp_parser_set_decl_spec_type (decl_specs,
14530 type_spec,
14531 token,
14532 /*type_definition_p=*/true);
14533 return type_spec;
14536 /* Fall through. */
14537 elaborated_type_specifier:
14538 /* We're declaring (not defining) a class or enum. */
14539 if (declares_class_or_enum)
14540 *declares_class_or_enum = 1;
14542 /* Fall through. */
14543 case RID_TYPENAME:
14544 /* Look for an elaborated-type-specifier. */
14545 type_spec
14546 = (cp_parser_elaborated_type_specifier
14547 (parser,
14548 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14549 is_declaration));
14550 if (decl_specs)
14551 cp_parser_set_decl_spec_type (decl_specs,
14552 type_spec,
14553 token,
14554 /*type_definition_p=*/false);
14555 return type_spec;
14557 case RID_CONST:
14558 ds = ds_const;
14559 if (is_cv_qualifier)
14560 *is_cv_qualifier = true;
14561 break;
14563 case RID_VOLATILE:
14564 ds = ds_volatile;
14565 if (is_cv_qualifier)
14566 *is_cv_qualifier = true;
14567 break;
14569 case RID_RESTRICT:
14570 ds = ds_restrict;
14571 if (is_cv_qualifier)
14572 *is_cv_qualifier = true;
14573 break;
14575 case RID_COMPLEX:
14576 /* The `__complex__' keyword is a GNU extension. */
14577 ds = ds_complex;
14578 break;
14580 default:
14581 break;
14584 /* Handle simple keywords. */
14585 if (ds != ds_last)
14587 if (decl_specs)
14589 set_and_check_decl_spec_loc (decl_specs, ds, token);
14590 decl_specs->any_specifiers_p = true;
14592 return cp_lexer_consume_token (parser->lexer)->u.value;
14595 /* If we do not already have a type-specifier, assume we are looking
14596 at a simple-type-specifier. */
14597 type_spec = cp_parser_simple_type_specifier (parser,
14598 decl_specs,
14599 flags);
14601 /* If we didn't find a type-specifier, and a type-specifier was not
14602 optional in this context, issue an error message. */
14603 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14605 cp_parser_error (parser, "expected type specifier");
14606 return error_mark_node;
14609 return type_spec;
14612 /* Parse a simple-type-specifier.
14614 simple-type-specifier:
14615 :: [opt] nested-name-specifier [opt] type-name
14616 :: [opt] nested-name-specifier template template-id
14617 char
14618 wchar_t
14619 bool
14620 short
14622 long
14623 signed
14624 unsigned
14625 float
14626 double
14627 void
14629 C++0x Extension:
14631 simple-type-specifier:
14632 auto
14633 decltype ( expression )
14634 char16_t
14635 char32_t
14636 __underlying_type ( type-id )
14638 GNU Extension:
14640 simple-type-specifier:
14641 __int128
14642 __typeof__ unary-expression
14643 __typeof__ ( type-id )
14644 __typeof__ ( type-id ) { initializer-list , [opt] }
14646 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14647 appropriately updated. */
14649 static tree
14650 cp_parser_simple_type_specifier (cp_parser* parser,
14651 cp_decl_specifier_seq *decl_specs,
14652 cp_parser_flags flags)
14654 tree type = NULL_TREE;
14655 cp_token *token;
14657 /* Peek at the next token. */
14658 token = cp_lexer_peek_token (parser->lexer);
14660 /* If we're looking at a keyword, things are easy. */
14661 switch (token->keyword)
14663 case RID_CHAR:
14664 if (decl_specs)
14665 decl_specs->explicit_char_p = true;
14666 type = char_type_node;
14667 break;
14668 case RID_CHAR16:
14669 type = char16_type_node;
14670 break;
14671 case RID_CHAR32:
14672 type = char32_type_node;
14673 break;
14674 case RID_WCHAR:
14675 type = wchar_type_node;
14676 break;
14677 case RID_BOOL:
14678 type = boolean_type_node;
14679 break;
14680 case RID_SHORT:
14681 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14682 type = short_integer_type_node;
14683 break;
14684 case RID_INT:
14685 if (decl_specs)
14686 decl_specs->explicit_int_p = true;
14687 type = integer_type_node;
14688 break;
14689 case RID_INT128:
14690 if (!int128_integer_type_node)
14691 break;
14692 if (decl_specs)
14693 decl_specs->explicit_int128_p = true;
14694 type = int128_integer_type_node;
14695 break;
14696 case RID_LONG:
14697 if (decl_specs)
14698 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14699 type = long_integer_type_node;
14700 break;
14701 case RID_SIGNED:
14702 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14703 type = integer_type_node;
14704 break;
14705 case RID_UNSIGNED:
14706 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14707 type = unsigned_type_node;
14708 break;
14709 case RID_FLOAT:
14710 type = float_type_node;
14711 break;
14712 case RID_DOUBLE:
14713 type = double_type_node;
14714 break;
14715 case RID_VOID:
14716 type = void_type_node;
14717 break;
14719 case RID_AUTO:
14720 maybe_warn_cpp0x (CPP0X_AUTO);
14721 if (parser->auto_is_implicit_function_template_parm_p)
14723 type = synthesize_implicit_template_parm (parser);
14725 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14727 if (cxx_dialect < cxx14)
14728 pedwarn (location_of (type), 0,
14729 "use of %<auto%> in lambda parameter declaration "
14730 "only available with "
14731 "-std=c++14 or -std=gnu++14");
14733 else if (cxx_dialect < cxx14)
14734 pedwarn (location_of (type), 0,
14735 "use of %<auto%> in parameter declaration "
14736 "only available with "
14737 "-std=c++14 or -std=gnu++14");
14738 else
14739 pedwarn (location_of (type), OPT_Wpedantic,
14740 "ISO C++ forbids use of %<auto%> in parameter "
14741 "declaration");
14743 else
14744 type = make_auto ();
14745 break;
14747 case RID_DECLTYPE:
14748 /* Since DR 743, decltype can either be a simple-type-specifier by
14749 itself or begin a nested-name-specifier. Parsing it will replace
14750 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14751 handling below decide what to do. */
14752 cp_parser_decltype (parser);
14753 cp_lexer_set_token_position (parser->lexer, token);
14754 break;
14756 case RID_TYPEOF:
14757 /* Consume the `typeof' token. */
14758 cp_lexer_consume_token (parser->lexer);
14759 /* Parse the operand to `typeof'. */
14760 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14761 /* If it is not already a TYPE, take its type. */
14762 if (!TYPE_P (type))
14763 type = finish_typeof (type);
14765 if (decl_specs)
14766 cp_parser_set_decl_spec_type (decl_specs, type,
14767 token,
14768 /*type_definition_p=*/false);
14770 return type;
14772 case RID_UNDERLYING_TYPE:
14773 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14774 if (decl_specs)
14775 cp_parser_set_decl_spec_type (decl_specs, type,
14776 token,
14777 /*type_definition_p=*/false);
14779 return type;
14781 case RID_BASES:
14782 case RID_DIRECT_BASES:
14783 type = cp_parser_trait_expr (parser, token->keyword);
14784 if (decl_specs)
14785 cp_parser_set_decl_spec_type (decl_specs, type,
14786 token,
14787 /*type_definition_p=*/false);
14788 return type;
14789 default:
14790 break;
14793 /* If token is an already-parsed decltype not followed by ::,
14794 it's a simple-type-specifier. */
14795 if (token->type == CPP_DECLTYPE
14796 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14798 type = token->u.value;
14799 if (decl_specs)
14800 cp_parser_set_decl_spec_type (decl_specs, type,
14801 token,
14802 /*type_definition_p=*/false);
14803 cp_lexer_consume_token (parser->lexer);
14804 return type;
14807 /* If the type-specifier was for a built-in type, we're done. */
14808 if (type)
14810 /* Record the type. */
14811 if (decl_specs
14812 && (token->keyword != RID_SIGNED
14813 && token->keyword != RID_UNSIGNED
14814 && token->keyword != RID_SHORT
14815 && token->keyword != RID_LONG))
14816 cp_parser_set_decl_spec_type (decl_specs,
14817 type,
14818 token,
14819 /*type_definition_p=*/false);
14820 if (decl_specs)
14821 decl_specs->any_specifiers_p = true;
14823 /* Consume the token. */
14824 cp_lexer_consume_token (parser->lexer);
14826 /* There is no valid C++ program where a non-template type is
14827 followed by a "<". That usually indicates that the user thought
14828 that the type was a template. */
14829 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14830 token->location);
14832 return TYPE_NAME (type);
14835 /* The type-specifier must be a user-defined type. */
14836 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14838 bool qualified_p;
14839 bool global_p;
14841 /* Don't gobble tokens or issue error messages if this is an
14842 optional type-specifier. */
14843 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14844 cp_parser_parse_tentatively (parser);
14846 /* Look for the optional `::' operator. */
14847 global_p
14848 = (cp_parser_global_scope_opt (parser,
14849 /*current_scope_valid_p=*/false)
14850 != NULL_TREE);
14851 /* Look for the nested-name specifier. */
14852 qualified_p
14853 = (cp_parser_nested_name_specifier_opt (parser,
14854 /*typename_keyword_p=*/false,
14855 /*check_dependency_p=*/true,
14856 /*type_p=*/false,
14857 /*is_declaration=*/false)
14858 != NULL_TREE);
14859 token = cp_lexer_peek_token (parser->lexer);
14860 /* If we have seen a nested-name-specifier, and the next token
14861 is `template', then we are using the template-id production. */
14862 if (parser->scope
14863 && cp_parser_optional_template_keyword (parser))
14865 /* Look for the template-id. */
14866 type = cp_parser_template_id (parser,
14867 /*template_keyword_p=*/true,
14868 /*check_dependency_p=*/true,
14869 none_type,
14870 /*is_declaration=*/false);
14871 /* If the template-id did not name a type, we are out of
14872 luck. */
14873 if (TREE_CODE (type) != TYPE_DECL)
14875 cp_parser_error (parser, "expected template-id for type");
14876 type = NULL_TREE;
14879 /* Otherwise, look for a type-name. */
14880 else
14881 type = cp_parser_type_name (parser);
14882 /* Keep track of all name-lookups performed in class scopes. */
14883 if (type
14884 && !global_p
14885 && !qualified_p
14886 && TREE_CODE (type) == TYPE_DECL
14887 && identifier_p (DECL_NAME (type)))
14888 maybe_note_name_used_in_class (DECL_NAME (type), type);
14889 /* If it didn't work out, we don't have a TYPE. */
14890 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14891 && !cp_parser_parse_definitely (parser))
14892 type = NULL_TREE;
14893 if (type && decl_specs)
14894 cp_parser_set_decl_spec_type (decl_specs, type,
14895 token,
14896 /*type_definition_p=*/false);
14899 /* If we didn't get a type-name, issue an error message. */
14900 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14902 cp_parser_error (parser, "expected type-name");
14903 return error_mark_node;
14906 if (type && type != error_mark_node)
14908 /* See if TYPE is an Objective-C type, and if so, parse and
14909 accept any protocol references following it. Do this before
14910 the cp_parser_check_for_invalid_template_id() call, because
14911 Objective-C types can be followed by '<...>' which would
14912 enclose protocol names rather than template arguments, and so
14913 everything is fine. */
14914 if (c_dialect_objc () && !parser->scope
14915 && (objc_is_id (type) || objc_is_class_name (type)))
14917 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14918 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14920 /* Clobber the "unqualified" type previously entered into
14921 DECL_SPECS with the new, improved protocol-qualified version. */
14922 if (decl_specs)
14923 decl_specs->type = qual_type;
14925 return qual_type;
14928 /* There is no valid C++ program where a non-template type is
14929 followed by a "<". That usually indicates that the user
14930 thought that the type was a template. */
14931 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14932 none_type,
14933 token->location);
14936 return type;
14939 /* Parse a type-name.
14941 type-name:
14942 class-name
14943 enum-name
14944 typedef-name
14945 simple-template-id [in c++0x]
14947 enum-name:
14948 identifier
14950 typedef-name:
14951 identifier
14953 Returns a TYPE_DECL for the type. */
14955 static tree
14956 cp_parser_type_name (cp_parser* parser)
14958 tree type_decl;
14960 /* We can't know yet whether it is a class-name or not. */
14961 cp_parser_parse_tentatively (parser);
14962 /* Try a class-name. */
14963 type_decl = cp_parser_class_name (parser,
14964 /*typename_keyword_p=*/false,
14965 /*template_keyword_p=*/false,
14966 none_type,
14967 /*check_dependency_p=*/true,
14968 /*class_head_p=*/false,
14969 /*is_declaration=*/false);
14970 /* If it's not a class-name, keep looking. */
14971 if (!cp_parser_parse_definitely (parser))
14973 if (cxx_dialect < cxx11)
14974 /* It must be a typedef-name or an enum-name. */
14975 return cp_parser_nonclass_name (parser);
14977 cp_parser_parse_tentatively (parser);
14978 /* It is either a simple-template-id representing an
14979 instantiation of an alias template... */
14980 type_decl = cp_parser_template_id (parser,
14981 /*template_keyword_p=*/false,
14982 /*check_dependency_p=*/true,
14983 none_type,
14984 /*is_declaration=*/false);
14985 /* Note that this must be an instantiation of an alias template
14986 because [temp.names]/6 says:
14988 A template-id that names an alias template specialization
14989 is a type-name.
14991 Whereas [temp.names]/7 says:
14993 A simple-template-id that names a class template
14994 specialization is a class-name. */
14995 if (type_decl != NULL_TREE
14996 && TREE_CODE (type_decl) == TYPE_DECL
14997 && TYPE_DECL_ALIAS_P (type_decl))
14998 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14999 else
15000 cp_parser_simulate_error (parser);
15002 if (!cp_parser_parse_definitely (parser))
15003 /* ... Or a typedef-name or an enum-name. */
15004 return cp_parser_nonclass_name (parser);
15007 return type_decl;
15010 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15012 enum-name:
15013 identifier
15015 typedef-name:
15016 identifier
15018 Returns a TYPE_DECL for the type. */
15020 static tree
15021 cp_parser_nonclass_name (cp_parser* parser)
15023 tree type_decl;
15024 tree identifier;
15026 cp_token *token = cp_lexer_peek_token (parser->lexer);
15027 identifier = cp_parser_identifier (parser);
15028 if (identifier == error_mark_node)
15029 return error_mark_node;
15031 /* Look up the type-name. */
15032 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15034 type_decl = strip_using_decl (type_decl);
15036 if (TREE_CODE (type_decl) != TYPE_DECL
15037 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15039 /* See if this is an Objective-C type. */
15040 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15041 tree type = objc_get_protocol_qualified_type (identifier, protos);
15042 if (type)
15043 type_decl = TYPE_NAME (type);
15046 /* Issue an error if we did not find a type-name. */
15047 if (TREE_CODE (type_decl) != TYPE_DECL
15048 /* In Objective-C, we have the complication that class names are
15049 normally type names and start declarations (eg, the
15050 "NSObject" in "NSObject *object;"), but can be used in an
15051 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15052 is an expression. So, a classname followed by a dot is not a
15053 valid type-name. */
15054 || (objc_is_class_name (TREE_TYPE (type_decl))
15055 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15057 if (!cp_parser_simulate_error (parser))
15058 cp_parser_name_lookup_error (parser, identifier, type_decl,
15059 NLE_TYPE, token->location);
15060 return error_mark_node;
15062 /* Remember that the name was used in the definition of the
15063 current class so that we can check later to see if the
15064 meaning would have been different after the class was
15065 entirely defined. */
15066 else if (type_decl != error_mark_node
15067 && !parser->scope)
15068 maybe_note_name_used_in_class (identifier, type_decl);
15070 return type_decl;
15073 /* Parse an elaborated-type-specifier. Note that the grammar given
15074 here incorporates the resolution to DR68.
15076 elaborated-type-specifier:
15077 class-key :: [opt] nested-name-specifier [opt] identifier
15078 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15079 enum-key :: [opt] nested-name-specifier [opt] identifier
15080 typename :: [opt] nested-name-specifier identifier
15081 typename :: [opt] nested-name-specifier template [opt]
15082 template-id
15084 GNU extension:
15086 elaborated-type-specifier:
15087 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15088 class-key attributes :: [opt] nested-name-specifier [opt]
15089 template [opt] template-id
15090 enum attributes :: [opt] nested-name-specifier [opt] identifier
15092 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15093 declared `friend'. If IS_DECLARATION is TRUE, then this
15094 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15095 something is being declared.
15097 Returns the TYPE specified. */
15099 static tree
15100 cp_parser_elaborated_type_specifier (cp_parser* parser,
15101 bool is_friend,
15102 bool is_declaration)
15104 enum tag_types tag_type;
15105 tree identifier;
15106 tree type = NULL_TREE;
15107 tree attributes = NULL_TREE;
15108 tree globalscope;
15109 cp_token *token = NULL;
15111 /* See if we're looking at the `enum' keyword. */
15112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15114 /* Consume the `enum' token. */
15115 cp_lexer_consume_token (parser->lexer);
15116 /* Remember that it's an enumeration type. */
15117 tag_type = enum_type;
15118 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15119 enums) is used here. */
15120 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15121 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15123 pedwarn (input_location, 0, "elaborated-type-specifier "
15124 "for a scoped enum must not use the %<%D%> keyword",
15125 cp_lexer_peek_token (parser->lexer)->u.value);
15126 /* Consume the `struct' or `class' and parse it anyway. */
15127 cp_lexer_consume_token (parser->lexer);
15129 /* Parse the attributes. */
15130 attributes = cp_parser_attributes_opt (parser);
15132 /* Or, it might be `typename'. */
15133 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15134 RID_TYPENAME))
15136 /* Consume the `typename' token. */
15137 cp_lexer_consume_token (parser->lexer);
15138 /* Remember that it's a `typename' type. */
15139 tag_type = typename_type;
15141 /* Otherwise it must be a class-key. */
15142 else
15144 tag_type = cp_parser_class_key (parser);
15145 if (tag_type == none_type)
15146 return error_mark_node;
15147 /* Parse the attributes. */
15148 attributes = cp_parser_attributes_opt (parser);
15151 /* Look for the `::' operator. */
15152 globalscope = cp_parser_global_scope_opt (parser,
15153 /*current_scope_valid_p=*/false);
15154 /* Look for the nested-name-specifier. */
15155 if (tag_type == typename_type && !globalscope)
15157 if (!cp_parser_nested_name_specifier (parser,
15158 /*typename_keyword_p=*/true,
15159 /*check_dependency_p=*/true,
15160 /*type_p=*/true,
15161 is_declaration))
15162 return error_mark_node;
15164 else
15165 /* Even though `typename' is not present, the proposed resolution
15166 to Core Issue 180 says that in `class A<T>::B', `B' should be
15167 considered a type-name, even if `A<T>' is dependent. */
15168 cp_parser_nested_name_specifier_opt (parser,
15169 /*typename_keyword_p=*/true,
15170 /*check_dependency_p=*/true,
15171 /*type_p=*/true,
15172 is_declaration);
15173 /* For everything but enumeration types, consider a template-id.
15174 For an enumeration type, consider only a plain identifier. */
15175 if (tag_type != enum_type)
15177 bool template_p = false;
15178 tree decl;
15180 /* Allow the `template' keyword. */
15181 template_p = cp_parser_optional_template_keyword (parser);
15182 /* If we didn't see `template', we don't know if there's a
15183 template-id or not. */
15184 if (!template_p)
15185 cp_parser_parse_tentatively (parser);
15186 /* Parse the template-id. */
15187 token = cp_lexer_peek_token (parser->lexer);
15188 decl = cp_parser_template_id (parser, template_p,
15189 /*check_dependency_p=*/true,
15190 tag_type,
15191 is_declaration);
15192 /* If we didn't find a template-id, look for an ordinary
15193 identifier. */
15194 if (!template_p && !cp_parser_parse_definitely (parser))
15196 /* We can get here when cp_parser_template_id, called by
15197 cp_parser_class_name with tag_type == none_type, succeeds
15198 and caches a BASELINK. Then, when called again here,
15199 instead of failing and returning an error_mark_node
15200 returns it (see template/typename17.C in C++11).
15201 ??? Could we diagnose this earlier? */
15202 else if (tag_type == typename_type && BASELINK_P (decl))
15204 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15205 type = error_mark_node;
15207 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15208 in effect, then we must assume that, upon instantiation, the
15209 template will correspond to a class. */
15210 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15211 && tag_type == typename_type)
15212 type = make_typename_type (parser->scope, decl,
15213 typename_type,
15214 /*complain=*/tf_error);
15215 /* If the `typename' keyword is in effect and DECL is not a type
15216 decl, then type is non existent. */
15217 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15219 else if (TREE_CODE (decl) == TYPE_DECL)
15220 type = check_elaborated_type_specifier (tag_type, decl,
15221 /*allow_template_p=*/true);
15222 else if (decl == error_mark_node)
15223 type = error_mark_node;
15226 if (!type)
15228 token = cp_lexer_peek_token (parser->lexer);
15229 identifier = cp_parser_identifier (parser);
15231 if (identifier == error_mark_node)
15233 parser->scope = NULL_TREE;
15234 return error_mark_node;
15237 /* For a `typename', we needn't call xref_tag. */
15238 if (tag_type == typename_type
15239 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15240 return cp_parser_make_typename_type (parser, identifier,
15241 token->location);
15243 /* Template parameter lists apply only if we are not within a
15244 function parameter list. */
15245 bool template_parm_lists_apply
15246 = parser->num_template_parameter_lists;
15247 if (template_parm_lists_apply)
15248 for (cp_binding_level *s = current_binding_level;
15249 s && s->kind != sk_template_parms;
15250 s = s->level_chain)
15251 if (s->kind == sk_function_parms)
15252 template_parm_lists_apply = false;
15254 /* Look up a qualified name in the usual way. */
15255 if (parser->scope)
15257 tree decl;
15258 tree ambiguous_decls;
15260 decl = cp_parser_lookup_name (parser, identifier,
15261 tag_type,
15262 /*is_template=*/false,
15263 /*is_namespace=*/false,
15264 /*check_dependency=*/true,
15265 &ambiguous_decls,
15266 token->location);
15268 /* If the lookup was ambiguous, an error will already have been
15269 issued. */
15270 if (ambiguous_decls)
15271 return error_mark_node;
15273 /* If we are parsing friend declaration, DECL may be a
15274 TEMPLATE_DECL tree node here. However, we need to check
15275 whether this TEMPLATE_DECL results in valid code. Consider
15276 the following example:
15278 namespace N {
15279 template <class T> class C {};
15281 class X {
15282 template <class T> friend class N::C; // #1, valid code
15284 template <class T> class Y {
15285 friend class N::C; // #2, invalid code
15288 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15289 name lookup of `N::C'. We see that friend declaration must
15290 be template for the code to be valid. Note that
15291 processing_template_decl does not work here since it is
15292 always 1 for the above two cases. */
15294 decl = (cp_parser_maybe_treat_template_as_class
15295 (decl, /*tag_name_p=*/is_friend
15296 && template_parm_lists_apply));
15298 if (TREE_CODE (decl) != TYPE_DECL)
15300 cp_parser_diagnose_invalid_type_name (parser,
15301 identifier,
15302 token->location);
15303 return error_mark_node;
15306 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15308 bool allow_template = (template_parm_lists_apply
15309 || DECL_SELF_REFERENCE_P (decl));
15310 type = check_elaborated_type_specifier (tag_type, decl,
15311 allow_template);
15313 if (type == error_mark_node)
15314 return error_mark_node;
15317 /* Forward declarations of nested types, such as
15319 class C1::C2;
15320 class C1::C2::C3;
15322 are invalid unless all components preceding the final '::'
15323 are complete. If all enclosing types are complete, these
15324 declarations become merely pointless.
15326 Invalid forward declarations of nested types are errors
15327 caught elsewhere in parsing. Those that are pointless arrive
15328 here. */
15330 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15331 && !is_friend && !processing_explicit_instantiation)
15332 warning (0, "declaration %qD does not declare anything", decl);
15334 type = TREE_TYPE (decl);
15336 else
15338 /* An elaborated-type-specifier sometimes introduces a new type and
15339 sometimes names an existing type. Normally, the rule is that it
15340 introduces a new type only if there is not an existing type of
15341 the same name already in scope. For example, given:
15343 struct S {};
15344 void f() { struct S s; }
15346 the `struct S' in the body of `f' is the same `struct S' as in
15347 the global scope; the existing definition is used. However, if
15348 there were no global declaration, this would introduce a new
15349 local class named `S'.
15351 An exception to this rule applies to the following code:
15353 namespace N { struct S; }
15355 Here, the elaborated-type-specifier names a new type
15356 unconditionally; even if there is already an `S' in the
15357 containing scope this declaration names a new type.
15358 This exception only applies if the elaborated-type-specifier
15359 forms the complete declaration:
15361 [class.name]
15363 A declaration consisting solely of `class-key identifier ;' is
15364 either a redeclaration of the name in the current scope or a
15365 forward declaration of the identifier as a class name. It
15366 introduces the name into the current scope.
15368 We are in this situation precisely when the next token is a `;'.
15370 An exception to the exception is that a `friend' declaration does
15371 *not* name a new type; i.e., given:
15373 struct S { friend struct T; };
15375 `T' is not a new type in the scope of `S'.
15377 Also, `new struct S' or `sizeof (struct S)' never results in the
15378 definition of a new type; a new type can only be declared in a
15379 declaration context. */
15381 tag_scope ts;
15382 bool template_p;
15384 if (is_friend)
15385 /* Friends have special name lookup rules. */
15386 ts = ts_within_enclosing_non_class;
15387 else if (is_declaration
15388 && cp_lexer_next_token_is (parser->lexer,
15389 CPP_SEMICOLON))
15390 /* This is a `class-key identifier ;' */
15391 ts = ts_current;
15392 else
15393 ts = ts_global;
15395 template_p =
15396 (template_parm_lists_apply
15397 && (cp_parser_next_token_starts_class_definition_p (parser)
15398 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15399 /* An unqualified name was used to reference this type, so
15400 there were no qualifying templates. */
15401 if (template_parm_lists_apply
15402 && !cp_parser_check_template_parameters (parser,
15403 /*num_templates=*/0,
15404 token->location,
15405 /*declarator=*/NULL))
15406 return error_mark_node;
15407 type = xref_tag (tag_type, identifier, ts, template_p);
15411 if (type == error_mark_node)
15412 return error_mark_node;
15414 /* Allow attributes on forward declarations of classes. */
15415 if (attributes)
15417 if (TREE_CODE (type) == TYPENAME_TYPE)
15418 warning (OPT_Wattributes,
15419 "attributes ignored on uninstantiated type");
15420 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15421 && ! processing_explicit_instantiation)
15422 warning (OPT_Wattributes,
15423 "attributes ignored on template instantiation");
15424 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15425 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15426 else
15427 warning (OPT_Wattributes,
15428 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15431 if (tag_type != enum_type)
15433 /* Indicate whether this class was declared as a `class' or as a
15434 `struct'. */
15435 if (TREE_CODE (type) == RECORD_TYPE)
15436 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15437 cp_parser_check_class_key (tag_type, type);
15440 /* A "<" cannot follow an elaborated type specifier. If that
15441 happens, the user was probably trying to form a template-id. */
15442 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15443 token->location);
15445 return type;
15448 /* Parse an enum-specifier.
15450 enum-specifier:
15451 enum-head { enumerator-list [opt] }
15452 enum-head { enumerator-list , } [C++0x]
15454 enum-head:
15455 enum-key identifier [opt] enum-base [opt]
15456 enum-key nested-name-specifier identifier enum-base [opt]
15458 enum-key:
15459 enum
15460 enum class [C++0x]
15461 enum struct [C++0x]
15463 enum-base: [C++0x]
15464 : type-specifier-seq
15466 opaque-enum-specifier:
15467 enum-key identifier enum-base [opt] ;
15469 GNU Extensions:
15470 enum-key attributes[opt] identifier [opt] enum-base [opt]
15471 { enumerator-list [opt] }attributes[opt]
15472 enum-key attributes[opt] identifier [opt] enum-base [opt]
15473 { enumerator-list, }attributes[opt] [C++0x]
15475 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15476 if the token stream isn't an enum-specifier after all. */
15478 static tree
15479 cp_parser_enum_specifier (cp_parser* parser)
15481 tree identifier;
15482 tree type = NULL_TREE;
15483 tree prev_scope;
15484 tree nested_name_specifier = NULL_TREE;
15485 tree attributes;
15486 bool scoped_enum_p = false;
15487 bool has_underlying_type = false;
15488 bool nested_being_defined = false;
15489 bool new_value_list = false;
15490 bool is_new_type = false;
15491 bool is_anonymous = false;
15492 tree underlying_type = NULL_TREE;
15493 cp_token *type_start_token = NULL;
15494 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15496 parser->colon_corrects_to_scope_p = false;
15498 /* Parse tentatively so that we can back up if we don't find a
15499 enum-specifier. */
15500 cp_parser_parse_tentatively (parser);
15502 /* Caller guarantees that the current token is 'enum', an identifier
15503 possibly follows, and the token after that is an opening brace.
15504 If we don't have an identifier, fabricate an anonymous name for
15505 the enumeration being defined. */
15506 cp_lexer_consume_token (parser->lexer);
15508 /* Parse the "class" or "struct", which indicates a scoped
15509 enumeration type in C++0x. */
15510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15511 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15513 if (cxx_dialect < cxx11)
15514 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15516 /* Consume the `struct' or `class' token. */
15517 cp_lexer_consume_token (parser->lexer);
15519 scoped_enum_p = true;
15522 attributes = cp_parser_attributes_opt (parser);
15524 /* Clear the qualification. */
15525 parser->scope = NULL_TREE;
15526 parser->qualifying_scope = NULL_TREE;
15527 parser->object_scope = NULL_TREE;
15529 /* Figure out in what scope the declaration is being placed. */
15530 prev_scope = current_scope ();
15532 type_start_token = cp_lexer_peek_token (parser->lexer);
15534 push_deferring_access_checks (dk_no_check);
15535 nested_name_specifier
15536 = cp_parser_nested_name_specifier_opt (parser,
15537 /*typename_keyword_p=*/true,
15538 /*check_dependency_p=*/false,
15539 /*type_p=*/false,
15540 /*is_declaration=*/false);
15542 if (nested_name_specifier)
15544 tree name;
15546 identifier = cp_parser_identifier (parser);
15547 name = cp_parser_lookup_name (parser, identifier,
15548 enum_type,
15549 /*is_template=*/false,
15550 /*is_namespace=*/false,
15551 /*check_dependency=*/true,
15552 /*ambiguous_decls=*/NULL,
15553 input_location);
15554 if (name && name != error_mark_node)
15556 type = TREE_TYPE (name);
15557 if (TREE_CODE (type) == TYPENAME_TYPE)
15559 /* Are template enums allowed in ISO? */
15560 if (template_parm_scope_p ())
15561 pedwarn (type_start_token->location, OPT_Wpedantic,
15562 "%qD is an enumeration template", name);
15563 /* ignore a typename reference, for it will be solved by name
15564 in start_enum. */
15565 type = NULL_TREE;
15568 else if (nested_name_specifier == error_mark_node)
15569 /* We already issued an error. */;
15570 else
15571 error_at (type_start_token->location,
15572 "%qD is not an enumerator-name", identifier);
15574 else
15576 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15577 identifier = cp_parser_identifier (parser);
15578 else
15580 identifier = make_anon_name ();
15581 is_anonymous = true;
15582 if (scoped_enum_p)
15583 error_at (type_start_token->location,
15584 "anonymous scoped enum is not allowed");
15587 pop_deferring_access_checks ();
15589 /* Check for the `:' that denotes a specified underlying type in C++0x.
15590 Note that a ':' could also indicate a bitfield width, however. */
15591 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15593 cp_decl_specifier_seq type_specifiers;
15595 /* Consume the `:'. */
15596 cp_lexer_consume_token (parser->lexer);
15598 /* Parse the type-specifier-seq. */
15599 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15600 /*is_trailing_return=*/false,
15601 &type_specifiers);
15603 /* At this point this is surely not elaborated type specifier. */
15604 if (!cp_parser_parse_definitely (parser))
15605 return NULL_TREE;
15607 if (cxx_dialect < cxx11)
15608 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15610 has_underlying_type = true;
15612 /* If that didn't work, stop. */
15613 if (type_specifiers.type != error_mark_node)
15615 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15616 /*initialized=*/0, NULL);
15617 if (underlying_type == error_mark_node
15618 || check_for_bare_parameter_packs (underlying_type))
15619 underlying_type = NULL_TREE;
15623 /* Look for the `{' but don't consume it yet. */
15624 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15626 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15628 cp_parser_error (parser, "expected %<{%>");
15629 if (has_underlying_type)
15631 type = NULL_TREE;
15632 goto out;
15635 /* An opaque-enum-specifier must have a ';' here. */
15636 if ((scoped_enum_p || underlying_type)
15637 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15639 cp_parser_error (parser, "expected %<;%> or %<{%>");
15640 if (has_underlying_type)
15642 type = NULL_TREE;
15643 goto out;
15648 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15649 return NULL_TREE;
15651 if (nested_name_specifier)
15653 if (CLASS_TYPE_P (nested_name_specifier))
15655 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15656 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15657 push_scope (nested_name_specifier);
15659 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15661 push_nested_namespace (nested_name_specifier);
15665 /* Issue an error message if type-definitions are forbidden here. */
15666 if (!cp_parser_check_type_definition (parser))
15667 type = error_mark_node;
15668 else
15669 /* Create the new type. We do this before consuming the opening
15670 brace so the enum will be recorded as being on the line of its
15671 tag (or the 'enum' keyword, if there is no tag). */
15672 type = start_enum (identifier, type, underlying_type,
15673 scoped_enum_p, &is_new_type);
15675 /* If the next token is not '{' it is an opaque-enum-specifier or an
15676 elaborated-type-specifier. */
15677 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15679 timevar_push (TV_PARSE_ENUM);
15680 if (nested_name_specifier
15681 && nested_name_specifier != error_mark_node)
15683 /* The following catches invalid code such as:
15684 enum class S<int>::E { A, B, C }; */
15685 if (!processing_specialization
15686 && CLASS_TYPE_P (nested_name_specifier)
15687 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15688 error_at (type_start_token->location, "cannot add an enumerator "
15689 "list to a template instantiation");
15691 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15693 error_at (type_start_token->location,
15694 "%<%T::%E%> has not been declared",
15695 TYPE_CONTEXT (nested_name_specifier),
15696 nested_name_specifier);
15697 type = error_mark_node;
15699 /* If that scope does not contain the scope in which the
15700 class was originally declared, the program is invalid. */
15701 else if (prev_scope && !is_ancestor (prev_scope,
15702 nested_name_specifier))
15704 if (at_namespace_scope_p ())
15705 error_at (type_start_token->location,
15706 "declaration of %qD in namespace %qD which does not "
15707 "enclose %qD",
15708 type, prev_scope, nested_name_specifier);
15709 else
15710 error_at (type_start_token->location,
15711 "declaration of %qD in %qD which does not "
15712 "enclose %qD",
15713 type, prev_scope, nested_name_specifier);
15714 type = error_mark_node;
15718 if (scoped_enum_p)
15719 begin_scope (sk_scoped_enum, type);
15721 /* Consume the opening brace. */
15722 cp_lexer_consume_token (parser->lexer);
15724 if (type == error_mark_node)
15725 ; /* Nothing to add */
15726 else if (OPAQUE_ENUM_P (type)
15727 || (cxx_dialect > cxx98 && processing_specialization))
15729 new_value_list = true;
15730 SET_OPAQUE_ENUM_P (type, false);
15731 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15733 else
15735 error_at (type_start_token->location,
15736 "multiple definition of %q#T", type);
15737 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15738 "previous definition here");
15739 type = error_mark_node;
15742 if (type == error_mark_node)
15743 cp_parser_skip_to_end_of_block_or_statement (parser);
15744 /* If the next token is not '}', then there are some enumerators. */
15745 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15747 if (is_anonymous && !scoped_enum_p)
15748 pedwarn (type_start_token->location, OPT_Wpedantic,
15749 "ISO C++ forbids empty anonymous enum");
15751 else
15752 cp_parser_enumerator_list (parser, type);
15754 /* Consume the final '}'. */
15755 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15757 if (scoped_enum_p)
15758 finish_scope ();
15759 timevar_pop (TV_PARSE_ENUM);
15761 else
15763 /* If a ';' follows, then it is an opaque-enum-specifier
15764 and additional restrictions apply. */
15765 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15767 if (is_anonymous)
15768 error_at (type_start_token->location,
15769 "opaque-enum-specifier without name");
15770 else if (nested_name_specifier)
15771 error_at (type_start_token->location,
15772 "opaque-enum-specifier must use a simple identifier");
15776 /* Look for trailing attributes to apply to this enumeration, and
15777 apply them if appropriate. */
15778 if (cp_parser_allow_gnu_extensions_p (parser))
15780 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15781 trailing_attr = chainon (trailing_attr, attributes);
15782 cplus_decl_attributes (&type,
15783 trailing_attr,
15784 (int) ATTR_FLAG_TYPE_IN_PLACE);
15787 /* Finish up the enumeration. */
15788 if (type != error_mark_node)
15790 if (new_value_list)
15791 finish_enum_value_list (type);
15792 if (is_new_type)
15793 finish_enum (type);
15796 if (nested_name_specifier)
15798 if (CLASS_TYPE_P (nested_name_specifier))
15800 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15801 pop_scope (nested_name_specifier);
15803 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15805 pop_nested_namespace (nested_name_specifier);
15808 out:
15809 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15810 return type;
15813 /* Parse an enumerator-list. The enumerators all have the indicated
15814 TYPE.
15816 enumerator-list:
15817 enumerator-definition
15818 enumerator-list , enumerator-definition */
15820 static void
15821 cp_parser_enumerator_list (cp_parser* parser, tree type)
15823 while (true)
15825 /* Parse an enumerator-definition. */
15826 cp_parser_enumerator_definition (parser, type);
15828 /* If the next token is not a ',', we've reached the end of
15829 the list. */
15830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15831 break;
15832 /* Otherwise, consume the `,' and keep going. */
15833 cp_lexer_consume_token (parser->lexer);
15834 /* If the next token is a `}', there is a trailing comma. */
15835 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15837 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15838 pedwarn (input_location, OPT_Wpedantic,
15839 "comma at end of enumerator list");
15840 break;
15845 /* Parse an enumerator-definition. The enumerator has the indicated
15846 TYPE.
15848 enumerator-definition:
15849 enumerator
15850 enumerator = constant-expression
15852 enumerator:
15853 identifier */
15855 static void
15856 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15858 tree identifier;
15859 tree value;
15860 location_t loc;
15862 /* Save the input location because we are interested in the location
15863 of the identifier and not the location of the explicit value. */
15864 loc = cp_lexer_peek_token (parser->lexer)->location;
15866 /* Look for the identifier. */
15867 identifier = cp_parser_identifier (parser);
15868 if (identifier == error_mark_node)
15869 return;
15871 /* If the next token is an '=', then there is an explicit value. */
15872 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15874 /* Consume the `=' token. */
15875 cp_lexer_consume_token (parser->lexer);
15876 /* Parse the value. */
15877 value = cp_parser_constant_expression (parser,
15878 /*allow_non_constant_p=*/false,
15879 NULL);
15881 else
15882 value = NULL_TREE;
15884 /* If we are processing a template, make sure the initializer of the
15885 enumerator doesn't contain any bare template parameter pack. */
15886 if (check_for_bare_parameter_packs (value))
15887 value = error_mark_node;
15889 /* integral_constant_value will pull out this expression, so make sure
15890 it's folded as appropriate. */
15891 value = fold_non_dependent_expr (value);
15893 /* Create the enumerator. */
15894 build_enumerator (identifier, value, type, loc);
15897 /* Parse a namespace-name.
15899 namespace-name:
15900 original-namespace-name
15901 namespace-alias
15903 Returns the NAMESPACE_DECL for the namespace. */
15905 static tree
15906 cp_parser_namespace_name (cp_parser* parser)
15908 tree identifier;
15909 tree namespace_decl;
15911 cp_token *token = cp_lexer_peek_token (parser->lexer);
15913 /* Get the name of the namespace. */
15914 identifier = cp_parser_identifier (parser);
15915 if (identifier == error_mark_node)
15916 return error_mark_node;
15918 /* Look up the identifier in the currently active scope. Look only
15919 for namespaces, due to:
15921 [basic.lookup.udir]
15923 When looking up a namespace-name in a using-directive or alias
15924 definition, only namespace names are considered.
15926 And:
15928 [basic.lookup.qual]
15930 During the lookup of a name preceding the :: scope resolution
15931 operator, object, function, and enumerator names are ignored.
15933 (Note that cp_parser_qualifying_entity only calls this
15934 function if the token after the name is the scope resolution
15935 operator.) */
15936 namespace_decl = cp_parser_lookup_name (parser, identifier,
15937 none_type,
15938 /*is_template=*/false,
15939 /*is_namespace=*/true,
15940 /*check_dependency=*/true,
15941 /*ambiguous_decls=*/NULL,
15942 token->location);
15943 /* If it's not a namespace, issue an error. */
15944 if (namespace_decl == error_mark_node
15945 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15947 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15948 error_at (token->location, "%qD is not a namespace-name", identifier);
15949 cp_parser_error (parser, "expected namespace-name");
15950 namespace_decl = error_mark_node;
15953 return namespace_decl;
15956 /* Parse a namespace-definition.
15958 namespace-definition:
15959 named-namespace-definition
15960 unnamed-namespace-definition
15962 named-namespace-definition:
15963 original-namespace-definition
15964 extension-namespace-definition
15966 original-namespace-definition:
15967 namespace identifier { namespace-body }
15969 extension-namespace-definition:
15970 namespace original-namespace-name { namespace-body }
15972 unnamed-namespace-definition:
15973 namespace { namespace-body } */
15975 static void
15976 cp_parser_namespace_definition (cp_parser* parser)
15978 tree identifier, attribs;
15979 bool has_visibility;
15980 bool is_inline;
15982 cp_ensure_no_omp_declare_simd (parser);
15983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15985 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15986 is_inline = true;
15987 cp_lexer_consume_token (parser->lexer);
15989 else
15990 is_inline = false;
15992 /* Look for the `namespace' keyword. */
15993 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15995 /* Get the name of the namespace. We do not attempt to distinguish
15996 between an original-namespace-definition and an
15997 extension-namespace-definition at this point. The semantic
15998 analysis routines are responsible for that. */
15999 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16000 identifier = cp_parser_identifier (parser);
16001 else
16002 identifier = NULL_TREE;
16004 /* Parse any specified attributes. */
16005 attribs = cp_parser_attributes_opt (parser);
16007 /* Look for the `{' to start the namespace. */
16008 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16009 /* Start the namespace. */
16010 push_namespace (identifier);
16012 /* "inline namespace" is equivalent to a stub namespace definition
16013 followed by a strong using directive. */
16014 if (is_inline)
16016 tree name_space = current_namespace;
16017 /* Set up namespace association. */
16018 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16019 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16020 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16021 /* Import the contents of the inline namespace. */
16022 pop_namespace ();
16023 do_using_directive (name_space);
16024 push_namespace (identifier);
16027 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16029 /* Parse the body of the namespace. */
16030 cp_parser_namespace_body (parser);
16032 if (has_visibility)
16033 pop_visibility (1);
16035 /* Finish the namespace. */
16036 pop_namespace ();
16037 /* Look for the final `}'. */
16038 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16041 /* Parse a namespace-body.
16043 namespace-body:
16044 declaration-seq [opt] */
16046 static void
16047 cp_parser_namespace_body (cp_parser* parser)
16049 cp_parser_declaration_seq_opt (parser);
16052 /* Parse a namespace-alias-definition.
16054 namespace-alias-definition:
16055 namespace identifier = qualified-namespace-specifier ; */
16057 static void
16058 cp_parser_namespace_alias_definition (cp_parser* parser)
16060 tree identifier;
16061 tree namespace_specifier;
16063 cp_token *token = cp_lexer_peek_token (parser->lexer);
16065 /* Look for the `namespace' keyword. */
16066 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16067 /* Look for the identifier. */
16068 identifier = cp_parser_identifier (parser);
16069 if (identifier == error_mark_node)
16070 return;
16071 /* Look for the `=' token. */
16072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16073 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16075 error_at (token->location, "%<namespace%> definition is not allowed here");
16076 /* Skip the definition. */
16077 cp_lexer_consume_token (parser->lexer);
16078 if (cp_parser_skip_to_closing_brace (parser))
16079 cp_lexer_consume_token (parser->lexer);
16080 return;
16082 cp_parser_require (parser, CPP_EQ, RT_EQ);
16083 /* Look for the qualified-namespace-specifier. */
16084 namespace_specifier
16085 = cp_parser_qualified_namespace_specifier (parser);
16086 /* Look for the `;' token. */
16087 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16089 /* Register the alias in the symbol table. */
16090 do_namespace_alias (identifier, namespace_specifier);
16093 /* Parse a qualified-namespace-specifier.
16095 qualified-namespace-specifier:
16096 :: [opt] nested-name-specifier [opt] namespace-name
16098 Returns a NAMESPACE_DECL corresponding to the specified
16099 namespace. */
16101 static tree
16102 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16104 /* Look for the optional `::'. */
16105 cp_parser_global_scope_opt (parser,
16106 /*current_scope_valid_p=*/false);
16108 /* Look for the optional nested-name-specifier. */
16109 cp_parser_nested_name_specifier_opt (parser,
16110 /*typename_keyword_p=*/false,
16111 /*check_dependency_p=*/true,
16112 /*type_p=*/false,
16113 /*is_declaration=*/true);
16115 return cp_parser_namespace_name (parser);
16118 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16119 access declaration.
16121 using-declaration:
16122 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16123 using :: unqualified-id ;
16125 access-declaration:
16126 qualified-id ;
16130 static bool
16131 cp_parser_using_declaration (cp_parser* parser,
16132 bool access_declaration_p)
16134 cp_token *token;
16135 bool typename_p = false;
16136 bool global_scope_p;
16137 tree decl;
16138 tree identifier;
16139 tree qscope;
16140 int oldcount = errorcount;
16141 cp_token *diag_token = NULL;
16143 if (access_declaration_p)
16145 diag_token = cp_lexer_peek_token (parser->lexer);
16146 cp_parser_parse_tentatively (parser);
16148 else
16150 /* Look for the `using' keyword. */
16151 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16153 /* Peek at the next token. */
16154 token = cp_lexer_peek_token (parser->lexer);
16155 /* See if it's `typename'. */
16156 if (token->keyword == RID_TYPENAME)
16158 /* Remember that we've seen it. */
16159 typename_p = true;
16160 /* Consume the `typename' token. */
16161 cp_lexer_consume_token (parser->lexer);
16165 /* Look for the optional global scope qualification. */
16166 global_scope_p
16167 = (cp_parser_global_scope_opt (parser,
16168 /*current_scope_valid_p=*/false)
16169 != NULL_TREE);
16171 /* If we saw `typename', or didn't see `::', then there must be a
16172 nested-name-specifier present. */
16173 if (typename_p || !global_scope_p)
16175 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16176 /*check_dependency_p=*/true,
16177 /*type_p=*/false,
16178 /*is_declaration=*/true);
16179 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16181 cp_parser_skip_to_end_of_block_or_statement (parser);
16182 return false;
16185 /* Otherwise, we could be in either of the two productions. In that
16186 case, treat the nested-name-specifier as optional. */
16187 else
16188 qscope = cp_parser_nested_name_specifier_opt (parser,
16189 /*typename_keyword_p=*/false,
16190 /*check_dependency_p=*/true,
16191 /*type_p=*/false,
16192 /*is_declaration=*/true);
16193 if (!qscope)
16194 qscope = global_namespace;
16195 else if (UNSCOPED_ENUM_P (qscope))
16196 qscope = CP_TYPE_CONTEXT (qscope);
16198 if (access_declaration_p && cp_parser_error_occurred (parser))
16199 /* Something has already gone wrong; there's no need to parse
16200 further. Since an error has occurred, the return value of
16201 cp_parser_parse_definitely will be false, as required. */
16202 return cp_parser_parse_definitely (parser);
16204 token = cp_lexer_peek_token (parser->lexer);
16205 /* Parse the unqualified-id. */
16206 identifier = cp_parser_unqualified_id (parser,
16207 /*template_keyword_p=*/false,
16208 /*check_dependency_p=*/true,
16209 /*declarator_p=*/true,
16210 /*optional_p=*/false);
16212 if (access_declaration_p)
16214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16215 cp_parser_simulate_error (parser);
16216 if (!cp_parser_parse_definitely (parser))
16217 return false;
16220 /* The function we call to handle a using-declaration is different
16221 depending on what scope we are in. */
16222 if (qscope == error_mark_node || identifier == error_mark_node)
16224 else if (!identifier_p (identifier)
16225 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16226 /* [namespace.udecl]
16228 A using declaration shall not name a template-id. */
16229 error_at (token->location,
16230 "a template-id may not appear in a using-declaration");
16231 else
16233 if (at_class_scope_p ())
16235 /* Create the USING_DECL. */
16236 decl = do_class_using_decl (parser->scope, identifier);
16238 if (decl && typename_p)
16239 USING_DECL_TYPENAME_P (decl) = 1;
16241 if (check_for_bare_parameter_packs (decl))
16243 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16244 return false;
16246 else
16247 /* Add it to the list of members in this class. */
16248 finish_member_declaration (decl);
16250 else
16252 decl = cp_parser_lookup_name_simple (parser,
16253 identifier,
16254 token->location);
16255 if (decl == error_mark_node)
16256 cp_parser_name_lookup_error (parser, identifier,
16257 decl, NLE_NULL,
16258 token->location);
16259 else if (check_for_bare_parameter_packs (decl))
16261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16262 return false;
16264 else if (!at_namespace_scope_p ())
16265 do_local_using_decl (decl, qscope, identifier);
16266 else
16267 do_toplevel_using_decl (decl, qscope, identifier);
16271 /* Look for the final `;'. */
16272 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16274 if (access_declaration_p && errorcount == oldcount)
16275 warning_at (diag_token->location, OPT_Wdeprecated,
16276 "access declarations are deprecated "
16277 "in favour of using-declarations; "
16278 "suggestion: add the %<using%> keyword");
16280 return true;
16283 /* Parse an alias-declaration.
16285 alias-declaration:
16286 using identifier attribute-specifier-seq [opt] = type-id */
16288 static tree
16289 cp_parser_alias_declaration (cp_parser* parser)
16291 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16292 location_t id_location;
16293 cp_declarator *declarator;
16294 cp_decl_specifier_seq decl_specs;
16295 bool member_p;
16296 const char *saved_message = NULL;
16298 /* Look for the `using' keyword. */
16299 cp_token *using_token
16300 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16301 if (using_token == NULL)
16302 return error_mark_node;
16304 id_location = cp_lexer_peek_token (parser->lexer)->location;
16305 id = cp_parser_identifier (parser);
16306 if (id == error_mark_node)
16307 return error_mark_node;
16309 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16310 attributes = cp_parser_attributes_opt (parser);
16311 if (attributes == error_mark_node)
16312 return error_mark_node;
16314 cp_parser_require (parser, CPP_EQ, RT_EQ);
16316 if (cp_parser_error_occurred (parser))
16317 return error_mark_node;
16319 cp_parser_commit_to_tentative_parse (parser);
16321 /* Now we are going to parse the type-id of the declaration. */
16324 [dcl.type]/3 says:
16326 "A type-specifier-seq shall not define a class or enumeration
16327 unless it appears in the type-id of an alias-declaration (7.1.3) that
16328 is not the declaration of a template-declaration."
16330 In other words, if we currently are in an alias template, the
16331 type-id should not define a type.
16333 So let's set parser->type_definition_forbidden_message in that
16334 case; cp_parser_check_type_definition (called by
16335 cp_parser_class_specifier) will then emit an error if a type is
16336 defined in the type-id. */
16337 if (parser->num_template_parameter_lists)
16339 saved_message = parser->type_definition_forbidden_message;
16340 parser->type_definition_forbidden_message =
16341 G_("types may not be defined in alias template declarations");
16344 type = cp_parser_type_id (parser);
16346 /* Restore the error message if need be. */
16347 if (parser->num_template_parameter_lists)
16348 parser->type_definition_forbidden_message = saved_message;
16350 if (type == error_mark_node
16351 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16353 cp_parser_skip_to_end_of_block_or_statement (parser);
16354 return error_mark_node;
16357 /* A typedef-name can also be introduced by an alias-declaration. The
16358 identifier following the using keyword becomes a typedef-name. It has
16359 the same semantics as if it were introduced by the typedef
16360 specifier. In particular, it does not define a new type and it shall
16361 not appear in the type-id. */
16363 clear_decl_specs (&decl_specs);
16364 decl_specs.type = type;
16365 if (attributes != NULL_TREE)
16367 decl_specs.attributes = attributes;
16368 set_and_check_decl_spec_loc (&decl_specs,
16369 ds_attribute,
16370 attrs_token);
16372 set_and_check_decl_spec_loc (&decl_specs,
16373 ds_typedef,
16374 using_token);
16375 set_and_check_decl_spec_loc (&decl_specs,
16376 ds_alias,
16377 using_token);
16379 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16380 declarator->id_loc = id_location;
16382 member_p = at_class_scope_p ();
16383 if (member_p)
16384 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16385 NULL_TREE, attributes);
16386 else
16387 decl = start_decl (declarator, &decl_specs, 0,
16388 attributes, NULL_TREE, &pushed_scope);
16389 if (decl == error_mark_node)
16390 return decl;
16392 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16394 if (pushed_scope)
16395 pop_scope (pushed_scope);
16397 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16398 added into the symbol table; otherwise, return the TYPE_DECL. */
16399 if (DECL_LANG_SPECIFIC (decl)
16400 && DECL_TEMPLATE_INFO (decl)
16401 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16403 decl = DECL_TI_TEMPLATE (decl);
16404 if (member_p)
16405 check_member_template (decl);
16408 return decl;
16411 /* Parse a using-directive.
16413 using-directive:
16414 using namespace :: [opt] nested-name-specifier [opt]
16415 namespace-name ; */
16417 static void
16418 cp_parser_using_directive (cp_parser* parser)
16420 tree namespace_decl;
16421 tree attribs;
16423 /* Look for the `using' keyword. */
16424 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16425 /* And the `namespace' keyword. */
16426 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16427 /* Look for the optional `::' operator. */
16428 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16429 /* And the optional nested-name-specifier. */
16430 cp_parser_nested_name_specifier_opt (parser,
16431 /*typename_keyword_p=*/false,
16432 /*check_dependency_p=*/true,
16433 /*type_p=*/false,
16434 /*is_declaration=*/true);
16435 /* Get the namespace being used. */
16436 namespace_decl = cp_parser_namespace_name (parser);
16437 /* And any specified attributes. */
16438 attribs = cp_parser_attributes_opt (parser);
16439 /* Update the symbol table. */
16440 parse_using_directive (namespace_decl, attribs);
16441 /* Look for the final `;'. */
16442 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16445 /* Parse an asm-definition.
16447 asm-definition:
16448 asm ( string-literal ) ;
16450 GNU Extension:
16452 asm-definition:
16453 asm volatile [opt] ( string-literal ) ;
16454 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16455 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16456 : asm-operand-list [opt] ) ;
16457 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16458 : asm-operand-list [opt]
16459 : asm-clobber-list [opt] ) ;
16460 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16461 : asm-clobber-list [opt]
16462 : asm-goto-list ) ; */
16464 static void
16465 cp_parser_asm_definition (cp_parser* parser)
16467 tree string;
16468 tree outputs = NULL_TREE;
16469 tree inputs = NULL_TREE;
16470 tree clobbers = NULL_TREE;
16471 tree labels = NULL_TREE;
16472 tree asm_stmt;
16473 bool volatile_p = false;
16474 bool extended_p = false;
16475 bool invalid_inputs_p = false;
16476 bool invalid_outputs_p = false;
16477 bool goto_p = false;
16478 required_token missing = RT_NONE;
16480 /* Look for the `asm' keyword. */
16481 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16482 /* See if the next token is `volatile'. */
16483 if (cp_parser_allow_gnu_extensions_p (parser)
16484 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16486 /* Remember that we saw the `volatile' keyword. */
16487 volatile_p = true;
16488 /* Consume the token. */
16489 cp_lexer_consume_token (parser->lexer);
16491 if (cp_parser_allow_gnu_extensions_p (parser)
16492 && parser->in_function_body
16493 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16495 /* Remember that we saw the `goto' keyword. */
16496 goto_p = true;
16497 /* Consume the token. */
16498 cp_lexer_consume_token (parser->lexer);
16500 /* Look for the opening `('. */
16501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16502 return;
16503 /* Look for the string. */
16504 string = cp_parser_string_literal (parser, false, false);
16505 if (string == error_mark_node)
16507 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16508 /*consume_paren=*/true);
16509 return;
16512 /* If we're allowing GNU extensions, check for the extended assembly
16513 syntax. Unfortunately, the `:' tokens need not be separated by
16514 a space in C, and so, for compatibility, we tolerate that here
16515 too. Doing that means that we have to treat the `::' operator as
16516 two `:' tokens. */
16517 if (cp_parser_allow_gnu_extensions_p (parser)
16518 && parser->in_function_body
16519 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16520 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16522 bool inputs_p = false;
16523 bool clobbers_p = false;
16524 bool labels_p = false;
16526 /* The extended syntax was used. */
16527 extended_p = true;
16529 /* Look for outputs. */
16530 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16532 /* Consume the `:'. */
16533 cp_lexer_consume_token (parser->lexer);
16534 /* Parse the output-operands. */
16535 if (cp_lexer_next_token_is_not (parser->lexer,
16536 CPP_COLON)
16537 && cp_lexer_next_token_is_not (parser->lexer,
16538 CPP_SCOPE)
16539 && cp_lexer_next_token_is_not (parser->lexer,
16540 CPP_CLOSE_PAREN)
16541 && !goto_p)
16542 outputs = cp_parser_asm_operand_list (parser);
16544 if (outputs == error_mark_node)
16545 invalid_outputs_p = true;
16547 /* If the next token is `::', there are no outputs, and the
16548 next token is the beginning of the inputs. */
16549 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16550 /* The inputs are coming next. */
16551 inputs_p = true;
16553 /* Look for inputs. */
16554 if (inputs_p
16555 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16557 /* Consume the `:' or `::'. */
16558 cp_lexer_consume_token (parser->lexer);
16559 /* Parse the output-operands. */
16560 if (cp_lexer_next_token_is_not (parser->lexer,
16561 CPP_COLON)
16562 && cp_lexer_next_token_is_not (parser->lexer,
16563 CPP_SCOPE)
16564 && cp_lexer_next_token_is_not (parser->lexer,
16565 CPP_CLOSE_PAREN))
16566 inputs = cp_parser_asm_operand_list (parser);
16568 if (inputs == error_mark_node)
16569 invalid_inputs_p = true;
16571 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16572 /* The clobbers are coming next. */
16573 clobbers_p = true;
16575 /* Look for clobbers. */
16576 if (clobbers_p
16577 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16579 clobbers_p = true;
16580 /* Consume the `:' or `::'. */
16581 cp_lexer_consume_token (parser->lexer);
16582 /* Parse the clobbers. */
16583 if (cp_lexer_next_token_is_not (parser->lexer,
16584 CPP_COLON)
16585 && cp_lexer_next_token_is_not (parser->lexer,
16586 CPP_CLOSE_PAREN))
16587 clobbers = cp_parser_asm_clobber_list (parser);
16589 else if (goto_p
16590 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16591 /* The labels are coming next. */
16592 labels_p = true;
16594 /* Look for labels. */
16595 if (labels_p
16596 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16598 labels_p = true;
16599 /* Consume the `:' or `::'. */
16600 cp_lexer_consume_token (parser->lexer);
16601 /* Parse the labels. */
16602 labels = cp_parser_asm_label_list (parser);
16605 if (goto_p && !labels_p)
16606 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16608 else if (goto_p)
16609 missing = RT_COLON_SCOPE;
16611 /* Look for the closing `)'. */
16612 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16613 missing ? missing : RT_CLOSE_PAREN))
16614 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16615 /*consume_paren=*/true);
16616 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16618 if (!invalid_inputs_p && !invalid_outputs_p)
16620 /* Create the ASM_EXPR. */
16621 if (parser->in_function_body)
16623 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16624 inputs, clobbers, labels);
16625 /* If the extended syntax was not used, mark the ASM_EXPR. */
16626 if (!extended_p)
16628 tree temp = asm_stmt;
16629 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16630 temp = TREE_OPERAND (temp, 0);
16632 ASM_INPUT_P (temp) = 1;
16635 else
16636 symtab->finalize_toplevel_asm (string);
16640 /* Declarators [gram.dcl.decl] */
16642 /* Parse an init-declarator.
16644 init-declarator:
16645 declarator initializer [opt]
16647 GNU Extension:
16649 init-declarator:
16650 declarator asm-specification [opt] attributes [opt] initializer [opt]
16652 function-definition:
16653 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16654 function-body
16655 decl-specifier-seq [opt] declarator function-try-block
16657 GNU Extension:
16659 function-definition:
16660 __extension__ function-definition
16662 TM Extension:
16664 function-definition:
16665 decl-specifier-seq [opt] declarator function-transaction-block
16667 The DECL_SPECIFIERS apply to this declarator. Returns a
16668 representation of the entity declared. If MEMBER_P is TRUE, then
16669 this declarator appears in a class scope. The new DECL created by
16670 this declarator is returned.
16672 The CHECKS are access checks that should be performed once we know
16673 what entity is being declared (and, therefore, what classes have
16674 befriended it).
16676 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16677 for a function-definition here as well. If the declarator is a
16678 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16679 be TRUE upon return. By that point, the function-definition will
16680 have been completely parsed.
16682 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16683 is FALSE.
16685 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16686 parsed declaration if it is an uninitialized single declarator not followed
16687 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16688 if present, will not be consumed. If returned, this declarator will be
16689 created with SD_INITIALIZED but will not call cp_finish_decl. */
16691 static tree
16692 cp_parser_init_declarator (cp_parser* parser,
16693 cp_decl_specifier_seq *decl_specifiers,
16694 vec<deferred_access_check, va_gc> *checks,
16695 bool function_definition_allowed_p,
16696 bool member_p,
16697 int declares_class_or_enum,
16698 bool* function_definition_p,
16699 tree* maybe_range_for_decl)
16701 cp_token *token = NULL, *asm_spec_start_token = NULL,
16702 *attributes_start_token = NULL;
16703 cp_declarator *declarator;
16704 tree prefix_attributes;
16705 tree attributes = NULL;
16706 tree asm_specification;
16707 tree initializer;
16708 tree decl = NULL_TREE;
16709 tree scope;
16710 int is_initialized;
16711 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16712 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16713 "(...)". */
16714 enum cpp_ttype initialization_kind;
16715 bool is_direct_init = false;
16716 bool is_non_constant_init;
16717 int ctor_dtor_or_conv_p;
16718 bool friend_p = cp_parser_friend_p (decl_specifiers);
16719 tree pushed_scope = NULL_TREE;
16720 bool range_for_decl_p = false;
16721 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16723 /* Gather the attributes that were provided with the
16724 decl-specifiers. */
16725 prefix_attributes = decl_specifiers->attributes;
16727 /* Assume that this is not the declarator for a function
16728 definition. */
16729 if (function_definition_p)
16730 *function_definition_p = false;
16732 /* Default arguments are only permitted for function parameters. */
16733 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16734 parser->default_arg_ok_p = false;
16736 /* Defer access checks while parsing the declarator; we cannot know
16737 what names are accessible until we know what is being
16738 declared. */
16739 resume_deferring_access_checks ();
16741 /* Parse the declarator. */
16742 token = cp_lexer_peek_token (parser->lexer);
16743 declarator
16744 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16745 &ctor_dtor_or_conv_p,
16746 /*parenthesized_p=*/NULL,
16747 member_p, friend_p);
16748 /* Gather up the deferred checks. */
16749 stop_deferring_access_checks ();
16751 parser->default_arg_ok_p = saved_default_arg_ok_p;
16753 /* If the DECLARATOR was erroneous, there's no need to go
16754 further. */
16755 if (declarator == cp_error_declarator)
16756 return error_mark_node;
16758 /* Check that the number of template-parameter-lists is OK. */
16759 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16760 token->location))
16761 return error_mark_node;
16763 if (declares_class_or_enum & 2)
16764 cp_parser_check_for_definition_in_return_type (declarator,
16765 decl_specifiers->type,
16766 decl_specifiers->locations[ds_type_spec]);
16768 /* Figure out what scope the entity declared by the DECLARATOR is
16769 located in. `grokdeclarator' sometimes changes the scope, so
16770 we compute it now. */
16771 scope = get_scope_of_declarator (declarator);
16773 /* Perform any lookups in the declared type which were thought to be
16774 dependent, but are not in the scope of the declarator. */
16775 decl_specifiers->type
16776 = maybe_update_decl_type (decl_specifiers->type, scope);
16778 /* If we're allowing GNU extensions, look for an
16779 asm-specification. */
16780 if (cp_parser_allow_gnu_extensions_p (parser))
16782 /* Look for an asm-specification. */
16783 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16784 asm_specification = cp_parser_asm_specification_opt (parser);
16786 else
16787 asm_specification = NULL_TREE;
16789 /* Look for attributes. */
16790 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16791 attributes = cp_parser_attributes_opt (parser);
16793 /* Peek at the next token. */
16794 token = cp_lexer_peek_token (parser->lexer);
16796 bool bogus_implicit_tmpl = false;
16798 if (function_declarator_p (declarator))
16800 /* Check to see if the token indicates the start of a
16801 function-definition. */
16802 if (cp_parser_token_starts_function_definition_p (token))
16804 if (!function_definition_allowed_p)
16806 /* If a function-definition should not appear here, issue an
16807 error message. */
16808 cp_parser_error (parser,
16809 "a function-definition is not allowed here");
16810 return error_mark_node;
16813 location_t func_brace_location
16814 = cp_lexer_peek_token (parser->lexer)->location;
16816 /* Neither attributes nor an asm-specification are allowed
16817 on a function-definition. */
16818 if (asm_specification)
16819 error_at (asm_spec_start_token->location,
16820 "an asm-specification is not allowed "
16821 "on a function-definition");
16822 if (attributes)
16823 error_at (attributes_start_token->location,
16824 "attributes are not allowed "
16825 "on a function-definition");
16826 /* This is a function-definition. */
16827 *function_definition_p = true;
16829 /* Parse the function definition. */
16830 if (member_p)
16831 decl = cp_parser_save_member_function_body (parser,
16832 decl_specifiers,
16833 declarator,
16834 prefix_attributes);
16835 else
16836 decl =
16837 (cp_parser_function_definition_from_specifiers_and_declarator
16838 (parser, decl_specifiers, prefix_attributes, declarator));
16840 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16842 /* This is where the prologue starts... */
16843 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16844 = func_brace_location;
16847 return decl;
16850 else if (parser->fully_implicit_function_template_p)
16852 /* A non-template declaration involving a function parameter list
16853 containing an implicit template parameter will be made into a
16854 template. If the resulting declaration is not going to be an
16855 actual function then finish the template scope here to prevent it.
16856 An error message will be issued once we have a decl to talk about.
16858 FIXME probably we should do type deduction rather than create an
16859 implicit template, but the standard currently doesn't allow it. */
16860 bogus_implicit_tmpl = true;
16861 finish_fully_implicit_template (parser, NULL_TREE);
16864 /* [dcl.dcl]
16866 Only in function declarations for constructors, destructors, and
16867 type conversions can the decl-specifier-seq be omitted.
16869 We explicitly postpone this check past the point where we handle
16870 function-definitions because we tolerate function-definitions
16871 that are missing their return types in some modes. */
16872 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16874 cp_parser_error (parser,
16875 "expected constructor, destructor, or type conversion");
16876 return error_mark_node;
16879 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16880 if (token->type == CPP_EQ
16881 || token->type == CPP_OPEN_PAREN
16882 || token->type == CPP_OPEN_BRACE)
16884 is_initialized = SD_INITIALIZED;
16885 initialization_kind = token->type;
16886 if (maybe_range_for_decl)
16887 *maybe_range_for_decl = error_mark_node;
16889 if (token->type == CPP_EQ
16890 && function_declarator_p (declarator))
16892 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16893 if (t2->keyword == RID_DEFAULT)
16894 is_initialized = SD_DEFAULTED;
16895 else if (t2->keyword == RID_DELETE)
16896 is_initialized = SD_DELETED;
16899 else
16901 /* If the init-declarator isn't initialized and isn't followed by a
16902 `,' or `;', it's not a valid init-declarator. */
16903 if (token->type != CPP_COMMA
16904 && token->type != CPP_SEMICOLON)
16906 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16907 range_for_decl_p = true;
16908 else
16910 cp_parser_error (parser, "expected initializer");
16911 return error_mark_node;
16914 is_initialized = SD_UNINITIALIZED;
16915 initialization_kind = CPP_EOF;
16918 /* Because start_decl has side-effects, we should only call it if we
16919 know we're going ahead. By this point, we know that we cannot
16920 possibly be looking at any other construct. */
16921 cp_parser_commit_to_tentative_parse (parser);
16923 /* Enter the newly declared entry in the symbol table. If we're
16924 processing a declaration in a class-specifier, we wait until
16925 after processing the initializer. */
16926 if (!member_p)
16928 if (parser->in_unbraced_linkage_specification_p)
16929 decl_specifiers->storage_class = sc_extern;
16930 decl = start_decl (declarator, decl_specifiers,
16931 range_for_decl_p? SD_INITIALIZED : is_initialized,
16932 attributes, prefix_attributes, &pushed_scope);
16933 cp_finalize_omp_declare_simd (parser, decl);
16934 /* Adjust location of decl if declarator->id_loc is more appropriate:
16935 set, and decl wasn't merged with another decl, in which case its
16936 location would be different from input_location, and more accurate. */
16937 if (DECL_P (decl)
16938 && declarator->id_loc != UNKNOWN_LOCATION
16939 && DECL_SOURCE_LOCATION (decl) == input_location)
16940 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16942 else if (scope)
16943 /* Enter the SCOPE. That way unqualified names appearing in the
16944 initializer will be looked up in SCOPE. */
16945 pushed_scope = push_scope (scope);
16947 /* Perform deferred access control checks, now that we know in which
16948 SCOPE the declared entity resides. */
16949 if (!member_p && decl)
16951 tree saved_current_function_decl = NULL_TREE;
16953 /* If the entity being declared is a function, pretend that we
16954 are in its scope. If it is a `friend', it may have access to
16955 things that would not otherwise be accessible. */
16956 if (TREE_CODE (decl) == FUNCTION_DECL)
16958 saved_current_function_decl = current_function_decl;
16959 current_function_decl = decl;
16962 /* Perform access checks for template parameters. */
16963 cp_parser_perform_template_parameter_access_checks (checks);
16965 /* Perform the access control checks for the declarator and the
16966 decl-specifiers. */
16967 perform_deferred_access_checks (tf_warning_or_error);
16969 /* Restore the saved value. */
16970 if (TREE_CODE (decl) == FUNCTION_DECL)
16971 current_function_decl = saved_current_function_decl;
16974 /* Parse the initializer. */
16975 initializer = NULL_TREE;
16976 is_direct_init = false;
16977 is_non_constant_init = true;
16978 if (is_initialized)
16980 if (function_declarator_p (declarator))
16982 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16983 if (initialization_kind == CPP_EQ)
16984 initializer = cp_parser_pure_specifier (parser);
16985 else
16987 /* If the declaration was erroneous, we don't really
16988 know what the user intended, so just silently
16989 consume the initializer. */
16990 if (decl != error_mark_node)
16991 error_at (initializer_start_token->location,
16992 "initializer provided for function");
16993 cp_parser_skip_to_closing_parenthesis (parser,
16994 /*recovering=*/true,
16995 /*or_comma=*/false,
16996 /*consume_paren=*/true);
16999 else
17001 /* We want to record the extra mangling scope for in-class
17002 initializers of class members and initializers of static data
17003 member templates. The former involves deferring
17004 parsing of the initializer until end of class as with default
17005 arguments. So right here we only handle the latter. */
17006 if (!member_p && processing_template_decl)
17007 start_lambda_scope (decl);
17008 initializer = cp_parser_initializer (parser,
17009 &is_direct_init,
17010 &is_non_constant_init);
17011 if (!member_p && processing_template_decl)
17012 finish_lambda_scope ();
17013 if (initializer == error_mark_node)
17014 cp_parser_skip_to_end_of_statement (parser);
17018 /* The old parser allows attributes to appear after a parenthesized
17019 initializer. Mark Mitchell proposed removing this functionality
17020 on the GCC mailing lists on 2002-08-13. This parser accepts the
17021 attributes -- but ignores them. */
17022 if (cp_parser_allow_gnu_extensions_p (parser)
17023 && initialization_kind == CPP_OPEN_PAREN)
17024 if (cp_parser_attributes_opt (parser))
17025 warning (OPT_Wattributes,
17026 "attributes after parenthesized initializer ignored");
17028 /* And now complain about a non-function implicit template. */
17029 if (bogus_implicit_tmpl)
17030 error_at (DECL_SOURCE_LOCATION (decl),
17031 "non-function %qD declared as implicit template", decl);
17033 /* For an in-class declaration, use `grokfield' to create the
17034 declaration. */
17035 if (member_p)
17037 if (pushed_scope)
17039 pop_scope (pushed_scope);
17040 pushed_scope = NULL_TREE;
17042 decl = grokfield (declarator, decl_specifiers,
17043 initializer, !is_non_constant_init,
17044 /*asmspec=*/NULL_TREE,
17045 chainon (attributes, prefix_attributes));
17046 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17047 cp_parser_save_default_args (parser, decl);
17048 cp_finalize_omp_declare_simd (parser, decl);
17051 /* Finish processing the declaration. But, skip member
17052 declarations. */
17053 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17055 cp_finish_decl (decl,
17056 initializer, !is_non_constant_init,
17057 asm_specification,
17058 /* If the initializer is in parentheses, then this is
17059 a direct-initialization, which means that an
17060 `explicit' constructor is OK. Otherwise, an
17061 `explicit' constructor cannot be used. */
17062 ((is_direct_init || !is_initialized)
17063 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17065 else if ((cxx_dialect != cxx98) && friend_p
17066 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17067 /* Core issue #226 (C++0x only): A default template-argument
17068 shall not be specified in a friend class template
17069 declaration. */
17070 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17071 /*is_partial=*/false, /*is_friend_decl=*/1);
17073 if (!friend_p && pushed_scope)
17074 pop_scope (pushed_scope);
17076 if (function_declarator_p (declarator)
17077 && parser->fully_implicit_function_template_p)
17079 if (member_p)
17080 decl = finish_fully_implicit_template (parser, decl);
17081 else
17082 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17085 return decl;
17088 /* Parse a declarator.
17090 declarator:
17091 direct-declarator
17092 ptr-operator declarator
17094 abstract-declarator:
17095 ptr-operator abstract-declarator [opt]
17096 direct-abstract-declarator
17098 GNU Extensions:
17100 declarator:
17101 attributes [opt] direct-declarator
17102 attributes [opt] ptr-operator declarator
17104 abstract-declarator:
17105 attributes [opt] ptr-operator abstract-declarator [opt]
17106 attributes [opt] direct-abstract-declarator
17108 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17109 detect constructor, destructor or conversion operators. It is set
17110 to -1 if the declarator is a name, and +1 if it is a
17111 function. Otherwise it is set to zero. Usually you just want to
17112 test for >0, but internally the negative value is used.
17114 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17115 a decl-specifier-seq unless it declares a constructor, destructor,
17116 or conversion. It might seem that we could check this condition in
17117 semantic analysis, rather than parsing, but that makes it difficult
17118 to handle something like `f()'. We want to notice that there are
17119 no decl-specifiers, and therefore realize that this is an
17120 expression, not a declaration.)
17122 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17123 the declarator is a direct-declarator of the form "(...)".
17125 MEMBER_P is true iff this declarator is a member-declarator.
17127 FRIEND_P is true iff this declarator is a friend. */
17129 static cp_declarator *
17130 cp_parser_declarator (cp_parser* parser,
17131 cp_parser_declarator_kind dcl_kind,
17132 int* ctor_dtor_or_conv_p,
17133 bool* parenthesized_p,
17134 bool member_p, bool friend_p)
17136 cp_declarator *declarator;
17137 enum tree_code code;
17138 cp_cv_quals cv_quals;
17139 tree class_type;
17140 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17142 /* Assume this is not a constructor, destructor, or type-conversion
17143 operator. */
17144 if (ctor_dtor_or_conv_p)
17145 *ctor_dtor_or_conv_p = 0;
17147 if (cp_parser_allow_gnu_extensions_p (parser))
17148 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17150 /* Check for the ptr-operator production. */
17151 cp_parser_parse_tentatively (parser);
17152 /* Parse the ptr-operator. */
17153 code = cp_parser_ptr_operator (parser,
17154 &class_type,
17155 &cv_quals,
17156 &std_attributes);
17158 /* If that worked, then we have a ptr-operator. */
17159 if (cp_parser_parse_definitely (parser))
17161 /* If a ptr-operator was found, then this declarator was not
17162 parenthesized. */
17163 if (parenthesized_p)
17164 *parenthesized_p = true;
17165 /* The dependent declarator is optional if we are parsing an
17166 abstract-declarator. */
17167 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17168 cp_parser_parse_tentatively (parser);
17170 /* Parse the dependent declarator. */
17171 declarator = cp_parser_declarator (parser, dcl_kind,
17172 /*ctor_dtor_or_conv_p=*/NULL,
17173 /*parenthesized_p=*/NULL,
17174 /*member_p=*/false,
17175 friend_p);
17177 /* If we are parsing an abstract-declarator, we must handle the
17178 case where the dependent declarator is absent. */
17179 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17180 && !cp_parser_parse_definitely (parser))
17181 declarator = NULL;
17183 declarator = cp_parser_make_indirect_declarator
17184 (code, class_type, cv_quals, declarator, std_attributes);
17186 /* Everything else is a direct-declarator. */
17187 else
17189 if (parenthesized_p)
17190 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17191 CPP_OPEN_PAREN);
17192 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17193 ctor_dtor_or_conv_p,
17194 member_p, friend_p);
17197 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17198 declarator->attributes = gnu_attributes;
17199 return declarator;
17202 /* Parse a direct-declarator or direct-abstract-declarator.
17204 direct-declarator:
17205 declarator-id
17206 direct-declarator ( parameter-declaration-clause )
17207 cv-qualifier-seq [opt]
17208 ref-qualifier [opt]
17209 exception-specification [opt]
17210 direct-declarator [ constant-expression [opt] ]
17211 ( declarator )
17213 direct-abstract-declarator:
17214 direct-abstract-declarator [opt]
17215 ( parameter-declaration-clause )
17216 cv-qualifier-seq [opt]
17217 ref-qualifier [opt]
17218 exception-specification [opt]
17219 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17220 ( abstract-declarator )
17222 Returns a representation of the declarator. DCL_KIND is
17223 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17224 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17225 we are parsing a direct-declarator. It is
17226 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17227 of ambiguity we prefer an abstract declarator, as per
17228 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17229 as for cp_parser_declarator. */
17231 static cp_declarator *
17232 cp_parser_direct_declarator (cp_parser* parser,
17233 cp_parser_declarator_kind dcl_kind,
17234 int* ctor_dtor_or_conv_p,
17235 bool member_p, bool friend_p)
17237 cp_token *token;
17238 cp_declarator *declarator = NULL;
17239 tree scope = NULL_TREE;
17240 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17241 bool saved_in_declarator_p = parser->in_declarator_p;
17242 bool first = true;
17243 tree pushed_scope = NULL_TREE;
17245 while (true)
17247 /* Peek at the next token. */
17248 token = cp_lexer_peek_token (parser->lexer);
17249 if (token->type == CPP_OPEN_PAREN)
17251 /* This is either a parameter-declaration-clause, or a
17252 parenthesized declarator. When we know we are parsing a
17253 named declarator, it must be a parenthesized declarator
17254 if FIRST is true. For instance, `(int)' is a
17255 parameter-declaration-clause, with an omitted
17256 direct-abstract-declarator. But `((*))', is a
17257 parenthesized abstract declarator. Finally, when T is a
17258 template parameter `(T)' is a
17259 parameter-declaration-clause, and not a parenthesized
17260 named declarator.
17262 We first try and parse a parameter-declaration-clause,
17263 and then try a nested declarator (if FIRST is true).
17265 It is not an error for it not to be a
17266 parameter-declaration-clause, even when FIRST is
17267 false. Consider,
17269 int i (int);
17270 int i (3);
17272 The first is the declaration of a function while the
17273 second is the definition of a variable, including its
17274 initializer.
17276 Having seen only the parenthesis, we cannot know which of
17277 these two alternatives should be selected. Even more
17278 complex are examples like:
17280 int i (int (a));
17281 int i (int (3));
17283 The former is a function-declaration; the latter is a
17284 variable initialization.
17286 Thus again, we try a parameter-declaration-clause, and if
17287 that fails, we back out and return. */
17289 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17291 tree params;
17292 bool is_declarator = false;
17294 /* In a member-declarator, the only valid interpretation
17295 of a parenthesis is the start of a
17296 parameter-declaration-clause. (It is invalid to
17297 initialize a static data member with a parenthesized
17298 initializer; only the "=" form of initialization is
17299 permitted.) */
17300 if (!member_p)
17301 cp_parser_parse_tentatively (parser);
17303 /* Consume the `('. */
17304 cp_lexer_consume_token (parser->lexer);
17305 if (first)
17307 /* If this is going to be an abstract declarator, we're
17308 in a declarator and we can't have default args. */
17309 parser->default_arg_ok_p = false;
17310 parser->in_declarator_p = true;
17313 begin_scope (sk_function_parms, NULL_TREE);
17315 /* Parse the parameter-declaration-clause. */
17316 params = cp_parser_parameter_declaration_clause (parser);
17318 /* Consume the `)'. */
17319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17321 /* If all went well, parse the cv-qualifier-seq,
17322 ref-qualifier and the exception-specification. */
17323 if (member_p || cp_parser_parse_definitely (parser))
17325 cp_cv_quals cv_quals;
17326 cp_virt_specifiers virt_specifiers;
17327 cp_ref_qualifier ref_qual;
17328 tree exception_specification;
17329 tree late_return;
17330 tree attrs;
17331 bool memfn = (member_p || (pushed_scope
17332 && CLASS_TYPE_P (pushed_scope)));
17334 is_declarator = true;
17336 if (ctor_dtor_or_conv_p)
17337 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17338 first = false;
17340 /* Parse the cv-qualifier-seq. */
17341 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17342 /* Parse the ref-qualifier. */
17343 ref_qual = cp_parser_ref_qualifier_opt (parser);
17344 /* And the exception-specification. */
17345 exception_specification
17346 = cp_parser_exception_specification_opt (parser);
17348 attrs = cp_parser_std_attribute_spec_seq (parser);
17350 /* In here, we handle cases where attribute is used after
17351 the function declaration. For example:
17352 void func (int x) __attribute__((vector(..))); */
17353 if (flag_cilkplus
17354 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17356 cp_parser_parse_tentatively (parser);
17357 tree attr = cp_parser_gnu_attributes_opt (parser);
17358 if (cp_lexer_next_token_is_not (parser->lexer,
17359 CPP_SEMICOLON)
17360 && cp_lexer_next_token_is_not (parser->lexer,
17361 CPP_OPEN_BRACE))
17362 cp_parser_abort_tentative_parse (parser);
17363 else if (!cp_parser_parse_definitely (parser))
17365 else
17366 attrs = chainon (attr, attrs);
17368 late_return = (cp_parser_late_return_type_opt
17369 (parser, declarator,
17370 memfn ? cv_quals : -1));
17373 /* Parse the virt-specifier-seq. */
17374 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17376 /* Create the function-declarator. */
17377 declarator = make_call_declarator (declarator,
17378 params,
17379 cv_quals,
17380 virt_specifiers,
17381 ref_qual,
17382 exception_specification,
17383 late_return);
17384 declarator->std_attributes = attrs;
17385 /* Any subsequent parameter lists are to do with
17386 return type, so are not those of the declared
17387 function. */
17388 parser->default_arg_ok_p = false;
17391 /* Remove the function parms from scope. */
17392 pop_bindings_and_leave_scope ();
17394 if (is_declarator)
17395 /* Repeat the main loop. */
17396 continue;
17399 /* If this is the first, we can try a parenthesized
17400 declarator. */
17401 if (first)
17403 bool saved_in_type_id_in_expr_p;
17405 parser->default_arg_ok_p = saved_default_arg_ok_p;
17406 parser->in_declarator_p = saved_in_declarator_p;
17408 /* Consume the `('. */
17409 cp_lexer_consume_token (parser->lexer);
17410 /* Parse the nested declarator. */
17411 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17412 parser->in_type_id_in_expr_p = true;
17413 declarator
17414 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17415 /*parenthesized_p=*/NULL,
17416 member_p, friend_p);
17417 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17418 first = false;
17419 /* Expect a `)'. */
17420 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17421 declarator = cp_error_declarator;
17422 if (declarator == cp_error_declarator)
17423 break;
17425 goto handle_declarator;
17427 /* Otherwise, we must be done. */
17428 else
17429 break;
17431 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17432 && token->type == CPP_OPEN_SQUARE
17433 && !cp_next_tokens_can_be_attribute_p (parser))
17435 /* Parse an array-declarator. */
17436 tree bounds, attrs;
17438 if (ctor_dtor_or_conv_p)
17439 *ctor_dtor_or_conv_p = 0;
17441 first = false;
17442 parser->default_arg_ok_p = false;
17443 parser->in_declarator_p = true;
17444 /* Consume the `['. */
17445 cp_lexer_consume_token (parser->lexer);
17446 /* Peek at the next token. */
17447 token = cp_lexer_peek_token (parser->lexer);
17448 /* If the next token is `]', then there is no
17449 constant-expression. */
17450 if (token->type != CPP_CLOSE_SQUARE)
17452 bool non_constant_p;
17453 bounds
17454 = cp_parser_constant_expression (parser,
17455 /*allow_non_constant=*/true,
17456 &non_constant_p);
17457 if (!non_constant_p)
17458 /* OK */;
17459 else if (error_operand_p (bounds))
17460 /* Already gave an error. */;
17461 else if (!parser->in_function_body
17462 || current_binding_level->kind == sk_function_parms)
17464 /* Normally, the array bound must be an integral constant
17465 expression. However, as an extension, we allow VLAs
17466 in function scopes as long as they aren't part of a
17467 parameter declaration. */
17468 cp_parser_error (parser,
17469 "array bound is not an integer constant");
17470 bounds = error_mark_node;
17472 else if (processing_template_decl
17473 && !type_dependent_expression_p (bounds))
17475 /* Remember this wasn't a constant-expression. */
17476 bounds = build_nop (TREE_TYPE (bounds), bounds);
17477 TREE_SIDE_EFFECTS (bounds) = 1;
17480 else
17481 bounds = NULL_TREE;
17482 /* Look for the closing `]'. */
17483 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17485 declarator = cp_error_declarator;
17486 break;
17489 attrs = cp_parser_std_attribute_spec_seq (parser);
17490 declarator = make_array_declarator (declarator, bounds);
17491 declarator->std_attributes = attrs;
17493 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17496 tree qualifying_scope;
17497 tree unqualified_name;
17498 tree attrs;
17499 special_function_kind sfk;
17500 bool abstract_ok;
17501 bool pack_expansion_p = false;
17502 cp_token *declarator_id_start_token;
17504 /* Parse a declarator-id */
17505 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17506 if (abstract_ok)
17508 cp_parser_parse_tentatively (parser);
17510 /* If we see an ellipsis, we should be looking at a
17511 parameter pack. */
17512 if (token->type == CPP_ELLIPSIS)
17514 /* Consume the `...' */
17515 cp_lexer_consume_token (parser->lexer);
17517 pack_expansion_p = true;
17521 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17522 unqualified_name
17523 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17524 qualifying_scope = parser->scope;
17525 if (abstract_ok)
17527 bool okay = false;
17529 if (!unqualified_name && pack_expansion_p)
17531 /* Check whether an error occurred. */
17532 okay = !cp_parser_error_occurred (parser);
17534 /* We already consumed the ellipsis to mark a
17535 parameter pack, but we have no way to report it,
17536 so abort the tentative parse. We will be exiting
17537 immediately anyway. */
17538 cp_parser_abort_tentative_parse (parser);
17540 else
17541 okay = cp_parser_parse_definitely (parser);
17543 if (!okay)
17544 unqualified_name = error_mark_node;
17545 else if (unqualified_name
17546 && (qualifying_scope
17547 || (!identifier_p (unqualified_name))))
17549 cp_parser_error (parser, "expected unqualified-id");
17550 unqualified_name = error_mark_node;
17554 if (!unqualified_name)
17555 return NULL;
17556 if (unqualified_name == error_mark_node)
17558 declarator = cp_error_declarator;
17559 pack_expansion_p = false;
17560 declarator->parameter_pack_p = false;
17561 break;
17564 attrs = cp_parser_std_attribute_spec_seq (parser);
17566 if (qualifying_scope && at_namespace_scope_p ()
17567 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17569 /* In the declaration of a member of a template class
17570 outside of the class itself, the SCOPE will sometimes
17571 be a TYPENAME_TYPE. For example, given:
17573 template <typename T>
17574 int S<T>::R::i = 3;
17576 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17577 this context, we must resolve S<T>::R to an ordinary
17578 type, rather than a typename type.
17580 The reason we normally avoid resolving TYPENAME_TYPEs
17581 is that a specialization of `S' might render
17582 `S<T>::R' not a type. However, if `S' is
17583 specialized, then this `i' will not be used, so there
17584 is no harm in resolving the types here. */
17585 tree type;
17587 /* Resolve the TYPENAME_TYPE. */
17588 type = resolve_typename_type (qualifying_scope,
17589 /*only_current_p=*/false);
17590 /* If that failed, the declarator is invalid. */
17591 if (TREE_CODE (type) == TYPENAME_TYPE)
17593 if (typedef_variant_p (type))
17594 error_at (declarator_id_start_token->location,
17595 "cannot define member of dependent typedef "
17596 "%qT", type);
17597 else
17598 error_at (declarator_id_start_token->location,
17599 "%<%T::%E%> is not a type",
17600 TYPE_CONTEXT (qualifying_scope),
17601 TYPE_IDENTIFIER (qualifying_scope));
17603 qualifying_scope = type;
17606 sfk = sfk_none;
17608 if (unqualified_name)
17610 tree class_type;
17612 if (qualifying_scope
17613 && CLASS_TYPE_P (qualifying_scope))
17614 class_type = qualifying_scope;
17615 else
17616 class_type = current_class_type;
17618 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17620 tree name_type = TREE_TYPE (unqualified_name);
17621 if (class_type && same_type_p (name_type, class_type))
17623 if (qualifying_scope
17624 && CLASSTYPE_USE_TEMPLATE (name_type))
17626 error_at (declarator_id_start_token->location,
17627 "invalid use of constructor as a template");
17628 inform (declarator_id_start_token->location,
17629 "use %<%T::%D%> instead of %<%T::%D%> to "
17630 "name the constructor in a qualified name",
17631 class_type,
17632 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17633 class_type, name_type);
17634 declarator = cp_error_declarator;
17635 break;
17637 else
17638 unqualified_name = constructor_name (class_type);
17640 else
17642 /* We do not attempt to print the declarator
17643 here because we do not have enough
17644 information about its original syntactic
17645 form. */
17646 cp_parser_error (parser, "invalid declarator");
17647 declarator = cp_error_declarator;
17648 break;
17652 if (class_type)
17654 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17655 sfk = sfk_destructor;
17656 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17657 sfk = sfk_conversion;
17658 else if (/* There's no way to declare a constructor
17659 for an anonymous type, even if the type
17660 got a name for linkage purposes. */
17661 !TYPE_WAS_ANONYMOUS (class_type)
17662 /* Handle correctly (c++/19200):
17664 struct S {
17665 struct T{};
17666 friend void S(T);
17669 and also:
17671 namespace N {
17672 void S();
17675 struct S {
17676 friend void N::S();
17677 }; */
17678 && !(friend_p
17679 && class_type != qualifying_scope)
17680 && constructor_name_p (unqualified_name,
17681 class_type))
17683 unqualified_name = constructor_name (class_type);
17684 sfk = sfk_constructor;
17686 else if (is_overloaded_fn (unqualified_name)
17687 && DECL_CONSTRUCTOR_P (get_first_fn
17688 (unqualified_name)))
17689 sfk = sfk_constructor;
17691 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17692 *ctor_dtor_or_conv_p = -1;
17695 declarator = make_id_declarator (qualifying_scope,
17696 unqualified_name,
17697 sfk);
17698 declarator->std_attributes = attrs;
17699 declarator->id_loc = token->location;
17700 declarator->parameter_pack_p = pack_expansion_p;
17702 if (pack_expansion_p)
17703 maybe_warn_variadic_templates ();
17706 handle_declarator:;
17707 scope = get_scope_of_declarator (declarator);
17708 if (scope)
17710 /* Any names that appear after the declarator-id for a
17711 member are looked up in the containing scope. */
17712 if (at_function_scope_p ())
17714 /* But declarations with qualified-ids can't appear in a
17715 function. */
17716 cp_parser_error (parser, "qualified-id in declaration");
17717 declarator = cp_error_declarator;
17718 break;
17720 pushed_scope = push_scope (scope);
17722 parser->in_declarator_p = true;
17723 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17724 || (declarator && declarator->kind == cdk_id))
17725 /* Default args are only allowed on function
17726 declarations. */
17727 parser->default_arg_ok_p = saved_default_arg_ok_p;
17728 else
17729 parser->default_arg_ok_p = false;
17731 first = false;
17733 /* We're done. */
17734 else
17735 break;
17738 /* For an abstract declarator, we might wind up with nothing at this
17739 point. That's an error; the declarator is not optional. */
17740 if (!declarator)
17741 cp_parser_error (parser, "expected declarator");
17743 /* If we entered a scope, we must exit it now. */
17744 if (pushed_scope)
17745 pop_scope (pushed_scope);
17747 parser->default_arg_ok_p = saved_default_arg_ok_p;
17748 parser->in_declarator_p = saved_in_declarator_p;
17750 return declarator;
17753 /* Parse a ptr-operator.
17755 ptr-operator:
17756 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17757 * cv-qualifier-seq [opt]
17759 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17760 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17762 GNU Extension:
17764 ptr-operator:
17765 & cv-qualifier-seq [opt]
17767 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17768 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17769 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17770 filled in with the TYPE containing the member. *CV_QUALS is
17771 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17772 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17773 Note that the tree codes returned by this function have nothing
17774 to do with the types of trees that will be eventually be created
17775 to represent the pointer or reference type being parsed. They are
17776 just constants with suggestive names. */
17777 static enum tree_code
17778 cp_parser_ptr_operator (cp_parser* parser,
17779 tree* type,
17780 cp_cv_quals *cv_quals,
17781 tree *attributes)
17783 enum tree_code code = ERROR_MARK;
17784 cp_token *token;
17785 tree attrs = NULL_TREE;
17787 /* Assume that it's not a pointer-to-member. */
17788 *type = NULL_TREE;
17789 /* And that there are no cv-qualifiers. */
17790 *cv_quals = TYPE_UNQUALIFIED;
17792 /* Peek at the next token. */
17793 token = cp_lexer_peek_token (parser->lexer);
17795 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17796 if (token->type == CPP_MULT)
17797 code = INDIRECT_REF;
17798 else if (token->type == CPP_AND)
17799 code = ADDR_EXPR;
17800 else if ((cxx_dialect != cxx98) &&
17801 token->type == CPP_AND_AND) /* C++0x only */
17802 code = NON_LVALUE_EXPR;
17804 if (code != ERROR_MARK)
17806 /* Consume the `*', `&' or `&&'. */
17807 cp_lexer_consume_token (parser->lexer);
17809 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17810 `&', if we are allowing GNU extensions. (The only qualifier
17811 that can legally appear after `&' is `restrict', but that is
17812 enforced during semantic analysis. */
17813 if (code == INDIRECT_REF
17814 || cp_parser_allow_gnu_extensions_p (parser))
17815 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17817 attrs = cp_parser_std_attribute_spec_seq (parser);
17818 if (attributes != NULL)
17819 *attributes = attrs;
17821 else
17823 /* Try the pointer-to-member case. */
17824 cp_parser_parse_tentatively (parser);
17825 /* Look for the optional `::' operator. */
17826 cp_parser_global_scope_opt (parser,
17827 /*current_scope_valid_p=*/false);
17828 /* Look for the nested-name specifier. */
17829 token = cp_lexer_peek_token (parser->lexer);
17830 cp_parser_nested_name_specifier (parser,
17831 /*typename_keyword_p=*/false,
17832 /*check_dependency_p=*/true,
17833 /*type_p=*/false,
17834 /*is_declaration=*/false);
17835 /* If we found it, and the next token is a `*', then we are
17836 indeed looking at a pointer-to-member operator. */
17837 if (!cp_parser_error_occurred (parser)
17838 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17840 /* Indicate that the `*' operator was used. */
17841 code = INDIRECT_REF;
17843 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17844 error_at (token->location, "%qD is a namespace", parser->scope);
17845 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17846 error_at (token->location, "cannot form pointer to member of "
17847 "non-class %q#T", parser->scope);
17848 else
17850 /* The type of which the member is a member is given by the
17851 current SCOPE. */
17852 *type = parser->scope;
17853 /* The next name will not be qualified. */
17854 parser->scope = NULL_TREE;
17855 parser->qualifying_scope = NULL_TREE;
17856 parser->object_scope = NULL_TREE;
17857 /* Look for optional c++11 attributes. */
17858 attrs = cp_parser_std_attribute_spec_seq (parser);
17859 if (attributes != NULL)
17860 *attributes = attrs;
17861 /* Look for the optional cv-qualifier-seq. */
17862 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17865 /* If that didn't work we don't have a ptr-operator. */
17866 if (!cp_parser_parse_definitely (parser))
17867 cp_parser_error (parser, "expected ptr-operator");
17870 return code;
17873 /* Parse an (optional) cv-qualifier-seq.
17875 cv-qualifier-seq:
17876 cv-qualifier cv-qualifier-seq [opt]
17878 cv-qualifier:
17879 const
17880 volatile
17882 GNU Extension:
17884 cv-qualifier:
17885 __restrict__
17887 Returns a bitmask representing the cv-qualifiers. */
17889 static cp_cv_quals
17890 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17892 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17894 while (true)
17896 cp_token *token;
17897 cp_cv_quals cv_qualifier;
17899 /* Peek at the next token. */
17900 token = cp_lexer_peek_token (parser->lexer);
17901 /* See if it's a cv-qualifier. */
17902 switch (token->keyword)
17904 case RID_CONST:
17905 cv_qualifier = TYPE_QUAL_CONST;
17906 break;
17908 case RID_VOLATILE:
17909 cv_qualifier = TYPE_QUAL_VOLATILE;
17910 break;
17912 case RID_RESTRICT:
17913 cv_qualifier = TYPE_QUAL_RESTRICT;
17914 break;
17916 default:
17917 cv_qualifier = TYPE_UNQUALIFIED;
17918 break;
17921 if (!cv_qualifier)
17922 break;
17924 if (cv_quals & cv_qualifier)
17926 error_at (token->location, "duplicate cv-qualifier");
17927 cp_lexer_purge_token (parser->lexer);
17929 else
17931 cp_lexer_consume_token (parser->lexer);
17932 cv_quals |= cv_qualifier;
17936 return cv_quals;
17939 /* Parse an (optional) ref-qualifier
17941 ref-qualifier:
17945 Returns cp_ref_qualifier representing ref-qualifier. */
17947 static cp_ref_qualifier
17948 cp_parser_ref_qualifier_opt (cp_parser* parser)
17950 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17952 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17953 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17954 return ref_qual;
17956 while (true)
17958 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17959 cp_token *token = cp_lexer_peek_token (parser->lexer);
17961 switch (token->type)
17963 case CPP_AND:
17964 curr_ref_qual = REF_QUAL_LVALUE;
17965 break;
17967 case CPP_AND_AND:
17968 curr_ref_qual = REF_QUAL_RVALUE;
17969 break;
17971 default:
17972 curr_ref_qual = REF_QUAL_NONE;
17973 break;
17976 if (!curr_ref_qual)
17977 break;
17978 else if (ref_qual)
17980 error_at (token->location, "multiple ref-qualifiers");
17981 cp_lexer_purge_token (parser->lexer);
17983 else
17985 ref_qual = curr_ref_qual;
17986 cp_lexer_consume_token (parser->lexer);
17990 return ref_qual;
17993 /* Parse an (optional) virt-specifier-seq.
17995 virt-specifier-seq:
17996 virt-specifier virt-specifier-seq [opt]
17998 virt-specifier:
17999 override
18000 final
18002 Returns a bitmask representing the virt-specifiers. */
18004 static cp_virt_specifiers
18005 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18007 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18009 while (true)
18011 cp_token *token;
18012 cp_virt_specifiers virt_specifier;
18014 /* Peek at the next token. */
18015 token = cp_lexer_peek_token (parser->lexer);
18016 /* See if it's a virt-specifier-qualifier. */
18017 if (token->type != CPP_NAME)
18018 break;
18019 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18021 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18022 virt_specifier = VIRT_SPEC_OVERRIDE;
18024 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18026 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18027 virt_specifier = VIRT_SPEC_FINAL;
18029 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18031 virt_specifier = VIRT_SPEC_FINAL;
18033 else
18034 break;
18036 if (virt_specifiers & virt_specifier)
18038 error_at (token->location, "duplicate virt-specifier");
18039 cp_lexer_purge_token (parser->lexer);
18041 else
18043 cp_lexer_consume_token (parser->lexer);
18044 virt_specifiers |= virt_specifier;
18047 return virt_specifiers;
18050 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18051 is in scope even though it isn't real. */
18053 void
18054 inject_this_parameter (tree ctype, cp_cv_quals quals)
18056 tree this_parm;
18058 if (current_class_ptr)
18060 /* We don't clear this between NSDMIs. Is it already what we want? */
18061 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18062 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18063 && cp_type_quals (type) == quals)
18064 return;
18067 this_parm = build_this_parm (ctype, quals);
18068 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18069 current_class_ptr = NULL_TREE;
18070 current_class_ref
18071 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18072 current_class_ptr = this_parm;
18075 /* Return true iff our current scope is a non-static data member
18076 initializer. */
18078 bool
18079 parsing_nsdmi (void)
18081 /* We recognize NSDMI context by the context-less 'this' pointer set up
18082 by the function above. */
18083 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18084 return true;
18085 return false;
18088 /* Parse a late-specified return type, if any. This is not a separate
18089 non-terminal, but part of a function declarator, which looks like
18091 -> trailing-type-specifier-seq abstract-declarator(opt)
18093 Returns the type indicated by the type-id.
18095 In addition to this this parses any queued up omp declare simd
18096 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18098 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18099 function. */
18101 static tree
18102 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18103 cp_cv_quals quals)
18105 cp_token *token;
18106 tree type = NULL_TREE;
18107 bool declare_simd_p = (parser->omp_declare_simd
18108 && declarator
18109 && declarator->kind == cdk_id);
18111 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18112 && declarator && declarator->kind == cdk_id);
18114 /* Peek at the next token. */
18115 token = cp_lexer_peek_token (parser->lexer);
18116 /* A late-specified return type is indicated by an initial '->'. */
18117 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18118 return NULL_TREE;
18120 tree save_ccp = current_class_ptr;
18121 tree save_ccr = current_class_ref;
18122 if (quals >= 0)
18124 /* DR 1207: 'this' is in scope in the trailing return type. */
18125 inject_this_parameter (current_class_type, quals);
18128 if (token->type == CPP_DEREF)
18130 /* Consume the ->. */
18131 cp_lexer_consume_token (parser->lexer);
18133 type = cp_parser_trailing_type_id (parser);
18136 if (cilk_simd_fn_vector_p)
18137 declarator->std_attributes
18138 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18139 declarator->std_attributes);
18140 if (declare_simd_p)
18141 declarator->std_attributes
18142 = cp_parser_late_parsing_omp_declare_simd (parser,
18143 declarator->std_attributes);
18145 if (quals >= 0)
18147 current_class_ptr = save_ccp;
18148 current_class_ref = save_ccr;
18151 return type;
18154 /* Parse a declarator-id.
18156 declarator-id:
18157 id-expression
18158 :: [opt] nested-name-specifier [opt] type-name
18160 In the `id-expression' case, the value returned is as for
18161 cp_parser_id_expression if the id-expression was an unqualified-id.
18162 If the id-expression was a qualified-id, then a SCOPE_REF is
18163 returned. The first operand is the scope (either a NAMESPACE_DECL
18164 or TREE_TYPE), but the second is still just a representation of an
18165 unqualified-id. */
18167 static tree
18168 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18170 tree id;
18171 /* The expression must be an id-expression. Assume that qualified
18172 names are the names of types so that:
18174 template <class T>
18175 int S<T>::R::i = 3;
18177 will work; we must treat `S<T>::R' as the name of a type.
18178 Similarly, assume that qualified names are templates, where
18179 required, so that:
18181 template <class T>
18182 int S<T>::R<T>::i = 3;
18184 will work, too. */
18185 id = cp_parser_id_expression (parser,
18186 /*template_keyword_p=*/false,
18187 /*check_dependency_p=*/false,
18188 /*template_p=*/NULL,
18189 /*declarator_p=*/true,
18190 optional_p);
18191 if (id && BASELINK_P (id))
18192 id = BASELINK_FUNCTIONS (id);
18193 return id;
18196 /* Parse a type-id.
18198 type-id:
18199 type-specifier-seq abstract-declarator [opt]
18201 Returns the TYPE specified. */
18203 static tree
18204 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18205 bool is_trailing_return)
18207 cp_decl_specifier_seq type_specifier_seq;
18208 cp_declarator *abstract_declarator;
18210 /* Parse the type-specifier-seq. */
18211 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18212 is_trailing_return,
18213 &type_specifier_seq);
18214 if (type_specifier_seq.type == error_mark_node)
18215 return error_mark_node;
18217 /* There might or might not be an abstract declarator. */
18218 cp_parser_parse_tentatively (parser);
18219 /* Look for the declarator. */
18220 abstract_declarator
18221 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18222 /*parenthesized_p=*/NULL,
18223 /*member_p=*/false,
18224 /*friend_p=*/false);
18225 /* Check to see if there really was a declarator. */
18226 if (!cp_parser_parse_definitely (parser))
18227 abstract_declarator = NULL;
18229 if (type_specifier_seq.type
18230 /* None of the valid uses of 'auto' in C++14 involve the type-id
18231 nonterminal, but it is valid in a trailing-return-type. */
18232 && !(cxx_dialect >= cxx14 && is_trailing_return)
18233 && type_uses_auto (type_specifier_seq.type))
18235 /* A type-id with type 'auto' is only ok if the abstract declarator
18236 is a function declarator with a late-specified return type. */
18237 if (abstract_declarator
18238 && abstract_declarator->kind == cdk_function
18239 && abstract_declarator->u.function.late_return_type)
18240 /* OK */;
18241 else
18243 error ("invalid use of %<auto%>");
18244 return error_mark_node;
18248 return groktypename (&type_specifier_seq, abstract_declarator,
18249 is_template_arg);
18252 static tree cp_parser_type_id (cp_parser *parser)
18254 return cp_parser_type_id_1 (parser, false, false);
18257 static tree cp_parser_template_type_arg (cp_parser *parser)
18259 tree r;
18260 const char *saved_message = parser->type_definition_forbidden_message;
18261 parser->type_definition_forbidden_message
18262 = G_("types may not be defined in template arguments");
18263 r = cp_parser_type_id_1 (parser, true, false);
18264 parser->type_definition_forbidden_message = saved_message;
18265 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18267 error ("invalid use of %<auto%> in template argument");
18268 r = error_mark_node;
18270 return r;
18273 static tree cp_parser_trailing_type_id (cp_parser *parser)
18275 return cp_parser_type_id_1 (parser, false, true);
18278 /* Parse a type-specifier-seq.
18280 type-specifier-seq:
18281 type-specifier type-specifier-seq [opt]
18283 GNU extension:
18285 type-specifier-seq:
18286 attributes type-specifier-seq [opt]
18288 If IS_DECLARATION is true, we are at the start of a "condition" or
18289 exception-declaration, so we might be followed by a declarator-id.
18291 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18292 i.e. we've just seen "->".
18294 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18296 static void
18297 cp_parser_type_specifier_seq (cp_parser* parser,
18298 bool is_declaration,
18299 bool is_trailing_return,
18300 cp_decl_specifier_seq *type_specifier_seq)
18302 bool seen_type_specifier = false;
18303 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18304 cp_token *start_token = NULL;
18306 /* Clear the TYPE_SPECIFIER_SEQ. */
18307 clear_decl_specs (type_specifier_seq);
18309 /* In the context of a trailing return type, enum E { } is an
18310 elaborated-type-specifier followed by a function-body, not an
18311 enum-specifier. */
18312 if (is_trailing_return)
18313 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18315 /* Parse the type-specifiers and attributes. */
18316 while (true)
18318 tree type_specifier;
18319 bool is_cv_qualifier;
18321 /* Check for attributes first. */
18322 if (cp_next_tokens_can_be_attribute_p (parser))
18324 type_specifier_seq->attributes =
18325 chainon (type_specifier_seq->attributes,
18326 cp_parser_attributes_opt (parser));
18327 continue;
18330 /* record the token of the beginning of the type specifier seq,
18331 for error reporting purposes*/
18332 if (!start_token)
18333 start_token = cp_lexer_peek_token (parser->lexer);
18335 /* Look for the type-specifier. */
18336 type_specifier = cp_parser_type_specifier (parser,
18337 flags,
18338 type_specifier_seq,
18339 /*is_declaration=*/false,
18340 NULL,
18341 &is_cv_qualifier);
18342 if (!type_specifier)
18344 /* If the first type-specifier could not be found, this is not a
18345 type-specifier-seq at all. */
18346 if (!seen_type_specifier)
18348 /* Set in_declarator_p to avoid skipping to the semicolon. */
18349 int in_decl = parser->in_declarator_p;
18350 parser->in_declarator_p = true;
18352 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18353 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18354 cp_parser_error (parser, "expected type-specifier");
18356 parser->in_declarator_p = in_decl;
18358 type_specifier_seq->type = error_mark_node;
18359 return;
18361 /* If subsequent type-specifiers could not be found, the
18362 type-specifier-seq is complete. */
18363 break;
18366 seen_type_specifier = true;
18367 /* The standard says that a condition can be:
18369 type-specifier-seq declarator = assignment-expression
18371 However, given:
18373 struct S {};
18374 if (int S = ...)
18376 we should treat the "S" as a declarator, not as a
18377 type-specifier. The standard doesn't say that explicitly for
18378 type-specifier-seq, but it does say that for
18379 decl-specifier-seq in an ordinary declaration. Perhaps it
18380 would be clearer just to allow a decl-specifier-seq here, and
18381 then add a semantic restriction that if any decl-specifiers
18382 that are not type-specifiers appear, the program is invalid. */
18383 if (is_declaration && !is_cv_qualifier)
18384 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18388 /* Return whether the function currently being declared has an associated
18389 template parameter list. */
18391 static bool
18392 function_being_declared_is_template_p (cp_parser* parser)
18394 if (!current_template_parms || processing_template_parmlist)
18395 return false;
18397 if (parser->implicit_template_scope)
18398 return true;
18400 if (at_class_scope_p ()
18401 && TYPE_BEING_DEFINED (current_class_type))
18402 return parser->num_template_parameter_lists != 0;
18404 return ((int) parser->num_template_parameter_lists > template_class_depth
18405 (current_class_type));
18408 /* Parse a parameter-declaration-clause.
18410 parameter-declaration-clause:
18411 parameter-declaration-list [opt] ... [opt]
18412 parameter-declaration-list , ...
18414 Returns a representation for the parameter declarations. A return
18415 value of NULL indicates a parameter-declaration-clause consisting
18416 only of an ellipsis. */
18418 static tree
18419 cp_parser_parameter_declaration_clause (cp_parser* parser)
18421 tree parameters;
18422 cp_token *token;
18423 bool ellipsis_p;
18424 bool is_error;
18426 struct cleanup {
18427 cp_parser* parser;
18428 int auto_is_implicit_function_template_parm_p;
18429 ~cleanup() {
18430 parser->auto_is_implicit_function_template_parm_p
18431 = auto_is_implicit_function_template_parm_p;
18433 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18435 (void) cleanup;
18437 if (!processing_specialization
18438 && !processing_template_parmlist
18439 && !processing_explicit_instantiation)
18440 if (!current_function_decl
18441 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18442 parser->auto_is_implicit_function_template_parm_p = true;
18444 /* Peek at the next token. */
18445 token = cp_lexer_peek_token (parser->lexer);
18446 /* Check for trivial parameter-declaration-clauses. */
18447 if (token->type == CPP_ELLIPSIS)
18449 /* Consume the `...' token. */
18450 cp_lexer_consume_token (parser->lexer);
18451 return NULL_TREE;
18453 else if (token->type == CPP_CLOSE_PAREN)
18454 /* There are no parameters. */
18456 #ifndef NO_IMPLICIT_EXTERN_C
18457 if (in_system_header_at (input_location)
18458 && current_class_type == NULL
18459 && current_lang_name == lang_name_c)
18460 return NULL_TREE;
18461 else
18462 #endif
18463 return void_list_node;
18465 /* Check for `(void)', too, which is a special case. */
18466 else if (token->keyword == RID_VOID
18467 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18468 == CPP_CLOSE_PAREN))
18470 /* Consume the `void' token. */
18471 cp_lexer_consume_token (parser->lexer);
18472 /* There are no parameters. */
18473 return void_list_node;
18476 /* Parse the parameter-declaration-list. */
18477 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18478 /* If a parse error occurred while parsing the
18479 parameter-declaration-list, then the entire
18480 parameter-declaration-clause is erroneous. */
18481 if (is_error)
18482 return NULL;
18484 /* Peek at the next token. */
18485 token = cp_lexer_peek_token (parser->lexer);
18486 /* If it's a `,', the clause should terminate with an ellipsis. */
18487 if (token->type == CPP_COMMA)
18489 /* Consume the `,'. */
18490 cp_lexer_consume_token (parser->lexer);
18491 /* Expect an ellipsis. */
18492 ellipsis_p
18493 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18495 /* It might also be `...' if the optional trailing `,' was
18496 omitted. */
18497 else if (token->type == CPP_ELLIPSIS)
18499 /* Consume the `...' token. */
18500 cp_lexer_consume_token (parser->lexer);
18501 /* And remember that we saw it. */
18502 ellipsis_p = true;
18504 else
18505 ellipsis_p = false;
18507 /* Finish the parameter list. */
18508 if (!ellipsis_p)
18509 parameters = chainon (parameters, void_list_node);
18511 return parameters;
18514 /* Parse a parameter-declaration-list.
18516 parameter-declaration-list:
18517 parameter-declaration
18518 parameter-declaration-list , parameter-declaration
18520 Returns a representation of the parameter-declaration-list, as for
18521 cp_parser_parameter_declaration_clause. However, the
18522 `void_list_node' is never appended to the list. Upon return,
18523 *IS_ERROR will be true iff an error occurred. */
18525 static tree
18526 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18528 tree parameters = NULL_TREE;
18529 tree *tail = &parameters;
18530 bool saved_in_unbraced_linkage_specification_p;
18531 int index = 0;
18533 /* Assume all will go well. */
18534 *is_error = false;
18535 /* The special considerations that apply to a function within an
18536 unbraced linkage specifications do not apply to the parameters
18537 to the function. */
18538 saved_in_unbraced_linkage_specification_p
18539 = parser->in_unbraced_linkage_specification_p;
18540 parser->in_unbraced_linkage_specification_p = false;
18542 /* Look for more parameters. */
18543 while (true)
18545 cp_parameter_declarator *parameter;
18546 tree decl = error_mark_node;
18547 bool parenthesized_p = false;
18548 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18549 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18550 (current_template_parms)) : 0);
18552 /* Parse the parameter. */
18553 parameter
18554 = cp_parser_parameter_declaration (parser,
18555 /*template_parm_p=*/false,
18556 &parenthesized_p);
18558 /* We don't know yet if the enclosing context is deprecated, so wait
18559 and warn in grokparms if appropriate. */
18560 deprecated_state = DEPRECATED_SUPPRESS;
18562 if (parameter)
18564 /* If a function parameter pack was specified and an implicit template
18565 parameter was introduced during cp_parser_parameter_declaration,
18566 change any implicit parameters introduced into packs. */
18567 if (parser->implicit_template_parms
18568 && parameter->declarator
18569 && parameter->declarator->parameter_pack_p)
18571 int latest_template_parm_idx = TREE_VEC_LENGTH
18572 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18574 if (latest_template_parm_idx != template_parm_idx)
18575 parameter->decl_specifiers.type = convert_generic_types_to_packs
18576 (parameter->decl_specifiers.type,
18577 template_parm_idx, latest_template_parm_idx);
18580 decl = grokdeclarator (parameter->declarator,
18581 &parameter->decl_specifiers,
18582 PARM,
18583 parameter->default_argument != NULL_TREE,
18584 &parameter->decl_specifiers.attributes);
18587 deprecated_state = DEPRECATED_NORMAL;
18589 /* If a parse error occurred parsing the parameter declaration,
18590 then the entire parameter-declaration-list is erroneous. */
18591 if (decl == error_mark_node)
18593 *is_error = true;
18594 parameters = error_mark_node;
18595 break;
18598 if (parameter->decl_specifiers.attributes)
18599 cplus_decl_attributes (&decl,
18600 parameter->decl_specifiers.attributes,
18602 if (DECL_NAME (decl))
18603 decl = pushdecl (decl);
18605 if (decl != error_mark_node)
18607 retrofit_lang_decl (decl);
18608 DECL_PARM_INDEX (decl) = ++index;
18609 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18612 /* Add the new parameter to the list. */
18613 *tail = build_tree_list (parameter->default_argument, decl);
18614 tail = &TREE_CHAIN (*tail);
18616 /* Peek at the next token. */
18617 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18618 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18619 /* These are for Objective-C++ */
18620 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18621 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18622 /* The parameter-declaration-list is complete. */
18623 break;
18624 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18626 cp_token *token;
18628 /* Peek at the next token. */
18629 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18630 /* If it's an ellipsis, then the list is complete. */
18631 if (token->type == CPP_ELLIPSIS)
18632 break;
18633 /* Otherwise, there must be more parameters. Consume the
18634 `,'. */
18635 cp_lexer_consume_token (parser->lexer);
18636 /* When parsing something like:
18638 int i(float f, double d)
18640 we can tell after seeing the declaration for "f" that we
18641 are not looking at an initialization of a variable "i",
18642 but rather at the declaration of a function "i".
18644 Due to the fact that the parsing of template arguments
18645 (as specified to a template-id) requires backtracking we
18646 cannot use this technique when inside a template argument
18647 list. */
18648 if (!parser->in_template_argument_list_p
18649 && !parser->in_type_id_in_expr_p
18650 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18651 /* However, a parameter-declaration of the form
18652 "float(f)" (which is a valid declaration of a
18653 parameter "f") can also be interpreted as an
18654 expression (the conversion of "f" to "float"). */
18655 && !parenthesized_p)
18656 cp_parser_commit_to_tentative_parse (parser);
18658 else
18660 cp_parser_error (parser, "expected %<,%> or %<...%>");
18661 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18662 cp_parser_skip_to_closing_parenthesis (parser,
18663 /*recovering=*/true,
18664 /*or_comma=*/false,
18665 /*consume_paren=*/false);
18666 break;
18670 parser->in_unbraced_linkage_specification_p
18671 = saved_in_unbraced_linkage_specification_p;
18673 /* Reset implicit_template_scope if we are about to leave the function
18674 parameter list that introduced it. Note that for out-of-line member
18675 definitions, there will be one or more class scopes before we get to
18676 the template parameter scope. */
18678 if (cp_binding_level *its = parser->implicit_template_scope)
18679 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18681 while (maybe_its->kind == sk_class)
18682 maybe_its = maybe_its->level_chain;
18683 if (maybe_its == its)
18685 parser->implicit_template_parms = 0;
18686 parser->implicit_template_scope = 0;
18690 return parameters;
18693 /* Parse a parameter declaration.
18695 parameter-declaration:
18696 decl-specifier-seq ... [opt] declarator
18697 decl-specifier-seq declarator = assignment-expression
18698 decl-specifier-seq ... [opt] abstract-declarator [opt]
18699 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18701 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18702 declares a template parameter. (In that case, a non-nested `>'
18703 token encountered during the parsing of the assignment-expression
18704 is not interpreted as a greater-than operator.)
18706 Returns a representation of the parameter, or NULL if an error
18707 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18708 true iff the declarator is of the form "(p)". */
18710 static cp_parameter_declarator *
18711 cp_parser_parameter_declaration (cp_parser *parser,
18712 bool template_parm_p,
18713 bool *parenthesized_p)
18715 int declares_class_or_enum;
18716 cp_decl_specifier_seq decl_specifiers;
18717 cp_declarator *declarator;
18718 tree default_argument;
18719 cp_token *token = NULL, *declarator_token_start = NULL;
18720 const char *saved_message;
18722 /* In a template parameter, `>' is not an operator.
18724 [temp.param]
18726 When parsing a default template-argument for a non-type
18727 template-parameter, the first non-nested `>' is taken as the end
18728 of the template parameter-list rather than a greater-than
18729 operator. */
18731 /* Type definitions may not appear in parameter types. */
18732 saved_message = parser->type_definition_forbidden_message;
18733 parser->type_definition_forbidden_message
18734 = G_("types may not be defined in parameter types");
18736 /* Parse the declaration-specifiers. */
18737 cp_parser_decl_specifier_seq (parser,
18738 CP_PARSER_FLAGS_NONE,
18739 &decl_specifiers,
18740 &declares_class_or_enum);
18742 /* Complain about missing 'typename' or other invalid type names. */
18743 if (!decl_specifiers.any_type_specifiers_p
18744 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18745 decl_specifiers.type = error_mark_node;
18747 /* If an error occurred, there's no reason to attempt to parse the
18748 rest of the declaration. */
18749 if (cp_parser_error_occurred (parser))
18751 parser->type_definition_forbidden_message = saved_message;
18752 return NULL;
18755 /* Peek at the next token. */
18756 token = cp_lexer_peek_token (parser->lexer);
18758 /* If the next token is a `)', `,', `=', `>', or `...', then there
18759 is no declarator. However, when variadic templates are enabled,
18760 there may be a declarator following `...'. */
18761 if (token->type == CPP_CLOSE_PAREN
18762 || token->type == CPP_COMMA
18763 || token->type == CPP_EQ
18764 || token->type == CPP_GREATER)
18766 declarator = NULL;
18767 if (parenthesized_p)
18768 *parenthesized_p = false;
18770 /* Otherwise, there should be a declarator. */
18771 else
18773 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18774 parser->default_arg_ok_p = false;
18776 /* After seeing a decl-specifier-seq, if the next token is not a
18777 "(", there is no possibility that the code is a valid
18778 expression. Therefore, if parsing tentatively, we commit at
18779 this point. */
18780 if (!parser->in_template_argument_list_p
18781 /* In an expression context, having seen:
18783 (int((char ...
18785 we cannot be sure whether we are looking at a
18786 function-type (taking a "char" as a parameter) or a cast
18787 of some object of type "char" to "int". */
18788 && !parser->in_type_id_in_expr_p
18789 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18790 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18791 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18792 cp_parser_commit_to_tentative_parse (parser);
18793 /* Parse the declarator. */
18794 declarator_token_start = token;
18795 declarator = cp_parser_declarator (parser,
18796 CP_PARSER_DECLARATOR_EITHER,
18797 /*ctor_dtor_or_conv_p=*/NULL,
18798 parenthesized_p,
18799 /*member_p=*/false,
18800 /*friend_p=*/false);
18801 parser->default_arg_ok_p = saved_default_arg_ok_p;
18802 /* After the declarator, allow more attributes. */
18803 decl_specifiers.attributes
18804 = chainon (decl_specifiers.attributes,
18805 cp_parser_attributes_opt (parser));
18808 /* If the next token is an ellipsis, and we have not seen a
18809 declarator name, and the type of the declarator contains parameter
18810 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18811 a parameter pack expansion expression. Otherwise, leave the
18812 ellipsis for a C-style variadic function. */
18813 token = cp_lexer_peek_token (parser->lexer);
18814 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18816 tree type = decl_specifiers.type;
18818 if (type && DECL_P (type))
18819 type = TREE_TYPE (type);
18821 if (type
18822 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18823 && declarator_can_be_parameter_pack (declarator)
18824 && (!declarator || !declarator->parameter_pack_p)
18825 && uses_parameter_packs (type))
18827 /* Consume the `...'. */
18828 cp_lexer_consume_token (parser->lexer);
18829 maybe_warn_variadic_templates ();
18831 /* Build a pack expansion type */
18832 if (declarator)
18833 declarator->parameter_pack_p = true;
18834 else
18835 decl_specifiers.type = make_pack_expansion (type);
18839 /* The restriction on defining new types applies only to the type
18840 of the parameter, not to the default argument. */
18841 parser->type_definition_forbidden_message = saved_message;
18843 /* If the next token is `=', then process a default argument. */
18844 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18846 token = cp_lexer_peek_token (parser->lexer);
18847 /* If we are defining a class, then the tokens that make up the
18848 default argument must be saved and processed later. */
18849 if (!template_parm_p && at_class_scope_p ()
18850 && TYPE_BEING_DEFINED (current_class_type)
18851 && !LAMBDA_TYPE_P (current_class_type))
18852 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18853 /* Outside of a class definition, we can just parse the
18854 assignment-expression. */
18855 else
18856 default_argument
18857 = cp_parser_default_argument (parser, template_parm_p);
18859 if (!parser->default_arg_ok_p)
18861 if (flag_permissive)
18862 warning (0, "deprecated use of default argument for parameter of non-function");
18863 else
18865 error_at (token->location,
18866 "default arguments are only "
18867 "permitted for function parameters");
18868 default_argument = NULL_TREE;
18871 else if ((declarator && declarator->parameter_pack_p)
18872 || (decl_specifiers.type
18873 && PACK_EXPANSION_P (decl_specifiers.type)))
18875 /* Find the name of the parameter pack. */
18876 cp_declarator *id_declarator = declarator;
18877 while (id_declarator && id_declarator->kind != cdk_id)
18878 id_declarator = id_declarator->declarator;
18880 if (id_declarator && id_declarator->kind == cdk_id)
18881 error_at (declarator_token_start->location,
18882 template_parm_p
18883 ? G_("template parameter pack %qD "
18884 "cannot have a default argument")
18885 : G_("parameter pack %qD cannot have "
18886 "a default argument"),
18887 id_declarator->u.id.unqualified_name);
18888 else
18889 error_at (declarator_token_start->location,
18890 template_parm_p
18891 ? G_("template parameter pack cannot have "
18892 "a default argument")
18893 : G_("parameter pack cannot have a "
18894 "default argument"));
18896 default_argument = NULL_TREE;
18899 else
18900 default_argument = NULL_TREE;
18902 return make_parameter_declarator (&decl_specifiers,
18903 declarator,
18904 default_argument);
18907 /* Parse a default argument and return it.
18909 TEMPLATE_PARM_P is true if this is a default argument for a
18910 non-type template parameter. */
18911 static tree
18912 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18914 tree default_argument = NULL_TREE;
18915 bool saved_greater_than_is_operator_p;
18916 bool saved_local_variables_forbidden_p;
18917 bool non_constant_p, is_direct_init;
18919 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18920 set correctly. */
18921 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18922 parser->greater_than_is_operator_p = !template_parm_p;
18923 /* Local variable names (and the `this' keyword) may not
18924 appear in a default argument. */
18925 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18926 parser->local_variables_forbidden_p = true;
18927 /* Parse the assignment-expression. */
18928 if (template_parm_p)
18929 push_deferring_access_checks (dk_no_deferred);
18930 tree saved_class_ptr = NULL_TREE;
18931 tree saved_class_ref = NULL_TREE;
18932 /* The "this" pointer is not valid in a default argument. */
18933 if (cfun)
18935 saved_class_ptr = current_class_ptr;
18936 cp_function_chain->x_current_class_ptr = NULL_TREE;
18937 saved_class_ref = current_class_ref;
18938 cp_function_chain->x_current_class_ref = NULL_TREE;
18940 default_argument
18941 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18942 /* Restore the "this" pointer. */
18943 if (cfun)
18945 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18946 cp_function_chain->x_current_class_ref = saved_class_ref;
18948 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18949 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18950 if (template_parm_p)
18951 pop_deferring_access_checks ();
18952 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18953 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18955 return default_argument;
18958 /* Parse a function-body.
18960 function-body:
18961 compound_statement */
18963 static void
18964 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18966 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18969 /* Parse a ctor-initializer-opt followed by a function-body. Return
18970 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18971 is true we are parsing a function-try-block. */
18973 static bool
18974 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18975 bool in_function_try_block)
18977 tree body, list;
18978 bool ctor_initializer_p;
18979 const bool check_body_p =
18980 DECL_CONSTRUCTOR_P (current_function_decl)
18981 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18982 tree last = NULL;
18984 /* Begin the function body. */
18985 body = begin_function_body ();
18986 /* Parse the optional ctor-initializer. */
18987 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18989 /* If we're parsing a constexpr constructor definition, we need
18990 to check that the constructor body is indeed empty. However,
18991 before we get to cp_parser_function_body lot of junk has been
18992 generated, so we can't just check that we have an empty block.
18993 Rather we take a snapshot of the outermost block, and check whether
18994 cp_parser_function_body changed its state. */
18995 if (check_body_p)
18997 list = cur_stmt_list;
18998 if (STATEMENT_LIST_TAIL (list))
18999 last = STATEMENT_LIST_TAIL (list)->stmt;
19001 /* Parse the function-body. */
19002 cp_parser_function_body (parser, in_function_try_block);
19003 if (check_body_p)
19004 check_constexpr_ctor_body (last, list);
19005 /* Finish the function body. */
19006 finish_function_body (body);
19008 return ctor_initializer_p;
19011 /* Parse an initializer.
19013 initializer:
19014 = initializer-clause
19015 ( expression-list )
19017 Returns an expression representing the initializer. If no
19018 initializer is present, NULL_TREE is returned.
19020 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19021 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19022 set to TRUE if there is no initializer present. If there is an
19023 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19024 is set to true; otherwise it is set to false. */
19026 static tree
19027 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19028 bool* non_constant_p)
19030 cp_token *token;
19031 tree init;
19033 /* Peek at the next token. */
19034 token = cp_lexer_peek_token (parser->lexer);
19036 /* Let our caller know whether or not this initializer was
19037 parenthesized. */
19038 *is_direct_init = (token->type != CPP_EQ);
19039 /* Assume that the initializer is constant. */
19040 *non_constant_p = false;
19042 if (token->type == CPP_EQ)
19044 /* Consume the `='. */
19045 cp_lexer_consume_token (parser->lexer);
19046 /* Parse the initializer-clause. */
19047 init = cp_parser_initializer_clause (parser, non_constant_p);
19049 else if (token->type == CPP_OPEN_PAREN)
19051 vec<tree, va_gc> *vec;
19052 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19053 /*cast_p=*/false,
19054 /*allow_expansion_p=*/true,
19055 non_constant_p);
19056 if (vec == NULL)
19057 return error_mark_node;
19058 init = build_tree_list_vec (vec);
19059 release_tree_vector (vec);
19061 else if (token->type == CPP_OPEN_BRACE)
19063 cp_lexer_set_source_position (parser->lexer);
19064 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19065 init = cp_parser_braced_list (parser, non_constant_p);
19066 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19068 else
19070 /* Anything else is an error. */
19071 cp_parser_error (parser, "expected initializer");
19072 init = error_mark_node;
19075 return init;
19078 /* Parse an initializer-clause.
19080 initializer-clause:
19081 assignment-expression
19082 braced-init-list
19084 Returns an expression representing the initializer.
19086 If the `assignment-expression' production is used the value
19087 returned is simply a representation for the expression.
19089 Otherwise, calls cp_parser_braced_list. */
19091 static tree
19092 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19094 tree initializer;
19096 /* Assume the expression is constant. */
19097 *non_constant_p = false;
19099 /* If it is not a `{', then we are looking at an
19100 assignment-expression. */
19101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19103 initializer
19104 = cp_parser_constant_expression (parser,
19105 /*allow_non_constant_p=*/true,
19106 non_constant_p);
19108 else
19109 initializer = cp_parser_braced_list (parser, non_constant_p);
19111 return initializer;
19114 /* Parse a brace-enclosed initializer list.
19116 braced-init-list:
19117 { initializer-list , [opt] }
19120 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19121 the elements of the initializer-list (or NULL, if the last
19122 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19123 NULL_TREE. There is no way to detect whether or not the optional
19124 trailing `,' was provided. NON_CONSTANT_P is as for
19125 cp_parser_initializer. */
19127 static tree
19128 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19130 tree initializer;
19132 /* Consume the `{' token. */
19133 cp_lexer_consume_token (parser->lexer);
19134 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19135 initializer = make_node (CONSTRUCTOR);
19136 /* If it's not a `}', then there is a non-trivial initializer. */
19137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19139 /* Parse the initializer list. */
19140 CONSTRUCTOR_ELTS (initializer)
19141 = cp_parser_initializer_list (parser, non_constant_p);
19142 /* A trailing `,' token is allowed. */
19143 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19144 cp_lexer_consume_token (parser->lexer);
19146 else
19147 *non_constant_p = false;
19148 /* Now, there should be a trailing `}'. */
19149 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19150 TREE_TYPE (initializer) = init_list_type_node;
19151 return initializer;
19154 /* Parse an initializer-list.
19156 initializer-list:
19157 initializer-clause ... [opt]
19158 initializer-list , initializer-clause ... [opt]
19160 GNU Extension:
19162 initializer-list:
19163 designation initializer-clause ...[opt]
19164 initializer-list , designation initializer-clause ...[opt]
19166 designation:
19167 . identifier =
19168 identifier :
19169 [ constant-expression ] =
19171 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19172 for the initializer. If the INDEX of the elt is non-NULL, it is the
19173 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19174 as for cp_parser_initializer. */
19176 static vec<constructor_elt, va_gc> *
19177 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19179 vec<constructor_elt, va_gc> *v = NULL;
19181 /* Assume all of the expressions are constant. */
19182 *non_constant_p = false;
19184 /* Parse the rest of the list. */
19185 while (true)
19187 cp_token *token;
19188 tree designator;
19189 tree initializer;
19190 bool clause_non_constant_p;
19192 /* If the next token is an identifier and the following one is a
19193 colon, we are looking at the GNU designated-initializer
19194 syntax. */
19195 if (cp_parser_allow_gnu_extensions_p (parser)
19196 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19197 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19199 /* Warn the user that they are using an extension. */
19200 pedwarn (input_location, OPT_Wpedantic,
19201 "ISO C++ does not allow designated initializers");
19202 /* Consume the identifier. */
19203 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19204 /* Consume the `:'. */
19205 cp_lexer_consume_token (parser->lexer);
19207 /* Also handle the C99 syntax, '. id ='. */
19208 else if (cp_parser_allow_gnu_extensions_p (parser)
19209 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19210 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19211 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19213 /* Warn the user that they are using an extension. */
19214 pedwarn (input_location, OPT_Wpedantic,
19215 "ISO C++ does not allow C99 designated initializers");
19216 /* Consume the `.'. */
19217 cp_lexer_consume_token (parser->lexer);
19218 /* Consume the identifier. */
19219 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19220 /* Consume the `='. */
19221 cp_lexer_consume_token (parser->lexer);
19223 /* Also handle C99 array designators, '[ const ] ='. */
19224 else if (cp_parser_allow_gnu_extensions_p (parser)
19225 && !c_dialect_objc ()
19226 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19228 /* In C++11, [ could start a lambda-introducer. */
19229 bool non_const = false;
19231 cp_parser_parse_tentatively (parser);
19232 cp_lexer_consume_token (parser->lexer);
19233 designator = cp_parser_constant_expression (parser, true, &non_const);
19234 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19235 cp_parser_require (parser, CPP_EQ, RT_EQ);
19236 if (!cp_parser_parse_definitely (parser))
19237 designator = NULL_TREE;
19238 else if (non_const)
19239 require_potential_rvalue_constant_expression (designator);
19241 else
19242 designator = NULL_TREE;
19244 /* Parse the initializer. */
19245 initializer = cp_parser_initializer_clause (parser,
19246 &clause_non_constant_p);
19247 /* If any clause is non-constant, so is the entire initializer. */
19248 if (clause_non_constant_p)
19249 *non_constant_p = true;
19251 /* If we have an ellipsis, this is an initializer pack
19252 expansion. */
19253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19255 /* Consume the `...'. */
19256 cp_lexer_consume_token (parser->lexer);
19258 /* Turn the initializer into an initializer expansion. */
19259 initializer = make_pack_expansion (initializer);
19262 /* Add it to the vector. */
19263 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19265 /* If the next token is not a comma, we have reached the end of
19266 the list. */
19267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19268 break;
19270 /* Peek at the next token. */
19271 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19272 /* If the next token is a `}', then we're still done. An
19273 initializer-clause can have a trailing `,' after the
19274 initializer-list and before the closing `}'. */
19275 if (token->type == CPP_CLOSE_BRACE)
19276 break;
19278 /* Consume the `,' token. */
19279 cp_lexer_consume_token (parser->lexer);
19282 return v;
19285 /* Classes [gram.class] */
19287 /* Parse a class-name.
19289 class-name:
19290 identifier
19291 template-id
19293 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19294 to indicate that names looked up in dependent types should be
19295 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19296 keyword has been used to indicate that the name that appears next
19297 is a template. TAG_TYPE indicates the explicit tag given before
19298 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19299 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19300 is the class being defined in a class-head.
19302 Returns the TYPE_DECL representing the class. */
19304 static tree
19305 cp_parser_class_name (cp_parser *parser,
19306 bool typename_keyword_p,
19307 bool template_keyword_p,
19308 enum tag_types tag_type,
19309 bool check_dependency_p,
19310 bool class_head_p,
19311 bool is_declaration)
19313 tree decl;
19314 tree scope;
19315 bool typename_p;
19316 cp_token *token;
19317 tree identifier = NULL_TREE;
19319 /* All class-names start with an identifier. */
19320 token = cp_lexer_peek_token (parser->lexer);
19321 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19323 cp_parser_error (parser, "expected class-name");
19324 return error_mark_node;
19327 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19328 to a template-id, so we save it here. */
19329 scope = parser->scope;
19330 if (scope == error_mark_node)
19331 return error_mark_node;
19333 /* Any name names a type if we're following the `typename' keyword
19334 in a qualified name where the enclosing scope is type-dependent. */
19335 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19336 && dependent_type_p (scope));
19337 /* Handle the common case (an identifier, but not a template-id)
19338 efficiently. */
19339 if (token->type == CPP_NAME
19340 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19342 cp_token *identifier_token;
19343 bool ambiguous_p;
19345 /* Look for the identifier. */
19346 identifier_token = cp_lexer_peek_token (parser->lexer);
19347 ambiguous_p = identifier_token->error_reported;
19348 identifier = cp_parser_identifier (parser);
19349 /* If the next token isn't an identifier, we are certainly not
19350 looking at a class-name. */
19351 if (identifier == error_mark_node)
19352 decl = error_mark_node;
19353 /* If we know this is a type-name, there's no need to look it
19354 up. */
19355 else if (typename_p)
19356 decl = identifier;
19357 else
19359 tree ambiguous_decls;
19360 /* If we already know that this lookup is ambiguous, then
19361 we've already issued an error message; there's no reason
19362 to check again. */
19363 if (ambiguous_p)
19365 cp_parser_simulate_error (parser);
19366 return error_mark_node;
19368 /* If the next token is a `::', then the name must be a type
19369 name.
19371 [basic.lookup.qual]
19373 During the lookup for a name preceding the :: scope
19374 resolution operator, object, function, and enumerator
19375 names are ignored. */
19376 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19377 tag_type = typename_type;
19378 /* Look up the name. */
19379 decl = cp_parser_lookup_name (parser, identifier,
19380 tag_type,
19381 /*is_template=*/false,
19382 /*is_namespace=*/false,
19383 check_dependency_p,
19384 &ambiguous_decls,
19385 identifier_token->location);
19386 if (ambiguous_decls)
19388 if (cp_parser_parsing_tentatively (parser))
19389 cp_parser_simulate_error (parser);
19390 return error_mark_node;
19394 else
19396 /* Try a template-id. */
19397 decl = cp_parser_template_id (parser, template_keyword_p,
19398 check_dependency_p,
19399 tag_type,
19400 is_declaration);
19401 if (decl == error_mark_node)
19402 return error_mark_node;
19405 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19407 /* If this is a typename, create a TYPENAME_TYPE. */
19408 if (typename_p && decl != error_mark_node)
19410 decl = make_typename_type (scope, decl, typename_type,
19411 /*complain=*/tf_error);
19412 if (decl != error_mark_node)
19413 decl = TYPE_NAME (decl);
19416 decl = strip_using_decl (decl);
19418 /* Check to see that it is really the name of a class. */
19419 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19420 && identifier_p (TREE_OPERAND (decl, 0))
19421 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19422 /* Situations like this:
19424 template <typename T> struct A {
19425 typename T::template X<int>::I i;
19428 are problematic. Is `T::template X<int>' a class-name? The
19429 standard does not seem to be definitive, but there is no other
19430 valid interpretation of the following `::'. Therefore, those
19431 names are considered class-names. */
19433 decl = make_typename_type (scope, decl, tag_type, tf_error);
19434 if (decl != error_mark_node)
19435 decl = TYPE_NAME (decl);
19437 else if (TREE_CODE (decl) != TYPE_DECL
19438 || TREE_TYPE (decl) == error_mark_node
19439 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19440 /* In Objective-C 2.0, a classname followed by '.' starts a
19441 dot-syntax expression, and it's not a type-name. */
19442 || (c_dialect_objc ()
19443 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19444 && objc_is_class_name (decl)))
19445 decl = error_mark_node;
19447 if (decl == error_mark_node)
19448 cp_parser_error (parser, "expected class-name");
19449 else if (identifier && !parser->scope)
19450 maybe_note_name_used_in_class (identifier, decl);
19452 return decl;
19455 /* Parse a class-specifier.
19457 class-specifier:
19458 class-head { member-specification [opt] }
19460 Returns the TREE_TYPE representing the class. */
19462 static tree
19463 cp_parser_class_specifier_1 (cp_parser* parser)
19465 tree type;
19466 tree attributes = NULL_TREE;
19467 bool nested_name_specifier_p;
19468 unsigned saved_num_template_parameter_lists;
19469 bool saved_in_function_body;
19470 unsigned char in_statement;
19471 bool in_switch_statement_p;
19472 bool saved_in_unbraced_linkage_specification_p;
19473 tree old_scope = NULL_TREE;
19474 tree scope = NULL_TREE;
19475 cp_token *closing_brace;
19477 push_deferring_access_checks (dk_no_deferred);
19479 /* Parse the class-head. */
19480 type = cp_parser_class_head (parser,
19481 &nested_name_specifier_p);
19482 /* If the class-head was a semantic disaster, skip the entire body
19483 of the class. */
19484 if (!type)
19486 cp_parser_skip_to_end_of_block_or_statement (parser);
19487 pop_deferring_access_checks ();
19488 return error_mark_node;
19491 /* Look for the `{'. */
19492 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19494 pop_deferring_access_checks ();
19495 return error_mark_node;
19498 cp_ensure_no_omp_declare_simd (parser);
19500 /* Issue an error message if type-definitions are forbidden here. */
19501 cp_parser_check_type_definition (parser);
19502 /* Remember that we are defining one more class. */
19503 ++parser->num_classes_being_defined;
19504 /* Inside the class, surrounding template-parameter-lists do not
19505 apply. */
19506 saved_num_template_parameter_lists
19507 = parser->num_template_parameter_lists;
19508 parser->num_template_parameter_lists = 0;
19509 /* We are not in a function body. */
19510 saved_in_function_body = parser->in_function_body;
19511 parser->in_function_body = false;
19512 /* Or in a loop. */
19513 in_statement = parser->in_statement;
19514 parser->in_statement = 0;
19515 /* Or in a switch. */
19516 in_switch_statement_p = parser->in_switch_statement_p;
19517 parser->in_switch_statement_p = false;
19518 /* We are not immediately inside an extern "lang" block. */
19519 saved_in_unbraced_linkage_specification_p
19520 = parser->in_unbraced_linkage_specification_p;
19521 parser->in_unbraced_linkage_specification_p = false;
19523 /* Start the class. */
19524 if (nested_name_specifier_p)
19526 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19527 old_scope = push_inner_scope (scope);
19529 type = begin_class_definition (type);
19531 if (type == error_mark_node)
19532 /* If the type is erroneous, skip the entire body of the class. */
19533 cp_parser_skip_to_closing_brace (parser);
19534 else
19535 /* Parse the member-specification. */
19536 cp_parser_member_specification_opt (parser);
19538 /* Look for the trailing `}'. */
19539 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19540 /* Look for trailing attributes to apply to this class. */
19541 if (cp_parser_allow_gnu_extensions_p (parser))
19542 attributes = cp_parser_gnu_attributes_opt (parser);
19543 if (type != error_mark_node)
19544 type = finish_struct (type, attributes);
19545 if (nested_name_specifier_p)
19546 pop_inner_scope (old_scope, scope);
19548 /* We've finished a type definition. Check for the common syntax
19549 error of forgetting a semicolon after the definition. We need to
19550 be careful, as we can't just check for not-a-semicolon and be done
19551 with it; the user might have typed:
19553 class X { } c = ...;
19554 class X { } *p = ...;
19556 and so forth. Instead, enumerate all the possible tokens that
19557 might follow this production; if we don't see one of them, then
19558 complain and silently insert the semicolon. */
19560 cp_token *token = cp_lexer_peek_token (parser->lexer);
19561 bool want_semicolon = true;
19563 if (cp_next_tokens_can_be_std_attribute_p (parser))
19564 /* Don't try to parse c++11 attributes here. As per the
19565 grammar, that should be a task for
19566 cp_parser_decl_specifier_seq. */
19567 want_semicolon = false;
19569 switch (token->type)
19571 case CPP_NAME:
19572 case CPP_SEMICOLON:
19573 case CPP_MULT:
19574 case CPP_AND:
19575 case CPP_OPEN_PAREN:
19576 case CPP_CLOSE_PAREN:
19577 case CPP_COMMA:
19578 want_semicolon = false;
19579 break;
19581 /* While it's legal for type qualifiers and storage class
19582 specifiers to follow type definitions in the grammar, only
19583 compiler testsuites contain code like that. Assume that if
19584 we see such code, then what we're really seeing is a case
19585 like:
19587 class X { }
19588 const <type> var = ...;
19592 class Y { }
19593 static <type> func (...) ...
19595 i.e. the qualifier or specifier applies to the next
19596 declaration. To do so, however, we need to look ahead one
19597 more token to see if *that* token is a type specifier.
19599 This code could be improved to handle:
19601 class Z { }
19602 static const <type> var = ...; */
19603 case CPP_KEYWORD:
19604 if (keyword_is_decl_specifier (token->keyword))
19606 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19608 /* Handling user-defined types here would be nice, but very
19609 tricky. */
19610 want_semicolon
19611 = (lookahead->type == CPP_KEYWORD
19612 && keyword_begins_type_specifier (lookahead->keyword));
19614 break;
19615 default:
19616 break;
19619 /* If we don't have a type, then something is very wrong and we
19620 shouldn't try to do anything clever. Likewise for not seeing the
19621 closing brace. */
19622 if (closing_brace && TYPE_P (type) && want_semicolon)
19624 cp_token_position prev
19625 = cp_lexer_previous_token_position (parser->lexer);
19626 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19627 location_t loc = prev_token->location;
19629 if (CLASSTYPE_DECLARED_CLASS (type))
19630 error_at (loc, "expected %<;%> after class definition");
19631 else if (TREE_CODE (type) == RECORD_TYPE)
19632 error_at (loc, "expected %<;%> after struct definition");
19633 else if (TREE_CODE (type) == UNION_TYPE)
19634 error_at (loc, "expected %<;%> after union definition");
19635 else
19636 gcc_unreachable ();
19638 /* Unget one token and smash it to look as though we encountered
19639 a semicolon in the input stream. */
19640 cp_lexer_set_token_position (parser->lexer, prev);
19641 token = cp_lexer_peek_token (parser->lexer);
19642 token->type = CPP_SEMICOLON;
19643 token->keyword = RID_MAX;
19647 /* If this class is not itself within the scope of another class,
19648 then we need to parse the bodies of all of the queued function
19649 definitions. Note that the queued functions defined in a class
19650 are not always processed immediately following the
19651 class-specifier for that class. Consider:
19653 struct A {
19654 struct B { void f() { sizeof (A); } };
19657 If `f' were processed before the processing of `A' were
19658 completed, there would be no way to compute the size of `A'.
19659 Note that the nesting we are interested in here is lexical --
19660 not the semantic nesting given by TYPE_CONTEXT. In particular,
19661 for:
19663 struct A { struct B; };
19664 struct A::B { void f() { } };
19666 there is no need to delay the parsing of `A::B::f'. */
19667 if (--parser->num_classes_being_defined == 0)
19669 tree decl;
19670 tree class_type = NULL_TREE;
19671 tree pushed_scope = NULL_TREE;
19672 unsigned ix;
19673 cp_default_arg_entry *e;
19674 tree save_ccp, save_ccr;
19676 /* In a first pass, parse default arguments to the functions.
19677 Then, in a second pass, parse the bodies of the functions.
19678 This two-phased approach handles cases like:
19680 struct S {
19681 void f() { g(); }
19682 void g(int i = 3);
19686 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19688 decl = e->decl;
19689 /* If there are default arguments that have not yet been processed,
19690 take care of them now. */
19691 if (class_type != e->class_type)
19693 if (pushed_scope)
19694 pop_scope (pushed_scope);
19695 class_type = e->class_type;
19696 pushed_scope = push_scope (class_type);
19698 /* Make sure that any template parameters are in scope. */
19699 maybe_begin_member_template_processing (decl);
19700 /* Parse the default argument expressions. */
19701 cp_parser_late_parsing_default_args (parser, decl);
19702 /* Remove any template parameters from the symbol table. */
19703 maybe_end_member_template_processing ();
19705 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19706 /* Now parse any NSDMIs. */
19707 save_ccp = current_class_ptr;
19708 save_ccr = current_class_ref;
19709 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19711 if (class_type != DECL_CONTEXT (decl))
19713 if (pushed_scope)
19714 pop_scope (pushed_scope);
19715 class_type = DECL_CONTEXT (decl);
19716 pushed_scope = push_scope (class_type);
19718 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19719 cp_parser_late_parsing_nsdmi (parser, decl);
19721 vec_safe_truncate (unparsed_nsdmis, 0);
19722 current_class_ptr = save_ccp;
19723 current_class_ref = save_ccr;
19724 if (pushed_scope)
19725 pop_scope (pushed_scope);
19727 /* Now do some post-NSDMI bookkeeping. */
19728 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19729 after_nsdmi_defaulted_late_checks (class_type);
19730 vec_safe_truncate (unparsed_classes, 0);
19731 after_nsdmi_defaulted_late_checks (type);
19733 /* Now parse the body of the functions. */
19734 if (flag_openmp)
19736 /* OpenMP UDRs need to be parsed before all other functions. */
19737 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19738 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19739 cp_parser_late_parsing_for_member (parser, decl);
19740 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19741 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19742 cp_parser_late_parsing_for_member (parser, decl);
19744 else
19745 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19746 cp_parser_late_parsing_for_member (parser, decl);
19747 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19749 else
19750 vec_safe_push (unparsed_classes, type);
19752 /* Put back any saved access checks. */
19753 pop_deferring_access_checks ();
19755 /* Restore saved state. */
19756 parser->in_switch_statement_p = in_switch_statement_p;
19757 parser->in_statement = in_statement;
19758 parser->in_function_body = saved_in_function_body;
19759 parser->num_template_parameter_lists
19760 = saved_num_template_parameter_lists;
19761 parser->in_unbraced_linkage_specification_p
19762 = saved_in_unbraced_linkage_specification_p;
19764 return type;
19767 static tree
19768 cp_parser_class_specifier (cp_parser* parser)
19770 tree ret;
19771 timevar_push (TV_PARSE_STRUCT);
19772 ret = cp_parser_class_specifier_1 (parser);
19773 timevar_pop (TV_PARSE_STRUCT);
19774 return ret;
19777 /* Parse a class-head.
19779 class-head:
19780 class-key identifier [opt] base-clause [opt]
19781 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19782 class-key nested-name-specifier [opt] template-id
19783 base-clause [opt]
19785 class-virt-specifier:
19786 final
19788 GNU Extensions:
19789 class-key attributes identifier [opt] base-clause [opt]
19790 class-key attributes nested-name-specifier identifier base-clause [opt]
19791 class-key attributes nested-name-specifier [opt] template-id
19792 base-clause [opt]
19794 Upon return BASES is initialized to the list of base classes (or
19795 NULL, if there are none) in the same form returned by
19796 cp_parser_base_clause.
19798 Returns the TYPE of the indicated class. Sets
19799 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19800 involving a nested-name-specifier was used, and FALSE otherwise.
19802 Returns error_mark_node if this is not a class-head.
19804 Returns NULL_TREE if the class-head is syntactically valid, but
19805 semantically invalid in a way that means we should skip the entire
19806 body of the class. */
19808 static tree
19809 cp_parser_class_head (cp_parser* parser,
19810 bool* nested_name_specifier_p)
19812 tree nested_name_specifier;
19813 enum tag_types class_key;
19814 tree id = NULL_TREE;
19815 tree type = NULL_TREE;
19816 tree attributes;
19817 tree bases;
19818 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19819 bool template_id_p = false;
19820 bool qualified_p = false;
19821 bool invalid_nested_name_p = false;
19822 bool invalid_explicit_specialization_p = false;
19823 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19824 tree pushed_scope = NULL_TREE;
19825 unsigned num_templates;
19826 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19827 /* Assume no nested-name-specifier will be present. */
19828 *nested_name_specifier_p = false;
19829 /* Assume no template parameter lists will be used in defining the
19830 type. */
19831 num_templates = 0;
19832 parser->colon_corrects_to_scope_p = false;
19834 /* Look for the class-key. */
19835 class_key = cp_parser_class_key (parser);
19836 if (class_key == none_type)
19837 return error_mark_node;
19839 /* Parse the attributes. */
19840 attributes = cp_parser_attributes_opt (parser);
19842 /* If the next token is `::', that is invalid -- but sometimes
19843 people do try to write:
19845 struct ::S {};
19847 Handle this gracefully by accepting the extra qualifier, and then
19848 issuing an error about it later if this really is a
19849 class-head. If it turns out just to be an elaborated type
19850 specifier, remain silent. */
19851 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19852 qualified_p = true;
19854 push_deferring_access_checks (dk_no_check);
19856 /* Determine the name of the class. Begin by looking for an
19857 optional nested-name-specifier. */
19858 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19859 nested_name_specifier
19860 = cp_parser_nested_name_specifier_opt (parser,
19861 /*typename_keyword_p=*/false,
19862 /*check_dependency_p=*/false,
19863 /*type_p=*/true,
19864 /*is_declaration=*/false);
19865 /* If there was a nested-name-specifier, then there *must* be an
19866 identifier. */
19867 if (nested_name_specifier)
19869 type_start_token = cp_lexer_peek_token (parser->lexer);
19870 /* Although the grammar says `identifier', it really means
19871 `class-name' or `template-name'. You are only allowed to
19872 define a class that has already been declared with this
19873 syntax.
19875 The proposed resolution for Core Issue 180 says that wherever
19876 you see `class T::X' you should treat `X' as a type-name.
19878 It is OK to define an inaccessible class; for example:
19880 class A { class B; };
19881 class A::B {};
19883 We do not know if we will see a class-name, or a
19884 template-name. We look for a class-name first, in case the
19885 class-name is a template-id; if we looked for the
19886 template-name first we would stop after the template-name. */
19887 cp_parser_parse_tentatively (parser);
19888 type = cp_parser_class_name (parser,
19889 /*typename_keyword_p=*/false,
19890 /*template_keyword_p=*/false,
19891 class_type,
19892 /*check_dependency_p=*/false,
19893 /*class_head_p=*/true,
19894 /*is_declaration=*/false);
19895 /* If that didn't work, ignore the nested-name-specifier. */
19896 if (!cp_parser_parse_definitely (parser))
19898 invalid_nested_name_p = true;
19899 type_start_token = cp_lexer_peek_token (parser->lexer);
19900 id = cp_parser_identifier (parser);
19901 if (id == error_mark_node)
19902 id = NULL_TREE;
19904 /* If we could not find a corresponding TYPE, treat this
19905 declaration like an unqualified declaration. */
19906 if (type == error_mark_node)
19907 nested_name_specifier = NULL_TREE;
19908 /* Otherwise, count the number of templates used in TYPE and its
19909 containing scopes. */
19910 else
19912 tree scope;
19914 for (scope = TREE_TYPE (type);
19915 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19916 scope = get_containing_scope (scope))
19917 if (TYPE_P (scope)
19918 && CLASS_TYPE_P (scope)
19919 && CLASSTYPE_TEMPLATE_INFO (scope)
19920 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19921 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19922 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19923 ++num_templates;
19926 /* Otherwise, the identifier is optional. */
19927 else
19929 /* We don't know whether what comes next is a template-id,
19930 an identifier, or nothing at all. */
19931 cp_parser_parse_tentatively (parser);
19932 /* Check for a template-id. */
19933 type_start_token = cp_lexer_peek_token (parser->lexer);
19934 id = cp_parser_template_id (parser,
19935 /*template_keyword_p=*/false,
19936 /*check_dependency_p=*/true,
19937 class_key,
19938 /*is_declaration=*/true);
19939 /* If that didn't work, it could still be an identifier. */
19940 if (!cp_parser_parse_definitely (parser))
19942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19944 type_start_token = cp_lexer_peek_token (parser->lexer);
19945 id = cp_parser_identifier (parser);
19947 else
19948 id = NULL_TREE;
19950 else
19952 template_id_p = true;
19953 ++num_templates;
19957 pop_deferring_access_checks ();
19959 if (id)
19961 cp_parser_check_for_invalid_template_id (parser, id,
19962 class_key,
19963 type_start_token->location);
19965 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19967 /* If it's not a `:' or a `{' then we can't really be looking at a
19968 class-head, since a class-head only appears as part of a
19969 class-specifier. We have to detect this situation before calling
19970 xref_tag, since that has irreversible side-effects. */
19971 if (!cp_parser_next_token_starts_class_definition_p (parser))
19973 cp_parser_error (parser, "expected %<{%> or %<:%>");
19974 type = error_mark_node;
19975 goto out;
19978 /* At this point, we're going ahead with the class-specifier, even
19979 if some other problem occurs. */
19980 cp_parser_commit_to_tentative_parse (parser);
19981 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19983 cp_parser_error (parser,
19984 "cannot specify %<override%> for a class");
19985 type = error_mark_node;
19986 goto out;
19988 /* Issue the error about the overly-qualified name now. */
19989 if (qualified_p)
19991 cp_parser_error (parser,
19992 "global qualification of class name is invalid");
19993 type = error_mark_node;
19994 goto out;
19996 else if (invalid_nested_name_p)
19998 cp_parser_error (parser,
19999 "qualified name does not name a class");
20000 type = error_mark_node;
20001 goto out;
20003 else if (nested_name_specifier)
20005 tree scope;
20007 /* Reject typedef-names in class heads. */
20008 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20010 error_at (type_start_token->location,
20011 "invalid class name in declaration of %qD",
20012 type);
20013 type = NULL_TREE;
20014 goto done;
20017 /* Figure out in what scope the declaration is being placed. */
20018 scope = current_scope ();
20019 /* If that scope does not contain the scope in which the
20020 class was originally declared, the program is invalid. */
20021 if (scope && !is_ancestor (scope, nested_name_specifier))
20023 if (at_namespace_scope_p ())
20024 error_at (type_start_token->location,
20025 "declaration of %qD in namespace %qD which does not "
20026 "enclose %qD",
20027 type, scope, nested_name_specifier);
20028 else
20029 error_at (type_start_token->location,
20030 "declaration of %qD in %qD which does not enclose %qD",
20031 type, scope, nested_name_specifier);
20032 type = NULL_TREE;
20033 goto done;
20035 /* [dcl.meaning]
20037 A declarator-id shall not be qualified except for the
20038 definition of a ... nested class outside of its class
20039 ... [or] the definition or explicit instantiation of a
20040 class member of a namespace outside of its namespace. */
20041 if (scope == nested_name_specifier)
20043 permerror (nested_name_specifier_token_start->location,
20044 "extra qualification not allowed");
20045 nested_name_specifier = NULL_TREE;
20046 num_templates = 0;
20049 /* An explicit-specialization must be preceded by "template <>". If
20050 it is not, try to recover gracefully. */
20051 if (at_namespace_scope_p ()
20052 && parser->num_template_parameter_lists == 0
20053 && template_id_p)
20055 error_at (type_start_token->location,
20056 "an explicit specialization must be preceded by %<template <>%>");
20057 invalid_explicit_specialization_p = true;
20058 /* Take the same action that would have been taken by
20059 cp_parser_explicit_specialization. */
20060 ++parser->num_template_parameter_lists;
20061 begin_specialization ();
20063 /* There must be no "return" statements between this point and the
20064 end of this function; set "type "to the correct return value and
20065 use "goto done;" to return. */
20066 /* Make sure that the right number of template parameters were
20067 present. */
20068 if (!cp_parser_check_template_parameters (parser, num_templates,
20069 type_start_token->location,
20070 /*declarator=*/NULL))
20072 /* If something went wrong, there is no point in even trying to
20073 process the class-definition. */
20074 type = NULL_TREE;
20075 goto done;
20078 /* Look up the type. */
20079 if (template_id_p)
20081 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20082 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20083 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20085 error_at (type_start_token->location,
20086 "function template %qD redeclared as a class template", id);
20087 type = error_mark_node;
20089 else
20091 type = TREE_TYPE (id);
20092 type = maybe_process_partial_specialization (type);
20094 if (nested_name_specifier)
20095 pushed_scope = push_scope (nested_name_specifier);
20097 else if (nested_name_specifier)
20099 tree class_type;
20101 /* Given:
20103 template <typename T> struct S { struct T };
20104 template <typename T> struct S<T>::T { };
20106 we will get a TYPENAME_TYPE when processing the definition of
20107 `S::T'. We need to resolve it to the actual type before we
20108 try to define it. */
20109 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20111 class_type = resolve_typename_type (TREE_TYPE (type),
20112 /*only_current_p=*/false);
20113 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20114 type = TYPE_NAME (class_type);
20115 else
20117 cp_parser_error (parser, "could not resolve typename type");
20118 type = error_mark_node;
20122 if (maybe_process_partial_specialization (TREE_TYPE (type))
20123 == error_mark_node)
20125 type = NULL_TREE;
20126 goto done;
20129 class_type = current_class_type;
20130 /* Enter the scope indicated by the nested-name-specifier. */
20131 pushed_scope = push_scope (nested_name_specifier);
20132 /* Get the canonical version of this type. */
20133 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20134 /* Call push_template_decl if it seems like we should be defining a
20135 template either from the template headers or the type we're
20136 defining, so that we diagnose both extra and missing headers. */
20137 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20138 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
20139 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
20140 (TREE_TYPE (type)))))
20141 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20143 type = push_template_decl (type);
20144 if (type == error_mark_node)
20146 type = NULL_TREE;
20147 goto done;
20151 type = TREE_TYPE (type);
20152 *nested_name_specifier_p = true;
20154 else /* The name is not a nested name. */
20156 /* If the class was unnamed, create a dummy name. */
20157 if (!id)
20158 id = make_anon_name ();
20159 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20160 parser->num_template_parameter_lists);
20163 /* Indicate whether this class was declared as a `class' or as a
20164 `struct'. */
20165 if (TREE_CODE (type) == RECORD_TYPE)
20166 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20167 cp_parser_check_class_key (class_key, type);
20169 /* If this type was already complete, and we see another definition,
20170 that's an error. */
20171 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20173 error_at (type_start_token->location, "redefinition of %q#T",
20174 type);
20175 error_at (type_start_token->location, "previous definition of %q+#T",
20176 type);
20177 type = NULL_TREE;
20178 goto done;
20180 else if (type == error_mark_node)
20181 type = NULL_TREE;
20183 if (type)
20185 /* Apply attributes now, before any use of the class as a template
20186 argument in its base list. */
20187 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20188 fixup_attribute_variants (type);
20191 /* We will have entered the scope containing the class; the names of
20192 base classes should be looked up in that context. For example:
20194 struct A { struct B {}; struct C; };
20195 struct A::C : B {};
20197 is valid. */
20199 /* Get the list of base-classes, if there is one. */
20200 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20202 /* PR59482: enter the class scope so that base-specifiers are looked
20203 up correctly. */
20204 if (type)
20205 pushclass (type);
20206 bases = cp_parser_base_clause (parser);
20207 /* PR59482: get out of the previously pushed class scope so that the
20208 subsequent pops pop the right thing. */
20209 if (type)
20210 popclass ();
20212 else
20213 bases = NULL_TREE;
20215 /* If we're really defining a class, process the base classes.
20216 If they're invalid, fail. */
20217 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20218 && !xref_basetypes (type, bases))
20219 type = NULL_TREE;
20221 done:
20222 /* Leave the scope given by the nested-name-specifier. We will
20223 enter the class scope itself while processing the members. */
20224 if (pushed_scope)
20225 pop_scope (pushed_scope);
20227 if (invalid_explicit_specialization_p)
20229 end_specialization ();
20230 --parser->num_template_parameter_lists;
20233 if (type)
20234 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20235 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20236 CLASSTYPE_FINAL (type) = 1;
20237 out:
20238 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20239 return type;
20242 /* Parse a class-key.
20244 class-key:
20245 class
20246 struct
20247 union
20249 Returns the kind of class-key specified, or none_type to indicate
20250 error. */
20252 static enum tag_types
20253 cp_parser_class_key (cp_parser* parser)
20255 cp_token *token;
20256 enum tag_types tag_type;
20258 /* Look for the class-key. */
20259 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20260 if (!token)
20261 return none_type;
20263 /* Check to see if the TOKEN is a class-key. */
20264 tag_type = cp_parser_token_is_class_key (token);
20265 if (!tag_type)
20266 cp_parser_error (parser, "expected class-key");
20267 return tag_type;
20270 /* Parse a type-parameter-key.
20272 type-parameter-key:
20273 class
20274 typename
20277 static void
20278 cp_parser_type_parameter_key (cp_parser* parser)
20280 /* Look for the type-parameter-key. */
20281 enum tag_types tag_type = none_type;
20282 cp_token *token = cp_lexer_peek_token (parser->lexer);
20283 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20285 cp_lexer_consume_token (parser->lexer);
20286 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20287 /* typename is not allowed in a template template parameter
20288 by the standard until C++1Z. */
20289 pedwarn (token->location, OPT_Wpedantic,
20290 "ISO C++ forbids typename key in template template parameter;"
20291 " use -std=c++1z or -std=gnu++1z");
20293 else
20294 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20296 return;
20299 /* Parse an (optional) member-specification.
20301 member-specification:
20302 member-declaration member-specification [opt]
20303 access-specifier : member-specification [opt] */
20305 static void
20306 cp_parser_member_specification_opt (cp_parser* parser)
20308 while (true)
20310 cp_token *token;
20311 enum rid keyword;
20313 /* Peek at the next token. */
20314 token = cp_lexer_peek_token (parser->lexer);
20315 /* If it's a `}', or EOF then we've seen all the members. */
20316 if (token->type == CPP_CLOSE_BRACE
20317 || token->type == CPP_EOF
20318 || token->type == CPP_PRAGMA_EOL)
20319 break;
20321 /* See if this token is a keyword. */
20322 keyword = token->keyword;
20323 switch (keyword)
20325 case RID_PUBLIC:
20326 case RID_PROTECTED:
20327 case RID_PRIVATE:
20328 /* Consume the access-specifier. */
20329 cp_lexer_consume_token (parser->lexer);
20330 /* Remember which access-specifier is active. */
20331 current_access_specifier = token->u.value;
20332 /* Look for the `:'. */
20333 cp_parser_require (parser, CPP_COLON, RT_COLON);
20334 break;
20336 default:
20337 /* Accept #pragmas at class scope. */
20338 if (token->type == CPP_PRAGMA)
20340 cp_parser_pragma (parser, pragma_member);
20341 break;
20344 /* Otherwise, the next construction must be a
20345 member-declaration. */
20346 cp_parser_member_declaration (parser);
20351 /* Parse a member-declaration.
20353 member-declaration:
20354 decl-specifier-seq [opt] member-declarator-list [opt] ;
20355 function-definition ; [opt]
20356 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20357 using-declaration
20358 template-declaration
20359 alias-declaration
20361 member-declarator-list:
20362 member-declarator
20363 member-declarator-list , member-declarator
20365 member-declarator:
20366 declarator pure-specifier [opt]
20367 declarator constant-initializer [opt]
20368 identifier [opt] : constant-expression
20370 GNU Extensions:
20372 member-declaration:
20373 __extension__ member-declaration
20375 member-declarator:
20376 declarator attributes [opt] pure-specifier [opt]
20377 declarator attributes [opt] constant-initializer [opt]
20378 identifier [opt] attributes [opt] : constant-expression
20380 C++0x Extensions:
20382 member-declaration:
20383 static_assert-declaration */
20385 static void
20386 cp_parser_member_declaration (cp_parser* parser)
20388 cp_decl_specifier_seq decl_specifiers;
20389 tree prefix_attributes;
20390 tree decl;
20391 int declares_class_or_enum;
20392 bool friend_p;
20393 cp_token *token = NULL;
20394 cp_token *decl_spec_token_start = NULL;
20395 cp_token *initializer_token_start = NULL;
20396 int saved_pedantic;
20397 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20399 /* Check for the `__extension__' keyword. */
20400 if (cp_parser_extension_opt (parser, &saved_pedantic))
20402 /* Recurse. */
20403 cp_parser_member_declaration (parser);
20404 /* Restore the old value of the PEDANTIC flag. */
20405 pedantic = saved_pedantic;
20407 return;
20410 /* Check for a template-declaration. */
20411 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20413 /* An explicit specialization here is an error condition, and we
20414 expect the specialization handler to detect and report this. */
20415 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20416 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20417 cp_parser_explicit_specialization (parser);
20418 else
20419 cp_parser_template_declaration (parser, /*member_p=*/true);
20421 return;
20424 /* Check for a using-declaration. */
20425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20427 if (cxx_dialect < cxx11)
20429 /* Parse the using-declaration. */
20430 cp_parser_using_declaration (parser,
20431 /*access_declaration_p=*/false);
20432 return;
20434 else
20436 tree decl;
20437 bool alias_decl_expected;
20438 cp_parser_parse_tentatively (parser);
20439 decl = cp_parser_alias_declaration (parser);
20440 /* Note that if we actually see the '=' token after the
20441 identifier, cp_parser_alias_declaration commits the
20442 tentative parse. In that case, we really expects an
20443 alias-declaration. Otherwise, we expect a using
20444 declaration. */
20445 alias_decl_expected =
20446 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20447 cp_parser_parse_definitely (parser);
20449 if (alias_decl_expected)
20450 finish_member_declaration (decl);
20451 else
20452 cp_parser_using_declaration (parser,
20453 /*access_declaration_p=*/false);
20454 return;
20458 /* Check for @defs. */
20459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20461 tree ivar, member;
20462 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20463 ivar = ivar_chains;
20464 while (ivar)
20466 member = ivar;
20467 ivar = TREE_CHAIN (member);
20468 TREE_CHAIN (member) = NULL_TREE;
20469 finish_member_declaration (member);
20471 return;
20474 /* If the next token is `static_assert' we have a static assertion. */
20475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20477 cp_parser_static_assert (parser, /*member_p=*/true);
20478 return;
20481 parser->colon_corrects_to_scope_p = false;
20483 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20484 goto out;
20486 /* Parse the decl-specifier-seq. */
20487 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20488 cp_parser_decl_specifier_seq (parser,
20489 CP_PARSER_FLAGS_OPTIONAL,
20490 &decl_specifiers,
20491 &declares_class_or_enum);
20492 /* Check for an invalid type-name. */
20493 if (!decl_specifiers.any_type_specifiers_p
20494 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20495 goto out;
20496 /* If there is no declarator, then the decl-specifier-seq should
20497 specify a type. */
20498 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20500 /* If there was no decl-specifier-seq, and the next token is a
20501 `;', then we have something like:
20503 struct S { ; };
20505 [class.mem]
20507 Each member-declaration shall declare at least one member
20508 name of the class. */
20509 if (!decl_specifiers.any_specifiers_p)
20511 cp_token *token = cp_lexer_peek_token (parser->lexer);
20512 if (!in_system_header_at (token->location))
20513 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20515 else
20517 tree type;
20519 /* See if this declaration is a friend. */
20520 friend_p = cp_parser_friend_p (&decl_specifiers);
20521 /* If there were decl-specifiers, check to see if there was
20522 a class-declaration. */
20523 type = check_tag_decl (&decl_specifiers,
20524 /*explicit_type_instantiation_p=*/false);
20525 /* Nested classes have already been added to the class, but
20526 a `friend' needs to be explicitly registered. */
20527 if (friend_p)
20529 /* If the `friend' keyword was present, the friend must
20530 be introduced with a class-key. */
20531 if (!declares_class_or_enum && cxx_dialect < cxx11)
20532 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20533 "in C++03 a class-key must be used "
20534 "when declaring a friend");
20535 /* In this case:
20537 template <typename T> struct A {
20538 friend struct A<T>::B;
20541 A<T>::B will be represented by a TYPENAME_TYPE, and
20542 therefore not recognized by check_tag_decl. */
20543 if (!type)
20545 type = decl_specifiers.type;
20546 if (type && TREE_CODE (type) == TYPE_DECL)
20547 type = TREE_TYPE (type);
20549 if (!type || !TYPE_P (type))
20550 error_at (decl_spec_token_start->location,
20551 "friend declaration does not name a class or "
20552 "function");
20553 else
20554 make_friend_class (current_class_type, type,
20555 /*complain=*/true);
20557 /* If there is no TYPE, an error message will already have
20558 been issued. */
20559 else if (!type || type == error_mark_node)
20561 /* An anonymous aggregate has to be handled specially; such
20562 a declaration really declares a data member (with a
20563 particular type), as opposed to a nested class. */
20564 else if (ANON_AGGR_TYPE_P (type))
20566 /* C++11 9.5/6. */
20567 if (decl_specifiers.storage_class != sc_none)
20568 error_at (decl_spec_token_start->location,
20569 "a storage class on an anonymous aggregate "
20570 "in class scope is not allowed");
20572 /* Remove constructors and such from TYPE, now that we
20573 know it is an anonymous aggregate. */
20574 fixup_anonymous_aggr (type);
20575 /* And make the corresponding data member. */
20576 decl = build_decl (decl_spec_token_start->location,
20577 FIELD_DECL, NULL_TREE, type);
20578 /* Add it to the class. */
20579 finish_member_declaration (decl);
20581 else
20582 cp_parser_check_access_in_redeclaration
20583 (TYPE_NAME (type),
20584 decl_spec_token_start->location);
20587 else
20589 bool assume_semicolon = false;
20591 /* Clear attributes from the decl_specifiers but keep them
20592 around as prefix attributes that apply them to the entity
20593 being declared. */
20594 prefix_attributes = decl_specifiers.attributes;
20595 decl_specifiers.attributes = NULL_TREE;
20597 /* See if these declarations will be friends. */
20598 friend_p = cp_parser_friend_p (&decl_specifiers);
20600 /* Keep going until we hit the `;' at the end of the
20601 declaration. */
20602 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20604 tree attributes = NULL_TREE;
20605 tree first_attribute;
20607 /* Peek at the next token. */
20608 token = cp_lexer_peek_token (parser->lexer);
20610 /* Check for a bitfield declaration. */
20611 if (token->type == CPP_COLON
20612 || (token->type == CPP_NAME
20613 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20614 == CPP_COLON))
20616 tree identifier;
20617 tree width;
20619 /* Get the name of the bitfield. Note that we cannot just
20620 check TOKEN here because it may have been invalidated by
20621 the call to cp_lexer_peek_nth_token above. */
20622 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20623 identifier = cp_parser_identifier (parser);
20624 else
20625 identifier = NULL_TREE;
20627 /* Consume the `:' token. */
20628 cp_lexer_consume_token (parser->lexer);
20629 /* Get the width of the bitfield. */
20630 width
20631 = cp_parser_constant_expression (parser,
20632 /*allow_non_constant=*/false,
20633 NULL);
20635 /* Look for attributes that apply to the bitfield. */
20636 attributes = cp_parser_attributes_opt (parser);
20637 /* Remember which attributes are prefix attributes and
20638 which are not. */
20639 first_attribute = attributes;
20640 /* Combine the attributes. */
20641 attributes = chainon (prefix_attributes, attributes);
20643 /* Create the bitfield declaration. */
20644 decl = grokbitfield (identifier
20645 ? make_id_declarator (NULL_TREE,
20646 identifier,
20647 sfk_none)
20648 : NULL,
20649 &decl_specifiers,
20650 width,
20651 attributes);
20653 else
20655 cp_declarator *declarator;
20656 tree initializer;
20657 tree asm_specification;
20658 int ctor_dtor_or_conv_p;
20660 /* Parse the declarator. */
20661 declarator
20662 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20663 &ctor_dtor_or_conv_p,
20664 /*parenthesized_p=*/NULL,
20665 /*member_p=*/true,
20666 friend_p);
20668 /* If something went wrong parsing the declarator, make sure
20669 that we at least consume some tokens. */
20670 if (declarator == cp_error_declarator)
20672 /* Skip to the end of the statement. */
20673 cp_parser_skip_to_end_of_statement (parser);
20674 /* If the next token is not a semicolon, that is
20675 probably because we just skipped over the body of
20676 a function. So, we consume a semicolon if
20677 present, but do not issue an error message if it
20678 is not present. */
20679 if (cp_lexer_next_token_is (parser->lexer,
20680 CPP_SEMICOLON))
20681 cp_lexer_consume_token (parser->lexer);
20682 goto out;
20685 if (declares_class_or_enum & 2)
20686 cp_parser_check_for_definition_in_return_type
20687 (declarator, decl_specifiers.type,
20688 decl_specifiers.locations[ds_type_spec]);
20690 /* Look for an asm-specification. */
20691 asm_specification = cp_parser_asm_specification_opt (parser);
20692 /* Look for attributes that apply to the declaration. */
20693 attributes = cp_parser_attributes_opt (parser);
20694 /* Remember which attributes are prefix attributes and
20695 which are not. */
20696 first_attribute = attributes;
20697 /* Combine the attributes. */
20698 attributes = chainon (prefix_attributes, attributes);
20700 /* If it's an `=', then we have a constant-initializer or a
20701 pure-specifier. It is not correct to parse the
20702 initializer before registering the member declaration
20703 since the member declaration should be in scope while
20704 its initializer is processed. However, the rest of the
20705 front end does not yet provide an interface that allows
20706 us to handle this correctly. */
20707 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20709 /* In [class.mem]:
20711 A pure-specifier shall be used only in the declaration of
20712 a virtual function.
20714 A member-declarator can contain a constant-initializer
20715 only if it declares a static member of integral or
20716 enumeration type.
20718 Therefore, if the DECLARATOR is for a function, we look
20719 for a pure-specifier; otherwise, we look for a
20720 constant-initializer. When we call `grokfield', it will
20721 perform more stringent semantics checks. */
20722 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20723 if (function_declarator_p (declarator)
20724 || (decl_specifiers.type
20725 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20726 && declarator->kind == cdk_id
20727 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20728 == FUNCTION_TYPE)))
20729 initializer = cp_parser_pure_specifier (parser);
20730 else if (decl_specifiers.storage_class != sc_static)
20731 initializer = cp_parser_save_nsdmi (parser);
20732 else if (cxx_dialect >= cxx11)
20734 bool nonconst;
20735 /* Don't require a constant rvalue in C++11, since we
20736 might want a reference constant. We'll enforce
20737 constancy later. */
20738 cp_lexer_consume_token (parser->lexer);
20739 /* Parse the initializer. */
20740 initializer = cp_parser_initializer_clause (parser,
20741 &nonconst);
20743 else
20744 /* Parse the initializer. */
20745 initializer = cp_parser_constant_initializer (parser);
20747 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20748 && !function_declarator_p (declarator))
20750 bool x;
20751 if (decl_specifiers.storage_class != sc_static)
20752 initializer = cp_parser_save_nsdmi (parser);
20753 else
20754 initializer = cp_parser_initializer (parser, &x, &x);
20756 /* Otherwise, there is no initializer. */
20757 else
20758 initializer = NULL_TREE;
20760 /* See if we are probably looking at a function
20761 definition. We are certainly not looking at a
20762 member-declarator. Calling `grokfield' has
20763 side-effects, so we must not do it unless we are sure
20764 that we are looking at a member-declarator. */
20765 if (cp_parser_token_starts_function_definition_p
20766 (cp_lexer_peek_token (parser->lexer)))
20768 /* The grammar does not allow a pure-specifier to be
20769 used when a member function is defined. (It is
20770 possible that this fact is an oversight in the
20771 standard, since a pure function may be defined
20772 outside of the class-specifier. */
20773 if (initializer && initializer_token_start)
20774 error_at (initializer_token_start->location,
20775 "pure-specifier on function-definition");
20776 decl = cp_parser_save_member_function_body (parser,
20777 &decl_specifiers,
20778 declarator,
20779 attributes);
20780 if (parser->fully_implicit_function_template_p)
20781 decl = finish_fully_implicit_template (parser, decl);
20782 /* If the member was not a friend, declare it here. */
20783 if (!friend_p)
20784 finish_member_declaration (decl);
20785 /* Peek at the next token. */
20786 token = cp_lexer_peek_token (parser->lexer);
20787 /* If the next token is a semicolon, consume it. */
20788 if (token->type == CPP_SEMICOLON)
20789 cp_lexer_consume_token (parser->lexer);
20790 goto out;
20792 else
20793 if (declarator->kind == cdk_function)
20794 declarator->id_loc = token->location;
20795 /* Create the declaration. */
20796 decl = grokfield (declarator, &decl_specifiers,
20797 initializer, /*init_const_expr_p=*/true,
20798 asm_specification, attributes);
20799 if (parser->fully_implicit_function_template_p)
20801 if (friend_p)
20802 finish_fully_implicit_template (parser, 0);
20803 else
20804 decl = finish_fully_implicit_template (parser, decl);
20808 cp_finalize_omp_declare_simd (parser, decl);
20810 /* Reset PREFIX_ATTRIBUTES. */
20811 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20812 attributes = TREE_CHAIN (attributes);
20813 if (attributes)
20814 TREE_CHAIN (attributes) = NULL_TREE;
20816 /* If there is any qualification still in effect, clear it
20817 now; we will be starting fresh with the next declarator. */
20818 parser->scope = NULL_TREE;
20819 parser->qualifying_scope = NULL_TREE;
20820 parser->object_scope = NULL_TREE;
20821 /* If it's a `,', then there are more declarators. */
20822 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20824 cp_lexer_consume_token (parser->lexer);
20825 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20827 cp_token *token = cp_lexer_previous_token (parser->lexer);
20828 error_at (token->location,
20829 "stray %<,%> at end of member declaration");
20832 /* If the next token isn't a `;', then we have a parse error. */
20833 else if (cp_lexer_next_token_is_not (parser->lexer,
20834 CPP_SEMICOLON))
20836 /* The next token might be a ways away from where the
20837 actual semicolon is missing. Find the previous token
20838 and use that for our error position. */
20839 cp_token *token = cp_lexer_previous_token (parser->lexer);
20840 error_at (token->location,
20841 "expected %<;%> at end of member declaration");
20843 /* Assume that the user meant to provide a semicolon. If
20844 we were to cp_parser_skip_to_end_of_statement, we might
20845 skip to a semicolon inside a member function definition
20846 and issue nonsensical error messages. */
20847 assume_semicolon = true;
20850 if (decl)
20852 /* Add DECL to the list of members. */
20853 if (!friend_p)
20854 finish_member_declaration (decl);
20856 if (TREE_CODE (decl) == FUNCTION_DECL)
20857 cp_parser_save_default_args (parser, decl);
20858 else if (TREE_CODE (decl) == FIELD_DECL
20859 && !DECL_C_BIT_FIELD (decl)
20860 && DECL_INITIAL (decl))
20861 /* Add DECL to the queue of NSDMI to be parsed later. */
20862 vec_safe_push (unparsed_nsdmis, decl);
20865 if (assume_semicolon)
20866 goto out;
20870 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20871 out:
20872 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20875 /* Parse a pure-specifier.
20877 pure-specifier:
20880 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20881 Otherwise, ERROR_MARK_NODE is returned. */
20883 static tree
20884 cp_parser_pure_specifier (cp_parser* parser)
20886 cp_token *token;
20888 /* Look for the `=' token. */
20889 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20890 return error_mark_node;
20891 /* Look for the `0' token. */
20892 token = cp_lexer_peek_token (parser->lexer);
20894 if (token->type == CPP_EOF
20895 || token->type == CPP_PRAGMA_EOL)
20896 return error_mark_node;
20898 cp_lexer_consume_token (parser->lexer);
20900 /* Accept = default or = delete in c++0x mode. */
20901 if (token->keyword == RID_DEFAULT
20902 || token->keyword == RID_DELETE)
20904 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20905 return token->u.value;
20908 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20909 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20911 cp_parser_error (parser,
20912 "invalid pure specifier (only %<= 0%> is allowed)");
20913 cp_parser_skip_to_end_of_statement (parser);
20914 return error_mark_node;
20916 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20918 error_at (token->location, "templates may not be %<virtual%>");
20919 return error_mark_node;
20922 return integer_zero_node;
20925 /* Parse a constant-initializer.
20927 constant-initializer:
20928 = constant-expression
20930 Returns a representation of the constant-expression. */
20932 static tree
20933 cp_parser_constant_initializer (cp_parser* parser)
20935 /* Look for the `=' token. */
20936 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20937 return error_mark_node;
20939 /* It is invalid to write:
20941 struct S { static const int i = { 7 }; };
20944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20946 cp_parser_error (parser,
20947 "a brace-enclosed initializer is not allowed here");
20948 /* Consume the opening brace. */
20949 cp_lexer_consume_token (parser->lexer);
20950 /* Skip the initializer. */
20951 cp_parser_skip_to_closing_brace (parser);
20952 /* Look for the trailing `}'. */
20953 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20955 return error_mark_node;
20958 return cp_parser_constant_expression (parser,
20959 /*allow_non_constant=*/false,
20960 NULL);
20963 /* Derived classes [gram.class.derived] */
20965 /* Parse a base-clause.
20967 base-clause:
20968 : base-specifier-list
20970 base-specifier-list:
20971 base-specifier ... [opt]
20972 base-specifier-list , base-specifier ... [opt]
20974 Returns a TREE_LIST representing the base-classes, in the order in
20975 which they were declared. The representation of each node is as
20976 described by cp_parser_base_specifier.
20978 In the case that no bases are specified, this function will return
20979 NULL_TREE, not ERROR_MARK_NODE. */
20981 static tree
20982 cp_parser_base_clause (cp_parser* parser)
20984 tree bases = NULL_TREE;
20986 /* Look for the `:' that begins the list. */
20987 cp_parser_require (parser, CPP_COLON, RT_COLON);
20989 /* Scan the base-specifier-list. */
20990 while (true)
20992 cp_token *token;
20993 tree base;
20994 bool pack_expansion_p = false;
20996 /* Look for the base-specifier. */
20997 base = cp_parser_base_specifier (parser);
20998 /* Look for the (optional) ellipsis. */
20999 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21001 /* Consume the `...'. */
21002 cp_lexer_consume_token (parser->lexer);
21004 pack_expansion_p = true;
21007 /* Add BASE to the front of the list. */
21008 if (base && base != error_mark_node)
21010 if (pack_expansion_p)
21011 /* Make this a pack expansion type. */
21012 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21014 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21016 TREE_CHAIN (base) = bases;
21017 bases = base;
21020 /* Peek at the next token. */
21021 token = cp_lexer_peek_token (parser->lexer);
21022 /* If it's not a comma, then the list is complete. */
21023 if (token->type != CPP_COMMA)
21024 break;
21025 /* Consume the `,'. */
21026 cp_lexer_consume_token (parser->lexer);
21029 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21030 base class had a qualified name. However, the next name that
21031 appears is certainly not qualified. */
21032 parser->scope = NULL_TREE;
21033 parser->qualifying_scope = NULL_TREE;
21034 parser->object_scope = NULL_TREE;
21036 return nreverse (bases);
21039 /* Parse a base-specifier.
21041 base-specifier:
21042 :: [opt] nested-name-specifier [opt] class-name
21043 virtual access-specifier [opt] :: [opt] nested-name-specifier
21044 [opt] class-name
21045 access-specifier virtual [opt] :: [opt] nested-name-specifier
21046 [opt] class-name
21048 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21049 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21050 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21051 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21053 static tree
21054 cp_parser_base_specifier (cp_parser* parser)
21056 cp_token *token;
21057 bool done = false;
21058 bool virtual_p = false;
21059 bool duplicate_virtual_error_issued_p = false;
21060 bool duplicate_access_error_issued_p = false;
21061 bool class_scope_p, template_p;
21062 tree access = access_default_node;
21063 tree type;
21065 /* Process the optional `virtual' and `access-specifier'. */
21066 while (!done)
21068 /* Peek at the next token. */
21069 token = cp_lexer_peek_token (parser->lexer);
21070 /* Process `virtual'. */
21071 switch (token->keyword)
21073 case RID_VIRTUAL:
21074 /* If `virtual' appears more than once, issue an error. */
21075 if (virtual_p && !duplicate_virtual_error_issued_p)
21077 cp_parser_error (parser,
21078 "%<virtual%> specified more than once in base-specified");
21079 duplicate_virtual_error_issued_p = true;
21082 virtual_p = true;
21084 /* Consume the `virtual' token. */
21085 cp_lexer_consume_token (parser->lexer);
21087 break;
21089 case RID_PUBLIC:
21090 case RID_PROTECTED:
21091 case RID_PRIVATE:
21092 /* If more than one access specifier appears, issue an
21093 error. */
21094 if (access != access_default_node
21095 && !duplicate_access_error_issued_p)
21097 cp_parser_error (parser,
21098 "more than one access specifier in base-specified");
21099 duplicate_access_error_issued_p = true;
21102 access = ridpointers[(int) token->keyword];
21104 /* Consume the access-specifier. */
21105 cp_lexer_consume_token (parser->lexer);
21107 break;
21109 default:
21110 done = true;
21111 break;
21114 /* It is not uncommon to see programs mechanically, erroneously, use
21115 the 'typename' keyword to denote (dependent) qualified types
21116 as base classes. */
21117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21119 token = cp_lexer_peek_token (parser->lexer);
21120 if (!processing_template_decl)
21121 error_at (token->location,
21122 "keyword %<typename%> not allowed outside of templates");
21123 else
21124 error_at (token->location,
21125 "keyword %<typename%> not allowed in this context "
21126 "(the base class is implicitly a type)");
21127 cp_lexer_consume_token (parser->lexer);
21130 /* Look for the optional `::' operator. */
21131 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21132 /* Look for the nested-name-specifier. The simplest way to
21133 implement:
21135 [temp.res]
21137 The keyword `typename' is not permitted in a base-specifier or
21138 mem-initializer; in these contexts a qualified name that
21139 depends on a template-parameter is implicitly assumed to be a
21140 type name.
21142 is to pretend that we have seen the `typename' keyword at this
21143 point. */
21144 cp_parser_nested_name_specifier_opt (parser,
21145 /*typename_keyword_p=*/true,
21146 /*check_dependency_p=*/true,
21147 typename_type,
21148 /*is_declaration=*/true);
21149 /* If the base class is given by a qualified name, assume that names
21150 we see are type names or templates, as appropriate. */
21151 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21152 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21154 if (!parser->scope
21155 && cp_lexer_next_token_is_decltype (parser->lexer))
21156 /* DR 950 allows decltype as a base-specifier. */
21157 type = cp_parser_decltype (parser);
21158 else
21160 /* Otherwise, look for the class-name. */
21161 type = cp_parser_class_name (parser,
21162 class_scope_p,
21163 template_p,
21164 typename_type,
21165 /*check_dependency_p=*/true,
21166 /*class_head_p=*/false,
21167 /*is_declaration=*/true);
21168 type = TREE_TYPE (type);
21171 if (type == error_mark_node)
21172 return error_mark_node;
21174 return finish_base_specifier (type, access, virtual_p);
21177 /* Exception handling [gram.exception] */
21179 /* Parse an (optional) noexcept-specification.
21181 noexcept-specification:
21182 noexcept ( constant-expression ) [opt]
21184 If no noexcept-specification is present, returns NULL_TREE.
21185 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21186 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21187 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21188 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21189 in which case a boolean condition is returned instead. */
21191 static tree
21192 cp_parser_noexcept_specification_opt (cp_parser* parser,
21193 bool require_constexpr,
21194 bool* consumed_expr,
21195 bool return_cond)
21197 cp_token *token;
21198 const char *saved_message;
21200 /* Peek at the next token. */
21201 token = cp_lexer_peek_token (parser->lexer);
21203 /* Is it a noexcept-specification? */
21204 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21206 tree expr;
21207 cp_lexer_consume_token (parser->lexer);
21209 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21211 cp_lexer_consume_token (parser->lexer);
21213 if (require_constexpr)
21215 /* Types may not be defined in an exception-specification. */
21216 saved_message = parser->type_definition_forbidden_message;
21217 parser->type_definition_forbidden_message
21218 = G_("types may not be defined in an exception-specification");
21220 expr = cp_parser_constant_expression (parser, false, NULL);
21222 /* Restore the saved message. */
21223 parser->type_definition_forbidden_message = saved_message;
21225 else
21227 expr = cp_parser_expression (parser);
21228 *consumed_expr = true;
21231 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21233 else
21235 expr = boolean_true_node;
21236 if (!require_constexpr)
21237 *consumed_expr = false;
21240 /* We cannot build a noexcept-spec right away because this will check
21241 that expr is a constexpr. */
21242 if (!return_cond)
21243 return build_noexcept_spec (expr, tf_warning_or_error);
21244 else
21245 return expr;
21247 else
21248 return NULL_TREE;
21251 /* Parse an (optional) exception-specification.
21253 exception-specification:
21254 throw ( type-id-list [opt] )
21256 Returns a TREE_LIST representing the exception-specification. The
21257 TREE_VALUE of each node is a type. */
21259 static tree
21260 cp_parser_exception_specification_opt (cp_parser* parser)
21262 cp_token *token;
21263 tree type_id_list;
21264 const char *saved_message;
21266 /* Peek at the next token. */
21267 token = cp_lexer_peek_token (parser->lexer);
21269 /* Is it a noexcept-specification? */
21270 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21271 false);
21272 if (type_id_list != NULL_TREE)
21273 return type_id_list;
21275 /* If it's not `throw', then there's no exception-specification. */
21276 if (!cp_parser_is_keyword (token, RID_THROW))
21277 return NULL_TREE;
21279 #if 0
21280 /* Enable this once a lot of code has transitioned to noexcept? */
21281 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21282 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21283 "deprecated in C++0x; use %<noexcept%> instead");
21284 #endif
21286 /* Consume the `throw'. */
21287 cp_lexer_consume_token (parser->lexer);
21289 /* Look for the `('. */
21290 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21292 /* Peek at the next token. */
21293 token = cp_lexer_peek_token (parser->lexer);
21294 /* If it's not a `)', then there is a type-id-list. */
21295 if (token->type != CPP_CLOSE_PAREN)
21297 /* Types may not be defined in an exception-specification. */
21298 saved_message = parser->type_definition_forbidden_message;
21299 parser->type_definition_forbidden_message
21300 = G_("types may not be defined in an exception-specification");
21301 /* Parse the type-id-list. */
21302 type_id_list = cp_parser_type_id_list (parser);
21303 /* Restore the saved message. */
21304 parser->type_definition_forbidden_message = saved_message;
21306 else
21307 type_id_list = empty_except_spec;
21309 /* Look for the `)'. */
21310 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21312 return type_id_list;
21315 /* Parse an (optional) type-id-list.
21317 type-id-list:
21318 type-id ... [opt]
21319 type-id-list , type-id ... [opt]
21321 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21322 in the order that the types were presented. */
21324 static tree
21325 cp_parser_type_id_list (cp_parser* parser)
21327 tree types = NULL_TREE;
21329 while (true)
21331 cp_token *token;
21332 tree type;
21334 /* Get the next type-id. */
21335 type = cp_parser_type_id (parser);
21336 /* Parse the optional ellipsis. */
21337 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21339 /* Consume the `...'. */
21340 cp_lexer_consume_token (parser->lexer);
21342 /* Turn the type into a pack expansion expression. */
21343 type = make_pack_expansion (type);
21345 /* Add it to the list. */
21346 types = add_exception_specifier (types, type, /*complain=*/1);
21347 /* Peek at the next token. */
21348 token = cp_lexer_peek_token (parser->lexer);
21349 /* If it is not a `,', we are done. */
21350 if (token->type != CPP_COMMA)
21351 break;
21352 /* Consume the `,'. */
21353 cp_lexer_consume_token (parser->lexer);
21356 return nreverse (types);
21359 /* Parse a try-block.
21361 try-block:
21362 try compound-statement handler-seq */
21364 static tree
21365 cp_parser_try_block (cp_parser* parser)
21367 tree try_block;
21369 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21370 try_block = begin_try_block ();
21371 cp_parser_compound_statement (parser, NULL, true, false);
21372 finish_try_block (try_block);
21373 cp_parser_handler_seq (parser);
21374 finish_handler_sequence (try_block);
21376 return try_block;
21379 /* Parse a function-try-block.
21381 function-try-block:
21382 try ctor-initializer [opt] function-body handler-seq */
21384 static bool
21385 cp_parser_function_try_block (cp_parser* parser)
21387 tree compound_stmt;
21388 tree try_block;
21389 bool ctor_initializer_p;
21391 /* Look for the `try' keyword. */
21392 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21393 return false;
21394 /* Let the rest of the front end know where we are. */
21395 try_block = begin_function_try_block (&compound_stmt);
21396 /* Parse the function-body. */
21397 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21398 (parser, /*in_function_try_block=*/true);
21399 /* We're done with the `try' part. */
21400 finish_function_try_block (try_block);
21401 /* Parse the handlers. */
21402 cp_parser_handler_seq (parser);
21403 /* We're done with the handlers. */
21404 finish_function_handler_sequence (try_block, compound_stmt);
21406 return ctor_initializer_p;
21409 /* Parse a handler-seq.
21411 handler-seq:
21412 handler handler-seq [opt] */
21414 static void
21415 cp_parser_handler_seq (cp_parser* parser)
21417 while (true)
21419 cp_token *token;
21421 /* Parse the handler. */
21422 cp_parser_handler (parser);
21423 /* Peek at the next token. */
21424 token = cp_lexer_peek_token (parser->lexer);
21425 /* If it's not `catch' then there are no more handlers. */
21426 if (!cp_parser_is_keyword (token, RID_CATCH))
21427 break;
21431 /* Parse a handler.
21433 handler:
21434 catch ( exception-declaration ) compound-statement */
21436 static void
21437 cp_parser_handler (cp_parser* parser)
21439 tree handler;
21440 tree declaration;
21442 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21443 handler = begin_handler ();
21444 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21445 declaration = cp_parser_exception_declaration (parser);
21446 finish_handler_parms (declaration, handler);
21447 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21448 cp_parser_compound_statement (parser, NULL, false, false);
21449 finish_handler (handler);
21452 /* Parse an exception-declaration.
21454 exception-declaration:
21455 type-specifier-seq declarator
21456 type-specifier-seq abstract-declarator
21457 type-specifier-seq
21460 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21461 ellipsis variant is used. */
21463 static tree
21464 cp_parser_exception_declaration (cp_parser* parser)
21466 cp_decl_specifier_seq type_specifiers;
21467 cp_declarator *declarator;
21468 const char *saved_message;
21470 /* If it's an ellipsis, it's easy to handle. */
21471 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21473 /* Consume the `...' token. */
21474 cp_lexer_consume_token (parser->lexer);
21475 return NULL_TREE;
21478 /* Types may not be defined in exception-declarations. */
21479 saved_message = parser->type_definition_forbidden_message;
21480 parser->type_definition_forbidden_message
21481 = G_("types may not be defined in exception-declarations");
21483 /* Parse the type-specifier-seq. */
21484 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21485 /*is_trailing_return=*/false,
21486 &type_specifiers);
21487 /* If it's a `)', then there is no declarator. */
21488 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21489 declarator = NULL;
21490 else
21491 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21492 /*ctor_dtor_or_conv_p=*/NULL,
21493 /*parenthesized_p=*/NULL,
21494 /*member_p=*/false,
21495 /*friend_p=*/false);
21497 /* Restore the saved message. */
21498 parser->type_definition_forbidden_message = saved_message;
21500 if (!type_specifiers.any_specifiers_p)
21501 return error_mark_node;
21503 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21506 /* Parse a throw-expression.
21508 throw-expression:
21509 throw assignment-expression [opt]
21511 Returns a THROW_EXPR representing the throw-expression. */
21513 static tree
21514 cp_parser_throw_expression (cp_parser* parser)
21516 tree expression;
21517 cp_token* token;
21519 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21520 token = cp_lexer_peek_token (parser->lexer);
21521 /* Figure out whether or not there is an assignment-expression
21522 following the "throw" keyword. */
21523 if (token->type == CPP_COMMA
21524 || token->type == CPP_SEMICOLON
21525 || token->type == CPP_CLOSE_PAREN
21526 || token->type == CPP_CLOSE_SQUARE
21527 || token->type == CPP_CLOSE_BRACE
21528 || token->type == CPP_COLON)
21529 expression = NULL_TREE;
21530 else
21531 expression = cp_parser_assignment_expression (parser,
21532 /*cast_p=*/false, NULL);
21534 return build_throw (expression);
21537 /* GNU Extensions */
21539 /* Parse an (optional) asm-specification.
21541 asm-specification:
21542 asm ( string-literal )
21544 If the asm-specification is present, returns a STRING_CST
21545 corresponding to the string-literal. Otherwise, returns
21546 NULL_TREE. */
21548 static tree
21549 cp_parser_asm_specification_opt (cp_parser* parser)
21551 cp_token *token;
21552 tree asm_specification;
21554 /* Peek at the next token. */
21555 token = cp_lexer_peek_token (parser->lexer);
21556 /* If the next token isn't the `asm' keyword, then there's no
21557 asm-specification. */
21558 if (!cp_parser_is_keyword (token, RID_ASM))
21559 return NULL_TREE;
21561 /* Consume the `asm' token. */
21562 cp_lexer_consume_token (parser->lexer);
21563 /* Look for the `('. */
21564 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21566 /* Look for the string-literal. */
21567 asm_specification = cp_parser_string_literal (parser, false, false);
21569 /* Look for the `)'. */
21570 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21572 return asm_specification;
21575 /* Parse an asm-operand-list.
21577 asm-operand-list:
21578 asm-operand
21579 asm-operand-list , asm-operand
21581 asm-operand:
21582 string-literal ( expression )
21583 [ string-literal ] string-literal ( expression )
21585 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21586 each node is the expression. The TREE_PURPOSE is itself a
21587 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21588 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21589 is a STRING_CST for the string literal before the parenthesis. Returns
21590 ERROR_MARK_NODE if any of the operands are invalid. */
21592 static tree
21593 cp_parser_asm_operand_list (cp_parser* parser)
21595 tree asm_operands = NULL_TREE;
21596 bool invalid_operands = false;
21598 while (true)
21600 tree string_literal;
21601 tree expression;
21602 tree name;
21604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21606 /* Consume the `[' token. */
21607 cp_lexer_consume_token (parser->lexer);
21608 /* Read the operand name. */
21609 name = cp_parser_identifier (parser);
21610 if (name != error_mark_node)
21611 name = build_string (IDENTIFIER_LENGTH (name),
21612 IDENTIFIER_POINTER (name));
21613 /* Look for the closing `]'. */
21614 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21616 else
21617 name = NULL_TREE;
21618 /* Look for the string-literal. */
21619 string_literal = cp_parser_string_literal (parser, false, false);
21621 /* Look for the `('. */
21622 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21623 /* Parse the expression. */
21624 expression = cp_parser_expression (parser);
21625 /* Look for the `)'. */
21626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21628 if (name == error_mark_node
21629 || string_literal == error_mark_node
21630 || expression == error_mark_node)
21631 invalid_operands = true;
21633 /* Add this operand to the list. */
21634 asm_operands = tree_cons (build_tree_list (name, string_literal),
21635 expression,
21636 asm_operands);
21637 /* If the next token is not a `,', there are no more
21638 operands. */
21639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21640 break;
21641 /* Consume the `,'. */
21642 cp_lexer_consume_token (parser->lexer);
21645 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21648 /* Parse an asm-clobber-list.
21650 asm-clobber-list:
21651 string-literal
21652 asm-clobber-list , string-literal
21654 Returns a TREE_LIST, indicating the clobbers in the order that they
21655 appeared. The TREE_VALUE of each node is a STRING_CST. */
21657 static tree
21658 cp_parser_asm_clobber_list (cp_parser* parser)
21660 tree clobbers = NULL_TREE;
21662 while (true)
21664 tree string_literal;
21666 /* Look for the string literal. */
21667 string_literal = cp_parser_string_literal (parser, false, false);
21668 /* Add it to the list. */
21669 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21670 /* If the next token is not a `,', then the list is
21671 complete. */
21672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21673 break;
21674 /* Consume the `,' token. */
21675 cp_lexer_consume_token (parser->lexer);
21678 return clobbers;
21681 /* Parse an asm-label-list.
21683 asm-label-list:
21684 identifier
21685 asm-label-list , identifier
21687 Returns a TREE_LIST, indicating the labels in the order that they
21688 appeared. The TREE_VALUE of each node is a label. */
21690 static tree
21691 cp_parser_asm_label_list (cp_parser* parser)
21693 tree labels = NULL_TREE;
21695 while (true)
21697 tree identifier, label, name;
21699 /* Look for the identifier. */
21700 identifier = cp_parser_identifier (parser);
21701 if (!error_operand_p (identifier))
21703 label = lookup_label (identifier);
21704 if (TREE_CODE (label) == LABEL_DECL)
21706 TREE_USED (label) = 1;
21707 check_goto (label);
21708 name = build_string (IDENTIFIER_LENGTH (identifier),
21709 IDENTIFIER_POINTER (identifier));
21710 labels = tree_cons (name, label, labels);
21713 /* If the next token is not a `,', then the list is
21714 complete. */
21715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21716 break;
21717 /* Consume the `,' token. */
21718 cp_lexer_consume_token (parser->lexer);
21721 return nreverse (labels);
21724 /* Return TRUE iff the next tokens in the stream are possibly the
21725 beginning of a GNU extension attribute. */
21727 static bool
21728 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21730 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21733 /* Return TRUE iff the next tokens in the stream are possibly the
21734 beginning of a standard C++-11 attribute specifier. */
21736 static bool
21737 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21739 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21742 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21743 beginning of a standard C++-11 attribute specifier. */
21745 static bool
21746 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21748 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21750 return (cxx_dialect >= cxx11
21751 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21752 || (token->type == CPP_OPEN_SQUARE
21753 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21754 && token->type == CPP_OPEN_SQUARE)));
21757 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21758 beginning of a GNU extension attribute. */
21760 static bool
21761 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21763 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21765 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21768 /* Return true iff the next tokens can be the beginning of either a
21769 GNU attribute list, or a standard C++11 attribute sequence. */
21771 static bool
21772 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21774 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21775 || cp_next_tokens_can_be_std_attribute_p (parser));
21778 /* Return true iff the next Nth tokens can be the beginning of either
21779 a GNU attribute list, or a standard C++11 attribute sequence. */
21781 static bool
21782 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21784 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21785 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21788 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21789 of GNU attributes, or return NULL. */
21791 static tree
21792 cp_parser_attributes_opt (cp_parser *parser)
21794 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21795 return cp_parser_gnu_attributes_opt (parser);
21796 return cp_parser_std_attribute_spec_seq (parser);
21799 #define CILK_SIMD_FN_CLAUSE_MASK \
21800 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21801 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21802 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21803 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21804 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21806 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21807 vector [(<clauses>)] */
21809 static void
21810 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21812 bool first_p = parser->cilk_simd_fn_info == NULL;
21813 cp_token *token = v_token;
21814 if (first_p)
21816 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21817 parser->cilk_simd_fn_info->error_seen = false;
21818 parser->cilk_simd_fn_info->fndecl_seen = false;
21819 parser->cilk_simd_fn_info->tokens = vNULL;
21821 int paren_scope = 0;
21822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21824 cp_lexer_consume_token (parser->lexer);
21825 v_token = cp_lexer_peek_token (parser->lexer);
21826 paren_scope++;
21828 while (paren_scope > 0)
21830 token = cp_lexer_peek_token (parser->lexer);
21831 if (token->type == CPP_OPEN_PAREN)
21832 paren_scope++;
21833 else if (token->type == CPP_CLOSE_PAREN)
21834 paren_scope--;
21835 /* Do not push the last ')' */
21836 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21837 cp_lexer_consume_token (parser->lexer);
21840 token->type = CPP_PRAGMA_EOL;
21841 parser->lexer->next_token = token;
21842 cp_lexer_consume_token (parser->lexer);
21844 struct cp_token_cache *cp
21845 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21846 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21849 /* Parse an (optional) series of attributes.
21851 attributes:
21852 attributes attribute
21854 attribute:
21855 __attribute__ (( attribute-list [opt] ))
21857 The return value is as for cp_parser_gnu_attribute_list. */
21859 static tree
21860 cp_parser_gnu_attributes_opt (cp_parser* parser)
21862 tree attributes = NULL_TREE;
21864 while (true)
21866 cp_token *token;
21867 tree attribute_list;
21868 bool ok = true;
21870 /* Peek at the next token. */
21871 token = cp_lexer_peek_token (parser->lexer);
21872 /* If it's not `__attribute__', then we're done. */
21873 if (token->keyword != RID_ATTRIBUTE)
21874 break;
21876 /* Consume the `__attribute__' keyword. */
21877 cp_lexer_consume_token (parser->lexer);
21878 /* Look for the two `(' tokens. */
21879 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21880 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21882 /* Peek at the next token. */
21883 token = cp_lexer_peek_token (parser->lexer);
21884 if (token->type != CPP_CLOSE_PAREN)
21885 /* Parse the attribute-list. */
21886 attribute_list = cp_parser_gnu_attribute_list (parser);
21887 else
21888 /* If the next token is a `)', then there is no attribute
21889 list. */
21890 attribute_list = NULL;
21892 /* Look for the two `)' tokens. */
21893 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21894 ok = false;
21895 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21896 ok = false;
21897 if (!ok)
21898 cp_parser_skip_to_end_of_statement (parser);
21900 /* Add these new attributes to the list. */
21901 attributes = chainon (attributes, attribute_list);
21904 return attributes;
21907 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21908 "__vector" or "__vector__." */
21910 static inline bool
21911 is_cilkplus_vector_p (tree name)
21913 if (flag_cilkplus && is_attribute_p ("vector", name))
21914 return true;
21915 return false;
21918 /* Parse a GNU attribute-list.
21920 attribute-list:
21921 attribute
21922 attribute-list , attribute
21924 attribute:
21925 identifier
21926 identifier ( identifier )
21927 identifier ( identifier , expression-list )
21928 identifier ( expression-list )
21930 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21931 to an attribute. The TREE_PURPOSE of each node is the identifier
21932 indicating which attribute is in use. The TREE_VALUE represents
21933 the arguments, if any. */
21935 static tree
21936 cp_parser_gnu_attribute_list (cp_parser* parser)
21938 tree attribute_list = NULL_TREE;
21939 bool save_translate_strings_p = parser->translate_strings_p;
21941 parser->translate_strings_p = false;
21942 while (true)
21944 cp_token *token;
21945 tree identifier;
21946 tree attribute;
21948 /* Look for the identifier. We also allow keywords here; for
21949 example `__attribute__ ((const))' is legal. */
21950 token = cp_lexer_peek_token (parser->lexer);
21951 if (token->type == CPP_NAME
21952 || token->type == CPP_KEYWORD)
21954 tree arguments = NULL_TREE;
21956 /* Consume the token, but save it since we need it for the
21957 SIMD enabled function parsing. */
21958 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21960 /* Save away the identifier that indicates which attribute
21961 this is. */
21962 identifier = (token->type == CPP_KEYWORD)
21963 /* For keywords, use the canonical spelling, not the
21964 parsed identifier. */
21965 ? ridpointers[(int) token->keyword]
21966 : id_token->u.value;
21968 attribute = build_tree_list (identifier, NULL_TREE);
21970 /* Peek at the next token. */
21971 token = cp_lexer_peek_token (parser->lexer);
21972 /* If it's an `(', then parse the attribute arguments. */
21973 if (token->type == CPP_OPEN_PAREN)
21975 vec<tree, va_gc> *vec;
21976 int attr_flag = (attribute_takes_identifier_p (identifier)
21977 ? id_attr : normal_attr);
21978 if (is_cilkplus_vector_p (identifier))
21980 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21981 continue;
21983 else
21984 vec = cp_parser_parenthesized_expression_list
21985 (parser, attr_flag, /*cast_p=*/false,
21986 /*allow_expansion_p=*/false,
21987 /*non_constant_p=*/NULL);
21988 if (vec == NULL)
21989 arguments = error_mark_node;
21990 else
21992 arguments = build_tree_list_vec (vec);
21993 release_tree_vector (vec);
21995 /* Save the arguments away. */
21996 TREE_VALUE (attribute) = arguments;
21998 else if (is_cilkplus_vector_p (identifier))
22000 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22001 continue;
22004 if (arguments != error_mark_node)
22006 /* Add this attribute to the list. */
22007 TREE_CHAIN (attribute) = attribute_list;
22008 attribute_list = attribute;
22011 token = cp_lexer_peek_token (parser->lexer);
22013 /* Now, look for more attributes. If the next token isn't a
22014 `,', we're done. */
22015 if (token->type != CPP_COMMA)
22016 break;
22018 /* Consume the comma and keep going. */
22019 cp_lexer_consume_token (parser->lexer);
22021 parser->translate_strings_p = save_translate_strings_p;
22023 /* We built up the list in reverse order. */
22024 return nreverse (attribute_list);
22027 /* Parse a standard C++11 attribute.
22029 The returned representation is a TREE_LIST which TREE_PURPOSE is
22030 the scoped name of the attribute, and the TREE_VALUE is its
22031 arguments list.
22033 Note that the scoped name of the attribute is itself a TREE_LIST
22034 which TREE_PURPOSE is the namespace of the attribute, and
22035 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22036 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22037 and which TREE_PURPOSE is directly the attribute name.
22039 Clients of the attribute code should use get_attribute_namespace
22040 and get_attribute_name to get the actual namespace and name of
22041 attributes, regardless of their being GNU or C++11 attributes.
22043 attribute:
22044 attribute-token attribute-argument-clause [opt]
22046 attribute-token:
22047 identifier
22048 attribute-scoped-token
22050 attribute-scoped-token:
22051 attribute-namespace :: identifier
22053 attribute-namespace:
22054 identifier
22056 attribute-argument-clause:
22057 ( balanced-token-seq )
22059 balanced-token-seq:
22060 balanced-token [opt]
22061 balanced-token-seq balanced-token
22063 balanced-token:
22064 ( balanced-token-seq )
22065 [ balanced-token-seq ]
22066 { balanced-token-seq }. */
22068 static tree
22069 cp_parser_std_attribute (cp_parser *parser)
22071 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22072 cp_token *token;
22074 /* First, parse name of the the attribute, a.k.a
22075 attribute-token. */
22077 token = cp_lexer_peek_token (parser->lexer);
22078 if (token->type == CPP_NAME)
22079 attr_id = token->u.value;
22080 else if (token->type == CPP_KEYWORD)
22081 attr_id = ridpointers[(int) token->keyword];
22082 else if (token->flags & NAMED_OP)
22083 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22085 if (attr_id == NULL_TREE)
22086 return NULL_TREE;
22088 cp_lexer_consume_token (parser->lexer);
22090 token = cp_lexer_peek_token (parser->lexer);
22091 if (token->type == CPP_SCOPE)
22093 /* We are seeing a scoped attribute token. */
22095 cp_lexer_consume_token (parser->lexer);
22096 attr_ns = attr_id;
22098 token = cp_lexer_consume_token (parser->lexer);
22099 if (token->type == CPP_NAME)
22100 attr_id = token->u.value;
22101 else if (token->type == CPP_KEYWORD)
22102 attr_id = ridpointers[(int) token->keyword];
22103 else
22105 error_at (token->location,
22106 "expected an identifier for the attribute name");
22107 return error_mark_node;
22109 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22110 NULL_TREE);
22111 token = cp_lexer_peek_token (parser->lexer);
22113 else
22115 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22116 NULL_TREE);
22117 /* C++11 noreturn attribute is equivalent to GNU's. */
22118 if (is_attribute_p ("noreturn", attr_id))
22119 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22120 /* C++14 deprecated attribute is equivalent to GNU's. */
22121 else if (cxx_dialect >= cxx14 && is_attribute_p ("deprecated", attr_id))
22122 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22125 /* Now parse the optional argument clause of the attribute. */
22127 if (token->type != CPP_OPEN_PAREN)
22128 return attribute;
22131 vec<tree, va_gc> *vec;
22132 int attr_flag = normal_attr;
22134 if (attr_ns == get_identifier ("gnu")
22135 && attribute_takes_identifier_p (attr_id))
22136 /* A GNU attribute that takes an identifier in parameter. */
22137 attr_flag = id_attr;
22139 vec = cp_parser_parenthesized_expression_list
22140 (parser, attr_flag, /*cast_p=*/false,
22141 /*allow_expansion_p=*/true,
22142 /*non_constant_p=*/NULL);
22143 if (vec == NULL)
22144 arguments = error_mark_node;
22145 else
22147 arguments = build_tree_list_vec (vec);
22148 release_tree_vector (vec);
22151 if (arguments == error_mark_node)
22152 attribute = error_mark_node;
22153 else
22154 TREE_VALUE (attribute) = arguments;
22157 return attribute;
22160 /* Parse a list of standard C++-11 attributes.
22162 attribute-list:
22163 attribute [opt]
22164 attribute-list , attribute[opt]
22165 attribute ...
22166 attribute-list , attribute ...
22169 static tree
22170 cp_parser_std_attribute_list (cp_parser *parser)
22172 tree attributes = NULL_TREE, attribute = NULL_TREE;
22173 cp_token *token = NULL;
22175 while (true)
22177 attribute = cp_parser_std_attribute (parser);
22178 if (attribute == error_mark_node)
22179 break;
22180 if (attribute != NULL_TREE)
22182 TREE_CHAIN (attribute) = attributes;
22183 attributes = attribute;
22185 token = cp_lexer_peek_token (parser->lexer);
22186 if (token->type != CPP_COMMA)
22187 break;
22188 cp_lexer_consume_token (parser->lexer);
22190 attributes = nreverse (attributes);
22191 return attributes;
22194 /* Parse a standard C++-11 attribute specifier.
22196 attribute-specifier:
22197 [ [ attribute-list ] ]
22198 alignment-specifier
22200 alignment-specifier:
22201 alignas ( type-id ... [opt] )
22202 alignas ( alignment-expression ... [opt] ). */
22204 static tree
22205 cp_parser_std_attribute_spec (cp_parser *parser)
22207 tree attributes = NULL_TREE;
22208 cp_token *token = cp_lexer_peek_token (parser->lexer);
22210 if (token->type == CPP_OPEN_SQUARE
22211 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22213 cp_lexer_consume_token (parser->lexer);
22214 cp_lexer_consume_token (parser->lexer);
22216 attributes = cp_parser_std_attribute_list (parser);
22218 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22219 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22220 cp_parser_skip_to_end_of_statement (parser);
22221 else
22222 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22223 when we are sure that we have actually parsed them. */
22224 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22226 else
22228 tree alignas_expr;
22230 /* Look for an alignment-specifier. */
22232 token = cp_lexer_peek_token (parser->lexer);
22234 if (token->type != CPP_KEYWORD
22235 || token->keyword != RID_ALIGNAS)
22236 return NULL_TREE;
22238 cp_lexer_consume_token (parser->lexer);
22239 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22241 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22243 cp_parser_error (parser, "expected %<(%>");
22244 return error_mark_node;
22247 cp_parser_parse_tentatively (parser);
22248 alignas_expr = cp_parser_type_id (parser);
22250 if (!cp_parser_parse_definitely (parser))
22252 gcc_assert (alignas_expr == error_mark_node
22253 || alignas_expr == NULL_TREE);
22255 alignas_expr =
22256 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22257 /**cp_id_kind=*/NULL);
22258 if (alignas_expr == error_mark_node)
22259 cp_parser_skip_to_end_of_statement (parser);
22260 if (alignas_expr == NULL_TREE
22261 || alignas_expr == error_mark_node)
22262 return alignas_expr;
22265 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22267 cp_parser_error (parser, "expected %<)%>");
22268 return error_mark_node;
22271 alignas_expr = cxx_alignas_expr (alignas_expr);
22273 /* Build the C++-11 representation of an 'aligned'
22274 attribute. */
22275 attributes =
22276 build_tree_list (build_tree_list (get_identifier ("gnu"),
22277 get_identifier ("aligned")),
22278 build_tree_list (NULL_TREE, alignas_expr));
22281 return attributes;
22284 /* Parse a standard C++-11 attribute-specifier-seq.
22286 attribute-specifier-seq:
22287 attribute-specifier-seq [opt] attribute-specifier
22290 static tree
22291 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22293 tree attr_specs = NULL;
22295 while (true)
22297 tree attr_spec = cp_parser_std_attribute_spec (parser);
22298 if (attr_spec == NULL_TREE)
22299 break;
22300 if (attr_spec == error_mark_node)
22301 return error_mark_node;
22303 TREE_CHAIN (attr_spec) = attr_specs;
22304 attr_specs = attr_spec;
22307 attr_specs = nreverse (attr_specs);
22308 return attr_specs;
22311 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22312 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22313 current value of the PEDANTIC flag, regardless of whether or not
22314 the `__extension__' keyword is present. The caller is responsible
22315 for restoring the value of the PEDANTIC flag. */
22317 static bool
22318 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22320 /* Save the old value of the PEDANTIC flag. */
22321 *saved_pedantic = pedantic;
22323 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22325 /* Consume the `__extension__' token. */
22326 cp_lexer_consume_token (parser->lexer);
22327 /* We're not being pedantic while the `__extension__' keyword is
22328 in effect. */
22329 pedantic = 0;
22331 return true;
22334 return false;
22337 /* Parse a label declaration.
22339 label-declaration:
22340 __label__ label-declarator-seq ;
22342 label-declarator-seq:
22343 identifier , label-declarator-seq
22344 identifier */
22346 static void
22347 cp_parser_label_declaration (cp_parser* parser)
22349 /* Look for the `__label__' keyword. */
22350 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22352 while (true)
22354 tree identifier;
22356 /* Look for an identifier. */
22357 identifier = cp_parser_identifier (parser);
22358 /* If we failed, stop. */
22359 if (identifier == error_mark_node)
22360 break;
22361 /* Declare it as a label. */
22362 finish_label_decl (identifier);
22363 /* If the next token is a `;', stop. */
22364 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22365 break;
22366 /* Look for the `,' separating the label declarations. */
22367 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22370 /* Look for the final `;'. */
22371 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22374 /* Support Functions */
22376 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22377 NAME should have one of the representations used for an
22378 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22379 is returned. If PARSER->SCOPE is a dependent type, then a
22380 SCOPE_REF is returned.
22382 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22383 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22384 was formed. Abstractly, such entities should not be passed to this
22385 function, because they do not need to be looked up, but it is
22386 simpler to check for this special case here, rather than at the
22387 call-sites.
22389 In cases not explicitly covered above, this function returns a
22390 DECL, OVERLOAD, or baselink representing the result of the lookup.
22391 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22392 is returned.
22394 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22395 (e.g., "struct") that was used. In that case bindings that do not
22396 refer to types are ignored.
22398 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22399 ignored.
22401 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22402 are ignored.
22404 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22405 types.
22407 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22408 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22409 NULL_TREE otherwise. */
22411 static tree
22412 cp_parser_lookup_name (cp_parser *parser, tree name,
22413 enum tag_types tag_type,
22414 bool is_template,
22415 bool is_namespace,
22416 bool check_dependency,
22417 tree *ambiguous_decls,
22418 location_t name_location)
22420 tree decl;
22421 tree object_type = parser->context->object_type;
22423 /* Assume that the lookup will be unambiguous. */
22424 if (ambiguous_decls)
22425 *ambiguous_decls = NULL_TREE;
22427 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22428 no longer valid. Note that if we are parsing tentatively, and
22429 the parse fails, OBJECT_TYPE will be automatically restored. */
22430 parser->context->object_type = NULL_TREE;
22432 if (name == error_mark_node)
22433 return error_mark_node;
22435 /* A template-id has already been resolved; there is no lookup to
22436 do. */
22437 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22438 return name;
22439 if (BASELINK_P (name))
22441 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22442 == TEMPLATE_ID_EXPR);
22443 return name;
22446 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22447 it should already have been checked to make sure that the name
22448 used matches the type being destroyed. */
22449 if (TREE_CODE (name) == BIT_NOT_EXPR)
22451 tree type;
22453 /* Figure out to which type this destructor applies. */
22454 if (parser->scope)
22455 type = parser->scope;
22456 else if (object_type)
22457 type = object_type;
22458 else
22459 type = current_class_type;
22460 /* If that's not a class type, there is no destructor. */
22461 if (!type || !CLASS_TYPE_P (type))
22462 return error_mark_node;
22463 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22464 lazily_declare_fn (sfk_destructor, type);
22465 if (!CLASSTYPE_DESTRUCTORS (type))
22466 return error_mark_node;
22467 /* If it was a class type, return the destructor. */
22468 return CLASSTYPE_DESTRUCTORS (type);
22471 /* By this point, the NAME should be an ordinary identifier. If
22472 the id-expression was a qualified name, the qualifying scope is
22473 stored in PARSER->SCOPE at this point. */
22474 gcc_assert (identifier_p (name));
22476 /* Perform the lookup. */
22477 if (parser->scope)
22479 bool dependent_p;
22481 if (parser->scope == error_mark_node)
22482 return error_mark_node;
22484 /* If the SCOPE is dependent, the lookup must be deferred until
22485 the template is instantiated -- unless we are explicitly
22486 looking up names in uninstantiated templates. Even then, we
22487 cannot look up the name if the scope is not a class type; it
22488 might, for example, be a template type parameter. */
22489 dependent_p = (TYPE_P (parser->scope)
22490 && dependent_scope_p (parser->scope));
22491 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22492 && dependent_p)
22493 /* Defer lookup. */
22494 decl = error_mark_node;
22495 else
22497 tree pushed_scope = NULL_TREE;
22499 /* If PARSER->SCOPE is a dependent type, then it must be a
22500 class type, and we must not be checking dependencies;
22501 otherwise, we would have processed this lookup above. So
22502 that PARSER->SCOPE is not considered a dependent base by
22503 lookup_member, we must enter the scope here. */
22504 if (dependent_p)
22505 pushed_scope = push_scope (parser->scope);
22507 /* If the PARSER->SCOPE is a template specialization, it
22508 may be instantiated during name lookup. In that case,
22509 errors may be issued. Even if we rollback the current
22510 tentative parse, those errors are valid. */
22511 decl = lookup_qualified_name (parser->scope, name,
22512 tag_type != none_type,
22513 /*complain=*/true);
22515 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22516 lookup result and the nested-name-specifier nominates a class C:
22517 * if the name specified after the nested-name-specifier, when
22518 looked up in C, is the injected-class-name of C (Clause 9), or
22519 * if the name specified after the nested-name-specifier is the
22520 same as the identifier or the simple-template-id's template-
22521 name in the last component of the nested-name-specifier,
22522 the name is instead considered to name the constructor of
22523 class C. [ Note: for example, the constructor is not an
22524 acceptable lookup result in an elaborated-type-specifier so
22525 the constructor would not be used in place of the
22526 injected-class-name. --end note ] Such a constructor name
22527 shall be used only in the declarator-id of a declaration that
22528 names a constructor or in a using-declaration. */
22529 if (tag_type == none_type
22530 && DECL_SELF_REFERENCE_P (decl)
22531 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22532 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22533 tag_type != none_type,
22534 /*complain=*/true);
22536 /* If we have a single function from a using decl, pull it out. */
22537 if (TREE_CODE (decl) == OVERLOAD
22538 && !really_overloaded_fn (decl))
22539 decl = OVL_FUNCTION (decl);
22541 if (pushed_scope)
22542 pop_scope (pushed_scope);
22545 /* If the scope is a dependent type and either we deferred lookup or
22546 we did lookup but didn't find the name, rememeber the name. */
22547 if (decl == error_mark_node && TYPE_P (parser->scope)
22548 && dependent_type_p (parser->scope))
22550 if (tag_type)
22552 tree type;
22554 /* The resolution to Core Issue 180 says that `struct
22555 A::B' should be considered a type-name, even if `A'
22556 is dependent. */
22557 type = make_typename_type (parser->scope, name, tag_type,
22558 /*complain=*/tf_error);
22559 if (type != error_mark_node)
22560 decl = TYPE_NAME (type);
22562 else if (is_template
22563 && (cp_parser_next_token_ends_template_argument_p (parser)
22564 || cp_lexer_next_token_is (parser->lexer,
22565 CPP_CLOSE_PAREN)))
22566 decl = make_unbound_class_template (parser->scope,
22567 name, NULL_TREE,
22568 /*complain=*/tf_error);
22569 else
22570 decl = build_qualified_name (/*type=*/NULL_TREE,
22571 parser->scope, name,
22572 is_template);
22574 parser->qualifying_scope = parser->scope;
22575 parser->object_scope = NULL_TREE;
22577 else if (object_type)
22579 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22580 OBJECT_TYPE is not a class. */
22581 if (CLASS_TYPE_P (object_type))
22582 /* If the OBJECT_TYPE is a template specialization, it may
22583 be instantiated during name lookup. In that case, errors
22584 may be issued. Even if we rollback the current tentative
22585 parse, those errors are valid. */
22586 decl = lookup_member (object_type,
22587 name,
22588 /*protect=*/0,
22589 tag_type != none_type,
22590 tf_warning_or_error);
22591 else
22592 decl = NULL_TREE;
22594 if (!decl)
22595 /* Look it up in the enclosing context. */
22596 decl = lookup_name_real (name, tag_type != none_type,
22597 /*nonclass=*/0,
22598 /*block_p=*/true, is_namespace, 0);
22599 parser->object_scope = object_type;
22600 parser->qualifying_scope = NULL_TREE;
22602 else
22604 decl = lookup_name_real (name, tag_type != none_type,
22605 /*nonclass=*/0,
22606 /*block_p=*/true, is_namespace, 0);
22607 parser->qualifying_scope = NULL_TREE;
22608 parser->object_scope = NULL_TREE;
22611 /* If the lookup failed, let our caller know. */
22612 if (!decl || decl == error_mark_node)
22613 return error_mark_node;
22615 /* Pull out the template from an injected-class-name (or multiple). */
22616 if (is_template)
22617 decl = maybe_get_template_decl_from_type_decl (decl);
22619 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22620 if (TREE_CODE (decl) == TREE_LIST)
22622 if (ambiguous_decls)
22623 *ambiguous_decls = decl;
22624 /* The error message we have to print is too complicated for
22625 cp_parser_error, so we incorporate its actions directly. */
22626 if (!cp_parser_simulate_error (parser))
22628 error_at (name_location, "reference to %qD is ambiguous",
22629 name);
22630 print_candidates (decl);
22632 return error_mark_node;
22635 gcc_assert (DECL_P (decl)
22636 || TREE_CODE (decl) == OVERLOAD
22637 || TREE_CODE (decl) == SCOPE_REF
22638 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22639 || BASELINK_P (decl));
22641 /* If we have resolved the name of a member declaration, check to
22642 see if the declaration is accessible. When the name resolves to
22643 set of overloaded functions, accessibility is checked when
22644 overload resolution is done.
22646 During an explicit instantiation, access is not checked at all,
22647 as per [temp.explicit]. */
22648 if (DECL_P (decl))
22649 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22651 maybe_record_typedef_use (decl);
22653 return decl;
22656 /* Like cp_parser_lookup_name, but for use in the typical case where
22657 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22658 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22660 static tree
22661 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22663 return cp_parser_lookup_name (parser, name,
22664 none_type,
22665 /*is_template=*/false,
22666 /*is_namespace=*/false,
22667 /*check_dependency=*/true,
22668 /*ambiguous_decls=*/NULL,
22669 location);
22672 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22673 the current context, return the TYPE_DECL. If TAG_NAME_P is
22674 true, the DECL indicates the class being defined in a class-head,
22675 or declared in an elaborated-type-specifier.
22677 Otherwise, return DECL. */
22679 static tree
22680 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22682 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22683 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22685 struct A {
22686 template <typename T> struct B;
22689 template <typename T> struct A::B {};
22691 Similarly, in an elaborated-type-specifier:
22693 namespace N { struct X{}; }
22695 struct A {
22696 template <typename T> friend struct N::X;
22699 However, if the DECL refers to a class type, and we are in
22700 the scope of the class, then the name lookup automatically
22701 finds the TYPE_DECL created by build_self_reference rather
22702 than a TEMPLATE_DECL. For example, in:
22704 template <class T> struct S {
22705 S s;
22708 there is no need to handle such case. */
22710 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22711 return DECL_TEMPLATE_RESULT (decl);
22713 return decl;
22716 /* If too many, or too few, template-parameter lists apply to the
22717 declarator, issue an error message. Returns TRUE if all went well,
22718 and FALSE otherwise. */
22720 static bool
22721 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22722 cp_declarator *declarator,
22723 location_t declarator_location)
22725 switch (declarator->kind)
22727 case cdk_id:
22729 unsigned num_templates = 0;
22730 tree scope = declarator->u.id.qualifying_scope;
22732 if (scope)
22733 num_templates = num_template_headers_for_class (scope);
22734 else if (TREE_CODE (declarator->u.id.unqualified_name)
22735 == TEMPLATE_ID_EXPR)
22736 /* If the DECLARATOR has the form `X<y>' then it uses one
22737 additional level of template parameters. */
22738 ++num_templates;
22740 return cp_parser_check_template_parameters
22741 (parser, num_templates, declarator_location, declarator);
22744 case cdk_function:
22745 case cdk_array:
22746 case cdk_pointer:
22747 case cdk_reference:
22748 case cdk_ptrmem:
22749 return (cp_parser_check_declarator_template_parameters
22750 (parser, declarator->declarator, declarator_location));
22752 case cdk_error:
22753 return true;
22755 default:
22756 gcc_unreachable ();
22758 return false;
22761 /* NUM_TEMPLATES were used in the current declaration. If that is
22762 invalid, return FALSE and issue an error messages. Otherwise,
22763 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22764 declarator and we can print more accurate diagnostics. */
22766 static bool
22767 cp_parser_check_template_parameters (cp_parser* parser,
22768 unsigned num_templates,
22769 location_t location,
22770 cp_declarator *declarator)
22772 /* If there are the same number of template classes and parameter
22773 lists, that's OK. */
22774 if (parser->num_template_parameter_lists == num_templates)
22775 return true;
22776 /* If there are more, but only one more, then we are referring to a
22777 member template. That's OK too. */
22778 if (parser->num_template_parameter_lists == num_templates + 1)
22779 return true;
22780 /* If there are more template classes than parameter lists, we have
22781 something like:
22783 template <class T> void S<T>::R<T>::f (); */
22784 if (parser->num_template_parameter_lists < num_templates)
22786 if (declarator && !current_function_decl)
22787 error_at (location, "specializing member %<%T::%E%> "
22788 "requires %<template<>%> syntax",
22789 declarator->u.id.qualifying_scope,
22790 declarator->u.id.unqualified_name);
22791 else if (declarator)
22792 error_at (location, "invalid declaration of %<%T::%E%>",
22793 declarator->u.id.qualifying_scope,
22794 declarator->u.id.unqualified_name);
22795 else
22796 error_at (location, "too few template-parameter-lists");
22797 return false;
22799 /* Otherwise, there are too many template parameter lists. We have
22800 something like:
22802 template <class T> template <class U> void S::f(); */
22803 error_at (location, "too many template-parameter-lists");
22804 return false;
22807 /* Parse an optional `::' token indicating that the following name is
22808 from the global namespace. If so, PARSER->SCOPE is set to the
22809 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22810 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22811 Returns the new value of PARSER->SCOPE, if the `::' token is
22812 present, and NULL_TREE otherwise. */
22814 static tree
22815 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22817 cp_token *token;
22819 /* Peek at the next token. */
22820 token = cp_lexer_peek_token (parser->lexer);
22821 /* If we're looking at a `::' token then we're starting from the
22822 global namespace, not our current location. */
22823 if (token->type == CPP_SCOPE)
22825 /* Consume the `::' token. */
22826 cp_lexer_consume_token (parser->lexer);
22827 /* Set the SCOPE so that we know where to start the lookup. */
22828 parser->scope = global_namespace;
22829 parser->qualifying_scope = global_namespace;
22830 parser->object_scope = NULL_TREE;
22832 return parser->scope;
22834 else if (!current_scope_valid_p)
22836 parser->scope = NULL_TREE;
22837 parser->qualifying_scope = NULL_TREE;
22838 parser->object_scope = NULL_TREE;
22841 return NULL_TREE;
22844 /* Returns TRUE if the upcoming token sequence is the start of a
22845 constructor declarator. If FRIEND_P is true, the declarator is
22846 preceded by the `friend' specifier. */
22848 static bool
22849 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22851 bool constructor_p;
22852 bool outside_class_specifier_p;
22853 tree nested_name_specifier;
22854 cp_token *next_token;
22856 /* The common case is that this is not a constructor declarator, so
22857 try to avoid doing lots of work if at all possible. It's not
22858 valid declare a constructor at function scope. */
22859 if (parser->in_function_body)
22860 return false;
22861 /* And only certain tokens can begin a constructor declarator. */
22862 next_token = cp_lexer_peek_token (parser->lexer);
22863 if (next_token->type != CPP_NAME
22864 && next_token->type != CPP_SCOPE
22865 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22866 && next_token->type != CPP_TEMPLATE_ID)
22867 return false;
22869 /* Parse tentatively; we are going to roll back all of the tokens
22870 consumed here. */
22871 cp_parser_parse_tentatively (parser);
22872 /* Assume that we are looking at a constructor declarator. */
22873 constructor_p = true;
22875 /* Look for the optional `::' operator. */
22876 cp_parser_global_scope_opt (parser,
22877 /*current_scope_valid_p=*/false);
22878 /* Look for the nested-name-specifier. */
22879 nested_name_specifier
22880 = (cp_parser_nested_name_specifier_opt (parser,
22881 /*typename_keyword_p=*/false,
22882 /*check_dependency_p=*/false,
22883 /*type_p=*/false,
22884 /*is_declaration=*/false));
22886 outside_class_specifier_p = (!at_class_scope_p ()
22887 || !TYPE_BEING_DEFINED (current_class_type)
22888 || friend_p);
22890 /* Outside of a class-specifier, there must be a
22891 nested-name-specifier. */
22892 if (!nested_name_specifier && outside_class_specifier_p)
22893 constructor_p = false;
22894 else if (nested_name_specifier == error_mark_node)
22895 constructor_p = false;
22897 /* If we have a class scope, this is easy; DR 147 says that S::S always
22898 names the constructor, and no other qualified name could. */
22899 if (constructor_p && nested_name_specifier
22900 && CLASS_TYPE_P (nested_name_specifier))
22902 tree id = cp_parser_unqualified_id (parser,
22903 /*template_keyword_p=*/false,
22904 /*check_dependency_p=*/false,
22905 /*declarator_p=*/true,
22906 /*optional_p=*/false);
22907 if (is_overloaded_fn (id))
22908 id = DECL_NAME (get_first_fn (id));
22909 if (!constructor_name_p (id, nested_name_specifier))
22910 constructor_p = false;
22912 /* If we still think that this might be a constructor-declarator,
22913 look for a class-name. */
22914 else if (constructor_p)
22916 /* If we have:
22918 template <typename T> struct S {
22919 S();
22922 we must recognize that the nested `S' names a class. */
22923 tree type_decl;
22924 type_decl = cp_parser_class_name (parser,
22925 /*typename_keyword_p=*/false,
22926 /*template_keyword_p=*/false,
22927 none_type,
22928 /*check_dependency_p=*/false,
22929 /*class_head_p=*/false,
22930 /*is_declaration=*/false);
22931 /* If there was no class-name, then this is not a constructor.
22932 Otherwise, if we are in a class-specifier and we aren't
22933 handling a friend declaration, check that its type matches
22934 current_class_type (c++/38313). Note: error_mark_node
22935 is left alone for error recovery purposes. */
22936 constructor_p = (!cp_parser_error_occurred (parser)
22937 && (outside_class_specifier_p
22938 || type_decl == error_mark_node
22939 || same_type_p (current_class_type,
22940 TREE_TYPE (type_decl))));
22942 /* If we're still considering a constructor, we have to see a `(',
22943 to begin the parameter-declaration-clause, followed by either a
22944 `)', an `...', or a decl-specifier. We need to check for a
22945 type-specifier to avoid being fooled into thinking that:
22947 S (f) (int);
22949 is a constructor. (It is actually a function named `f' that
22950 takes one parameter (of type `int') and returns a value of type
22951 `S'. */
22952 if (constructor_p
22953 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22954 constructor_p = false;
22956 if (constructor_p
22957 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22958 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22959 /* A parameter declaration begins with a decl-specifier,
22960 which is either the "attribute" keyword, a storage class
22961 specifier, or (usually) a type-specifier. */
22962 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22964 tree type;
22965 tree pushed_scope = NULL_TREE;
22966 unsigned saved_num_template_parameter_lists;
22968 /* Names appearing in the type-specifier should be looked up
22969 in the scope of the class. */
22970 if (current_class_type)
22971 type = NULL_TREE;
22972 else
22974 type = TREE_TYPE (type_decl);
22975 if (TREE_CODE (type) == TYPENAME_TYPE)
22977 type = resolve_typename_type (type,
22978 /*only_current_p=*/false);
22979 if (TREE_CODE (type) == TYPENAME_TYPE)
22981 cp_parser_abort_tentative_parse (parser);
22982 return false;
22985 pushed_scope = push_scope (type);
22988 /* Inside the constructor parameter list, surrounding
22989 template-parameter-lists do not apply. */
22990 saved_num_template_parameter_lists
22991 = parser->num_template_parameter_lists;
22992 parser->num_template_parameter_lists = 0;
22994 /* Look for the type-specifier. */
22995 cp_parser_type_specifier (parser,
22996 CP_PARSER_FLAGS_NONE,
22997 /*decl_specs=*/NULL,
22998 /*is_declarator=*/true,
22999 /*declares_class_or_enum=*/NULL,
23000 /*is_cv_qualifier=*/NULL);
23002 parser->num_template_parameter_lists
23003 = saved_num_template_parameter_lists;
23005 /* Leave the scope of the class. */
23006 if (pushed_scope)
23007 pop_scope (pushed_scope);
23009 constructor_p = !cp_parser_error_occurred (parser);
23013 /* We did not really want to consume any tokens. */
23014 cp_parser_abort_tentative_parse (parser);
23016 return constructor_p;
23019 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23020 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23021 they must be performed once we are in the scope of the function.
23023 Returns the function defined. */
23025 static tree
23026 cp_parser_function_definition_from_specifiers_and_declarator
23027 (cp_parser* parser,
23028 cp_decl_specifier_seq *decl_specifiers,
23029 tree attributes,
23030 const cp_declarator *declarator)
23032 tree fn;
23033 bool success_p;
23035 /* Begin the function-definition. */
23036 success_p = start_function (decl_specifiers, declarator, attributes);
23038 /* The things we're about to see are not directly qualified by any
23039 template headers we've seen thus far. */
23040 reset_specialization ();
23042 /* If there were names looked up in the decl-specifier-seq that we
23043 did not check, check them now. We must wait until we are in the
23044 scope of the function to perform the checks, since the function
23045 might be a friend. */
23046 perform_deferred_access_checks (tf_warning_or_error);
23048 if (success_p)
23050 cp_finalize_omp_declare_simd (parser, current_function_decl);
23051 parser->omp_declare_simd = NULL;
23054 if (!success_p)
23056 /* Skip the entire function. */
23057 cp_parser_skip_to_end_of_block_or_statement (parser);
23058 fn = error_mark_node;
23060 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23062 /* Seen already, skip it. An error message has already been output. */
23063 cp_parser_skip_to_end_of_block_or_statement (parser);
23064 fn = current_function_decl;
23065 current_function_decl = NULL_TREE;
23066 /* If this is a function from a class, pop the nested class. */
23067 if (current_class_name)
23068 pop_nested_class ();
23070 else
23072 timevar_id_t tv;
23073 if (DECL_DECLARED_INLINE_P (current_function_decl))
23074 tv = TV_PARSE_INLINE;
23075 else
23076 tv = TV_PARSE_FUNC;
23077 timevar_push (tv);
23078 fn = cp_parser_function_definition_after_declarator (parser,
23079 /*inline_p=*/false);
23080 timevar_pop (tv);
23083 return fn;
23086 /* Parse the part of a function-definition that follows the
23087 declarator. INLINE_P is TRUE iff this function is an inline
23088 function defined within a class-specifier.
23090 Returns the function defined. */
23092 static tree
23093 cp_parser_function_definition_after_declarator (cp_parser* parser,
23094 bool inline_p)
23096 tree fn;
23097 bool ctor_initializer_p = false;
23098 bool saved_in_unbraced_linkage_specification_p;
23099 bool saved_in_function_body;
23100 unsigned saved_num_template_parameter_lists;
23101 cp_token *token;
23102 bool fully_implicit_function_template_p
23103 = parser->fully_implicit_function_template_p;
23104 parser->fully_implicit_function_template_p = false;
23105 tree implicit_template_parms
23106 = parser->implicit_template_parms;
23107 parser->implicit_template_parms = 0;
23108 cp_binding_level* implicit_template_scope
23109 = parser->implicit_template_scope;
23110 parser->implicit_template_scope = 0;
23112 saved_in_function_body = parser->in_function_body;
23113 parser->in_function_body = true;
23114 /* If the next token is `return', then the code may be trying to
23115 make use of the "named return value" extension that G++ used to
23116 support. */
23117 token = cp_lexer_peek_token (parser->lexer);
23118 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23120 /* Consume the `return' keyword. */
23121 cp_lexer_consume_token (parser->lexer);
23122 /* Look for the identifier that indicates what value is to be
23123 returned. */
23124 cp_parser_identifier (parser);
23125 /* Issue an error message. */
23126 error_at (token->location,
23127 "named return values are no longer supported");
23128 /* Skip tokens until we reach the start of the function body. */
23129 while (true)
23131 cp_token *token = cp_lexer_peek_token (parser->lexer);
23132 if (token->type == CPP_OPEN_BRACE
23133 || token->type == CPP_EOF
23134 || token->type == CPP_PRAGMA_EOL)
23135 break;
23136 cp_lexer_consume_token (parser->lexer);
23139 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23140 anything declared inside `f'. */
23141 saved_in_unbraced_linkage_specification_p
23142 = parser->in_unbraced_linkage_specification_p;
23143 parser->in_unbraced_linkage_specification_p = false;
23144 /* Inside the function, surrounding template-parameter-lists do not
23145 apply. */
23146 saved_num_template_parameter_lists
23147 = parser->num_template_parameter_lists;
23148 parser->num_template_parameter_lists = 0;
23150 start_lambda_scope (current_function_decl);
23152 /* If the next token is `try', `__transaction_atomic', or
23153 `__transaction_relaxed`, then we are looking at either function-try-block
23154 or function-transaction-block. Note that all of these include the
23155 function-body. */
23156 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23157 ctor_initializer_p = cp_parser_function_transaction (parser,
23158 RID_TRANSACTION_ATOMIC);
23159 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23160 RID_TRANSACTION_RELAXED))
23161 ctor_initializer_p = cp_parser_function_transaction (parser,
23162 RID_TRANSACTION_RELAXED);
23163 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23164 ctor_initializer_p = cp_parser_function_try_block (parser);
23165 else
23166 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23167 (parser, /*in_function_try_block=*/false);
23169 finish_lambda_scope ();
23171 /* Finish the function. */
23172 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23173 (inline_p ? 2 : 0));
23174 /* Generate code for it, if necessary. */
23175 expand_or_defer_fn (fn);
23176 /* Restore the saved values. */
23177 parser->in_unbraced_linkage_specification_p
23178 = saved_in_unbraced_linkage_specification_p;
23179 parser->num_template_parameter_lists
23180 = saved_num_template_parameter_lists;
23181 parser->in_function_body = saved_in_function_body;
23183 parser->fully_implicit_function_template_p
23184 = fully_implicit_function_template_p;
23185 parser->implicit_template_parms
23186 = implicit_template_parms;
23187 parser->implicit_template_scope
23188 = implicit_template_scope;
23190 if (parser->fully_implicit_function_template_p)
23191 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23193 return fn;
23196 /* Parse a template-declaration, assuming that the `export' (and
23197 `extern') keywords, if present, has already been scanned. MEMBER_P
23198 is as for cp_parser_template_declaration. */
23200 static void
23201 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23203 tree decl = NULL_TREE;
23204 vec<deferred_access_check, va_gc> *checks;
23205 tree parameter_list;
23206 bool friend_p = false;
23207 bool need_lang_pop;
23208 cp_token *token;
23210 /* Look for the `template' keyword. */
23211 token = cp_lexer_peek_token (parser->lexer);
23212 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23213 return;
23215 /* And the `<'. */
23216 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23217 return;
23218 if (at_class_scope_p () && current_function_decl)
23220 /* 14.5.2.2 [temp.mem]
23222 A local class shall not have member templates. */
23223 error_at (token->location,
23224 "invalid declaration of member template in local class");
23225 cp_parser_skip_to_end_of_block_or_statement (parser);
23226 return;
23228 /* [temp]
23230 A template ... shall not have C linkage. */
23231 if (current_lang_name == lang_name_c)
23233 error_at (token->location, "template with C linkage");
23234 /* Give it C++ linkage to avoid confusing other parts of the
23235 front end. */
23236 push_lang_context (lang_name_cplusplus);
23237 need_lang_pop = true;
23239 else
23240 need_lang_pop = false;
23242 /* We cannot perform access checks on the template parameter
23243 declarations until we know what is being declared, just as we
23244 cannot check the decl-specifier list. */
23245 push_deferring_access_checks (dk_deferred);
23247 /* If the next token is `>', then we have an invalid
23248 specialization. Rather than complain about an invalid template
23249 parameter, issue an error message here. */
23250 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23252 cp_parser_error (parser, "invalid explicit specialization");
23253 begin_specialization ();
23254 parameter_list = NULL_TREE;
23256 else
23258 /* Parse the template parameters. */
23259 parameter_list = cp_parser_template_parameter_list (parser);
23262 /* Get the deferred access checks from the parameter list. These
23263 will be checked once we know what is being declared, as for a
23264 member template the checks must be performed in the scope of the
23265 class containing the member. */
23266 checks = get_deferred_access_checks ();
23268 /* Look for the `>'. */
23269 cp_parser_skip_to_end_of_template_parameter_list (parser);
23270 /* We just processed one more parameter list. */
23271 ++parser->num_template_parameter_lists;
23272 /* If the next token is `template', there are more template
23273 parameters. */
23274 if (cp_lexer_next_token_is_keyword (parser->lexer,
23275 RID_TEMPLATE))
23276 cp_parser_template_declaration_after_export (parser, member_p);
23277 else if (cxx_dialect >= cxx11
23278 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23279 decl = cp_parser_alias_declaration (parser);
23280 else
23282 /* There are no access checks when parsing a template, as we do not
23283 know if a specialization will be a friend. */
23284 push_deferring_access_checks (dk_no_check);
23285 token = cp_lexer_peek_token (parser->lexer);
23286 decl = cp_parser_single_declaration (parser,
23287 checks,
23288 member_p,
23289 /*explicit_specialization_p=*/false,
23290 &friend_p);
23291 pop_deferring_access_checks ();
23293 /* If this is a member template declaration, let the front
23294 end know. */
23295 if (member_p && !friend_p && decl)
23297 if (TREE_CODE (decl) == TYPE_DECL)
23298 cp_parser_check_access_in_redeclaration (decl, token->location);
23300 decl = finish_member_template_decl (decl);
23302 else if (friend_p && decl
23303 && DECL_DECLARES_TYPE_P (decl))
23304 make_friend_class (current_class_type, TREE_TYPE (decl),
23305 /*complain=*/true);
23307 /* We are done with the current parameter list. */
23308 --parser->num_template_parameter_lists;
23310 pop_deferring_access_checks ();
23312 /* Finish up. */
23313 finish_template_decl (parameter_list);
23315 /* Check the template arguments for a literal operator template. */
23316 if (decl
23317 && DECL_DECLARES_FUNCTION_P (decl)
23318 && UDLIT_OPER_P (DECL_NAME (decl)))
23320 bool ok = true;
23321 if (parameter_list == NULL_TREE)
23322 ok = false;
23323 else
23325 int num_parms = TREE_VEC_LENGTH (parameter_list);
23326 if (num_parms == 1)
23328 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23329 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23330 if (TREE_TYPE (parm) != char_type_node
23331 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23332 ok = false;
23334 else if (num_parms == 2 && cxx_dialect >= cxx14)
23336 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23337 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23338 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23339 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23340 if (TREE_TYPE (parm) != TREE_TYPE (type)
23341 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23342 ok = false;
23344 else
23345 ok = false;
23347 if (!ok)
23349 if (cxx_dialect >= cxx14)
23350 error ("literal operator template %qD has invalid parameter list."
23351 " Expected non-type template argument pack <char...>"
23352 " or <typename CharT, CharT...>",
23353 decl);
23354 else
23355 error ("literal operator template %qD has invalid parameter list."
23356 " Expected non-type template argument pack <char...>",
23357 decl);
23360 /* Register member declarations. */
23361 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23362 finish_member_declaration (decl);
23363 /* For the erroneous case of a template with C linkage, we pushed an
23364 implicit C++ linkage scope; exit that scope now. */
23365 if (need_lang_pop)
23366 pop_lang_context ();
23367 /* If DECL is a function template, we must return to parse it later.
23368 (Even though there is no definition, there might be default
23369 arguments that need handling.) */
23370 if (member_p && decl
23371 && DECL_DECLARES_FUNCTION_P (decl))
23372 vec_safe_push (unparsed_funs_with_definitions, decl);
23375 /* Perform the deferred access checks from a template-parameter-list.
23376 CHECKS is a TREE_LIST of access checks, as returned by
23377 get_deferred_access_checks. */
23379 static void
23380 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23382 ++processing_template_parmlist;
23383 perform_access_checks (checks, tf_warning_or_error);
23384 --processing_template_parmlist;
23387 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23388 `function-definition' sequence that follows a template header.
23389 If MEMBER_P is true, this declaration appears in a class scope.
23391 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23392 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23394 static tree
23395 cp_parser_single_declaration (cp_parser* parser,
23396 vec<deferred_access_check, va_gc> *checks,
23397 bool member_p,
23398 bool explicit_specialization_p,
23399 bool* friend_p)
23401 int declares_class_or_enum;
23402 tree decl = NULL_TREE;
23403 cp_decl_specifier_seq decl_specifiers;
23404 bool function_definition_p = false;
23405 cp_token *decl_spec_token_start;
23407 /* This function is only used when processing a template
23408 declaration. */
23409 gcc_assert (innermost_scope_kind () == sk_template_parms
23410 || innermost_scope_kind () == sk_template_spec);
23412 /* Defer access checks until we know what is being declared. */
23413 push_deferring_access_checks (dk_deferred);
23415 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23416 alternative. */
23417 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23418 cp_parser_decl_specifier_seq (parser,
23419 CP_PARSER_FLAGS_OPTIONAL,
23420 &decl_specifiers,
23421 &declares_class_or_enum);
23422 if (friend_p)
23423 *friend_p = cp_parser_friend_p (&decl_specifiers);
23425 /* There are no template typedefs. */
23426 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23428 error_at (decl_spec_token_start->location,
23429 "template declaration of %<typedef%>");
23430 decl = error_mark_node;
23433 /* Gather up the access checks that occurred the
23434 decl-specifier-seq. */
23435 stop_deferring_access_checks ();
23437 /* Check for the declaration of a template class. */
23438 if (declares_class_or_enum)
23440 if (cp_parser_declares_only_class_p (parser))
23442 decl = shadow_tag (&decl_specifiers);
23444 /* In this case:
23446 struct C {
23447 friend template <typename T> struct A<T>::B;
23450 A<T>::B will be represented by a TYPENAME_TYPE, and
23451 therefore not recognized by shadow_tag. */
23452 if (friend_p && *friend_p
23453 && !decl
23454 && decl_specifiers.type
23455 && TYPE_P (decl_specifiers.type))
23456 decl = decl_specifiers.type;
23458 if (decl && decl != error_mark_node)
23459 decl = TYPE_NAME (decl);
23460 else
23461 decl = error_mark_node;
23463 /* Perform access checks for template parameters. */
23464 cp_parser_perform_template_parameter_access_checks (checks);
23468 /* Complain about missing 'typename' or other invalid type names. */
23469 if (!decl_specifiers.any_type_specifiers_p
23470 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23472 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23473 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23474 the rest of this declaration. */
23475 decl = error_mark_node;
23476 goto out;
23479 /* If it's not a template class, try for a template function. If
23480 the next token is a `;', then this declaration does not declare
23481 anything. But, if there were errors in the decl-specifiers, then
23482 the error might well have come from an attempted class-specifier.
23483 In that case, there's no need to warn about a missing declarator. */
23484 if (!decl
23485 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23486 || decl_specifiers.type != error_mark_node))
23488 decl = cp_parser_init_declarator (parser,
23489 &decl_specifiers,
23490 checks,
23491 /*function_definition_allowed_p=*/true,
23492 member_p,
23493 declares_class_or_enum,
23494 &function_definition_p,
23495 NULL);
23497 /* 7.1.1-1 [dcl.stc]
23499 A storage-class-specifier shall not be specified in an explicit
23500 specialization... */
23501 if (decl
23502 && explicit_specialization_p
23503 && decl_specifiers.storage_class != sc_none)
23505 error_at (decl_spec_token_start->location,
23506 "explicit template specialization cannot have a storage class");
23507 decl = error_mark_node;
23510 if (decl && VAR_P (decl))
23511 check_template_variable (decl);
23514 /* Look for a trailing `;' after the declaration. */
23515 if (!function_definition_p
23516 && (decl == error_mark_node
23517 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23518 cp_parser_skip_to_end_of_block_or_statement (parser);
23520 out:
23521 pop_deferring_access_checks ();
23523 /* Clear any current qualification; whatever comes next is the start
23524 of something new. */
23525 parser->scope = NULL_TREE;
23526 parser->qualifying_scope = NULL_TREE;
23527 parser->object_scope = NULL_TREE;
23529 return decl;
23532 /* Parse a cast-expression that is not the operand of a unary "&". */
23534 static tree
23535 cp_parser_simple_cast_expression (cp_parser *parser)
23537 return cp_parser_cast_expression (parser, /*address_p=*/false,
23538 /*cast_p=*/false, /*decltype*/false, NULL);
23541 /* Parse a functional cast to TYPE. Returns an expression
23542 representing the cast. */
23544 static tree
23545 cp_parser_functional_cast (cp_parser* parser, tree type)
23547 vec<tree, va_gc> *vec;
23548 tree expression_list;
23549 tree cast;
23550 bool nonconst_p;
23552 if (!type)
23553 type = error_mark_node;
23555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23557 cp_lexer_set_source_position (parser->lexer);
23558 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23559 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23560 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23561 if (TREE_CODE (type) == TYPE_DECL)
23562 type = TREE_TYPE (type);
23563 return finish_compound_literal (type, expression_list,
23564 tf_warning_or_error);
23568 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23569 /*cast_p=*/true,
23570 /*allow_expansion_p=*/true,
23571 /*non_constant_p=*/NULL);
23572 if (vec == NULL)
23573 expression_list = error_mark_node;
23574 else
23576 expression_list = build_tree_list_vec (vec);
23577 release_tree_vector (vec);
23580 cast = build_functional_cast (type, expression_list,
23581 tf_warning_or_error);
23582 /* [expr.const]/1: In an integral constant expression "only type
23583 conversions to integral or enumeration type can be used". */
23584 if (TREE_CODE (type) == TYPE_DECL)
23585 type = TREE_TYPE (type);
23586 if (cast != error_mark_node
23587 && !cast_valid_in_integral_constant_expression_p (type)
23588 && cp_parser_non_integral_constant_expression (parser,
23589 NIC_CONSTRUCTOR))
23590 return error_mark_node;
23591 return cast;
23594 /* Save the tokens that make up the body of a member function defined
23595 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23596 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23597 specifiers applied to the declaration. Returns the FUNCTION_DECL
23598 for the member function. */
23600 static tree
23601 cp_parser_save_member_function_body (cp_parser* parser,
23602 cp_decl_specifier_seq *decl_specifiers,
23603 cp_declarator *declarator,
23604 tree attributes)
23606 cp_token *first;
23607 cp_token *last;
23608 tree fn;
23610 /* Create the FUNCTION_DECL. */
23611 fn = grokmethod (decl_specifiers, declarator, attributes);
23612 cp_finalize_omp_declare_simd (parser, fn);
23613 /* If something went badly wrong, bail out now. */
23614 if (fn == error_mark_node)
23616 /* If there's a function-body, skip it. */
23617 if (cp_parser_token_starts_function_definition_p
23618 (cp_lexer_peek_token (parser->lexer)))
23619 cp_parser_skip_to_end_of_block_or_statement (parser);
23620 return error_mark_node;
23623 /* Remember it, if there default args to post process. */
23624 cp_parser_save_default_args (parser, fn);
23626 /* Save away the tokens that make up the body of the
23627 function. */
23628 first = parser->lexer->next_token;
23629 /* Handle function try blocks. */
23630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23631 cp_lexer_consume_token (parser->lexer);
23632 /* We can have braced-init-list mem-initializers before the fn body. */
23633 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23635 cp_lexer_consume_token (parser->lexer);
23636 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23638 /* cache_group will stop after an un-nested { } pair, too. */
23639 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23640 break;
23642 /* variadic mem-inits have ... after the ')'. */
23643 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23644 cp_lexer_consume_token (parser->lexer);
23647 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23648 /* Handle function try blocks. */
23649 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23650 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23651 last = parser->lexer->next_token;
23653 /* Save away the inline definition; we will process it when the
23654 class is complete. */
23655 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23656 DECL_PENDING_INLINE_P (fn) = 1;
23658 /* We need to know that this was defined in the class, so that
23659 friend templates are handled correctly. */
23660 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23662 /* Add FN to the queue of functions to be parsed later. */
23663 vec_safe_push (unparsed_funs_with_definitions, fn);
23665 return fn;
23668 /* Save the tokens that make up the in-class initializer for a non-static
23669 data member. Returns a DEFAULT_ARG. */
23671 static tree
23672 cp_parser_save_nsdmi (cp_parser* parser)
23674 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23677 /* Parse a template-argument-list, as well as the trailing ">" (but
23678 not the opening "<"). See cp_parser_template_argument_list for the
23679 return value. */
23681 static tree
23682 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23684 tree arguments;
23685 tree saved_scope;
23686 tree saved_qualifying_scope;
23687 tree saved_object_scope;
23688 bool saved_greater_than_is_operator_p;
23689 int saved_unevaluated_operand;
23690 int saved_inhibit_evaluation_warnings;
23692 /* [temp.names]
23694 When parsing a template-id, the first non-nested `>' is taken as
23695 the end of the template-argument-list rather than a greater-than
23696 operator. */
23697 saved_greater_than_is_operator_p
23698 = parser->greater_than_is_operator_p;
23699 parser->greater_than_is_operator_p = false;
23700 /* Parsing the argument list may modify SCOPE, so we save it
23701 here. */
23702 saved_scope = parser->scope;
23703 saved_qualifying_scope = parser->qualifying_scope;
23704 saved_object_scope = parser->object_scope;
23705 /* We need to evaluate the template arguments, even though this
23706 template-id may be nested within a "sizeof". */
23707 saved_unevaluated_operand = cp_unevaluated_operand;
23708 cp_unevaluated_operand = 0;
23709 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23710 c_inhibit_evaluation_warnings = 0;
23711 /* Parse the template-argument-list itself. */
23712 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23713 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23714 arguments = NULL_TREE;
23715 else
23716 arguments = cp_parser_template_argument_list (parser);
23717 /* Look for the `>' that ends the template-argument-list. If we find
23718 a '>>' instead, it's probably just a typo. */
23719 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23721 if (cxx_dialect != cxx98)
23723 /* In C++0x, a `>>' in a template argument list or cast
23724 expression is considered to be two separate `>'
23725 tokens. So, change the current token to a `>', but don't
23726 consume it: it will be consumed later when the outer
23727 template argument list (or cast expression) is parsed.
23728 Note that this replacement of `>' for `>>' is necessary
23729 even if we are parsing tentatively: in the tentative
23730 case, after calling
23731 cp_parser_enclosed_template_argument_list we will always
23732 throw away all of the template arguments and the first
23733 closing `>', either because the template argument list
23734 was erroneous or because we are replacing those tokens
23735 with a CPP_TEMPLATE_ID token. The second `>' (which will
23736 not have been thrown away) is needed either to close an
23737 outer template argument list or to complete a new-style
23738 cast. */
23739 cp_token *token = cp_lexer_peek_token (parser->lexer);
23740 token->type = CPP_GREATER;
23742 else if (!saved_greater_than_is_operator_p)
23744 /* If we're in a nested template argument list, the '>>' has
23745 to be a typo for '> >'. We emit the error message, but we
23746 continue parsing and we push a '>' as next token, so that
23747 the argument list will be parsed correctly. Note that the
23748 global source location is still on the token before the
23749 '>>', so we need to say explicitly where we want it. */
23750 cp_token *token = cp_lexer_peek_token (parser->lexer);
23751 error_at (token->location, "%<>>%> should be %<> >%> "
23752 "within a nested template argument list");
23754 token->type = CPP_GREATER;
23756 else
23758 /* If this is not a nested template argument list, the '>>'
23759 is a typo for '>'. Emit an error message and continue.
23760 Same deal about the token location, but here we can get it
23761 right by consuming the '>>' before issuing the diagnostic. */
23762 cp_token *token = cp_lexer_consume_token (parser->lexer);
23763 error_at (token->location,
23764 "spurious %<>>%>, use %<>%> to terminate "
23765 "a template argument list");
23768 else
23769 cp_parser_skip_to_end_of_template_parameter_list (parser);
23770 /* The `>' token might be a greater-than operator again now. */
23771 parser->greater_than_is_operator_p
23772 = saved_greater_than_is_operator_p;
23773 /* Restore the SAVED_SCOPE. */
23774 parser->scope = saved_scope;
23775 parser->qualifying_scope = saved_qualifying_scope;
23776 parser->object_scope = saved_object_scope;
23777 cp_unevaluated_operand = saved_unevaluated_operand;
23778 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23780 return arguments;
23783 /* MEMBER_FUNCTION is a member function, or a friend. If default
23784 arguments, or the body of the function have not yet been parsed,
23785 parse them now. */
23787 static void
23788 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23790 timevar_push (TV_PARSE_INMETH);
23791 /* If this member is a template, get the underlying
23792 FUNCTION_DECL. */
23793 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23794 member_function = DECL_TEMPLATE_RESULT (member_function);
23796 /* There should not be any class definitions in progress at this
23797 point; the bodies of members are only parsed outside of all class
23798 definitions. */
23799 gcc_assert (parser->num_classes_being_defined == 0);
23800 /* While we're parsing the member functions we might encounter more
23801 classes. We want to handle them right away, but we don't want
23802 them getting mixed up with functions that are currently in the
23803 queue. */
23804 push_unparsed_function_queues (parser);
23806 /* Make sure that any template parameters are in scope. */
23807 maybe_begin_member_template_processing (member_function);
23809 /* If the body of the function has not yet been parsed, parse it
23810 now. */
23811 if (DECL_PENDING_INLINE_P (member_function))
23813 tree function_scope;
23814 cp_token_cache *tokens;
23816 /* The function is no longer pending; we are processing it. */
23817 tokens = DECL_PENDING_INLINE_INFO (member_function);
23818 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23819 DECL_PENDING_INLINE_P (member_function) = 0;
23821 /* If this is a local class, enter the scope of the containing
23822 function. */
23823 function_scope = current_function_decl;
23824 if (function_scope)
23825 push_function_context ();
23827 /* Push the body of the function onto the lexer stack. */
23828 cp_parser_push_lexer_for_tokens (parser, tokens);
23830 /* Let the front end know that we going to be defining this
23831 function. */
23832 start_preparsed_function (member_function, NULL_TREE,
23833 SF_PRE_PARSED | SF_INCLASS_INLINE);
23835 /* Don't do access checking if it is a templated function. */
23836 if (processing_template_decl)
23837 push_deferring_access_checks (dk_no_check);
23839 /* #pragma omp declare reduction needs special parsing. */
23840 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23842 parser->lexer->in_pragma = true;
23843 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23844 finish_function (/*inline*/2);
23845 cp_check_omp_declare_reduction (member_function);
23847 else
23848 /* Now, parse the body of the function. */
23849 cp_parser_function_definition_after_declarator (parser,
23850 /*inline_p=*/true);
23852 if (processing_template_decl)
23853 pop_deferring_access_checks ();
23855 /* Leave the scope of the containing function. */
23856 if (function_scope)
23857 pop_function_context ();
23858 cp_parser_pop_lexer (parser);
23861 /* Remove any template parameters from the symbol table. */
23862 maybe_end_member_template_processing ();
23864 /* Restore the queue. */
23865 pop_unparsed_function_queues (parser);
23866 timevar_pop (TV_PARSE_INMETH);
23869 /* If DECL contains any default args, remember it on the unparsed
23870 functions queue. */
23872 static void
23873 cp_parser_save_default_args (cp_parser* parser, tree decl)
23875 tree probe;
23877 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23878 probe;
23879 probe = TREE_CHAIN (probe))
23880 if (TREE_PURPOSE (probe))
23882 cp_default_arg_entry entry = {current_class_type, decl};
23883 vec_safe_push (unparsed_funs_with_default_args, entry);
23884 break;
23888 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23889 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23890 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23891 from the parameter-type-list. */
23893 static tree
23894 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23895 tree default_arg, tree parmtype)
23897 cp_token_cache *tokens;
23898 tree parsed_arg;
23899 bool dummy;
23901 if (default_arg == error_mark_node)
23902 return error_mark_node;
23904 /* Push the saved tokens for the default argument onto the parser's
23905 lexer stack. */
23906 tokens = DEFARG_TOKENS (default_arg);
23907 cp_parser_push_lexer_for_tokens (parser, tokens);
23909 start_lambda_scope (decl);
23911 /* Parse the default argument. */
23912 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23913 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23914 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23916 finish_lambda_scope ();
23918 if (parsed_arg == error_mark_node)
23919 cp_parser_skip_to_end_of_statement (parser);
23921 if (!processing_template_decl)
23923 /* In a non-template class, check conversions now. In a template,
23924 we'll wait and instantiate these as needed. */
23925 if (TREE_CODE (decl) == PARM_DECL)
23926 parsed_arg = check_default_argument (parmtype, parsed_arg,
23927 tf_warning_or_error);
23928 else
23929 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
23932 /* If the token stream has not been completely used up, then
23933 there was extra junk after the end of the default
23934 argument. */
23935 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23937 if (TREE_CODE (decl) == PARM_DECL)
23938 cp_parser_error (parser, "expected %<,%>");
23939 else
23940 cp_parser_error (parser, "expected %<;%>");
23943 /* Revert to the main lexer. */
23944 cp_parser_pop_lexer (parser);
23946 return parsed_arg;
23949 /* FIELD is a non-static data member with an initializer which we saved for
23950 later; parse it now. */
23952 static void
23953 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23955 tree def;
23957 maybe_begin_member_template_processing (field);
23959 push_unparsed_function_queues (parser);
23960 def = cp_parser_late_parse_one_default_arg (parser, field,
23961 DECL_INITIAL (field),
23962 NULL_TREE);
23963 pop_unparsed_function_queues (parser);
23965 maybe_end_member_template_processing ();
23967 DECL_INITIAL (field) = def;
23970 /* FN is a FUNCTION_DECL which may contains a parameter with an
23971 unparsed DEFAULT_ARG. Parse the default args now. This function
23972 assumes that the current scope is the scope in which the default
23973 argument should be processed. */
23975 static void
23976 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23978 bool saved_local_variables_forbidden_p;
23979 tree parm, parmdecl;
23981 /* While we're parsing the default args, we might (due to the
23982 statement expression extension) encounter more classes. We want
23983 to handle them right away, but we don't want them getting mixed
23984 up with default args that are currently in the queue. */
23985 push_unparsed_function_queues (parser);
23987 /* Local variable names (and the `this' keyword) may not appear
23988 in a default argument. */
23989 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23990 parser->local_variables_forbidden_p = true;
23992 push_defarg_context (fn);
23994 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23995 parmdecl = DECL_ARGUMENTS (fn);
23996 parm && parm != void_list_node;
23997 parm = TREE_CHAIN (parm),
23998 parmdecl = DECL_CHAIN (parmdecl))
24000 tree default_arg = TREE_PURPOSE (parm);
24001 tree parsed_arg;
24002 vec<tree, va_gc> *insts;
24003 tree copy;
24004 unsigned ix;
24006 if (!default_arg)
24007 continue;
24009 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24010 /* This can happen for a friend declaration for a function
24011 already declared with default arguments. */
24012 continue;
24014 parsed_arg
24015 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24016 default_arg,
24017 TREE_VALUE (parm));
24018 if (parsed_arg == error_mark_node)
24020 continue;
24023 TREE_PURPOSE (parm) = parsed_arg;
24025 /* Update any instantiations we've already created. */
24026 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24027 vec_safe_iterate (insts, ix, &copy); ix++)
24028 TREE_PURPOSE (copy) = parsed_arg;
24031 pop_defarg_context ();
24033 /* Make sure no default arg is missing. */
24034 check_default_args (fn);
24036 /* Restore the state of local_variables_forbidden_p. */
24037 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24039 /* Restore the queue. */
24040 pop_unparsed_function_queues (parser);
24043 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24045 sizeof ... ( identifier )
24047 where the 'sizeof' token has already been consumed. */
24049 static tree
24050 cp_parser_sizeof_pack (cp_parser *parser)
24052 /* Consume the `...'. */
24053 cp_lexer_consume_token (parser->lexer);
24054 maybe_warn_variadic_templates ();
24056 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24057 if (paren)
24058 cp_lexer_consume_token (parser->lexer);
24059 else
24060 permerror (cp_lexer_peek_token (parser->lexer)->location,
24061 "%<sizeof...%> argument must be surrounded by parentheses");
24063 cp_token *token = cp_lexer_peek_token (parser->lexer);
24064 tree name = cp_parser_identifier (parser);
24065 if (name == error_mark_node)
24066 return error_mark_node;
24067 /* The name is not qualified. */
24068 parser->scope = NULL_TREE;
24069 parser->qualifying_scope = NULL_TREE;
24070 parser->object_scope = NULL_TREE;
24071 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24072 if (expr == error_mark_node)
24073 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24074 token->location);
24075 if (TREE_CODE (expr) == TYPE_DECL)
24076 expr = TREE_TYPE (expr);
24077 else if (TREE_CODE (expr) == CONST_DECL)
24078 expr = DECL_INITIAL (expr);
24079 expr = make_pack_expansion (expr);
24081 if (paren)
24082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24084 return expr;
24087 /* Parse the operand of `sizeof' (or a similar operator). Returns
24088 either a TYPE or an expression, depending on the form of the
24089 input. The KEYWORD indicates which kind of expression we have
24090 encountered. */
24092 static tree
24093 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24095 tree expr = NULL_TREE;
24096 const char *saved_message;
24097 char *tmp;
24098 bool saved_integral_constant_expression_p;
24099 bool saved_non_integral_constant_expression_p;
24101 /* If it's a `...', then we are computing the length of a parameter
24102 pack. */
24103 if (keyword == RID_SIZEOF
24104 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24105 return cp_parser_sizeof_pack (parser);
24107 /* Types cannot be defined in a `sizeof' expression. Save away the
24108 old message. */
24109 saved_message = parser->type_definition_forbidden_message;
24110 /* And create the new one. */
24111 tmp = concat ("types may not be defined in %<",
24112 IDENTIFIER_POINTER (ridpointers[keyword]),
24113 "%> expressions", NULL);
24114 parser->type_definition_forbidden_message = tmp;
24116 /* The restrictions on constant-expressions do not apply inside
24117 sizeof expressions. */
24118 saved_integral_constant_expression_p
24119 = parser->integral_constant_expression_p;
24120 saved_non_integral_constant_expression_p
24121 = parser->non_integral_constant_expression_p;
24122 parser->integral_constant_expression_p = false;
24124 /* Do not actually evaluate the expression. */
24125 ++cp_unevaluated_operand;
24126 ++c_inhibit_evaluation_warnings;
24127 /* If it's a `(', then we might be looking at the type-id
24128 construction. */
24129 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24131 tree type = NULL_TREE;
24133 /* We can't be sure yet whether we're looking at a type-id or an
24134 expression. */
24135 cp_parser_parse_tentatively (parser);
24136 /* Note: as a GNU Extension, compound literals are considered
24137 postfix-expressions as they are in C99, so they are valid
24138 arguments to sizeof. See comment in cp_parser_cast_expression
24139 for details. */
24140 if (cp_parser_compound_literal_p (parser))
24141 cp_parser_simulate_error (parser);
24142 else
24144 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24145 parser->in_type_id_in_expr_p = true;
24146 /* Look for the type-id. */
24147 type = cp_parser_type_id (parser);
24148 /* Look for the closing `)'. */
24149 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24150 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24153 /* If all went well, then we're done. */
24154 if (cp_parser_parse_definitely (parser))
24156 cp_decl_specifier_seq decl_specs;
24158 /* Build a trivial decl-specifier-seq. */
24159 clear_decl_specs (&decl_specs);
24160 decl_specs.type = type;
24162 /* Call grokdeclarator to figure out what type this is. */
24163 expr = grokdeclarator (NULL,
24164 &decl_specs,
24165 TYPENAME,
24166 /*initialized=*/0,
24167 /*attrlist=*/NULL);
24171 /* If the type-id production did not work out, then we must be
24172 looking at the unary-expression production. */
24173 if (!expr)
24174 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24175 /*cast_p=*/false, NULL);
24177 /* Go back to evaluating expressions. */
24178 --cp_unevaluated_operand;
24179 --c_inhibit_evaluation_warnings;
24181 /* Free the message we created. */
24182 free (tmp);
24183 /* And restore the old one. */
24184 parser->type_definition_forbidden_message = saved_message;
24185 parser->integral_constant_expression_p
24186 = saved_integral_constant_expression_p;
24187 parser->non_integral_constant_expression_p
24188 = saved_non_integral_constant_expression_p;
24190 return expr;
24193 /* If the current declaration has no declarator, return true. */
24195 static bool
24196 cp_parser_declares_only_class_p (cp_parser *parser)
24198 /* If the next token is a `;' or a `,' then there is no
24199 declarator. */
24200 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24201 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24204 /* Update the DECL_SPECS to reflect the storage class indicated by
24205 KEYWORD. */
24207 static void
24208 cp_parser_set_storage_class (cp_parser *parser,
24209 cp_decl_specifier_seq *decl_specs,
24210 enum rid keyword,
24211 cp_token *token)
24213 cp_storage_class storage_class;
24215 if (parser->in_unbraced_linkage_specification_p)
24217 error_at (token->location, "invalid use of %qD in linkage specification",
24218 ridpointers[keyword]);
24219 return;
24221 else if (decl_specs->storage_class != sc_none)
24223 decl_specs->conflicting_specifiers_p = true;
24224 return;
24227 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24228 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24229 && decl_specs->gnu_thread_keyword_p)
24231 pedwarn (decl_specs->locations[ds_thread], 0,
24232 "%<__thread%> before %qD", ridpointers[keyword]);
24235 switch (keyword)
24237 case RID_AUTO:
24238 storage_class = sc_auto;
24239 break;
24240 case RID_REGISTER:
24241 storage_class = sc_register;
24242 break;
24243 case RID_STATIC:
24244 storage_class = sc_static;
24245 break;
24246 case RID_EXTERN:
24247 storage_class = sc_extern;
24248 break;
24249 case RID_MUTABLE:
24250 storage_class = sc_mutable;
24251 break;
24252 default:
24253 gcc_unreachable ();
24255 decl_specs->storage_class = storage_class;
24256 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24258 /* A storage class specifier cannot be applied alongside a typedef
24259 specifier. If there is a typedef specifier present then set
24260 conflicting_specifiers_p which will trigger an error later
24261 on in grokdeclarator. */
24262 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24263 decl_specs->conflicting_specifiers_p = true;
24266 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24267 is true, the type is a class or enum definition. */
24269 static void
24270 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24271 tree type_spec,
24272 cp_token *token,
24273 bool type_definition_p)
24275 decl_specs->any_specifiers_p = true;
24277 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24278 (with, for example, in "typedef int wchar_t;") we remember that
24279 this is what happened. In system headers, we ignore these
24280 declarations so that G++ can work with system headers that are not
24281 C++-safe. */
24282 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24283 && !type_definition_p
24284 && (type_spec == boolean_type_node
24285 || type_spec == char16_type_node
24286 || type_spec == char32_type_node
24287 || type_spec == wchar_type_node)
24288 && (decl_specs->type
24289 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24290 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24291 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24292 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24294 decl_specs->redefined_builtin_type = type_spec;
24295 set_and_check_decl_spec_loc (decl_specs,
24296 ds_redefined_builtin_type_spec,
24297 token);
24298 if (!decl_specs->type)
24300 decl_specs->type = type_spec;
24301 decl_specs->type_definition_p = false;
24302 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24305 else if (decl_specs->type)
24306 decl_specs->multiple_types_p = true;
24307 else
24309 decl_specs->type = type_spec;
24310 decl_specs->type_definition_p = type_definition_p;
24311 decl_specs->redefined_builtin_type = NULL_TREE;
24312 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24316 /* True iff TOKEN is the GNU keyword __thread. */
24318 static bool
24319 token_is__thread (cp_token *token)
24321 gcc_assert (token->keyword == RID_THREAD);
24322 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24325 /* Set the location for a declarator specifier and check if it is
24326 duplicated.
24328 DECL_SPECS is the sequence of declarator specifiers onto which to
24329 set the location.
24331 DS is the single declarator specifier to set which location is to
24332 be set onto the existing sequence of declarators.
24334 LOCATION is the location for the declarator specifier to
24335 consider. */
24337 static void
24338 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24339 cp_decl_spec ds, cp_token *token)
24341 gcc_assert (ds < ds_last);
24343 if (decl_specs == NULL)
24344 return;
24346 source_location location = token->location;
24348 if (decl_specs->locations[ds] == 0)
24350 decl_specs->locations[ds] = location;
24351 if (ds == ds_thread)
24352 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24354 else
24356 if (ds == ds_long)
24358 if (decl_specs->locations[ds_long_long] != 0)
24359 error_at (location,
24360 "%<long long long%> is too long for GCC");
24361 else
24363 decl_specs->locations[ds_long_long] = location;
24364 pedwarn_cxx98 (location,
24365 OPT_Wlong_long,
24366 "ISO C++ 1998 does not support %<long long%>");
24369 else if (ds == ds_thread)
24371 bool gnu = token_is__thread (token);
24372 if (gnu != decl_specs->gnu_thread_keyword_p)
24373 error_at (location,
24374 "both %<__thread%> and %<thread_local%> specified");
24375 else
24376 error_at (location, "duplicate %qD", token->u.value);
24378 else
24380 static const char *const decl_spec_names[] = {
24381 "signed",
24382 "unsigned",
24383 "short",
24384 "long",
24385 "const",
24386 "volatile",
24387 "restrict",
24388 "inline",
24389 "virtual",
24390 "explicit",
24391 "friend",
24392 "typedef",
24393 "using",
24394 "constexpr",
24395 "__complex"
24397 error_at (location,
24398 "duplicate %qs", decl_spec_names[ds]);
24403 /* Return true iff the declarator specifier DS is present in the
24404 sequence of declarator specifiers DECL_SPECS. */
24406 bool
24407 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24408 cp_decl_spec ds)
24410 gcc_assert (ds < ds_last);
24412 if (decl_specs == NULL)
24413 return false;
24415 return decl_specs->locations[ds] != 0;
24418 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24419 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24421 static bool
24422 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24424 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24427 /* Issue an error message indicating that TOKEN_DESC was expected.
24428 If KEYWORD is true, it indicated this function is called by
24429 cp_parser_require_keword and the required token can only be
24430 a indicated keyword. */
24432 static void
24433 cp_parser_required_error (cp_parser *parser,
24434 required_token token_desc,
24435 bool keyword)
24437 switch (token_desc)
24439 case RT_NEW:
24440 cp_parser_error (parser, "expected %<new%>");
24441 return;
24442 case RT_DELETE:
24443 cp_parser_error (parser, "expected %<delete%>");
24444 return;
24445 case RT_RETURN:
24446 cp_parser_error (parser, "expected %<return%>");
24447 return;
24448 case RT_WHILE:
24449 cp_parser_error (parser, "expected %<while%>");
24450 return;
24451 case RT_EXTERN:
24452 cp_parser_error (parser, "expected %<extern%>");
24453 return;
24454 case RT_STATIC_ASSERT:
24455 cp_parser_error (parser, "expected %<static_assert%>");
24456 return;
24457 case RT_DECLTYPE:
24458 cp_parser_error (parser, "expected %<decltype%>");
24459 return;
24460 case RT_OPERATOR:
24461 cp_parser_error (parser, "expected %<operator%>");
24462 return;
24463 case RT_CLASS:
24464 cp_parser_error (parser, "expected %<class%>");
24465 return;
24466 case RT_TEMPLATE:
24467 cp_parser_error (parser, "expected %<template%>");
24468 return;
24469 case RT_NAMESPACE:
24470 cp_parser_error (parser, "expected %<namespace%>");
24471 return;
24472 case RT_USING:
24473 cp_parser_error (parser, "expected %<using%>");
24474 return;
24475 case RT_ASM:
24476 cp_parser_error (parser, "expected %<asm%>");
24477 return;
24478 case RT_TRY:
24479 cp_parser_error (parser, "expected %<try%>");
24480 return;
24481 case RT_CATCH:
24482 cp_parser_error (parser, "expected %<catch%>");
24483 return;
24484 case RT_THROW:
24485 cp_parser_error (parser, "expected %<throw%>");
24486 return;
24487 case RT_LABEL:
24488 cp_parser_error (parser, "expected %<__label__%>");
24489 return;
24490 case RT_AT_TRY:
24491 cp_parser_error (parser, "expected %<@try%>");
24492 return;
24493 case RT_AT_SYNCHRONIZED:
24494 cp_parser_error (parser, "expected %<@synchronized%>");
24495 return;
24496 case RT_AT_THROW:
24497 cp_parser_error (parser, "expected %<@throw%>");
24498 return;
24499 case RT_TRANSACTION_ATOMIC:
24500 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24501 return;
24502 case RT_TRANSACTION_RELAXED:
24503 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24504 return;
24505 default:
24506 break;
24508 if (!keyword)
24510 switch (token_desc)
24512 case RT_SEMICOLON:
24513 cp_parser_error (parser, "expected %<;%>");
24514 return;
24515 case RT_OPEN_PAREN:
24516 cp_parser_error (parser, "expected %<(%>");
24517 return;
24518 case RT_CLOSE_BRACE:
24519 cp_parser_error (parser, "expected %<}%>");
24520 return;
24521 case RT_OPEN_BRACE:
24522 cp_parser_error (parser, "expected %<{%>");
24523 return;
24524 case RT_CLOSE_SQUARE:
24525 cp_parser_error (parser, "expected %<]%>");
24526 return;
24527 case RT_OPEN_SQUARE:
24528 cp_parser_error (parser, "expected %<[%>");
24529 return;
24530 case RT_COMMA:
24531 cp_parser_error (parser, "expected %<,%>");
24532 return;
24533 case RT_SCOPE:
24534 cp_parser_error (parser, "expected %<::%>");
24535 return;
24536 case RT_LESS:
24537 cp_parser_error (parser, "expected %<<%>");
24538 return;
24539 case RT_GREATER:
24540 cp_parser_error (parser, "expected %<>%>");
24541 return;
24542 case RT_EQ:
24543 cp_parser_error (parser, "expected %<=%>");
24544 return;
24545 case RT_ELLIPSIS:
24546 cp_parser_error (parser, "expected %<...%>");
24547 return;
24548 case RT_MULT:
24549 cp_parser_error (parser, "expected %<*%>");
24550 return;
24551 case RT_COMPL:
24552 cp_parser_error (parser, "expected %<~%>");
24553 return;
24554 case RT_COLON:
24555 cp_parser_error (parser, "expected %<:%>");
24556 return;
24557 case RT_COLON_SCOPE:
24558 cp_parser_error (parser, "expected %<:%> or %<::%>");
24559 return;
24560 case RT_CLOSE_PAREN:
24561 cp_parser_error (parser, "expected %<)%>");
24562 return;
24563 case RT_COMMA_CLOSE_PAREN:
24564 cp_parser_error (parser, "expected %<,%> or %<)%>");
24565 return;
24566 case RT_PRAGMA_EOL:
24567 cp_parser_error (parser, "expected end of line");
24568 return;
24569 case RT_NAME:
24570 cp_parser_error (parser, "expected identifier");
24571 return;
24572 case RT_SELECT:
24573 cp_parser_error (parser, "expected selection-statement");
24574 return;
24575 case RT_INTERATION:
24576 cp_parser_error (parser, "expected iteration-statement");
24577 return;
24578 case RT_JUMP:
24579 cp_parser_error (parser, "expected jump-statement");
24580 return;
24581 case RT_CLASS_KEY:
24582 cp_parser_error (parser, "expected class-key");
24583 return;
24584 case RT_CLASS_TYPENAME_TEMPLATE:
24585 cp_parser_error (parser,
24586 "expected %<class%>, %<typename%>, or %<template%>");
24587 return;
24588 default:
24589 gcc_unreachable ();
24592 else
24593 gcc_unreachable ();
24598 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24599 issue an error message indicating that TOKEN_DESC was expected.
24601 Returns the token consumed, if the token had the appropriate type.
24602 Otherwise, returns NULL. */
24604 static cp_token *
24605 cp_parser_require (cp_parser* parser,
24606 enum cpp_ttype type,
24607 required_token token_desc)
24609 if (cp_lexer_next_token_is (parser->lexer, type))
24610 return cp_lexer_consume_token (parser->lexer);
24611 else
24613 /* Output the MESSAGE -- unless we're parsing tentatively. */
24614 if (!cp_parser_simulate_error (parser))
24615 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24616 return NULL;
24620 /* An error message is produced if the next token is not '>'.
24621 All further tokens are skipped until the desired token is
24622 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24624 static void
24625 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24627 /* Current level of '< ... >'. */
24628 unsigned level = 0;
24629 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24630 unsigned nesting_depth = 0;
24632 /* Are we ready, yet? If not, issue error message. */
24633 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24634 return;
24636 /* Skip tokens until the desired token is found. */
24637 while (true)
24639 /* Peek at the next token. */
24640 switch (cp_lexer_peek_token (parser->lexer)->type)
24642 case CPP_LESS:
24643 if (!nesting_depth)
24644 ++level;
24645 break;
24647 case CPP_RSHIFT:
24648 if (cxx_dialect == cxx98)
24649 /* C++0x views the `>>' operator as two `>' tokens, but
24650 C++98 does not. */
24651 break;
24652 else if (!nesting_depth && level-- == 0)
24654 /* We've hit a `>>' where the first `>' closes the
24655 template argument list, and the second `>' is
24656 spurious. Just consume the `>>' and stop; we've
24657 already produced at least one error. */
24658 cp_lexer_consume_token (parser->lexer);
24659 return;
24661 /* Fall through for C++0x, so we handle the second `>' in
24662 the `>>'. */
24664 case CPP_GREATER:
24665 if (!nesting_depth && level-- == 0)
24667 /* We've reached the token we want, consume it and stop. */
24668 cp_lexer_consume_token (parser->lexer);
24669 return;
24671 break;
24673 case CPP_OPEN_PAREN:
24674 case CPP_OPEN_SQUARE:
24675 ++nesting_depth;
24676 break;
24678 case CPP_CLOSE_PAREN:
24679 case CPP_CLOSE_SQUARE:
24680 if (nesting_depth-- == 0)
24681 return;
24682 break;
24684 case CPP_EOF:
24685 case CPP_PRAGMA_EOL:
24686 case CPP_SEMICOLON:
24687 case CPP_OPEN_BRACE:
24688 case CPP_CLOSE_BRACE:
24689 /* The '>' was probably forgotten, don't look further. */
24690 return;
24692 default:
24693 break;
24696 /* Consume this token. */
24697 cp_lexer_consume_token (parser->lexer);
24701 /* If the next token is the indicated keyword, consume it. Otherwise,
24702 issue an error message indicating that TOKEN_DESC was expected.
24704 Returns the token consumed, if the token had the appropriate type.
24705 Otherwise, returns NULL. */
24707 static cp_token *
24708 cp_parser_require_keyword (cp_parser* parser,
24709 enum rid keyword,
24710 required_token token_desc)
24712 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24714 if (token && token->keyword != keyword)
24716 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24717 return NULL;
24720 return token;
24723 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24724 function-definition. */
24726 static bool
24727 cp_parser_token_starts_function_definition_p (cp_token* token)
24729 return (/* An ordinary function-body begins with an `{'. */
24730 token->type == CPP_OPEN_BRACE
24731 /* A ctor-initializer begins with a `:'. */
24732 || token->type == CPP_COLON
24733 /* A function-try-block begins with `try'. */
24734 || token->keyword == RID_TRY
24735 /* A function-transaction-block begins with `__transaction_atomic'
24736 or `__transaction_relaxed'. */
24737 || token->keyword == RID_TRANSACTION_ATOMIC
24738 || token->keyword == RID_TRANSACTION_RELAXED
24739 /* The named return value extension begins with `return'. */
24740 || token->keyword == RID_RETURN);
24743 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24744 definition. */
24746 static bool
24747 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24749 cp_token *token;
24751 token = cp_lexer_peek_token (parser->lexer);
24752 return (token->type == CPP_OPEN_BRACE
24753 || (token->type == CPP_COLON
24754 && !parser->colon_doesnt_start_class_def_p));
24757 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24758 C++0x) ending a template-argument. */
24760 static bool
24761 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24763 cp_token *token;
24765 token = cp_lexer_peek_token (parser->lexer);
24766 return (token->type == CPP_COMMA
24767 || token->type == CPP_GREATER
24768 || token->type == CPP_ELLIPSIS
24769 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24772 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24773 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24775 static bool
24776 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24777 size_t n)
24779 cp_token *token;
24781 token = cp_lexer_peek_nth_token (parser->lexer, n);
24782 if (token->type == CPP_LESS)
24783 return true;
24784 /* Check for the sequence `<::' in the original code. It would be lexed as
24785 `[:', where `[' is a digraph, and there is no whitespace before
24786 `:'. */
24787 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24789 cp_token *token2;
24790 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24791 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24792 return true;
24794 return false;
24797 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24798 or none_type otherwise. */
24800 static enum tag_types
24801 cp_parser_token_is_class_key (cp_token* token)
24803 switch (token->keyword)
24805 case RID_CLASS:
24806 return class_type;
24807 case RID_STRUCT:
24808 return record_type;
24809 case RID_UNION:
24810 return union_type;
24812 default:
24813 return none_type;
24817 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
24818 or none_type otherwise or if the token is null. */
24820 static enum tag_types
24821 cp_parser_token_is_type_parameter_key (cp_token* token)
24823 if (!token)
24824 return none_type;
24826 switch (token->keyword)
24828 case RID_CLASS:
24829 return class_type;
24830 case RID_TYPENAME:
24831 return typename_type;
24833 default:
24834 return none_type;
24838 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24840 static void
24841 cp_parser_check_class_key (enum tag_types class_key, tree type)
24843 if (type == error_mark_node)
24844 return;
24845 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24847 if (permerror (input_location, "%qs tag used in naming %q#T",
24848 class_key == union_type ? "union"
24849 : class_key == record_type ? "struct" : "class",
24850 type))
24851 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24852 "%q#T was previously declared here", type);
24856 /* Issue an error message if DECL is redeclared with different
24857 access than its original declaration [class.access.spec/3].
24858 This applies to nested classes and nested class templates.
24859 [class.mem/1]. */
24861 static void
24862 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24864 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24865 return;
24867 if ((TREE_PRIVATE (decl)
24868 != (current_access_specifier == access_private_node))
24869 || (TREE_PROTECTED (decl)
24870 != (current_access_specifier == access_protected_node)))
24871 error_at (location, "%qD redeclared with different access", decl);
24874 /* Look for the `template' keyword, as a syntactic disambiguator.
24875 Return TRUE iff it is present, in which case it will be
24876 consumed. */
24878 static bool
24879 cp_parser_optional_template_keyword (cp_parser *parser)
24881 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24883 /* In C++98 the `template' keyword can only be used within templates;
24884 outside templates the parser can always figure out what is a
24885 template and what is not. In C++11, per the resolution of DR 468,
24886 `template' is allowed in cases where it is not strictly necessary. */
24887 if (!processing_template_decl
24888 && pedantic && cxx_dialect == cxx98)
24890 cp_token *token = cp_lexer_peek_token (parser->lexer);
24891 pedwarn (token->location, OPT_Wpedantic,
24892 "in C++98 %<template%> (as a disambiguator) is only "
24893 "allowed within templates");
24894 /* If this part of the token stream is rescanned, the same
24895 error message would be generated. So, we purge the token
24896 from the stream. */
24897 cp_lexer_purge_token (parser->lexer);
24898 return false;
24900 else
24902 /* Consume the `template' keyword. */
24903 cp_lexer_consume_token (parser->lexer);
24904 return true;
24907 return false;
24910 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24911 set PARSER->SCOPE, and perform other related actions. */
24913 static void
24914 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24916 int i;
24917 struct tree_check *check_value;
24918 deferred_access_check *chk;
24919 vec<deferred_access_check, va_gc> *checks;
24921 /* Get the stored value. */
24922 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24923 /* Perform any access checks that were deferred. */
24924 checks = check_value->checks;
24925 if (checks)
24927 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24928 perform_or_defer_access_check (chk->binfo,
24929 chk->decl,
24930 chk->diag_decl, tf_warning_or_error);
24932 /* Set the scope from the stored value. */
24933 parser->scope = check_value->value;
24934 parser->qualifying_scope = check_value->qualifying_scope;
24935 parser->object_scope = NULL_TREE;
24938 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24939 encounter the end of a block before what we were looking for. */
24941 static bool
24942 cp_parser_cache_group (cp_parser *parser,
24943 enum cpp_ttype end,
24944 unsigned depth)
24946 while (true)
24948 cp_token *token = cp_lexer_peek_token (parser->lexer);
24950 /* Abort a parenthesized expression if we encounter a semicolon. */
24951 if ((end == CPP_CLOSE_PAREN || depth == 0)
24952 && token->type == CPP_SEMICOLON)
24953 return true;
24954 /* If we've reached the end of the file, stop. */
24955 if (token->type == CPP_EOF
24956 || (end != CPP_PRAGMA_EOL
24957 && token->type == CPP_PRAGMA_EOL))
24958 return true;
24959 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24960 /* We've hit the end of an enclosing block, so there's been some
24961 kind of syntax error. */
24962 return true;
24964 /* Consume the token. */
24965 cp_lexer_consume_token (parser->lexer);
24966 /* See if it starts a new group. */
24967 if (token->type == CPP_OPEN_BRACE)
24969 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24970 /* In theory this should probably check end == '}', but
24971 cp_parser_save_member_function_body needs it to exit
24972 after either '}' or ')' when called with ')'. */
24973 if (depth == 0)
24974 return false;
24976 else if (token->type == CPP_OPEN_PAREN)
24978 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24979 if (depth == 0 && end == CPP_CLOSE_PAREN)
24980 return false;
24982 else if (token->type == CPP_PRAGMA)
24983 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24984 else if (token->type == end)
24985 return false;
24989 /* Like above, for caching a default argument or NSDMI. Both of these are
24990 terminated by a non-nested comma, but it can be unclear whether or not a
24991 comma is nested in a template argument list unless we do more parsing.
24992 In order to handle this ambiguity, when we encounter a ',' after a '<'
24993 we try to parse what follows as a parameter-declaration-list (in the
24994 case of a default argument) or a member-declarator (in the case of an
24995 NSDMI). If that succeeds, then we stop caching. */
24997 static tree
24998 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25000 unsigned depth = 0;
25001 int maybe_template_id = 0;
25002 cp_token *first_token;
25003 cp_token *token;
25004 tree default_argument;
25006 /* Add tokens until we have processed the entire default
25007 argument. We add the range [first_token, token). */
25008 first_token = cp_lexer_peek_token (parser->lexer);
25009 if (first_token->type == CPP_OPEN_BRACE)
25011 /* For list-initialization, this is straightforward. */
25012 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25013 token = cp_lexer_peek_token (parser->lexer);
25015 else while (true)
25017 bool done = false;
25019 /* Peek at the next token. */
25020 token = cp_lexer_peek_token (parser->lexer);
25021 /* What we do depends on what token we have. */
25022 switch (token->type)
25024 /* In valid code, a default argument must be
25025 immediately followed by a `,' `)', or `...'. */
25026 case CPP_COMMA:
25027 if (depth == 0 && maybe_template_id)
25029 /* If we've seen a '<', we might be in a
25030 template-argument-list. Until Core issue 325 is
25031 resolved, we don't know how this situation ought
25032 to be handled, so try to DTRT. We check whether
25033 what comes after the comma is a valid parameter
25034 declaration list. If it is, then the comma ends
25035 the default argument; otherwise the default
25036 argument continues. */
25037 bool error = false;
25039 /* Set ITALP so cp_parser_parameter_declaration_list
25040 doesn't decide to commit to this parse. */
25041 bool saved_italp = parser->in_template_argument_list_p;
25042 parser->in_template_argument_list_p = true;
25044 cp_parser_parse_tentatively (parser);
25045 cp_lexer_consume_token (parser->lexer);
25047 if (nsdmi)
25049 int ctor_dtor_or_conv_p;
25050 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25051 &ctor_dtor_or_conv_p,
25052 /*parenthesized_p=*/NULL,
25053 /*member_p=*/true,
25054 /*friend_p=*/false);
25056 else
25058 begin_scope (sk_function_parms, NULL_TREE);
25059 cp_parser_parameter_declaration_list (parser, &error);
25060 pop_bindings_and_leave_scope ();
25062 if (!cp_parser_error_occurred (parser) && !error)
25063 done = true;
25064 cp_parser_abort_tentative_parse (parser);
25066 parser->in_template_argument_list_p = saved_italp;
25067 break;
25069 case CPP_CLOSE_PAREN:
25070 case CPP_ELLIPSIS:
25071 /* If we run into a non-nested `;', `}', or `]',
25072 then the code is invalid -- but the default
25073 argument is certainly over. */
25074 case CPP_SEMICOLON:
25075 case CPP_CLOSE_BRACE:
25076 case CPP_CLOSE_SQUARE:
25077 if (depth == 0
25078 /* Handle correctly int n = sizeof ... ( p ); */
25079 && token->type != CPP_ELLIPSIS)
25080 done = true;
25081 /* Update DEPTH, if necessary. */
25082 else if (token->type == CPP_CLOSE_PAREN
25083 || token->type == CPP_CLOSE_BRACE
25084 || token->type == CPP_CLOSE_SQUARE)
25085 --depth;
25086 break;
25088 case CPP_OPEN_PAREN:
25089 case CPP_OPEN_SQUARE:
25090 case CPP_OPEN_BRACE:
25091 ++depth;
25092 break;
25094 case CPP_LESS:
25095 if (depth == 0)
25096 /* This might be the comparison operator, or it might
25097 start a template argument list. */
25098 ++maybe_template_id;
25099 break;
25101 case CPP_RSHIFT:
25102 if (cxx_dialect == cxx98)
25103 break;
25104 /* Fall through for C++0x, which treats the `>>'
25105 operator like two `>' tokens in certain
25106 cases. */
25108 case CPP_GREATER:
25109 if (depth == 0)
25111 /* This might be an operator, or it might close a
25112 template argument list. But if a previous '<'
25113 started a template argument list, this will have
25114 closed it, so we can't be in one anymore. */
25115 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25116 if (maybe_template_id < 0)
25117 maybe_template_id = 0;
25119 break;
25121 /* If we run out of tokens, issue an error message. */
25122 case CPP_EOF:
25123 case CPP_PRAGMA_EOL:
25124 error_at (token->location, "file ends in default argument");
25125 done = true;
25126 break;
25128 case CPP_NAME:
25129 case CPP_SCOPE:
25130 /* In these cases, we should look for template-ids.
25131 For example, if the default argument is
25132 `X<int, double>()', we need to do name lookup to
25133 figure out whether or not `X' is a template; if
25134 so, the `,' does not end the default argument.
25136 That is not yet done. */
25137 break;
25139 default:
25140 break;
25143 /* If we've reached the end, stop. */
25144 if (done)
25145 break;
25147 /* Add the token to the token block. */
25148 token = cp_lexer_consume_token (parser->lexer);
25151 /* Create a DEFAULT_ARG to represent the unparsed default
25152 argument. */
25153 default_argument = make_node (DEFAULT_ARG);
25154 DEFARG_TOKENS (default_argument)
25155 = cp_token_cache_new (first_token, token);
25156 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25158 return default_argument;
25161 /* Begin parsing tentatively. We always save tokens while parsing
25162 tentatively so that if the tentative parsing fails we can restore the
25163 tokens. */
25165 static void
25166 cp_parser_parse_tentatively (cp_parser* parser)
25168 /* Enter a new parsing context. */
25169 parser->context = cp_parser_context_new (parser->context);
25170 /* Begin saving tokens. */
25171 cp_lexer_save_tokens (parser->lexer);
25172 /* In order to avoid repetitive access control error messages,
25173 access checks are queued up until we are no longer parsing
25174 tentatively. */
25175 push_deferring_access_checks (dk_deferred);
25178 /* Commit to the currently active tentative parse. */
25180 static void
25181 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25183 cp_parser_context *context;
25184 cp_lexer *lexer;
25186 /* Mark all of the levels as committed. */
25187 lexer = parser->lexer;
25188 for (context = parser->context; context->next; context = context->next)
25190 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25191 break;
25192 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25193 while (!cp_lexer_saving_tokens (lexer))
25194 lexer = lexer->next;
25195 cp_lexer_commit_tokens (lexer);
25199 /* Commit to the topmost currently active tentative parse.
25201 Note that this function shouldn't be called when there are
25202 irreversible side-effects while in a tentative state. For
25203 example, we shouldn't create a permanent entry in the symbol
25204 table, or issue an error message that might not apply if the
25205 tentative parse is aborted. */
25207 static void
25208 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25210 cp_parser_context *context = parser->context;
25211 cp_lexer *lexer = parser->lexer;
25213 if (context)
25215 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25216 return;
25217 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25219 while (!cp_lexer_saving_tokens (lexer))
25220 lexer = lexer->next;
25221 cp_lexer_commit_tokens (lexer);
25225 /* Abort the currently active tentative parse. All consumed tokens
25226 will be rolled back, and no diagnostics will be issued. */
25228 static void
25229 cp_parser_abort_tentative_parse (cp_parser* parser)
25231 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25232 || errorcount > 0);
25233 cp_parser_simulate_error (parser);
25234 /* Now, pretend that we want to see if the construct was
25235 successfully parsed. */
25236 cp_parser_parse_definitely (parser);
25239 /* Stop parsing tentatively. If a parse error has occurred, restore the
25240 token stream. Otherwise, commit to the tokens we have consumed.
25241 Returns true if no error occurred; false otherwise. */
25243 static bool
25244 cp_parser_parse_definitely (cp_parser* parser)
25246 bool error_occurred;
25247 cp_parser_context *context;
25249 /* Remember whether or not an error occurred, since we are about to
25250 destroy that information. */
25251 error_occurred = cp_parser_error_occurred (parser);
25252 /* Remove the topmost context from the stack. */
25253 context = parser->context;
25254 parser->context = context->next;
25255 /* If no parse errors occurred, commit to the tentative parse. */
25256 if (!error_occurred)
25258 /* Commit to the tokens read tentatively, unless that was
25259 already done. */
25260 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25261 cp_lexer_commit_tokens (parser->lexer);
25263 pop_to_parent_deferring_access_checks ();
25265 /* Otherwise, if errors occurred, roll back our state so that things
25266 are just as they were before we began the tentative parse. */
25267 else
25269 cp_lexer_rollback_tokens (parser->lexer);
25270 pop_deferring_access_checks ();
25272 /* Add the context to the front of the free list. */
25273 context->next = cp_parser_context_free_list;
25274 cp_parser_context_free_list = context;
25276 return !error_occurred;
25279 /* Returns true if we are parsing tentatively and are not committed to
25280 this tentative parse. */
25282 static bool
25283 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25285 return (cp_parser_parsing_tentatively (parser)
25286 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25289 /* Returns nonzero iff an error has occurred during the most recent
25290 tentative parse. */
25292 static bool
25293 cp_parser_error_occurred (cp_parser* parser)
25295 return (cp_parser_parsing_tentatively (parser)
25296 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25299 /* Returns nonzero if GNU extensions are allowed. */
25301 static bool
25302 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25304 return parser->allow_gnu_extensions_p;
25307 /* Objective-C++ Productions */
25310 /* Parse an Objective-C expression, which feeds into a primary-expression
25311 above.
25313 objc-expression:
25314 objc-message-expression
25315 objc-string-literal
25316 objc-encode-expression
25317 objc-protocol-expression
25318 objc-selector-expression
25320 Returns a tree representation of the expression. */
25322 static tree
25323 cp_parser_objc_expression (cp_parser* parser)
25325 /* Try to figure out what kind of declaration is present. */
25326 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25328 switch (kwd->type)
25330 case CPP_OPEN_SQUARE:
25331 return cp_parser_objc_message_expression (parser);
25333 case CPP_OBJC_STRING:
25334 kwd = cp_lexer_consume_token (parser->lexer);
25335 return objc_build_string_object (kwd->u.value);
25337 case CPP_KEYWORD:
25338 switch (kwd->keyword)
25340 case RID_AT_ENCODE:
25341 return cp_parser_objc_encode_expression (parser);
25343 case RID_AT_PROTOCOL:
25344 return cp_parser_objc_protocol_expression (parser);
25346 case RID_AT_SELECTOR:
25347 return cp_parser_objc_selector_expression (parser);
25349 default:
25350 break;
25352 default:
25353 error_at (kwd->location,
25354 "misplaced %<@%D%> Objective-C++ construct",
25355 kwd->u.value);
25356 cp_parser_skip_to_end_of_block_or_statement (parser);
25359 return error_mark_node;
25362 /* Parse an Objective-C message expression.
25364 objc-message-expression:
25365 [ objc-message-receiver objc-message-args ]
25367 Returns a representation of an Objective-C message. */
25369 static tree
25370 cp_parser_objc_message_expression (cp_parser* parser)
25372 tree receiver, messageargs;
25374 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25375 receiver = cp_parser_objc_message_receiver (parser);
25376 messageargs = cp_parser_objc_message_args (parser);
25377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25379 return objc_build_message_expr (receiver, messageargs);
25382 /* Parse an objc-message-receiver.
25384 objc-message-receiver:
25385 expression
25386 simple-type-specifier
25388 Returns a representation of the type or expression. */
25390 static tree
25391 cp_parser_objc_message_receiver (cp_parser* parser)
25393 tree rcv;
25395 /* An Objective-C message receiver may be either (1) a type
25396 or (2) an expression. */
25397 cp_parser_parse_tentatively (parser);
25398 rcv = cp_parser_expression (parser);
25400 if (cp_parser_parse_definitely (parser))
25401 return rcv;
25403 rcv = cp_parser_simple_type_specifier (parser,
25404 /*decl_specs=*/NULL,
25405 CP_PARSER_FLAGS_NONE);
25407 return objc_get_class_reference (rcv);
25410 /* Parse the arguments and selectors comprising an Objective-C message.
25412 objc-message-args:
25413 objc-selector
25414 objc-selector-args
25415 objc-selector-args , objc-comma-args
25417 objc-selector-args:
25418 objc-selector [opt] : assignment-expression
25419 objc-selector-args objc-selector [opt] : assignment-expression
25421 objc-comma-args:
25422 assignment-expression
25423 objc-comma-args , assignment-expression
25425 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25426 selector arguments and TREE_VALUE containing a list of comma
25427 arguments. */
25429 static tree
25430 cp_parser_objc_message_args (cp_parser* parser)
25432 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25433 bool maybe_unary_selector_p = true;
25434 cp_token *token = cp_lexer_peek_token (parser->lexer);
25436 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25438 tree selector = NULL_TREE, arg;
25440 if (token->type != CPP_COLON)
25441 selector = cp_parser_objc_selector (parser);
25443 /* Detect if we have a unary selector. */
25444 if (maybe_unary_selector_p
25445 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25446 return build_tree_list (selector, NULL_TREE);
25448 maybe_unary_selector_p = false;
25449 cp_parser_require (parser, CPP_COLON, RT_COLON);
25450 arg = cp_parser_assignment_expression (parser, false, NULL);
25452 sel_args
25453 = chainon (sel_args,
25454 build_tree_list (selector, arg));
25456 token = cp_lexer_peek_token (parser->lexer);
25459 /* Handle non-selector arguments, if any. */
25460 while (token->type == CPP_COMMA)
25462 tree arg;
25464 cp_lexer_consume_token (parser->lexer);
25465 arg = cp_parser_assignment_expression (parser, false, NULL);
25467 addl_args
25468 = chainon (addl_args,
25469 build_tree_list (NULL_TREE, arg));
25471 token = cp_lexer_peek_token (parser->lexer);
25474 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25476 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25477 return build_tree_list (error_mark_node, error_mark_node);
25480 return build_tree_list (sel_args, addl_args);
25483 /* Parse an Objective-C encode expression.
25485 objc-encode-expression:
25486 @encode objc-typename
25488 Returns an encoded representation of the type argument. */
25490 static tree
25491 cp_parser_objc_encode_expression (cp_parser* parser)
25493 tree type;
25494 cp_token *token;
25496 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25497 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25498 token = cp_lexer_peek_token (parser->lexer);
25499 type = complete_type (cp_parser_type_id (parser));
25500 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25502 if (!type)
25504 error_at (token->location,
25505 "%<@encode%> must specify a type as an argument");
25506 return error_mark_node;
25509 /* This happens if we find @encode(T) (where T is a template
25510 typename or something dependent on a template typename) when
25511 parsing a template. In that case, we can't compile it
25512 immediately, but we rather create an AT_ENCODE_EXPR which will
25513 need to be instantiated when the template is used.
25515 if (dependent_type_p (type))
25517 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25518 TREE_READONLY (value) = 1;
25519 return value;
25522 return objc_build_encode_expr (type);
25525 /* Parse an Objective-C @defs expression. */
25527 static tree
25528 cp_parser_objc_defs_expression (cp_parser *parser)
25530 tree name;
25532 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25533 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25534 name = cp_parser_identifier (parser);
25535 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25537 return objc_get_class_ivars (name);
25540 /* Parse an Objective-C protocol expression.
25542 objc-protocol-expression:
25543 @protocol ( identifier )
25545 Returns a representation of the protocol expression. */
25547 static tree
25548 cp_parser_objc_protocol_expression (cp_parser* parser)
25550 tree proto;
25552 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25553 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25554 proto = cp_parser_identifier (parser);
25555 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25557 return objc_build_protocol_expr (proto);
25560 /* Parse an Objective-C selector expression.
25562 objc-selector-expression:
25563 @selector ( objc-method-signature )
25565 objc-method-signature:
25566 objc-selector
25567 objc-selector-seq
25569 objc-selector-seq:
25570 objc-selector :
25571 objc-selector-seq objc-selector :
25573 Returns a representation of the method selector. */
25575 static tree
25576 cp_parser_objc_selector_expression (cp_parser* parser)
25578 tree sel_seq = NULL_TREE;
25579 bool maybe_unary_selector_p = true;
25580 cp_token *token;
25581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25583 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25584 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25585 token = cp_lexer_peek_token (parser->lexer);
25587 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25588 || token->type == CPP_SCOPE)
25590 tree selector = NULL_TREE;
25592 if (token->type != CPP_COLON
25593 || token->type == CPP_SCOPE)
25594 selector = cp_parser_objc_selector (parser);
25596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25597 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25599 /* Detect if we have a unary selector. */
25600 if (maybe_unary_selector_p)
25602 sel_seq = selector;
25603 goto finish_selector;
25605 else
25607 cp_parser_error (parser, "expected %<:%>");
25610 maybe_unary_selector_p = false;
25611 token = cp_lexer_consume_token (parser->lexer);
25613 if (token->type == CPP_SCOPE)
25615 sel_seq
25616 = chainon (sel_seq,
25617 build_tree_list (selector, NULL_TREE));
25618 sel_seq
25619 = chainon (sel_seq,
25620 build_tree_list (NULL_TREE, NULL_TREE));
25622 else
25623 sel_seq
25624 = chainon (sel_seq,
25625 build_tree_list (selector, NULL_TREE));
25627 token = cp_lexer_peek_token (parser->lexer);
25630 finish_selector:
25631 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25633 return objc_build_selector_expr (loc, sel_seq);
25636 /* Parse a list of identifiers.
25638 objc-identifier-list:
25639 identifier
25640 objc-identifier-list , identifier
25642 Returns a TREE_LIST of identifier nodes. */
25644 static tree
25645 cp_parser_objc_identifier_list (cp_parser* parser)
25647 tree identifier;
25648 tree list;
25649 cp_token *sep;
25651 identifier = cp_parser_identifier (parser);
25652 if (identifier == error_mark_node)
25653 return error_mark_node;
25655 list = build_tree_list (NULL_TREE, identifier);
25656 sep = cp_lexer_peek_token (parser->lexer);
25658 while (sep->type == CPP_COMMA)
25660 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25661 identifier = cp_parser_identifier (parser);
25662 if (identifier == error_mark_node)
25663 return list;
25665 list = chainon (list, build_tree_list (NULL_TREE,
25666 identifier));
25667 sep = cp_lexer_peek_token (parser->lexer);
25670 return list;
25673 /* Parse an Objective-C alias declaration.
25675 objc-alias-declaration:
25676 @compatibility_alias identifier identifier ;
25678 This function registers the alias mapping with the Objective-C front end.
25679 It returns nothing. */
25681 static void
25682 cp_parser_objc_alias_declaration (cp_parser* parser)
25684 tree alias, orig;
25686 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25687 alias = cp_parser_identifier (parser);
25688 orig = cp_parser_identifier (parser);
25689 objc_declare_alias (alias, orig);
25690 cp_parser_consume_semicolon_at_end_of_statement (parser);
25693 /* Parse an Objective-C class forward-declaration.
25695 objc-class-declaration:
25696 @class objc-identifier-list ;
25698 The function registers the forward declarations with the Objective-C
25699 front end. It returns nothing. */
25701 static void
25702 cp_parser_objc_class_declaration (cp_parser* parser)
25704 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25705 while (true)
25707 tree id;
25709 id = cp_parser_identifier (parser);
25710 if (id == error_mark_node)
25711 break;
25713 objc_declare_class (id);
25715 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25716 cp_lexer_consume_token (parser->lexer);
25717 else
25718 break;
25720 cp_parser_consume_semicolon_at_end_of_statement (parser);
25723 /* Parse a list of Objective-C protocol references.
25725 objc-protocol-refs-opt:
25726 objc-protocol-refs [opt]
25728 objc-protocol-refs:
25729 < objc-identifier-list >
25731 Returns a TREE_LIST of identifiers, if any. */
25733 static tree
25734 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25736 tree protorefs = NULL_TREE;
25738 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25740 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25741 protorefs = cp_parser_objc_identifier_list (parser);
25742 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25745 return protorefs;
25748 /* Parse a Objective-C visibility specification. */
25750 static void
25751 cp_parser_objc_visibility_spec (cp_parser* parser)
25753 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25755 switch (vis->keyword)
25757 case RID_AT_PRIVATE:
25758 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25759 break;
25760 case RID_AT_PROTECTED:
25761 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25762 break;
25763 case RID_AT_PUBLIC:
25764 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25765 break;
25766 case RID_AT_PACKAGE:
25767 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25768 break;
25769 default:
25770 return;
25773 /* Eat '@private'/'@protected'/'@public'. */
25774 cp_lexer_consume_token (parser->lexer);
25777 /* Parse an Objective-C method type. Return 'true' if it is a class
25778 (+) method, and 'false' if it is an instance (-) method. */
25780 static inline bool
25781 cp_parser_objc_method_type (cp_parser* parser)
25783 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25784 return true;
25785 else
25786 return false;
25789 /* Parse an Objective-C protocol qualifier. */
25791 static tree
25792 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25794 tree quals = NULL_TREE, node;
25795 cp_token *token = cp_lexer_peek_token (parser->lexer);
25797 node = token->u.value;
25799 while (node && identifier_p (node)
25800 && (node == ridpointers [(int) RID_IN]
25801 || node == ridpointers [(int) RID_OUT]
25802 || node == ridpointers [(int) RID_INOUT]
25803 || node == ridpointers [(int) RID_BYCOPY]
25804 || node == ridpointers [(int) RID_BYREF]
25805 || node == ridpointers [(int) RID_ONEWAY]))
25807 quals = tree_cons (NULL_TREE, node, quals);
25808 cp_lexer_consume_token (parser->lexer);
25809 token = cp_lexer_peek_token (parser->lexer);
25810 node = token->u.value;
25813 return quals;
25816 /* Parse an Objective-C typename. */
25818 static tree
25819 cp_parser_objc_typename (cp_parser* parser)
25821 tree type_name = NULL_TREE;
25823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25825 tree proto_quals, cp_type = NULL_TREE;
25827 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25828 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25830 /* An ObjC type name may consist of just protocol qualifiers, in which
25831 case the type shall default to 'id'. */
25832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25834 cp_type = cp_parser_type_id (parser);
25836 /* If the type could not be parsed, an error has already
25837 been produced. For error recovery, behave as if it had
25838 not been specified, which will use the default type
25839 'id'. */
25840 if (cp_type == error_mark_node)
25842 cp_type = NULL_TREE;
25843 /* We need to skip to the closing parenthesis as
25844 cp_parser_type_id() does not seem to do it for
25845 us. */
25846 cp_parser_skip_to_closing_parenthesis (parser,
25847 /*recovering=*/true,
25848 /*or_comma=*/false,
25849 /*consume_paren=*/false);
25853 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25854 type_name = build_tree_list (proto_quals, cp_type);
25857 return type_name;
25860 /* Check to see if TYPE refers to an Objective-C selector name. */
25862 static bool
25863 cp_parser_objc_selector_p (enum cpp_ttype type)
25865 return (type == CPP_NAME || type == CPP_KEYWORD
25866 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25867 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25868 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25869 || type == CPP_XOR || type == CPP_XOR_EQ);
25872 /* Parse an Objective-C selector. */
25874 static tree
25875 cp_parser_objc_selector (cp_parser* parser)
25877 cp_token *token = cp_lexer_consume_token (parser->lexer);
25879 if (!cp_parser_objc_selector_p (token->type))
25881 error_at (token->location, "invalid Objective-C++ selector name");
25882 return error_mark_node;
25885 /* C++ operator names are allowed to appear in ObjC selectors. */
25886 switch (token->type)
25888 case CPP_AND_AND: return get_identifier ("and");
25889 case CPP_AND_EQ: return get_identifier ("and_eq");
25890 case CPP_AND: return get_identifier ("bitand");
25891 case CPP_OR: return get_identifier ("bitor");
25892 case CPP_COMPL: return get_identifier ("compl");
25893 case CPP_NOT: return get_identifier ("not");
25894 case CPP_NOT_EQ: return get_identifier ("not_eq");
25895 case CPP_OR_OR: return get_identifier ("or");
25896 case CPP_OR_EQ: return get_identifier ("or_eq");
25897 case CPP_XOR: return get_identifier ("xor");
25898 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25899 default: return token->u.value;
25903 /* Parse an Objective-C params list. */
25905 static tree
25906 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25908 tree params = NULL_TREE;
25909 bool maybe_unary_selector_p = true;
25910 cp_token *token = cp_lexer_peek_token (parser->lexer);
25912 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25914 tree selector = NULL_TREE, type_name, identifier;
25915 tree parm_attr = NULL_TREE;
25917 if (token->keyword == RID_ATTRIBUTE)
25918 break;
25920 if (token->type != CPP_COLON)
25921 selector = cp_parser_objc_selector (parser);
25923 /* Detect if we have a unary selector. */
25924 if (maybe_unary_selector_p
25925 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25927 params = selector; /* Might be followed by attributes. */
25928 break;
25931 maybe_unary_selector_p = false;
25932 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25934 /* Something went quite wrong. There should be a colon
25935 here, but there is not. Stop parsing parameters. */
25936 break;
25938 type_name = cp_parser_objc_typename (parser);
25939 /* New ObjC allows attributes on parameters too. */
25940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25941 parm_attr = cp_parser_attributes_opt (parser);
25942 identifier = cp_parser_identifier (parser);
25944 params
25945 = chainon (params,
25946 objc_build_keyword_decl (selector,
25947 type_name,
25948 identifier,
25949 parm_attr));
25951 token = cp_lexer_peek_token (parser->lexer);
25954 if (params == NULL_TREE)
25956 cp_parser_error (parser, "objective-c++ method declaration is expected");
25957 return error_mark_node;
25960 /* We allow tail attributes for the method. */
25961 if (token->keyword == RID_ATTRIBUTE)
25963 *attributes = cp_parser_attributes_opt (parser);
25964 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25965 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25966 return params;
25967 cp_parser_error (parser,
25968 "method attributes must be specified at the end");
25969 return error_mark_node;
25972 if (params == NULL_TREE)
25974 cp_parser_error (parser, "objective-c++ method declaration is expected");
25975 return error_mark_node;
25977 return params;
25980 /* Parse the non-keyword Objective-C params. */
25982 static tree
25983 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25984 tree* attributes)
25986 tree params = make_node (TREE_LIST);
25987 cp_token *token = cp_lexer_peek_token (parser->lexer);
25988 *ellipsisp = false; /* Initially, assume no ellipsis. */
25990 while (token->type == CPP_COMMA)
25992 cp_parameter_declarator *parmdecl;
25993 tree parm;
25995 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25996 token = cp_lexer_peek_token (parser->lexer);
25998 if (token->type == CPP_ELLIPSIS)
26000 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26001 *ellipsisp = true;
26002 token = cp_lexer_peek_token (parser->lexer);
26003 break;
26006 /* TODO: parse attributes for tail parameters. */
26007 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26008 parm = grokdeclarator (parmdecl->declarator,
26009 &parmdecl->decl_specifiers,
26010 PARM, /*initialized=*/0,
26011 /*attrlist=*/NULL);
26013 chainon (params, build_tree_list (NULL_TREE, parm));
26014 token = cp_lexer_peek_token (parser->lexer);
26017 /* We allow tail attributes for the method. */
26018 if (token->keyword == RID_ATTRIBUTE)
26020 if (*attributes == NULL_TREE)
26022 *attributes = cp_parser_attributes_opt (parser);
26023 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26024 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26025 return params;
26027 else
26028 /* We have an error, but parse the attributes, so that we can
26029 carry on. */
26030 *attributes = cp_parser_attributes_opt (parser);
26032 cp_parser_error (parser,
26033 "method attributes must be specified at the end");
26034 return error_mark_node;
26037 return params;
26040 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26042 static void
26043 cp_parser_objc_interstitial_code (cp_parser* parser)
26045 cp_token *token = cp_lexer_peek_token (parser->lexer);
26047 /* If the next token is `extern' and the following token is a string
26048 literal, then we have a linkage specification. */
26049 if (token->keyword == RID_EXTERN
26050 && cp_parser_is_pure_string_literal
26051 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26052 cp_parser_linkage_specification (parser);
26053 /* Handle #pragma, if any. */
26054 else if (token->type == CPP_PRAGMA)
26055 cp_parser_pragma (parser, pragma_objc_icode);
26056 /* Allow stray semicolons. */
26057 else if (token->type == CPP_SEMICOLON)
26058 cp_lexer_consume_token (parser->lexer);
26059 /* Mark methods as optional or required, when building protocols. */
26060 else if (token->keyword == RID_AT_OPTIONAL)
26062 cp_lexer_consume_token (parser->lexer);
26063 objc_set_method_opt (true);
26065 else if (token->keyword == RID_AT_REQUIRED)
26067 cp_lexer_consume_token (parser->lexer);
26068 objc_set_method_opt (false);
26070 else if (token->keyword == RID_NAMESPACE)
26071 cp_parser_namespace_definition (parser);
26072 /* Other stray characters must generate errors. */
26073 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26075 cp_lexer_consume_token (parser->lexer);
26076 error ("stray %qs between Objective-C++ methods",
26077 token->type == CPP_OPEN_BRACE ? "{" : "}");
26079 /* Finally, try to parse a block-declaration, or a function-definition. */
26080 else
26081 cp_parser_block_declaration (parser, /*statement_p=*/false);
26084 /* Parse a method signature. */
26086 static tree
26087 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26089 tree rettype, kwdparms, optparms;
26090 bool ellipsis = false;
26091 bool is_class_method;
26093 is_class_method = cp_parser_objc_method_type (parser);
26094 rettype = cp_parser_objc_typename (parser);
26095 *attributes = NULL_TREE;
26096 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26097 if (kwdparms == error_mark_node)
26098 return error_mark_node;
26099 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26100 if (optparms == error_mark_node)
26101 return error_mark_node;
26103 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26106 static bool
26107 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26109 tree tattr;
26110 cp_lexer_save_tokens (parser->lexer);
26111 tattr = cp_parser_attributes_opt (parser);
26112 gcc_assert (tattr) ;
26114 /* If the attributes are followed by a method introducer, this is not allowed.
26115 Dump the attributes and flag the situation. */
26116 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26117 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26118 return true;
26120 /* Otherwise, the attributes introduce some interstitial code, possibly so
26121 rewind to allow that check. */
26122 cp_lexer_rollback_tokens (parser->lexer);
26123 return false;
26126 /* Parse an Objective-C method prototype list. */
26128 static void
26129 cp_parser_objc_method_prototype_list (cp_parser* parser)
26131 cp_token *token = cp_lexer_peek_token (parser->lexer);
26133 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26135 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26137 tree attributes, sig;
26138 bool is_class_method;
26139 if (token->type == CPP_PLUS)
26140 is_class_method = true;
26141 else
26142 is_class_method = false;
26143 sig = cp_parser_objc_method_signature (parser, &attributes);
26144 if (sig == error_mark_node)
26146 cp_parser_skip_to_end_of_block_or_statement (parser);
26147 token = cp_lexer_peek_token (parser->lexer);
26148 continue;
26150 objc_add_method_declaration (is_class_method, sig, attributes);
26151 cp_parser_consume_semicolon_at_end_of_statement (parser);
26153 else if (token->keyword == RID_AT_PROPERTY)
26154 cp_parser_objc_at_property_declaration (parser);
26155 else if (token->keyword == RID_ATTRIBUTE
26156 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26157 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26158 OPT_Wattributes,
26159 "prefix attributes are ignored for methods");
26160 else
26161 /* Allow for interspersed non-ObjC++ code. */
26162 cp_parser_objc_interstitial_code (parser);
26164 token = cp_lexer_peek_token (parser->lexer);
26167 if (token->type != CPP_EOF)
26168 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26169 else
26170 cp_parser_error (parser, "expected %<@end%>");
26172 objc_finish_interface ();
26175 /* Parse an Objective-C method definition list. */
26177 static void
26178 cp_parser_objc_method_definition_list (cp_parser* parser)
26180 cp_token *token = cp_lexer_peek_token (parser->lexer);
26182 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26184 tree meth;
26186 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26188 cp_token *ptk;
26189 tree sig, attribute;
26190 bool is_class_method;
26191 if (token->type == CPP_PLUS)
26192 is_class_method = true;
26193 else
26194 is_class_method = false;
26195 push_deferring_access_checks (dk_deferred);
26196 sig = cp_parser_objc_method_signature (parser, &attribute);
26197 if (sig == error_mark_node)
26199 cp_parser_skip_to_end_of_block_or_statement (parser);
26200 token = cp_lexer_peek_token (parser->lexer);
26201 continue;
26203 objc_start_method_definition (is_class_method, sig, attribute,
26204 NULL_TREE);
26206 /* For historical reasons, we accept an optional semicolon. */
26207 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26208 cp_lexer_consume_token (parser->lexer);
26210 ptk = cp_lexer_peek_token (parser->lexer);
26211 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26212 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26214 perform_deferred_access_checks (tf_warning_or_error);
26215 stop_deferring_access_checks ();
26216 meth = cp_parser_function_definition_after_declarator (parser,
26217 false);
26218 pop_deferring_access_checks ();
26219 objc_finish_method_definition (meth);
26222 /* The following case will be removed once @synthesize is
26223 completely implemented. */
26224 else if (token->keyword == RID_AT_PROPERTY)
26225 cp_parser_objc_at_property_declaration (parser);
26226 else if (token->keyword == RID_AT_SYNTHESIZE)
26227 cp_parser_objc_at_synthesize_declaration (parser);
26228 else if (token->keyword == RID_AT_DYNAMIC)
26229 cp_parser_objc_at_dynamic_declaration (parser);
26230 else if (token->keyword == RID_ATTRIBUTE
26231 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26232 warning_at (token->location, OPT_Wattributes,
26233 "prefix attributes are ignored for methods");
26234 else
26235 /* Allow for interspersed non-ObjC++ code. */
26236 cp_parser_objc_interstitial_code (parser);
26238 token = cp_lexer_peek_token (parser->lexer);
26241 if (token->type != CPP_EOF)
26242 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26243 else
26244 cp_parser_error (parser, "expected %<@end%>");
26246 objc_finish_implementation ();
26249 /* Parse Objective-C ivars. */
26251 static void
26252 cp_parser_objc_class_ivars (cp_parser* parser)
26254 cp_token *token = cp_lexer_peek_token (parser->lexer);
26256 if (token->type != CPP_OPEN_BRACE)
26257 return; /* No ivars specified. */
26259 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26260 token = cp_lexer_peek_token (parser->lexer);
26262 while (token->type != CPP_CLOSE_BRACE
26263 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26265 cp_decl_specifier_seq declspecs;
26266 int decl_class_or_enum_p;
26267 tree prefix_attributes;
26269 cp_parser_objc_visibility_spec (parser);
26271 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26272 break;
26274 cp_parser_decl_specifier_seq (parser,
26275 CP_PARSER_FLAGS_OPTIONAL,
26276 &declspecs,
26277 &decl_class_or_enum_p);
26279 /* auto, register, static, extern, mutable. */
26280 if (declspecs.storage_class != sc_none)
26282 cp_parser_error (parser, "invalid type for instance variable");
26283 declspecs.storage_class = sc_none;
26286 /* thread_local. */
26287 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26289 cp_parser_error (parser, "invalid type for instance variable");
26290 declspecs.locations[ds_thread] = 0;
26293 /* typedef. */
26294 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26296 cp_parser_error (parser, "invalid type for instance variable");
26297 declspecs.locations[ds_typedef] = 0;
26300 prefix_attributes = declspecs.attributes;
26301 declspecs.attributes = NULL_TREE;
26303 /* Keep going until we hit the `;' at the end of the
26304 declaration. */
26305 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26307 tree width = NULL_TREE, attributes, first_attribute, decl;
26308 cp_declarator *declarator = NULL;
26309 int ctor_dtor_or_conv_p;
26311 /* Check for a (possibly unnamed) bitfield declaration. */
26312 token = cp_lexer_peek_token (parser->lexer);
26313 if (token->type == CPP_COLON)
26314 goto eat_colon;
26316 if (token->type == CPP_NAME
26317 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26318 == CPP_COLON))
26320 /* Get the name of the bitfield. */
26321 declarator = make_id_declarator (NULL_TREE,
26322 cp_parser_identifier (parser),
26323 sfk_none);
26325 eat_colon:
26326 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26327 /* Get the width of the bitfield. */
26328 width
26329 = cp_parser_constant_expression (parser,
26330 /*allow_non_constant=*/false,
26331 NULL);
26333 else
26335 /* Parse the declarator. */
26336 declarator
26337 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26338 &ctor_dtor_or_conv_p,
26339 /*parenthesized_p=*/NULL,
26340 /*member_p=*/false,
26341 /*friend_p=*/false);
26344 /* Look for attributes that apply to the ivar. */
26345 attributes = cp_parser_attributes_opt (parser);
26346 /* Remember which attributes are prefix attributes and
26347 which are not. */
26348 first_attribute = attributes;
26349 /* Combine the attributes. */
26350 attributes = chainon (prefix_attributes, attributes);
26352 if (width)
26353 /* Create the bitfield declaration. */
26354 decl = grokbitfield (declarator, &declspecs,
26355 width,
26356 attributes);
26357 else
26358 decl = grokfield (declarator, &declspecs,
26359 NULL_TREE, /*init_const_expr_p=*/false,
26360 NULL_TREE, attributes);
26362 /* Add the instance variable. */
26363 if (decl != error_mark_node && decl != NULL_TREE)
26364 objc_add_instance_variable (decl);
26366 /* Reset PREFIX_ATTRIBUTES. */
26367 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26368 attributes = TREE_CHAIN (attributes);
26369 if (attributes)
26370 TREE_CHAIN (attributes) = NULL_TREE;
26372 token = cp_lexer_peek_token (parser->lexer);
26374 if (token->type == CPP_COMMA)
26376 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26377 continue;
26379 break;
26382 cp_parser_consume_semicolon_at_end_of_statement (parser);
26383 token = cp_lexer_peek_token (parser->lexer);
26386 if (token->keyword == RID_AT_END)
26387 cp_parser_error (parser, "expected %<}%>");
26389 /* Do not consume the RID_AT_END, so it will be read again as terminating
26390 the @interface of @implementation. */
26391 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26392 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26394 /* For historical reasons, we accept an optional semicolon. */
26395 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26396 cp_lexer_consume_token (parser->lexer);
26399 /* Parse an Objective-C protocol declaration. */
26401 static void
26402 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26404 tree proto, protorefs;
26405 cp_token *tok;
26407 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26410 tok = cp_lexer_peek_token (parser->lexer);
26411 error_at (tok->location, "identifier expected after %<@protocol%>");
26412 cp_parser_consume_semicolon_at_end_of_statement (parser);
26413 return;
26416 /* See if we have a forward declaration or a definition. */
26417 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26419 /* Try a forward declaration first. */
26420 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26422 while (true)
26424 tree id;
26426 id = cp_parser_identifier (parser);
26427 if (id == error_mark_node)
26428 break;
26430 objc_declare_protocol (id, attributes);
26432 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26433 cp_lexer_consume_token (parser->lexer);
26434 else
26435 break;
26437 cp_parser_consume_semicolon_at_end_of_statement (parser);
26440 /* Ok, we got a full-fledged definition (or at least should). */
26441 else
26443 proto = cp_parser_identifier (parser);
26444 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26445 objc_start_protocol (proto, protorefs, attributes);
26446 cp_parser_objc_method_prototype_list (parser);
26450 /* Parse an Objective-C superclass or category. */
26452 static void
26453 cp_parser_objc_superclass_or_category (cp_parser *parser,
26454 bool iface_p,
26455 tree *super,
26456 tree *categ, bool *is_class_extension)
26458 cp_token *next = cp_lexer_peek_token (parser->lexer);
26460 *super = *categ = NULL_TREE;
26461 *is_class_extension = false;
26462 if (next->type == CPP_COLON)
26464 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26465 *super = cp_parser_identifier (parser);
26467 else if (next->type == CPP_OPEN_PAREN)
26469 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26471 /* If there is no category name, and this is an @interface, we
26472 have a class extension. */
26473 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26475 *categ = NULL_TREE;
26476 *is_class_extension = true;
26478 else
26479 *categ = cp_parser_identifier (parser);
26481 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26485 /* Parse an Objective-C class interface. */
26487 static void
26488 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26490 tree name, super, categ, protos;
26491 bool is_class_extension;
26493 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26494 name = cp_parser_identifier (parser);
26495 if (name == error_mark_node)
26497 /* It's hard to recover because even if valid @interface stuff
26498 is to follow, we can't compile it (or validate it) if we
26499 don't even know which class it refers to. Let's assume this
26500 was a stray '@interface' token in the stream and skip it.
26502 return;
26504 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26505 &is_class_extension);
26506 protos = cp_parser_objc_protocol_refs_opt (parser);
26508 /* We have either a class or a category on our hands. */
26509 if (categ || is_class_extension)
26510 objc_start_category_interface (name, categ, protos, attributes);
26511 else
26513 objc_start_class_interface (name, super, protos, attributes);
26514 /* Handle instance variable declarations, if any. */
26515 cp_parser_objc_class_ivars (parser);
26516 objc_continue_interface ();
26519 cp_parser_objc_method_prototype_list (parser);
26522 /* Parse an Objective-C class implementation. */
26524 static void
26525 cp_parser_objc_class_implementation (cp_parser* parser)
26527 tree name, super, categ;
26528 bool is_class_extension;
26530 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26531 name = cp_parser_identifier (parser);
26532 if (name == error_mark_node)
26534 /* It's hard to recover because even if valid @implementation
26535 stuff is to follow, we can't compile it (or validate it) if
26536 we don't even know which class it refers to. Let's assume
26537 this was a stray '@implementation' token in the stream and
26538 skip it.
26540 return;
26542 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26543 &is_class_extension);
26545 /* We have either a class or a category on our hands. */
26546 if (categ)
26547 objc_start_category_implementation (name, categ);
26548 else
26550 objc_start_class_implementation (name, super);
26551 /* Handle instance variable declarations, if any. */
26552 cp_parser_objc_class_ivars (parser);
26553 objc_continue_implementation ();
26556 cp_parser_objc_method_definition_list (parser);
26559 /* Consume the @end token and finish off the implementation. */
26561 static void
26562 cp_parser_objc_end_implementation (cp_parser* parser)
26564 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26565 objc_finish_implementation ();
26568 /* Parse an Objective-C declaration. */
26570 static void
26571 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26573 /* Try to figure out what kind of declaration is present. */
26574 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26576 if (attributes)
26577 switch (kwd->keyword)
26579 case RID_AT_ALIAS:
26580 case RID_AT_CLASS:
26581 case RID_AT_END:
26582 error_at (kwd->location, "attributes may not be specified before"
26583 " the %<@%D%> Objective-C++ keyword",
26584 kwd->u.value);
26585 attributes = NULL;
26586 break;
26587 case RID_AT_IMPLEMENTATION:
26588 warning_at (kwd->location, OPT_Wattributes,
26589 "prefix attributes are ignored before %<@%D%>",
26590 kwd->u.value);
26591 attributes = NULL;
26592 default:
26593 break;
26596 switch (kwd->keyword)
26598 case RID_AT_ALIAS:
26599 cp_parser_objc_alias_declaration (parser);
26600 break;
26601 case RID_AT_CLASS:
26602 cp_parser_objc_class_declaration (parser);
26603 break;
26604 case RID_AT_PROTOCOL:
26605 cp_parser_objc_protocol_declaration (parser, attributes);
26606 break;
26607 case RID_AT_INTERFACE:
26608 cp_parser_objc_class_interface (parser, attributes);
26609 break;
26610 case RID_AT_IMPLEMENTATION:
26611 cp_parser_objc_class_implementation (parser);
26612 break;
26613 case RID_AT_END:
26614 cp_parser_objc_end_implementation (parser);
26615 break;
26616 default:
26617 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26618 kwd->u.value);
26619 cp_parser_skip_to_end_of_block_or_statement (parser);
26623 /* Parse an Objective-C try-catch-finally statement.
26625 objc-try-catch-finally-stmt:
26626 @try compound-statement objc-catch-clause-seq [opt]
26627 objc-finally-clause [opt]
26629 objc-catch-clause-seq:
26630 objc-catch-clause objc-catch-clause-seq [opt]
26632 objc-catch-clause:
26633 @catch ( objc-exception-declaration ) compound-statement
26635 objc-finally-clause:
26636 @finally compound-statement
26638 objc-exception-declaration:
26639 parameter-declaration
26640 '...'
26642 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26644 Returns NULL_TREE.
26646 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26647 for C. Keep them in sync. */
26649 static tree
26650 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26652 location_t location;
26653 tree stmt;
26655 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26656 location = cp_lexer_peek_token (parser->lexer)->location;
26657 objc_maybe_warn_exceptions (location);
26658 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26659 node, lest it get absorbed into the surrounding block. */
26660 stmt = push_stmt_list ();
26661 cp_parser_compound_statement (parser, NULL, false, false);
26662 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26664 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26666 cp_parameter_declarator *parm;
26667 tree parameter_declaration = error_mark_node;
26668 bool seen_open_paren = false;
26670 cp_lexer_consume_token (parser->lexer);
26671 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26672 seen_open_paren = true;
26673 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26675 /* We have "@catch (...)" (where the '...' are literally
26676 what is in the code). Skip the '...'.
26677 parameter_declaration is set to NULL_TREE, and
26678 objc_being_catch_clauses() knows that that means
26679 '...'. */
26680 cp_lexer_consume_token (parser->lexer);
26681 parameter_declaration = NULL_TREE;
26683 else
26685 /* We have "@catch (NSException *exception)" or something
26686 like that. Parse the parameter declaration. */
26687 parm = cp_parser_parameter_declaration (parser, false, NULL);
26688 if (parm == NULL)
26689 parameter_declaration = error_mark_node;
26690 else
26691 parameter_declaration = grokdeclarator (parm->declarator,
26692 &parm->decl_specifiers,
26693 PARM, /*initialized=*/0,
26694 /*attrlist=*/NULL);
26696 if (seen_open_paren)
26697 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26698 else
26700 /* If there was no open parenthesis, we are recovering from
26701 an error, and we are trying to figure out what mistake
26702 the user has made. */
26704 /* If there is an immediate closing parenthesis, the user
26705 probably forgot the opening one (ie, they typed "@catch
26706 NSException *e)". Parse the closing parenthesis and keep
26707 going. */
26708 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26709 cp_lexer_consume_token (parser->lexer);
26711 /* If these is no immediate closing parenthesis, the user
26712 probably doesn't know that parenthesis are required at
26713 all (ie, they typed "@catch NSException *e"). So, just
26714 forget about the closing parenthesis and keep going. */
26716 objc_begin_catch_clause (parameter_declaration);
26717 cp_parser_compound_statement (parser, NULL, false, false);
26718 objc_finish_catch_clause ();
26720 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26722 cp_lexer_consume_token (parser->lexer);
26723 location = cp_lexer_peek_token (parser->lexer)->location;
26724 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26725 node, lest it get absorbed into the surrounding block. */
26726 stmt = push_stmt_list ();
26727 cp_parser_compound_statement (parser, NULL, false, false);
26728 objc_build_finally_clause (location, pop_stmt_list (stmt));
26731 return objc_finish_try_stmt ();
26734 /* Parse an Objective-C synchronized statement.
26736 objc-synchronized-stmt:
26737 @synchronized ( expression ) compound-statement
26739 Returns NULL_TREE. */
26741 static tree
26742 cp_parser_objc_synchronized_statement (cp_parser *parser)
26744 location_t location;
26745 tree lock, stmt;
26747 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26749 location = cp_lexer_peek_token (parser->lexer)->location;
26750 objc_maybe_warn_exceptions (location);
26751 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26752 lock = cp_parser_expression (parser);
26753 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26755 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26756 node, lest it get absorbed into the surrounding block. */
26757 stmt = push_stmt_list ();
26758 cp_parser_compound_statement (parser, NULL, false, false);
26760 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26763 /* Parse an Objective-C throw statement.
26765 objc-throw-stmt:
26766 @throw assignment-expression [opt] ;
26768 Returns a constructed '@throw' statement. */
26770 static tree
26771 cp_parser_objc_throw_statement (cp_parser *parser)
26773 tree expr = NULL_TREE;
26774 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26776 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26779 expr = cp_parser_expression (parser);
26781 cp_parser_consume_semicolon_at_end_of_statement (parser);
26783 return objc_build_throw_stmt (loc, expr);
26786 /* Parse an Objective-C statement. */
26788 static tree
26789 cp_parser_objc_statement (cp_parser * parser)
26791 /* Try to figure out what kind of declaration is present. */
26792 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26794 switch (kwd->keyword)
26796 case RID_AT_TRY:
26797 return cp_parser_objc_try_catch_finally_statement (parser);
26798 case RID_AT_SYNCHRONIZED:
26799 return cp_parser_objc_synchronized_statement (parser);
26800 case RID_AT_THROW:
26801 return cp_parser_objc_throw_statement (parser);
26802 default:
26803 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26804 kwd->u.value);
26805 cp_parser_skip_to_end_of_block_or_statement (parser);
26808 return error_mark_node;
26811 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26812 look ahead to see if an objc keyword follows the attributes. This
26813 is to detect the use of prefix attributes on ObjC @interface and
26814 @protocol. */
26816 static bool
26817 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26819 cp_lexer_save_tokens (parser->lexer);
26820 *attrib = cp_parser_attributes_opt (parser);
26821 gcc_assert (*attrib);
26822 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26824 cp_lexer_commit_tokens (parser->lexer);
26825 return true;
26827 cp_lexer_rollback_tokens (parser->lexer);
26828 return false;
26831 /* This routine is a minimal replacement for
26832 c_parser_struct_declaration () used when parsing the list of
26833 types/names or ObjC++ properties. For example, when parsing the
26834 code
26836 @property (readonly) int a, b, c;
26838 this function is responsible for parsing "int a, int b, int c" and
26839 returning the declarations as CHAIN of DECLs.
26841 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26842 similar parsing. */
26843 static tree
26844 cp_parser_objc_struct_declaration (cp_parser *parser)
26846 tree decls = NULL_TREE;
26847 cp_decl_specifier_seq declspecs;
26848 int decl_class_or_enum_p;
26849 tree prefix_attributes;
26851 cp_parser_decl_specifier_seq (parser,
26852 CP_PARSER_FLAGS_NONE,
26853 &declspecs,
26854 &decl_class_or_enum_p);
26856 if (declspecs.type == error_mark_node)
26857 return error_mark_node;
26859 /* auto, register, static, extern, mutable. */
26860 if (declspecs.storage_class != sc_none)
26862 cp_parser_error (parser, "invalid type for property");
26863 declspecs.storage_class = sc_none;
26866 /* thread_local. */
26867 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26869 cp_parser_error (parser, "invalid type for property");
26870 declspecs.locations[ds_thread] = 0;
26873 /* typedef. */
26874 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26876 cp_parser_error (parser, "invalid type for property");
26877 declspecs.locations[ds_typedef] = 0;
26880 prefix_attributes = declspecs.attributes;
26881 declspecs.attributes = NULL_TREE;
26883 /* Keep going until we hit the `;' at the end of the declaration. */
26884 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26886 tree attributes, first_attribute, decl;
26887 cp_declarator *declarator;
26888 cp_token *token;
26890 /* Parse the declarator. */
26891 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26892 NULL, NULL, false, false);
26894 /* Look for attributes that apply to the ivar. */
26895 attributes = cp_parser_attributes_opt (parser);
26896 /* Remember which attributes are prefix attributes and
26897 which are not. */
26898 first_attribute = attributes;
26899 /* Combine the attributes. */
26900 attributes = chainon (prefix_attributes, attributes);
26902 decl = grokfield (declarator, &declspecs,
26903 NULL_TREE, /*init_const_expr_p=*/false,
26904 NULL_TREE, attributes);
26906 if (decl == error_mark_node || decl == NULL_TREE)
26907 return error_mark_node;
26909 /* Reset PREFIX_ATTRIBUTES. */
26910 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26911 attributes = TREE_CHAIN (attributes);
26912 if (attributes)
26913 TREE_CHAIN (attributes) = NULL_TREE;
26915 DECL_CHAIN (decl) = decls;
26916 decls = decl;
26918 token = cp_lexer_peek_token (parser->lexer);
26919 if (token->type == CPP_COMMA)
26921 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26922 continue;
26924 else
26925 break;
26927 return decls;
26930 /* Parse an Objective-C @property declaration. The syntax is:
26932 objc-property-declaration:
26933 '@property' objc-property-attributes[opt] struct-declaration ;
26935 objc-property-attributes:
26936 '(' objc-property-attribute-list ')'
26938 objc-property-attribute-list:
26939 objc-property-attribute
26940 objc-property-attribute-list, objc-property-attribute
26942 objc-property-attribute
26943 'getter' = identifier
26944 'setter' = identifier
26945 'readonly'
26946 'readwrite'
26947 'assign'
26948 'retain'
26949 'copy'
26950 'nonatomic'
26952 For example:
26953 @property NSString *name;
26954 @property (readonly) id object;
26955 @property (retain, nonatomic, getter=getTheName) id name;
26956 @property int a, b, c;
26958 PS: This function is identical to
26959 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26960 static void
26961 cp_parser_objc_at_property_declaration (cp_parser *parser)
26963 /* The following variables hold the attributes of the properties as
26964 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26965 seen. When we see an attribute, we set them to 'true' (if they
26966 are boolean properties) or to the identifier (if they have an
26967 argument, ie, for getter and setter). Note that here we only
26968 parse the list of attributes, check the syntax and accumulate the
26969 attributes that we find. objc_add_property_declaration() will
26970 then process the information. */
26971 bool property_assign = false;
26972 bool property_copy = false;
26973 tree property_getter_ident = NULL_TREE;
26974 bool property_nonatomic = false;
26975 bool property_readonly = false;
26976 bool property_readwrite = false;
26977 bool property_retain = false;
26978 tree property_setter_ident = NULL_TREE;
26980 /* 'properties' is the list of properties that we read. Usually a
26981 single one, but maybe more (eg, in "@property int a, b, c;" there
26982 are three). */
26983 tree properties;
26984 location_t loc;
26986 loc = cp_lexer_peek_token (parser->lexer)->location;
26988 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26990 /* Parse the optional attribute list... */
26991 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26993 /* Eat the '('. */
26994 cp_lexer_consume_token (parser->lexer);
26996 while (true)
26998 bool syntax_error = false;
26999 cp_token *token = cp_lexer_peek_token (parser->lexer);
27000 enum rid keyword;
27002 if (token->type != CPP_NAME)
27004 cp_parser_error (parser, "expected identifier");
27005 break;
27007 keyword = C_RID_CODE (token->u.value);
27008 cp_lexer_consume_token (parser->lexer);
27009 switch (keyword)
27011 case RID_ASSIGN: property_assign = true; break;
27012 case RID_COPY: property_copy = true; break;
27013 case RID_NONATOMIC: property_nonatomic = true; break;
27014 case RID_READONLY: property_readonly = true; break;
27015 case RID_READWRITE: property_readwrite = true; break;
27016 case RID_RETAIN: property_retain = true; break;
27018 case RID_GETTER:
27019 case RID_SETTER:
27020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27022 if (keyword == RID_GETTER)
27023 cp_parser_error (parser,
27024 "missing %<=%> (after %<getter%> attribute)");
27025 else
27026 cp_parser_error (parser,
27027 "missing %<=%> (after %<setter%> attribute)");
27028 syntax_error = true;
27029 break;
27031 cp_lexer_consume_token (parser->lexer); /* eat the = */
27032 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27034 cp_parser_error (parser, "expected identifier");
27035 syntax_error = true;
27036 break;
27038 if (keyword == RID_SETTER)
27040 if (property_setter_ident != NULL_TREE)
27042 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27043 cp_lexer_consume_token (parser->lexer);
27045 else
27046 property_setter_ident = cp_parser_objc_selector (parser);
27047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27048 cp_parser_error (parser, "setter name must terminate with %<:%>");
27049 else
27050 cp_lexer_consume_token (parser->lexer);
27052 else
27054 if (property_getter_ident != NULL_TREE)
27056 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27057 cp_lexer_consume_token (parser->lexer);
27059 else
27060 property_getter_ident = cp_parser_objc_selector (parser);
27062 break;
27063 default:
27064 cp_parser_error (parser, "unknown property attribute");
27065 syntax_error = true;
27066 break;
27069 if (syntax_error)
27070 break;
27072 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27073 cp_lexer_consume_token (parser->lexer);
27074 else
27075 break;
27078 /* FIXME: "@property (setter, assign);" will generate a spurious
27079 "error: expected ‘)’ before ‘,’ token". This is because
27080 cp_parser_require, unlike the C counterpart, will produce an
27081 error even if we are in error recovery. */
27082 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27084 cp_parser_skip_to_closing_parenthesis (parser,
27085 /*recovering=*/true,
27086 /*or_comma=*/false,
27087 /*consume_paren=*/true);
27091 /* ... and the property declaration(s). */
27092 properties = cp_parser_objc_struct_declaration (parser);
27094 if (properties == error_mark_node)
27096 cp_parser_skip_to_end_of_statement (parser);
27097 /* If the next token is now a `;', consume it. */
27098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27099 cp_lexer_consume_token (parser->lexer);
27100 return;
27103 if (properties == NULL_TREE)
27104 cp_parser_error (parser, "expected identifier");
27105 else
27107 /* Comma-separated properties are chained together in
27108 reverse order; add them one by one. */
27109 properties = nreverse (properties);
27111 for (; properties; properties = TREE_CHAIN (properties))
27112 objc_add_property_declaration (loc, copy_node (properties),
27113 property_readonly, property_readwrite,
27114 property_assign, property_retain,
27115 property_copy, property_nonatomic,
27116 property_getter_ident, property_setter_ident);
27119 cp_parser_consume_semicolon_at_end_of_statement (parser);
27122 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27124 objc-synthesize-declaration:
27125 @synthesize objc-synthesize-identifier-list ;
27127 objc-synthesize-identifier-list:
27128 objc-synthesize-identifier
27129 objc-synthesize-identifier-list, objc-synthesize-identifier
27131 objc-synthesize-identifier
27132 identifier
27133 identifier = identifier
27135 For example:
27136 @synthesize MyProperty;
27137 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27139 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27140 for C. Keep them in sync.
27142 static void
27143 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27145 tree list = NULL_TREE;
27146 location_t loc;
27147 loc = cp_lexer_peek_token (parser->lexer)->location;
27149 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27150 while (true)
27152 tree property, ivar;
27153 property = cp_parser_identifier (parser);
27154 if (property == error_mark_node)
27156 cp_parser_consume_semicolon_at_end_of_statement (parser);
27157 return;
27159 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27161 cp_lexer_consume_token (parser->lexer);
27162 ivar = cp_parser_identifier (parser);
27163 if (ivar == error_mark_node)
27165 cp_parser_consume_semicolon_at_end_of_statement (parser);
27166 return;
27169 else
27170 ivar = NULL_TREE;
27171 list = chainon (list, build_tree_list (ivar, property));
27172 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27173 cp_lexer_consume_token (parser->lexer);
27174 else
27175 break;
27177 cp_parser_consume_semicolon_at_end_of_statement (parser);
27178 objc_add_synthesize_declaration (loc, list);
27181 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27183 objc-dynamic-declaration:
27184 @dynamic identifier-list ;
27186 For example:
27187 @dynamic MyProperty;
27188 @dynamic MyProperty, AnotherProperty;
27190 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27191 for C. Keep them in sync.
27193 static void
27194 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27196 tree list = NULL_TREE;
27197 location_t loc;
27198 loc = cp_lexer_peek_token (parser->lexer)->location;
27200 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27201 while (true)
27203 tree property;
27204 property = cp_parser_identifier (parser);
27205 if (property == error_mark_node)
27207 cp_parser_consume_semicolon_at_end_of_statement (parser);
27208 return;
27210 list = chainon (list, build_tree_list (NULL, property));
27211 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27212 cp_lexer_consume_token (parser->lexer);
27213 else
27214 break;
27216 cp_parser_consume_semicolon_at_end_of_statement (parser);
27217 objc_add_dynamic_declaration (loc, list);
27221 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27223 /* Returns name of the next clause.
27224 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27225 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27226 returned and the token is consumed. */
27228 static pragma_omp_clause
27229 cp_parser_omp_clause_name (cp_parser *parser)
27231 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27233 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27234 result = PRAGMA_OMP_CLAUSE_IF;
27235 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27236 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27237 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27238 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27239 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27240 result = PRAGMA_OMP_CLAUSE_FOR;
27241 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27243 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27244 const char *p = IDENTIFIER_POINTER (id);
27246 switch (p[0])
27248 case 'a':
27249 if (!strcmp ("aligned", p))
27250 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27251 break;
27252 case 'c':
27253 if (!strcmp ("collapse", p))
27254 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27255 else if (!strcmp ("copyin", p))
27256 result = PRAGMA_OMP_CLAUSE_COPYIN;
27257 else if (!strcmp ("copyprivate", p))
27258 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27259 break;
27260 case 'd':
27261 if (!strcmp ("depend", p))
27262 result = PRAGMA_OMP_CLAUSE_DEPEND;
27263 else if (!strcmp ("device", p))
27264 result = PRAGMA_OMP_CLAUSE_DEVICE;
27265 else if (!strcmp ("dist_schedule", p))
27266 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27267 break;
27268 case 'f':
27269 if (!strcmp ("final", p))
27270 result = PRAGMA_OMP_CLAUSE_FINAL;
27271 else if (!strcmp ("firstprivate", p))
27272 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27273 else if (!strcmp ("from", p))
27274 result = PRAGMA_OMP_CLAUSE_FROM;
27275 break;
27276 case 'i':
27277 if (!strcmp ("inbranch", p))
27278 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27279 break;
27280 case 'l':
27281 if (!strcmp ("lastprivate", p))
27282 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27283 else if (!strcmp ("linear", p))
27284 result = PRAGMA_OMP_CLAUSE_LINEAR;
27285 break;
27286 case 'm':
27287 if (!strcmp ("map", p))
27288 result = PRAGMA_OMP_CLAUSE_MAP;
27289 else if (!strcmp ("mergeable", p))
27290 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27291 else if (flag_cilkplus && !strcmp ("mask", p))
27292 result = PRAGMA_CILK_CLAUSE_MASK;
27293 break;
27294 case 'n':
27295 if (!strcmp ("notinbranch", p))
27296 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27297 else if (!strcmp ("nowait", p))
27298 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27299 else if (flag_cilkplus && !strcmp ("nomask", p))
27300 result = PRAGMA_CILK_CLAUSE_NOMASK;
27301 else if (!strcmp ("num_teams", p))
27302 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27303 else if (!strcmp ("num_threads", p))
27304 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27305 break;
27306 case 'o':
27307 if (!strcmp ("ordered", p))
27308 result = PRAGMA_OMP_CLAUSE_ORDERED;
27309 break;
27310 case 'p':
27311 if (!strcmp ("parallel", p))
27312 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27313 else if (!strcmp ("proc_bind", p))
27314 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27315 break;
27316 case 'r':
27317 if (!strcmp ("reduction", p))
27318 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27319 break;
27320 case 's':
27321 if (!strcmp ("safelen", p))
27322 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27323 else if (!strcmp ("schedule", p))
27324 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27325 else if (!strcmp ("sections", p))
27326 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27327 else if (!strcmp ("shared", p))
27328 result = PRAGMA_OMP_CLAUSE_SHARED;
27329 else if (!strcmp ("simdlen", p))
27330 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27331 break;
27332 case 't':
27333 if (!strcmp ("taskgroup", p))
27334 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27335 else if (!strcmp ("thread_limit", p))
27336 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27337 else if (!strcmp ("to", p))
27338 result = PRAGMA_OMP_CLAUSE_TO;
27339 break;
27340 case 'u':
27341 if (!strcmp ("uniform", p))
27342 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27343 else if (!strcmp ("untied", p))
27344 result = PRAGMA_OMP_CLAUSE_UNTIED;
27345 break;
27346 case 'v':
27347 if (flag_cilkplus && !strcmp ("vectorlength", p))
27348 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27349 break;
27353 if (result != PRAGMA_OMP_CLAUSE_NONE)
27354 cp_lexer_consume_token (parser->lexer);
27356 return result;
27359 /* Validate that a clause of the given type does not already exist. */
27361 static void
27362 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27363 const char *name, location_t location)
27365 tree c;
27367 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27368 if (OMP_CLAUSE_CODE (c) == code)
27370 error_at (location, "too many %qs clauses", name);
27371 break;
27375 /* OpenMP 2.5:
27376 variable-list:
27377 identifier
27378 variable-list , identifier
27380 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27381 colon). An opening parenthesis will have been consumed by the caller.
27383 If KIND is nonzero, create the appropriate node and install the decl
27384 in OMP_CLAUSE_DECL and add the node to the head of the list.
27386 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27387 return the list created.
27389 COLON can be NULL if only closing parenthesis should end the list,
27390 or pointer to bool which will receive false if the list is terminated
27391 by closing parenthesis or true if the list is terminated by colon. */
27393 static tree
27394 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27395 tree list, bool *colon)
27397 cp_token *token;
27398 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27399 if (colon)
27401 parser->colon_corrects_to_scope_p = false;
27402 *colon = false;
27404 while (1)
27406 tree name, decl;
27408 token = cp_lexer_peek_token (parser->lexer);
27409 name = cp_parser_id_expression (parser, /*template_p=*/false,
27410 /*check_dependency_p=*/true,
27411 /*template_p=*/NULL,
27412 /*declarator_p=*/false,
27413 /*optional_p=*/false);
27414 if (name == error_mark_node)
27415 goto skip_comma;
27417 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27418 if (decl == error_mark_node)
27419 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27420 token->location);
27421 else if (kind != 0)
27423 switch (kind)
27425 case OMP_CLAUSE_MAP:
27426 case OMP_CLAUSE_FROM:
27427 case OMP_CLAUSE_TO:
27428 case OMP_CLAUSE_DEPEND:
27429 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27431 tree low_bound = NULL_TREE, length = NULL_TREE;
27433 parser->colon_corrects_to_scope_p = false;
27434 cp_lexer_consume_token (parser->lexer);
27435 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27436 low_bound = cp_parser_expression (parser);
27437 if (!colon)
27438 parser->colon_corrects_to_scope_p
27439 = saved_colon_corrects_to_scope_p;
27440 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27441 length = integer_one_node;
27442 else
27444 /* Look for `:'. */
27445 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27446 goto skip_comma;
27447 if (!cp_lexer_next_token_is (parser->lexer,
27448 CPP_CLOSE_SQUARE))
27449 length = cp_parser_expression (parser);
27451 /* Look for the closing `]'. */
27452 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27453 RT_CLOSE_SQUARE))
27454 goto skip_comma;
27455 decl = tree_cons (low_bound, length, decl);
27457 break;
27458 default:
27459 break;
27462 tree u = build_omp_clause (token->location, kind);
27463 OMP_CLAUSE_DECL (u) = decl;
27464 OMP_CLAUSE_CHAIN (u) = list;
27465 list = u;
27467 else
27468 list = tree_cons (decl, NULL_TREE, list);
27470 get_comma:
27471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27472 break;
27473 cp_lexer_consume_token (parser->lexer);
27476 if (colon)
27477 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27479 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27481 *colon = true;
27482 cp_parser_require (parser, CPP_COLON, RT_COLON);
27483 return list;
27486 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27488 int ending;
27490 /* Try to resync to an unnested comma. Copied from
27491 cp_parser_parenthesized_expression_list. */
27492 skip_comma:
27493 if (colon)
27494 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27495 ending = cp_parser_skip_to_closing_parenthesis (parser,
27496 /*recovering=*/true,
27497 /*or_comma=*/true,
27498 /*consume_paren=*/true);
27499 if (ending < 0)
27500 goto get_comma;
27503 return list;
27506 /* Similarly, but expect leading and trailing parenthesis. This is a very
27507 common case for omp clauses. */
27509 static tree
27510 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27512 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27513 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27514 return list;
27517 /* OpenMP 3.0:
27518 collapse ( constant-expression ) */
27520 static tree
27521 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27523 tree c, num;
27524 location_t loc;
27525 HOST_WIDE_INT n;
27527 loc = cp_lexer_peek_token (parser->lexer)->location;
27528 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27529 return list;
27531 num = cp_parser_constant_expression (parser, false, NULL);
27533 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27534 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27535 /*or_comma=*/false,
27536 /*consume_paren=*/true);
27538 if (num == error_mark_node)
27539 return list;
27540 num = fold_non_dependent_expr (num);
27541 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27542 || !tree_fits_shwi_p (num)
27543 || (n = tree_to_shwi (num)) <= 0
27544 || (int) n != n)
27546 error_at (loc, "collapse argument needs positive constant integer expression");
27547 return list;
27550 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27551 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27552 OMP_CLAUSE_CHAIN (c) = list;
27553 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27555 return c;
27558 /* OpenMP 2.5:
27559 default ( shared | none ) */
27561 static tree
27562 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27564 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27565 tree c;
27567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27568 return list;
27569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27571 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27572 const char *p = IDENTIFIER_POINTER (id);
27574 switch (p[0])
27576 case 'n':
27577 if (strcmp ("none", p) != 0)
27578 goto invalid_kind;
27579 kind = OMP_CLAUSE_DEFAULT_NONE;
27580 break;
27582 case 's':
27583 if (strcmp ("shared", p) != 0)
27584 goto invalid_kind;
27585 kind = OMP_CLAUSE_DEFAULT_SHARED;
27586 break;
27588 default:
27589 goto invalid_kind;
27592 cp_lexer_consume_token (parser->lexer);
27594 else
27596 invalid_kind:
27597 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27600 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27602 /*or_comma=*/false,
27603 /*consume_paren=*/true);
27605 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27606 return list;
27608 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27609 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27610 OMP_CLAUSE_CHAIN (c) = list;
27611 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27613 return c;
27616 /* OpenMP 3.1:
27617 final ( expression ) */
27619 static tree
27620 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27622 tree t, c;
27624 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27625 return list;
27627 t = cp_parser_condition (parser);
27629 if (t == error_mark_node
27630 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27631 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27632 /*or_comma=*/false,
27633 /*consume_paren=*/true);
27635 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27637 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27638 OMP_CLAUSE_FINAL_EXPR (c) = t;
27639 OMP_CLAUSE_CHAIN (c) = list;
27641 return c;
27644 /* OpenMP 2.5:
27645 if ( expression ) */
27647 static tree
27648 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27650 tree t, c;
27652 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27653 return list;
27655 t = cp_parser_condition (parser);
27657 if (t == error_mark_node
27658 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27659 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27660 /*or_comma=*/false,
27661 /*consume_paren=*/true);
27663 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27665 c = build_omp_clause (location, OMP_CLAUSE_IF);
27666 OMP_CLAUSE_IF_EXPR (c) = t;
27667 OMP_CLAUSE_CHAIN (c) = list;
27669 return c;
27672 /* OpenMP 3.1:
27673 mergeable */
27675 static tree
27676 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27677 tree list, location_t location)
27679 tree c;
27681 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27682 location);
27684 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27685 OMP_CLAUSE_CHAIN (c) = list;
27686 return c;
27689 /* OpenMP 2.5:
27690 nowait */
27692 static tree
27693 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27694 tree list, location_t location)
27696 tree c;
27698 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27700 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27701 OMP_CLAUSE_CHAIN (c) = list;
27702 return c;
27705 /* OpenMP 2.5:
27706 num_threads ( expression ) */
27708 static tree
27709 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27710 location_t location)
27712 tree t, c;
27714 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27715 return list;
27717 t = cp_parser_expression (parser);
27719 if (t == error_mark_node
27720 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27722 /*or_comma=*/false,
27723 /*consume_paren=*/true);
27725 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27726 "num_threads", location);
27728 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27729 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27730 OMP_CLAUSE_CHAIN (c) = list;
27732 return c;
27735 /* OpenMP 2.5:
27736 ordered */
27738 static tree
27739 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27740 tree list, location_t location)
27742 tree c;
27744 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27745 "ordered", location);
27747 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27748 OMP_CLAUSE_CHAIN (c) = list;
27749 return c;
27752 /* OpenMP 2.5:
27753 reduction ( reduction-operator : variable-list )
27755 reduction-operator:
27756 One of: + * - & ^ | && ||
27758 OpenMP 3.1:
27760 reduction-operator:
27761 One of: + * - & ^ | && || min max
27763 OpenMP 4.0:
27765 reduction-operator:
27766 One of: + * - & ^ | && ||
27767 id-expression */
27769 static tree
27770 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27772 enum tree_code code = ERROR_MARK;
27773 tree nlist, c, id = NULL_TREE;
27775 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27776 return list;
27778 switch (cp_lexer_peek_token (parser->lexer)->type)
27780 case CPP_PLUS: code = PLUS_EXPR; break;
27781 case CPP_MULT: code = MULT_EXPR; break;
27782 case CPP_MINUS: code = MINUS_EXPR; break;
27783 case CPP_AND: code = BIT_AND_EXPR; break;
27784 case CPP_XOR: code = BIT_XOR_EXPR; break;
27785 case CPP_OR: code = BIT_IOR_EXPR; break;
27786 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27787 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27788 default: break;
27791 if (code != ERROR_MARK)
27792 cp_lexer_consume_token (parser->lexer);
27793 else
27795 bool saved_colon_corrects_to_scope_p;
27796 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27797 parser->colon_corrects_to_scope_p = false;
27798 id = cp_parser_id_expression (parser, /*template_p=*/false,
27799 /*check_dependency_p=*/true,
27800 /*template_p=*/NULL,
27801 /*declarator_p=*/false,
27802 /*optional_p=*/false);
27803 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27804 if (identifier_p (id))
27806 const char *p = IDENTIFIER_POINTER (id);
27808 if (strcmp (p, "min") == 0)
27809 code = MIN_EXPR;
27810 else if (strcmp (p, "max") == 0)
27811 code = MAX_EXPR;
27812 else if (id == ansi_opname (PLUS_EXPR))
27813 code = PLUS_EXPR;
27814 else if (id == ansi_opname (MULT_EXPR))
27815 code = MULT_EXPR;
27816 else if (id == ansi_opname (MINUS_EXPR))
27817 code = MINUS_EXPR;
27818 else if (id == ansi_opname (BIT_AND_EXPR))
27819 code = BIT_AND_EXPR;
27820 else if (id == ansi_opname (BIT_IOR_EXPR))
27821 code = BIT_IOR_EXPR;
27822 else if (id == ansi_opname (BIT_XOR_EXPR))
27823 code = BIT_XOR_EXPR;
27824 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27825 code = TRUTH_ANDIF_EXPR;
27826 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27827 code = TRUTH_ORIF_EXPR;
27828 id = omp_reduction_id (code, id, NULL_TREE);
27829 tree scope = parser->scope;
27830 if (scope)
27831 id = build_qualified_name (NULL_TREE, scope, id, false);
27832 parser->scope = NULL_TREE;
27833 parser->qualifying_scope = NULL_TREE;
27834 parser->object_scope = NULL_TREE;
27836 else
27838 error ("invalid reduction-identifier");
27839 resync_fail:
27840 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27841 /*or_comma=*/false,
27842 /*consume_paren=*/true);
27843 return list;
27847 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27848 goto resync_fail;
27850 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27851 NULL);
27852 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27854 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27855 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27858 return nlist;
27861 /* OpenMP 2.5:
27862 schedule ( schedule-kind )
27863 schedule ( schedule-kind , expression )
27865 schedule-kind:
27866 static | dynamic | guided | runtime | auto */
27868 static tree
27869 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27871 tree c, t;
27873 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27874 return list;
27876 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27878 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27880 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27881 const char *p = IDENTIFIER_POINTER (id);
27883 switch (p[0])
27885 case 'd':
27886 if (strcmp ("dynamic", p) != 0)
27887 goto invalid_kind;
27888 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27889 break;
27891 case 'g':
27892 if (strcmp ("guided", p) != 0)
27893 goto invalid_kind;
27894 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27895 break;
27897 case 'r':
27898 if (strcmp ("runtime", p) != 0)
27899 goto invalid_kind;
27900 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27901 break;
27903 default:
27904 goto invalid_kind;
27907 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27908 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27909 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27910 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27911 else
27912 goto invalid_kind;
27913 cp_lexer_consume_token (parser->lexer);
27915 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27917 cp_token *token;
27918 cp_lexer_consume_token (parser->lexer);
27920 token = cp_lexer_peek_token (parser->lexer);
27921 t = cp_parser_assignment_expression (parser, false, NULL);
27923 if (t == error_mark_node)
27924 goto resync_fail;
27925 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27926 error_at (token->location, "schedule %<runtime%> does not take "
27927 "a %<chunk_size%> parameter");
27928 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27929 error_at (token->location, "schedule %<auto%> does not take "
27930 "a %<chunk_size%> parameter");
27931 else
27932 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27934 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27935 goto resync_fail;
27937 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27938 goto resync_fail;
27940 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27941 OMP_CLAUSE_CHAIN (c) = list;
27942 return c;
27944 invalid_kind:
27945 cp_parser_error (parser, "invalid schedule kind");
27946 resync_fail:
27947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27948 /*or_comma=*/false,
27949 /*consume_paren=*/true);
27950 return list;
27953 /* OpenMP 3.0:
27954 untied */
27956 static tree
27957 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27958 tree list, location_t location)
27960 tree c;
27962 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27964 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27965 OMP_CLAUSE_CHAIN (c) = list;
27966 return c;
27969 /* OpenMP 4.0:
27970 inbranch
27971 notinbranch */
27973 static tree
27974 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27975 tree list, location_t location)
27977 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27978 tree c = build_omp_clause (location, code);
27979 OMP_CLAUSE_CHAIN (c) = list;
27980 return c;
27983 /* OpenMP 4.0:
27984 parallel
27986 sections
27987 taskgroup */
27989 static tree
27990 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27991 enum omp_clause_code code,
27992 tree list, location_t location)
27994 tree c = build_omp_clause (location, code);
27995 OMP_CLAUSE_CHAIN (c) = list;
27996 return c;
27999 /* OpenMP 4.0:
28000 num_teams ( expression ) */
28002 static tree
28003 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28004 location_t location)
28006 tree t, c;
28008 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28009 return list;
28011 t = cp_parser_expression (parser);
28013 if (t == error_mark_node
28014 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28016 /*or_comma=*/false,
28017 /*consume_paren=*/true);
28019 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28020 "num_teams", location);
28022 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28023 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28024 OMP_CLAUSE_CHAIN (c) = list;
28026 return c;
28029 /* OpenMP 4.0:
28030 thread_limit ( expression ) */
28032 static tree
28033 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28034 location_t location)
28036 tree t, c;
28038 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28039 return list;
28041 t = cp_parser_expression (parser);
28043 if (t == error_mark_node
28044 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28045 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28046 /*or_comma=*/false,
28047 /*consume_paren=*/true);
28049 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28050 "thread_limit", location);
28052 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28053 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28054 OMP_CLAUSE_CHAIN (c) = list;
28056 return c;
28059 /* OpenMP 4.0:
28060 aligned ( variable-list )
28061 aligned ( variable-list : constant-expression ) */
28063 static tree
28064 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28066 tree nlist, c, alignment = NULL_TREE;
28067 bool colon;
28069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28070 return list;
28072 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28073 &colon);
28075 if (colon)
28077 alignment = cp_parser_constant_expression (parser, false, NULL);
28079 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28080 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28081 /*or_comma=*/false,
28082 /*consume_paren=*/true);
28084 if (alignment == error_mark_node)
28085 alignment = NULL_TREE;
28088 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28089 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28091 return nlist;
28094 /* OpenMP 4.0:
28095 linear ( variable-list )
28096 linear ( variable-list : expression ) */
28098 static tree
28099 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28100 bool is_cilk_simd_fn)
28102 tree nlist, c, step = integer_one_node;
28103 bool colon;
28105 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28106 return list;
28108 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28109 &colon);
28111 if (colon)
28113 step = cp_parser_expression (parser);
28115 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28117 sorry ("using parameters for %<linear%> step is not supported yet");
28118 step = integer_one_node;
28120 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28121 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28122 /*or_comma=*/false,
28123 /*consume_paren=*/true);
28125 if (step == error_mark_node)
28126 return list;
28129 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28130 OMP_CLAUSE_LINEAR_STEP (c) = step;
28132 return nlist;
28135 /* OpenMP 4.0:
28136 safelen ( constant-expression ) */
28138 static tree
28139 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28140 location_t location)
28142 tree t, c;
28144 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28145 return list;
28147 t = cp_parser_constant_expression (parser, false, NULL);
28149 if (t == error_mark_node
28150 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28152 /*or_comma=*/false,
28153 /*consume_paren=*/true);
28155 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28157 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28158 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28159 OMP_CLAUSE_CHAIN (c) = list;
28161 return c;
28164 /* OpenMP 4.0:
28165 simdlen ( constant-expression ) */
28167 static tree
28168 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28169 location_t location)
28171 tree t, c;
28173 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28174 return list;
28176 t = cp_parser_constant_expression (parser, false, NULL);
28178 if (t == error_mark_node
28179 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28180 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28181 /*or_comma=*/false,
28182 /*consume_paren=*/true);
28184 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28186 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28187 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28188 OMP_CLAUSE_CHAIN (c) = list;
28190 return c;
28193 /* OpenMP 4.0:
28194 depend ( depend-kind : variable-list )
28196 depend-kind:
28197 in | out | inout */
28199 static tree
28200 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28202 tree nlist, c;
28203 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28206 return list;
28208 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28210 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28211 const char *p = IDENTIFIER_POINTER (id);
28213 if (strcmp ("in", p) == 0)
28214 kind = OMP_CLAUSE_DEPEND_IN;
28215 else if (strcmp ("inout", p) == 0)
28216 kind = OMP_CLAUSE_DEPEND_INOUT;
28217 else if (strcmp ("out", p) == 0)
28218 kind = OMP_CLAUSE_DEPEND_OUT;
28219 else
28220 goto invalid_kind;
28222 else
28223 goto invalid_kind;
28225 cp_lexer_consume_token (parser->lexer);
28226 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28227 goto resync_fail;
28229 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28230 NULL);
28232 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28233 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28235 return nlist;
28237 invalid_kind:
28238 cp_parser_error (parser, "invalid depend kind");
28239 resync_fail:
28240 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28241 /*or_comma=*/false,
28242 /*consume_paren=*/true);
28243 return list;
28246 /* OpenMP 4.0:
28247 map ( map-kind : variable-list )
28248 map ( variable-list )
28250 map-kind:
28251 alloc | to | from | tofrom */
28253 static tree
28254 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28256 tree nlist, c;
28257 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28259 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28260 return list;
28262 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28263 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28266 const char *p = IDENTIFIER_POINTER (id);
28268 if (strcmp ("alloc", p) == 0)
28269 kind = OMP_CLAUSE_MAP_ALLOC;
28270 else if (strcmp ("to", p) == 0)
28271 kind = OMP_CLAUSE_MAP_TO;
28272 else if (strcmp ("from", p) == 0)
28273 kind = OMP_CLAUSE_MAP_FROM;
28274 else if (strcmp ("tofrom", p) == 0)
28275 kind = OMP_CLAUSE_MAP_TOFROM;
28276 else
28278 cp_parser_error (parser, "invalid map kind");
28279 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28280 /*or_comma=*/false,
28281 /*consume_paren=*/true);
28282 return list;
28284 cp_lexer_consume_token (parser->lexer);
28285 cp_lexer_consume_token (parser->lexer);
28288 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28289 NULL);
28291 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28292 OMP_CLAUSE_MAP_KIND (c) = kind;
28294 return nlist;
28297 /* OpenMP 4.0:
28298 device ( expression ) */
28300 static tree
28301 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28302 location_t location)
28304 tree t, c;
28306 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28307 return list;
28309 t = cp_parser_expression (parser);
28311 if (t == error_mark_node
28312 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28314 /*or_comma=*/false,
28315 /*consume_paren=*/true);
28317 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28318 "device", location);
28320 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28321 OMP_CLAUSE_DEVICE_ID (c) = t;
28322 OMP_CLAUSE_CHAIN (c) = list;
28324 return c;
28327 /* OpenMP 4.0:
28328 dist_schedule ( static )
28329 dist_schedule ( static , expression ) */
28331 static tree
28332 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28333 location_t location)
28335 tree c, t;
28337 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28338 return list;
28340 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28342 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28343 goto invalid_kind;
28344 cp_lexer_consume_token (parser->lexer);
28346 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28348 cp_lexer_consume_token (parser->lexer);
28350 t = cp_parser_assignment_expression (parser, false, NULL);
28352 if (t == error_mark_node)
28353 goto resync_fail;
28354 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28356 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28357 goto resync_fail;
28359 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28360 goto resync_fail;
28362 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28363 location);
28364 OMP_CLAUSE_CHAIN (c) = list;
28365 return c;
28367 invalid_kind:
28368 cp_parser_error (parser, "invalid dist_schedule kind");
28369 resync_fail:
28370 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28371 /*or_comma=*/false,
28372 /*consume_paren=*/true);
28373 return list;
28376 /* OpenMP 4.0:
28377 proc_bind ( proc-bind-kind )
28379 proc-bind-kind:
28380 master | close | spread */
28382 static tree
28383 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28384 location_t location)
28386 tree c;
28387 enum omp_clause_proc_bind_kind kind;
28389 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28390 return list;
28392 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28394 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28395 const char *p = IDENTIFIER_POINTER (id);
28397 if (strcmp ("master", p) == 0)
28398 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28399 else if (strcmp ("close", p) == 0)
28400 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28401 else if (strcmp ("spread", p) == 0)
28402 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28403 else
28404 goto invalid_kind;
28406 else
28407 goto invalid_kind;
28409 cp_lexer_consume_token (parser->lexer);
28410 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28411 goto resync_fail;
28413 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28414 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28415 location);
28416 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28417 OMP_CLAUSE_CHAIN (c) = list;
28418 return c;
28420 invalid_kind:
28421 cp_parser_error (parser, "invalid depend kind");
28422 resync_fail:
28423 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28424 /*or_comma=*/false,
28425 /*consume_paren=*/true);
28426 return list;
28429 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28430 is a bitmask in MASK. Return the list of clauses found; the result
28431 of clause default goes in *pdefault. */
28433 static tree
28434 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28435 const char *where, cp_token *pragma_tok,
28436 bool finish_p = true)
28438 tree clauses = NULL;
28439 bool first = true;
28440 cp_token *token = NULL;
28441 bool cilk_simd_fn = false;
28443 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28445 pragma_omp_clause c_kind;
28446 const char *c_name;
28447 tree prev = clauses;
28449 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28450 cp_lexer_consume_token (parser->lexer);
28452 token = cp_lexer_peek_token (parser->lexer);
28453 c_kind = cp_parser_omp_clause_name (parser);
28455 switch (c_kind)
28457 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28458 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28459 token->location);
28460 c_name = "collapse";
28461 break;
28462 case PRAGMA_OMP_CLAUSE_COPYIN:
28463 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28464 c_name = "copyin";
28465 break;
28466 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28467 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28468 clauses);
28469 c_name = "copyprivate";
28470 break;
28471 case PRAGMA_OMP_CLAUSE_DEFAULT:
28472 clauses = cp_parser_omp_clause_default (parser, clauses,
28473 token->location);
28474 c_name = "default";
28475 break;
28476 case PRAGMA_OMP_CLAUSE_FINAL:
28477 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28478 c_name = "final";
28479 break;
28480 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28481 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28482 clauses);
28483 c_name = "firstprivate";
28484 break;
28485 case PRAGMA_OMP_CLAUSE_IF:
28486 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28487 c_name = "if";
28488 break;
28489 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28490 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28491 clauses);
28492 c_name = "lastprivate";
28493 break;
28494 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28495 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28496 token->location);
28497 c_name = "mergeable";
28498 break;
28499 case PRAGMA_OMP_CLAUSE_NOWAIT:
28500 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28501 c_name = "nowait";
28502 break;
28503 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28504 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28505 token->location);
28506 c_name = "num_threads";
28507 break;
28508 case PRAGMA_OMP_CLAUSE_ORDERED:
28509 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28510 token->location);
28511 c_name = "ordered";
28512 break;
28513 case PRAGMA_OMP_CLAUSE_PRIVATE:
28514 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28515 clauses);
28516 c_name = "private";
28517 break;
28518 case PRAGMA_OMP_CLAUSE_REDUCTION:
28519 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28520 c_name = "reduction";
28521 break;
28522 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28523 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28524 token->location);
28525 c_name = "schedule";
28526 break;
28527 case PRAGMA_OMP_CLAUSE_SHARED:
28528 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28529 clauses);
28530 c_name = "shared";
28531 break;
28532 case PRAGMA_OMP_CLAUSE_UNTIED:
28533 clauses = cp_parser_omp_clause_untied (parser, clauses,
28534 token->location);
28535 c_name = "untied";
28536 break;
28537 case PRAGMA_OMP_CLAUSE_INBRANCH:
28538 case PRAGMA_CILK_CLAUSE_MASK:
28539 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28540 clauses, token->location);
28541 c_name = "inbranch";
28542 break;
28543 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28544 case PRAGMA_CILK_CLAUSE_NOMASK:
28545 clauses = cp_parser_omp_clause_branch (parser,
28546 OMP_CLAUSE_NOTINBRANCH,
28547 clauses, token->location);
28548 c_name = "notinbranch";
28549 break;
28550 case PRAGMA_OMP_CLAUSE_PARALLEL:
28551 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28552 clauses, token->location);
28553 c_name = "parallel";
28554 if (!first)
28556 clause_not_first:
28557 error_at (token->location, "%qs must be the first clause of %qs",
28558 c_name, where);
28559 clauses = prev;
28561 break;
28562 case PRAGMA_OMP_CLAUSE_FOR:
28563 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28564 clauses, token->location);
28565 c_name = "for";
28566 if (!first)
28567 goto clause_not_first;
28568 break;
28569 case PRAGMA_OMP_CLAUSE_SECTIONS:
28570 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28571 clauses, token->location);
28572 c_name = "sections";
28573 if (!first)
28574 goto clause_not_first;
28575 break;
28576 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28577 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28578 clauses, token->location);
28579 c_name = "taskgroup";
28580 if (!first)
28581 goto clause_not_first;
28582 break;
28583 case PRAGMA_OMP_CLAUSE_TO:
28584 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28585 clauses);
28586 c_name = "to";
28587 break;
28588 case PRAGMA_OMP_CLAUSE_FROM:
28589 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28590 clauses);
28591 c_name = "from";
28592 break;
28593 case PRAGMA_OMP_CLAUSE_UNIFORM:
28594 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28595 clauses);
28596 c_name = "uniform";
28597 break;
28598 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28599 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28600 token->location);
28601 c_name = "num_teams";
28602 break;
28603 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28604 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28605 token->location);
28606 c_name = "thread_limit";
28607 break;
28608 case PRAGMA_OMP_CLAUSE_ALIGNED:
28609 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28610 c_name = "aligned";
28611 break;
28612 case PRAGMA_OMP_CLAUSE_LINEAR:
28613 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28614 cilk_simd_fn = true;
28615 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28616 c_name = "linear";
28617 break;
28618 case PRAGMA_OMP_CLAUSE_DEPEND:
28619 clauses = cp_parser_omp_clause_depend (parser, clauses);
28620 c_name = "depend";
28621 break;
28622 case PRAGMA_OMP_CLAUSE_MAP:
28623 clauses = cp_parser_omp_clause_map (parser, clauses);
28624 c_name = "map";
28625 break;
28626 case PRAGMA_OMP_CLAUSE_DEVICE:
28627 clauses = cp_parser_omp_clause_device (parser, clauses,
28628 token->location);
28629 c_name = "device";
28630 break;
28631 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28632 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28633 token->location);
28634 c_name = "dist_schedule";
28635 break;
28636 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28637 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28638 token->location);
28639 c_name = "proc_bind";
28640 break;
28641 case PRAGMA_OMP_CLAUSE_SAFELEN:
28642 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28643 token->location);
28644 c_name = "safelen";
28645 break;
28646 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28647 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28648 token->location);
28649 c_name = "simdlen";
28650 break;
28651 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28652 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28653 c_name = "simdlen";
28654 break;
28655 default:
28656 cp_parser_error (parser, "expected clause");
28657 goto saw_error;
28660 first = false;
28662 if (((mask >> c_kind) & 1) == 0)
28664 /* Remove the invalid clause(s) from the list to avoid
28665 confusing the rest of the compiler. */
28666 clauses = prev;
28667 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28670 saw_error:
28671 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28672 no reason to skip to the end. */
28673 if (!(flag_cilkplus && pragma_tok == NULL))
28674 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28675 if (finish_p)
28676 return finish_omp_clauses (clauses);
28677 return clauses;
28680 /* OpenMP 2.5:
28681 structured-block:
28682 statement
28684 In practice, we're also interested in adding the statement to an
28685 outer node. So it is convenient if we work around the fact that
28686 cp_parser_statement calls add_stmt. */
28688 static unsigned
28689 cp_parser_begin_omp_structured_block (cp_parser *parser)
28691 unsigned save = parser->in_statement;
28693 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28694 This preserves the "not within loop or switch" style error messages
28695 for nonsense cases like
28696 void foo() {
28697 #pragma omp single
28698 break;
28701 if (parser->in_statement)
28702 parser->in_statement = IN_OMP_BLOCK;
28704 return save;
28707 static void
28708 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28710 parser->in_statement = save;
28713 static tree
28714 cp_parser_omp_structured_block (cp_parser *parser)
28716 tree stmt = begin_omp_structured_block ();
28717 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28719 cp_parser_statement (parser, NULL_TREE, false, NULL);
28721 cp_parser_end_omp_structured_block (parser, save);
28722 return finish_omp_structured_block (stmt);
28725 /* OpenMP 2.5:
28726 # pragma omp atomic new-line
28727 expression-stmt
28729 expression-stmt:
28730 x binop= expr | x++ | ++x | x-- | --x
28731 binop:
28732 +, *, -, /, &, ^, |, <<, >>
28734 where x is an lvalue expression with scalar type.
28736 OpenMP 3.1:
28737 # pragma omp atomic new-line
28738 update-stmt
28740 # pragma omp atomic read new-line
28741 read-stmt
28743 # pragma omp atomic write new-line
28744 write-stmt
28746 # pragma omp atomic update new-line
28747 update-stmt
28749 # pragma omp atomic capture new-line
28750 capture-stmt
28752 # pragma omp atomic capture new-line
28753 capture-block
28755 read-stmt:
28756 v = x
28757 write-stmt:
28758 x = expr
28759 update-stmt:
28760 expression-stmt | x = x binop expr
28761 capture-stmt:
28762 v = expression-stmt
28763 capture-block:
28764 { v = x; update-stmt; } | { update-stmt; v = x; }
28766 OpenMP 4.0:
28767 update-stmt:
28768 expression-stmt | x = x binop expr | x = expr binop x
28769 capture-stmt:
28770 v = update-stmt
28771 capture-block:
28772 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28774 where x and v are lvalue expressions with scalar type. */
28776 static void
28777 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28779 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28780 tree rhs1 = NULL_TREE, orig_lhs;
28781 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28782 bool structured_block = false;
28783 bool seq_cst = false;
28785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28788 const char *p = IDENTIFIER_POINTER (id);
28790 if (!strcmp (p, "seq_cst"))
28792 seq_cst = true;
28793 cp_lexer_consume_token (parser->lexer);
28794 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28795 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28796 cp_lexer_consume_token (parser->lexer);
28799 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28802 const char *p = IDENTIFIER_POINTER (id);
28804 if (!strcmp (p, "read"))
28805 code = OMP_ATOMIC_READ;
28806 else if (!strcmp (p, "write"))
28807 code = NOP_EXPR;
28808 else if (!strcmp (p, "update"))
28809 code = OMP_ATOMIC;
28810 else if (!strcmp (p, "capture"))
28811 code = OMP_ATOMIC_CAPTURE_NEW;
28812 else
28813 p = NULL;
28814 if (p)
28815 cp_lexer_consume_token (parser->lexer);
28817 if (!seq_cst)
28819 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28820 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28821 cp_lexer_consume_token (parser->lexer);
28823 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28825 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28826 const char *p = IDENTIFIER_POINTER (id);
28828 if (!strcmp (p, "seq_cst"))
28830 seq_cst = true;
28831 cp_lexer_consume_token (parser->lexer);
28835 cp_parser_require_pragma_eol (parser, pragma_tok);
28837 switch (code)
28839 case OMP_ATOMIC_READ:
28840 case NOP_EXPR: /* atomic write */
28841 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28842 /*cast_p=*/false, NULL);
28843 if (v == error_mark_node)
28844 goto saw_error;
28845 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28846 goto saw_error;
28847 if (code == NOP_EXPR)
28848 lhs = cp_parser_expression (parser);
28849 else
28850 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28851 /*cast_p=*/false, NULL);
28852 if (lhs == error_mark_node)
28853 goto saw_error;
28854 if (code == NOP_EXPR)
28856 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28857 opcode. */
28858 code = OMP_ATOMIC;
28859 rhs = lhs;
28860 lhs = v;
28861 v = NULL_TREE;
28863 goto done;
28864 case OMP_ATOMIC_CAPTURE_NEW:
28865 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28867 cp_lexer_consume_token (parser->lexer);
28868 structured_block = true;
28870 else
28872 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28873 /*cast_p=*/false, NULL);
28874 if (v == error_mark_node)
28875 goto saw_error;
28876 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28877 goto saw_error;
28879 default:
28880 break;
28883 restart:
28884 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28885 /*cast_p=*/false, NULL);
28886 orig_lhs = lhs;
28887 switch (TREE_CODE (lhs))
28889 case ERROR_MARK:
28890 goto saw_error;
28892 case POSTINCREMENT_EXPR:
28893 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28894 code = OMP_ATOMIC_CAPTURE_OLD;
28895 /* FALLTHROUGH */
28896 case PREINCREMENT_EXPR:
28897 lhs = TREE_OPERAND (lhs, 0);
28898 opcode = PLUS_EXPR;
28899 rhs = integer_one_node;
28900 break;
28902 case POSTDECREMENT_EXPR:
28903 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28904 code = OMP_ATOMIC_CAPTURE_OLD;
28905 /* FALLTHROUGH */
28906 case PREDECREMENT_EXPR:
28907 lhs = TREE_OPERAND (lhs, 0);
28908 opcode = MINUS_EXPR;
28909 rhs = integer_one_node;
28910 break;
28912 case COMPOUND_EXPR:
28913 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28914 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28915 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28916 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28917 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28918 (TREE_OPERAND (lhs, 1), 0), 0)))
28919 == BOOLEAN_TYPE)
28920 /* Undo effects of boolean_increment for post {in,de}crement. */
28921 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28922 /* FALLTHRU */
28923 case MODIFY_EXPR:
28924 if (TREE_CODE (lhs) == MODIFY_EXPR
28925 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28927 /* Undo effects of boolean_increment. */
28928 if (integer_onep (TREE_OPERAND (lhs, 1)))
28930 /* This is pre or post increment. */
28931 rhs = TREE_OPERAND (lhs, 1);
28932 lhs = TREE_OPERAND (lhs, 0);
28933 opcode = NOP_EXPR;
28934 if (code == OMP_ATOMIC_CAPTURE_NEW
28935 && !structured_block
28936 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28937 code = OMP_ATOMIC_CAPTURE_OLD;
28938 break;
28941 /* FALLTHRU */
28942 default:
28943 switch (cp_lexer_peek_token (parser->lexer)->type)
28945 case CPP_MULT_EQ:
28946 opcode = MULT_EXPR;
28947 break;
28948 case CPP_DIV_EQ:
28949 opcode = TRUNC_DIV_EXPR;
28950 break;
28951 case CPP_PLUS_EQ:
28952 opcode = PLUS_EXPR;
28953 break;
28954 case CPP_MINUS_EQ:
28955 opcode = MINUS_EXPR;
28956 break;
28957 case CPP_LSHIFT_EQ:
28958 opcode = LSHIFT_EXPR;
28959 break;
28960 case CPP_RSHIFT_EQ:
28961 opcode = RSHIFT_EXPR;
28962 break;
28963 case CPP_AND_EQ:
28964 opcode = BIT_AND_EXPR;
28965 break;
28966 case CPP_OR_EQ:
28967 opcode = BIT_IOR_EXPR;
28968 break;
28969 case CPP_XOR_EQ:
28970 opcode = BIT_XOR_EXPR;
28971 break;
28972 case CPP_EQ:
28973 enum cp_parser_prec oprec;
28974 cp_token *token;
28975 cp_lexer_consume_token (parser->lexer);
28976 cp_parser_parse_tentatively (parser);
28977 rhs1 = cp_parser_simple_cast_expression (parser);
28978 if (rhs1 == error_mark_node)
28980 cp_parser_abort_tentative_parse (parser);
28981 cp_parser_simple_cast_expression (parser);
28982 goto saw_error;
28984 token = cp_lexer_peek_token (parser->lexer);
28985 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28987 cp_parser_abort_tentative_parse (parser);
28988 cp_parser_parse_tentatively (parser);
28989 rhs = cp_parser_binary_expression (parser, false, true,
28990 PREC_NOT_OPERATOR, NULL);
28991 if (rhs == error_mark_node)
28993 cp_parser_abort_tentative_parse (parser);
28994 cp_parser_binary_expression (parser, false, true,
28995 PREC_NOT_OPERATOR, NULL);
28996 goto saw_error;
28998 switch (TREE_CODE (rhs))
29000 case MULT_EXPR:
29001 case TRUNC_DIV_EXPR:
29002 case PLUS_EXPR:
29003 case MINUS_EXPR:
29004 case LSHIFT_EXPR:
29005 case RSHIFT_EXPR:
29006 case BIT_AND_EXPR:
29007 case BIT_IOR_EXPR:
29008 case BIT_XOR_EXPR:
29009 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29011 if (cp_parser_parse_definitely (parser))
29013 opcode = TREE_CODE (rhs);
29014 rhs1 = TREE_OPERAND (rhs, 0);
29015 rhs = TREE_OPERAND (rhs, 1);
29016 goto stmt_done;
29018 else
29019 goto saw_error;
29021 break;
29022 default:
29023 break;
29025 cp_parser_abort_tentative_parse (parser);
29026 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29028 rhs = cp_parser_expression (parser);
29029 if (rhs == error_mark_node)
29030 goto saw_error;
29031 opcode = NOP_EXPR;
29032 rhs1 = NULL_TREE;
29033 goto stmt_done;
29035 cp_parser_error (parser,
29036 "invalid form of %<#pragma omp atomic%>");
29037 goto saw_error;
29039 if (!cp_parser_parse_definitely (parser))
29040 goto saw_error;
29041 switch (token->type)
29043 case CPP_SEMICOLON:
29044 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29046 code = OMP_ATOMIC_CAPTURE_OLD;
29047 v = lhs;
29048 lhs = NULL_TREE;
29049 lhs1 = rhs1;
29050 rhs1 = NULL_TREE;
29051 cp_lexer_consume_token (parser->lexer);
29052 goto restart;
29054 else if (structured_block)
29056 opcode = NOP_EXPR;
29057 rhs = rhs1;
29058 rhs1 = NULL_TREE;
29059 goto stmt_done;
29061 cp_parser_error (parser,
29062 "invalid form of %<#pragma omp atomic%>");
29063 goto saw_error;
29064 case CPP_MULT:
29065 opcode = MULT_EXPR;
29066 break;
29067 case CPP_DIV:
29068 opcode = TRUNC_DIV_EXPR;
29069 break;
29070 case CPP_PLUS:
29071 opcode = PLUS_EXPR;
29072 break;
29073 case CPP_MINUS:
29074 opcode = MINUS_EXPR;
29075 break;
29076 case CPP_LSHIFT:
29077 opcode = LSHIFT_EXPR;
29078 break;
29079 case CPP_RSHIFT:
29080 opcode = RSHIFT_EXPR;
29081 break;
29082 case CPP_AND:
29083 opcode = BIT_AND_EXPR;
29084 break;
29085 case CPP_OR:
29086 opcode = BIT_IOR_EXPR;
29087 break;
29088 case CPP_XOR:
29089 opcode = BIT_XOR_EXPR;
29090 break;
29091 default:
29092 cp_parser_error (parser,
29093 "invalid operator for %<#pragma omp atomic%>");
29094 goto saw_error;
29096 oprec = TOKEN_PRECEDENCE (token);
29097 gcc_assert (oprec != PREC_NOT_OPERATOR);
29098 if (commutative_tree_code (opcode))
29099 oprec = (enum cp_parser_prec) (oprec - 1);
29100 cp_lexer_consume_token (parser->lexer);
29101 rhs = cp_parser_binary_expression (parser, false, false,
29102 oprec, NULL);
29103 if (rhs == error_mark_node)
29104 goto saw_error;
29105 goto stmt_done;
29106 /* FALLTHROUGH */
29107 default:
29108 cp_parser_error (parser,
29109 "invalid operator for %<#pragma omp atomic%>");
29110 goto saw_error;
29112 cp_lexer_consume_token (parser->lexer);
29114 rhs = cp_parser_expression (parser);
29115 if (rhs == error_mark_node)
29116 goto saw_error;
29117 break;
29119 stmt_done:
29120 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29122 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29123 goto saw_error;
29124 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29125 /*cast_p=*/false, NULL);
29126 if (v == error_mark_node)
29127 goto saw_error;
29128 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29129 goto saw_error;
29130 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29131 /*cast_p=*/false, NULL);
29132 if (lhs1 == error_mark_node)
29133 goto saw_error;
29135 if (structured_block)
29137 cp_parser_consume_semicolon_at_end_of_statement (parser);
29138 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29140 done:
29141 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29142 if (!structured_block)
29143 cp_parser_consume_semicolon_at_end_of_statement (parser);
29144 return;
29146 saw_error:
29147 cp_parser_skip_to_end_of_block_or_statement (parser);
29148 if (structured_block)
29150 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29151 cp_lexer_consume_token (parser->lexer);
29152 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29154 cp_parser_skip_to_end_of_block_or_statement (parser);
29155 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29156 cp_lexer_consume_token (parser->lexer);
29162 /* OpenMP 2.5:
29163 # pragma omp barrier new-line */
29165 static void
29166 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29168 cp_parser_require_pragma_eol (parser, pragma_tok);
29169 finish_omp_barrier ();
29172 /* OpenMP 2.5:
29173 # pragma omp critical [(name)] new-line
29174 structured-block */
29176 static tree
29177 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29179 tree stmt, name = NULL;
29181 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29183 cp_lexer_consume_token (parser->lexer);
29185 name = cp_parser_identifier (parser);
29187 if (name == error_mark_node
29188 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29189 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29190 /*or_comma=*/false,
29191 /*consume_paren=*/true);
29192 if (name == error_mark_node)
29193 name = NULL;
29195 cp_parser_require_pragma_eol (parser, pragma_tok);
29197 stmt = cp_parser_omp_structured_block (parser);
29198 return c_finish_omp_critical (input_location, stmt, name);
29201 /* OpenMP 2.5:
29202 # pragma omp flush flush-vars[opt] new-line
29204 flush-vars:
29205 ( variable-list ) */
29207 static void
29208 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29211 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29212 cp_parser_require_pragma_eol (parser, pragma_tok);
29214 finish_omp_flush ();
29217 /* Helper function, to parse omp for increment expression. */
29219 static tree
29220 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29222 tree cond = cp_parser_binary_expression (parser, false, true,
29223 PREC_NOT_OPERATOR, NULL);
29224 if (cond == error_mark_node
29225 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29227 cp_parser_skip_to_end_of_statement (parser);
29228 return error_mark_node;
29231 switch (TREE_CODE (cond))
29233 case GT_EXPR:
29234 case GE_EXPR:
29235 case LT_EXPR:
29236 case LE_EXPR:
29237 break;
29238 case NE_EXPR:
29239 if (code == CILK_SIMD || code == CILK_FOR)
29240 break;
29241 /* Fall through: OpenMP disallows NE_EXPR. */
29242 default:
29243 return error_mark_node;
29246 /* If decl is an iterator, preserve LHS and RHS of the relational
29247 expr until finish_omp_for. */
29248 if (decl
29249 && (type_dependent_expression_p (decl)
29250 || CLASS_TYPE_P (TREE_TYPE (decl))))
29251 return cond;
29253 return build_x_binary_op (input_location, TREE_CODE (cond),
29254 TREE_OPERAND (cond, 0), ERROR_MARK,
29255 TREE_OPERAND (cond, 1), ERROR_MARK,
29256 /*overload=*/NULL, tf_warning_or_error);
29259 /* Helper function, to parse omp for increment expression. */
29261 static tree
29262 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29264 cp_token *token = cp_lexer_peek_token (parser->lexer);
29265 enum tree_code op;
29266 tree lhs, rhs;
29267 cp_id_kind idk;
29268 bool decl_first;
29270 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29272 op = (token->type == CPP_PLUS_PLUS
29273 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29274 cp_lexer_consume_token (parser->lexer);
29275 lhs = cp_parser_simple_cast_expression (parser);
29276 if (lhs != decl)
29277 return error_mark_node;
29278 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29281 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29282 if (lhs != decl)
29283 return error_mark_node;
29285 token = cp_lexer_peek_token (parser->lexer);
29286 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29288 op = (token->type == CPP_PLUS_PLUS
29289 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29290 cp_lexer_consume_token (parser->lexer);
29291 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29294 op = cp_parser_assignment_operator_opt (parser);
29295 if (op == ERROR_MARK)
29296 return error_mark_node;
29298 if (op != NOP_EXPR)
29300 rhs = cp_parser_assignment_expression (parser, false, NULL);
29301 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29302 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29305 lhs = cp_parser_binary_expression (parser, false, false,
29306 PREC_ADDITIVE_EXPRESSION, NULL);
29307 token = cp_lexer_peek_token (parser->lexer);
29308 decl_first = lhs == decl;
29309 if (decl_first)
29310 lhs = NULL_TREE;
29311 if (token->type != CPP_PLUS
29312 && token->type != CPP_MINUS)
29313 return error_mark_node;
29317 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29318 cp_lexer_consume_token (parser->lexer);
29319 rhs = cp_parser_binary_expression (parser, false, false,
29320 PREC_ADDITIVE_EXPRESSION, NULL);
29321 token = cp_lexer_peek_token (parser->lexer);
29322 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29324 if (lhs == NULL_TREE)
29326 if (op == PLUS_EXPR)
29327 lhs = rhs;
29328 else
29329 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29330 tf_warning_or_error);
29332 else
29333 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29334 ERROR_MARK, NULL, tf_warning_or_error);
29337 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29339 if (!decl_first)
29341 if (rhs != decl || op == MINUS_EXPR)
29342 return error_mark_node;
29343 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29345 else
29346 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29348 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29351 /* Parse the initialization statement of either an OpenMP for loop or
29352 a Cilk Plus for loop.
29354 Return true if the resulting construct should have an
29355 OMP_CLAUSE_PRIVATE added to it. */
29357 static bool
29358 cp_parser_omp_for_loop_init (cp_parser *parser,
29359 enum tree_code code,
29360 tree &this_pre_body,
29361 vec<tree, va_gc> *for_block,
29362 tree &init,
29363 tree &decl,
29364 tree &real_decl)
29366 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29367 return false;
29369 bool add_private_clause = false;
29371 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29373 init-expr:
29374 var = lb
29375 integer-type var = lb
29376 random-access-iterator-type var = lb
29377 pointer-type var = lb
29379 cp_decl_specifier_seq type_specifiers;
29381 /* First, try to parse as an initialized declaration. See
29382 cp_parser_condition, from whence the bulk of this is copied. */
29384 cp_parser_parse_tentatively (parser);
29385 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29386 /*is_trailing_return=*/false,
29387 &type_specifiers);
29388 if (cp_parser_parse_definitely (parser))
29390 /* If parsing a type specifier seq succeeded, then this
29391 MUST be a initialized declaration. */
29392 tree asm_specification, attributes;
29393 cp_declarator *declarator;
29395 declarator = cp_parser_declarator (parser,
29396 CP_PARSER_DECLARATOR_NAMED,
29397 /*ctor_dtor_or_conv_p=*/NULL,
29398 /*parenthesized_p=*/NULL,
29399 /*member_p=*/false,
29400 /*friend_p=*/false);
29401 attributes = cp_parser_attributes_opt (parser);
29402 asm_specification = cp_parser_asm_specification_opt (parser);
29404 if (declarator == cp_error_declarator)
29405 cp_parser_skip_to_end_of_statement (parser);
29407 else
29409 tree pushed_scope, auto_node;
29411 decl = start_decl (declarator, &type_specifiers,
29412 SD_INITIALIZED, attributes,
29413 /*prefix_attributes=*/NULL_TREE,
29414 &pushed_scope);
29416 auto_node = type_uses_auto (TREE_TYPE (decl));
29417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29419 if (cp_lexer_next_token_is (parser->lexer,
29420 CPP_OPEN_PAREN))
29422 if (code != CILK_SIMD && code != CILK_FOR)
29423 error ("parenthesized initialization is not allowed in "
29424 "OpenMP %<for%> loop");
29425 else
29426 error ("parenthesized initialization is "
29427 "not allowed in for-loop");
29429 else
29430 /* Trigger an error. */
29431 cp_parser_require (parser, CPP_EQ, RT_EQ);
29433 init = error_mark_node;
29434 cp_parser_skip_to_end_of_statement (parser);
29436 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29437 || type_dependent_expression_p (decl)
29438 || auto_node)
29440 bool is_direct_init, is_non_constant_init;
29442 init = cp_parser_initializer (parser,
29443 &is_direct_init,
29444 &is_non_constant_init);
29446 if (auto_node)
29448 TREE_TYPE (decl)
29449 = do_auto_deduction (TREE_TYPE (decl), init,
29450 auto_node);
29452 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29453 && !type_dependent_expression_p (decl))
29454 goto non_class;
29457 cp_finish_decl (decl, init, !is_non_constant_init,
29458 asm_specification,
29459 LOOKUP_ONLYCONVERTING);
29460 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29462 vec_safe_push (for_block, this_pre_body);
29463 init = NULL_TREE;
29465 else
29466 init = pop_stmt_list (this_pre_body);
29467 this_pre_body = NULL_TREE;
29469 else
29471 /* Consume '='. */
29472 cp_lexer_consume_token (parser->lexer);
29473 init = cp_parser_assignment_expression (parser, false, NULL);
29475 non_class:
29476 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29477 init = error_mark_node;
29478 else
29479 cp_finish_decl (decl, NULL_TREE,
29480 /*init_const_expr_p=*/false,
29481 asm_specification,
29482 LOOKUP_ONLYCONVERTING);
29485 if (pushed_scope)
29486 pop_scope (pushed_scope);
29489 else
29491 cp_id_kind idk;
29492 /* If parsing a type specifier sequence failed, then
29493 this MUST be a simple expression. */
29494 if (code == CILK_FOR)
29495 error ("%<_Cilk_for%> allows expression instead of declaration only "
29496 "in C, not in C++");
29497 cp_parser_parse_tentatively (parser);
29498 decl = cp_parser_primary_expression (parser, false, false,
29499 false, &idk);
29500 if (!cp_parser_error_occurred (parser)
29501 && decl
29502 && DECL_P (decl)
29503 && CLASS_TYPE_P (TREE_TYPE (decl)))
29505 tree rhs;
29507 cp_parser_parse_definitely (parser);
29508 cp_parser_require (parser, CPP_EQ, RT_EQ);
29509 rhs = cp_parser_assignment_expression (parser, false, NULL);
29510 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29511 decl, NOP_EXPR,
29512 rhs,
29513 tf_warning_or_error));
29514 add_private_clause = true;
29516 else
29518 decl = NULL;
29519 cp_parser_abort_tentative_parse (parser);
29520 init = cp_parser_expression (parser);
29521 if (init)
29523 if (TREE_CODE (init) == MODIFY_EXPR
29524 || TREE_CODE (init) == MODOP_EXPR)
29525 real_decl = TREE_OPERAND (init, 0);
29529 return add_private_clause;
29532 /* Parse the restricted form of the for statement allowed by OpenMP. */
29534 static tree
29535 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29536 tree *cclauses)
29538 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29539 tree real_decl, initv, condv, incrv, declv;
29540 tree this_pre_body, cl;
29541 location_t loc_first;
29542 bool collapse_err = false;
29543 int i, collapse = 1, nbraces = 0;
29544 vec<tree, va_gc> *for_block = make_tree_vector ();
29546 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29547 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29548 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29550 gcc_assert (collapse >= 1);
29552 declv = make_tree_vec (collapse);
29553 initv = make_tree_vec (collapse);
29554 condv = make_tree_vec (collapse);
29555 incrv = make_tree_vec (collapse);
29557 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29559 for (i = 0; i < collapse; i++)
29561 int bracecount = 0;
29562 bool add_private_clause = false;
29563 location_t loc;
29565 if (code != CILK_FOR
29566 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29568 cp_parser_error (parser, "for statement expected");
29569 return NULL;
29571 if (code == CILK_FOR
29572 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29574 cp_parser_error (parser, "_Cilk_for statement expected");
29575 return NULL;
29577 loc = cp_lexer_consume_token (parser->lexer)->location;
29579 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29580 return NULL;
29582 init = decl = real_decl = NULL;
29583 this_pre_body = push_stmt_list ();
29585 add_private_clause
29586 |= cp_parser_omp_for_loop_init (parser, code,
29587 this_pre_body, for_block,
29588 init, decl, real_decl);
29590 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29591 if (this_pre_body)
29593 this_pre_body = pop_stmt_list (this_pre_body);
29594 if (pre_body)
29596 tree t = pre_body;
29597 pre_body = push_stmt_list ();
29598 add_stmt (t);
29599 add_stmt (this_pre_body);
29600 pre_body = pop_stmt_list (pre_body);
29602 else
29603 pre_body = this_pre_body;
29606 if (decl)
29607 real_decl = decl;
29608 if (cclauses != NULL
29609 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29610 && real_decl != NULL_TREE)
29612 tree *c;
29613 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29614 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29615 && OMP_CLAUSE_DECL (*c) == real_decl)
29617 error_at (loc, "iteration variable %qD"
29618 " should not be firstprivate", real_decl);
29619 *c = OMP_CLAUSE_CHAIN (*c);
29621 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29622 && OMP_CLAUSE_DECL (*c) == real_decl)
29624 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29625 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29626 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29627 OMP_CLAUSE_DECL (l) = real_decl;
29628 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29629 if (code == OMP_SIMD)
29631 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29632 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29634 else
29636 OMP_CLAUSE_CHAIN (l) = clauses;
29637 clauses = l;
29639 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29640 CP_OMP_CLAUSE_INFO (*c) = NULL;
29641 add_private_clause = false;
29643 else
29645 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29646 && OMP_CLAUSE_DECL (*c) == real_decl)
29647 add_private_clause = false;
29648 c = &OMP_CLAUSE_CHAIN (*c);
29652 if (add_private_clause)
29654 tree c;
29655 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29657 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29658 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29659 && OMP_CLAUSE_DECL (c) == decl)
29660 break;
29661 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29662 && OMP_CLAUSE_DECL (c) == decl)
29663 error_at (loc, "iteration variable %qD "
29664 "should not be firstprivate",
29665 decl);
29666 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29667 && OMP_CLAUSE_DECL (c) == decl)
29668 error_at (loc, "iteration variable %qD should not be reduction",
29669 decl);
29671 if (c == NULL)
29673 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29674 OMP_CLAUSE_DECL (c) = decl;
29675 c = finish_omp_clauses (c);
29676 if (c)
29678 OMP_CLAUSE_CHAIN (c) = clauses;
29679 clauses = c;
29684 cond = NULL;
29685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29686 cond = cp_parser_omp_for_cond (parser, decl, code);
29687 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29689 incr = NULL;
29690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29692 /* If decl is an iterator, preserve the operator on decl
29693 until finish_omp_for. */
29694 if (real_decl
29695 && ((processing_template_decl
29696 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29697 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29698 incr = cp_parser_omp_for_incr (parser, real_decl);
29699 else
29700 incr = cp_parser_expression (parser);
29701 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29702 SET_EXPR_LOCATION (incr, input_location);
29705 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29706 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29707 /*or_comma=*/false,
29708 /*consume_paren=*/true);
29710 TREE_VEC_ELT (declv, i) = decl;
29711 TREE_VEC_ELT (initv, i) = init;
29712 TREE_VEC_ELT (condv, i) = cond;
29713 TREE_VEC_ELT (incrv, i) = incr;
29715 if (i == collapse - 1)
29716 break;
29718 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29719 in between the collapsed for loops to be still considered perfectly
29720 nested. Hopefully the final version clarifies this.
29721 For now handle (multiple) {'s and empty statements. */
29722 cp_parser_parse_tentatively (parser);
29725 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29726 break;
29727 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29729 cp_lexer_consume_token (parser->lexer);
29730 bracecount++;
29732 else if (bracecount
29733 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29734 cp_lexer_consume_token (parser->lexer);
29735 else
29737 loc = cp_lexer_peek_token (parser->lexer)->location;
29738 error_at (loc, "not enough collapsed for loops");
29739 collapse_err = true;
29740 cp_parser_abort_tentative_parse (parser);
29741 declv = NULL_TREE;
29742 break;
29745 while (1);
29747 if (declv)
29749 cp_parser_parse_definitely (parser);
29750 nbraces += bracecount;
29754 /* Note that we saved the original contents of this flag when we entered
29755 the structured block, and so we don't need to re-save it here. */
29756 if (code == CILK_SIMD || code == CILK_FOR)
29757 parser->in_statement = IN_CILK_SIMD_FOR;
29758 else
29759 parser->in_statement = IN_OMP_FOR;
29761 /* Note that the grammar doesn't call for a structured block here,
29762 though the loop as a whole is a structured block. */
29763 body = push_stmt_list ();
29764 cp_parser_statement (parser, NULL_TREE, false, NULL);
29765 body = pop_stmt_list (body);
29767 if (declv == NULL_TREE)
29768 ret = NULL_TREE;
29769 else
29770 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29771 pre_body, clauses);
29773 while (nbraces)
29775 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29777 cp_lexer_consume_token (parser->lexer);
29778 nbraces--;
29780 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29781 cp_lexer_consume_token (parser->lexer);
29782 else
29784 if (!collapse_err)
29786 error_at (cp_lexer_peek_token (parser->lexer)->location,
29787 "collapsed loops not perfectly nested");
29789 collapse_err = true;
29790 cp_parser_statement_seq_opt (parser, NULL);
29791 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29792 break;
29796 while (!for_block->is_empty ())
29797 add_stmt (pop_stmt_list (for_block->pop ()));
29798 release_tree_vector (for_block);
29800 return ret;
29803 /* Helper function for OpenMP parsing, split clauses and call
29804 finish_omp_clauses on each of the set of clauses afterwards. */
29806 static void
29807 cp_omp_split_clauses (location_t loc, enum tree_code code,
29808 omp_clause_mask mask, tree clauses, tree *cclauses)
29810 int i;
29811 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29812 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29813 if (cclauses[i])
29814 cclauses[i] = finish_omp_clauses (cclauses[i]);
29817 /* OpenMP 4.0:
29818 #pragma omp simd simd-clause[optseq] new-line
29819 for-loop */
29821 #define OMP_SIMD_CLAUSE_MASK \
29822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29830 static tree
29831 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29832 char *p_name, omp_clause_mask mask, tree *cclauses)
29834 tree clauses, sb, ret;
29835 unsigned int save;
29836 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29838 strcat (p_name, " simd");
29839 mask |= OMP_SIMD_CLAUSE_MASK;
29840 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29842 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29843 cclauses == NULL);
29844 if (cclauses)
29846 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29847 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29850 sb = begin_omp_structured_block ();
29851 save = cp_parser_begin_omp_structured_block (parser);
29853 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29855 cp_parser_end_omp_structured_block (parser, save);
29856 add_stmt (finish_omp_structured_block (sb));
29858 return ret;
29861 /* OpenMP 2.5:
29862 #pragma omp for for-clause[optseq] new-line
29863 for-loop
29865 OpenMP 4.0:
29866 #pragma omp for simd for-simd-clause[optseq] new-line
29867 for-loop */
29869 #define OMP_FOR_CLAUSE_MASK \
29870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29879 static tree
29880 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29881 char *p_name, omp_clause_mask mask, tree *cclauses)
29883 tree clauses, sb, ret;
29884 unsigned int save;
29885 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29887 strcat (p_name, " for");
29888 mask |= OMP_FOR_CLAUSE_MASK;
29889 if (cclauses)
29890 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29892 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29894 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29895 const char *p = IDENTIFIER_POINTER (id);
29897 if (strcmp (p, "simd") == 0)
29899 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29900 if (cclauses == NULL)
29901 cclauses = cclauses_buf;
29903 cp_lexer_consume_token (parser->lexer);
29904 if (!flag_openmp) /* flag_openmp_simd */
29905 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29906 cclauses);
29907 sb = begin_omp_structured_block ();
29908 save = cp_parser_begin_omp_structured_block (parser);
29909 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29910 cclauses);
29911 cp_parser_end_omp_structured_block (parser, save);
29912 tree body = finish_omp_structured_block (sb);
29913 if (ret == NULL)
29914 return ret;
29915 ret = make_node (OMP_FOR);
29916 TREE_TYPE (ret) = void_type_node;
29917 OMP_FOR_BODY (ret) = body;
29918 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29919 SET_EXPR_LOCATION (ret, loc);
29920 add_stmt (ret);
29921 return ret;
29924 if (!flag_openmp) /* flag_openmp_simd */
29926 cp_parser_require_pragma_eol (parser, pragma_tok);
29927 return NULL_TREE;
29930 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29931 cclauses == NULL);
29932 if (cclauses)
29934 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29935 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29938 sb = begin_omp_structured_block ();
29939 save = cp_parser_begin_omp_structured_block (parser);
29941 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29943 cp_parser_end_omp_structured_block (parser, save);
29944 add_stmt (finish_omp_structured_block (sb));
29946 return ret;
29949 /* OpenMP 2.5:
29950 # pragma omp master new-line
29951 structured-block */
29953 static tree
29954 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29956 cp_parser_require_pragma_eol (parser, pragma_tok);
29957 return c_finish_omp_master (input_location,
29958 cp_parser_omp_structured_block (parser));
29961 /* OpenMP 2.5:
29962 # pragma omp ordered new-line
29963 structured-block */
29965 static tree
29966 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29968 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29969 cp_parser_require_pragma_eol (parser, pragma_tok);
29970 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29973 /* OpenMP 2.5:
29975 section-scope:
29976 { section-sequence }
29978 section-sequence:
29979 section-directive[opt] structured-block
29980 section-sequence section-directive structured-block */
29982 static tree
29983 cp_parser_omp_sections_scope (cp_parser *parser)
29985 tree stmt, substmt;
29986 bool error_suppress = false;
29987 cp_token *tok;
29989 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29990 return NULL_TREE;
29992 stmt = push_stmt_list ();
29994 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29996 substmt = cp_parser_omp_structured_block (parser);
29997 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29998 add_stmt (substmt);
30001 while (1)
30003 tok = cp_lexer_peek_token (parser->lexer);
30004 if (tok->type == CPP_CLOSE_BRACE)
30005 break;
30006 if (tok->type == CPP_EOF)
30007 break;
30009 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30011 cp_lexer_consume_token (parser->lexer);
30012 cp_parser_require_pragma_eol (parser, tok);
30013 error_suppress = false;
30015 else if (!error_suppress)
30017 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30018 error_suppress = true;
30021 substmt = cp_parser_omp_structured_block (parser);
30022 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30023 add_stmt (substmt);
30025 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30027 substmt = pop_stmt_list (stmt);
30029 stmt = make_node (OMP_SECTIONS);
30030 TREE_TYPE (stmt) = void_type_node;
30031 OMP_SECTIONS_BODY (stmt) = substmt;
30033 add_stmt (stmt);
30034 return stmt;
30037 /* OpenMP 2.5:
30038 # pragma omp sections sections-clause[optseq] newline
30039 sections-scope */
30041 #define OMP_SECTIONS_CLAUSE_MASK \
30042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30048 static tree
30049 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30050 char *p_name, omp_clause_mask mask, tree *cclauses)
30052 tree clauses, ret;
30053 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30055 strcat (p_name, " sections");
30056 mask |= OMP_SECTIONS_CLAUSE_MASK;
30057 if (cclauses)
30058 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30060 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30061 cclauses == NULL);
30062 if (cclauses)
30064 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30065 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30068 ret = cp_parser_omp_sections_scope (parser);
30069 if (ret)
30070 OMP_SECTIONS_CLAUSES (ret) = clauses;
30072 return ret;
30075 /* OpenMP 2.5:
30076 # pragma omp parallel parallel-clause[optseq] new-line
30077 structured-block
30078 # pragma omp parallel for parallel-for-clause[optseq] new-line
30079 structured-block
30080 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30081 structured-block
30083 OpenMP 4.0:
30084 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30085 structured-block */
30087 #define OMP_PARALLEL_CLAUSE_MASK \
30088 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30098 static tree
30099 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30100 char *p_name, omp_clause_mask mask, tree *cclauses)
30102 tree stmt, clauses, block;
30103 unsigned int save;
30104 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30106 strcat (p_name, " parallel");
30107 mask |= OMP_PARALLEL_CLAUSE_MASK;
30109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30111 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30112 if (cclauses == NULL)
30113 cclauses = cclauses_buf;
30115 cp_lexer_consume_token (parser->lexer);
30116 if (!flag_openmp) /* flag_openmp_simd */
30117 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30118 block = begin_omp_parallel ();
30119 save = cp_parser_begin_omp_structured_block (parser);
30120 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30121 cp_parser_end_omp_structured_block (parser, save);
30122 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30123 block);
30124 if (ret == NULL_TREE)
30125 return ret;
30126 OMP_PARALLEL_COMBINED (stmt) = 1;
30127 return stmt;
30129 else if (cclauses)
30131 error_at (loc, "expected %<for%> after %qs", p_name);
30132 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30133 return NULL_TREE;
30135 else if (!flag_openmp) /* flag_openmp_simd */
30137 cp_parser_require_pragma_eol (parser, pragma_tok);
30138 return NULL_TREE;
30140 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30142 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30143 const char *p = IDENTIFIER_POINTER (id);
30144 if (strcmp (p, "sections") == 0)
30146 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30147 cclauses = cclauses_buf;
30149 cp_lexer_consume_token (parser->lexer);
30150 block = begin_omp_parallel ();
30151 save = cp_parser_begin_omp_structured_block (parser);
30152 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30153 cp_parser_end_omp_structured_block (parser, save);
30154 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30155 block);
30156 OMP_PARALLEL_COMBINED (stmt) = 1;
30157 return stmt;
30161 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30163 block = begin_omp_parallel ();
30164 save = cp_parser_begin_omp_structured_block (parser);
30165 cp_parser_statement (parser, NULL_TREE, false, NULL);
30166 cp_parser_end_omp_structured_block (parser, save);
30167 stmt = finish_omp_parallel (clauses, block);
30168 return stmt;
30171 /* OpenMP 2.5:
30172 # pragma omp single single-clause[optseq] new-line
30173 structured-block */
30175 #define OMP_SINGLE_CLAUSE_MASK \
30176 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30181 static tree
30182 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30184 tree stmt = make_node (OMP_SINGLE);
30185 TREE_TYPE (stmt) = void_type_node;
30187 OMP_SINGLE_CLAUSES (stmt)
30188 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30189 "#pragma omp single", pragma_tok);
30190 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30192 return add_stmt (stmt);
30195 /* OpenMP 3.0:
30196 # pragma omp task task-clause[optseq] new-line
30197 structured-block */
30199 #define OMP_TASK_CLAUSE_MASK \
30200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30210 static tree
30211 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30213 tree clauses, block;
30214 unsigned int save;
30216 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30217 "#pragma omp task", pragma_tok);
30218 block = begin_omp_task ();
30219 save = cp_parser_begin_omp_structured_block (parser);
30220 cp_parser_statement (parser, NULL_TREE, false, NULL);
30221 cp_parser_end_omp_structured_block (parser, save);
30222 return finish_omp_task (clauses, block);
30225 /* OpenMP 3.0:
30226 # pragma omp taskwait new-line */
30228 static void
30229 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30231 cp_parser_require_pragma_eol (parser, pragma_tok);
30232 finish_omp_taskwait ();
30235 /* OpenMP 3.1:
30236 # pragma omp taskyield new-line */
30238 static void
30239 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30241 cp_parser_require_pragma_eol (parser, pragma_tok);
30242 finish_omp_taskyield ();
30245 /* OpenMP 4.0:
30246 # pragma omp taskgroup new-line
30247 structured-block */
30249 static tree
30250 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30252 cp_parser_require_pragma_eol (parser, pragma_tok);
30253 return c_finish_omp_taskgroup (input_location,
30254 cp_parser_omp_structured_block (parser));
30258 /* OpenMP 2.5:
30259 # pragma omp threadprivate (variable-list) */
30261 static void
30262 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30264 tree vars;
30266 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30267 cp_parser_require_pragma_eol (parser, pragma_tok);
30269 finish_omp_threadprivate (vars);
30272 /* OpenMP 4.0:
30273 # pragma omp cancel cancel-clause[optseq] new-line */
30275 #define OMP_CANCEL_CLAUSE_MASK \
30276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30282 static void
30283 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30285 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30286 "#pragma omp cancel", pragma_tok);
30287 finish_omp_cancel (clauses);
30290 /* OpenMP 4.0:
30291 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30293 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30299 static void
30300 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30302 tree clauses;
30303 bool point_seen = false;
30305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30307 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30308 const char *p = IDENTIFIER_POINTER (id);
30310 if (strcmp (p, "point") == 0)
30312 cp_lexer_consume_token (parser->lexer);
30313 point_seen = true;
30316 if (!point_seen)
30318 cp_parser_error (parser, "expected %<point%>");
30319 cp_parser_require_pragma_eol (parser, pragma_tok);
30320 return;
30323 clauses = cp_parser_omp_all_clauses (parser,
30324 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30325 "#pragma omp cancellation point",
30326 pragma_tok);
30327 finish_omp_cancellation_point (clauses);
30330 /* OpenMP 4.0:
30331 #pragma omp distribute distribute-clause[optseq] new-line
30332 for-loop */
30334 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30340 static tree
30341 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30342 char *p_name, omp_clause_mask mask, tree *cclauses)
30344 tree clauses, sb, ret;
30345 unsigned int save;
30346 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30348 strcat (p_name, " distribute");
30349 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30353 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30354 const char *p = IDENTIFIER_POINTER (id);
30355 bool simd = false;
30356 bool parallel = false;
30358 if (strcmp (p, "simd") == 0)
30359 simd = true;
30360 else
30361 parallel = strcmp (p, "parallel") == 0;
30362 if (parallel || simd)
30364 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30365 if (cclauses == NULL)
30366 cclauses = cclauses_buf;
30367 cp_lexer_consume_token (parser->lexer);
30368 if (!flag_openmp) /* flag_openmp_simd */
30370 if (simd)
30371 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30372 cclauses);
30373 else
30374 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30375 cclauses);
30377 sb = begin_omp_structured_block ();
30378 save = cp_parser_begin_omp_structured_block (parser);
30379 if (simd)
30380 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30381 cclauses);
30382 else
30383 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30384 cclauses);
30385 cp_parser_end_omp_structured_block (parser, save);
30386 tree body = finish_omp_structured_block (sb);
30387 if (ret == NULL)
30388 return ret;
30389 ret = make_node (OMP_DISTRIBUTE);
30390 TREE_TYPE (ret) = void_type_node;
30391 OMP_FOR_BODY (ret) = body;
30392 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30393 SET_EXPR_LOCATION (ret, loc);
30394 add_stmt (ret);
30395 return ret;
30398 if (!flag_openmp) /* flag_openmp_simd */
30400 cp_parser_require_pragma_eol (parser, pragma_tok);
30401 return NULL_TREE;
30404 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30405 cclauses == NULL);
30406 if (cclauses)
30408 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30409 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30412 sb = begin_omp_structured_block ();
30413 save = cp_parser_begin_omp_structured_block (parser);
30415 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30417 cp_parser_end_omp_structured_block (parser, save);
30418 add_stmt (finish_omp_structured_block (sb));
30420 return ret;
30423 /* OpenMP 4.0:
30424 # pragma omp teams teams-clause[optseq] new-line
30425 structured-block */
30427 #define OMP_TEAMS_CLAUSE_MASK \
30428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30436 static tree
30437 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30438 char *p_name, omp_clause_mask mask, tree *cclauses)
30440 tree clauses, sb, ret;
30441 unsigned int save;
30442 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30444 strcat (p_name, " teams");
30445 mask |= OMP_TEAMS_CLAUSE_MASK;
30447 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30449 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30450 const char *p = IDENTIFIER_POINTER (id);
30451 if (strcmp (p, "distribute") == 0)
30453 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30454 if (cclauses == NULL)
30455 cclauses = cclauses_buf;
30457 cp_lexer_consume_token (parser->lexer);
30458 if (!flag_openmp) /* flag_openmp_simd */
30459 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30460 cclauses);
30461 sb = begin_omp_structured_block ();
30462 save = cp_parser_begin_omp_structured_block (parser);
30463 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30464 cclauses);
30465 cp_parser_end_omp_structured_block (parser, save);
30466 tree body = finish_omp_structured_block (sb);
30467 if (ret == NULL)
30468 return ret;
30469 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30470 ret = make_node (OMP_TEAMS);
30471 TREE_TYPE (ret) = void_type_node;
30472 OMP_TEAMS_CLAUSES (ret) = clauses;
30473 OMP_TEAMS_BODY (ret) = body;
30474 return add_stmt (ret);
30477 if (!flag_openmp) /* flag_openmp_simd */
30479 cp_parser_require_pragma_eol (parser, pragma_tok);
30480 return NULL_TREE;
30483 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30484 cclauses == NULL);
30485 if (cclauses)
30487 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30488 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30491 tree stmt = make_node (OMP_TEAMS);
30492 TREE_TYPE (stmt) = void_type_node;
30493 OMP_TEAMS_CLAUSES (stmt) = clauses;
30494 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30496 return add_stmt (stmt);
30499 /* OpenMP 4.0:
30500 # pragma omp target data target-data-clause[optseq] new-line
30501 structured-block */
30503 #define OMP_TARGET_DATA_CLAUSE_MASK \
30504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30508 static tree
30509 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30511 tree stmt = make_node (OMP_TARGET_DATA);
30512 TREE_TYPE (stmt) = void_type_node;
30514 OMP_TARGET_DATA_CLAUSES (stmt)
30515 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30516 "#pragma omp target data", pragma_tok);
30517 keep_next_level (true);
30518 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30520 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30521 return add_stmt (stmt);
30524 /* OpenMP 4.0:
30525 # pragma omp target update target-update-clause[optseq] new-line */
30527 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30528 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30533 static bool
30534 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30535 enum pragma_context context)
30537 if (context == pragma_stmt)
30539 error_at (pragma_tok->location,
30540 "%<#pragma omp target update%> may only be "
30541 "used in compound statements");
30542 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30543 return false;
30546 tree clauses
30547 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30548 "#pragma omp target update", pragma_tok);
30549 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30550 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30552 error_at (pragma_tok->location,
30553 "%<#pragma omp target update must contain at least one "
30554 "%<from%> or %<to%> clauses");
30555 return false;
30558 tree stmt = make_node (OMP_TARGET_UPDATE);
30559 TREE_TYPE (stmt) = void_type_node;
30560 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30561 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30562 add_stmt (stmt);
30563 return false;
30566 /* OpenMP 4.0:
30567 # pragma omp target target-clause[optseq] new-line
30568 structured-block */
30570 #define OMP_TARGET_CLAUSE_MASK \
30571 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30575 static bool
30576 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30577 enum pragma_context context)
30579 if (context != pragma_stmt && context != pragma_compound)
30581 cp_parser_error (parser, "expected declaration specifiers");
30582 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30583 return false;
30586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30589 const char *p = IDENTIFIER_POINTER (id);
30591 if (strcmp (p, "teams") == 0)
30593 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30594 char p_name[sizeof ("#pragma omp target teams distribute "
30595 "parallel for simd")];
30597 cp_lexer_consume_token (parser->lexer);
30598 strcpy (p_name, "#pragma omp target");
30599 if (!flag_openmp) /* flag_openmp_simd */
30601 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30602 OMP_TARGET_CLAUSE_MASK,
30603 cclauses);
30604 return stmt != NULL_TREE;
30606 keep_next_level (true);
30607 tree sb = begin_omp_structured_block ();
30608 unsigned save = cp_parser_begin_omp_structured_block (parser);
30609 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30610 OMP_TARGET_CLAUSE_MASK, cclauses);
30611 cp_parser_end_omp_structured_block (parser, save);
30612 tree body = finish_omp_structured_block (sb);
30613 if (ret == NULL_TREE)
30614 return false;
30615 tree stmt = make_node (OMP_TARGET);
30616 TREE_TYPE (stmt) = void_type_node;
30617 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30618 OMP_TARGET_BODY (stmt) = body;
30619 add_stmt (stmt);
30620 return true;
30622 else if (!flag_openmp) /* flag_openmp_simd */
30624 cp_parser_require_pragma_eol (parser, pragma_tok);
30625 return false;
30627 else if (strcmp (p, "data") == 0)
30629 cp_lexer_consume_token (parser->lexer);
30630 cp_parser_omp_target_data (parser, pragma_tok);
30631 return true;
30633 else if (strcmp (p, "update") == 0)
30635 cp_lexer_consume_token (parser->lexer);
30636 return cp_parser_omp_target_update (parser, pragma_tok, context);
30640 tree stmt = make_node (OMP_TARGET);
30641 TREE_TYPE (stmt) = void_type_node;
30643 OMP_TARGET_CLAUSES (stmt)
30644 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30645 "#pragma omp target", pragma_tok);
30646 keep_next_level (true);
30647 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30649 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30650 add_stmt (stmt);
30651 return true;
30654 /* OpenMP 4.0:
30655 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30657 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30665 static void
30666 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30667 enum pragma_context context)
30669 bool first_p = parser->omp_declare_simd == NULL;
30670 cp_omp_declare_simd_data data;
30671 if (first_p)
30673 data.error_seen = false;
30674 data.fndecl_seen = false;
30675 data.tokens = vNULL;
30676 parser->omp_declare_simd = &data;
30678 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30679 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30680 cp_lexer_consume_token (parser->lexer);
30681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30682 parser->omp_declare_simd->error_seen = true;
30683 cp_parser_require_pragma_eol (parser, pragma_tok);
30684 struct cp_token_cache *cp
30685 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30686 parser->omp_declare_simd->tokens.safe_push (cp);
30687 if (first_p)
30689 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30690 cp_parser_pragma (parser, context);
30691 switch (context)
30693 case pragma_external:
30694 cp_parser_declaration (parser);
30695 break;
30696 case pragma_member:
30697 cp_parser_member_declaration (parser);
30698 break;
30699 case pragma_objc_icode:
30700 cp_parser_block_declaration (parser, /*statement_p=*/false);
30701 break;
30702 default:
30703 cp_parser_declaration_statement (parser);
30704 break;
30706 if (parser->omp_declare_simd
30707 && !parser->omp_declare_simd->error_seen
30708 && !parser->omp_declare_simd->fndecl_seen)
30709 error_at (pragma_tok->location,
30710 "%<#pragma omp declare simd%> not immediately followed by "
30711 "function declaration or definition");
30712 data.tokens.release ();
30713 parser->omp_declare_simd = NULL;
30717 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30718 This function is modelled similar to the late parsing of omp declare
30719 simd. */
30721 static tree
30722 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30724 struct cp_token_cache *ce;
30725 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30726 int ii = 0;
30728 if (parser->omp_declare_simd != NULL)
30730 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30731 " marked as a Cilk Plus SIMD-enabled function");
30732 XDELETE (parser->cilk_simd_fn_info);
30733 parser->cilk_simd_fn_info = NULL;
30734 return attrs;
30736 if (!info->error_seen && info->fndecl_seen)
30738 error ("vector attribute not immediately followed by a single function"
30739 " declaration or definition");
30740 info->error_seen = true;
30742 if (info->error_seen)
30743 return attrs;
30745 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30747 tree c, cl;
30749 cp_parser_push_lexer_for_tokens (parser, ce);
30750 parser->lexer->in_pragma = true;
30751 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30752 "SIMD-enabled functions attribute",
30753 NULL);
30754 cp_parser_pop_lexer (parser);
30755 if (cl)
30756 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30758 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30759 TREE_CHAIN (c) = attrs;
30760 attrs = c;
30762 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30763 TREE_CHAIN (c) = attrs;
30764 if (processing_template_decl)
30765 ATTR_IS_DEPENDENT (c) = 1;
30766 attrs = c;
30768 info->fndecl_seen = true;
30769 XDELETE (parser->cilk_simd_fn_info);
30770 parser->cilk_simd_fn_info = NULL;
30771 return attrs;
30774 /* Finalize #pragma omp declare simd clauses after direct declarator has
30775 been parsed, and put that into "omp declare simd" attribute. */
30777 static tree
30778 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30780 struct cp_token_cache *ce;
30781 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30782 int i;
30784 if (!data->error_seen && data->fndecl_seen)
30786 error ("%<#pragma omp declare simd%> not immediately followed by "
30787 "a single function declaration or definition");
30788 data->error_seen = true;
30789 return attrs;
30791 if (data->error_seen)
30792 return attrs;
30794 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30796 tree c, cl;
30798 cp_parser_push_lexer_for_tokens (parser, ce);
30799 parser->lexer->in_pragma = true;
30800 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30801 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30802 cp_lexer_consume_token (parser->lexer);
30803 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30804 "#pragma omp declare simd", pragma_tok);
30805 cp_parser_pop_lexer (parser);
30806 if (cl)
30807 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30808 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30809 TREE_CHAIN (c) = attrs;
30810 if (processing_template_decl)
30811 ATTR_IS_DEPENDENT (c) = 1;
30812 attrs = c;
30815 data->fndecl_seen = true;
30816 return attrs;
30820 /* OpenMP 4.0:
30821 # pragma omp declare target new-line
30822 declarations and definitions
30823 # pragma omp end declare target new-line */
30825 static void
30826 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30828 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30829 scope_chain->omp_declare_target_attribute++;
30832 static void
30833 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30835 const char *p = "";
30836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30839 p = IDENTIFIER_POINTER (id);
30841 if (strcmp (p, "declare") == 0)
30843 cp_lexer_consume_token (parser->lexer);
30844 p = "";
30845 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30848 p = IDENTIFIER_POINTER (id);
30850 if (strcmp (p, "target") == 0)
30851 cp_lexer_consume_token (parser->lexer);
30852 else
30854 cp_parser_error (parser, "expected %<target%>");
30855 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30856 return;
30859 else
30861 cp_parser_error (parser, "expected %<declare%>");
30862 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30863 return;
30865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30866 if (!scope_chain->omp_declare_target_attribute)
30867 error_at (pragma_tok->location,
30868 "%<#pragma omp end declare target%> without corresponding "
30869 "%<#pragma omp declare target%>");
30870 else
30871 scope_chain->omp_declare_target_attribute--;
30874 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30875 expression and optional initializer clause of
30876 #pragma omp declare reduction. We store the expression(s) as
30877 either 3, 6 or 7 special statements inside of the artificial function's
30878 body. The first two statements are DECL_EXPRs for the artificial
30879 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30880 expression that uses those variables.
30881 If there was any INITIALIZER clause, this is followed by further statements,
30882 the fourth and fifth statements are DECL_EXPRs for the artificial
30883 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30884 constructor variant (first token after open paren is not omp_priv),
30885 then the sixth statement is a statement with the function call expression
30886 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30887 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30888 to initialize the OMP_PRIV artificial variable and there is seventh
30889 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30891 static bool
30892 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30894 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30895 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30896 type = TREE_TYPE (type);
30897 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30898 DECL_ARTIFICIAL (omp_out) = 1;
30899 pushdecl (omp_out);
30900 add_decl_expr (omp_out);
30901 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30902 DECL_ARTIFICIAL (omp_in) = 1;
30903 pushdecl (omp_in);
30904 add_decl_expr (omp_in);
30905 tree combiner;
30906 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30908 keep_next_level (true);
30909 tree block = begin_omp_structured_block ();
30910 combiner = cp_parser_expression (parser);
30911 finish_expr_stmt (combiner);
30912 block = finish_omp_structured_block (block);
30913 add_stmt (block);
30915 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30916 return false;
30918 const char *p = "";
30919 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30921 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30922 p = IDENTIFIER_POINTER (id);
30925 if (strcmp (p, "initializer") == 0)
30927 cp_lexer_consume_token (parser->lexer);
30928 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30929 return false;
30931 p = "";
30932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30934 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30935 p = IDENTIFIER_POINTER (id);
30938 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30939 DECL_ARTIFICIAL (omp_priv) = 1;
30940 pushdecl (omp_priv);
30941 add_decl_expr (omp_priv);
30942 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30943 DECL_ARTIFICIAL (omp_orig) = 1;
30944 pushdecl (omp_orig);
30945 add_decl_expr (omp_orig);
30947 keep_next_level (true);
30948 block = begin_omp_structured_block ();
30950 bool ctor = false;
30951 if (strcmp (p, "omp_priv") == 0)
30953 bool is_direct_init, is_non_constant_init;
30954 ctor = true;
30955 cp_lexer_consume_token (parser->lexer);
30956 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30957 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30958 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30959 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30960 == CPP_CLOSE_PAREN
30961 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30962 == CPP_CLOSE_PAREN))
30964 finish_omp_structured_block (block);
30965 error ("invalid initializer clause");
30966 return false;
30968 initializer = cp_parser_initializer (parser, &is_direct_init,
30969 &is_non_constant_init);
30970 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30971 NULL_TREE, LOOKUP_ONLYCONVERTING);
30973 else
30975 cp_parser_parse_tentatively (parser);
30976 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30977 /*check_dependency_p=*/true,
30978 /*template_p=*/NULL,
30979 /*declarator_p=*/false,
30980 /*optional_p=*/false);
30981 vec<tree, va_gc> *args;
30982 if (fn_name == error_mark_node
30983 || cp_parser_error_occurred (parser)
30984 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30985 || ((args = cp_parser_parenthesized_expression_list
30986 (parser, non_attr, /*cast_p=*/false,
30987 /*allow_expansion_p=*/true,
30988 /*non_constant_p=*/NULL)),
30989 cp_parser_error_occurred (parser)))
30991 finish_omp_structured_block (block);
30992 cp_parser_abort_tentative_parse (parser);
30993 cp_parser_error (parser, "expected id-expression (arguments)");
30994 return false;
30996 unsigned int i;
30997 tree arg;
30998 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30999 if (arg == omp_priv
31000 || (TREE_CODE (arg) == ADDR_EXPR
31001 && TREE_OPERAND (arg, 0) == omp_priv))
31002 break;
31003 cp_parser_abort_tentative_parse (parser);
31004 if (arg == NULL_TREE)
31005 error ("one of the initializer call arguments should be %<omp_priv%>"
31006 " or %<&omp_priv%>");
31007 initializer = cp_parser_postfix_expression (parser, false, false, false,
31008 false, NULL);
31009 finish_expr_stmt (initializer);
31012 block = finish_omp_structured_block (block);
31013 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31014 finish_expr_stmt (block);
31016 if (ctor)
31017 add_decl_expr (omp_orig);
31019 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31020 return false;
31023 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31024 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31026 return true;
31029 /* OpenMP 4.0
31030 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31031 initializer-clause[opt] new-line
31033 initializer-clause:
31034 initializer (omp_priv initializer)
31035 initializer (function-name (argument-list)) */
31037 static void
31038 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31039 enum pragma_context)
31041 auto_vec<tree> types;
31042 enum tree_code reduc_code = ERROR_MARK;
31043 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31044 unsigned int i;
31045 cp_token *first_token;
31046 cp_token_cache *cp;
31047 int errs;
31048 void *p;
31050 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31051 p = obstack_alloc (&declarator_obstack, 0);
31053 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31054 goto fail;
31056 switch (cp_lexer_peek_token (parser->lexer)->type)
31058 case CPP_PLUS:
31059 reduc_code = PLUS_EXPR;
31060 break;
31061 case CPP_MULT:
31062 reduc_code = MULT_EXPR;
31063 break;
31064 case CPP_MINUS:
31065 reduc_code = MINUS_EXPR;
31066 break;
31067 case CPP_AND:
31068 reduc_code = BIT_AND_EXPR;
31069 break;
31070 case CPP_XOR:
31071 reduc_code = BIT_XOR_EXPR;
31072 break;
31073 case CPP_OR:
31074 reduc_code = BIT_IOR_EXPR;
31075 break;
31076 case CPP_AND_AND:
31077 reduc_code = TRUTH_ANDIF_EXPR;
31078 break;
31079 case CPP_OR_OR:
31080 reduc_code = TRUTH_ORIF_EXPR;
31081 break;
31082 case CPP_NAME:
31083 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31084 break;
31085 default:
31086 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31087 "%<|%>, %<&&%>, %<||%> or identifier");
31088 goto fail;
31091 if (reduc_code != ERROR_MARK)
31092 cp_lexer_consume_token (parser->lexer);
31094 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31095 if (reduc_id == error_mark_node)
31096 goto fail;
31098 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31099 goto fail;
31101 /* Types may not be defined in declare reduction type list. */
31102 const char *saved_message;
31103 saved_message = parser->type_definition_forbidden_message;
31104 parser->type_definition_forbidden_message
31105 = G_("types may not be defined in declare reduction type list");
31106 bool saved_colon_corrects_to_scope_p;
31107 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31108 parser->colon_corrects_to_scope_p = false;
31109 bool saved_colon_doesnt_start_class_def_p;
31110 saved_colon_doesnt_start_class_def_p
31111 = parser->colon_doesnt_start_class_def_p;
31112 parser->colon_doesnt_start_class_def_p = true;
31114 while (true)
31116 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31117 type = cp_parser_type_id (parser);
31118 if (type == error_mark_node)
31120 else if (ARITHMETIC_TYPE_P (type)
31121 && (orig_reduc_id == NULL_TREE
31122 || (TREE_CODE (type) != COMPLEX_TYPE
31123 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31124 "min") == 0
31125 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31126 "max") == 0))))
31127 error_at (loc, "predeclared arithmetic type %qT in "
31128 "%<#pragma omp declare reduction%>", type);
31129 else if (TREE_CODE (type) == FUNCTION_TYPE
31130 || TREE_CODE (type) == METHOD_TYPE
31131 || TREE_CODE (type) == ARRAY_TYPE)
31132 error_at (loc, "function or array type %qT in "
31133 "%<#pragma omp declare reduction%>", type);
31134 else if (TREE_CODE (type) == REFERENCE_TYPE)
31135 error_at (loc, "reference type %qT in "
31136 "%<#pragma omp declare reduction%>", type);
31137 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31138 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31139 "%<#pragma omp declare reduction%>", type);
31140 else
31141 types.safe_push (type);
31143 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31144 cp_lexer_consume_token (parser->lexer);
31145 else
31146 break;
31149 /* Restore the saved message. */
31150 parser->type_definition_forbidden_message = saved_message;
31151 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31152 parser->colon_doesnt_start_class_def_p
31153 = saved_colon_doesnt_start_class_def_p;
31155 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31156 || types.is_empty ())
31158 fail:
31159 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31160 goto done;
31163 first_token = cp_lexer_peek_token (parser->lexer);
31164 cp = NULL;
31165 errs = errorcount;
31166 FOR_EACH_VEC_ELT (types, i, type)
31168 tree fntype
31169 = build_function_type_list (void_type_node,
31170 cp_build_reference_type (type, false),
31171 NULL_TREE);
31172 tree this_reduc_id = reduc_id;
31173 if (!dependent_type_p (type))
31174 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31175 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31176 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31177 DECL_ARTIFICIAL (fndecl) = 1;
31178 DECL_EXTERNAL (fndecl) = 1;
31179 DECL_DECLARED_INLINE_P (fndecl) = 1;
31180 DECL_IGNORED_P (fndecl) = 1;
31181 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31182 DECL_ATTRIBUTES (fndecl)
31183 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31184 DECL_ATTRIBUTES (fndecl));
31185 if (processing_template_decl)
31186 fndecl = push_template_decl (fndecl);
31187 bool block_scope = false;
31188 tree block = NULL_TREE;
31189 if (current_function_decl)
31191 block_scope = true;
31192 DECL_CONTEXT (fndecl) = global_namespace;
31193 if (!processing_template_decl)
31194 pushdecl (fndecl);
31196 else if (current_class_type)
31198 if (cp == NULL)
31200 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31201 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31202 cp_lexer_consume_token (parser->lexer);
31203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31204 goto fail;
31205 cp = cp_token_cache_new (first_token,
31206 cp_lexer_peek_nth_token (parser->lexer,
31207 2));
31209 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31210 finish_member_declaration (fndecl);
31211 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31212 DECL_PENDING_INLINE_P (fndecl) = 1;
31213 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31214 continue;
31216 else
31218 DECL_CONTEXT (fndecl) = current_namespace;
31219 pushdecl (fndecl);
31221 if (!block_scope)
31222 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31223 else
31224 block = begin_omp_structured_block ();
31225 if (cp)
31227 cp_parser_push_lexer_for_tokens (parser, cp);
31228 parser->lexer->in_pragma = true;
31230 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31232 if (!block_scope)
31233 finish_function (0);
31234 else
31235 DECL_CONTEXT (fndecl) = current_function_decl;
31236 if (cp)
31237 cp_parser_pop_lexer (parser);
31238 goto fail;
31240 if (cp)
31241 cp_parser_pop_lexer (parser);
31242 if (!block_scope)
31243 finish_function (0);
31244 else
31246 DECL_CONTEXT (fndecl) = current_function_decl;
31247 block = finish_omp_structured_block (block);
31248 if (TREE_CODE (block) == BIND_EXPR)
31249 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31250 else if (TREE_CODE (block) == STATEMENT_LIST)
31251 DECL_SAVED_TREE (fndecl) = block;
31252 if (processing_template_decl)
31253 add_decl_expr (fndecl);
31255 cp_check_omp_declare_reduction (fndecl);
31256 if (cp == NULL && types.length () > 1)
31257 cp = cp_token_cache_new (first_token,
31258 cp_lexer_peek_nth_token (parser->lexer, 2));
31259 if (errs != errorcount)
31260 break;
31263 cp_parser_require_pragma_eol (parser, pragma_tok);
31265 done:
31266 /* Free any declarators allocated. */
31267 obstack_free (&declarator_obstack, p);
31270 /* OpenMP 4.0
31271 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31272 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31273 initializer-clause[opt] new-line
31274 #pragma omp declare target new-line */
31276 static void
31277 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31278 enum pragma_context context)
31280 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31282 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31283 const char *p = IDENTIFIER_POINTER (id);
31285 if (strcmp (p, "simd") == 0)
31287 cp_lexer_consume_token (parser->lexer);
31288 cp_parser_omp_declare_simd (parser, pragma_tok,
31289 context);
31290 return;
31292 cp_ensure_no_omp_declare_simd (parser);
31293 if (strcmp (p, "reduction") == 0)
31295 cp_lexer_consume_token (parser->lexer);
31296 cp_parser_omp_declare_reduction (parser, pragma_tok,
31297 context);
31298 return;
31300 if (!flag_openmp) /* flag_openmp_simd */
31302 cp_parser_require_pragma_eol (parser, pragma_tok);
31303 return;
31305 if (strcmp (p, "target") == 0)
31307 cp_lexer_consume_token (parser->lexer);
31308 cp_parser_omp_declare_target (parser, pragma_tok);
31309 return;
31312 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31313 "or %<target%>");
31314 cp_parser_require_pragma_eol (parser, pragma_tok);
31317 /* Main entry point to OpenMP statement pragmas. */
31319 static void
31320 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31322 tree stmt;
31323 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31324 omp_clause_mask mask (0);
31326 switch (pragma_tok->pragma_kind)
31328 case PRAGMA_OMP_ATOMIC:
31329 cp_parser_omp_atomic (parser, pragma_tok);
31330 return;
31331 case PRAGMA_OMP_CRITICAL:
31332 stmt = cp_parser_omp_critical (parser, pragma_tok);
31333 break;
31334 case PRAGMA_OMP_DISTRIBUTE:
31335 strcpy (p_name, "#pragma omp");
31336 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31337 break;
31338 case PRAGMA_OMP_FOR:
31339 strcpy (p_name, "#pragma omp");
31340 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31341 break;
31342 case PRAGMA_OMP_MASTER:
31343 stmt = cp_parser_omp_master (parser, pragma_tok);
31344 break;
31345 case PRAGMA_OMP_ORDERED:
31346 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31347 break;
31348 case PRAGMA_OMP_PARALLEL:
31349 strcpy (p_name, "#pragma omp");
31350 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31351 break;
31352 case PRAGMA_OMP_SECTIONS:
31353 strcpy (p_name, "#pragma omp");
31354 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31355 break;
31356 case PRAGMA_OMP_SIMD:
31357 strcpy (p_name, "#pragma omp");
31358 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31359 break;
31360 case PRAGMA_OMP_SINGLE:
31361 stmt = cp_parser_omp_single (parser, pragma_tok);
31362 break;
31363 case PRAGMA_OMP_TASK:
31364 stmt = cp_parser_omp_task (parser, pragma_tok);
31365 break;
31366 case PRAGMA_OMP_TASKGROUP:
31367 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31368 break;
31369 case PRAGMA_OMP_TEAMS:
31370 strcpy (p_name, "#pragma omp");
31371 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31372 break;
31373 default:
31374 gcc_unreachable ();
31377 if (stmt)
31378 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31381 /* Transactional Memory parsing routines. */
31383 /* Parse a transaction attribute.
31385 txn-attribute:
31386 attribute
31387 [ [ identifier ] ]
31389 ??? Simplify this when C++0x bracket attributes are
31390 implemented properly. */
31392 static tree
31393 cp_parser_txn_attribute_opt (cp_parser *parser)
31395 cp_token *token;
31396 tree attr_name, attr = NULL;
31398 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31399 return cp_parser_attributes_opt (parser);
31401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31402 return NULL_TREE;
31403 cp_lexer_consume_token (parser->lexer);
31404 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31405 goto error1;
31407 token = cp_lexer_peek_token (parser->lexer);
31408 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31410 token = cp_lexer_consume_token (parser->lexer);
31412 attr_name = (token->type == CPP_KEYWORD
31413 /* For keywords, use the canonical spelling,
31414 not the parsed identifier. */
31415 ? ridpointers[(int) token->keyword]
31416 : token->u.value);
31417 attr = build_tree_list (attr_name, NULL_TREE);
31419 else
31420 cp_parser_error (parser, "expected identifier");
31422 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31423 error1:
31424 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31425 return attr;
31428 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31430 transaction-statement:
31431 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31432 compound-statement
31433 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31436 static tree
31437 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31439 unsigned char old_in = parser->in_transaction;
31440 unsigned char this_in = 1, new_in;
31441 cp_token *token;
31442 tree stmt, attrs, noex;
31444 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31445 || keyword == RID_TRANSACTION_RELAXED);
31446 token = cp_parser_require_keyword (parser, keyword,
31447 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31448 : RT_TRANSACTION_RELAXED));
31449 gcc_assert (token != NULL);
31451 if (keyword == RID_TRANSACTION_RELAXED)
31452 this_in |= TM_STMT_ATTR_RELAXED;
31453 else
31455 attrs = cp_parser_txn_attribute_opt (parser);
31456 if (attrs)
31457 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31460 /* Parse a noexcept specification. */
31461 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31463 /* Keep track if we're in the lexical scope of an outer transaction. */
31464 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31466 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31468 parser->in_transaction = new_in;
31469 cp_parser_compound_statement (parser, NULL, false, false);
31470 parser->in_transaction = old_in;
31472 finish_transaction_stmt (stmt, NULL, this_in, noex);
31474 return stmt;
31477 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31479 transaction-expression:
31480 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31481 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31484 static tree
31485 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31487 unsigned char old_in = parser->in_transaction;
31488 unsigned char this_in = 1;
31489 cp_token *token;
31490 tree expr, noex;
31491 bool noex_expr;
31493 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31494 || keyword == RID_TRANSACTION_RELAXED);
31496 if (!flag_tm)
31497 error (keyword == RID_TRANSACTION_RELAXED
31498 ? G_("%<__transaction_relaxed%> without transactional memory "
31499 "support enabled")
31500 : G_("%<__transaction_atomic%> without transactional memory "
31501 "support enabled"));
31503 token = cp_parser_require_keyword (parser, keyword,
31504 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31505 : RT_TRANSACTION_RELAXED));
31506 gcc_assert (token != NULL);
31508 if (keyword == RID_TRANSACTION_RELAXED)
31509 this_in |= TM_STMT_ATTR_RELAXED;
31511 /* Set this early. This might mean that we allow transaction_cancel in
31512 an expression that we find out later actually has to be a constexpr.
31513 However, we expect that cxx_constant_value will be able to deal with
31514 this; also, if the noexcept has no constexpr, then what we parse next
31515 really is a transaction's body. */
31516 parser->in_transaction = this_in;
31518 /* Parse a noexcept specification. */
31519 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31520 true);
31522 if (!noex || !noex_expr
31523 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31525 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31527 expr = cp_parser_expression (parser);
31528 expr = finish_parenthesized_expr (expr);
31530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31532 else
31534 /* The only expression that is available got parsed for the noexcept
31535 already. noexcept is true then. */
31536 expr = noex;
31537 noex = boolean_true_node;
31540 expr = build_transaction_expr (token->location, expr, this_in, noex);
31541 parser->in_transaction = old_in;
31543 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31544 return error_mark_node;
31546 return (flag_tm ? expr : error_mark_node);
31549 /* Parse a function-transaction-block.
31551 function-transaction-block:
31552 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31553 function-body
31554 __transaction_atomic txn-attribute[opt] function-try-block
31555 __transaction_relaxed ctor-initializer[opt] function-body
31556 __transaction_relaxed function-try-block
31559 static bool
31560 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31562 unsigned char old_in = parser->in_transaction;
31563 unsigned char new_in = 1;
31564 tree compound_stmt, stmt, attrs;
31565 bool ctor_initializer_p;
31566 cp_token *token;
31568 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31569 || keyword == RID_TRANSACTION_RELAXED);
31570 token = cp_parser_require_keyword (parser, keyword,
31571 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31572 : RT_TRANSACTION_RELAXED));
31573 gcc_assert (token != NULL);
31575 if (keyword == RID_TRANSACTION_RELAXED)
31576 new_in |= TM_STMT_ATTR_RELAXED;
31577 else
31579 attrs = cp_parser_txn_attribute_opt (parser);
31580 if (attrs)
31581 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31584 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31586 parser->in_transaction = new_in;
31588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31589 ctor_initializer_p = cp_parser_function_try_block (parser);
31590 else
31591 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31592 (parser, /*in_function_try_block=*/false);
31594 parser->in_transaction = old_in;
31596 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31598 return ctor_initializer_p;
31601 /* Parse a __transaction_cancel statement.
31603 cancel-statement:
31604 __transaction_cancel txn-attribute[opt] ;
31605 __transaction_cancel txn-attribute[opt] throw-expression ;
31607 ??? Cancel and throw is not yet implemented. */
31609 static tree
31610 cp_parser_transaction_cancel (cp_parser *parser)
31612 cp_token *token;
31613 bool is_outer = false;
31614 tree stmt, attrs;
31616 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31617 RT_TRANSACTION_CANCEL);
31618 gcc_assert (token != NULL);
31620 attrs = cp_parser_txn_attribute_opt (parser);
31621 if (attrs)
31622 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31624 /* ??? Parse cancel-and-throw here. */
31626 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31628 if (!flag_tm)
31630 error_at (token->location, "%<__transaction_cancel%> without "
31631 "transactional memory support enabled");
31632 return error_mark_node;
31634 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31636 error_at (token->location, "%<__transaction_cancel%> within a "
31637 "%<__transaction_relaxed%>");
31638 return error_mark_node;
31640 else if (is_outer)
31642 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31643 && !is_tm_may_cancel_outer (current_function_decl))
31645 error_at (token->location, "outer %<__transaction_cancel%> not "
31646 "within outer %<__transaction_atomic%>");
31647 error_at (token->location,
31648 " or a %<transaction_may_cancel_outer%> function");
31649 return error_mark_node;
31652 else if (parser->in_transaction == 0)
31654 error_at (token->location, "%<__transaction_cancel%> not within "
31655 "%<__transaction_atomic%>");
31656 return error_mark_node;
31659 stmt = build_tm_abort_call (token->location, is_outer);
31660 add_stmt (stmt);
31662 return stmt;
31665 /* The parser. */
31667 static GTY (()) cp_parser *the_parser;
31670 /* Special handling for the first token or line in the file. The first
31671 thing in the file might be #pragma GCC pch_preprocess, which loads a
31672 PCH file, which is a GC collection point. So we need to handle this
31673 first pragma without benefit of an existing lexer structure.
31675 Always returns one token to the caller in *FIRST_TOKEN. This is
31676 either the true first token of the file, or the first token after
31677 the initial pragma. */
31679 static void
31680 cp_parser_initial_pragma (cp_token *first_token)
31682 tree name = NULL;
31684 cp_lexer_get_preprocessor_token (NULL, first_token);
31685 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31686 return;
31688 cp_lexer_get_preprocessor_token (NULL, first_token);
31689 if (first_token->type == CPP_STRING)
31691 name = first_token->u.value;
31693 cp_lexer_get_preprocessor_token (NULL, first_token);
31694 if (first_token->type != CPP_PRAGMA_EOL)
31695 error_at (first_token->location,
31696 "junk at end of %<#pragma GCC pch_preprocess%>");
31698 else
31699 error_at (first_token->location, "expected string literal");
31701 /* Skip to the end of the pragma. */
31702 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31703 cp_lexer_get_preprocessor_token (NULL, first_token);
31705 /* Now actually load the PCH file. */
31706 if (name)
31707 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31709 /* Read one more token to return to our caller. We have to do this
31710 after reading the PCH file in, since its pointers have to be
31711 live. */
31712 cp_lexer_get_preprocessor_token (NULL, first_token);
31715 /* Parses the grainsize pragma for the _Cilk_for statement.
31716 Syntax:
31717 #pragma cilk grainsize = <VALUE>. */
31719 static void
31720 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31722 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31724 tree exp = cp_parser_binary_expression (parser, false, false,
31725 PREC_NOT_OPERATOR, NULL);
31726 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31727 if (!exp || exp == error_mark_node)
31729 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31730 return;
31733 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31735 cp_parser_cilk_for (parser, exp);
31736 else
31737 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31738 "%<#pragma cilk grainsize%> is not followed by "
31739 "%<_Cilk_for%>");
31740 return;
31742 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31745 /* Normal parsing of a pragma token. Here we can (and must) use the
31746 regular lexer. */
31748 static bool
31749 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31751 cp_token *pragma_tok;
31752 unsigned int id;
31754 pragma_tok = cp_lexer_consume_token (parser->lexer);
31755 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31756 parser->lexer->in_pragma = true;
31758 id = pragma_tok->pragma_kind;
31759 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31760 cp_ensure_no_omp_declare_simd (parser);
31761 switch (id)
31763 case PRAGMA_GCC_PCH_PREPROCESS:
31764 error_at (pragma_tok->location,
31765 "%<#pragma GCC pch_preprocess%> must be first");
31766 break;
31768 case PRAGMA_OMP_BARRIER:
31769 switch (context)
31771 case pragma_compound:
31772 cp_parser_omp_barrier (parser, pragma_tok);
31773 return false;
31774 case pragma_stmt:
31775 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31776 "used in compound statements");
31777 break;
31778 default:
31779 goto bad_stmt;
31781 break;
31783 case PRAGMA_OMP_FLUSH:
31784 switch (context)
31786 case pragma_compound:
31787 cp_parser_omp_flush (parser, pragma_tok);
31788 return false;
31789 case pragma_stmt:
31790 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31791 "used in compound statements");
31792 break;
31793 default:
31794 goto bad_stmt;
31796 break;
31798 case PRAGMA_OMP_TASKWAIT:
31799 switch (context)
31801 case pragma_compound:
31802 cp_parser_omp_taskwait (parser, pragma_tok);
31803 return false;
31804 case pragma_stmt:
31805 error_at (pragma_tok->location,
31806 "%<#pragma omp taskwait%> may only be "
31807 "used in compound statements");
31808 break;
31809 default:
31810 goto bad_stmt;
31812 break;
31814 case PRAGMA_OMP_TASKYIELD:
31815 switch (context)
31817 case pragma_compound:
31818 cp_parser_omp_taskyield (parser, pragma_tok);
31819 return false;
31820 case pragma_stmt:
31821 error_at (pragma_tok->location,
31822 "%<#pragma omp taskyield%> may only be "
31823 "used in compound statements");
31824 break;
31825 default:
31826 goto bad_stmt;
31828 break;
31830 case PRAGMA_OMP_CANCEL:
31831 switch (context)
31833 case pragma_compound:
31834 cp_parser_omp_cancel (parser, pragma_tok);
31835 return false;
31836 case pragma_stmt:
31837 error_at (pragma_tok->location,
31838 "%<#pragma omp cancel%> may only be "
31839 "used in compound statements");
31840 break;
31841 default:
31842 goto bad_stmt;
31844 break;
31846 case PRAGMA_OMP_CANCELLATION_POINT:
31847 switch (context)
31849 case pragma_compound:
31850 cp_parser_omp_cancellation_point (parser, pragma_tok);
31851 return false;
31852 case pragma_stmt:
31853 error_at (pragma_tok->location,
31854 "%<#pragma omp cancellation point%> may only be "
31855 "used in compound statements");
31856 break;
31857 default:
31858 goto bad_stmt;
31860 break;
31862 case PRAGMA_OMP_THREADPRIVATE:
31863 cp_parser_omp_threadprivate (parser, pragma_tok);
31864 return false;
31866 case PRAGMA_OMP_DECLARE_REDUCTION:
31867 cp_parser_omp_declare (parser, pragma_tok, context);
31868 return false;
31870 case PRAGMA_OMP_ATOMIC:
31871 case PRAGMA_OMP_CRITICAL:
31872 case PRAGMA_OMP_DISTRIBUTE:
31873 case PRAGMA_OMP_FOR:
31874 case PRAGMA_OMP_MASTER:
31875 case PRAGMA_OMP_ORDERED:
31876 case PRAGMA_OMP_PARALLEL:
31877 case PRAGMA_OMP_SECTIONS:
31878 case PRAGMA_OMP_SIMD:
31879 case PRAGMA_OMP_SINGLE:
31880 case PRAGMA_OMP_TASK:
31881 case PRAGMA_OMP_TASKGROUP:
31882 case PRAGMA_OMP_TEAMS:
31883 if (context != pragma_stmt && context != pragma_compound)
31884 goto bad_stmt;
31885 cp_parser_omp_construct (parser, pragma_tok);
31886 return true;
31888 case PRAGMA_OMP_TARGET:
31889 return cp_parser_omp_target (parser, pragma_tok, context);
31891 case PRAGMA_OMP_END_DECLARE_TARGET:
31892 cp_parser_omp_end_declare_target (parser, pragma_tok);
31893 return false;
31895 case PRAGMA_OMP_SECTION:
31896 error_at (pragma_tok->location,
31897 "%<#pragma omp section%> may only be used in "
31898 "%<#pragma omp sections%> construct");
31899 break;
31901 case PRAGMA_IVDEP:
31903 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31904 cp_token *tok;
31905 tok = cp_lexer_peek_token (the_parser->lexer);
31906 if (tok->type != CPP_KEYWORD
31907 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31908 && tok->keyword != RID_DO))
31910 cp_parser_error (parser, "for, while or do statement expected");
31911 return false;
31913 cp_parser_iteration_statement (parser, true);
31914 return true;
31917 case PRAGMA_CILK_SIMD:
31918 if (context == pragma_external)
31920 error_at (pragma_tok->location,
31921 "%<#pragma simd%> must be inside a function");
31922 break;
31924 cp_parser_cilk_simd (parser, pragma_tok);
31925 return true;
31927 case PRAGMA_CILK_GRAINSIZE:
31928 if (context == pragma_external)
31930 error_at (pragma_tok->location,
31931 "%<#pragma cilk grainsize%> must be inside a function");
31932 break;
31935 /* Ignore the pragma if Cilk Plus is not enabled. */
31936 if (flag_cilkplus)
31938 cp_parser_cilk_grainsize (parser, pragma_tok);
31939 return true;
31941 else
31943 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
31944 "%<#pragma cilk grainsize%>");
31945 break;
31948 default:
31949 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31950 c_invoke_pragma_handler (id);
31951 break;
31953 bad_stmt:
31954 cp_parser_error (parser, "expected declaration specifiers");
31955 break;
31958 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31959 return false;
31962 /* The interface the pragma parsers have to the lexer. */
31964 enum cpp_ttype
31965 pragma_lex (tree *value)
31967 cp_token *tok;
31968 enum cpp_ttype ret;
31970 tok = cp_lexer_peek_token (the_parser->lexer);
31972 ret = tok->type;
31973 *value = tok->u.value;
31975 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31976 ret = CPP_EOF;
31977 else if (ret == CPP_STRING)
31978 *value = cp_parser_string_literal (the_parser, false, false);
31979 else
31981 cp_lexer_consume_token (the_parser->lexer);
31982 if (ret == CPP_KEYWORD)
31983 ret = CPP_NAME;
31986 return ret;
31990 /* External interface. */
31992 /* Parse one entire translation unit. */
31994 void
31995 c_parse_file (void)
31997 static bool already_called = false;
31999 if (already_called)
32000 fatal_error ("inter-module optimizations not implemented for C++");
32001 already_called = true;
32003 the_parser = cp_parser_new ();
32004 push_deferring_access_checks (flag_access_control
32005 ? dk_no_deferred : dk_no_check);
32006 cp_parser_translation_unit (the_parser);
32007 the_parser = NULL;
32010 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32011 vectorlength clause:
32012 Syntax:
32013 vectorlength ( constant-expression ) */
32015 static tree
32016 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32017 bool is_simd_fn)
32019 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32020 tree expr;
32021 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32022 safelen clause. Thus, vectorlength is represented as OMP 4.0
32023 safelen. For SIMD-enabled function it is represented by OMP 4.0
32024 simdlen. */
32025 if (!is_simd_fn)
32026 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32027 loc);
32028 else
32029 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32030 loc);
32032 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32033 return error_mark_node;
32035 expr = cp_parser_constant_expression (parser, false, NULL);
32036 expr = maybe_constant_value (expr);
32038 /* If expr == error_mark_node, then don't emit any errors nor
32039 create a clause. if any of the above functions returns
32040 error mark node then they would have emitted an error message. */
32041 if (expr == error_mark_node)
32043 else if (!TREE_TYPE (expr)
32044 || !TREE_CONSTANT (expr)
32045 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32046 error_at (loc, "vectorlength must be an integer constant");
32047 else if (TREE_CONSTANT (expr)
32048 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32049 error_at (loc, "vectorlength must be a power of 2");
32050 else
32052 tree c;
32053 if (!is_simd_fn)
32055 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32056 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32057 OMP_CLAUSE_CHAIN (c) = clauses;
32058 clauses = c;
32060 else
32062 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32063 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32064 OMP_CLAUSE_CHAIN (c) = clauses;
32065 clauses = c;
32069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32070 return error_mark_node;
32071 return clauses;
32074 /* Handles the Cilk Plus #pragma simd linear clause.
32075 Syntax:
32076 linear ( simd-linear-variable-list )
32078 simd-linear-variable-list:
32079 simd-linear-variable
32080 simd-linear-variable-list , simd-linear-variable
32082 simd-linear-variable:
32083 id-expression
32084 id-expression : simd-linear-step
32086 simd-linear-step:
32087 conditional-expression */
32089 static tree
32090 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32092 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32095 return clauses;
32096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32098 cp_parser_error (parser, "expected identifier");
32099 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32100 return error_mark_node;
32103 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32104 parser->colon_corrects_to_scope_p = false;
32105 while (1)
32107 cp_token *token = cp_lexer_peek_token (parser->lexer);
32108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32110 cp_parser_error (parser, "expected variable-name");
32111 clauses = error_mark_node;
32112 break;
32115 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32116 false, false);
32117 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32118 token->location);
32119 if (decl == error_mark_node)
32121 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32122 token->location);
32123 clauses = error_mark_node;
32125 else
32127 tree e = NULL_TREE;
32128 tree step_size = integer_one_node;
32130 /* If present, parse the linear step. Otherwise, assume the default
32131 value of 1. */
32132 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32134 cp_lexer_consume_token (parser->lexer);
32136 e = cp_parser_assignment_expression (parser, false, NULL);
32137 e = maybe_constant_value (e);
32139 if (e == error_mark_node)
32141 /* If an error has occurred, then the whole pragma is
32142 considered ill-formed. Thus, no reason to keep
32143 parsing. */
32144 clauses = error_mark_node;
32145 break;
32147 else if (type_dependent_expression_p (e)
32148 || value_dependent_expression_p (e)
32149 || (TREE_TYPE (e)
32150 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32151 && (TREE_CONSTANT (e)
32152 || DECL_P (e))))
32153 step_size = e;
32154 else
32155 cp_parser_error (parser,
32156 "step size must be an integer constant "
32157 "expression or an integer variable");
32160 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32161 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32162 OMP_CLAUSE_DECL (l) = decl;
32163 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32164 OMP_CLAUSE_CHAIN (l) = clauses;
32165 clauses = l;
32167 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32168 cp_lexer_consume_token (parser->lexer);
32169 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32170 break;
32171 else
32173 error_at (cp_lexer_peek_token (parser->lexer)->location,
32174 "expected %<,%> or %<)%> after %qE", decl);
32175 clauses = error_mark_node;
32176 break;
32179 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32180 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32181 return clauses;
32184 /* Returns the name of the next clause. If the clause is not
32185 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32186 token is not consumed. Otherwise, the appropriate enum from the
32187 pragma_simd_clause is returned and the token is consumed. */
32189 static pragma_omp_clause
32190 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32192 pragma_omp_clause clause_type;
32193 cp_token *token = cp_lexer_peek_token (parser->lexer);
32195 if (token->keyword == RID_PRIVATE)
32196 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32197 else if (!token->u.value || token->type != CPP_NAME)
32198 return PRAGMA_CILK_CLAUSE_NONE;
32199 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32200 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32201 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32202 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32203 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32204 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32205 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32206 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32207 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32208 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32209 else
32210 return PRAGMA_CILK_CLAUSE_NONE;
32212 cp_lexer_consume_token (parser->lexer);
32213 return clause_type;
32216 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32218 static tree
32219 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32221 tree clauses = NULL_TREE;
32223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32224 && clauses != error_mark_node)
32226 pragma_omp_clause c_kind;
32227 c_kind = cp_parser_cilk_simd_clause_name (parser);
32228 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32229 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32230 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32231 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32232 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32233 /* Use the OpenMP 4.0 equivalent function. */
32234 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32235 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32236 /* Use the OpenMP 4.0 equivalent function. */
32237 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32238 clauses);
32239 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32240 /* Use the OMP 4.0 equivalent function. */
32241 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32242 clauses);
32243 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32244 /* Use the OMP 4.0 equivalent function. */
32245 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32246 else
32248 clauses = error_mark_node;
32249 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32250 break;
32254 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32256 if (clauses == error_mark_node)
32257 return error_mark_node;
32258 else
32259 return c_finish_cilk_clauses (clauses);
32262 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32264 static void
32265 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32267 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32269 if (clauses == error_mark_node)
32270 return;
32272 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32274 error_at (cp_lexer_peek_token (parser->lexer)->location,
32275 "for statement expected");
32276 return;
32279 tree sb = begin_omp_structured_block ();
32280 int save = cp_parser_begin_omp_structured_block (parser);
32281 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32282 if (ret)
32283 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32284 cp_parser_end_omp_structured_block (parser, save);
32285 add_stmt (finish_omp_structured_block (sb));
32288 /* Main entry-point for parsing Cilk Plus _Cilk_for
32289 loops. The return value is error_mark_node
32290 when errors happen and CILK_FOR tree on success. */
32292 static tree
32293 cp_parser_cilk_for (cp_parser *parser, tree grain)
32295 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32296 gcc_unreachable ();
32298 tree sb = begin_omp_structured_block ();
32299 int save = cp_parser_begin_omp_structured_block (parser);
32301 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32302 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32303 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32304 clauses = finish_omp_clauses (clauses);
32306 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32307 if (ret)
32308 cpp_validate_cilk_plus_loop (ret);
32309 else
32310 ret = error_mark_node;
32312 cp_parser_end_omp_structured_block (parser, save);
32313 add_stmt (finish_omp_structured_block (sb));
32314 return ret;
32317 /* Create an identifier for a generic parameter type (a synthesized
32318 template parameter implied by `auto' or a concept identifier). */
32320 static GTY(()) int generic_parm_count;
32321 static tree
32322 make_generic_type_name ()
32324 char buf[32];
32325 sprintf (buf, "auto:%d", ++generic_parm_count);
32326 return get_identifier (buf);
32329 /* Predicate that behaves as is_auto_or_concept but matches the parent
32330 node of the generic type rather than the generic type itself. This
32331 allows for type transformation in add_implicit_template_parms. */
32333 static inline bool
32334 tree_type_is_auto_or_concept (const_tree t)
32336 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32339 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32340 (creating a new template parameter list if necessary). Returns the newly
32341 created template type parm. */
32343 tree
32344 synthesize_implicit_template_parm (cp_parser *parser)
32346 gcc_assert (current_binding_level->kind == sk_function_parms);
32348 /* We are either continuing a function template that already contains implicit
32349 template parameters, creating a new fully-implicit function template, or
32350 extending an existing explicit function template with implicit template
32351 parameters. */
32353 cp_binding_level *const entry_scope = current_binding_level;
32355 bool become_template = false;
32356 cp_binding_level *parent_scope = 0;
32358 if (parser->implicit_template_scope)
32360 gcc_assert (parser->implicit_template_parms);
32362 current_binding_level = parser->implicit_template_scope;
32364 else
32366 /* Roll back to the existing template parameter scope (in the case of
32367 extending an explicit function template) or introduce a new template
32368 parameter scope ahead of the function parameter scope (or class scope
32369 in the case of out-of-line member definitions). The function scope is
32370 added back after template parameter synthesis below. */
32372 cp_binding_level *scope = entry_scope;
32374 while (scope->kind == sk_function_parms)
32376 parent_scope = scope;
32377 scope = scope->level_chain;
32379 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32381 /* If not defining a class, then any class scope is a scope level in
32382 an out-of-line member definition. In this case simply wind back
32383 beyond the first such scope to inject the template parameter list.
32384 Otherwise wind back to the class being defined. The latter can
32385 occur in class member friend declarations such as:
32387 class A {
32388 void foo (auto);
32390 class B {
32391 friend void A::foo (auto);
32394 The template parameter list synthesized for the friend declaration
32395 must be injected in the scope of 'B'. This can also occur in
32396 erroneous cases such as:
32398 struct A {
32399 struct B {
32400 void foo (auto);
32402 void B::foo (auto) {}
32405 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32406 but, nevertheless, the template parameter list synthesized for the
32407 declarator should be injected into the scope of 'A' as if the
32408 ill-formed template was specified explicitly. */
32410 while (scope->kind == sk_class && !scope->defining_class_p)
32412 parent_scope = scope;
32413 scope = scope->level_chain;
32417 current_binding_level = scope;
32419 if (scope->kind != sk_template_parms
32420 || !function_being_declared_is_template_p (parser))
32422 /* Introduce a new template parameter list for implicit template
32423 parameters. */
32425 become_template = true;
32427 parser->implicit_template_scope
32428 = begin_scope (sk_template_parms, NULL);
32430 ++processing_template_decl;
32432 parser->fully_implicit_function_template_p = true;
32433 ++parser->num_template_parameter_lists;
32435 else
32437 /* Synthesize implicit template parameters at the end of the explicit
32438 template parameter list. */
32440 gcc_assert (current_template_parms);
32442 parser->implicit_template_scope = scope;
32444 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32445 parser->implicit_template_parms
32446 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32450 /* Synthesize a new template parameter and track the current template
32451 parameter chain with implicit_template_parms. */
32453 tree synth_id = make_generic_type_name ();
32454 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32455 synth_id);
32456 tree new_parm
32457 = process_template_parm (parser->implicit_template_parms,
32458 input_location,
32459 build_tree_list (NULL_TREE, synth_tmpl_parm),
32460 /*non_type=*/false,
32461 /*param_pack=*/false);
32464 if (parser->implicit_template_parms)
32465 parser->implicit_template_parms
32466 = TREE_CHAIN (parser->implicit_template_parms);
32467 else
32468 parser->implicit_template_parms = new_parm;
32470 tree new_type = TREE_TYPE (getdecls ());
32472 /* If creating a fully implicit function template, start the new implicit
32473 template parameter list with this synthesized type, otherwise grow the
32474 current template parameter list. */
32476 if (become_template)
32478 parent_scope->level_chain = current_binding_level;
32480 tree new_parms = make_tree_vec (1);
32481 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32482 current_template_parms = tree_cons (size_int (processing_template_decl),
32483 new_parms, current_template_parms);
32485 else
32487 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32488 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32489 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32490 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32493 current_binding_level = entry_scope;
32495 return new_type;
32498 /* Finish the declaration of a fully implicit function template. Such a
32499 template has no explicit template parameter list so has not been through the
32500 normal template head and tail processing. synthesize_implicit_template_parm
32501 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32502 provided if the declaration is a class member such that its template
32503 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32504 form is returned. Otherwise NULL_TREE is returned. */
32506 tree
32507 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32509 gcc_assert (parser->fully_implicit_function_template_p);
32511 if (member_decl_opt && member_decl_opt != error_mark_node
32512 && DECL_VIRTUAL_P (member_decl_opt))
32514 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32515 "implicit templates may not be %<virtual%>");
32516 DECL_VIRTUAL_P (member_decl_opt) = false;
32519 if (member_decl_opt)
32520 member_decl_opt = finish_member_template_decl (member_decl_opt);
32521 end_template_decl ();
32523 parser->fully_implicit_function_template_p = false;
32524 --parser->num_template_parameter_lists;
32526 return member_decl_opt;
32529 #include "gt-cp-parser.h"