/cp
[official-gcc.git] / gcc / cp / parser.c
blob33ad886524e76e27e84b5a03fe15a094a5bce746
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 *);
2490 static bool cp_parser_array_designator_p
2491 (cp_parser *);
2492 static bool cp_parser_skip_to_closing_square_bracket
2493 (cp_parser *);
2495 /* Returns nonzero if we are parsing tentatively. */
2497 static inline bool
2498 cp_parser_parsing_tentatively (cp_parser* parser)
2500 return parser->context->next != NULL;
2503 /* Returns nonzero if TOKEN is a string literal. */
2505 static bool
2506 cp_parser_is_pure_string_literal (cp_token* token)
2508 return (token->type == CPP_STRING ||
2509 token->type == CPP_STRING16 ||
2510 token->type == CPP_STRING32 ||
2511 token->type == CPP_WSTRING ||
2512 token->type == CPP_UTF8STRING);
2515 /* Returns nonzero if TOKEN is a string literal
2516 of a user-defined string literal. */
2518 static bool
2519 cp_parser_is_string_literal (cp_token* token)
2521 return (cp_parser_is_pure_string_literal (token) ||
2522 token->type == CPP_STRING_USERDEF ||
2523 token->type == CPP_STRING16_USERDEF ||
2524 token->type == CPP_STRING32_USERDEF ||
2525 token->type == CPP_WSTRING_USERDEF ||
2526 token->type == CPP_UTF8STRING_USERDEF);
2529 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2531 static bool
2532 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2534 return token->keyword == keyword;
2537 /* If not parsing tentatively, issue a diagnostic of the form
2538 FILE:LINE: MESSAGE before TOKEN
2539 where TOKEN is the next token in the input stream. MESSAGE
2540 (specified by the caller) is usually of the form "expected
2541 OTHER-TOKEN". */
2543 static void
2544 cp_parser_error (cp_parser* parser, const char* gmsgid)
2546 if (!cp_parser_simulate_error (parser))
2548 cp_token *token = cp_lexer_peek_token (parser->lexer);
2549 /* This diagnostic makes more sense if it is tagged to the line
2550 of the token we just peeked at. */
2551 cp_lexer_set_source_position_from_token (token);
2553 if (token->type == CPP_PRAGMA)
2555 error_at (token->location,
2556 "%<#pragma%> is not allowed here");
2557 cp_parser_skip_to_pragma_eol (parser, token);
2558 return;
2561 c_parse_error (gmsgid,
2562 /* Because c_parser_error does not understand
2563 CPP_KEYWORD, keywords are treated like
2564 identifiers. */
2565 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2566 token->u.value, token->flags);
2570 /* Issue an error about name-lookup failing. NAME is the
2571 IDENTIFIER_NODE DECL is the result of
2572 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2573 the thing that we hoped to find. */
2575 static void
2576 cp_parser_name_lookup_error (cp_parser* parser,
2577 tree name,
2578 tree decl,
2579 name_lookup_error desired,
2580 location_t location)
2582 /* If name lookup completely failed, tell the user that NAME was not
2583 declared. */
2584 if (decl == error_mark_node)
2586 if (parser->scope && parser->scope != global_namespace)
2587 error_at (location, "%<%E::%E%> has not been declared",
2588 parser->scope, name);
2589 else if (parser->scope == global_namespace)
2590 error_at (location, "%<::%E%> has not been declared", name);
2591 else if (parser->object_scope
2592 && !CLASS_TYPE_P (parser->object_scope))
2593 error_at (location, "request for member %qE in non-class type %qT",
2594 name, parser->object_scope);
2595 else if (parser->object_scope)
2596 error_at (location, "%<%T::%E%> has not been declared",
2597 parser->object_scope, name);
2598 else
2599 error_at (location, "%qE has not been declared", name);
2601 else if (parser->scope && parser->scope != global_namespace)
2603 switch (desired)
2605 case NLE_TYPE:
2606 error_at (location, "%<%E::%E%> is not a type",
2607 parser->scope, name);
2608 break;
2609 case NLE_CXX98:
2610 error_at (location, "%<%E::%E%> is not a class or namespace",
2611 parser->scope, name);
2612 break;
2613 case NLE_NOT_CXX98:
2614 error_at (location,
2615 "%<%E::%E%> is not a class, namespace, or enumeration",
2616 parser->scope, name);
2617 break;
2618 default:
2619 gcc_unreachable ();
2623 else if (parser->scope == global_namespace)
2625 switch (desired)
2627 case NLE_TYPE:
2628 error_at (location, "%<::%E%> is not a type", name);
2629 break;
2630 case NLE_CXX98:
2631 error_at (location, "%<::%E%> is not a class or namespace", name);
2632 break;
2633 case NLE_NOT_CXX98:
2634 error_at (location,
2635 "%<::%E%> is not a class, namespace, or enumeration",
2636 name);
2637 break;
2638 default:
2639 gcc_unreachable ();
2642 else
2644 switch (desired)
2646 case NLE_TYPE:
2647 error_at (location, "%qE is not a type", name);
2648 break;
2649 case NLE_CXX98:
2650 error_at (location, "%qE is not a class or namespace", name);
2651 break;
2652 case NLE_NOT_CXX98:
2653 error_at (location,
2654 "%qE is not a class, namespace, or enumeration", name);
2655 break;
2656 default:
2657 gcc_unreachable ();
2662 /* If we are parsing tentatively, remember that an error has occurred
2663 during this tentative parse. Returns true if the error was
2664 simulated; false if a message should be issued by the caller. */
2666 static bool
2667 cp_parser_simulate_error (cp_parser* parser)
2669 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2671 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2672 return true;
2674 return false;
2677 /* This function is called when a type is defined. If type
2678 definitions are forbidden at this point, an error message is
2679 issued. */
2681 static bool
2682 cp_parser_check_type_definition (cp_parser* parser)
2684 /* If types are forbidden here, issue a message. */
2685 if (parser->type_definition_forbidden_message)
2687 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2688 in the message need to be interpreted. */
2689 error (parser->type_definition_forbidden_message);
2690 return false;
2692 return true;
2695 /* This function is called when the DECLARATOR is processed. The TYPE
2696 was a type defined in the decl-specifiers. If it is invalid to
2697 define a type in the decl-specifiers for DECLARATOR, an error is
2698 issued. TYPE_LOCATION is the location of TYPE and is used
2699 for error reporting. */
2701 static void
2702 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2703 tree type, location_t type_location)
2705 /* [dcl.fct] forbids type definitions in return types.
2706 Unfortunately, it's not easy to know whether or not we are
2707 processing a return type until after the fact. */
2708 while (declarator
2709 && (declarator->kind == cdk_pointer
2710 || declarator->kind == cdk_reference
2711 || declarator->kind == cdk_ptrmem))
2712 declarator = declarator->declarator;
2713 if (declarator
2714 && declarator->kind == cdk_function)
2716 error_at (type_location,
2717 "new types may not be defined in a return type");
2718 inform (type_location,
2719 "(perhaps a semicolon is missing after the definition of %qT)",
2720 type);
2724 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2725 "<" in any valid C++ program. If the next token is indeed "<",
2726 issue a message warning the user about what appears to be an
2727 invalid attempt to form a template-id. LOCATION is the location
2728 of the type-specifier (TYPE) */
2730 static void
2731 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2732 tree type,
2733 enum tag_types tag_type,
2734 location_t location)
2736 cp_token_position start = 0;
2738 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2740 if (TYPE_P (type))
2741 error_at (location, "%qT is not a template", type);
2742 else if (identifier_p (type))
2744 if (tag_type != none_type)
2745 error_at (location, "%qE is not a class template", type);
2746 else
2747 error_at (location, "%qE is not a template", type);
2749 else
2750 error_at (location, "invalid template-id");
2751 /* Remember the location of the invalid "<". */
2752 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2753 start = cp_lexer_token_position (parser->lexer, true);
2754 /* Consume the "<". */
2755 cp_lexer_consume_token (parser->lexer);
2756 /* Parse the template arguments. */
2757 cp_parser_enclosed_template_argument_list (parser);
2758 /* Permanently remove the invalid template arguments so that
2759 this error message is not issued again. */
2760 if (start)
2761 cp_lexer_purge_tokens_after (parser->lexer, start);
2765 /* If parsing an integral constant-expression, issue an error message
2766 about the fact that THING appeared and return true. Otherwise,
2767 return false. In either case, set
2768 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2770 static bool
2771 cp_parser_non_integral_constant_expression (cp_parser *parser,
2772 non_integral_constant thing)
2774 parser->non_integral_constant_expression_p = true;
2775 if (parser->integral_constant_expression_p)
2777 if (!parser->allow_non_integral_constant_expression_p)
2779 const char *msg = NULL;
2780 switch (thing)
2782 case NIC_FLOAT:
2783 error ("floating-point literal "
2784 "cannot appear in a constant-expression");
2785 return true;
2786 case NIC_CAST:
2787 error ("a cast to a type other than an integral or "
2788 "enumeration type cannot appear in a "
2789 "constant-expression");
2790 return true;
2791 case NIC_TYPEID:
2792 error ("%<typeid%> operator "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_NCC:
2796 error ("non-constant compound literals "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_FUNC_CALL:
2800 error ("a function call "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_INC:
2804 error ("an increment "
2805 "cannot appear in a constant-expression");
2806 return true;
2807 case NIC_DEC:
2808 error ("an decrement "
2809 "cannot appear in a constant-expression");
2810 return true;
2811 case NIC_ARRAY_REF:
2812 error ("an array reference "
2813 "cannot appear in a constant-expression");
2814 return true;
2815 case NIC_ADDR_LABEL:
2816 error ("the address of a label "
2817 "cannot appear in a constant-expression");
2818 return true;
2819 case NIC_OVERLOADED:
2820 error ("calls to overloaded operators "
2821 "cannot appear in a constant-expression");
2822 return true;
2823 case NIC_ASSIGNMENT:
2824 error ("an assignment cannot appear in a constant-expression");
2825 return true;
2826 case NIC_COMMA:
2827 error ("a comma operator "
2828 "cannot appear in a constant-expression");
2829 return true;
2830 case NIC_CONSTRUCTOR:
2831 error ("a call to a constructor "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_TRANSACTION:
2835 error ("a transaction expression "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_THIS:
2839 msg = "this";
2840 break;
2841 case NIC_FUNC_NAME:
2842 msg = "__FUNCTION__";
2843 break;
2844 case NIC_PRETTY_FUNC:
2845 msg = "__PRETTY_FUNCTION__";
2846 break;
2847 case NIC_C99_FUNC:
2848 msg = "__func__";
2849 break;
2850 case NIC_VA_ARG:
2851 msg = "va_arg";
2852 break;
2853 case NIC_ARROW:
2854 msg = "->";
2855 break;
2856 case NIC_POINT:
2857 msg = ".";
2858 break;
2859 case NIC_STAR:
2860 msg = "*";
2861 break;
2862 case NIC_ADDR:
2863 msg = "&";
2864 break;
2865 case NIC_PREINCREMENT:
2866 msg = "++";
2867 break;
2868 case NIC_PREDECREMENT:
2869 msg = "--";
2870 break;
2871 case NIC_NEW:
2872 msg = "new";
2873 break;
2874 case NIC_DEL:
2875 msg = "delete";
2876 break;
2877 default:
2878 gcc_unreachable ();
2880 if (msg)
2881 error ("%qs cannot appear in a constant-expression", msg);
2882 return true;
2885 return false;
2888 /* Emit a diagnostic for an invalid type name. This function commits
2889 to the current active tentative parse, if any. (Otherwise, the
2890 problematic construct might be encountered again later, resulting
2891 in duplicate error messages.) LOCATION is the location of ID. */
2893 static void
2894 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2895 location_t location)
2897 tree decl, ambiguous_decls;
2898 cp_parser_commit_to_tentative_parse (parser);
2899 /* Try to lookup the identifier. */
2900 decl = cp_parser_lookup_name (parser, id, none_type,
2901 /*is_template=*/false,
2902 /*is_namespace=*/false,
2903 /*check_dependency=*/true,
2904 &ambiguous_decls, location);
2905 if (ambiguous_decls)
2906 /* If the lookup was ambiguous, an error will already have
2907 been issued. */
2908 return;
2909 /* If the lookup found a template-name, it means that the user forgot
2910 to specify an argument list. Emit a useful error message. */
2911 if (TREE_CODE (decl) == TEMPLATE_DECL)
2912 error_at (location,
2913 "invalid use of template-name %qE without an argument list",
2914 decl);
2915 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2916 error_at (location, "invalid use of destructor %qD as a type", id);
2917 else if (TREE_CODE (decl) == TYPE_DECL)
2918 /* Something like 'unsigned A a;' */
2919 error_at (location, "invalid combination of multiple type-specifiers");
2920 else if (!parser->scope)
2922 /* Issue an error message. */
2923 error_at (location, "%qE does not name a type", id);
2924 /* If we're in a template class, it's possible that the user was
2925 referring to a type from a base class. For example:
2927 template <typename T> struct A { typedef T X; };
2928 template <typename T> struct B : public A<T> { X x; };
2930 The user should have said "typename A<T>::X". */
2931 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2932 inform (location, "C++11 %<constexpr%> only available with "
2933 "-std=c++11 or -std=gnu++11");
2934 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2935 inform (location, "C++11 %<noexcept%> only available with "
2936 "-std=c++11 or -std=gnu++11");
2937 else if (cxx_dialect < cxx11
2938 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2939 inform (location, "C++11 %<thread_local%> only available with "
2940 "-std=c++11 or -std=gnu++11");
2941 else if (processing_template_decl && current_class_type
2942 && TYPE_BINFO (current_class_type))
2944 tree b;
2946 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2948 b = TREE_CHAIN (b))
2950 tree base_type = BINFO_TYPE (b);
2951 if (CLASS_TYPE_P (base_type)
2952 && dependent_type_p (base_type))
2954 tree field;
2955 /* Go from a particular instantiation of the
2956 template (which will have an empty TYPE_FIELDs),
2957 to the main version. */
2958 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2959 for (field = TYPE_FIELDS (base_type);
2960 field;
2961 field = DECL_CHAIN (field))
2962 if (TREE_CODE (field) == TYPE_DECL
2963 && DECL_NAME (field) == id)
2965 inform (location,
2966 "(perhaps %<typename %T::%E%> was intended)",
2967 BINFO_TYPE (b), id);
2968 break;
2970 if (field)
2971 break;
2976 /* Here we diagnose qualified-ids where the scope is actually correct,
2977 but the identifier does not resolve to a valid type name. */
2978 else if (parser->scope != error_mark_node)
2980 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2982 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2983 error_at (location_of (id),
2984 "%qE in namespace %qE does not name a template type",
2985 id, parser->scope);
2986 else
2987 error_at (location_of (id),
2988 "%qE in namespace %qE does not name a type",
2989 id, parser->scope);
2991 else if (CLASS_TYPE_P (parser->scope)
2992 && constructor_name_p (id, parser->scope))
2994 /* A<T>::A<T>() */
2995 error_at (location, "%<%T::%E%> names the constructor, not"
2996 " the type", parser->scope, id);
2997 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2998 error_at (location, "and %qT has no template constructors",
2999 parser->scope);
3001 else if (TYPE_P (parser->scope)
3002 && dependent_scope_p (parser->scope))
3003 error_at (location, "need %<typename%> before %<%T::%E%> because "
3004 "%qT is a dependent scope",
3005 parser->scope, id, parser->scope);
3006 else if (TYPE_P (parser->scope))
3008 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3009 error_at (location_of (id),
3010 "%qE in %q#T does not name a template type",
3011 id, parser->scope);
3012 else
3013 error_at (location_of (id),
3014 "%qE in %q#T does not name a type",
3015 id, parser->scope);
3017 else
3018 gcc_unreachable ();
3022 /* Check for a common situation where a type-name should be present,
3023 but is not, and issue a sensible error message. Returns true if an
3024 invalid type-name was detected.
3026 The situation handled by this function are variable declarations of the
3027 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3028 Usually, `ID' should name a type, but if we got here it means that it
3029 does not. We try to emit the best possible error message depending on
3030 how exactly the id-expression looks like. */
3032 static bool
3033 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3035 tree id;
3036 cp_token *token = cp_lexer_peek_token (parser->lexer);
3038 /* Avoid duplicate error about ambiguous lookup. */
3039 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3041 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3042 if (next->type == CPP_NAME && next->error_reported)
3043 goto out;
3046 cp_parser_parse_tentatively (parser);
3047 id = cp_parser_id_expression (parser,
3048 /*template_keyword_p=*/false,
3049 /*check_dependency_p=*/true,
3050 /*template_p=*/NULL,
3051 /*declarator_p=*/true,
3052 /*optional_p=*/false);
3053 /* If the next token is a (, this is a function with no explicit return
3054 type, i.e. constructor, destructor or conversion op. */
3055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3056 || TREE_CODE (id) == TYPE_DECL)
3058 cp_parser_abort_tentative_parse (parser);
3059 return false;
3061 if (!cp_parser_parse_definitely (parser))
3062 return false;
3064 /* Emit a diagnostic for the invalid type. */
3065 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3066 out:
3067 /* If we aren't in the middle of a declarator (i.e. in a
3068 parameter-declaration-clause), skip to the end of the declaration;
3069 there's no point in trying to process it. */
3070 if (!parser->in_declarator_p)
3071 cp_parser_skip_to_end_of_block_or_statement (parser);
3072 return true;
3075 /* Consume tokens up to, and including, the next non-nested closing `)'.
3076 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3077 are doing error recovery. Returns -1 if OR_COMMA is true and we
3078 found an unnested comma. */
3080 static int
3081 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3082 bool recovering,
3083 bool or_comma,
3084 bool consume_paren)
3086 unsigned paren_depth = 0;
3087 unsigned brace_depth = 0;
3088 unsigned square_depth = 0;
3090 if (recovering && !or_comma
3091 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3092 return 0;
3094 while (true)
3096 cp_token * token = cp_lexer_peek_token (parser->lexer);
3098 switch (token->type)
3100 case CPP_EOF:
3101 case CPP_PRAGMA_EOL:
3102 /* If we've run out of tokens, then there is no closing `)'. */
3103 return 0;
3105 /* This is good for lambda expression capture-lists. */
3106 case CPP_OPEN_SQUARE:
3107 ++square_depth;
3108 break;
3109 case CPP_CLOSE_SQUARE:
3110 if (!square_depth--)
3111 return 0;
3112 break;
3114 case CPP_SEMICOLON:
3115 /* This matches the processing in skip_to_end_of_statement. */
3116 if (!brace_depth)
3117 return 0;
3118 break;
3120 case CPP_OPEN_BRACE:
3121 ++brace_depth;
3122 break;
3123 case CPP_CLOSE_BRACE:
3124 if (!brace_depth--)
3125 return 0;
3126 break;
3128 case CPP_COMMA:
3129 if (recovering && or_comma && !brace_depth && !paren_depth
3130 && !square_depth)
3131 return -1;
3132 break;
3134 case CPP_OPEN_PAREN:
3135 if (!brace_depth)
3136 ++paren_depth;
3137 break;
3139 case CPP_CLOSE_PAREN:
3140 if (!brace_depth && !paren_depth--)
3142 if (consume_paren)
3143 cp_lexer_consume_token (parser->lexer);
3144 return 1;
3146 break;
3148 default:
3149 break;
3152 /* Consume the token. */
3153 cp_lexer_consume_token (parser->lexer);
3157 /* Consume tokens until we reach the end of the current statement.
3158 Normally, that will be just before consuming a `;'. However, if a
3159 non-nested `}' comes first, then we stop before consuming that. */
3161 static void
3162 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3164 unsigned nesting_depth = 0;
3166 /* Unwind generic function template scope if necessary. */
3167 if (parser->fully_implicit_function_template_p)
3168 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3170 while (true)
3172 cp_token *token = cp_lexer_peek_token (parser->lexer);
3174 switch (token->type)
3176 case CPP_EOF:
3177 case CPP_PRAGMA_EOL:
3178 /* If we've run out of tokens, stop. */
3179 return;
3181 case CPP_SEMICOLON:
3182 /* If the next token is a `;', we have reached the end of the
3183 statement. */
3184 if (!nesting_depth)
3185 return;
3186 break;
3188 case CPP_CLOSE_BRACE:
3189 /* If this is a non-nested '}', stop before consuming it.
3190 That way, when confronted with something like:
3192 { 3 + }
3194 we stop before consuming the closing '}', even though we
3195 have not yet reached a `;'. */
3196 if (nesting_depth == 0)
3197 return;
3199 /* If it is the closing '}' for a block that we have
3200 scanned, stop -- but only after consuming the token.
3201 That way given:
3203 void f g () { ... }
3204 typedef int I;
3206 we will stop after the body of the erroneously declared
3207 function, but before consuming the following `typedef'
3208 declaration. */
3209 if (--nesting_depth == 0)
3211 cp_lexer_consume_token (parser->lexer);
3212 return;
3215 case CPP_OPEN_BRACE:
3216 ++nesting_depth;
3217 break;
3219 default:
3220 break;
3223 /* Consume the token. */
3224 cp_lexer_consume_token (parser->lexer);
3228 /* This function is called at the end of a statement or declaration.
3229 If the next token is a semicolon, it is consumed; otherwise, error
3230 recovery is attempted. */
3232 static void
3233 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3235 /* Look for the trailing `;'. */
3236 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3238 /* If there is additional (erroneous) input, skip to the end of
3239 the statement. */
3240 cp_parser_skip_to_end_of_statement (parser);
3241 /* If the next token is now a `;', consume it. */
3242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3243 cp_lexer_consume_token (parser->lexer);
3247 /* Skip tokens until we have consumed an entire block, or until we
3248 have consumed a non-nested `;'. */
3250 static void
3251 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3253 int nesting_depth = 0;
3255 /* Unwind generic function template scope if necessary. */
3256 if (parser->fully_implicit_function_template_p)
3257 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3259 while (nesting_depth >= 0)
3261 cp_token *token = cp_lexer_peek_token (parser->lexer);
3263 switch (token->type)
3265 case CPP_EOF:
3266 case CPP_PRAGMA_EOL:
3267 /* If we've run out of tokens, stop. */
3268 return;
3270 case CPP_SEMICOLON:
3271 /* Stop if this is an unnested ';'. */
3272 if (!nesting_depth)
3273 nesting_depth = -1;
3274 break;
3276 case CPP_CLOSE_BRACE:
3277 /* Stop if this is an unnested '}', or closes the outermost
3278 nesting level. */
3279 nesting_depth--;
3280 if (nesting_depth < 0)
3281 return;
3282 if (!nesting_depth)
3283 nesting_depth = -1;
3284 break;
3286 case CPP_OPEN_BRACE:
3287 /* Nest. */
3288 nesting_depth++;
3289 break;
3291 default:
3292 break;
3295 /* Consume the token. */
3296 cp_lexer_consume_token (parser->lexer);
3300 /* Skip tokens until a non-nested closing curly brace is the next
3301 token, or there are no more tokens. Return true in the first case,
3302 false otherwise. */
3304 static bool
3305 cp_parser_skip_to_closing_brace (cp_parser *parser)
3307 unsigned nesting_depth = 0;
3309 while (true)
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3313 switch (token->type)
3315 case CPP_EOF:
3316 case CPP_PRAGMA_EOL:
3317 /* If we've run out of tokens, stop. */
3318 return false;
3320 case CPP_CLOSE_BRACE:
3321 /* If the next token is a non-nested `}', then we have reached
3322 the end of the current block. */
3323 if (nesting_depth-- == 0)
3324 return true;
3325 break;
3327 case CPP_OPEN_BRACE:
3328 /* If it the next token is a `{', then we are entering a new
3329 block. Consume the entire block. */
3330 ++nesting_depth;
3331 break;
3333 default:
3334 break;
3337 /* Consume the token. */
3338 cp_lexer_consume_token (parser->lexer);
3342 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3343 parameter is the PRAGMA token, allowing us to purge the entire pragma
3344 sequence. */
3346 static void
3347 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3349 cp_token *token;
3351 parser->lexer->in_pragma = false;
3354 token = cp_lexer_consume_token (parser->lexer);
3355 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3357 /* Ensure that the pragma is not parsed again. */
3358 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3361 /* Require pragma end of line, resyncing with it as necessary. The
3362 arguments are as for cp_parser_skip_to_pragma_eol. */
3364 static void
3365 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3367 parser->lexer->in_pragma = false;
3368 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3369 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3372 /* This is a simple wrapper around make_typename_type. When the id is
3373 an unresolved identifier node, we can provide a superior diagnostic
3374 using cp_parser_diagnose_invalid_type_name. */
3376 static tree
3377 cp_parser_make_typename_type (cp_parser *parser, tree id,
3378 location_t id_location)
3380 tree result;
3381 if (identifier_p (id))
3383 result = make_typename_type (parser->scope, id, typename_type,
3384 /*complain=*/tf_none);
3385 if (result == error_mark_node)
3386 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3387 return result;
3389 return make_typename_type (parser->scope, id, typename_type, tf_error);
3392 /* This is a wrapper around the
3393 make_{pointer,ptrmem,reference}_declarator functions that decides
3394 which one to call based on the CODE and CLASS_TYPE arguments. The
3395 CODE argument should be one of the values returned by
3396 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3397 appertain to the pointer or reference. */
3399 static cp_declarator *
3400 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3401 cp_cv_quals cv_qualifiers,
3402 cp_declarator *target,
3403 tree attributes)
3405 if (code == ERROR_MARK)
3406 return cp_error_declarator;
3408 if (code == INDIRECT_REF)
3409 if (class_type == NULL_TREE)
3410 return make_pointer_declarator (cv_qualifiers, target, attributes);
3411 else
3412 return make_ptrmem_declarator (cv_qualifiers, class_type,
3413 target, attributes);
3414 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3415 return make_reference_declarator (cv_qualifiers, target,
3416 false, attributes);
3417 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3418 return make_reference_declarator (cv_qualifiers, target,
3419 true, attributes);
3420 gcc_unreachable ();
3423 /* Create a new C++ parser. */
3425 static cp_parser *
3426 cp_parser_new (void)
3428 cp_parser *parser;
3429 cp_lexer *lexer;
3430 unsigned i;
3432 /* cp_lexer_new_main is called before doing GC allocation because
3433 cp_lexer_new_main might load a PCH file. */
3434 lexer = cp_lexer_new_main ();
3436 /* Initialize the binops_by_token so that we can get the tree
3437 directly from the token. */
3438 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3439 binops_by_token[binops[i].token_type] = binops[i];
3441 parser = ggc_cleared_alloc<cp_parser> ();
3442 parser->lexer = lexer;
3443 parser->context = cp_parser_context_new (NULL);
3445 /* For now, we always accept GNU extensions. */
3446 parser->allow_gnu_extensions_p = 1;
3448 /* The `>' token is a greater-than operator, not the end of a
3449 template-id. */
3450 parser->greater_than_is_operator_p = true;
3452 parser->default_arg_ok_p = true;
3454 /* We are not parsing a constant-expression. */
3455 parser->integral_constant_expression_p = false;
3456 parser->allow_non_integral_constant_expression_p = false;
3457 parser->non_integral_constant_expression_p = false;
3459 /* Local variable names are not forbidden. */
3460 parser->local_variables_forbidden_p = false;
3462 /* We are not processing an `extern "C"' declaration. */
3463 parser->in_unbraced_linkage_specification_p = false;
3465 /* We are not processing a declarator. */
3466 parser->in_declarator_p = false;
3468 /* We are not processing a template-argument-list. */
3469 parser->in_template_argument_list_p = false;
3471 /* We are not in an iteration statement. */
3472 parser->in_statement = 0;
3474 /* We are not in a switch statement. */
3475 parser->in_switch_statement_p = false;
3477 /* We are not parsing a type-id inside an expression. */
3478 parser->in_type_id_in_expr_p = false;
3480 /* Declarations aren't implicitly extern "C". */
3481 parser->implicit_extern_c = false;
3483 /* String literals should be translated to the execution character set. */
3484 parser->translate_strings_p = true;
3486 /* We are not parsing a function body. */
3487 parser->in_function_body = false;
3489 /* We can correct until told otherwise. */
3490 parser->colon_corrects_to_scope_p = true;
3492 /* The unparsed function queue is empty. */
3493 push_unparsed_function_queues (parser);
3495 /* There are no classes being defined. */
3496 parser->num_classes_being_defined = 0;
3498 /* No template parameters apply. */
3499 parser->num_template_parameter_lists = 0;
3501 /* Not declaring an implicit function template. */
3502 parser->auto_is_implicit_function_template_parm_p = false;
3503 parser->fully_implicit_function_template_p = false;
3504 parser->implicit_template_parms = 0;
3505 parser->implicit_template_scope = 0;
3507 return parser;
3510 /* Create a cp_lexer structure which will emit the tokens in CACHE
3511 and push it onto the parser's lexer stack. This is used for delayed
3512 parsing of in-class method bodies and default arguments, and should
3513 not be confused with tentative parsing. */
3514 static void
3515 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3517 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3518 lexer->next = parser->lexer;
3519 parser->lexer = lexer;
3521 /* Move the current source position to that of the first token in the
3522 new lexer. */
3523 cp_lexer_set_source_position_from_token (lexer->next_token);
3526 /* Pop the top lexer off the parser stack. This is never used for the
3527 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3528 static void
3529 cp_parser_pop_lexer (cp_parser *parser)
3531 cp_lexer *lexer = parser->lexer;
3532 parser->lexer = lexer->next;
3533 cp_lexer_destroy (lexer);
3535 /* Put the current source position back where it was before this
3536 lexer was pushed. */
3537 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3540 /* Lexical conventions [gram.lex] */
3542 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3543 identifier. */
3545 static tree
3546 cp_parser_identifier (cp_parser* parser)
3548 cp_token *token;
3550 /* Look for the identifier. */
3551 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3552 /* Return the value. */
3553 return token ? token->u.value : error_mark_node;
3556 /* Parse a sequence of adjacent string constants. Returns a
3557 TREE_STRING representing the combined, nul-terminated string
3558 constant. If TRANSLATE is true, translate the string to the
3559 execution character set. If WIDE_OK is true, a wide string is
3560 invalid here.
3562 C++98 [lex.string] says that if a narrow string literal token is
3563 adjacent to a wide string literal token, the behavior is undefined.
3564 However, C99 6.4.5p4 says that this results in a wide string literal.
3565 We follow C99 here, for consistency with the C front end.
3567 This code is largely lifted from lex_string() in c-lex.c.
3569 FUTURE: ObjC++ will need to handle @-strings here. */
3570 static tree
3571 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3572 bool lookup_udlit = true)
3574 tree value;
3575 size_t count;
3576 struct obstack str_ob;
3577 cpp_string str, istr, *strs;
3578 cp_token *tok;
3579 enum cpp_ttype type, curr_type;
3580 int have_suffix_p = 0;
3581 tree string_tree;
3582 tree suffix_id = NULL_TREE;
3583 bool curr_tok_is_userdef_p = false;
3585 tok = cp_lexer_peek_token (parser->lexer);
3586 if (!cp_parser_is_string_literal (tok))
3588 cp_parser_error (parser, "expected string-literal");
3589 return error_mark_node;
3592 if (cpp_userdef_string_p (tok->type))
3594 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3595 curr_type = cpp_userdef_string_remove_type (tok->type);
3596 curr_tok_is_userdef_p = true;
3598 else
3600 string_tree = tok->u.value;
3601 curr_type = tok->type;
3603 type = curr_type;
3605 /* Try to avoid the overhead of creating and destroying an obstack
3606 for the common case of just one string. */
3607 if (!cp_parser_is_string_literal
3608 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3610 cp_lexer_consume_token (parser->lexer);
3612 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3613 str.len = TREE_STRING_LENGTH (string_tree);
3614 count = 1;
3616 if (curr_tok_is_userdef_p)
3618 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3619 have_suffix_p = 1;
3620 curr_type = cpp_userdef_string_remove_type (tok->type);
3622 else
3623 curr_type = tok->type;
3625 strs = &str;
3627 else
3629 gcc_obstack_init (&str_ob);
3630 count = 0;
3634 cp_lexer_consume_token (parser->lexer);
3635 count++;
3636 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3637 str.len = TREE_STRING_LENGTH (string_tree);
3639 if (curr_tok_is_userdef_p)
3641 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3642 if (have_suffix_p == 0)
3644 suffix_id = curr_suffix_id;
3645 have_suffix_p = 1;
3647 else if (have_suffix_p == 1
3648 && curr_suffix_id != suffix_id)
3650 error ("inconsistent user-defined literal suffixes"
3651 " %qD and %qD in string literal",
3652 suffix_id, curr_suffix_id);
3653 have_suffix_p = -1;
3655 curr_type = cpp_userdef_string_remove_type (tok->type);
3657 else
3658 curr_type = tok->type;
3660 if (type != curr_type)
3662 if (type == CPP_STRING)
3663 type = curr_type;
3664 else if (curr_type != CPP_STRING)
3665 error_at (tok->location,
3666 "unsupported non-standard concatenation "
3667 "of string literals");
3670 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3672 tok = cp_lexer_peek_token (parser->lexer);
3673 if (cpp_userdef_string_p (tok->type))
3675 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3676 curr_type = cpp_userdef_string_remove_type (tok->type);
3677 curr_tok_is_userdef_p = true;
3679 else
3681 string_tree = tok->u.value;
3682 curr_type = tok->type;
3683 curr_tok_is_userdef_p = false;
3686 while (cp_parser_is_string_literal (tok));
3688 strs = (cpp_string *) obstack_finish (&str_ob);
3691 if (type != CPP_STRING && !wide_ok)
3693 cp_parser_error (parser, "a wide string is invalid in this context");
3694 type = CPP_STRING;
3697 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3698 (parse_in, strs, count, &istr, type))
3700 value = build_string (istr.len, (const char *)istr.text);
3701 free (CONST_CAST (unsigned char *, istr.text));
3703 switch (type)
3705 default:
3706 case CPP_STRING:
3707 case CPP_UTF8STRING:
3708 TREE_TYPE (value) = char_array_type_node;
3709 break;
3710 case CPP_STRING16:
3711 TREE_TYPE (value) = char16_array_type_node;
3712 break;
3713 case CPP_STRING32:
3714 TREE_TYPE (value) = char32_array_type_node;
3715 break;
3716 case CPP_WSTRING:
3717 TREE_TYPE (value) = wchar_array_type_node;
3718 break;
3721 value = fix_string_type (value);
3723 if (have_suffix_p)
3725 tree literal = build_userdef_literal (suffix_id, value,
3726 OT_NONE, NULL_TREE);
3727 if (lookup_udlit)
3728 value = cp_parser_userdef_string_literal (literal);
3729 else
3730 value = literal;
3733 else
3734 /* cpp_interpret_string has issued an error. */
3735 value = error_mark_node;
3737 if (count > 1)
3738 obstack_free (&str_ob, 0);
3740 return value;
3743 /* Look up a literal operator with the name and the exact arguments. */
3745 static tree
3746 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3748 tree decl, fns;
3749 decl = lookup_name (name);
3750 if (!decl || !is_overloaded_fn (decl))
3751 return error_mark_node;
3753 for (fns = decl; fns; fns = OVL_NEXT (fns))
3755 unsigned int ix;
3756 bool found = true;
3757 tree fn = OVL_CURRENT (fns);
3758 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3759 if (parmtypes != NULL_TREE)
3761 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3762 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3764 tree tparm = TREE_VALUE (parmtypes);
3765 tree targ = TREE_TYPE ((*args)[ix]);
3766 bool ptr = TYPE_PTR_P (tparm);
3767 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3768 if ((ptr || arr || !same_type_p (tparm, targ))
3769 && (!ptr || !arr
3770 || !same_type_p (TREE_TYPE (tparm),
3771 TREE_TYPE (targ))))
3772 found = false;
3774 if (found
3775 && ix == vec_safe_length (args)
3776 /* May be this should be sufficient_parms_p instead,
3777 depending on how exactly should user-defined literals
3778 work in presence of default arguments on the literal
3779 operator parameters. */
3780 && parmtypes == void_list_node)
3781 return fn;
3785 return error_mark_node;
3788 /* Parse a user-defined char constant. Returns a call to a user-defined
3789 literal operator taking the character as an argument. */
3791 static tree
3792 cp_parser_userdef_char_literal (cp_parser *parser)
3794 cp_token *token = cp_lexer_consume_token (parser->lexer);
3795 tree literal = token->u.value;
3796 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3797 tree value = USERDEF_LITERAL_VALUE (literal);
3798 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3799 tree decl, result;
3801 /* Build up a call to the user-defined operator */
3802 /* Lookup the name we got back from the id-expression. */
3803 vec<tree, va_gc> *args = make_tree_vector ();
3804 vec_safe_push (args, value);
3805 decl = lookup_literal_operator (name, args);
3806 if (!decl || decl == error_mark_node)
3808 error ("unable to find character literal operator %qD with %qT argument",
3809 name, TREE_TYPE (value));
3810 release_tree_vector (args);
3811 return error_mark_node;
3813 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3814 release_tree_vector (args);
3815 if (result != error_mark_node)
3816 return result;
3818 error ("unable to find character literal operator %qD with %qT argument",
3819 name, TREE_TYPE (value));
3820 return error_mark_node;
3823 /* A subroutine of cp_parser_userdef_numeric_literal to
3824 create a char... template parameter pack from a string node. */
3826 static tree
3827 make_char_string_pack (tree value)
3829 tree charvec;
3830 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3831 const char *str = TREE_STRING_POINTER (value);
3832 int i, len = TREE_STRING_LENGTH (value) - 1;
3833 tree argvec = make_tree_vec (1);
3835 /* Fill in CHARVEC with all of the parameters. */
3836 charvec = make_tree_vec (len);
3837 for (i = 0; i < len; ++i)
3838 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3840 /* Build the argument packs. */
3841 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3842 TREE_TYPE (argpack) = char_type_node;
3844 TREE_VEC_ELT (argvec, 0) = argpack;
3846 return argvec;
3849 /* A subroutine of cp_parser_userdef_numeric_literal to
3850 create a char... template parameter pack from a string node. */
3852 static tree
3853 make_string_pack (tree value)
3855 tree charvec;
3856 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3857 const unsigned char *str
3858 = (const unsigned char *) TREE_STRING_POINTER (value);
3859 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3860 int len = TREE_STRING_LENGTH (value) / sz - 1;
3861 tree argvec = make_tree_vec (2);
3863 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3864 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3866 /* First template parm is character type. */
3867 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3869 /* Fill in CHARVEC with all of the parameters. */
3870 charvec = make_tree_vec (len);
3871 for (int i = 0; i < len; ++i)
3872 TREE_VEC_ELT (charvec, i)
3873 = double_int_to_tree (str_char_type_node,
3874 double_int::from_buffer (str + i * sz, sz));
3876 /* Build the argument packs. */
3877 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3878 TREE_TYPE (argpack) = str_char_type_node;
3880 TREE_VEC_ELT (argvec, 1) = argpack;
3882 return argvec;
3885 /* Parse a user-defined numeric constant. returns a call to a user-defined
3886 literal operator. */
3888 static tree
3889 cp_parser_userdef_numeric_literal (cp_parser *parser)
3891 cp_token *token = cp_lexer_consume_token (parser->lexer);
3892 tree literal = token->u.value;
3893 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3894 tree value = USERDEF_LITERAL_VALUE (literal);
3895 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3896 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3897 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3898 tree decl, result;
3899 vec<tree, va_gc> *args;
3901 /* Look for a literal operator taking the exact type of numeric argument
3902 as the literal value. */
3903 args = make_tree_vector ();
3904 vec_safe_push (args, value);
3905 decl = lookup_literal_operator (name, args);
3906 if (decl && decl != error_mark_node)
3908 result = finish_call_expr (decl, &args, false, true, tf_none);
3909 if (result != error_mark_node)
3911 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3912 warning_at (token->location, OPT_Woverflow,
3913 "integer literal exceeds range of %qT type",
3914 long_long_unsigned_type_node);
3915 else
3917 if (overflow > 0)
3918 warning_at (token->location, OPT_Woverflow,
3919 "floating literal exceeds range of %qT type",
3920 long_double_type_node);
3921 else if (overflow < 0)
3922 warning_at (token->location, OPT_Woverflow,
3923 "floating literal truncated to zero");
3925 release_tree_vector (args);
3926 return result;
3929 release_tree_vector (args);
3931 /* If the numeric argument didn't work, look for a raw literal
3932 operator taking a const char* argument consisting of the number
3933 in string format. */
3934 args = make_tree_vector ();
3935 vec_safe_push (args, num_string);
3936 decl = lookup_literal_operator (name, args);
3937 if (decl && decl != error_mark_node)
3939 result = finish_call_expr (decl, &args, false, true, tf_none);
3940 if (result != error_mark_node)
3942 release_tree_vector (args);
3943 return result;
3946 release_tree_vector (args);
3948 /* If the raw literal didn't work, look for a non-type template
3949 function with parameter pack char.... Call the function with
3950 template parameter characters representing the number. */
3951 args = make_tree_vector ();
3952 decl = lookup_literal_operator (name, args);
3953 if (decl && decl != error_mark_node)
3955 tree tmpl_args = make_char_string_pack (num_string);
3956 decl = lookup_template_function (decl, tmpl_args);
3957 result = finish_call_expr (decl, &args, false, true, tf_none);
3958 if (result != error_mark_node)
3960 release_tree_vector (args);
3961 return result;
3964 release_tree_vector (args);
3966 error ("unable to find numeric literal operator %qD", name);
3967 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3968 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3969 "to enable more built-in suffixes");
3970 return error_mark_node;
3973 /* Parse a user-defined string constant. Returns a call to a user-defined
3974 literal operator taking a character pointer and the length of the string
3975 as arguments. */
3977 static tree
3978 cp_parser_userdef_string_literal (tree literal)
3980 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3981 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3982 tree value = USERDEF_LITERAL_VALUE (literal);
3983 int len = TREE_STRING_LENGTH (value)
3984 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3985 tree decl, result;
3986 vec<tree, va_gc> *args;
3988 /* Look for a template function with typename parameter CharT
3989 and parameter pack CharT... Call the function with
3990 template parameter characters representing the string. */
3991 args = make_tree_vector ();
3992 decl = lookup_literal_operator (name, args);
3993 if (decl && decl != error_mark_node)
3995 tree tmpl_args = make_string_pack (value);
3996 decl = lookup_template_function (decl, tmpl_args);
3997 result = finish_call_expr (decl, &args, false, true, tf_none);
3998 if (result != error_mark_node)
4000 release_tree_vector (args);
4001 return result;
4004 release_tree_vector (args);
4006 /* Build up a call to the user-defined operator */
4007 /* Lookup the name we got back from the id-expression. */
4008 args = make_tree_vector ();
4009 vec_safe_push (args, value);
4010 vec_safe_push (args, build_int_cst (size_type_node, len));
4011 decl = lookup_name (name);
4012 if (!decl || decl == error_mark_node)
4014 error ("unable to find string literal operator %qD", name);
4015 release_tree_vector (args);
4016 return error_mark_node;
4018 result = finish_call_expr (decl, &args, false, true, tf_none);
4019 release_tree_vector (args);
4020 if (result != error_mark_node)
4021 return result;
4023 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4024 name, TREE_TYPE (value), size_type_node);
4025 return error_mark_node;
4029 /* Basic concepts [gram.basic] */
4031 /* Parse a translation-unit.
4033 translation-unit:
4034 declaration-seq [opt]
4036 Returns TRUE if all went well. */
4038 static bool
4039 cp_parser_translation_unit (cp_parser* parser)
4041 /* The address of the first non-permanent object on the declarator
4042 obstack. */
4043 static void *declarator_obstack_base;
4045 bool success;
4047 /* Create the declarator obstack, if necessary. */
4048 if (!cp_error_declarator)
4050 gcc_obstack_init (&declarator_obstack);
4051 /* Create the error declarator. */
4052 cp_error_declarator = make_declarator (cdk_error);
4053 /* Create the empty parameter list. */
4054 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4055 /* Remember where the base of the declarator obstack lies. */
4056 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4059 cp_parser_declaration_seq_opt (parser);
4061 /* If there are no tokens left then all went well. */
4062 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4064 /* Get rid of the token array; we don't need it any more. */
4065 cp_lexer_destroy (parser->lexer);
4066 parser->lexer = NULL;
4068 /* This file might have been a context that's implicitly extern
4069 "C". If so, pop the lang context. (Only relevant for PCH.) */
4070 if (parser->implicit_extern_c)
4072 pop_lang_context ();
4073 parser->implicit_extern_c = false;
4076 /* Finish up. */
4077 finish_translation_unit ();
4079 success = true;
4081 else
4083 cp_parser_error (parser, "expected declaration");
4084 success = false;
4087 /* Make sure the declarator obstack was fully cleaned up. */
4088 gcc_assert (obstack_next_free (&declarator_obstack)
4089 == declarator_obstack_base);
4091 /* All went well. */
4092 return success;
4095 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4096 decltype context. */
4098 static inline tsubst_flags_t
4099 complain_flags (bool decltype_p)
4101 tsubst_flags_t complain = tf_warning_or_error;
4102 if (decltype_p)
4103 complain |= tf_decltype;
4104 return complain;
4107 /* Expressions [gram.expr] */
4109 /* Parse a primary-expression.
4111 primary-expression:
4112 literal
4113 this
4114 ( expression )
4115 id-expression
4116 lambda-expression (C++11)
4118 GNU Extensions:
4120 primary-expression:
4121 ( compound-statement )
4122 __builtin_va_arg ( assignment-expression , type-id )
4123 __builtin_offsetof ( type-id , offsetof-expression )
4125 C++ Extensions:
4126 __has_nothrow_assign ( type-id )
4127 __has_nothrow_constructor ( type-id )
4128 __has_nothrow_copy ( type-id )
4129 __has_trivial_assign ( type-id )
4130 __has_trivial_constructor ( type-id )
4131 __has_trivial_copy ( type-id )
4132 __has_trivial_destructor ( type-id )
4133 __has_virtual_destructor ( type-id )
4134 __is_abstract ( type-id )
4135 __is_base_of ( type-id , type-id )
4136 __is_class ( type-id )
4137 __is_convertible_to ( type-id , type-id )
4138 __is_empty ( type-id )
4139 __is_enum ( type-id )
4140 __is_final ( type-id )
4141 __is_literal_type ( type-id )
4142 __is_pod ( type-id )
4143 __is_polymorphic ( type-id )
4144 __is_std_layout ( type-id )
4145 __is_trivial ( type-id )
4146 __is_union ( type-id )
4148 Objective-C++ Extension:
4150 primary-expression:
4151 objc-expression
4153 literal:
4154 __null
4156 ADDRESS_P is true iff this expression was immediately preceded by
4157 "&" and therefore might denote a pointer-to-member. CAST_P is true
4158 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4159 true iff this expression is a template argument.
4161 Returns a representation of the expression. Upon return, *IDK
4162 indicates what kind of id-expression (if any) was present. */
4164 static tree
4165 cp_parser_primary_expression (cp_parser *parser,
4166 bool address_p,
4167 bool cast_p,
4168 bool template_arg_p,
4169 bool decltype_p,
4170 cp_id_kind *idk)
4172 cp_token *token = NULL;
4174 /* Assume the primary expression is not an id-expression. */
4175 *idk = CP_ID_KIND_NONE;
4177 /* Peek at the next token. */
4178 token = cp_lexer_peek_token (parser->lexer);
4179 switch (token->type)
4181 /* literal:
4182 integer-literal
4183 character-literal
4184 floating-literal
4185 string-literal
4186 boolean-literal
4187 pointer-literal
4188 user-defined-literal */
4189 case CPP_CHAR:
4190 case CPP_CHAR16:
4191 case CPP_CHAR32:
4192 case CPP_WCHAR:
4193 case CPP_NUMBER:
4194 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4195 return cp_parser_userdef_numeric_literal (parser);
4196 token = cp_lexer_consume_token (parser->lexer);
4197 if (TREE_CODE (token->u.value) == FIXED_CST)
4199 error_at (token->location,
4200 "fixed-point types not supported in C++");
4201 return error_mark_node;
4203 /* Floating-point literals are only allowed in an integral
4204 constant expression if they are cast to an integral or
4205 enumeration type. */
4206 if (TREE_CODE (token->u.value) == REAL_CST
4207 && parser->integral_constant_expression_p
4208 && pedantic)
4210 /* CAST_P will be set even in invalid code like "int(2.7 +
4211 ...)". Therefore, we have to check that the next token
4212 is sure to end the cast. */
4213 if (cast_p)
4215 cp_token *next_token;
4217 next_token = cp_lexer_peek_token (parser->lexer);
4218 if (/* The comma at the end of an
4219 enumerator-definition. */
4220 next_token->type != CPP_COMMA
4221 /* The curly brace at the end of an enum-specifier. */
4222 && next_token->type != CPP_CLOSE_BRACE
4223 /* The end of a statement. */
4224 && next_token->type != CPP_SEMICOLON
4225 /* The end of the cast-expression. */
4226 && next_token->type != CPP_CLOSE_PAREN
4227 /* The end of an array bound. */
4228 && next_token->type != CPP_CLOSE_SQUARE
4229 /* The closing ">" in a template-argument-list. */
4230 && (next_token->type != CPP_GREATER
4231 || parser->greater_than_is_operator_p)
4232 /* C++0x only: A ">>" treated like two ">" tokens,
4233 in a template-argument-list. */
4234 && (next_token->type != CPP_RSHIFT
4235 || (cxx_dialect == cxx98)
4236 || parser->greater_than_is_operator_p))
4237 cast_p = false;
4240 /* If we are within a cast, then the constraint that the
4241 cast is to an integral or enumeration type will be
4242 checked at that point. If we are not within a cast, then
4243 this code is invalid. */
4244 if (!cast_p)
4245 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4247 return token->u.value;
4249 case CPP_CHAR_USERDEF:
4250 case CPP_CHAR16_USERDEF:
4251 case CPP_CHAR32_USERDEF:
4252 case CPP_WCHAR_USERDEF:
4253 return cp_parser_userdef_char_literal (parser);
4255 case CPP_STRING:
4256 case CPP_STRING16:
4257 case CPP_STRING32:
4258 case CPP_WSTRING:
4259 case CPP_UTF8STRING:
4260 case CPP_STRING_USERDEF:
4261 case CPP_STRING16_USERDEF:
4262 case CPP_STRING32_USERDEF:
4263 case CPP_WSTRING_USERDEF:
4264 case CPP_UTF8STRING_USERDEF:
4265 /* ??? Should wide strings be allowed when parser->translate_strings_p
4266 is false (i.e. in attributes)? If not, we can kill the third
4267 argument to cp_parser_string_literal. */
4268 return cp_parser_string_literal (parser,
4269 parser->translate_strings_p,
4270 true);
4272 case CPP_OPEN_PAREN:
4274 tree expr;
4275 bool saved_greater_than_is_operator_p;
4277 /* Consume the `('. */
4278 cp_lexer_consume_token (parser->lexer);
4279 /* Within a parenthesized expression, a `>' token is always
4280 the greater-than operator. */
4281 saved_greater_than_is_operator_p
4282 = parser->greater_than_is_operator_p;
4283 parser->greater_than_is_operator_p = true;
4284 /* If we see `( { ' then we are looking at the beginning of
4285 a GNU statement-expression. */
4286 if (cp_parser_allow_gnu_extensions_p (parser)
4287 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4289 /* Statement-expressions are not allowed by the standard. */
4290 pedwarn (token->location, OPT_Wpedantic,
4291 "ISO C++ forbids braced-groups within expressions");
4293 /* And they're not allowed outside of a function-body; you
4294 cannot, for example, write:
4296 int i = ({ int j = 3; j + 1; });
4298 at class or namespace scope. */
4299 if (!parser->in_function_body
4300 || parser->in_template_argument_list_p)
4302 error_at (token->location,
4303 "statement-expressions are not allowed outside "
4304 "functions nor in template-argument lists");
4305 cp_parser_skip_to_end_of_block_or_statement (parser);
4306 expr = error_mark_node;
4308 else
4310 /* Start the statement-expression. */
4311 expr = begin_stmt_expr ();
4312 /* Parse the compound-statement. */
4313 cp_parser_compound_statement (parser, expr, false, false);
4314 /* Finish up. */
4315 expr = finish_stmt_expr (expr, false);
4318 else
4320 /* Parse the parenthesized expression. */
4321 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4322 /* Let the front end know that this expression was
4323 enclosed in parentheses. This matters in case, for
4324 example, the expression is of the form `A::B', since
4325 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4326 not. */
4327 expr = finish_parenthesized_expr (expr);
4328 /* DR 705: Wrapping an unqualified name in parentheses
4329 suppresses arg-dependent lookup. We want to pass back
4330 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4331 (c++/37862), but none of the others. */
4332 if (*idk != CP_ID_KIND_QUALIFIED)
4333 *idk = CP_ID_KIND_NONE;
4335 /* The `>' token might be the end of a template-id or
4336 template-parameter-list now. */
4337 parser->greater_than_is_operator_p
4338 = saved_greater_than_is_operator_p;
4339 /* Consume the `)'. */
4340 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4341 cp_parser_skip_to_end_of_statement (parser);
4343 return expr;
4346 case CPP_OPEN_SQUARE:
4347 if (c_dialect_objc ())
4348 /* We have an Objective-C++ message. */
4349 return cp_parser_objc_expression (parser);
4351 tree lam = cp_parser_lambda_expression (parser);
4352 /* Don't warn about a failed tentative parse. */
4353 if (cp_parser_error_occurred (parser))
4354 return error_mark_node;
4355 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4356 return lam;
4359 case CPP_OBJC_STRING:
4360 if (c_dialect_objc ())
4361 /* We have an Objective-C++ string literal. */
4362 return cp_parser_objc_expression (parser);
4363 cp_parser_error (parser, "expected primary-expression");
4364 return error_mark_node;
4366 case CPP_KEYWORD:
4367 switch (token->keyword)
4369 /* These two are the boolean literals. */
4370 case RID_TRUE:
4371 cp_lexer_consume_token (parser->lexer);
4372 return boolean_true_node;
4373 case RID_FALSE:
4374 cp_lexer_consume_token (parser->lexer);
4375 return boolean_false_node;
4377 /* The `__null' literal. */
4378 case RID_NULL:
4379 cp_lexer_consume_token (parser->lexer);
4380 return null_node;
4382 /* The `nullptr' literal. */
4383 case RID_NULLPTR:
4384 cp_lexer_consume_token (parser->lexer);
4385 return nullptr_node;
4387 /* Recognize the `this' keyword. */
4388 case RID_THIS:
4389 cp_lexer_consume_token (parser->lexer);
4390 if (parser->local_variables_forbidden_p)
4392 error_at (token->location,
4393 "%<this%> may not be used in this context");
4394 return error_mark_node;
4396 /* Pointers cannot appear in constant-expressions. */
4397 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4398 return error_mark_node;
4399 return finish_this_expr ();
4401 /* The `operator' keyword can be the beginning of an
4402 id-expression. */
4403 case RID_OPERATOR:
4404 goto id_expression;
4406 case RID_FUNCTION_NAME:
4407 case RID_PRETTY_FUNCTION_NAME:
4408 case RID_C99_FUNCTION_NAME:
4410 non_integral_constant name;
4412 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4413 __func__ are the names of variables -- but they are
4414 treated specially. Therefore, they are handled here,
4415 rather than relying on the generic id-expression logic
4416 below. Grammatically, these names are id-expressions.
4418 Consume the token. */
4419 token = cp_lexer_consume_token (parser->lexer);
4421 switch (token->keyword)
4423 case RID_FUNCTION_NAME:
4424 name = NIC_FUNC_NAME;
4425 break;
4426 case RID_PRETTY_FUNCTION_NAME:
4427 name = NIC_PRETTY_FUNC;
4428 break;
4429 case RID_C99_FUNCTION_NAME:
4430 name = NIC_C99_FUNC;
4431 break;
4432 default:
4433 gcc_unreachable ();
4436 if (cp_parser_non_integral_constant_expression (parser, name))
4437 return error_mark_node;
4439 /* Look up the name. */
4440 return finish_fname (token->u.value);
4443 case RID_VA_ARG:
4445 tree expression;
4446 tree type;
4447 source_location type_location;
4449 /* The `__builtin_va_arg' construct is used to handle
4450 `va_arg'. Consume the `__builtin_va_arg' token. */
4451 cp_lexer_consume_token (parser->lexer);
4452 /* Look for the opening `('. */
4453 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4454 /* Now, parse the assignment-expression. */
4455 expression = cp_parser_assignment_expression (parser,
4456 /*cast_p=*/false, NULL);
4457 /* Look for the `,'. */
4458 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4459 type_location = cp_lexer_peek_token (parser->lexer)->location;
4460 /* Parse the type-id. */
4461 type = cp_parser_type_id (parser);
4462 /* Look for the closing `)'. */
4463 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4464 /* Using `va_arg' in a constant-expression is not
4465 allowed. */
4466 if (cp_parser_non_integral_constant_expression (parser,
4467 NIC_VA_ARG))
4468 return error_mark_node;
4469 return build_x_va_arg (type_location, expression, type);
4472 case RID_OFFSETOF:
4473 return cp_parser_builtin_offsetof (parser);
4475 case RID_HAS_NOTHROW_ASSIGN:
4476 case RID_HAS_NOTHROW_CONSTRUCTOR:
4477 case RID_HAS_NOTHROW_COPY:
4478 case RID_HAS_TRIVIAL_ASSIGN:
4479 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4480 case RID_HAS_TRIVIAL_COPY:
4481 case RID_HAS_TRIVIAL_DESTRUCTOR:
4482 case RID_HAS_VIRTUAL_DESTRUCTOR:
4483 case RID_IS_ABSTRACT:
4484 case RID_IS_BASE_OF:
4485 case RID_IS_CLASS:
4486 case RID_IS_CONVERTIBLE_TO:
4487 case RID_IS_EMPTY:
4488 case RID_IS_ENUM:
4489 case RID_IS_FINAL:
4490 case RID_IS_LITERAL_TYPE:
4491 case RID_IS_POD:
4492 case RID_IS_POLYMORPHIC:
4493 case RID_IS_STD_LAYOUT:
4494 case RID_IS_TRIVIAL:
4495 case RID_IS_UNION:
4496 return cp_parser_trait_expr (parser, token->keyword);
4498 /* Objective-C++ expressions. */
4499 case RID_AT_ENCODE:
4500 case RID_AT_PROTOCOL:
4501 case RID_AT_SELECTOR:
4502 return cp_parser_objc_expression (parser);
4504 case RID_TEMPLATE:
4505 if (parser->in_function_body
4506 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4507 == CPP_LESS))
4509 error_at (token->location,
4510 "a template declaration cannot appear at block scope");
4511 cp_parser_skip_to_end_of_block_or_statement (parser);
4512 return error_mark_node;
4514 default:
4515 cp_parser_error (parser, "expected primary-expression");
4516 return error_mark_node;
4519 /* An id-expression can start with either an identifier, a
4520 `::' as the beginning of a qualified-id, or the "operator"
4521 keyword. */
4522 case CPP_NAME:
4523 case CPP_SCOPE:
4524 case CPP_TEMPLATE_ID:
4525 case CPP_NESTED_NAME_SPECIFIER:
4527 tree id_expression;
4528 tree decl;
4529 const char *error_msg;
4530 bool template_p;
4531 bool done;
4532 cp_token *id_expr_token;
4534 id_expression:
4535 /* Parse the id-expression. */
4536 id_expression
4537 = cp_parser_id_expression (parser,
4538 /*template_keyword_p=*/false,
4539 /*check_dependency_p=*/true,
4540 &template_p,
4541 /*declarator_p=*/false,
4542 /*optional_p=*/false);
4543 if (id_expression == error_mark_node)
4544 return error_mark_node;
4545 id_expr_token = token;
4546 token = cp_lexer_peek_token (parser->lexer);
4547 done = (token->type != CPP_OPEN_SQUARE
4548 && token->type != CPP_OPEN_PAREN
4549 && token->type != CPP_DOT
4550 && token->type != CPP_DEREF
4551 && token->type != CPP_PLUS_PLUS
4552 && token->type != CPP_MINUS_MINUS);
4553 /* If we have a template-id, then no further lookup is
4554 required. If the template-id was for a template-class, we
4555 will sometimes have a TYPE_DECL at this point. */
4556 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4557 || TREE_CODE (id_expression) == TYPE_DECL)
4558 decl = id_expression;
4559 /* Look up the name. */
4560 else
4562 tree ambiguous_decls;
4564 /* If we already know that this lookup is ambiguous, then
4565 we've already issued an error message; there's no reason
4566 to check again. */
4567 if (id_expr_token->type == CPP_NAME
4568 && id_expr_token->error_reported)
4570 cp_parser_simulate_error (parser);
4571 return error_mark_node;
4574 decl = cp_parser_lookup_name (parser, id_expression,
4575 none_type,
4576 template_p,
4577 /*is_namespace=*/false,
4578 /*check_dependency=*/true,
4579 &ambiguous_decls,
4580 id_expr_token->location);
4581 /* If the lookup was ambiguous, an error will already have
4582 been issued. */
4583 if (ambiguous_decls)
4584 return error_mark_node;
4586 /* In Objective-C++, we may have an Objective-C 2.0
4587 dot-syntax for classes here. */
4588 if (c_dialect_objc ()
4589 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4590 && TREE_CODE (decl) == TYPE_DECL
4591 && objc_is_class_name (decl))
4593 tree component;
4594 cp_lexer_consume_token (parser->lexer);
4595 component = cp_parser_identifier (parser);
4596 if (component == error_mark_node)
4597 return error_mark_node;
4599 return objc_build_class_component_ref (id_expression, component);
4602 /* In Objective-C++, an instance variable (ivar) may be preferred
4603 to whatever cp_parser_lookup_name() found. */
4604 decl = objc_lookup_ivar (decl, id_expression);
4606 /* If name lookup gives us a SCOPE_REF, then the
4607 qualifying scope was dependent. */
4608 if (TREE_CODE (decl) == SCOPE_REF)
4610 /* At this point, we do not know if DECL is a valid
4611 integral constant expression. We assume that it is
4612 in fact such an expression, so that code like:
4614 template <int N> struct A {
4615 int a[B<N>::i];
4618 is accepted. At template-instantiation time, we
4619 will check that B<N>::i is actually a constant. */
4620 return decl;
4622 /* Check to see if DECL is a local variable in a context
4623 where that is forbidden. */
4624 if (parser->local_variables_forbidden_p
4625 && local_variable_p (decl))
4627 /* It might be that we only found DECL because we are
4628 trying to be generous with pre-ISO scoping rules.
4629 For example, consider:
4631 int i;
4632 void g() {
4633 for (int i = 0; i < 10; ++i) {}
4634 extern void f(int j = i);
4637 Here, name look up will originally find the out
4638 of scope `i'. We need to issue a warning message,
4639 but then use the global `i'. */
4640 decl = check_for_out_of_scope_variable (decl);
4641 if (local_variable_p (decl))
4643 error_at (id_expr_token->location,
4644 "local variable %qD may not appear in this context",
4645 decl);
4646 return error_mark_node;
4651 decl = (finish_id_expression
4652 (id_expression, decl, parser->scope,
4653 idk,
4654 parser->integral_constant_expression_p,
4655 parser->allow_non_integral_constant_expression_p,
4656 &parser->non_integral_constant_expression_p,
4657 template_p, done, address_p,
4658 template_arg_p,
4659 &error_msg,
4660 id_expr_token->location));
4661 if (error_msg)
4662 cp_parser_error (parser, error_msg);
4663 return decl;
4666 /* Anything else is an error. */
4667 default:
4668 cp_parser_error (parser, "expected primary-expression");
4669 return error_mark_node;
4673 static inline tree
4674 cp_parser_primary_expression (cp_parser *parser,
4675 bool address_p,
4676 bool cast_p,
4677 bool template_arg_p,
4678 cp_id_kind *idk)
4680 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4681 /*decltype*/false, idk);
4684 /* Parse an id-expression.
4686 id-expression:
4687 unqualified-id
4688 qualified-id
4690 qualified-id:
4691 :: [opt] nested-name-specifier template [opt] unqualified-id
4692 :: identifier
4693 :: operator-function-id
4694 :: template-id
4696 Return a representation of the unqualified portion of the
4697 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4698 a `::' or nested-name-specifier.
4700 Often, if the id-expression was a qualified-id, the caller will
4701 want to make a SCOPE_REF to represent the qualified-id. This
4702 function does not do this in order to avoid wastefully creating
4703 SCOPE_REFs when they are not required.
4705 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4706 `template' keyword.
4708 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4709 uninstantiated templates.
4711 If *TEMPLATE_P is non-NULL, it is set to true iff the
4712 `template' keyword is used to explicitly indicate that the entity
4713 named is a template.
4715 If DECLARATOR_P is true, the id-expression is appearing as part of
4716 a declarator, rather than as part of an expression. */
4718 static tree
4719 cp_parser_id_expression (cp_parser *parser,
4720 bool template_keyword_p,
4721 bool check_dependency_p,
4722 bool *template_p,
4723 bool declarator_p,
4724 bool optional_p)
4726 bool global_scope_p;
4727 bool nested_name_specifier_p;
4729 /* Assume the `template' keyword was not used. */
4730 if (template_p)
4731 *template_p = template_keyword_p;
4733 /* Look for the optional `::' operator. */
4734 global_scope_p
4735 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4736 != NULL_TREE);
4737 /* Look for the optional nested-name-specifier. */
4738 nested_name_specifier_p
4739 = (cp_parser_nested_name_specifier_opt (parser,
4740 /*typename_keyword_p=*/false,
4741 check_dependency_p,
4742 /*type_p=*/false,
4743 declarator_p)
4744 != NULL_TREE);
4745 /* If there is a nested-name-specifier, then we are looking at
4746 the first qualified-id production. */
4747 if (nested_name_specifier_p)
4749 tree saved_scope;
4750 tree saved_object_scope;
4751 tree saved_qualifying_scope;
4752 tree unqualified_id;
4753 bool is_template;
4755 /* See if the next token is the `template' keyword. */
4756 if (!template_p)
4757 template_p = &is_template;
4758 *template_p = cp_parser_optional_template_keyword (parser);
4759 /* Name lookup we do during the processing of the
4760 unqualified-id might obliterate SCOPE. */
4761 saved_scope = parser->scope;
4762 saved_object_scope = parser->object_scope;
4763 saved_qualifying_scope = parser->qualifying_scope;
4764 /* Process the final unqualified-id. */
4765 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4766 check_dependency_p,
4767 declarator_p,
4768 /*optional_p=*/false);
4769 /* Restore the SAVED_SCOPE for our caller. */
4770 parser->scope = saved_scope;
4771 parser->object_scope = saved_object_scope;
4772 parser->qualifying_scope = saved_qualifying_scope;
4774 return unqualified_id;
4776 /* Otherwise, if we are in global scope, then we are looking at one
4777 of the other qualified-id productions. */
4778 else if (global_scope_p)
4780 cp_token *token;
4781 tree id;
4783 /* Peek at the next token. */
4784 token = cp_lexer_peek_token (parser->lexer);
4786 /* If it's an identifier, and the next token is not a "<", then
4787 we can avoid the template-id case. This is an optimization
4788 for this common case. */
4789 if (token->type == CPP_NAME
4790 && !cp_parser_nth_token_starts_template_argument_list_p
4791 (parser, 2))
4792 return cp_parser_identifier (parser);
4794 cp_parser_parse_tentatively (parser);
4795 /* Try a template-id. */
4796 id = cp_parser_template_id (parser,
4797 /*template_keyword_p=*/false,
4798 /*check_dependency_p=*/true,
4799 none_type,
4800 declarator_p);
4801 /* If that worked, we're done. */
4802 if (cp_parser_parse_definitely (parser))
4803 return id;
4805 /* Peek at the next token. (Changes in the token buffer may
4806 have invalidated the pointer obtained above.) */
4807 token = cp_lexer_peek_token (parser->lexer);
4809 switch (token->type)
4811 case CPP_NAME:
4812 return cp_parser_identifier (parser);
4814 case CPP_KEYWORD:
4815 if (token->keyword == RID_OPERATOR)
4816 return cp_parser_operator_function_id (parser);
4817 /* Fall through. */
4819 default:
4820 cp_parser_error (parser, "expected id-expression");
4821 return error_mark_node;
4824 else
4825 return cp_parser_unqualified_id (parser, template_keyword_p,
4826 /*check_dependency_p=*/true,
4827 declarator_p,
4828 optional_p);
4831 /* Parse an unqualified-id.
4833 unqualified-id:
4834 identifier
4835 operator-function-id
4836 conversion-function-id
4837 ~ class-name
4838 template-id
4840 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4841 keyword, in a construct like `A::template ...'.
4843 Returns a representation of unqualified-id. For the `identifier'
4844 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4845 production a BIT_NOT_EXPR is returned; the operand of the
4846 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4847 other productions, see the documentation accompanying the
4848 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4849 names are looked up in uninstantiated templates. If DECLARATOR_P
4850 is true, the unqualified-id is appearing as part of a declarator,
4851 rather than as part of an expression. */
4853 static tree
4854 cp_parser_unqualified_id (cp_parser* parser,
4855 bool template_keyword_p,
4856 bool check_dependency_p,
4857 bool declarator_p,
4858 bool optional_p)
4860 cp_token *token;
4862 /* Peek at the next token. */
4863 token = cp_lexer_peek_token (parser->lexer);
4865 switch (token->type)
4867 case CPP_NAME:
4869 tree id;
4871 /* We don't know yet whether or not this will be a
4872 template-id. */
4873 cp_parser_parse_tentatively (parser);
4874 /* Try a template-id. */
4875 id = cp_parser_template_id (parser, template_keyword_p,
4876 check_dependency_p,
4877 none_type,
4878 declarator_p);
4879 /* If it worked, we're done. */
4880 if (cp_parser_parse_definitely (parser))
4881 return id;
4882 /* Otherwise, it's an ordinary identifier. */
4883 return cp_parser_identifier (parser);
4886 case CPP_TEMPLATE_ID:
4887 return cp_parser_template_id (parser, template_keyword_p,
4888 check_dependency_p,
4889 none_type,
4890 declarator_p);
4892 case CPP_COMPL:
4894 tree type_decl;
4895 tree qualifying_scope;
4896 tree object_scope;
4897 tree scope;
4898 bool done;
4900 /* Consume the `~' token. */
4901 cp_lexer_consume_token (parser->lexer);
4902 /* Parse the class-name. The standard, as written, seems to
4903 say that:
4905 template <typename T> struct S { ~S (); };
4906 template <typename T> S<T>::~S() {}
4908 is invalid, since `~' must be followed by a class-name, but
4909 `S<T>' is dependent, and so not known to be a class.
4910 That's not right; we need to look in uninstantiated
4911 templates. A further complication arises from:
4913 template <typename T> void f(T t) {
4914 t.T::~T();
4917 Here, it is not possible to look up `T' in the scope of `T'
4918 itself. We must look in both the current scope, and the
4919 scope of the containing complete expression.
4921 Yet another issue is:
4923 struct S {
4924 int S;
4925 ~S();
4928 S::~S() {}
4930 The standard does not seem to say that the `S' in `~S'
4931 should refer to the type `S' and not the data member
4932 `S::S'. */
4934 /* DR 244 says that we look up the name after the "~" in the
4935 same scope as we looked up the qualifying name. That idea
4936 isn't fully worked out; it's more complicated than that. */
4937 scope = parser->scope;
4938 object_scope = parser->object_scope;
4939 qualifying_scope = parser->qualifying_scope;
4941 /* Check for invalid scopes. */
4942 if (scope == error_mark_node)
4944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4945 cp_lexer_consume_token (parser->lexer);
4946 return error_mark_node;
4948 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4950 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4951 error_at (token->location,
4952 "scope %qT before %<~%> is not a class-name",
4953 scope);
4954 cp_parser_simulate_error (parser);
4955 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4956 cp_lexer_consume_token (parser->lexer);
4957 return error_mark_node;
4959 gcc_assert (!scope || TYPE_P (scope));
4961 /* If the name is of the form "X::~X" it's OK even if X is a
4962 typedef. */
4963 token = cp_lexer_peek_token (parser->lexer);
4964 if (scope
4965 && token->type == CPP_NAME
4966 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4967 != CPP_LESS)
4968 && (token->u.value == TYPE_IDENTIFIER (scope)
4969 || (CLASS_TYPE_P (scope)
4970 && constructor_name_p (token->u.value, scope))))
4972 cp_lexer_consume_token (parser->lexer);
4973 return build_nt (BIT_NOT_EXPR, scope);
4976 /* ~auto means the destructor of whatever the object is. */
4977 if (cp_parser_is_keyword (token, RID_AUTO))
4979 if (cxx_dialect < cxx14)
4980 pedwarn (input_location, 0,
4981 "%<~auto%> only available with "
4982 "-std=c++14 or -std=gnu++14");
4983 cp_lexer_consume_token (parser->lexer);
4984 return build_nt (BIT_NOT_EXPR, make_auto ());
4987 /* If there was an explicit qualification (S::~T), first look
4988 in the scope given by the qualification (i.e., S).
4990 Note: in the calls to cp_parser_class_name below we pass
4991 typename_type so that lookup finds the injected-class-name
4992 rather than the constructor. */
4993 done = false;
4994 type_decl = NULL_TREE;
4995 if (scope)
4997 cp_parser_parse_tentatively (parser);
4998 type_decl = cp_parser_class_name (parser,
4999 /*typename_keyword_p=*/false,
5000 /*template_keyword_p=*/false,
5001 typename_type,
5002 /*check_dependency=*/false,
5003 /*class_head_p=*/false,
5004 declarator_p);
5005 if (cp_parser_parse_definitely (parser))
5006 done = true;
5008 /* In "N::S::~S", look in "N" as well. */
5009 if (!done && scope && qualifying_scope)
5011 cp_parser_parse_tentatively (parser);
5012 parser->scope = qualifying_scope;
5013 parser->object_scope = NULL_TREE;
5014 parser->qualifying_scope = NULL_TREE;
5015 type_decl
5016 = cp_parser_class_name (parser,
5017 /*typename_keyword_p=*/false,
5018 /*template_keyword_p=*/false,
5019 typename_type,
5020 /*check_dependency=*/false,
5021 /*class_head_p=*/false,
5022 declarator_p);
5023 if (cp_parser_parse_definitely (parser))
5024 done = true;
5026 /* In "p->S::~T", look in the scope given by "*p" as well. */
5027 else if (!done && object_scope)
5029 cp_parser_parse_tentatively (parser);
5030 parser->scope = object_scope;
5031 parser->object_scope = NULL_TREE;
5032 parser->qualifying_scope = NULL_TREE;
5033 type_decl
5034 = cp_parser_class_name (parser,
5035 /*typename_keyword_p=*/false,
5036 /*template_keyword_p=*/false,
5037 typename_type,
5038 /*check_dependency=*/false,
5039 /*class_head_p=*/false,
5040 declarator_p);
5041 if (cp_parser_parse_definitely (parser))
5042 done = true;
5044 /* Look in the surrounding context. */
5045 if (!done)
5047 parser->scope = NULL_TREE;
5048 parser->object_scope = NULL_TREE;
5049 parser->qualifying_scope = NULL_TREE;
5050 if (processing_template_decl)
5051 cp_parser_parse_tentatively (parser);
5052 type_decl
5053 = cp_parser_class_name (parser,
5054 /*typename_keyword_p=*/false,
5055 /*template_keyword_p=*/false,
5056 typename_type,
5057 /*check_dependency=*/false,
5058 /*class_head_p=*/false,
5059 declarator_p);
5060 if (processing_template_decl
5061 && ! cp_parser_parse_definitely (parser))
5063 /* We couldn't find a type with this name, so just accept
5064 it and check for a match at instantiation time. */
5065 type_decl = cp_parser_identifier (parser);
5066 if (type_decl != error_mark_node)
5067 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5068 return type_decl;
5071 /* If an error occurred, assume that the name of the
5072 destructor is the same as the name of the qualifying
5073 class. That allows us to keep parsing after running
5074 into ill-formed destructor names. */
5075 if (type_decl == error_mark_node && scope)
5076 return build_nt (BIT_NOT_EXPR, scope);
5077 else if (type_decl == error_mark_node)
5078 return error_mark_node;
5080 /* Check that destructor name and scope match. */
5081 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5083 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5084 error_at (token->location,
5085 "declaration of %<~%T%> as member of %qT",
5086 type_decl, scope);
5087 cp_parser_simulate_error (parser);
5088 return error_mark_node;
5091 /* [class.dtor]
5093 A typedef-name that names a class shall not be used as the
5094 identifier in the declarator for a destructor declaration. */
5095 if (declarator_p
5096 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5097 && !DECL_SELF_REFERENCE_P (type_decl)
5098 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5099 error_at (token->location,
5100 "typedef-name %qD used as destructor declarator",
5101 type_decl);
5103 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5106 case CPP_KEYWORD:
5107 if (token->keyword == RID_OPERATOR)
5109 tree id;
5111 /* This could be a template-id, so we try that first. */
5112 cp_parser_parse_tentatively (parser);
5113 /* Try a template-id. */
5114 id = cp_parser_template_id (parser, template_keyword_p,
5115 /*check_dependency_p=*/true,
5116 none_type,
5117 declarator_p);
5118 /* If that worked, we're done. */
5119 if (cp_parser_parse_definitely (parser))
5120 return id;
5121 /* We still don't know whether we're looking at an
5122 operator-function-id or a conversion-function-id. */
5123 cp_parser_parse_tentatively (parser);
5124 /* Try an operator-function-id. */
5125 id = cp_parser_operator_function_id (parser);
5126 /* If that didn't work, try a conversion-function-id. */
5127 if (!cp_parser_parse_definitely (parser))
5128 id = cp_parser_conversion_function_id (parser);
5129 else if (UDLIT_OPER_P (id))
5131 /* 17.6.3.3.5 */
5132 const char *name = UDLIT_OP_SUFFIX (id);
5133 if (name[0] != '_' && !in_system_header_at (input_location)
5134 && declarator_p)
5135 warning (0, "literal operator suffixes not preceded by %<_%>"
5136 " are reserved for future standardization");
5139 return id;
5141 /* Fall through. */
5143 default:
5144 if (optional_p)
5145 return NULL_TREE;
5146 cp_parser_error (parser, "expected unqualified-id");
5147 return error_mark_node;
5151 /* Parse an (optional) nested-name-specifier.
5153 nested-name-specifier: [C++98]
5154 class-or-namespace-name :: nested-name-specifier [opt]
5155 class-or-namespace-name :: template nested-name-specifier [opt]
5157 nested-name-specifier: [C++0x]
5158 type-name ::
5159 namespace-name ::
5160 nested-name-specifier identifier ::
5161 nested-name-specifier template [opt] simple-template-id ::
5163 PARSER->SCOPE should be set appropriately before this function is
5164 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5165 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5166 in name lookups.
5168 Sets PARSER->SCOPE to the class (TYPE) or namespace
5169 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5170 it unchanged if there is no nested-name-specifier. Returns the new
5171 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5173 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5174 part of a declaration and/or decl-specifier. */
5176 static tree
5177 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5178 bool typename_keyword_p,
5179 bool check_dependency_p,
5180 bool type_p,
5181 bool is_declaration)
5183 bool success = false;
5184 cp_token_position start = 0;
5185 cp_token *token;
5187 /* Remember where the nested-name-specifier starts. */
5188 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5190 start = cp_lexer_token_position (parser->lexer, false);
5191 push_deferring_access_checks (dk_deferred);
5194 while (true)
5196 tree new_scope;
5197 tree old_scope;
5198 tree saved_qualifying_scope;
5199 bool template_keyword_p;
5201 /* Spot cases that cannot be the beginning of a
5202 nested-name-specifier. */
5203 token = cp_lexer_peek_token (parser->lexer);
5205 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5206 the already parsed nested-name-specifier. */
5207 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5209 /* Grab the nested-name-specifier and continue the loop. */
5210 cp_parser_pre_parsed_nested_name_specifier (parser);
5211 /* If we originally encountered this nested-name-specifier
5212 with IS_DECLARATION set to false, we will not have
5213 resolved TYPENAME_TYPEs, so we must do so here. */
5214 if (is_declaration
5215 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5217 new_scope = resolve_typename_type (parser->scope,
5218 /*only_current_p=*/false);
5219 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5220 parser->scope = new_scope;
5222 success = true;
5223 continue;
5226 /* Spot cases that cannot be the beginning of a
5227 nested-name-specifier. On the second and subsequent times
5228 through the loop, we look for the `template' keyword. */
5229 if (success && token->keyword == RID_TEMPLATE)
5231 /* A template-id can start a nested-name-specifier. */
5232 else if (token->type == CPP_TEMPLATE_ID)
5234 /* DR 743: decltype can be used in a nested-name-specifier. */
5235 else if (token_is_decltype (token))
5237 else
5239 /* If the next token is not an identifier, then it is
5240 definitely not a type-name or namespace-name. */
5241 if (token->type != CPP_NAME)
5242 break;
5243 /* If the following token is neither a `<' (to begin a
5244 template-id), nor a `::', then we are not looking at a
5245 nested-name-specifier. */
5246 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5248 if (token->type == CPP_COLON
5249 && parser->colon_corrects_to_scope_p
5250 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5252 error_at (token->location,
5253 "found %<:%> in nested-name-specifier, expected %<::%>");
5254 token->type = CPP_SCOPE;
5257 if (token->type != CPP_SCOPE
5258 && !cp_parser_nth_token_starts_template_argument_list_p
5259 (parser, 2))
5260 break;
5263 /* The nested-name-specifier is optional, so we parse
5264 tentatively. */
5265 cp_parser_parse_tentatively (parser);
5267 /* Look for the optional `template' keyword, if this isn't the
5268 first time through the loop. */
5269 if (success)
5270 template_keyword_p = cp_parser_optional_template_keyword (parser);
5271 else
5272 template_keyword_p = false;
5274 /* Save the old scope since the name lookup we are about to do
5275 might destroy it. */
5276 old_scope = parser->scope;
5277 saved_qualifying_scope = parser->qualifying_scope;
5278 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5279 look up names in "X<T>::I" in order to determine that "Y" is
5280 a template. So, if we have a typename at this point, we make
5281 an effort to look through it. */
5282 if (is_declaration
5283 && !typename_keyword_p
5284 && parser->scope
5285 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5286 parser->scope = resolve_typename_type (parser->scope,
5287 /*only_current_p=*/false);
5288 /* Parse the qualifying entity. */
5289 new_scope
5290 = cp_parser_qualifying_entity (parser,
5291 typename_keyword_p,
5292 template_keyword_p,
5293 check_dependency_p,
5294 type_p,
5295 is_declaration);
5296 /* Look for the `::' token. */
5297 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5299 /* If we found what we wanted, we keep going; otherwise, we're
5300 done. */
5301 if (!cp_parser_parse_definitely (parser))
5303 bool error_p = false;
5305 /* Restore the OLD_SCOPE since it was valid before the
5306 failed attempt at finding the last
5307 class-or-namespace-name. */
5308 parser->scope = old_scope;
5309 parser->qualifying_scope = saved_qualifying_scope;
5311 /* If the next token is a decltype, and the one after that is a
5312 `::', then the decltype has failed to resolve to a class or
5313 enumeration type. Give this error even when parsing
5314 tentatively since it can't possibly be valid--and we're going
5315 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5316 won't get another chance.*/
5317 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5318 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5319 == CPP_SCOPE))
5321 token = cp_lexer_consume_token (parser->lexer);
5322 error_at (token->location, "decltype evaluates to %qT, "
5323 "which is not a class or enumeration type",
5324 token->u.value);
5325 parser->scope = error_mark_node;
5326 error_p = true;
5327 /* As below. */
5328 success = true;
5329 cp_lexer_consume_token (parser->lexer);
5332 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5333 break;
5334 /* If the next token is an identifier, and the one after
5335 that is a `::', then any valid interpretation would have
5336 found a class-or-namespace-name. */
5337 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5338 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5339 == CPP_SCOPE)
5340 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5341 != CPP_COMPL))
5343 token = cp_lexer_consume_token (parser->lexer);
5344 if (!error_p)
5346 if (!token->error_reported)
5348 tree decl;
5349 tree ambiguous_decls;
5351 decl = cp_parser_lookup_name (parser, token->u.value,
5352 none_type,
5353 /*is_template=*/false,
5354 /*is_namespace=*/false,
5355 /*check_dependency=*/true,
5356 &ambiguous_decls,
5357 token->location);
5358 if (TREE_CODE (decl) == TEMPLATE_DECL)
5359 error_at (token->location,
5360 "%qD used without template parameters",
5361 decl);
5362 else if (ambiguous_decls)
5364 // cp_parser_lookup_name has the same diagnostic,
5365 // thus make sure to emit it at most once.
5366 if (cp_parser_uncommitted_to_tentative_parse_p
5367 (parser))
5369 error_at (token->location,
5370 "reference to %qD is ambiguous",
5371 token->u.value);
5372 print_candidates (ambiguous_decls);
5374 decl = error_mark_node;
5376 else
5378 if (cxx_dialect != cxx98)
5379 cp_parser_name_lookup_error
5380 (parser, token->u.value, decl, NLE_NOT_CXX98,
5381 token->location);
5382 else
5383 cp_parser_name_lookup_error
5384 (parser, token->u.value, decl, NLE_CXX98,
5385 token->location);
5388 parser->scope = error_mark_node;
5389 error_p = true;
5390 /* Treat this as a successful nested-name-specifier
5391 due to:
5393 [basic.lookup.qual]
5395 If the name found is not a class-name (clause
5396 _class_) or namespace-name (_namespace.def_), the
5397 program is ill-formed. */
5398 success = true;
5400 cp_lexer_consume_token (parser->lexer);
5402 break;
5404 /* We've found one valid nested-name-specifier. */
5405 success = true;
5406 /* Name lookup always gives us a DECL. */
5407 if (TREE_CODE (new_scope) == TYPE_DECL)
5408 new_scope = TREE_TYPE (new_scope);
5409 /* Uses of "template" must be followed by actual templates. */
5410 if (template_keyword_p
5411 && !(CLASS_TYPE_P (new_scope)
5412 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5414 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5415 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5417 == TEMPLATE_ID_EXPR)))
5418 permerror (input_location, TYPE_P (new_scope)
5419 ? G_("%qT is not a template")
5420 : G_("%qD is not a template"),
5421 new_scope);
5422 /* If it is a class scope, try to complete it; we are about to
5423 be looking up names inside the class. */
5424 if (TYPE_P (new_scope)
5425 /* Since checking types for dependency can be expensive,
5426 avoid doing it if the type is already complete. */
5427 && !COMPLETE_TYPE_P (new_scope)
5428 /* Do not try to complete dependent types. */
5429 && !dependent_type_p (new_scope))
5431 new_scope = complete_type (new_scope);
5432 /* If it is a typedef to current class, use the current
5433 class instead, as the typedef won't have any names inside
5434 it yet. */
5435 if (!COMPLETE_TYPE_P (new_scope)
5436 && currently_open_class (new_scope))
5437 new_scope = TYPE_MAIN_VARIANT (new_scope);
5439 /* Make sure we look in the right scope the next time through
5440 the loop. */
5441 parser->scope = new_scope;
5444 /* If parsing tentatively, replace the sequence of tokens that makes
5445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5446 token. That way, should we re-parse the token stream, we will
5447 not have to repeat the effort required to do the parse, nor will
5448 we issue duplicate error messages. */
5449 if (success && start)
5451 cp_token *token;
5453 token = cp_lexer_token_at (parser->lexer, start);
5454 /* Reset the contents of the START token. */
5455 token->type = CPP_NESTED_NAME_SPECIFIER;
5456 /* Retrieve any deferred checks. Do not pop this access checks yet
5457 so the memory will not be reclaimed during token replacing below. */
5458 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5459 token->u.tree_check_value->value = parser->scope;
5460 token->u.tree_check_value->checks = get_deferred_access_checks ();
5461 token->u.tree_check_value->qualifying_scope =
5462 parser->qualifying_scope;
5463 token->keyword = RID_MAX;
5465 /* Purge all subsequent tokens. */
5466 cp_lexer_purge_tokens_after (parser->lexer, start);
5469 if (start)
5470 pop_to_parent_deferring_access_checks ();
5472 return success ? parser->scope : NULL_TREE;
5475 /* Parse a nested-name-specifier. See
5476 cp_parser_nested_name_specifier_opt for details. This function
5477 behaves identically, except that it will an issue an error if no
5478 nested-name-specifier is present. */
5480 static tree
5481 cp_parser_nested_name_specifier (cp_parser *parser,
5482 bool typename_keyword_p,
5483 bool check_dependency_p,
5484 bool type_p,
5485 bool is_declaration)
5487 tree scope;
5489 /* Look for the nested-name-specifier. */
5490 scope = cp_parser_nested_name_specifier_opt (parser,
5491 typename_keyword_p,
5492 check_dependency_p,
5493 type_p,
5494 is_declaration);
5495 /* If it was not present, issue an error message. */
5496 if (!scope)
5498 cp_parser_error (parser, "expected nested-name-specifier");
5499 parser->scope = NULL_TREE;
5502 return scope;
5505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5506 this is either a class-name or a namespace-name (which corresponds
5507 to the class-or-namespace-name production in the grammar). For
5508 C++0x, it can also be a type-name that refers to an enumeration
5509 type or a simple-template-id.
5511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5514 TYPE_P is TRUE iff the next name should be taken as a class-name,
5515 even the same name is declared to be another entity in the same
5516 scope.
5518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5519 specified by the class-or-namespace-name. If neither is found the
5520 ERROR_MARK_NODE is returned. */
5522 static tree
5523 cp_parser_qualifying_entity (cp_parser *parser,
5524 bool typename_keyword_p,
5525 bool template_keyword_p,
5526 bool check_dependency_p,
5527 bool type_p,
5528 bool is_declaration)
5530 tree saved_scope;
5531 tree saved_qualifying_scope;
5532 tree saved_object_scope;
5533 tree scope;
5534 bool only_class_p;
5535 bool successful_parse_p;
5537 /* DR 743: decltype can appear in a nested-name-specifier. */
5538 if (cp_lexer_next_token_is_decltype (parser->lexer))
5540 scope = cp_parser_decltype (parser);
5541 if (TREE_CODE (scope) != ENUMERAL_TYPE
5542 && !MAYBE_CLASS_TYPE_P (scope))
5544 cp_parser_simulate_error (parser);
5545 return error_mark_node;
5547 if (TYPE_NAME (scope))
5548 scope = TYPE_NAME (scope);
5549 return scope;
5552 /* Before we try to parse the class-name, we must save away the
5553 current PARSER->SCOPE since cp_parser_class_name will destroy
5554 it. */
5555 saved_scope = parser->scope;
5556 saved_qualifying_scope = parser->qualifying_scope;
5557 saved_object_scope = parser->object_scope;
5558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5559 there is no need to look for a namespace-name. */
5560 only_class_p = template_keyword_p
5561 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5562 if (!only_class_p)
5563 cp_parser_parse_tentatively (parser);
5564 scope = cp_parser_class_name (parser,
5565 typename_keyword_p,
5566 template_keyword_p,
5567 type_p ? class_type : none_type,
5568 check_dependency_p,
5569 /*class_head_p=*/false,
5570 is_declaration);
5571 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5572 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5573 if (!only_class_p
5574 && cxx_dialect != cxx98
5575 && !successful_parse_p)
5577 /* Restore the saved scope. */
5578 parser->scope = saved_scope;
5579 parser->qualifying_scope = saved_qualifying_scope;
5580 parser->object_scope = saved_object_scope;
5582 /* Parse tentatively. */
5583 cp_parser_parse_tentatively (parser);
5585 /* Parse a type-name */
5586 scope = cp_parser_type_name (parser);
5588 /* "If the name found does not designate a namespace or a class,
5589 enumeration, or dependent type, the program is ill-formed."
5591 We cover classes and dependent types above and namespaces below,
5592 so this code is only looking for enums. */
5593 if (!scope || TREE_CODE (scope) != TYPE_DECL
5594 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5595 cp_parser_simulate_error (parser);
5597 successful_parse_p = cp_parser_parse_definitely (parser);
5599 /* If that didn't work, try for a namespace-name. */
5600 if (!only_class_p && !successful_parse_p)
5602 /* Restore the saved scope. */
5603 parser->scope = saved_scope;
5604 parser->qualifying_scope = saved_qualifying_scope;
5605 parser->object_scope = saved_object_scope;
5606 /* If we are not looking at an identifier followed by the scope
5607 resolution operator, then this is not part of a
5608 nested-name-specifier. (Note that this function is only used
5609 to parse the components of a nested-name-specifier.) */
5610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5611 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5612 return error_mark_node;
5613 scope = cp_parser_namespace_name (parser);
5616 return scope;
5619 /* Return true if we are looking at a compound-literal, false otherwise. */
5621 static bool
5622 cp_parser_compound_literal_p (cp_parser *parser)
5624 /* Consume the `('. */
5625 cp_lexer_consume_token (parser->lexer);
5627 cp_lexer_save_tokens (parser->lexer);
5629 /* Skip tokens until the next token is a closing parenthesis.
5630 If we find the closing `)', and the next token is a `{', then
5631 we are looking at a compound-literal. */
5632 bool compound_literal_p
5633 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5634 /*consume_paren=*/true)
5635 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5637 /* Roll back the tokens we skipped. */
5638 cp_lexer_rollback_tokens (parser->lexer);
5640 return compound_literal_p;
5643 /* Parse a postfix-expression.
5645 postfix-expression:
5646 primary-expression
5647 postfix-expression [ expression ]
5648 postfix-expression ( expression-list [opt] )
5649 simple-type-specifier ( expression-list [opt] )
5650 typename :: [opt] nested-name-specifier identifier
5651 ( expression-list [opt] )
5652 typename :: [opt] nested-name-specifier template [opt] template-id
5653 ( expression-list [opt] )
5654 postfix-expression . template [opt] id-expression
5655 postfix-expression -> template [opt] id-expression
5656 postfix-expression . pseudo-destructor-name
5657 postfix-expression -> pseudo-destructor-name
5658 postfix-expression ++
5659 postfix-expression --
5660 dynamic_cast < type-id > ( expression )
5661 static_cast < type-id > ( expression )
5662 reinterpret_cast < type-id > ( expression )
5663 const_cast < type-id > ( expression )
5664 typeid ( expression )
5665 typeid ( type-id )
5667 GNU Extension:
5669 postfix-expression:
5670 ( type-id ) { initializer-list , [opt] }
5672 This extension is a GNU version of the C99 compound-literal
5673 construct. (The C99 grammar uses `type-name' instead of `type-id',
5674 but they are essentially the same concept.)
5676 If ADDRESS_P is true, the postfix expression is the operand of the
5677 `&' operator. CAST_P is true if this expression is the target of a
5678 cast.
5680 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5681 class member access expressions [expr.ref].
5683 Returns a representation of the expression. */
5685 static tree
5686 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5687 bool member_access_only_p, bool decltype_p,
5688 cp_id_kind * pidk_return)
5690 cp_token *token;
5691 location_t loc;
5692 enum rid keyword;
5693 cp_id_kind idk = CP_ID_KIND_NONE;
5694 tree postfix_expression = NULL_TREE;
5695 bool is_member_access = false;
5696 int saved_in_statement = -1;
5698 /* Peek at the next token. */
5699 token = cp_lexer_peek_token (parser->lexer);
5700 loc = token->location;
5701 /* Some of the productions are determined by keywords. */
5702 keyword = token->keyword;
5703 switch (keyword)
5705 case RID_DYNCAST:
5706 case RID_STATCAST:
5707 case RID_REINTCAST:
5708 case RID_CONSTCAST:
5710 tree type;
5711 tree expression;
5712 const char *saved_message;
5713 bool saved_in_type_id_in_expr_p;
5715 /* All of these can be handled in the same way from the point
5716 of view of parsing. Begin by consuming the token
5717 identifying the cast. */
5718 cp_lexer_consume_token (parser->lexer);
5720 /* New types cannot be defined in the cast. */
5721 saved_message = parser->type_definition_forbidden_message;
5722 parser->type_definition_forbidden_message
5723 = G_("types may not be defined in casts");
5725 /* Look for the opening `<'. */
5726 cp_parser_require (parser, CPP_LESS, RT_LESS);
5727 /* Parse the type to which we are casting. */
5728 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5729 parser->in_type_id_in_expr_p = true;
5730 type = cp_parser_type_id (parser);
5731 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5732 /* Look for the closing `>'. */
5733 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5734 /* Restore the old message. */
5735 parser->type_definition_forbidden_message = saved_message;
5737 bool saved_greater_than_is_operator_p
5738 = parser->greater_than_is_operator_p;
5739 parser->greater_than_is_operator_p = true;
5741 /* And the expression which is being cast. */
5742 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5743 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5744 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5746 parser->greater_than_is_operator_p
5747 = saved_greater_than_is_operator_p;
5749 /* Only type conversions to integral or enumeration types
5750 can be used in constant-expressions. */
5751 if (!cast_valid_in_integral_constant_expression_p (type)
5752 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5753 return error_mark_node;
5755 switch (keyword)
5757 case RID_DYNCAST:
5758 postfix_expression
5759 = build_dynamic_cast (type, expression, tf_warning_or_error);
5760 break;
5761 case RID_STATCAST:
5762 postfix_expression
5763 = build_static_cast (type, expression, tf_warning_or_error);
5764 break;
5765 case RID_REINTCAST:
5766 postfix_expression
5767 = build_reinterpret_cast (type, expression,
5768 tf_warning_or_error);
5769 break;
5770 case RID_CONSTCAST:
5771 postfix_expression
5772 = build_const_cast (type, expression, tf_warning_or_error);
5773 break;
5774 default:
5775 gcc_unreachable ();
5778 break;
5780 case RID_TYPEID:
5782 tree type;
5783 const char *saved_message;
5784 bool saved_in_type_id_in_expr_p;
5786 /* Consume the `typeid' token. */
5787 cp_lexer_consume_token (parser->lexer);
5788 /* Look for the `(' token. */
5789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5790 /* Types cannot be defined in a `typeid' expression. */
5791 saved_message = parser->type_definition_forbidden_message;
5792 parser->type_definition_forbidden_message
5793 = G_("types may not be defined in a %<typeid%> expression");
5794 /* We can't be sure yet whether we're looking at a type-id or an
5795 expression. */
5796 cp_parser_parse_tentatively (parser);
5797 /* Try a type-id first. */
5798 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5799 parser->in_type_id_in_expr_p = true;
5800 type = cp_parser_type_id (parser);
5801 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5802 /* Look for the `)' token. Otherwise, we can't be sure that
5803 we're not looking at an expression: consider `typeid (int
5804 (3))', for example. */
5805 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5806 /* If all went well, simply lookup the type-id. */
5807 if (cp_parser_parse_definitely (parser))
5808 postfix_expression = get_typeid (type, tf_warning_or_error);
5809 /* Otherwise, fall back to the expression variant. */
5810 else
5812 tree expression;
5814 /* Look for an expression. */
5815 expression = cp_parser_expression (parser, & idk);
5816 /* Compute its typeid. */
5817 postfix_expression = build_typeid (expression, tf_warning_or_error);
5818 /* Look for the `)' token. */
5819 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5821 /* Restore the saved message. */
5822 parser->type_definition_forbidden_message = saved_message;
5823 /* `typeid' may not appear in an integral constant expression. */
5824 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5825 return error_mark_node;
5827 break;
5829 case RID_TYPENAME:
5831 tree type;
5832 /* The syntax permitted here is the same permitted for an
5833 elaborated-type-specifier. */
5834 type = cp_parser_elaborated_type_specifier (parser,
5835 /*is_friend=*/false,
5836 /*is_declaration=*/false);
5837 postfix_expression = cp_parser_functional_cast (parser, type);
5839 break;
5841 case RID_CILK_SPAWN:
5843 cp_lexer_consume_token (parser->lexer);
5844 token = cp_lexer_peek_token (parser->lexer);
5845 if (token->type == CPP_SEMICOLON)
5847 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5848 "an expression");
5849 postfix_expression = error_mark_node;
5850 break;
5852 else if (!current_function_decl)
5854 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5855 "inside a function");
5856 postfix_expression = error_mark_node;
5857 break;
5859 else
5861 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5862 saved_in_statement = parser->in_statement;
5863 parser->in_statement |= IN_CILK_SPAWN;
5865 cfun->calls_cilk_spawn = 1;
5866 postfix_expression =
5867 cp_parser_postfix_expression (parser, false, false,
5868 false, false, &idk);
5869 if (!flag_cilkplus)
5871 error_at (token->location, "-fcilkplus must be enabled to use"
5872 " %<_Cilk_spawn%>");
5873 cfun->calls_cilk_spawn = 0;
5875 else if (saved_in_statement & IN_CILK_SPAWN)
5877 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5878 "are not permitted");
5879 postfix_expression = error_mark_node;
5880 cfun->calls_cilk_spawn = 0;
5882 else
5884 postfix_expression = build_cilk_spawn (token->location,
5885 postfix_expression);
5886 if (postfix_expression != error_mark_node)
5887 SET_EXPR_LOCATION (postfix_expression, input_location);
5888 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5890 break;
5893 case RID_BUILTIN_SHUFFLE:
5895 vec<tree, va_gc> *vec;
5896 unsigned int i;
5897 tree p;
5899 cp_lexer_consume_token (parser->lexer);
5900 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5901 /*cast_p=*/false, /*allow_expansion_p=*/true,
5902 /*non_constant_p=*/NULL);
5903 if (vec == NULL)
5904 return error_mark_node;
5906 FOR_EACH_VEC_ELT (*vec, i, p)
5907 mark_exp_read (p);
5909 if (vec->length () == 2)
5910 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5911 tf_warning_or_error);
5912 else if (vec->length () == 3)
5913 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5914 tf_warning_or_error);
5915 else
5917 error_at (loc, "wrong number of arguments to "
5918 "%<__builtin_shuffle%>");
5919 return error_mark_node;
5921 break;
5924 default:
5926 tree type;
5928 /* If the next thing is a simple-type-specifier, we may be
5929 looking at a functional cast. We could also be looking at
5930 an id-expression. So, we try the functional cast, and if
5931 that doesn't work we fall back to the primary-expression. */
5932 cp_parser_parse_tentatively (parser);
5933 /* Look for the simple-type-specifier. */
5934 type = cp_parser_simple_type_specifier (parser,
5935 /*decl_specs=*/NULL,
5936 CP_PARSER_FLAGS_NONE);
5937 /* Parse the cast itself. */
5938 if (!cp_parser_error_occurred (parser))
5939 postfix_expression
5940 = cp_parser_functional_cast (parser, type);
5941 /* If that worked, we're done. */
5942 if (cp_parser_parse_definitely (parser))
5943 break;
5945 /* If the functional-cast didn't work out, try a
5946 compound-literal. */
5947 if (cp_parser_allow_gnu_extensions_p (parser)
5948 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5950 tree initializer = NULL_TREE;
5952 cp_parser_parse_tentatively (parser);
5954 /* Avoid calling cp_parser_type_id pointlessly, see comment
5955 in cp_parser_cast_expression about c++/29234. */
5956 if (!cp_parser_compound_literal_p (parser))
5957 cp_parser_simulate_error (parser);
5958 else
5960 /* Parse the type. */
5961 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5962 parser->in_type_id_in_expr_p = true;
5963 type = cp_parser_type_id (parser);
5964 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5965 /* Look for the `)'. */
5966 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5969 /* If things aren't going well, there's no need to
5970 keep going. */
5971 if (!cp_parser_error_occurred (parser))
5973 bool non_constant_p;
5974 /* Parse the brace-enclosed initializer list. */
5975 initializer = cp_parser_braced_list (parser,
5976 &non_constant_p);
5978 /* If that worked, we're definitely looking at a
5979 compound-literal expression. */
5980 if (cp_parser_parse_definitely (parser))
5982 /* Warn the user that a compound literal is not
5983 allowed in standard C++. */
5984 pedwarn (input_location, OPT_Wpedantic,
5985 "ISO C++ forbids compound-literals");
5986 /* For simplicity, we disallow compound literals in
5987 constant-expressions. We could
5988 allow compound literals of integer type, whose
5989 initializer was a constant, in constant
5990 expressions. Permitting that usage, as a further
5991 extension, would not change the meaning of any
5992 currently accepted programs. (Of course, as
5993 compound literals are not part of ISO C++, the
5994 standard has nothing to say.) */
5995 if (cp_parser_non_integral_constant_expression (parser,
5996 NIC_NCC))
5998 postfix_expression = error_mark_node;
5999 break;
6001 /* Form the representation of the compound-literal. */
6002 postfix_expression
6003 = finish_compound_literal (type, initializer,
6004 tf_warning_or_error);
6005 break;
6009 /* It must be a primary-expression. */
6010 postfix_expression
6011 = cp_parser_primary_expression (parser, address_p, cast_p,
6012 /*template_arg_p=*/false,
6013 decltype_p,
6014 &idk);
6016 break;
6019 /* Note that we don't need to worry about calling build_cplus_new on a
6020 class-valued CALL_EXPR in decltype when it isn't the end of the
6021 postfix-expression; unary_complex_lvalue will take care of that for
6022 all these cases. */
6024 /* Keep looping until the postfix-expression is complete. */
6025 while (true)
6027 if (idk == CP_ID_KIND_UNQUALIFIED
6028 && identifier_p (postfix_expression)
6029 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6030 /* It is not a Koenig lookup function call. */
6031 postfix_expression
6032 = unqualified_name_lookup_error (postfix_expression);
6034 /* Peek at the next token. */
6035 token = cp_lexer_peek_token (parser->lexer);
6037 switch (token->type)
6039 case CPP_OPEN_SQUARE:
6040 if (cp_next_tokens_can_be_std_attribute_p (parser))
6042 cp_parser_error (parser,
6043 "two consecutive %<[%> shall "
6044 "only introduce an attribute");
6045 return error_mark_node;
6047 postfix_expression
6048 = cp_parser_postfix_open_square_expression (parser,
6049 postfix_expression,
6050 false,
6051 decltype_p);
6052 idk = CP_ID_KIND_NONE;
6053 is_member_access = false;
6054 break;
6056 case CPP_OPEN_PAREN:
6057 /* postfix-expression ( expression-list [opt] ) */
6059 bool koenig_p;
6060 bool is_builtin_constant_p;
6061 bool saved_integral_constant_expression_p = false;
6062 bool saved_non_integral_constant_expression_p = false;
6063 tsubst_flags_t complain = complain_flags (decltype_p);
6064 vec<tree, va_gc> *args;
6066 is_member_access = false;
6068 is_builtin_constant_p
6069 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6070 if (is_builtin_constant_p)
6072 /* The whole point of __builtin_constant_p is to allow
6073 non-constant expressions to appear as arguments. */
6074 saved_integral_constant_expression_p
6075 = parser->integral_constant_expression_p;
6076 saved_non_integral_constant_expression_p
6077 = parser->non_integral_constant_expression_p;
6078 parser->integral_constant_expression_p = false;
6080 args = (cp_parser_parenthesized_expression_list
6081 (parser, non_attr,
6082 /*cast_p=*/false, /*allow_expansion_p=*/true,
6083 /*non_constant_p=*/NULL,
6084 /*want_literal_zero_p=*/warn_memset_transposed_args));
6085 if (is_builtin_constant_p)
6087 parser->integral_constant_expression_p
6088 = saved_integral_constant_expression_p;
6089 parser->non_integral_constant_expression_p
6090 = saved_non_integral_constant_expression_p;
6093 if (args == NULL)
6095 postfix_expression = error_mark_node;
6096 break;
6099 /* Function calls are not permitted in
6100 constant-expressions. */
6101 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6102 && cp_parser_non_integral_constant_expression (parser,
6103 NIC_FUNC_CALL))
6105 postfix_expression = error_mark_node;
6106 release_tree_vector (args);
6107 break;
6110 koenig_p = false;
6111 if (idk == CP_ID_KIND_UNQUALIFIED
6112 || idk == CP_ID_KIND_TEMPLATE_ID)
6114 if (identifier_p (postfix_expression))
6116 if (!args->is_empty ())
6118 koenig_p = true;
6119 if (!any_type_dependent_arguments_p (args))
6120 postfix_expression
6121 = perform_koenig_lookup (postfix_expression, args,
6122 complain);
6124 else
6125 postfix_expression
6126 = unqualified_fn_lookup_error (postfix_expression);
6128 /* We do not perform argument-dependent lookup if
6129 normal lookup finds a non-function, in accordance
6130 with the expected resolution of DR 218. */
6131 else if (!args->is_empty ()
6132 && is_overloaded_fn (postfix_expression))
6134 tree fn = get_first_fn (postfix_expression);
6135 fn = STRIP_TEMPLATE (fn);
6137 /* Do not do argument dependent lookup if regular
6138 lookup finds a member function or a block-scope
6139 function declaration. [basic.lookup.argdep]/3 */
6140 if (!DECL_FUNCTION_MEMBER_P (fn)
6141 && !DECL_LOCAL_FUNCTION_P (fn))
6143 koenig_p = true;
6144 if (!any_type_dependent_arguments_p (args))
6145 postfix_expression
6146 = perform_koenig_lookup (postfix_expression, args,
6147 complain);
6152 if (warn_memset_transposed_args)
6154 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6155 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6156 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6157 && vec_safe_length (args) == 3
6158 && integer_zerop ((*args)[2])
6159 && LITERAL_ZERO_P ((*args)[2])
6160 && !(integer_zerop ((*args)[1])
6161 && LITERAL_ZERO_P ((*args)[1])))
6162 warning (OPT_Wmemset_transposed_args,
6163 "%<memset%> used with constant zero length "
6164 "parameter; this could be due to transposed "
6165 "parameters");
6167 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6168 to avoid leaking those into folder and middle-end. */
6169 unsigned int i;
6170 tree arg;
6171 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6172 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6173 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6176 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6178 tree instance = TREE_OPERAND (postfix_expression, 0);
6179 tree fn = TREE_OPERAND (postfix_expression, 1);
6181 if (processing_template_decl
6182 && (type_dependent_expression_p (instance)
6183 || (!BASELINK_P (fn)
6184 && TREE_CODE (fn) != FIELD_DECL)
6185 || type_dependent_expression_p (fn)
6186 || any_type_dependent_arguments_p (args)))
6188 postfix_expression
6189 = build_nt_call_vec (postfix_expression, args);
6190 release_tree_vector (args);
6191 break;
6194 if (BASELINK_P (fn))
6196 postfix_expression
6197 = (build_new_method_call
6198 (instance, fn, &args, NULL_TREE,
6199 (idk == CP_ID_KIND_QUALIFIED
6200 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6201 : LOOKUP_NORMAL),
6202 /*fn_p=*/NULL,
6203 complain));
6205 else
6206 postfix_expression
6207 = finish_call_expr (postfix_expression, &args,
6208 /*disallow_virtual=*/false,
6209 /*koenig_p=*/false,
6210 complain);
6212 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6213 || TREE_CODE (postfix_expression) == MEMBER_REF
6214 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6215 postfix_expression = (build_offset_ref_call_from_tree
6216 (postfix_expression, &args,
6217 complain));
6218 else if (idk == CP_ID_KIND_QUALIFIED)
6219 /* A call to a static class member, or a namespace-scope
6220 function. */
6221 postfix_expression
6222 = finish_call_expr (postfix_expression, &args,
6223 /*disallow_virtual=*/true,
6224 koenig_p,
6225 complain);
6226 else
6227 /* All other function calls. */
6228 postfix_expression
6229 = finish_call_expr (postfix_expression, &args,
6230 /*disallow_virtual=*/false,
6231 koenig_p,
6232 complain);
6234 protected_set_expr_location (postfix_expression, token->location);
6236 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6237 idk = CP_ID_KIND_NONE;
6239 release_tree_vector (args);
6241 break;
6243 case CPP_DOT:
6244 case CPP_DEREF:
6245 /* postfix-expression . template [opt] id-expression
6246 postfix-expression . pseudo-destructor-name
6247 postfix-expression -> template [opt] id-expression
6248 postfix-expression -> pseudo-destructor-name */
6250 /* Consume the `.' or `->' operator. */
6251 cp_lexer_consume_token (parser->lexer);
6253 postfix_expression
6254 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6255 postfix_expression,
6256 false, &idk, loc);
6258 is_member_access = true;
6259 break;
6261 case CPP_PLUS_PLUS:
6262 /* postfix-expression ++ */
6263 /* Consume the `++' token. */
6264 cp_lexer_consume_token (parser->lexer);
6265 /* Generate a representation for the complete expression. */
6266 postfix_expression
6267 = finish_increment_expr (postfix_expression,
6268 POSTINCREMENT_EXPR);
6269 /* Increments may not appear in constant-expressions. */
6270 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6271 postfix_expression = error_mark_node;
6272 idk = CP_ID_KIND_NONE;
6273 is_member_access = false;
6274 break;
6276 case CPP_MINUS_MINUS:
6277 /* postfix-expression -- */
6278 /* Consume the `--' token. */
6279 cp_lexer_consume_token (parser->lexer);
6280 /* Generate a representation for the complete expression. */
6281 postfix_expression
6282 = finish_increment_expr (postfix_expression,
6283 POSTDECREMENT_EXPR);
6284 /* Decrements may not appear in constant-expressions. */
6285 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6286 postfix_expression = error_mark_node;
6287 idk = CP_ID_KIND_NONE;
6288 is_member_access = false;
6289 break;
6291 default:
6292 if (pidk_return != NULL)
6293 * pidk_return = idk;
6294 if (member_access_only_p)
6295 return is_member_access? postfix_expression : error_mark_node;
6296 else
6297 return postfix_expression;
6301 /* We should never get here. */
6302 gcc_unreachable ();
6303 return error_mark_node;
6306 /* This function parses Cilk Plus array notations. If a normal array expr. is
6307 parsed then the array index is passed back to the caller through *INIT_INDEX
6308 and the function returns a NULL_TREE. If array notation expr. is parsed,
6309 then *INIT_INDEX is ignored by the caller and the function returns
6310 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6311 error_mark_node. */
6313 static tree
6314 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6315 tree array_value)
6317 cp_token *token = NULL;
6318 tree length_index, stride = NULL_TREE, value_tree, array_type;
6319 if (!array_value || array_value == error_mark_node)
6321 cp_parser_skip_to_end_of_statement (parser);
6322 return error_mark_node;
6325 array_type = TREE_TYPE (array_value);
6327 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6328 parser->colon_corrects_to_scope_p = false;
6329 token = cp_lexer_peek_token (parser->lexer);
6331 if (!token)
6333 cp_parser_error (parser, "expected %<:%> or numeral");
6334 return error_mark_node;
6336 else if (token->type == CPP_COLON)
6338 /* Consume the ':'. */
6339 cp_lexer_consume_token (parser->lexer);
6341 /* If we are here, then we have a case like this A[:]. */
6342 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6344 cp_parser_error (parser, "expected %<]%>");
6345 cp_parser_skip_to_end_of_statement (parser);
6346 return error_mark_node;
6348 *init_index = NULL_TREE;
6349 stride = NULL_TREE;
6350 length_index = NULL_TREE;
6352 else
6354 /* If we are here, then there are three valid possibilities:
6355 1. ARRAY [ EXP ]
6356 2. ARRAY [ EXP : EXP ]
6357 3. ARRAY [ EXP : EXP : EXP ] */
6359 *init_index = cp_parser_expression (parser);
6360 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6362 /* This indicates that we have a normal array expression. */
6363 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6364 return NULL_TREE;
6367 /* Consume the ':'. */
6368 cp_lexer_consume_token (parser->lexer);
6369 length_index = cp_parser_expression (parser);
6370 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6372 cp_lexer_consume_token (parser->lexer);
6373 stride = cp_parser_expression (parser);
6376 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6378 if (*init_index == error_mark_node || length_index == error_mark_node
6379 || stride == error_mark_node || array_type == error_mark_node)
6381 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6382 cp_lexer_consume_token (parser->lexer);
6383 return error_mark_node;
6385 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6387 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6388 length_index, stride, array_type);
6389 return value_tree;
6392 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6393 by cp_parser_builtin_offsetof. We're looking for
6395 postfix-expression [ expression ]
6396 postfix-expression [ braced-init-list ] (C++11)
6398 FOR_OFFSETOF is set if we're being called in that context, which
6399 changes how we deal with integer constant expressions. */
6401 static tree
6402 cp_parser_postfix_open_square_expression (cp_parser *parser,
6403 tree postfix_expression,
6404 bool for_offsetof,
6405 bool decltype_p)
6407 tree index = NULL_TREE;
6408 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6409 bool saved_greater_than_is_operator_p;
6411 /* Consume the `[' token. */
6412 cp_lexer_consume_token (parser->lexer);
6414 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6415 parser->greater_than_is_operator_p = true;
6417 /* Parse the index expression. */
6418 /* ??? For offsetof, there is a question of what to allow here. If
6419 offsetof is not being used in an integral constant expression context,
6420 then we *could* get the right answer by computing the value at runtime.
6421 If we are in an integral constant expression context, then we might
6422 could accept any constant expression; hard to say without analysis.
6423 Rather than open the barn door too wide right away, allow only integer
6424 constant expressions here. */
6425 if (for_offsetof)
6426 index = cp_parser_constant_expression (parser, false, NULL);
6427 else
6429 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6431 bool expr_nonconst_p;
6432 cp_lexer_set_source_position (parser->lexer);
6433 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6434 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6435 if (flag_cilkplus
6436 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6438 error_at (cp_lexer_peek_token (parser->lexer)->location,
6439 "braced list index is not allowed with array "
6440 "notation");
6441 cp_parser_skip_to_end_of_statement (parser);
6442 return error_mark_node;
6445 else if (flag_cilkplus)
6447 /* Here are have these two options:
6448 ARRAY[EXP : EXP] - Array notation expr with default
6449 stride of 1.
6450 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6451 stride. */
6452 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6453 postfix_expression);
6454 if (an_exp)
6455 return an_exp;
6457 else
6458 index = cp_parser_expression (parser);
6461 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6463 /* Look for the closing `]'. */
6464 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6466 /* Build the ARRAY_REF. */
6467 postfix_expression = grok_array_decl (loc, postfix_expression,
6468 index, decltype_p);
6470 /* When not doing offsetof, array references are not permitted in
6471 constant-expressions. */
6472 if (!for_offsetof
6473 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6474 postfix_expression = error_mark_node;
6476 return postfix_expression;
6479 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6480 by cp_parser_builtin_offsetof. We're looking for
6482 postfix-expression . template [opt] id-expression
6483 postfix-expression . pseudo-destructor-name
6484 postfix-expression -> template [opt] id-expression
6485 postfix-expression -> pseudo-destructor-name
6487 FOR_OFFSETOF is set if we're being called in that context. That sorta
6488 limits what of the above we'll actually accept, but nevermind.
6489 TOKEN_TYPE is the "." or "->" token, which will already have been
6490 removed from the stream. */
6492 static tree
6493 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6494 enum cpp_ttype token_type,
6495 tree postfix_expression,
6496 bool for_offsetof, cp_id_kind *idk,
6497 location_t location)
6499 tree name;
6500 bool dependent_p;
6501 bool pseudo_destructor_p;
6502 tree scope = NULL_TREE;
6504 /* If this is a `->' operator, dereference the pointer. */
6505 if (token_type == CPP_DEREF)
6506 postfix_expression = build_x_arrow (location, postfix_expression,
6507 tf_warning_or_error);
6508 /* Check to see whether or not the expression is type-dependent. */
6509 dependent_p = type_dependent_expression_p (postfix_expression);
6510 /* The identifier following the `->' or `.' is not qualified. */
6511 parser->scope = NULL_TREE;
6512 parser->qualifying_scope = NULL_TREE;
6513 parser->object_scope = NULL_TREE;
6514 *idk = CP_ID_KIND_NONE;
6516 /* Enter the scope corresponding to the type of the object
6517 given by the POSTFIX_EXPRESSION. */
6518 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6520 scope = TREE_TYPE (postfix_expression);
6521 /* According to the standard, no expression should ever have
6522 reference type. Unfortunately, we do not currently match
6523 the standard in this respect in that our internal representation
6524 of an expression may have reference type even when the standard
6525 says it does not. Therefore, we have to manually obtain the
6526 underlying type here. */
6527 scope = non_reference (scope);
6528 /* The type of the POSTFIX_EXPRESSION must be complete. */
6529 if (scope == unknown_type_node)
6531 error_at (location, "%qE does not have class type",
6532 postfix_expression);
6533 scope = NULL_TREE;
6535 /* Unlike the object expression in other contexts, *this is not
6536 required to be of complete type for purposes of class member
6537 access (5.2.5) outside the member function body. */
6538 else if (postfix_expression != current_class_ref
6539 && !(processing_template_decl && scope == current_class_type))
6540 scope = complete_type_or_else (scope, NULL_TREE);
6541 /* Let the name lookup machinery know that we are processing a
6542 class member access expression. */
6543 parser->context->object_type = scope;
6544 /* If something went wrong, we want to be able to discern that case,
6545 as opposed to the case where there was no SCOPE due to the type
6546 of expression being dependent. */
6547 if (!scope)
6548 scope = error_mark_node;
6549 /* If the SCOPE was erroneous, make the various semantic analysis
6550 functions exit quickly -- and without issuing additional error
6551 messages. */
6552 if (scope == error_mark_node)
6553 postfix_expression = error_mark_node;
6556 /* Assume this expression is not a pseudo-destructor access. */
6557 pseudo_destructor_p = false;
6559 /* If the SCOPE is a scalar type, then, if this is a valid program,
6560 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6561 is type dependent, it can be pseudo-destructor-name or something else.
6562 Try to parse it as pseudo-destructor-name first. */
6563 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6565 tree s;
6566 tree type;
6568 cp_parser_parse_tentatively (parser);
6569 /* Parse the pseudo-destructor-name. */
6570 s = NULL_TREE;
6571 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6572 &s, &type);
6573 if (dependent_p
6574 && (cp_parser_error_occurred (parser)
6575 || !SCALAR_TYPE_P (type)))
6576 cp_parser_abort_tentative_parse (parser);
6577 else if (cp_parser_parse_definitely (parser))
6579 pseudo_destructor_p = true;
6580 postfix_expression
6581 = finish_pseudo_destructor_expr (postfix_expression,
6582 s, type, location);
6586 if (!pseudo_destructor_p)
6588 /* If the SCOPE is not a scalar type, we are looking at an
6589 ordinary class member access expression, rather than a
6590 pseudo-destructor-name. */
6591 bool template_p;
6592 cp_token *token = cp_lexer_peek_token (parser->lexer);
6593 /* Parse the id-expression. */
6594 name = (cp_parser_id_expression
6595 (parser,
6596 cp_parser_optional_template_keyword (parser),
6597 /*check_dependency_p=*/true,
6598 &template_p,
6599 /*declarator_p=*/false,
6600 /*optional_p=*/false));
6601 /* In general, build a SCOPE_REF if the member name is qualified.
6602 However, if the name was not dependent and has already been
6603 resolved; there is no need to build the SCOPE_REF. For example;
6605 struct X { void f(); };
6606 template <typename T> void f(T* t) { t->X::f(); }
6608 Even though "t" is dependent, "X::f" is not and has been resolved
6609 to a BASELINK; there is no need to include scope information. */
6611 /* But we do need to remember that there was an explicit scope for
6612 virtual function calls. */
6613 if (parser->scope)
6614 *idk = CP_ID_KIND_QUALIFIED;
6616 /* If the name is a template-id that names a type, we will get a
6617 TYPE_DECL here. That is invalid code. */
6618 if (TREE_CODE (name) == TYPE_DECL)
6620 error_at (token->location, "invalid use of %qD", name);
6621 postfix_expression = error_mark_node;
6623 else
6625 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6627 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6629 error_at (token->location, "%<%D::%D%> is not a class member",
6630 parser->scope, name);
6631 postfix_expression = error_mark_node;
6633 else
6634 name = build_qualified_name (/*type=*/NULL_TREE,
6635 parser->scope,
6636 name,
6637 template_p);
6638 parser->scope = NULL_TREE;
6639 parser->qualifying_scope = NULL_TREE;
6640 parser->object_scope = NULL_TREE;
6642 if (parser->scope && name && BASELINK_P (name))
6643 adjust_result_of_qualified_name_lookup
6644 (name, parser->scope, scope);
6645 postfix_expression
6646 = finish_class_member_access_expr (postfix_expression, name,
6647 template_p,
6648 tf_warning_or_error);
6652 /* We no longer need to look up names in the scope of the object on
6653 the left-hand side of the `.' or `->' operator. */
6654 parser->context->object_type = NULL_TREE;
6656 /* Outside of offsetof, these operators may not appear in
6657 constant-expressions. */
6658 if (!for_offsetof
6659 && (cp_parser_non_integral_constant_expression
6660 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6661 postfix_expression = error_mark_node;
6663 return postfix_expression;
6666 /* Cache of LITERAL_ZERO_P constants. */
6668 static GTY(()) tree literal_zeros[itk_none];
6670 /* Parse a parenthesized expression-list.
6672 expression-list:
6673 assignment-expression
6674 expression-list, assignment-expression
6676 attribute-list:
6677 expression-list
6678 identifier
6679 identifier, expression-list
6681 CAST_P is true if this expression is the target of a cast.
6683 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6684 argument pack.
6686 Returns a vector of trees. Each element is a representation of an
6687 assignment-expression. NULL is returned if the ( and or ) are
6688 missing. An empty, but allocated, vector is returned on no
6689 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6690 if we are parsing an attribute list for an attribute that wants a
6691 plain identifier argument, normal_attr for an attribute that wants
6692 an expression, or non_attr if we aren't parsing an attribute list. If
6693 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6694 not all of the expressions in the list were constant.
6695 WANT_LITERAL_ZERO_P is true if the caller is interested in
6696 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6697 immediately, this can be removed. */
6699 static vec<tree, va_gc> *
6700 cp_parser_parenthesized_expression_list (cp_parser* parser,
6701 int is_attribute_list,
6702 bool cast_p,
6703 bool allow_expansion_p,
6704 bool *non_constant_p,
6705 bool want_literal_zero_p)
6707 vec<tree, va_gc> *expression_list;
6708 bool fold_expr_p = is_attribute_list != non_attr;
6709 tree identifier = NULL_TREE;
6710 bool saved_greater_than_is_operator_p;
6712 /* Assume all the expressions will be constant. */
6713 if (non_constant_p)
6714 *non_constant_p = false;
6716 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6717 return NULL;
6719 expression_list = make_tree_vector ();
6721 /* Within a parenthesized expression, a `>' token is always
6722 the greater-than operator. */
6723 saved_greater_than_is_operator_p
6724 = parser->greater_than_is_operator_p;
6725 parser->greater_than_is_operator_p = true;
6727 /* Consume expressions until there are no more. */
6728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6729 while (true)
6731 tree expr;
6733 /* At the beginning of attribute lists, check to see if the
6734 next token is an identifier. */
6735 if (is_attribute_list == id_attr
6736 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6738 cp_token *token;
6740 /* Consume the identifier. */
6741 token = cp_lexer_consume_token (parser->lexer);
6742 /* Save the identifier. */
6743 identifier = token->u.value;
6745 else
6747 bool expr_non_constant_p;
6749 /* Parse the next assignment-expression. */
6750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6752 /* A braced-init-list. */
6753 cp_lexer_set_source_position (parser->lexer);
6754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6755 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6756 if (non_constant_p && expr_non_constant_p)
6757 *non_constant_p = true;
6759 else if (non_constant_p)
6761 expr = (cp_parser_constant_expression
6762 (parser, /*allow_non_constant_p=*/true,
6763 &expr_non_constant_p));
6764 if (expr_non_constant_p)
6765 *non_constant_p = true;
6767 else
6769 expr = NULL_TREE;
6770 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6771 switch (tok->type)
6773 case CPP_NUMBER:
6774 case CPP_CHAR:
6775 case CPP_WCHAR:
6776 case CPP_CHAR16:
6777 case CPP_CHAR32:
6778 /* If a parameter is literal zero alone, remember it
6779 for -Wmemset-transposed-args warning. */
6780 if (integer_zerop (tok->u.value)
6781 && !TREE_OVERFLOW (tok->u.value)
6782 && want_literal_zero_p
6783 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6784 == CPP_COMMA
6785 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6786 == CPP_CLOSE_PAREN))
6788 unsigned int i;
6789 for (i = 0; i < itk_none; ++i)
6790 if (TREE_TYPE (tok->u.value) == integer_types[i])
6791 break;
6792 if (i < itk_none && literal_zeros[i])
6793 expr = literal_zeros[i];
6794 else
6796 expr = copy_node (tok->u.value);
6797 LITERAL_ZERO_P (expr) = 1;
6798 if (i < itk_none)
6799 literal_zeros[i] = expr;
6801 /* Consume the 0 token (or '\0', 0LL etc.). */
6802 cp_lexer_consume_token (parser->lexer);
6804 break;
6805 default:
6806 break;
6808 if (expr == NULL_TREE)
6809 expr = cp_parser_assignment_expression (parser, cast_p,
6810 NULL);
6813 if (fold_expr_p)
6814 expr = fold_non_dependent_expr (expr);
6816 /* If we have an ellipsis, then this is an expression
6817 expansion. */
6818 if (allow_expansion_p
6819 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6821 /* Consume the `...'. */
6822 cp_lexer_consume_token (parser->lexer);
6824 /* Build the argument pack. */
6825 expr = make_pack_expansion (expr);
6828 /* Add it to the list. We add error_mark_node
6829 expressions to the list, so that we can still tell if
6830 the correct form for a parenthesized expression-list
6831 is found. That gives better errors. */
6832 vec_safe_push (expression_list, expr);
6834 if (expr == error_mark_node)
6835 goto skip_comma;
6838 /* After the first item, attribute lists look the same as
6839 expression lists. */
6840 is_attribute_list = non_attr;
6842 get_comma:;
6843 /* If the next token isn't a `,', then we are done. */
6844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6845 break;
6847 /* Otherwise, consume the `,' and keep going. */
6848 cp_lexer_consume_token (parser->lexer);
6851 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6853 int ending;
6855 skip_comma:;
6856 /* We try and resync to an unnested comma, as that will give the
6857 user better diagnostics. */
6858 ending = cp_parser_skip_to_closing_parenthesis (parser,
6859 /*recovering=*/true,
6860 /*or_comma=*/true,
6861 /*consume_paren=*/true);
6862 if (ending < 0)
6863 goto get_comma;
6864 if (!ending)
6866 parser->greater_than_is_operator_p
6867 = saved_greater_than_is_operator_p;
6868 return NULL;
6872 parser->greater_than_is_operator_p
6873 = saved_greater_than_is_operator_p;
6875 if (identifier)
6876 vec_safe_insert (expression_list, 0, identifier);
6878 return expression_list;
6881 /* Parse a pseudo-destructor-name.
6883 pseudo-destructor-name:
6884 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6885 :: [opt] nested-name-specifier template template-id :: ~ type-name
6886 :: [opt] nested-name-specifier [opt] ~ type-name
6888 If either of the first two productions is used, sets *SCOPE to the
6889 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6890 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6891 or ERROR_MARK_NODE if the parse fails. */
6893 static void
6894 cp_parser_pseudo_destructor_name (cp_parser* parser,
6895 tree object,
6896 tree* scope,
6897 tree* type)
6899 bool nested_name_specifier_p;
6901 /* Handle ~auto. */
6902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6903 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6904 && !type_dependent_expression_p (object))
6906 if (cxx_dialect < cxx14)
6907 pedwarn (input_location, 0,
6908 "%<~auto%> only available with "
6909 "-std=c++14 or -std=gnu++14");
6910 cp_lexer_consume_token (parser->lexer);
6911 cp_lexer_consume_token (parser->lexer);
6912 *scope = NULL_TREE;
6913 *type = TREE_TYPE (object);
6914 return;
6917 /* Assume that things will not work out. */
6918 *type = error_mark_node;
6920 /* Look for the optional `::' operator. */
6921 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6922 /* Look for the optional nested-name-specifier. */
6923 nested_name_specifier_p
6924 = (cp_parser_nested_name_specifier_opt (parser,
6925 /*typename_keyword_p=*/false,
6926 /*check_dependency_p=*/true,
6927 /*type_p=*/false,
6928 /*is_declaration=*/false)
6929 != NULL_TREE);
6930 /* Now, if we saw a nested-name-specifier, we might be doing the
6931 second production. */
6932 if (nested_name_specifier_p
6933 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6935 /* Consume the `template' keyword. */
6936 cp_lexer_consume_token (parser->lexer);
6937 /* Parse the template-id. */
6938 cp_parser_template_id (parser,
6939 /*template_keyword_p=*/true,
6940 /*check_dependency_p=*/false,
6941 class_type,
6942 /*is_declaration=*/true);
6943 /* Look for the `::' token. */
6944 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6946 /* If the next token is not a `~', then there might be some
6947 additional qualification. */
6948 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6950 /* At this point, we're looking for "type-name :: ~". The type-name
6951 must not be a class-name, since this is a pseudo-destructor. So,
6952 it must be either an enum-name, or a typedef-name -- both of which
6953 are just identifiers. So, we peek ahead to check that the "::"
6954 and "~" tokens are present; if they are not, then we can avoid
6955 calling type_name. */
6956 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6957 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6958 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6960 cp_parser_error (parser, "non-scalar type");
6961 return;
6964 /* Look for the type-name. */
6965 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6966 if (*scope == error_mark_node)
6967 return;
6969 /* Look for the `::' token. */
6970 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6972 else
6973 *scope = NULL_TREE;
6975 /* Look for the `~'. */
6976 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6978 /* Once we see the ~, this has to be a pseudo-destructor. */
6979 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6980 cp_parser_commit_to_topmost_tentative_parse (parser);
6982 /* Look for the type-name again. We are not responsible for
6983 checking that it matches the first type-name. */
6984 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6987 /* Parse a unary-expression.
6989 unary-expression:
6990 postfix-expression
6991 ++ cast-expression
6992 -- cast-expression
6993 unary-operator cast-expression
6994 sizeof unary-expression
6995 sizeof ( type-id )
6996 alignof ( type-id ) [C++0x]
6997 new-expression
6998 delete-expression
7000 GNU Extensions:
7002 unary-expression:
7003 __extension__ cast-expression
7004 __alignof__ unary-expression
7005 __alignof__ ( type-id )
7006 alignof unary-expression [C++0x]
7007 __real__ cast-expression
7008 __imag__ cast-expression
7009 && identifier
7010 sizeof ( type-id ) { initializer-list , [opt] }
7011 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7012 __alignof__ ( type-id ) { initializer-list , [opt] }
7014 ADDRESS_P is true iff the unary-expression is appearing as the
7015 operand of the `&' operator. CAST_P is true if this expression is
7016 the target of a cast.
7018 Returns a representation of the expression. */
7020 static tree
7021 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7022 bool decltype_p, cp_id_kind * pidk)
7024 cp_token *token;
7025 enum tree_code unary_operator;
7027 /* Peek at the next token. */
7028 token = cp_lexer_peek_token (parser->lexer);
7029 /* Some keywords give away the kind of expression. */
7030 if (token->type == CPP_KEYWORD)
7032 enum rid keyword = token->keyword;
7034 switch (keyword)
7036 case RID_ALIGNOF:
7037 case RID_SIZEOF:
7039 tree operand, ret;
7040 enum tree_code op;
7041 location_t first_loc;
7043 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7044 /* Consume the token. */
7045 cp_lexer_consume_token (parser->lexer);
7046 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7047 /* Parse the operand. */
7048 operand = cp_parser_sizeof_operand (parser, keyword);
7050 if (TYPE_P (operand))
7051 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7052 else
7054 /* ISO C++ defines alignof only with types, not with
7055 expressions. So pedwarn if alignof is used with a non-
7056 type expression. However, __alignof__ is ok. */
7057 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7058 pedwarn (token->location, OPT_Wpedantic,
7059 "ISO C++ does not allow %<alignof%> "
7060 "with a non-type");
7062 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7064 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7065 SIZEOF_EXPR with the original operand. */
7066 if (op == SIZEOF_EXPR && ret != error_mark_node)
7068 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7070 if (!processing_template_decl && TYPE_P (operand))
7072 ret = build_min (SIZEOF_EXPR, size_type_node,
7073 build1 (NOP_EXPR, operand,
7074 error_mark_node));
7075 SIZEOF_EXPR_TYPE_P (ret) = 1;
7077 else
7078 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7079 TREE_SIDE_EFFECTS (ret) = 0;
7080 TREE_READONLY (ret) = 1;
7082 SET_EXPR_LOCATION (ret, first_loc);
7084 return ret;
7087 case RID_NEW:
7088 return cp_parser_new_expression (parser);
7090 case RID_DELETE:
7091 return cp_parser_delete_expression (parser);
7093 case RID_EXTENSION:
7095 /* The saved value of the PEDANTIC flag. */
7096 int saved_pedantic;
7097 tree expr;
7099 /* Save away the PEDANTIC flag. */
7100 cp_parser_extension_opt (parser, &saved_pedantic);
7101 /* Parse the cast-expression. */
7102 expr = cp_parser_simple_cast_expression (parser);
7103 /* Restore the PEDANTIC flag. */
7104 pedantic = saved_pedantic;
7106 return expr;
7109 case RID_REALPART:
7110 case RID_IMAGPART:
7112 tree expression;
7114 /* Consume the `__real__' or `__imag__' token. */
7115 cp_lexer_consume_token (parser->lexer);
7116 /* Parse the cast-expression. */
7117 expression = cp_parser_simple_cast_expression (parser);
7118 /* Create the complete representation. */
7119 return build_x_unary_op (token->location,
7120 (keyword == RID_REALPART
7121 ? REALPART_EXPR : IMAGPART_EXPR),
7122 expression,
7123 tf_warning_or_error);
7125 break;
7127 case RID_TRANSACTION_ATOMIC:
7128 case RID_TRANSACTION_RELAXED:
7129 return cp_parser_transaction_expression (parser, keyword);
7131 case RID_NOEXCEPT:
7133 tree expr;
7134 const char *saved_message;
7135 bool saved_integral_constant_expression_p;
7136 bool saved_non_integral_constant_expression_p;
7137 bool saved_greater_than_is_operator_p;
7139 cp_lexer_consume_token (parser->lexer);
7140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7142 saved_message = parser->type_definition_forbidden_message;
7143 parser->type_definition_forbidden_message
7144 = G_("types may not be defined in %<noexcept%> expressions");
7146 saved_integral_constant_expression_p
7147 = parser->integral_constant_expression_p;
7148 saved_non_integral_constant_expression_p
7149 = parser->non_integral_constant_expression_p;
7150 parser->integral_constant_expression_p = false;
7152 saved_greater_than_is_operator_p
7153 = parser->greater_than_is_operator_p;
7154 parser->greater_than_is_operator_p = true;
7156 ++cp_unevaluated_operand;
7157 ++c_inhibit_evaluation_warnings;
7158 expr = cp_parser_expression (parser);
7159 --c_inhibit_evaluation_warnings;
7160 --cp_unevaluated_operand;
7162 parser->greater_than_is_operator_p
7163 = saved_greater_than_is_operator_p;
7165 parser->integral_constant_expression_p
7166 = saved_integral_constant_expression_p;
7167 parser->non_integral_constant_expression_p
7168 = saved_non_integral_constant_expression_p;
7170 parser->type_definition_forbidden_message = saved_message;
7172 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7173 return finish_noexcept_expr (expr, tf_warning_or_error);
7176 default:
7177 break;
7181 /* Look for the `:: new' and `:: delete', which also signal the
7182 beginning of a new-expression, or delete-expression,
7183 respectively. If the next token is `::', then it might be one of
7184 these. */
7185 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7187 enum rid keyword;
7189 /* See if the token after the `::' is one of the keywords in
7190 which we're interested. */
7191 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7192 /* If it's `new', we have a new-expression. */
7193 if (keyword == RID_NEW)
7194 return cp_parser_new_expression (parser);
7195 /* Similarly, for `delete'. */
7196 else if (keyword == RID_DELETE)
7197 return cp_parser_delete_expression (parser);
7200 /* Look for a unary operator. */
7201 unary_operator = cp_parser_unary_operator (token);
7202 /* The `++' and `--' operators can be handled similarly, even though
7203 they are not technically unary-operators in the grammar. */
7204 if (unary_operator == ERROR_MARK)
7206 if (token->type == CPP_PLUS_PLUS)
7207 unary_operator = PREINCREMENT_EXPR;
7208 else if (token->type == CPP_MINUS_MINUS)
7209 unary_operator = PREDECREMENT_EXPR;
7210 /* Handle the GNU address-of-label extension. */
7211 else if (cp_parser_allow_gnu_extensions_p (parser)
7212 && token->type == CPP_AND_AND)
7214 tree identifier;
7215 tree expression;
7216 location_t loc = token->location;
7218 /* Consume the '&&' token. */
7219 cp_lexer_consume_token (parser->lexer);
7220 /* Look for the identifier. */
7221 identifier = cp_parser_identifier (parser);
7222 /* Create an expression representing the address. */
7223 expression = finish_label_address_expr (identifier, loc);
7224 if (cp_parser_non_integral_constant_expression (parser,
7225 NIC_ADDR_LABEL))
7226 expression = error_mark_node;
7227 return expression;
7230 if (unary_operator != ERROR_MARK)
7232 tree cast_expression;
7233 tree expression = error_mark_node;
7234 non_integral_constant non_constant_p = NIC_NONE;
7235 location_t loc = token->location;
7236 tsubst_flags_t complain = complain_flags (decltype_p);
7238 /* Consume the operator token. */
7239 token = cp_lexer_consume_token (parser->lexer);
7240 /* Parse the cast-expression. */
7241 cast_expression
7242 = cp_parser_cast_expression (parser,
7243 unary_operator == ADDR_EXPR,
7244 /*cast_p=*/false,
7245 /*decltype*/false,
7246 pidk);
7247 /* Now, build an appropriate representation. */
7248 switch (unary_operator)
7250 case INDIRECT_REF:
7251 non_constant_p = NIC_STAR;
7252 expression = build_x_indirect_ref (loc, cast_expression,
7253 RO_UNARY_STAR,
7254 complain);
7255 break;
7257 case ADDR_EXPR:
7258 non_constant_p = NIC_ADDR;
7259 /* Fall through. */
7260 case BIT_NOT_EXPR:
7261 expression = build_x_unary_op (loc, unary_operator,
7262 cast_expression,
7263 complain);
7264 break;
7266 case PREINCREMENT_EXPR:
7267 case PREDECREMENT_EXPR:
7268 non_constant_p = unary_operator == PREINCREMENT_EXPR
7269 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7270 /* Fall through. */
7271 case UNARY_PLUS_EXPR:
7272 case NEGATE_EXPR:
7273 case TRUTH_NOT_EXPR:
7274 expression = finish_unary_op_expr (loc, unary_operator,
7275 cast_expression, complain);
7276 break;
7278 default:
7279 gcc_unreachable ();
7282 if (non_constant_p != NIC_NONE
7283 && cp_parser_non_integral_constant_expression (parser,
7284 non_constant_p))
7285 expression = error_mark_node;
7287 return expression;
7290 return cp_parser_postfix_expression (parser, address_p, cast_p,
7291 /*member_access_only_p=*/false,
7292 decltype_p,
7293 pidk);
7296 static inline tree
7297 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7298 cp_id_kind * pidk)
7300 return cp_parser_unary_expression (parser, address_p, cast_p,
7301 /*decltype*/false, pidk);
7304 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7305 unary-operator, the corresponding tree code is returned. */
7307 static enum tree_code
7308 cp_parser_unary_operator (cp_token* token)
7310 switch (token->type)
7312 case CPP_MULT:
7313 return INDIRECT_REF;
7315 case CPP_AND:
7316 return ADDR_EXPR;
7318 case CPP_PLUS:
7319 return UNARY_PLUS_EXPR;
7321 case CPP_MINUS:
7322 return NEGATE_EXPR;
7324 case CPP_NOT:
7325 return TRUTH_NOT_EXPR;
7327 case CPP_COMPL:
7328 return BIT_NOT_EXPR;
7330 default:
7331 return ERROR_MARK;
7335 /* Parse a new-expression.
7337 new-expression:
7338 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7339 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7341 Returns a representation of the expression. */
7343 static tree
7344 cp_parser_new_expression (cp_parser* parser)
7346 bool global_scope_p;
7347 vec<tree, va_gc> *placement;
7348 tree type;
7349 vec<tree, va_gc> *initializer;
7350 tree nelts = NULL_TREE;
7351 tree ret;
7353 /* Look for the optional `::' operator. */
7354 global_scope_p
7355 = (cp_parser_global_scope_opt (parser,
7356 /*current_scope_valid_p=*/false)
7357 != NULL_TREE);
7358 /* Look for the `new' operator. */
7359 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7360 /* There's no easy way to tell a new-placement from the
7361 `( type-id )' construct. */
7362 cp_parser_parse_tentatively (parser);
7363 /* Look for a new-placement. */
7364 placement = cp_parser_new_placement (parser);
7365 /* If that didn't work out, there's no new-placement. */
7366 if (!cp_parser_parse_definitely (parser))
7368 if (placement != NULL)
7369 release_tree_vector (placement);
7370 placement = NULL;
7373 /* If the next token is a `(', then we have a parenthesized
7374 type-id. */
7375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7377 cp_token *token;
7378 const char *saved_message = parser->type_definition_forbidden_message;
7380 /* Consume the `('. */
7381 cp_lexer_consume_token (parser->lexer);
7383 /* Parse the type-id. */
7384 parser->type_definition_forbidden_message
7385 = G_("types may not be defined in a new-expression");
7386 type = cp_parser_type_id (parser);
7387 parser->type_definition_forbidden_message = saved_message;
7389 /* Look for the closing `)'. */
7390 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7391 token = cp_lexer_peek_token (parser->lexer);
7392 /* There should not be a direct-new-declarator in this production,
7393 but GCC used to allowed this, so we check and emit a sensible error
7394 message for this case. */
7395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7397 error_at (token->location,
7398 "array bound forbidden after parenthesized type-id");
7399 inform (token->location,
7400 "try removing the parentheses around the type-id");
7401 cp_parser_direct_new_declarator (parser);
7404 /* Otherwise, there must be a new-type-id. */
7405 else
7406 type = cp_parser_new_type_id (parser, &nelts);
7408 /* If the next token is a `(' or '{', then we have a new-initializer. */
7409 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7410 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7411 initializer = cp_parser_new_initializer (parser);
7412 else
7413 initializer = NULL;
7415 /* A new-expression may not appear in an integral constant
7416 expression. */
7417 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7418 ret = error_mark_node;
7419 else
7421 /* Create a representation of the new-expression. */
7422 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7423 tf_warning_or_error);
7426 if (placement != NULL)
7427 release_tree_vector (placement);
7428 if (initializer != NULL)
7429 release_tree_vector (initializer);
7431 return ret;
7434 /* Parse a new-placement.
7436 new-placement:
7437 ( expression-list )
7439 Returns the same representation as for an expression-list. */
7441 static vec<tree, va_gc> *
7442 cp_parser_new_placement (cp_parser* parser)
7444 vec<tree, va_gc> *expression_list;
7446 /* Parse the expression-list. */
7447 expression_list = (cp_parser_parenthesized_expression_list
7448 (parser, non_attr, /*cast_p=*/false,
7449 /*allow_expansion_p=*/true,
7450 /*non_constant_p=*/NULL));
7452 return expression_list;
7455 /* Parse a new-type-id.
7457 new-type-id:
7458 type-specifier-seq new-declarator [opt]
7460 Returns the TYPE allocated. If the new-type-id indicates an array
7461 type, *NELTS is set to the number of elements in the last array
7462 bound; the TYPE will not include the last array bound. */
7464 static tree
7465 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7467 cp_decl_specifier_seq type_specifier_seq;
7468 cp_declarator *new_declarator;
7469 cp_declarator *declarator;
7470 cp_declarator *outer_declarator;
7471 const char *saved_message;
7473 /* The type-specifier sequence must not contain type definitions.
7474 (It cannot contain declarations of new types either, but if they
7475 are not definitions we will catch that because they are not
7476 complete.) */
7477 saved_message = parser->type_definition_forbidden_message;
7478 parser->type_definition_forbidden_message
7479 = G_("types may not be defined in a new-type-id");
7480 /* Parse the type-specifier-seq. */
7481 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7482 /*is_trailing_return=*/false,
7483 &type_specifier_seq);
7484 /* Restore the old message. */
7485 parser->type_definition_forbidden_message = saved_message;
7487 if (type_specifier_seq.type == error_mark_node)
7488 return error_mark_node;
7490 /* Parse the new-declarator. */
7491 new_declarator = cp_parser_new_declarator_opt (parser);
7493 /* Determine the number of elements in the last array dimension, if
7494 any. */
7495 *nelts = NULL_TREE;
7496 /* Skip down to the last array dimension. */
7497 declarator = new_declarator;
7498 outer_declarator = NULL;
7499 while (declarator && (declarator->kind == cdk_pointer
7500 || declarator->kind == cdk_ptrmem))
7502 outer_declarator = declarator;
7503 declarator = declarator->declarator;
7505 while (declarator
7506 && declarator->kind == cdk_array
7507 && declarator->declarator
7508 && declarator->declarator->kind == cdk_array)
7510 outer_declarator = declarator;
7511 declarator = declarator->declarator;
7514 if (declarator && declarator->kind == cdk_array)
7516 *nelts = declarator->u.array.bounds;
7517 if (*nelts == error_mark_node)
7518 *nelts = integer_one_node;
7520 if (outer_declarator)
7521 outer_declarator->declarator = declarator->declarator;
7522 else
7523 new_declarator = NULL;
7526 return groktypename (&type_specifier_seq, new_declarator, false);
7529 /* Parse an (optional) new-declarator.
7531 new-declarator:
7532 ptr-operator new-declarator [opt]
7533 direct-new-declarator
7535 Returns the declarator. */
7537 static cp_declarator *
7538 cp_parser_new_declarator_opt (cp_parser* parser)
7540 enum tree_code code;
7541 tree type, std_attributes = NULL_TREE;
7542 cp_cv_quals cv_quals;
7544 /* We don't know if there's a ptr-operator next, or not. */
7545 cp_parser_parse_tentatively (parser);
7546 /* Look for a ptr-operator. */
7547 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7548 /* If that worked, look for more new-declarators. */
7549 if (cp_parser_parse_definitely (parser))
7551 cp_declarator *declarator;
7553 /* Parse another optional declarator. */
7554 declarator = cp_parser_new_declarator_opt (parser);
7556 declarator = cp_parser_make_indirect_declarator
7557 (code, type, cv_quals, declarator, std_attributes);
7559 return declarator;
7562 /* If the next token is a `[', there is a direct-new-declarator. */
7563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7564 return cp_parser_direct_new_declarator (parser);
7566 return NULL;
7569 /* Parse a direct-new-declarator.
7571 direct-new-declarator:
7572 [ expression ]
7573 direct-new-declarator [constant-expression]
7577 static cp_declarator *
7578 cp_parser_direct_new_declarator (cp_parser* parser)
7580 cp_declarator *declarator = NULL;
7582 while (true)
7584 tree expression;
7585 cp_token *token;
7587 /* Look for the opening `['. */
7588 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7590 token = cp_lexer_peek_token (parser->lexer);
7591 expression = cp_parser_expression (parser);
7592 /* The standard requires that the expression have integral
7593 type. DR 74 adds enumeration types. We believe that the
7594 real intent is that these expressions be handled like the
7595 expression in a `switch' condition, which also allows
7596 classes with a single conversion to integral or
7597 enumeration type. */
7598 if (!processing_template_decl)
7600 expression
7601 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7602 expression,
7603 /*complain=*/true);
7604 if (!expression)
7606 error_at (token->location,
7607 "expression in new-declarator must have integral "
7608 "or enumeration type");
7609 expression = error_mark_node;
7613 /* Look for the closing `]'. */
7614 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7616 /* Add this bound to the declarator. */
7617 declarator = make_array_declarator (declarator, expression);
7619 /* If the next token is not a `[', then there are no more
7620 bounds. */
7621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7622 break;
7625 return declarator;
7628 /* Parse a new-initializer.
7630 new-initializer:
7631 ( expression-list [opt] )
7632 braced-init-list
7634 Returns a representation of the expression-list. */
7636 static vec<tree, va_gc> *
7637 cp_parser_new_initializer (cp_parser* parser)
7639 vec<tree, va_gc> *expression_list;
7641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7643 tree t;
7644 bool expr_non_constant_p;
7645 cp_lexer_set_source_position (parser->lexer);
7646 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7647 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7648 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7649 expression_list = make_tree_vector_single (t);
7651 else
7652 expression_list = (cp_parser_parenthesized_expression_list
7653 (parser, non_attr, /*cast_p=*/false,
7654 /*allow_expansion_p=*/true,
7655 /*non_constant_p=*/NULL));
7657 return expression_list;
7660 /* Parse a delete-expression.
7662 delete-expression:
7663 :: [opt] delete cast-expression
7664 :: [opt] delete [ ] cast-expression
7666 Returns a representation of the expression. */
7668 static tree
7669 cp_parser_delete_expression (cp_parser* parser)
7671 bool global_scope_p;
7672 bool array_p;
7673 tree expression;
7675 /* Look for the optional `::' operator. */
7676 global_scope_p
7677 = (cp_parser_global_scope_opt (parser,
7678 /*current_scope_valid_p=*/false)
7679 != NULL_TREE);
7680 /* Look for the `delete' keyword. */
7681 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7682 /* See if the array syntax is in use. */
7683 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7685 /* Consume the `[' token. */
7686 cp_lexer_consume_token (parser->lexer);
7687 /* Look for the `]' token. */
7688 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7689 /* Remember that this is the `[]' construct. */
7690 array_p = true;
7692 else
7693 array_p = false;
7695 /* Parse the cast-expression. */
7696 expression = cp_parser_simple_cast_expression (parser);
7698 /* A delete-expression may not appear in an integral constant
7699 expression. */
7700 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7701 return error_mark_node;
7703 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7704 tf_warning_or_error);
7707 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7708 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7709 0 otherwise. */
7711 static int
7712 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7714 cp_token *token = cp_lexer_peek_token (parser->lexer);
7715 switch (token->type)
7717 case CPP_COMMA:
7718 case CPP_SEMICOLON:
7719 case CPP_QUERY:
7720 case CPP_COLON:
7721 case CPP_CLOSE_SQUARE:
7722 case CPP_CLOSE_PAREN:
7723 case CPP_CLOSE_BRACE:
7724 case CPP_OPEN_BRACE:
7725 case CPP_DOT:
7726 case CPP_DOT_STAR:
7727 case CPP_DEREF:
7728 case CPP_DEREF_STAR:
7729 case CPP_DIV:
7730 case CPP_MOD:
7731 case CPP_LSHIFT:
7732 case CPP_RSHIFT:
7733 case CPP_LESS:
7734 case CPP_GREATER:
7735 case CPP_LESS_EQ:
7736 case CPP_GREATER_EQ:
7737 case CPP_EQ_EQ:
7738 case CPP_NOT_EQ:
7739 case CPP_EQ:
7740 case CPP_MULT_EQ:
7741 case CPP_DIV_EQ:
7742 case CPP_MOD_EQ:
7743 case CPP_PLUS_EQ:
7744 case CPP_MINUS_EQ:
7745 case CPP_RSHIFT_EQ:
7746 case CPP_LSHIFT_EQ:
7747 case CPP_AND_EQ:
7748 case CPP_XOR_EQ:
7749 case CPP_OR_EQ:
7750 case CPP_XOR:
7751 case CPP_OR:
7752 case CPP_OR_OR:
7753 case CPP_EOF:
7754 case CPP_ELLIPSIS:
7755 return 0;
7757 case CPP_OPEN_PAREN:
7758 /* In ((type ()) () the last () isn't a valid cast-expression,
7759 so the whole must be parsed as postfix-expression. */
7760 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7761 != CPP_CLOSE_PAREN;
7763 case CPP_OPEN_SQUARE:
7764 /* '[' may start a primary-expression in obj-c++ and in C++11,
7765 as a lambda-expression, eg, '(void)[]{}'. */
7766 if (cxx_dialect >= cxx11)
7767 return -1;
7768 return c_dialect_objc ();
7770 case CPP_PLUS_PLUS:
7771 case CPP_MINUS_MINUS:
7772 /* '++' and '--' may or may not start a cast-expression:
7774 struct T { void operator++(int); };
7775 void f() { (T())++; }
7779 int a;
7780 (int)++a; */
7781 return -1;
7783 default:
7784 return 1;
7788 /* Parse a cast-expression.
7790 cast-expression:
7791 unary-expression
7792 ( type-id ) cast-expression
7794 ADDRESS_P is true iff the unary-expression is appearing as the
7795 operand of the `&' operator. CAST_P is true if this expression is
7796 the target of a cast.
7798 Returns a representation of the expression. */
7800 static tree
7801 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7802 bool decltype_p, cp_id_kind * pidk)
7804 /* If it's a `(', then we might be looking at a cast. */
7805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7807 tree type = NULL_TREE;
7808 tree expr = NULL_TREE;
7809 int cast_expression = 0;
7810 const char *saved_message;
7812 /* There's no way to know yet whether or not this is a cast.
7813 For example, `(int (3))' is a unary-expression, while `(int)
7814 3' is a cast. So, we resort to parsing tentatively. */
7815 cp_parser_parse_tentatively (parser);
7816 /* Types may not be defined in a cast. */
7817 saved_message = parser->type_definition_forbidden_message;
7818 parser->type_definition_forbidden_message
7819 = G_("types may not be defined in casts");
7820 /* Consume the `('. */
7821 cp_lexer_consume_token (parser->lexer);
7822 /* A very tricky bit is that `(struct S) { 3 }' is a
7823 compound-literal (which we permit in C++ as an extension).
7824 But, that construct is not a cast-expression -- it is a
7825 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7826 is legal; if the compound-literal were a cast-expression,
7827 you'd need an extra set of parentheses.) But, if we parse
7828 the type-id, and it happens to be a class-specifier, then we
7829 will commit to the parse at that point, because we cannot
7830 undo the action that is done when creating a new class. So,
7831 then we cannot back up and do a postfix-expression.
7833 Another tricky case is the following (c++/29234):
7835 struct S { void operator () (); };
7837 void foo ()
7839 ( S()() );
7842 As a type-id we parse the parenthesized S()() as a function
7843 returning a function, groktypename complains and we cannot
7844 back up in this case either.
7846 Therefore, we scan ahead to the closing `)', and check to see
7847 if the tokens after the `)' can start a cast-expression. Otherwise
7848 we are dealing with an unary-expression, a postfix-expression
7849 or something else.
7851 Yet another tricky case, in C++11, is the following (c++/54891):
7853 (void)[]{};
7855 The issue is that usually, besides the case of lambda-expressions,
7856 the parenthesized type-id cannot be followed by '[', and, eg, we
7857 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7858 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7859 we don't commit, we try a cast-expression, then an unary-expression.
7861 Save tokens so that we can put them back. */
7862 cp_lexer_save_tokens (parser->lexer);
7864 /* We may be looking at a cast-expression. */
7865 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7866 /*consume_paren=*/true))
7867 cast_expression
7868 = cp_parser_tokens_start_cast_expression (parser);
7870 /* Roll back the tokens we skipped. */
7871 cp_lexer_rollback_tokens (parser->lexer);
7872 /* If we aren't looking at a cast-expression, simulate an error so
7873 that the call to cp_parser_error_occurred below returns true. */
7874 if (!cast_expression)
7875 cp_parser_simulate_error (parser);
7876 else
7878 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7879 parser->in_type_id_in_expr_p = true;
7880 /* Look for the type-id. */
7881 type = cp_parser_type_id (parser);
7882 /* Look for the closing `)'. */
7883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7884 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7887 /* Restore the saved message. */
7888 parser->type_definition_forbidden_message = saved_message;
7890 /* At this point this can only be either a cast or a
7891 parenthesized ctor such as `(T ())' that looks like a cast to
7892 function returning T. */
7893 if (!cp_parser_error_occurred (parser))
7895 /* Only commit if the cast-expression doesn't start with
7896 '++', '--', or '[' in C++11. */
7897 if (cast_expression > 0)
7898 cp_parser_commit_to_topmost_tentative_parse (parser);
7900 expr = cp_parser_cast_expression (parser,
7901 /*address_p=*/false,
7902 /*cast_p=*/true,
7903 /*decltype_p=*/false,
7904 pidk);
7906 if (cp_parser_parse_definitely (parser))
7908 /* Warn about old-style casts, if so requested. */
7909 if (warn_old_style_cast
7910 && !in_system_header_at (input_location)
7911 && !VOID_TYPE_P (type)
7912 && current_lang_name != lang_name_c)
7913 warning (OPT_Wold_style_cast, "use of old-style cast");
7915 /* Only type conversions to integral or enumeration types
7916 can be used in constant-expressions. */
7917 if (!cast_valid_in_integral_constant_expression_p (type)
7918 && cp_parser_non_integral_constant_expression (parser,
7919 NIC_CAST))
7920 return error_mark_node;
7922 /* Perform the cast. */
7923 expr = build_c_cast (input_location, type, expr);
7924 return expr;
7927 else
7928 cp_parser_abort_tentative_parse (parser);
7931 /* If we get here, then it's not a cast, so it must be a
7932 unary-expression. */
7933 return cp_parser_unary_expression (parser, address_p, cast_p,
7934 decltype_p, pidk);
7937 /* Parse a binary expression of the general form:
7939 pm-expression:
7940 cast-expression
7941 pm-expression .* cast-expression
7942 pm-expression ->* cast-expression
7944 multiplicative-expression:
7945 pm-expression
7946 multiplicative-expression * pm-expression
7947 multiplicative-expression / pm-expression
7948 multiplicative-expression % pm-expression
7950 additive-expression:
7951 multiplicative-expression
7952 additive-expression + multiplicative-expression
7953 additive-expression - multiplicative-expression
7955 shift-expression:
7956 additive-expression
7957 shift-expression << additive-expression
7958 shift-expression >> additive-expression
7960 relational-expression:
7961 shift-expression
7962 relational-expression < shift-expression
7963 relational-expression > shift-expression
7964 relational-expression <= shift-expression
7965 relational-expression >= shift-expression
7967 GNU Extension:
7969 relational-expression:
7970 relational-expression <? shift-expression
7971 relational-expression >? shift-expression
7973 equality-expression:
7974 relational-expression
7975 equality-expression == relational-expression
7976 equality-expression != relational-expression
7978 and-expression:
7979 equality-expression
7980 and-expression & equality-expression
7982 exclusive-or-expression:
7983 and-expression
7984 exclusive-or-expression ^ and-expression
7986 inclusive-or-expression:
7987 exclusive-or-expression
7988 inclusive-or-expression | exclusive-or-expression
7990 logical-and-expression:
7991 inclusive-or-expression
7992 logical-and-expression && inclusive-or-expression
7994 logical-or-expression:
7995 logical-and-expression
7996 logical-or-expression || logical-and-expression
7998 All these are implemented with a single function like:
8000 binary-expression:
8001 simple-cast-expression
8002 binary-expression <token> binary-expression
8004 CAST_P is true if this expression is the target of a cast.
8006 The binops_by_token map is used to get the tree codes for each <token> type.
8007 binary-expressions are associated according to a precedence table. */
8009 #define TOKEN_PRECEDENCE(token) \
8010 (((token->type == CPP_GREATER \
8011 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8012 && !parser->greater_than_is_operator_p) \
8013 ? PREC_NOT_OPERATOR \
8014 : binops_by_token[token->type].prec)
8016 static tree
8017 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8018 bool no_toplevel_fold_p,
8019 bool decltype_p,
8020 enum cp_parser_prec prec,
8021 cp_id_kind * pidk)
8023 cp_parser_expression_stack stack;
8024 cp_parser_expression_stack_entry *sp = &stack[0];
8025 cp_parser_expression_stack_entry current;
8026 tree rhs;
8027 cp_token *token;
8028 enum tree_code rhs_type;
8029 enum cp_parser_prec new_prec, lookahead_prec;
8030 tree overload;
8032 /* Parse the first expression. */
8033 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8034 ? TRUTH_NOT_EXPR : ERROR_MARK);
8035 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8036 cast_p, decltype_p, pidk);
8037 current.prec = prec;
8039 if (cp_parser_error_occurred (parser))
8040 return error_mark_node;
8042 for (;;)
8044 /* Get an operator token. */
8045 token = cp_lexer_peek_token (parser->lexer);
8047 if (warn_cxx0x_compat
8048 && token->type == CPP_RSHIFT
8049 && !parser->greater_than_is_operator_p)
8051 if (warning_at (token->location, OPT_Wc__0x_compat,
8052 "%<>>%> operator is treated"
8053 " as two right angle brackets in C++11"))
8054 inform (token->location,
8055 "suggest parentheses around %<>>%> expression");
8058 new_prec = TOKEN_PRECEDENCE (token);
8060 /* Popping an entry off the stack means we completed a subexpression:
8061 - either we found a token which is not an operator (`>' where it is not
8062 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8063 will happen repeatedly;
8064 - or, we found an operator which has lower priority. This is the case
8065 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8066 parsing `3 * 4'. */
8067 if (new_prec <= current.prec)
8069 if (sp == stack)
8070 break;
8071 else
8072 goto pop;
8075 get_rhs:
8076 current.tree_type = binops_by_token[token->type].tree_type;
8077 current.loc = token->location;
8079 /* We used the operator token. */
8080 cp_lexer_consume_token (parser->lexer);
8082 /* For "false && x" or "true || x", x will never be executed;
8083 disable warnings while evaluating it. */
8084 if (current.tree_type == TRUTH_ANDIF_EXPR)
8085 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8086 else if (current.tree_type == TRUTH_ORIF_EXPR)
8087 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8089 /* Extract another operand. It may be the RHS of this expression
8090 or the LHS of a new, higher priority expression. */
8091 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8092 ? TRUTH_NOT_EXPR : ERROR_MARK);
8093 rhs = cp_parser_simple_cast_expression (parser);
8095 /* Get another operator token. Look up its precedence to avoid
8096 building a useless (immediately popped) stack entry for common
8097 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8098 token = cp_lexer_peek_token (parser->lexer);
8099 lookahead_prec = TOKEN_PRECEDENCE (token);
8100 if (lookahead_prec > new_prec)
8102 /* ... and prepare to parse the RHS of the new, higher priority
8103 expression. Since precedence levels on the stack are
8104 monotonically increasing, we do not have to care about
8105 stack overflows. */
8106 *sp = current;
8107 ++sp;
8108 current.lhs = rhs;
8109 current.lhs_type = rhs_type;
8110 current.prec = new_prec;
8111 new_prec = lookahead_prec;
8112 goto get_rhs;
8114 pop:
8115 lookahead_prec = new_prec;
8116 /* If the stack is not empty, we have parsed into LHS the right side
8117 (`4' in the example above) of an expression we had suspended.
8118 We can use the information on the stack to recover the LHS (`3')
8119 from the stack together with the tree code (`MULT_EXPR'), and
8120 the precedence of the higher level subexpression
8121 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8122 which will be used to actually build the additive expression. */
8123 rhs = current.lhs;
8124 rhs_type = current.lhs_type;
8125 --sp;
8126 current = *sp;
8129 /* Undo the disabling of warnings done above. */
8130 if (current.tree_type == TRUTH_ANDIF_EXPR)
8131 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8132 else if (current.tree_type == TRUTH_ORIF_EXPR)
8133 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8135 if (warn_logical_not_paren
8136 && current.lhs_type == TRUTH_NOT_EXPR)
8137 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8139 overload = NULL;
8140 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8141 ERROR_MARK for everything that is not a binary expression.
8142 This makes warn_about_parentheses miss some warnings that
8143 involve unary operators. For unary expressions we should
8144 pass the correct tree_code unless the unary expression was
8145 surrounded by parentheses.
8147 if (no_toplevel_fold_p
8148 && lookahead_prec <= current.prec
8149 && sp == stack)
8150 current.lhs = build2 (current.tree_type,
8151 TREE_CODE_CLASS (current.tree_type)
8152 == tcc_comparison
8153 ? boolean_type_node : TREE_TYPE (current.lhs),
8154 current.lhs, rhs);
8155 else
8156 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8157 current.lhs, current.lhs_type,
8158 rhs, rhs_type, &overload,
8159 complain_flags (decltype_p));
8160 current.lhs_type = current.tree_type;
8161 if (EXPR_P (current.lhs))
8162 SET_EXPR_LOCATION (current.lhs, current.loc);
8164 /* If the binary operator required the use of an overloaded operator,
8165 then this expression cannot be an integral constant-expression.
8166 An overloaded operator can be used even if both operands are
8167 otherwise permissible in an integral constant-expression if at
8168 least one of the operands is of enumeration type. */
8170 if (overload
8171 && cp_parser_non_integral_constant_expression (parser,
8172 NIC_OVERLOADED))
8173 return error_mark_node;
8176 return current.lhs;
8179 static tree
8180 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8181 bool no_toplevel_fold_p,
8182 enum cp_parser_prec prec,
8183 cp_id_kind * pidk)
8185 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8186 /*decltype*/false, prec, pidk);
8189 /* Parse the `? expression : assignment-expression' part of a
8190 conditional-expression. The LOGICAL_OR_EXPR is the
8191 logical-or-expression that started the conditional-expression.
8192 Returns a representation of the entire conditional-expression.
8194 This routine is used by cp_parser_assignment_expression.
8196 ? expression : assignment-expression
8198 GNU Extensions:
8200 ? : assignment-expression */
8202 static tree
8203 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8205 tree expr;
8206 tree assignment_expr;
8207 struct cp_token *token;
8208 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8210 /* Consume the `?' token. */
8211 cp_lexer_consume_token (parser->lexer);
8212 token = cp_lexer_peek_token (parser->lexer);
8213 if (cp_parser_allow_gnu_extensions_p (parser)
8214 && token->type == CPP_COLON)
8216 pedwarn (token->location, OPT_Wpedantic,
8217 "ISO C++ does not allow ?: with omitted middle operand");
8218 /* Implicit true clause. */
8219 expr = NULL_TREE;
8220 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8221 warn_for_omitted_condop (token->location, logical_or_expr);
8223 else
8225 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8226 parser->colon_corrects_to_scope_p = false;
8227 /* Parse the expression. */
8228 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8229 expr = cp_parser_expression (parser);
8230 c_inhibit_evaluation_warnings +=
8231 ((logical_or_expr == truthvalue_true_node)
8232 - (logical_or_expr == truthvalue_false_node));
8233 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8236 /* The next token should be a `:'. */
8237 cp_parser_require (parser, CPP_COLON, RT_COLON);
8238 /* Parse the assignment-expression. */
8239 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8240 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8242 /* Build the conditional-expression. */
8243 return build_x_conditional_expr (loc, logical_or_expr,
8244 expr,
8245 assignment_expr,
8246 tf_warning_or_error);
8249 /* Parse an assignment-expression.
8251 assignment-expression:
8252 conditional-expression
8253 logical-or-expression assignment-operator assignment_expression
8254 throw-expression
8256 CAST_P is true if this expression is the target of a cast.
8257 DECLTYPE_P is true if this expression is the operand of decltype.
8259 Returns a representation for the expression. */
8261 static tree
8262 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8263 bool decltype_p, cp_id_kind * pidk)
8265 tree expr;
8267 /* If the next token is the `throw' keyword, then we're looking at
8268 a throw-expression. */
8269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8270 expr = cp_parser_throw_expression (parser);
8271 /* Otherwise, it must be that we are looking at a
8272 logical-or-expression. */
8273 else
8275 /* Parse the binary expressions (logical-or-expression). */
8276 expr = cp_parser_binary_expression (parser, cast_p, false,
8277 decltype_p,
8278 PREC_NOT_OPERATOR, pidk);
8279 /* If the next token is a `?' then we're actually looking at a
8280 conditional-expression. */
8281 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8282 return cp_parser_question_colon_clause (parser, expr);
8283 else
8285 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8287 /* If it's an assignment-operator, we're using the second
8288 production. */
8289 enum tree_code assignment_operator
8290 = cp_parser_assignment_operator_opt (parser);
8291 if (assignment_operator != ERROR_MARK)
8293 bool non_constant_p;
8294 location_t saved_input_location;
8296 /* Parse the right-hand side of the assignment. */
8297 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8299 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8300 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8302 /* An assignment may not appear in a
8303 constant-expression. */
8304 if (cp_parser_non_integral_constant_expression (parser,
8305 NIC_ASSIGNMENT))
8306 return error_mark_node;
8307 /* Build the assignment expression. Its default
8308 location is the location of the '=' token. */
8309 saved_input_location = input_location;
8310 input_location = loc;
8311 expr = build_x_modify_expr (loc, expr,
8312 assignment_operator,
8313 rhs,
8314 complain_flags (decltype_p));
8315 input_location = saved_input_location;
8320 return expr;
8323 static tree
8324 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8325 cp_id_kind * pidk)
8327 return cp_parser_assignment_expression (parser, cast_p,
8328 /*decltype*/false, pidk);
8331 /* Parse an (optional) assignment-operator.
8333 assignment-operator: one of
8334 = *= /= %= += -= >>= <<= &= ^= |=
8336 GNU Extension:
8338 assignment-operator: one of
8339 <?= >?=
8341 If the next token is an assignment operator, the corresponding tree
8342 code is returned, and the token is consumed. For example, for
8343 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8344 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8345 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8346 operator, ERROR_MARK is returned. */
8348 static enum tree_code
8349 cp_parser_assignment_operator_opt (cp_parser* parser)
8351 enum tree_code op;
8352 cp_token *token;
8354 /* Peek at the next token. */
8355 token = cp_lexer_peek_token (parser->lexer);
8357 switch (token->type)
8359 case CPP_EQ:
8360 op = NOP_EXPR;
8361 break;
8363 case CPP_MULT_EQ:
8364 op = MULT_EXPR;
8365 break;
8367 case CPP_DIV_EQ:
8368 op = TRUNC_DIV_EXPR;
8369 break;
8371 case CPP_MOD_EQ:
8372 op = TRUNC_MOD_EXPR;
8373 break;
8375 case CPP_PLUS_EQ:
8376 op = PLUS_EXPR;
8377 break;
8379 case CPP_MINUS_EQ:
8380 op = MINUS_EXPR;
8381 break;
8383 case CPP_RSHIFT_EQ:
8384 op = RSHIFT_EXPR;
8385 break;
8387 case CPP_LSHIFT_EQ:
8388 op = LSHIFT_EXPR;
8389 break;
8391 case CPP_AND_EQ:
8392 op = BIT_AND_EXPR;
8393 break;
8395 case CPP_XOR_EQ:
8396 op = BIT_XOR_EXPR;
8397 break;
8399 case CPP_OR_EQ:
8400 op = BIT_IOR_EXPR;
8401 break;
8403 default:
8404 /* Nothing else is an assignment operator. */
8405 op = ERROR_MARK;
8408 /* If it was an assignment operator, consume it. */
8409 if (op != ERROR_MARK)
8410 cp_lexer_consume_token (parser->lexer);
8412 return op;
8415 /* Parse an expression.
8417 expression:
8418 assignment-expression
8419 expression , assignment-expression
8421 CAST_P is true if this expression is the target of a cast.
8422 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8423 except possibly parenthesized or on the RHS of a comma (N3276).
8425 Returns a representation of the expression. */
8427 static tree
8428 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8429 bool cast_p, bool decltype_p)
8431 tree expression = NULL_TREE;
8432 location_t loc = UNKNOWN_LOCATION;
8434 while (true)
8436 tree assignment_expression;
8438 /* Parse the next assignment-expression. */
8439 assignment_expression
8440 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8442 /* We don't create a temporary for a call that is the immediate operand
8443 of decltype or on the RHS of a comma. But when we see a comma, we
8444 need to create a temporary for a call on the LHS. */
8445 if (decltype_p && !processing_template_decl
8446 && TREE_CODE (assignment_expression) == CALL_EXPR
8447 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8448 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8449 assignment_expression
8450 = build_cplus_new (TREE_TYPE (assignment_expression),
8451 assignment_expression, tf_warning_or_error);
8453 /* If this is the first assignment-expression, we can just
8454 save it away. */
8455 if (!expression)
8456 expression = assignment_expression;
8457 else
8458 expression = build_x_compound_expr (loc, expression,
8459 assignment_expression,
8460 complain_flags (decltype_p));
8461 /* If the next token is not a comma, then we are done with the
8462 expression. */
8463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8464 break;
8465 /* Consume the `,'. */
8466 loc = cp_lexer_peek_token (parser->lexer)->location;
8467 cp_lexer_consume_token (parser->lexer);
8468 /* A comma operator cannot appear in a constant-expression. */
8469 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8470 expression = error_mark_node;
8473 return expression;
8476 /* Parse a constant-expression.
8478 constant-expression:
8479 conditional-expression
8481 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8482 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8483 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8484 is false, NON_CONSTANT_P should be NULL. */
8486 static tree
8487 cp_parser_constant_expression (cp_parser* parser,
8488 bool allow_non_constant_p,
8489 bool *non_constant_p)
8491 bool saved_integral_constant_expression_p;
8492 bool saved_allow_non_integral_constant_expression_p;
8493 bool saved_non_integral_constant_expression_p;
8494 tree expression;
8496 /* It might seem that we could simply parse the
8497 conditional-expression, and then check to see if it were
8498 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8499 one that the compiler can figure out is constant, possibly after
8500 doing some simplifications or optimizations. The standard has a
8501 precise definition of constant-expression, and we must honor
8502 that, even though it is somewhat more restrictive.
8504 For example:
8506 int i[(2, 3)];
8508 is not a legal declaration, because `(2, 3)' is not a
8509 constant-expression. The `,' operator is forbidden in a
8510 constant-expression. However, GCC's constant-folding machinery
8511 will fold this operation to an INTEGER_CST for `3'. */
8513 /* Save the old settings. */
8514 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8515 saved_allow_non_integral_constant_expression_p
8516 = parser->allow_non_integral_constant_expression_p;
8517 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8518 /* We are now parsing a constant-expression. */
8519 parser->integral_constant_expression_p = true;
8520 parser->allow_non_integral_constant_expression_p
8521 = (allow_non_constant_p || cxx_dialect >= cxx11);
8522 parser->non_integral_constant_expression_p = false;
8523 /* Although the grammar says "conditional-expression", we parse an
8524 "assignment-expression", which also permits "throw-expression"
8525 and the use of assignment operators. In the case that
8526 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8527 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8528 actually essential that we look for an assignment-expression.
8529 For example, cp_parser_initializer_clauses uses this function to
8530 determine whether a particular assignment-expression is in fact
8531 constant. */
8532 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8533 /* Restore the old settings. */
8534 parser->integral_constant_expression_p
8535 = saved_integral_constant_expression_p;
8536 parser->allow_non_integral_constant_expression_p
8537 = saved_allow_non_integral_constant_expression_p;
8538 if (cxx_dialect >= cxx11)
8540 /* Require an rvalue constant expression here; that's what our
8541 callers expect. Reference constant expressions are handled
8542 separately in e.g. cp_parser_template_argument. */
8543 bool is_const = potential_rvalue_constant_expression (expression);
8544 parser->non_integral_constant_expression_p = !is_const;
8545 if (!is_const && !allow_non_constant_p)
8546 require_potential_rvalue_constant_expression (expression);
8548 if (allow_non_constant_p)
8549 *non_constant_p = parser->non_integral_constant_expression_p;
8550 parser->non_integral_constant_expression_p
8551 = saved_non_integral_constant_expression_p;
8553 return expression;
8556 /* Parse __builtin_offsetof.
8558 offsetof-expression:
8559 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8561 offsetof-member-designator:
8562 id-expression
8563 | offsetof-member-designator "." id-expression
8564 | offsetof-member-designator "[" expression "]"
8565 | offsetof-member-designator "->" id-expression */
8567 static tree
8568 cp_parser_builtin_offsetof (cp_parser *parser)
8570 int save_ice_p, save_non_ice_p;
8571 tree type, expr;
8572 cp_id_kind dummy;
8573 cp_token *token;
8575 /* We're about to accept non-integral-constant things, but will
8576 definitely yield an integral constant expression. Save and
8577 restore these values around our local parsing. */
8578 save_ice_p = parser->integral_constant_expression_p;
8579 save_non_ice_p = parser->non_integral_constant_expression_p;
8581 /* Consume the "__builtin_offsetof" token. */
8582 cp_lexer_consume_token (parser->lexer);
8583 /* Consume the opening `('. */
8584 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8585 /* Parse the type-id. */
8586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8587 type = cp_parser_type_id (parser);
8588 /* Look for the `,'. */
8589 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8590 token = cp_lexer_peek_token (parser->lexer);
8592 /* Build the (type *)null that begins the traditional offsetof macro. */
8593 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8594 tf_warning_or_error);
8596 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8597 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8598 true, &dummy, token->location);
8599 while (true)
8601 token = cp_lexer_peek_token (parser->lexer);
8602 switch (token->type)
8604 case CPP_OPEN_SQUARE:
8605 /* offsetof-member-designator "[" expression "]" */
8606 expr = cp_parser_postfix_open_square_expression (parser, expr,
8607 true, false);
8608 break;
8610 case CPP_DEREF:
8611 /* offsetof-member-designator "->" identifier */
8612 expr = grok_array_decl (token->location, expr,
8613 integer_zero_node, false);
8614 /* FALLTHRU */
8616 case CPP_DOT:
8617 /* offsetof-member-designator "." identifier */
8618 cp_lexer_consume_token (parser->lexer);
8619 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8620 expr, true, &dummy,
8621 token->location);
8622 break;
8624 case CPP_CLOSE_PAREN:
8625 /* Consume the ")" token. */
8626 cp_lexer_consume_token (parser->lexer);
8627 goto success;
8629 default:
8630 /* Error. We know the following require will fail, but
8631 that gives the proper error message. */
8632 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8633 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8634 expr = error_mark_node;
8635 goto failure;
8639 success:
8640 /* If we're processing a template, we can't finish the semantics yet.
8641 Otherwise we can fold the entire expression now. */
8642 if (processing_template_decl)
8644 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8645 SET_EXPR_LOCATION (expr, loc);
8647 else
8648 expr = finish_offsetof (expr, loc);
8650 failure:
8651 parser->integral_constant_expression_p = save_ice_p;
8652 parser->non_integral_constant_expression_p = save_non_ice_p;
8654 return expr;
8657 /* Parse a trait expression.
8659 Returns a representation of the expression, the underlying type
8660 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8662 static tree
8663 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8665 cp_trait_kind kind;
8666 tree type1, type2 = NULL_TREE;
8667 bool binary = false;
8668 cp_decl_specifier_seq decl_specs;
8670 switch (keyword)
8672 case RID_HAS_NOTHROW_ASSIGN:
8673 kind = CPTK_HAS_NOTHROW_ASSIGN;
8674 break;
8675 case RID_HAS_NOTHROW_CONSTRUCTOR:
8676 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8677 break;
8678 case RID_HAS_NOTHROW_COPY:
8679 kind = CPTK_HAS_NOTHROW_COPY;
8680 break;
8681 case RID_HAS_TRIVIAL_ASSIGN:
8682 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8683 break;
8684 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8685 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8686 break;
8687 case RID_HAS_TRIVIAL_COPY:
8688 kind = CPTK_HAS_TRIVIAL_COPY;
8689 break;
8690 case RID_HAS_TRIVIAL_DESTRUCTOR:
8691 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8692 break;
8693 case RID_HAS_VIRTUAL_DESTRUCTOR:
8694 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8695 break;
8696 case RID_IS_ABSTRACT:
8697 kind = CPTK_IS_ABSTRACT;
8698 break;
8699 case RID_IS_BASE_OF:
8700 kind = CPTK_IS_BASE_OF;
8701 binary = true;
8702 break;
8703 case RID_IS_CLASS:
8704 kind = CPTK_IS_CLASS;
8705 break;
8706 case RID_IS_CONVERTIBLE_TO:
8707 kind = CPTK_IS_CONVERTIBLE_TO;
8708 binary = true;
8709 break;
8710 case RID_IS_EMPTY:
8711 kind = CPTK_IS_EMPTY;
8712 break;
8713 case RID_IS_ENUM:
8714 kind = CPTK_IS_ENUM;
8715 break;
8716 case RID_IS_FINAL:
8717 kind = CPTK_IS_FINAL;
8718 break;
8719 case RID_IS_LITERAL_TYPE:
8720 kind = CPTK_IS_LITERAL_TYPE;
8721 break;
8722 case RID_IS_POD:
8723 kind = CPTK_IS_POD;
8724 break;
8725 case RID_IS_POLYMORPHIC:
8726 kind = CPTK_IS_POLYMORPHIC;
8727 break;
8728 case RID_IS_STD_LAYOUT:
8729 kind = CPTK_IS_STD_LAYOUT;
8730 break;
8731 case RID_IS_TRIVIAL:
8732 kind = CPTK_IS_TRIVIAL;
8733 break;
8734 case RID_IS_UNION:
8735 kind = CPTK_IS_UNION;
8736 break;
8737 case RID_UNDERLYING_TYPE:
8738 kind = CPTK_UNDERLYING_TYPE;
8739 break;
8740 case RID_BASES:
8741 kind = CPTK_BASES;
8742 break;
8743 case RID_DIRECT_BASES:
8744 kind = CPTK_DIRECT_BASES;
8745 break;
8746 default:
8747 gcc_unreachable ();
8750 /* Consume the token. */
8751 cp_lexer_consume_token (parser->lexer);
8753 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8755 type1 = cp_parser_type_id (parser);
8757 if (type1 == error_mark_node)
8758 return error_mark_node;
8760 /* Build a trivial decl-specifier-seq. */
8761 clear_decl_specs (&decl_specs);
8762 decl_specs.type = type1;
8764 /* Call grokdeclarator to figure out what type this is. */
8765 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8766 /*initialized=*/0, /*attrlist=*/NULL);
8768 if (binary)
8770 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8772 type2 = cp_parser_type_id (parser);
8774 if (type2 == error_mark_node)
8775 return error_mark_node;
8777 /* Build a trivial decl-specifier-seq. */
8778 clear_decl_specs (&decl_specs);
8779 decl_specs.type = type2;
8781 /* Call grokdeclarator to figure out what type this is. */
8782 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8783 /*initialized=*/0, /*attrlist=*/NULL);
8786 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8788 /* Complete the trait expression, which may mean either processing
8789 the trait expr now or saving it for template instantiation. */
8790 switch(kind)
8792 case CPTK_UNDERLYING_TYPE:
8793 return finish_underlying_type (type1);
8794 case CPTK_BASES:
8795 return finish_bases (type1, false);
8796 case CPTK_DIRECT_BASES:
8797 return finish_bases (type1, true);
8798 default:
8799 return finish_trait_expr (kind, type1, type2);
8803 /* Lambdas that appear in variable initializer or default argument scope
8804 get that in their mangling, so we need to record it. We might as well
8805 use the count for function and namespace scopes as well. */
8806 static GTY(()) tree lambda_scope;
8807 static GTY(()) int lambda_count;
8808 typedef struct GTY(()) tree_int
8810 tree t;
8811 int i;
8812 } tree_int;
8813 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8815 static void
8816 start_lambda_scope (tree decl)
8818 tree_int ti;
8819 gcc_assert (decl);
8820 /* Once we're inside a function, we ignore other scopes and just push
8821 the function again so that popping works properly. */
8822 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8823 decl = current_function_decl;
8824 ti.t = lambda_scope;
8825 ti.i = lambda_count;
8826 vec_safe_push (lambda_scope_stack, ti);
8827 if (lambda_scope != decl)
8829 /* Don't reset the count if we're still in the same function. */
8830 lambda_scope = decl;
8831 lambda_count = 0;
8835 static void
8836 record_lambda_scope (tree lambda)
8838 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8839 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8842 static void
8843 finish_lambda_scope (void)
8845 tree_int *p = &lambda_scope_stack->last ();
8846 if (lambda_scope != p->t)
8848 lambda_scope = p->t;
8849 lambda_count = p->i;
8851 lambda_scope_stack->pop ();
8854 /* Parse a lambda expression.
8856 lambda-expression:
8857 lambda-introducer lambda-declarator [opt] compound-statement
8859 Returns a representation of the expression. */
8861 static tree
8862 cp_parser_lambda_expression (cp_parser* parser)
8864 tree lambda_expr = build_lambda_expr ();
8865 tree type;
8866 bool ok = true;
8867 cp_token *token = cp_lexer_peek_token (parser->lexer);
8869 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8871 if (cp_unevaluated_operand)
8873 if (!token->error_reported)
8875 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8876 "lambda-expression in unevaluated context");
8877 token->error_reported = true;
8879 ok = false;
8882 /* We may be in the middle of deferred access check. Disable
8883 it now. */
8884 push_deferring_access_checks (dk_no_deferred);
8886 cp_parser_lambda_introducer (parser, lambda_expr);
8888 type = begin_lambda_type (lambda_expr);
8889 if (type == error_mark_node)
8890 return error_mark_node;
8892 record_lambda_scope (lambda_expr);
8894 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8895 determine_visibility (TYPE_NAME (type));
8897 /* Now that we've started the type, add the capture fields for any
8898 explicit captures. */
8899 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8902 /* Inside the class, surrounding template-parameter-lists do not apply. */
8903 unsigned int saved_num_template_parameter_lists
8904 = parser->num_template_parameter_lists;
8905 unsigned char in_statement = parser->in_statement;
8906 bool in_switch_statement_p = parser->in_switch_statement_p;
8907 bool fully_implicit_function_template_p
8908 = parser->fully_implicit_function_template_p;
8909 tree implicit_template_parms = parser->implicit_template_parms;
8910 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8911 bool auto_is_implicit_function_template_parm_p
8912 = parser->auto_is_implicit_function_template_parm_p;
8914 parser->num_template_parameter_lists = 0;
8915 parser->in_statement = 0;
8916 parser->in_switch_statement_p = false;
8917 parser->fully_implicit_function_template_p = false;
8918 parser->implicit_template_parms = 0;
8919 parser->implicit_template_scope = 0;
8920 parser->auto_is_implicit_function_template_parm_p = false;
8922 /* By virtue of defining a local class, a lambda expression has access to
8923 the private variables of enclosing classes. */
8925 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8927 if (ok)
8928 cp_parser_lambda_body (parser, lambda_expr);
8929 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8931 if (cp_parser_skip_to_closing_brace (parser))
8932 cp_lexer_consume_token (parser->lexer);
8935 /* The capture list was built up in reverse order; fix that now. */
8936 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8937 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8939 if (ok)
8940 maybe_add_lambda_conv_op (type);
8942 type = finish_struct (type, /*attributes=*/NULL_TREE);
8944 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8945 parser->in_statement = in_statement;
8946 parser->in_switch_statement_p = in_switch_statement_p;
8947 parser->fully_implicit_function_template_p
8948 = fully_implicit_function_template_p;
8949 parser->implicit_template_parms = implicit_template_parms;
8950 parser->implicit_template_scope = implicit_template_scope;
8951 parser->auto_is_implicit_function_template_parm_p
8952 = auto_is_implicit_function_template_parm_p;
8955 pop_deferring_access_checks ();
8957 /* This field is only used during parsing of the lambda. */
8958 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8960 /* This lambda shouldn't have any proxies left at this point. */
8961 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8962 /* And now that we're done, push proxies for an enclosing lambda. */
8963 insert_pending_capture_proxies ();
8965 if (ok)
8966 return build_lambda_object (lambda_expr);
8967 else
8968 return error_mark_node;
8971 /* Parse the beginning of a lambda expression.
8973 lambda-introducer:
8974 [ lambda-capture [opt] ]
8976 LAMBDA_EXPR is the current representation of the lambda expression. */
8978 static void
8979 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8981 /* Need commas after the first capture. */
8982 bool first = true;
8984 /* Eat the leading `['. */
8985 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8987 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8988 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8989 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8990 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8991 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8992 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8994 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8996 cp_lexer_consume_token (parser->lexer);
8997 first = false;
9000 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9002 cp_token* capture_token;
9003 tree capture_id;
9004 tree capture_init_expr;
9005 cp_id_kind idk = CP_ID_KIND_NONE;
9006 bool explicit_init_p = false;
9008 enum capture_kind_type
9010 BY_COPY,
9011 BY_REFERENCE
9013 enum capture_kind_type capture_kind = BY_COPY;
9015 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9017 error ("expected end of capture-list");
9018 return;
9021 if (first)
9022 first = false;
9023 else
9024 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9026 /* Possibly capture `this'. */
9027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9029 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9030 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9031 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9032 "with by-copy capture default");
9033 cp_lexer_consume_token (parser->lexer);
9034 add_capture (lambda_expr,
9035 /*id=*/this_identifier,
9036 /*initializer=*/finish_this_expr(),
9037 /*by_reference_p=*/false,
9038 explicit_init_p);
9039 continue;
9042 /* Remember whether we want to capture as a reference or not. */
9043 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9045 capture_kind = BY_REFERENCE;
9046 cp_lexer_consume_token (parser->lexer);
9049 /* Get the identifier. */
9050 capture_token = cp_lexer_peek_token (parser->lexer);
9051 capture_id = cp_parser_identifier (parser);
9053 if (capture_id == error_mark_node)
9054 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9055 delimiters, but I modified this to stop on unnested ']' as well. It
9056 was already changed to stop on unnested '}', so the
9057 "closing_parenthesis" name is no more misleading with my change. */
9059 cp_parser_skip_to_closing_parenthesis (parser,
9060 /*recovering=*/true,
9061 /*or_comma=*/true,
9062 /*consume_paren=*/true);
9063 break;
9066 /* Find the initializer for this capture. */
9067 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9068 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9069 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9071 bool direct, non_constant;
9072 /* An explicit initializer exists. */
9073 if (cxx_dialect < cxx14)
9074 pedwarn (input_location, 0,
9075 "lambda capture initializers "
9076 "only available with -std=c++14 or -std=gnu++14");
9077 capture_init_expr = cp_parser_initializer (parser, &direct,
9078 &non_constant);
9079 explicit_init_p = true;
9080 if (capture_init_expr == NULL_TREE)
9082 error ("empty initializer for lambda init-capture");
9083 capture_init_expr = error_mark_node;
9086 else
9088 const char* error_msg;
9090 /* Turn the identifier into an id-expression. */
9091 capture_init_expr
9092 = cp_parser_lookup_name_simple (parser, capture_id,
9093 capture_token->location);
9095 if (capture_init_expr == error_mark_node)
9097 unqualified_name_lookup_error (capture_id);
9098 continue;
9100 else if (DECL_P (capture_init_expr)
9101 && (!VAR_P (capture_init_expr)
9102 && TREE_CODE (capture_init_expr) != PARM_DECL))
9104 error_at (capture_token->location,
9105 "capture of non-variable %qD ",
9106 capture_init_expr);
9107 inform (0, "%q+#D declared here", capture_init_expr);
9108 continue;
9110 if (VAR_P (capture_init_expr)
9111 && decl_storage_duration (capture_init_expr) != dk_auto)
9113 if (pedwarn (capture_token->location, 0, "capture of variable "
9114 "%qD with non-automatic storage duration",
9115 capture_init_expr))
9116 inform (0, "%q+#D declared here", capture_init_expr);
9117 continue;
9120 capture_init_expr
9121 = finish_id_expression
9122 (capture_id,
9123 capture_init_expr,
9124 parser->scope,
9125 &idk,
9126 /*integral_constant_expression_p=*/false,
9127 /*allow_non_integral_constant_expression_p=*/false,
9128 /*non_integral_constant_expression_p=*/NULL,
9129 /*template_p=*/false,
9130 /*done=*/true,
9131 /*address_p=*/false,
9132 /*template_arg_p=*/false,
9133 &error_msg,
9134 capture_token->location);
9136 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9138 cp_lexer_consume_token (parser->lexer);
9139 capture_init_expr = make_pack_expansion (capture_init_expr);
9141 else
9142 check_for_bare_parameter_packs (capture_init_expr);
9145 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9146 && !explicit_init_p)
9148 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9149 && capture_kind == BY_COPY)
9150 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9151 "of %qD redundant with by-copy capture default",
9152 capture_id);
9153 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9154 && capture_kind == BY_REFERENCE)
9155 pedwarn (capture_token->location, 0, "explicit by-reference "
9156 "capture of %qD redundant with by-reference capture "
9157 "default", capture_id);
9160 add_capture (lambda_expr,
9161 capture_id,
9162 capture_init_expr,
9163 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9164 explicit_init_p);
9167 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9170 /* Parse the (optional) middle of a lambda expression.
9172 lambda-declarator:
9173 < template-parameter-list [opt] >
9174 ( parameter-declaration-clause [opt] )
9175 attribute-specifier [opt]
9176 mutable [opt]
9177 exception-specification [opt]
9178 lambda-return-type-clause [opt]
9180 LAMBDA_EXPR is the current representation of the lambda expression. */
9182 static bool
9183 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9185 /* 5.1.1.4 of the standard says:
9186 If a lambda-expression does not include a lambda-declarator, it is as if
9187 the lambda-declarator were ().
9188 This means an empty parameter list, no attributes, and no exception
9189 specification. */
9190 tree param_list = void_list_node;
9191 tree attributes = NULL_TREE;
9192 tree exception_spec = NULL_TREE;
9193 tree template_param_list = NULL_TREE;
9195 /* The template-parameter-list is optional, but must begin with
9196 an opening angle if present. */
9197 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9199 if (cxx_dialect < cxx14)
9200 pedwarn (parser->lexer->next_token->location, 0,
9201 "lambda templates are only available with "
9202 "-std=c++14 or -std=gnu++14");
9204 cp_lexer_consume_token (parser->lexer);
9206 template_param_list = cp_parser_template_parameter_list (parser);
9208 cp_parser_skip_to_end_of_template_parameter_list (parser);
9210 /* We just processed one more parameter list. */
9211 ++parser->num_template_parameter_lists;
9214 /* The parameter-declaration-clause is optional (unless
9215 template-parameter-list was given), but must begin with an
9216 opening parenthesis if present. */
9217 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9219 cp_lexer_consume_token (parser->lexer);
9221 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9223 /* Parse parameters. */
9224 param_list = cp_parser_parameter_declaration_clause (parser);
9226 /* Default arguments shall not be specified in the
9227 parameter-declaration-clause of a lambda-declarator. */
9228 for (tree t = param_list; t; t = TREE_CHAIN (t))
9229 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9230 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9231 "default argument specified for lambda parameter");
9233 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9235 attributes = cp_parser_attributes_opt (parser);
9237 /* Parse optional `mutable' keyword. */
9238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9240 cp_lexer_consume_token (parser->lexer);
9241 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9244 /* Parse optional exception specification. */
9245 exception_spec = cp_parser_exception_specification_opt (parser);
9247 /* Parse optional trailing return type. */
9248 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9250 cp_lexer_consume_token (parser->lexer);
9251 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9252 = cp_parser_trailing_type_id (parser);
9255 /* The function parameters must be in scope all the way until after the
9256 trailing-return-type in case of decltype. */
9257 pop_bindings_and_leave_scope ();
9259 else if (template_param_list != NULL_TREE) // generate diagnostic
9260 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9262 /* Create the function call operator.
9264 Messing with declarators like this is no uglier than building up the
9265 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9266 other code. */
9268 cp_decl_specifier_seq return_type_specs;
9269 cp_declarator* declarator;
9270 tree fco;
9271 int quals;
9272 void *p;
9274 clear_decl_specs (&return_type_specs);
9275 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9276 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9277 else
9278 /* Maybe we will deduce the return type later. */
9279 return_type_specs.type = make_auto ();
9281 p = obstack_alloc (&declarator_obstack, 0);
9283 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9284 sfk_none);
9286 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9287 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9288 declarator = make_call_declarator (declarator, param_list, quals,
9289 VIRT_SPEC_UNSPECIFIED,
9290 REF_QUAL_NONE,
9291 exception_spec,
9292 /*late_return_type=*/NULL_TREE);
9293 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9295 fco = grokmethod (&return_type_specs,
9296 declarator,
9297 attributes);
9298 if (fco != error_mark_node)
9300 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9301 DECL_ARTIFICIAL (fco) = 1;
9302 /* Give the object parameter a different name. */
9303 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9304 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9305 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9307 if (template_param_list)
9309 fco = finish_member_template_decl (fco);
9310 finish_template_decl (template_param_list);
9311 --parser->num_template_parameter_lists;
9313 else if (parser->fully_implicit_function_template_p)
9314 fco = finish_fully_implicit_template (parser, fco);
9316 finish_member_declaration (fco);
9318 obstack_free (&declarator_obstack, p);
9320 return (fco != error_mark_node);
9324 /* Parse the body of a lambda expression, which is simply
9326 compound-statement
9328 but which requires special handling.
9329 LAMBDA_EXPR is the current representation of the lambda expression. */
9331 static void
9332 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9334 bool nested = (current_function_decl != NULL_TREE);
9335 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9336 if (nested)
9337 push_function_context ();
9338 else
9339 /* Still increment function_depth so that we don't GC in the
9340 middle of an expression. */
9341 ++function_depth;
9342 /* Clear this in case we're in the middle of a default argument. */
9343 parser->local_variables_forbidden_p = false;
9345 /* Finish the function call operator
9346 - class_specifier
9347 + late_parsing_for_member
9348 + function_definition_after_declarator
9349 + ctor_initializer_opt_and_function_body */
9351 tree fco = lambda_function (lambda_expr);
9352 tree body;
9353 bool done = false;
9354 tree compound_stmt;
9355 tree cap;
9357 /* Let the front end know that we are going to be defining this
9358 function. */
9359 start_preparsed_function (fco,
9360 NULL_TREE,
9361 SF_PRE_PARSED | SF_INCLASS_INLINE);
9363 start_lambda_scope (fco);
9364 body = begin_function_body ();
9366 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9367 goto out;
9369 /* Push the proxies for any explicit captures. */
9370 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9371 cap = TREE_CHAIN (cap))
9372 build_capture_proxy (TREE_PURPOSE (cap));
9374 compound_stmt = begin_compound_stmt (0);
9376 /* 5.1.1.4 of the standard says:
9377 If a lambda-expression does not include a trailing-return-type, it
9378 is as if the trailing-return-type denotes the following type:
9379 * if the compound-statement is of the form
9380 { return attribute-specifier [opt] expression ; }
9381 the type of the returned expression after lvalue-to-rvalue
9382 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9383 (_conv.array_ 4.2), and function-to-pointer conversion
9384 (_conv.func_ 4.3);
9385 * otherwise, void. */
9387 /* In a lambda that has neither a lambda-return-type-clause
9388 nor a deducible form, errors should be reported for return statements
9389 in the body. Since we used void as the placeholder return type, parsing
9390 the body as usual will give such desired behavior. */
9391 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9392 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9393 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9395 tree expr = NULL_TREE;
9396 cp_id_kind idk = CP_ID_KIND_NONE;
9398 /* Parse tentatively in case there's more after the initial return
9399 statement. */
9400 cp_parser_parse_tentatively (parser);
9402 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9404 expr = cp_parser_expression (parser, &idk);
9406 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9407 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9409 if (cp_parser_parse_definitely (parser))
9411 if (!processing_template_decl)
9412 apply_deduced_return_type (fco, lambda_return_type (expr));
9414 /* Will get error here if type not deduced yet. */
9415 finish_return_stmt (expr);
9417 done = true;
9421 if (!done)
9423 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9424 cp_parser_label_declaration (parser);
9425 cp_parser_statement_seq_opt (parser, NULL_TREE);
9426 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9429 finish_compound_stmt (compound_stmt);
9431 out:
9432 finish_function_body (body);
9433 finish_lambda_scope ();
9435 /* Finish the function and generate code for it if necessary. */
9436 tree fn = finish_function (/*inline*/2);
9438 /* Only expand if the call op is not a template. */
9439 if (!DECL_TEMPLATE_INFO (fco))
9440 expand_or_defer_fn (fn);
9443 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9444 if (nested)
9445 pop_function_context();
9446 else
9447 --function_depth;
9450 /* Statements [gram.stmt.stmt] */
9452 /* Parse a statement.
9454 statement:
9455 labeled-statement
9456 expression-statement
9457 compound-statement
9458 selection-statement
9459 iteration-statement
9460 jump-statement
9461 declaration-statement
9462 try-block
9464 C++11:
9466 statement:
9467 labeled-statement
9468 attribute-specifier-seq (opt) expression-statement
9469 attribute-specifier-seq (opt) compound-statement
9470 attribute-specifier-seq (opt) selection-statement
9471 attribute-specifier-seq (opt) iteration-statement
9472 attribute-specifier-seq (opt) jump-statement
9473 declaration-statement
9474 attribute-specifier-seq (opt) try-block
9476 TM Extension:
9478 statement:
9479 atomic-statement
9481 IN_COMPOUND is true when the statement is nested inside a
9482 cp_parser_compound_statement; this matters for certain pragmas.
9484 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9485 is a (possibly labeled) if statement which is not enclosed in braces
9486 and has an else clause. This is used to implement -Wparentheses. */
9488 static void
9489 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9490 bool in_compound, bool *if_p)
9492 tree statement, std_attrs = NULL_TREE;
9493 cp_token *token;
9494 location_t statement_location, attrs_location;
9496 restart:
9497 if (if_p != NULL)
9498 *if_p = false;
9499 /* There is no statement yet. */
9500 statement = NULL_TREE;
9502 cp_lexer_save_tokens (parser->lexer);
9503 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9504 if (c_dialect_objc ())
9505 /* In obj-c++, seeing '[[' might be the either the beginning of
9506 c++11 attributes, or a nested objc-message-expression. So
9507 let's parse the c++11 attributes tentatively. */
9508 cp_parser_parse_tentatively (parser);
9509 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9510 if (c_dialect_objc ())
9512 if (!cp_parser_parse_definitely (parser))
9513 std_attrs = NULL_TREE;
9516 /* Peek at the next token. */
9517 token = cp_lexer_peek_token (parser->lexer);
9518 /* Remember the location of the first token in the statement. */
9519 statement_location = token->location;
9520 /* If this is a keyword, then that will often determine what kind of
9521 statement we have. */
9522 if (token->type == CPP_KEYWORD)
9524 enum rid keyword = token->keyword;
9526 switch (keyword)
9528 case RID_CASE:
9529 case RID_DEFAULT:
9530 /* Looks like a labeled-statement with a case label.
9531 Parse the label, and then use tail recursion to parse
9532 the statement. */
9533 cp_parser_label_for_labeled_statement (parser, std_attrs);
9534 goto restart;
9536 case RID_IF:
9537 case RID_SWITCH:
9538 statement = cp_parser_selection_statement (parser, if_p);
9539 break;
9541 case RID_WHILE:
9542 case RID_DO:
9543 case RID_FOR:
9544 statement = cp_parser_iteration_statement (parser, false);
9545 break;
9547 case RID_CILK_FOR:
9548 if (!flag_cilkplus)
9550 error_at (cp_lexer_peek_token (parser->lexer)->location,
9551 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9552 cp_lexer_consume_token (parser->lexer);
9553 statement = error_mark_node;
9555 else
9556 statement = cp_parser_cilk_for (parser, integer_zero_node);
9557 break;
9559 case RID_BREAK:
9560 case RID_CONTINUE:
9561 case RID_RETURN:
9562 case RID_GOTO:
9563 statement = cp_parser_jump_statement (parser);
9564 break;
9566 case RID_CILK_SYNC:
9567 cp_lexer_consume_token (parser->lexer);
9568 if (flag_cilkplus)
9570 tree sync_expr = build_cilk_sync ();
9571 SET_EXPR_LOCATION (sync_expr,
9572 token->location);
9573 statement = finish_expr_stmt (sync_expr);
9575 else
9577 error_at (token->location, "-fcilkplus must be enabled to use"
9578 " %<_Cilk_sync%>");
9579 statement = error_mark_node;
9581 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9582 break;
9584 /* Objective-C++ exception-handling constructs. */
9585 case RID_AT_TRY:
9586 case RID_AT_CATCH:
9587 case RID_AT_FINALLY:
9588 case RID_AT_SYNCHRONIZED:
9589 case RID_AT_THROW:
9590 statement = cp_parser_objc_statement (parser);
9591 break;
9593 case RID_TRY:
9594 statement = cp_parser_try_block (parser);
9595 break;
9597 case RID_NAMESPACE:
9598 /* This must be a namespace alias definition. */
9599 cp_parser_declaration_statement (parser);
9600 return;
9602 case RID_TRANSACTION_ATOMIC:
9603 case RID_TRANSACTION_RELAXED:
9604 statement = cp_parser_transaction (parser, keyword);
9605 break;
9606 case RID_TRANSACTION_CANCEL:
9607 statement = cp_parser_transaction_cancel (parser);
9608 break;
9610 default:
9611 /* It might be a keyword like `int' that can start a
9612 declaration-statement. */
9613 break;
9616 else if (token->type == CPP_NAME)
9618 /* If the next token is a `:', then we are looking at a
9619 labeled-statement. */
9620 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9621 if (token->type == CPP_COLON)
9623 /* Looks like a labeled-statement with an ordinary label.
9624 Parse the label, and then use tail recursion to parse
9625 the statement. */
9627 cp_parser_label_for_labeled_statement (parser, std_attrs);
9628 goto restart;
9631 /* Anything that starts with a `{' must be a compound-statement. */
9632 else if (token->type == CPP_OPEN_BRACE)
9633 statement = cp_parser_compound_statement (parser, NULL, false, false);
9634 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9635 a statement all its own. */
9636 else if (token->type == CPP_PRAGMA)
9638 /* Only certain OpenMP pragmas are attached to statements, and thus
9639 are considered statements themselves. All others are not. In
9640 the context of a compound, accept the pragma as a "statement" and
9641 return so that we can check for a close brace. Otherwise we
9642 require a real statement and must go back and read one. */
9643 if (in_compound)
9644 cp_parser_pragma (parser, pragma_compound);
9645 else if (!cp_parser_pragma (parser, pragma_stmt))
9646 goto restart;
9647 return;
9649 else if (token->type == CPP_EOF)
9651 cp_parser_error (parser, "expected statement");
9652 return;
9655 /* Everything else must be a declaration-statement or an
9656 expression-statement. Try for the declaration-statement
9657 first, unless we are looking at a `;', in which case we know that
9658 we have an expression-statement. */
9659 if (!statement)
9661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9663 if (std_attrs != NULL_TREE)
9665 /* Attributes should be parsed as part of the the
9666 declaration, so let's un-parse them. */
9667 cp_lexer_rollback_tokens (parser->lexer);
9668 std_attrs = NULL_TREE;
9671 cp_parser_parse_tentatively (parser);
9672 /* Try to parse the declaration-statement. */
9673 cp_parser_declaration_statement (parser);
9674 /* If that worked, we're done. */
9675 if (cp_parser_parse_definitely (parser))
9676 return;
9678 /* Look for an expression-statement instead. */
9679 statement = cp_parser_expression_statement (parser, in_statement_expr);
9682 /* Set the line number for the statement. */
9683 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9684 SET_EXPR_LOCATION (statement, statement_location);
9686 /* Note that for now, we don't do anything with c++11 statements
9687 parsed at this level. */
9688 if (std_attrs != NULL_TREE)
9689 warning_at (attrs_location,
9690 OPT_Wattributes,
9691 "attributes at the beginning of statement are ignored");
9694 /* Parse the label for a labeled-statement, i.e.
9696 identifier :
9697 case constant-expression :
9698 default :
9700 GNU Extension:
9701 case constant-expression ... constant-expression : statement
9703 When a label is parsed without errors, the label is added to the
9704 parse tree by the finish_* functions, so this function doesn't
9705 have to return the label. */
9707 static void
9708 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9710 cp_token *token;
9711 tree label = NULL_TREE;
9712 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9714 /* The next token should be an identifier. */
9715 token = cp_lexer_peek_token (parser->lexer);
9716 if (token->type != CPP_NAME
9717 && token->type != CPP_KEYWORD)
9719 cp_parser_error (parser, "expected labeled-statement");
9720 return;
9723 parser->colon_corrects_to_scope_p = false;
9724 switch (token->keyword)
9726 case RID_CASE:
9728 tree expr, expr_hi;
9729 cp_token *ellipsis;
9731 /* Consume the `case' token. */
9732 cp_lexer_consume_token (parser->lexer);
9733 /* Parse the constant-expression. */
9734 expr = cp_parser_constant_expression (parser,
9735 /*allow_non_constant_p=*/false,
9736 NULL);
9738 ellipsis = cp_lexer_peek_token (parser->lexer);
9739 if (ellipsis->type == CPP_ELLIPSIS)
9741 /* Consume the `...' token. */
9742 cp_lexer_consume_token (parser->lexer);
9743 expr_hi =
9744 cp_parser_constant_expression (parser,
9745 /*allow_non_constant_p=*/false,
9746 NULL);
9747 /* We don't need to emit warnings here, as the common code
9748 will do this for us. */
9750 else
9751 expr_hi = NULL_TREE;
9753 if (parser->in_switch_statement_p)
9754 finish_case_label (token->location, expr, expr_hi);
9755 else
9756 error_at (token->location,
9757 "case label %qE not within a switch statement",
9758 expr);
9760 break;
9762 case RID_DEFAULT:
9763 /* Consume the `default' token. */
9764 cp_lexer_consume_token (parser->lexer);
9766 if (parser->in_switch_statement_p)
9767 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9768 else
9769 error_at (token->location, "case label not within a switch statement");
9770 break;
9772 default:
9773 /* Anything else must be an ordinary label. */
9774 label = finish_label_stmt (cp_parser_identifier (parser));
9775 break;
9778 /* Require the `:' token. */
9779 cp_parser_require (parser, CPP_COLON, RT_COLON);
9781 /* An ordinary label may optionally be followed by attributes.
9782 However, this is only permitted if the attributes are then
9783 followed by a semicolon. This is because, for backward
9784 compatibility, when parsing
9785 lab: __attribute__ ((unused)) int i;
9786 we want the attribute to attach to "i", not "lab". */
9787 if (label != NULL_TREE
9788 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9790 tree attrs;
9791 cp_parser_parse_tentatively (parser);
9792 attrs = cp_parser_gnu_attributes_opt (parser);
9793 if (attrs == NULL_TREE
9794 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9795 cp_parser_abort_tentative_parse (parser);
9796 else if (!cp_parser_parse_definitely (parser))
9798 else
9799 attributes = chainon (attributes, attrs);
9802 if (attributes != NULL_TREE)
9803 cplus_decl_attributes (&label, attributes, 0);
9805 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9808 /* Parse an expression-statement.
9810 expression-statement:
9811 expression [opt] ;
9813 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9814 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9815 indicates whether this expression-statement is part of an
9816 expression statement. */
9818 static tree
9819 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9821 tree statement = NULL_TREE;
9822 cp_token *token = cp_lexer_peek_token (parser->lexer);
9824 /* If the next token is a ';', then there is no expression
9825 statement. */
9826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9828 statement = cp_parser_expression (parser);
9829 if (statement == error_mark_node
9830 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9832 cp_parser_skip_to_end_of_block_or_statement (parser);
9833 return error_mark_node;
9837 /* Give a helpful message for "A<T>::type t;" and the like. */
9838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9839 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9841 if (TREE_CODE (statement) == SCOPE_REF)
9842 error_at (token->location, "need %<typename%> before %qE because "
9843 "%qT is a dependent scope",
9844 statement, TREE_OPERAND (statement, 0));
9845 else if (is_overloaded_fn (statement)
9846 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9848 /* A::A a; */
9849 tree fn = get_first_fn (statement);
9850 error_at (token->location,
9851 "%<%T::%D%> names the constructor, not the type",
9852 DECL_CONTEXT (fn), DECL_NAME (fn));
9856 /* Consume the final `;'. */
9857 cp_parser_consume_semicolon_at_end_of_statement (parser);
9859 if (in_statement_expr
9860 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9861 /* This is the final expression statement of a statement
9862 expression. */
9863 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9864 else if (statement)
9865 statement = finish_expr_stmt (statement);
9867 return statement;
9870 /* Parse a compound-statement.
9872 compound-statement:
9873 { statement-seq [opt] }
9875 GNU extension:
9877 compound-statement:
9878 { label-declaration-seq [opt] statement-seq [opt] }
9880 label-declaration-seq:
9881 label-declaration
9882 label-declaration-seq label-declaration
9884 Returns a tree representing the statement. */
9886 static tree
9887 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9888 bool in_try, bool function_body)
9890 tree compound_stmt;
9892 /* Consume the `{'. */
9893 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9894 return error_mark_node;
9895 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9896 && !function_body)
9897 pedwarn (input_location, OPT_Wpedantic,
9898 "compound-statement in constexpr function");
9899 /* Begin the compound-statement. */
9900 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9901 /* If the next keyword is `__label__' we have a label declaration. */
9902 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9903 cp_parser_label_declaration (parser);
9904 /* Parse an (optional) statement-seq. */
9905 cp_parser_statement_seq_opt (parser, in_statement_expr);
9906 /* Finish the compound-statement. */
9907 finish_compound_stmt (compound_stmt);
9908 /* Consume the `}'. */
9909 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9911 return compound_stmt;
9914 /* Parse an (optional) statement-seq.
9916 statement-seq:
9917 statement
9918 statement-seq [opt] statement */
9920 static void
9921 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9923 /* Scan statements until there aren't any more. */
9924 while (true)
9926 cp_token *token = cp_lexer_peek_token (parser->lexer);
9928 /* If we are looking at a `}', then we have run out of
9929 statements; the same is true if we have reached the end
9930 of file, or have stumbled upon a stray '@end'. */
9931 if (token->type == CPP_CLOSE_BRACE
9932 || token->type == CPP_EOF
9933 || token->type == CPP_PRAGMA_EOL
9934 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9935 break;
9937 /* If we are in a compound statement and find 'else' then
9938 something went wrong. */
9939 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9941 if (parser->in_statement & IN_IF_STMT)
9942 break;
9943 else
9945 token = cp_lexer_consume_token (parser->lexer);
9946 error_at (token->location, "%<else%> without a previous %<if%>");
9950 /* Parse the statement. */
9951 cp_parser_statement (parser, in_statement_expr, true, NULL);
9955 /* Parse a selection-statement.
9957 selection-statement:
9958 if ( condition ) statement
9959 if ( condition ) statement else statement
9960 switch ( condition ) statement
9962 Returns the new IF_STMT or SWITCH_STMT.
9964 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9965 is a (possibly labeled) if statement which is not enclosed in
9966 braces and has an else clause. This is used to implement
9967 -Wparentheses. */
9969 static tree
9970 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9972 cp_token *token;
9973 enum rid keyword;
9975 if (if_p != NULL)
9976 *if_p = false;
9978 /* Peek at the next token. */
9979 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9981 /* See what kind of keyword it is. */
9982 keyword = token->keyword;
9983 switch (keyword)
9985 case RID_IF:
9986 case RID_SWITCH:
9988 tree statement;
9989 tree condition;
9991 /* Look for the `('. */
9992 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9994 cp_parser_skip_to_end_of_statement (parser);
9995 return error_mark_node;
9998 /* Begin the selection-statement. */
9999 if (keyword == RID_IF)
10000 statement = begin_if_stmt ();
10001 else
10002 statement = begin_switch_stmt ();
10004 /* Parse the condition. */
10005 condition = cp_parser_condition (parser);
10006 /* Look for the `)'. */
10007 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10008 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10009 /*consume_paren=*/true);
10011 if (keyword == RID_IF)
10013 bool nested_if;
10014 unsigned char in_statement;
10016 /* Add the condition. */
10017 finish_if_stmt_cond (condition, statement);
10019 /* Parse the then-clause. */
10020 in_statement = parser->in_statement;
10021 parser->in_statement |= IN_IF_STMT;
10022 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10024 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10025 add_stmt (build_empty_stmt (loc));
10026 cp_lexer_consume_token (parser->lexer);
10027 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10028 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10029 "empty body in an %<if%> statement");
10030 nested_if = false;
10032 else
10033 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10034 parser->in_statement = in_statement;
10036 finish_then_clause (statement);
10038 /* If the next token is `else', parse the else-clause. */
10039 if (cp_lexer_next_token_is_keyword (parser->lexer,
10040 RID_ELSE))
10042 /* Consume the `else' keyword. */
10043 cp_lexer_consume_token (parser->lexer);
10044 begin_else_clause (statement);
10045 /* Parse the else-clause. */
10046 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10048 location_t loc;
10049 loc = cp_lexer_peek_token (parser->lexer)->location;
10050 warning_at (loc,
10051 OPT_Wempty_body, "suggest braces around "
10052 "empty body in an %<else%> statement");
10053 add_stmt (build_empty_stmt (loc));
10054 cp_lexer_consume_token (parser->lexer);
10056 else
10057 cp_parser_implicitly_scoped_statement (parser, NULL);
10059 finish_else_clause (statement);
10061 /* If we are currently parsing a then-clause, then
10062 IF_P will not be NULL. We set it to true to
10063 indicate that this if statement has an else clause.
10064 This may trigger the Wparentheses warning below
10065 when we get back up to the parent if statement. */
10066 if (if_p != NULL)
10067 *if_p = true;
10069 else
10071 /* This if statement does not have an else clause. If
10072 NESTED_IF is true, then the then-clause is an if
10073 statement which does have an else clause. We warn
10074 about the potential ambiguity. */
10075 if (nested_if)
10076 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10077 "suggest explicit braces to avoid ambiguous"
10078 " %<else%>");
10081 /* Now we're all done with the if-statement. */
10082 finish_if_stmt (statement);
10084 else
10086 bool in_switch_statement_p;
10087 unsigned char in_statement;
10089 /* Add the condition. */
10090 finish_switch_cond (condition, statement);
10092 /* Parse the body of the switch-statement. */
10093 in_switch_statement_p = parser->in_switch_statement_p;
10094 in_statement = parser->in_statement;
10095 parser->in_switch_statement_p = true;
10096 parser->in_statement |= IN_SWITCH_STMT;
10097 cp_parser_implicitly_scoped_statement (parser, NULL);
10098 parser->in_switch_statement_p = in_switch_statement_p;
10099 parser->in_statement = in_statement;
10101 /* Now we're all done with the switch-statement. */
10102 finish_switch_stmt (statement);
10105 return statement;
10107 break;
10109 default:
10110 cp_parser_error (parser, "expected selection-statement");
10111 return error_mark_node;
10115 /* Parse a condition.
10117 condition:
10118 expression
10119 type-specifier-seq declarator = initializer-clause
10120 type-specifier-seq declarator braced-init-list
10122 GNU Extension:
10124 condition:
10125 type-specifier-seq declarator asm-specification [opt]
10126 attributes [opt] = assignment-expression
10128 Returns the expression that should be tested. */
10130 static tree
10131 cp_parser_condition (cp_parser* parser)
10133 cp_decl_specifier_seq type_specifiers;
10134 const char *saved_message;
10135 int declares_class_or_enum;
10137 /* Try the declaration first. */
10138 cp_parser_parse_tentatively (parser);
10139 /* New types are not allowed in the type-specifier-seq for a
10140 condition. */
10141 saved_message = parser->type_definition_forbidden_message;
10142 parser->type_definition_forbidden_message
10143 = G_("types may not be defined in conditions");
10144 /* Parse the type-specifier-seq. */
10145 cp_parser_decl_specifier_seq (parser,
10146 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10147 &type_specifiers,
10148 &declares_class_or_enum);
10149 /* Restore the saved message. */
10150 parser->type_definition_forbidden_message = saved_message;
10151 /* If all is well, we might be looking at a declaration. */
10152 if (!cp_parser_error_occurred (parser))
10154 tree decl;
10155 tree asm_specification;
10156 tree attributes;
10157 cp_declarator *declarator;
10158 tree initializer = NULL_TREE;
10160 /* Parse the declarator. */
10161 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10162 /*ctor_dtor_or_conv_p=*/NULL,
10163 /*parenthesized_p=*/NULL,
10164 /*member_p=*/false,
10165 /*friend_p=*/false);
10166 /* Parse the attributes. */
10167 attributes = cp_parser_attributes_opt (parser);
10168 /* Parse the asm-specification. */
10169 asm_specification = cp_parser_asm_specification_opt (parser);
10170 /* If the next token is not an `=' or '{', then we might still be
10171 looking at an expression. For example:
10173 if (A(a).x)
10175 looks like a decl-specifier-seq and a declarator -- but then
10176 there is no `=', so this is an expression. */
10177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10178 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10179 cp_parser_simulate_error (parser);
10181 /* If we did see an `=' or '{', then we are looking at a declaration
10182 for sure. */
10183 if (cp_parser_parse_definitely (parser))
10185 tree pushed_scope;
10186 bool non_constant_p;
10187 bool flags = LOOKUP_ONLYCONVERTING;
10189 /* Create the declaration. */
10190 decl = start_decl (declarator, &type_specifiers,
10191 /*initialized_p=*/true,
10192 attributes, /*prefix_attributes=*/NULL_TREE,
10193 &pushed_scope);
10195 /* Parse the initializer. */
10196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10198 initializer = cp_parser_braced_list (parser, &non_constant_p);
10199 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10200 flags = 0;
10202 else
10204 /* Consume the `='. */
10205 cp_parser_require (parser, CPP_EQ, RT_EQ);
10206 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10208 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10209 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10211 /* Process the initializer. */
10212 cp_finish_decl (decl,
10213 initializer, !non_constant_p,
10214 asm_specification,
10215 flags);
10217 if (pushed_scope)
10218 pop_scope (pushed_scope);
10220 return convert_from_reference (decl);
10223 /* If we didn't even get past the declarator successfully, we are
10224 definitely not looking at a declaration. */
10225 else
10226 cp_parser_abort_tentative_parse (parser);
10228 /* Otherwise, we are looking at an expression. */
10229 return cp_parser_expression (parser);
10232 /* Parses a for-statement or range-for-statement until the closing ')',
10233 not included. */
10235 static tree
10236 cp_parser_for (cp_parser *parser, bool ivdep)
10238 tree init, scope, decl;
10239 bool is_range_for;
10241 /* Begin the for-statement. */
10242 scope = begin_for_scope (&init);
10244 /* Parse the initialization. */
10245 is_range_for = cp_parser_for_init_statement (parser, &decl);
10247 if (is_range_for)
10248 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10249 else
10250 return cp_parser_c_for (parser, scope, init, ivdep);
10253 static tree
10254 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10256 /* Normal for loop */
10257 tree condition = NULL_TREE;
10258 tree expression = NULL_TREE;
10259 tree stmt;
10261 stmt = begin_for_stmt (scope, init);
10262 /* The for-init-statement has already been parsed in
10263 cp_parser_for_init_statement, so no work is needed here. */
10264 finish_for_init_stmt (stmt);
10266 /* If there's a condition, process it. */
10267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10268 condition = cp_parser_condition (parser);
10269 else if (ivdep)
10271 cp_parser_error (parser, "missing loop condition in loop with "
10272 "%<GCC ivdep%> pragma");
10273 condition = error_mark_node;
10275 finish_for_cond (condition, stmt, ivdep);
10276 /* Look for the `;'. */
10277 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10279 /* If there's an expression, process it. */
10280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10281 expression = cp_parser_expression (parser);
10282 finish_for_expr (expression, stmt);
10284 return stmt;
10287 /* Tries to parse a range-based for-statement:
10289 range-based-for:
10290 decl-specifier-seq declarator : expression
10292 The decl-specifier-seq declarator and the `:' are already parsed by
10293 cp_parser_for_init_statement. If processing_template_decl it returns a
10294 newly created RANGE_FOR_STMT; if not, it is converted to a
10295 regular FOR_STMT. */
10297 static tree
10298 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10299 bool ivdep)
10301 tree stmt, range_expr;
10303 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10305 bool expr_non_constant_p;
10306 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10308 else
10309 range_expr = cp_parser_expression (parser);
10311 /* If in template, STMT is converted to a normal for-statement
10312 at instantiation. If not, it is done just ahead. */
10313 if (processing_template_decl)
10315 if (check_for_bare_parameter_packs (range_expr))
10316 range_expr = error_mark_node;
10317 stmt = begin_range_for_stmt (scope, init);
10318 if (ivdep)
10319 RANGE_FOR_IVDEP (stmt) = 1;
10320 finish_range_for_decl (stmt, range_decl, range_expr);
10321 if (!type_dependent_expression_p (range_expr)
10322 /* do_auto_deduction doesn't mess with template init-lists. */
10323 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10324 do_range_for_auto_deduction (range_decl, range_expr);
10326 else
10328 stmt = begin_for_stmt (scope, init);
10329 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10331 return stmt;
10334 /* Subroutine of cp_convert_range_for: given the initializer expression,
10335 builds up the range temporary. */
10337 static tree
10338 build_range_temp (tree range_expr)
10340 tree range_type, range_temp;
10342 /* Find out the type deduced by the declaration
10343 `auto &&__range = range_expr'. */
10344 range_type = cp_build_reference_type (make_auto (), true);
10345 range_type = do_auto_deduction (range_type, range_expr,
10346 type_uses_auto (range_type));
10348 /* Create the __range variable. */
10349 range_temp = build_decl (input_location, VAR_DECL,
10350 get_identifier ("__for_range"), range_type);
10351 TREE_USED (range_temp) = 1;
10352 DECL_ARTIFICIAL (range_temp) = 1;
10354 return range_temp;
10357 /* Used by cp_parser_range_for in template context: we aren't going to
10358 do a full conversion yet, but we still need to resolve auto in the
10359 type of the for-range-declaration if present. This is basically
10360 a shortcut version of cp_convert_range_for. */
10362 static void
10363 do_range_for_auto_deduction (tree decl, tree range_expr)
10365 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10366 if (auto_node)
10368 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10369 range_temp = convert_from_reference (build_range_temp (range_expr));
10370 iter_type = (cp_parser_perform_range_for_lookup
10371 (range_temp, &begin_dummy, &end_dummy));
10372 if (iter_type)
10374 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10375 iter_type);
10376 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10377 tf_warning_or_error);
10378 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10379 iter_decl, auto_node);
10384 /* Converts a range-based for-statement into a normal
10385 for-statement, as per the definition.
10387 for (RANGE_DECL : RANGE_EXPR)
10388 BLOCK
10390 should be equivalent to:
10393 auto &&__range = RANGE_EXPR;
10394 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10395 __begin != __end;
10396 ++__begin)
10398 RANGE_DECL = *__begin;
10399 BLOCK
10403 If RANGE_EXPR is an array:
10404 BEGIN_EXPR = __range
10405 END_EXPR = __range + ARRAY_SIZE(__range)
10406 Else if RANGE_EXPR has a member 'begin' or 'end':
10407 BEGIN_EXPR = __range.begin()
10408 END_EXPR = __range.end()
10409 Else:
10410 BEGIN_EXPR = begin(__range)
10411 END_EXPR = end(__range);
10413 If __range has a member 'begin' but not 'end', or vice versa, we must
10414 still use the second alternative (it will surely fail, however).
10415 When calling begin()/end() in the third alternative we must use
10416 argument dependent lookup, but always considering 'std' as an associated
10417 namespace. */
10419 tree
10420 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10421 bool ivdep)
10423 tree begin, end;
10424 tree iter_type, begin_expr, end_expr;
10425 tree condition, expression;
10427 if (range_decl == error_mark_node || range_expr == error_mark_node)
10428 /* If an error happened previously do nothing or else a lot of
10429 unhelpful errors would be issued. */
10430 begin_expr = end_expr = iter_type = error_mark_node;
10431 else
10433 tree range_temp;
10435 if (TREE_CODE (range_expr) == VAR_DECL
10436 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10437 /* Can't bind a reference to an array of runtime bound. */
10438 range_temp = range_expr;
10439 else
10441 range_temp = build_range_temp (range_expr);
10442 pushdecl (range_temp);
10443 cp_finish_decl (range_temp, range_expr,
10444 /*is_constant_init*/false, NULL_TREE,
10445 LOOKUP_ONLYCONVERTING);
10446 range_temp = convert_from_reference (range_temp);
10448 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10449 &begin_expr, &end_expr);
10452 /* The new for initialization statement. */
10453 begin = build_decl (input_location, VAR_DECL,
10454 get_identifier ("__for_begin"), iter_type);
10455 TREE_USED (begin) = 1;
10456 DECL_ARTIFICIAL (begin) = 1;
10457 pushdecl (begin);
10458 cp_finish_decl (begin, begin_expr,
10459 /*is_constant_init*/false, NULL_TREE,
10460 LOOKUP_ONLYCONVERTING);
10462 end = build_decl (input_location, VAR_DECL,
10463 get_identifier ("__for_end"), iter_type);
10464 TREE_USED (end) = 1;
10465 DECL_ARTIFICIAL (end) = 1;
10466 pushdecl (end);
10467 cp_finish_decl (end, end_expr,
10468 /*is_constant_init*/false, NULL_TREE,
10469 LOOKUP_ONLYCONVERTING);
10471 finish_for_init_stmt (statement);
10473 /* The new for condition. */
10474 condition = build_x_binary_op (input_location, NE_EXPR,
10475 begin, ERROR_MARK,
10476 end, ERROR_MARK,
10477 NULL, tf_warning_or_error);
10478 finish_for_cond (condition, statement, ivdep);
10480 /* The new increment expression. */
10481 expression = finish_unary_op_expr (input_location,
10482 PREINCREMENT_EXPR, begin,
10483 tf_warning_or_error);
10484 finish_for_expr (expression, statement);
10486 /* The declaration is initialized with *__begin inside the loop body. */
10487 cp_finish_decl (range_decl,
10488 build_x_indirect_ref (input_location, begin, RO_NULL,
10489 tf_warning_or_error),
10490 /*is_constant_init*/false, NULL_TREE,
10491 LOOKUP_ONLYCONVERTING);
10493 return statement;
10496 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10497 We need to solve both at the same time because the method used
10498 depends on the existence of members begin or end.
10499 Returns the type deduced for the iterator expression. */
10501 static tree
10502 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10504 if (error_operand_p (range))
10506 *begin = *end = error_mark_node;
10507 return error_mark_node;
10510 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10512 error ("range-based %<for%> expression of type %qT "
10513 "has incomplete type", TREE_TYPE (range));
10514 *begin = *end = error_mark_node;
10515 return error_mark_node;
10517 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10519 /* If RANGE is an array, we will use pointer arithmetic. */
10520 *begin = range;
10521 *end = build_binary_op (input_location, PLUS_EXPR,
10522 range,
10523 array_type_nelts_top (TREE_TYPE (range)),
10525 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10527 else
10529 /* If it is not an array, we must do a bit of magic. */
10530 tree id_begin, id_end;
10531 tree member_begin, member_end;
10533 *begin = *end = error_mark_node;
10535 id_begin = get_identifier ("begin");
10536 id_end = get_identifier ("end");
10537 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10538 /*protect=*/2, /*want_type=*/false,
10539 tf_warning_or_error);
10540 member_end = lookup_member (TREE_TYPE (range), id_end,
10541 /*protect=*/2, /*want_type=*/false,
10542 tf_warning_or_error);
10544 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10546 /* Use the member functions. */
10547 if (member_begin != NULL_TREE)
10548 *begin = cp_parser_range_for_member_function (range, id_begin);
10549 else
10550 error ("range-based %<for%> expression of type %qT has an "
10551 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10553 if (member_end != NULL_TREE)
10554 *end = cp_parser_range_for_member_function (range, id_end);
10555 else
10556 error ("range-based %<for%> expression of type %qT has a "
10557 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10559 else
10561 /* Use global functions with ADL. */
10562 vec<tree, va_gc> *vec;
10563 vec = make_tree_vector ();
10565 vec_safe_push (vec, range);
10567 member_begin = perform_koenig_lookup (id_begin, vec,
10568 tf_warning_or_error);
10569 *begin = finish_call_expr (member_begin, &vec, false, true,
10570 tf_warning_or_error);
10571 member_end = perform_koenig_lookup (id_end, vec,
10572 tf_warning_or_error);
10573 *end = finish_call_expr (member_end, &vec, false, true,
10574 tf_warning_or_error);
10576 release_tree_vector (vec);
10579 /* Last common checks. */
10580 if (*begin == error_mark_node || *end == error_mark_node)
10582 /* If one of the expressions is an error do no more checks. */
10583 *begin = *end = error_mark_node;
10584 return error_mark_node;
10586 else if (type_dependent_expression_p (*begin)
10587 || type_dependent_expression_p (*end))
10588 /* Can happen, when, eg, in a template context, Koenig lookup
10589 can't resolve begin/end (c++/58503). */
10590 return NULL_TREE;
10591 else
10593 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10594 /* The unqualified type of the __begin and __end temporaries should
10595 be the same, as required by the multiple auto declaration. */
10596 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10597 error ("inconsistent begin/end types in range-based %<for%> "
10598 "statement: %qT and %qT",
10599 TREE_TYPE (*begin), TREE_TYPE (*end));
10600 return iter_type;
10605 /* Helper function for cp_parser_perform_range_for_lookup.
10606 Builds a tree for RANGE.IDENTIFIER(). */
10608 static tree
10609 cp_parser_range_for_member_function (tree range, tree identifier)
10611 tree member, res;
10612 vec<tree, va_gc> *vec;
10614 member = finish_class_member_access_expr (range, identifier,
10615 false, tf_warning_or_error);
10616 if (member == error_mark_node)
10617 return error_mark_node;
10619 vec = make_tree_vector ();
10620 res = finish_call_expr (member, &vec,
10621 /*disallow_virtual=*/false,
10622 /*koenig_p=*/false,
10623 tf_warning_or_error);
10624 release_tree_vector (vec);
10625 return res;
10628 /* Parse an iteration-statement.
10630 iteration-statement:
10631 while ( condition ) statement
10632 do statement while ( expression ) ;
10633 for ( for-init-statement condition [opt] ; expression [opt] )
10634 statement
10636 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10638 static tree
10639 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10641 cp_token *token;
10642 enum rid keyword;
10643 tree statement;
10644 unsigned char in_statement;
10646 /* Peek at the next token. */
10647 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10648 if (!token)
10649 return error_mark_node;
10651 /* Remember whether or not we are already within an iteration
10652 statement. */
10653 in_statement = parser->in_statement;
10655 /* See what kind of keyword it is. */
10656 keyword = token->keyword;
10657 switch (keyword)
10659 case RID_WHILE:
10661 tree condition;
10663 /* Begin the while-statement. */
10664 statement = begin_while_stmt ();
10665 /* Look for the `('. */
10666 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10667 /* Parse the condition. */
10668 condition = cp_parser_condition (parser);
10669 finish_while_stmt_cond (condition, statement, ivdep);
10670 /* Look for the `)'. */
10671 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10672 /* Parse the dependent statement. */
10673 parser->in_statement = IN_ITERATION_STMT;
10674 cp_parser_already_scoped_statement (parser);
10675 parser->in_statement = in_statement;
10676 /* We're done with the while-statement. */
10677 finish_while_stmt (statement);
10679 break;
10681 case RID_DO:
10683 tree expression;
10685 /* Begin the do-statement. */
10686 statement = begin_do_stmt ();
10687 /* Parse the body of the do-statement. */
10688 parser->in_statement = IN_ITERATION_STMT;
10689 cp_parser_implicitly_scoped_statement (parser, NULL);
10690 parser->in_statement = in_statement;
10691 finish_do_body (statement);
10692 /* Look for the `while' keyword. */
10693 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10694 /* Look for the `('. */
10695 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10696 /* Parse the expression. */
10697 expression = cp_parser_expression (parser);
10698 /* We're done with the do-statement. */
10699 finish_do_stmt (expression, statement, ivdep);
10700 /* Look for the `)'. */
10701 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10702 /* Look for the `;'. */
10703 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10705 break;
10707 case RID_FOR:
10709 /* Look for the `('. */
10710 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10712 statement = cp_parser_for (parser, ivdep);
10714 /* Look for the `)'. */
10715 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10717 /* Parse the body of the for-statement. */
10718 parser->in_statement = IN_ITERATION_STMT;
10719 cp_parser_already_scoped_statement (parser);
10720 parser->in_statement = in_statement;
10722 /* We're done with the for-statement. */
10723 finish_for_stmt (statement);
10725 break;
10727 default:
10728 cp_parser_error (parser, "expected iteration-statement");
10729 statement = error_mark_node;
10730 break;
10733 return statement;
10736 /* Parse a for-init-statement or the declarator of a range-based-for.
10737 Returns true if a range-based-for declaration is seen.
10739 for-init-statement:
10740 expression-statement
10741 simple-declaration */
10743 static bool
10744 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10746 /* If the next token is a `;', then we have an empty
10747 expression-statement. Grammatically, this is also a
10748 simple-declaration, but an invalid one, because it does not
10749 declare anything. Therefore, if we did not handle this case
10750 specially, we would issue an error message about an invalid
10751 declaration. */
10752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10754 bool is_range_for = false;
10755 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10757 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10758 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10760 /* N3994 -- for (id : init) ... */
10761 if (cxx_dialect < cxx1z)
10762 pedwarn (input_location, 0, "range-based for loop without a "
10763 "type-specifier only available with "
10764 "-std=c++1z or -std=gnu++1z");
10765 tree name = cp_parser_identifier (parser);
10766 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10767 *decl = build_decl (input_location, VAR_DECL, name, type);
10768 pushdecl (*decl);
10769 cp_lexer_consume_token (parser->lexer);
10770 return true;
10773 /* A colon is used in range-based for. */
10774 parser->colon_corrects_to_scope_p = false;
10776 /* We're going to speculatively look for a declaration, falling back
10777 to an expression, if necessary. */
10778 cp_parser_parse_tentatively (parser);
10779 /* Parse the declaration. */
10780 cp_parser_simple_declaration (parser,
10781 /*function_definition_allowed_p=*/false,
10782 decl);
10783 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10784 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10786 /* It is a range-for, consume the ':' */
10787 cp_lexer_consume_token (parser->lexer);
10788 is_range_for = true;
10789 if (cxx_dialect < cxx11)
10791 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10792 "range-based %<for%> loops only available with "
10793 "-std=c++11 or -std=gnu++11");
10794 *decl = error_mark_node;
10797 else
10798 /* The ';' is not consumed yet because we told
10799 cp_parser_simple_declaration not to. */
10800 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10802 if (cp_parser_parse_definitely (parser))
10803 return is_range_for;
10804 /* If the tentative parse failed, then we shall need to look for an
10805 expression-statement. */
10807 /* If we are here, it is an expression-statement. */
10808 cp_parser_expression_statement (parser, NULL_TREE);
10809 return false;
10812 /* Parse a jump-statement.
10814 jump-statement:
10815 break ;
10816 continue ;
10817 return expression [opt] ;
10818 return braced-init-list ;
10819 goto identifier ;
10821 GNU extension:
10823 jump-statement:
10824 goto * expression ;
10826 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10828 static tree
10829 cp_parser_jump_statement (cp_parser* parser)
10831 tree statement = error_mark_node;
10832 cp_token *token;
10833 enum rid keyword;
10834 unsigned char in_statement;
10836 /* Peek at the next token. */
10837 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10838 if (!token)
10839 return error_mark_node;
10841 /* See what kind of keyword it is. */
10842 keyword = token->keyword;
10843 switch (keyword)
10845 case RID_BREAK:
10846 in_statement = parser->in_statement & ~IN_IF_STMT;
10847 switch (in_statement)
10849 case 0:
10850 error_at (token->location, "break statement not within loop or switch");
10851 break;
10852 default:
10853 gcc_assert ((in_statement & IN_SWITCH_STMT)
10854 || in_statement == IN_ITERATION_STMT);
10855 statement = finish_break_stmt ();
10856 if (in_statement == IN_ITERATION_STMT)
10857 break_maybe_infinite_loop ();
10858 break;
10859 case IN_OMP_BLOCK:
10860 error_at (token->location, "invalid exit from OpenMP structured block");
10861 break;
10862 case IN_OMP_FOR:
10863 error_at (token->location, "break statement used with OpenMP for loop");
10864 break;
10865 case IN_CILK_SIMD_FOR:
10866 error_at (token->location, "break statement used with Cilk Plus for loop");
10867 break;
10869 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10870 break;
10872 case RID_CONTINUE:
10873 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10875 case 0:
10876 error_at (token->location, "continue statement not within a loop");
10877 break;
10878 case IN_CILK_SIMD_FOR:
10879 error_at (token->location,
10880 "continue statement within %<#pragma simd%> loop body");
10881 /* Fall through. */
10882 case IN_ITERATION_STMT:
10883 case IN_OMP_FOR:
10884 statement = finish_continue_stmt ();
10885 break;
10886 case IN_OMP_BLOCK:
10887 error_at (token->location, "invalid exit from OpenMP structured block");
10888 break;
10889 default:
10890 gcc_unreachable ();
10892 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10893 break;
10895 case RID_RETURN:
10897 tree expr;
10898 bool expr_non_constant_p;
10900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10902 cp_lexer_set_source_position (parser->lexer);
10903 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10904 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10906 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10907 expr = cp_parser_expression (parser);
10908 else
10909 /* If the next token is a `;', then there is no
10910 expression. */
10911 expr = NULL_TREE;
10912 /* Build the return-statement. */
10913 statement = finish_return_stmt (expr);
10914 /* Look for the final `;'. */
10915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10917 break;
10919 case RID_GOTO:
10920 /* Create the goto-statement. */
10921 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10923 /* Issue a warning about this use of a GNU extension. */
10924 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10925 /* Consume the '*' token. */
10926 cp_lexer_consume_token (parser->lexer);
10927 /* Parse the dependent expression. */
10928 finish_goto_stmt (cp_parser_expression (parser));
10930 else
10931 finish_goto_stmt (cp_parser_identifier (parser));
10932 /* Look for the final `;'. */
10933 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10934 break;
10936 default:
10937 cp_parser_error (parser, "expected jump-statement");
10938 break;
10941 return statement;
10944 /* Parse a declaration-statement.
10946 declaration-statement:
10947 block-declaration */
10949 static void
10950 cp_parser_declaration_statement (cp_parser* parser)
10952 void *p;
10954 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10955 p = obstack_alloc (&declarator_obstack, 0);
10957 /* Parse the block-declaration. */
10958 cp_parser_block_declaration (parser, /*statement_p=*/true);
10960 /* Free any declarators allocated. */
10961 obstack_free (&declarator_obstack, p);
10964 /* Some dependent statements (like `if (cond) statement'), are
10965 implicitly in their own scope. In other words, if the statement is
10966 a single statement (as opposed to a compound-statement), it is
10967 none-the-less treated as if it were enclosed in braces. Any
10968 declarations appearing in the dependent statement are out of scope
10969 after control passes that point. This function parses a statement,
10970 but ensures that is in its own scope, even if it is not a
10971 compound-statement.
10973 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10974 is a (possibly labeled) if statement which is not enclosed in
10975 braces and has an else clause. This is used to implement
10976 -Wparentheses.
10978 Returns the new statement. */
10980 static tree
10981 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10983 tree statement;
10985 if (if_p != NULL)
10986 *if_p = false;
10988 /* Mark if () ; with a special NOP_EXPR. */
10989 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10991 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10992 cp_lexer_consume_token (parser->lexer);
10993 statement = add_stmt (build_empty_stmt (loc));
10995 /* if a compound is opened, we simply parse the statement directly. */
10996 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10997 statement = cp_parser_compound_statement (parser, NULL, false, false);
10998 /* If the token is not a `{', then we must take special action. */
10999 else
11001 /* Create a compound-statement. */
11002 statement = begin_compound_stmt (0);
11003 /* Parse the dependent-statement. */
11004 cp_parser_statement (parser, NULL_TREE, false, if_p);
11005 /* Finish the dummy compound-statement. */
11006 finish_compound_stmt (statement);
11009 /* Return the statement. */
11010 return statement;
11013 /* For some dependent statements (like `while (cond) statement'), we
11014 have already created a scope. Therefore, even if the dependent
11015 statement is a compound-statement, we do not want to create another
11016 scope. */
11018 static void
11019 cp_parser_already_scoped_statement (cp_parser* parser)
11021 /* If the token is a `{', then we must take special action. */
11022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11023 cp_parser_statement (parser, NULL_TREE, false, NULL);
11024 else
11026 /* Avoid calling cp_parser_compound_statement, so that we
11027 don't create a new scope. Do everything else by hand. */
11028 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11029 /* If the next keyword is `__label__' we have a label declaration. */
11030 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11031 cp_parser_label_declaration (parser);
11032 /* Parse an (optional) statement-seq. */
11033 cp_parser_statement_seq_opt (parser, NULL_TREE);
11034 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11038 /* Declarations [gram.dcl.dcl] */
11040 /* Parse an optional declaration-sequence.
11042 declaration-seq:
11043 declaration
11044 declaration-seq declaration */
11046 static void
11047 cp_parser_declaration_seq_opt (cp_parser* parser)
11049 while (true)
11051 cp_token *token;
11053 token = cp_lexer_peek_token (parser->lexer);
11055 if (token->type == CPP_CLOSE_BRACE
11056 || token->type == CPP_EOF
11057 || token->type == CPP_PRAGMA_EOL)
11058 break;
11060 if (token->type == CPP_SEMICOLON)
11062 /* A declaration consisting of a single semicolon is
11063 invalid. Allow it unless we're being pedantic. */
11064 cp_lexer_consume_token (parser->lexer);
11065 if (!in_system_header_at (input_location))
11066 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11067 continue;
11070 /* If we're entering or exiting a region that's implicitly
11071 extern "C", modify the lang context appropriately. */
11072 if (!parser->implicit_extern_c && token->implicit_extern_c)
11074 push_lang_context (lang_name_c);
11075 parser->implicit_extern_c = true;
11077 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11079 pop_lang_context ();
11080 parser->implicit_extern_c = false;
11083 if (token->type == CPP_PRAGMA)
11085 /* A top-level declaration can consist solely of a #pragma.
11086 A nested declaration cannot, so this is done here and not
11087 in cp_parser_declaration. (A #pragma at block scope is
11088 handled in cp_parser_statement.) */
11089 cp_parser_pragma (parser, pragma_external);
11090 continue;
11093 /* Parse the declaration itself. */
11094 cp_parser_declaration (parser);
11098 /* Parse a declaration.
11100 declaration:
11101 block-declaration
11102 function-definition
11103 template-declaration
11104 explicit-instantiation
11105 explicit-specialization
11106 linkage-specification
11107 namespace-definition
11109 GNU extension:
11111 declaration:
11112 __extension__ declaration */
11114 static void
11115 cp_parser_declaration (cp_parser* parser)
11117 cp_token token1;
11118 cp_token token2;
11119 int saved_pedantic;
11120 void *p;
11121 tree attributes = NULL_TREE;
11123 /* Check for the `__extension__' keyword. */
11124 if (cp_parser_extension_opt (parser, &saved_pedantic))
11126 /* Parse the qualified declaration. */
11127 cp_parser_declaration (parser);
11128 /* Restore the PEDANTIC flag. */
11129 pedantic = saved_pedantic;
11131 return;
11134 /* Try to figure out what kind of declaration is present. */
11135 token1 = *cp_lexer_peek_token (parser->lexer);
11137 if (token1.type != CPP_EOF)
11138 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11139 else
11141 token2.type = CPP_EOF;
11142 token2.keyword = RID_MAX;
11145 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11146 p = obstack_alloc (&declarator_obstack, 0);
11148 /* If the next token is `extern' and the following token is a string
11149 literal, then we have a linkage specification. */
11150 if (token1.keyword == RID_EXTERN
11151 && cp_parser_is_pure_string_literal (&token2))
11152 cp_parser_linkage_specification (parser);
11153 /* If the next token is `template', then we have either a template
11154 declaration, an explicit instantiation, or an explicit
11155 specialization. */
11156 else if (token1.keyword == RID_TEMPLATE)
11158 /* `template <>' indicates a template specialization. */
11159 if (token2.type == CPP_LESS
11160 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11161 cp_parser_explicit_specialization (parser);
11162 /* `template <' indicates a template declaration. */
11163 else if (token2.type == CPP_LESS)
11164 cp_parser_template_declaration (parser, /*member_p=*/false);
11165 /* Anything else must be an explicit instantiation. */
11166 else
11167 cp_parser_explicit_instantiation (parser);
11169 /* If the next token is `export', then we have a template
11170 declaration. */
11171 else if (token1.keyword == RID_EXPORT)
11172 cp_parser_template_declaration (parser, /*member_p=*/false);
11173 /* If the next token is `extern', 'static' or 'inline' and the one
11174 after that is `template', we have a GNU extended explicit
11175 instantiation directive. */
11176 else if (cp_parser_allow_gnu_extensions_p (parser)
11177 && (token1.keyword == RID_EXTERN
11178 || token1.keyword == RID_STATIC
11179 || token1.keyword == RID_INLINE)
11180 && token2.keyword == RID_TEMPLATE)
11181 cp_parser_explicit_instantiation (parser);
11182 /* If the next token is `namespace', check for a named or unnamed
11183 namespace definition. */
11184 else if (token1.keyword == RID_NAMESPACE
11185 && (/* A named namespace definition. */
11186 (token2.type == CPP_NAME
11187 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11188 != CPP_EQ))
11189 /* An unnamed namespace definition. */
11190 || token2.type == CPP_OPEN_BRACE
11191 || token2.keyword == RID_ATTRIBUTE))
11192 cp_parser_namespace_definition (parser);
11193 /* An inline (associated) namespace definition. */
11194 else if (token1.keyword == RID_INLINE
11195 && token2.keyword == RID_NAMESPACE)
11196 cp_parser_namespace_definition (parser);
11197 /* Objective-C++ declaration/definition. */
11198 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11199 cp_parser_objc_declaration (parser, NULL_TREE);
11200 else if (c_dialect_objc ()
11201 && token1.keyword == RID_ATTRIBUTE
11202 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11203 cp_parser_objc_declaration (parser, attributes);
11204 /* We must have either a block declaration or a function
11205 definition. */
11206 else
11207 /* Try to parse a block-declaration, or a function-definition. */
11208 cp_parser_block_declaration (parser, /*statement_p=*/false);
11210 /* Free any declarators allocated. */
11211 obstack_free (&declarator_obstack, p);
11214 /* Parse a block-declaration.
11216 block-declaration:
11217 simple-declaration
11218 asm-definition
11219 namespace-alias-definition
11220 using-declaration
11221 using-directive
11223 GNU Extension:
11225 block-declaration:
11226 __extension__ block-declaration
11228 C++0x Extension:
11230 block-declaration:
11231 static_assert-declaration
11233 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11234 part of a declaration-statement. */
11236 static void
11237 cp_parser_block_declaration (cp_parser *parser,
11238 bool statement_p)
11240 cp_token *token1;
11241 int saved_pedantic;
11243 /* Check for the `__extension__' keyword. */
11244 if (cp_parser_extension_opt (parser, &saved_pedantic))
11246 /* Parse the qualified declaration. */
11247 cp_parser_block_declaration (parser, statement_p);
11248 /* Restore the PEDANTIC flag. */
11249 pedantic = saved_pedantic;
11251 return;
11254 /* Peek at the next token to figure out which kind of declaration is
11255 present. */
11256 token1 = cp_lexer_peek_token (parser->lexer);
11258 /* If the next keyword is `asm', we have an asm-definition. */
11259 if (token1->keyword == RID_ASM)
11261 if (statement_p)
11262 cp_parser_commit_to_tentative_parse (parser);
11263 cp_parser_asm_definition (parser);
11265 /* If the next keyword is `namespace', we have a
11266 namespace-alias-definition. */
11267 else if (token1->keyword == RID_NAMESPACE)
11268 cp_parser_namespace_alias_definition (parser);
11269 /* If the next keyword is `using', we have a
11270 using-declaration, a using-directive, or an alias-declaration. */
11271 else if (token1->keyword == RID_USING)
11273 cp_token *token2;
11275 if (statement_p)
11276 cp_parser_commit_to_tentative_parse (parser);
11277 /* If the token after `using' is `namespace', then we have a
11278 using-directive. */
11279 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11280 if (token2->keyword == RID_NAMESPACE)
11281 cp_parser_using_directive (parser);
11282 /* If the second token after 'using' is '=', then we have an
11283 alias-declaration. */
11284 else if (cxx_dialect >= cxx11
11285 && token2->type == CPP_NAME
11286 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11287 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11288 cp_parser_alias_declaration (parser);
11289 /* Otherwise, it's a using-declaration. */
11290 else
11291 cp_parser_using_declaration (parser,
11292 /*access_declaration_p=*/false);
11294 /* If the next keyword is `__label__' we have a misplaced label
11295 declaration. */
11296 else if (token1->keyword == RID_LABEL)
11298 cp_lexer_consume_token (parser->lexer);
11299 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11300 cp_parser_skip_to_end_of_statement (parser);
11301 /* If the next token is now a `;', consume it. */
11302 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11303 cp_lexer_consume_token (parser->lexer);
11305 /* If the next token is `static_assert' we have a static assertion. */
11306 else if (token1->keyword == RID_STATIC_ASSERT)
11307 cp_parser_static_assert (parser, /*member_p=*/false);
11308 /* Anything else must be a simple-declaration. */
11309 else
11310 cp_parser_simple_declaration (parser, !statement_p,
11311 /*maybe_range_for_decl*/NULL);
11314 /* Parse a simple-declaration.
11316 simple-declaration:
11317 decl-specifier-seq [opt] init-declarator-list [opt] ;
11319 init-declarator-list:
11320 init-declarator
11321 init-declarator-list , init-declarator
11323 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11324 function-definition as a simple-declaration.
11326 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11327 parsed declaration if it is an uninitialized single declarator not followed
11328 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11329 if present, will not be consumed. */
11331 static void
11332 cp_parser_simple_declaration (cp_parser* parser,
11333 bool function_definition_allowed_p,
11334 tree *maybe_range_for_decl)
11336 cp_decl_specifier_seq decl_specifiers;
11337 int declares_class_or_enum;
11338 bool saw_declarator;
11340 if (maybe_range_for_decl)
11341 *maybe_range_for_decl = NULL_TREE;
11343 /* Defer access checks until we know what is being declared; the
11344 checks for names appearing in the decl-specifier-seq should be
11345 done as if we were in the scope of the thing being declared. */
11346 push_deferring_access_checks (dk_deferred);
11348 /* Parse the decl-specifier-seq. We have to keep track of whether
11349 or not the decl-specifier-seq declares a named class or
11350 enumeration type, since that is the only case in which the
11351 init-declarator-list is allowed to be empty.
11353 [dcl.dcl]
11355 In a simple-declaration, the optional init-declarator-list can be
11356 omitted only when declaring a class or enumeration, that is when
11357 the decl-specifier-seq contains either a class-specifier, an
11358 elaborated-type-specifier, or an enum-specifier. */
11359 cp_parser_decl_specifier_seq (parser,
11360 CP_PARSER_FLAGS_OPTIONAL,
11361 &decl_specifiers,
11362 &declares_class_or_enum);
11363 /* We no longer need to defer access checks. */
11364 stop_deferring_access_checks ();
11366 /* In a block scope, a valid declaration must always have a
11367 decl-specifier-seq. By not trying to parse declarators, we can
11368 resolve the declaration/expression ambiguity more quickly. */
11369 if (!function_definition_allowed_p
11370 && !decl_specifiers.any_specifiers_p)
11372 cp_parser_error (parser, "expected declaration");
11373 goto done;
11376 /* If the next two tokens are both identifiers, the code is
11377 erroneous. The usual cause of this situation is code like:
11379 T t;
11381 where "T" should name a type -- but does not. */
11382 if (!decl_specifiers.any_type_specifiers_p
11383 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11385 /* If parsing tentatively, we should commit; we really are
11386 looking at a declaration. */
11387 cp_parser_commit_to_tentative_parse (parser);
11388 /* Give up. */
11389 goto done;
11392 /* If we have seen at least one decl-specifier, and the next token
11393 is not a parenthesis, then we must be looking at a declaration.
11394 (After "int (" we might be looking at a functional cast.) */
11395 if (decl_specifiers.any_specifiers_p
11396 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11397 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11398 && !cp_parser_error_occurred (parser))
11399 cp_parser_commit_to_tentative_parse (parser);
11401 /* Keep going until we hit the `;' at the end of the simple
11402 declaration. */
11403 saw_declarator = false;
11404 while (cp_lexer_next_token_is_not (parser->lexer,
11405 CPP_SEMICOLON))
11407 cp_token *token;
11408 bool function_definition_p;
11409 tree decl;
11411 if (saw_declarator)
11413 /* If we are processing next declarator, coma is expected */
11414 token = cp_lexer_peek_token (parser->lexer);
11415 gcc_assert (token->type == CPP_COMMA);
11416 cp_lexer_consume_token (parser->lexer);
11417 if (maybe_range_for_decl)
11418 *maybe_range_for_decl = error_mark_node;
11420 else
11421 saw_declarator = true;
11423 /* Parse the init-declarator. */
11424 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11425 /*checks=*/NULL,
11426 function_definition_allowed_p,
11427 /*member_p=*/false,
11428 declares_class_or_enum,
11429 &function_definition_p,
11430 maybe_range_for_decl);
11431 /* If an error occurred while parsing tentatively, exit quickly.
11432 (That usually happens when in the body of a function; each
11433 statement is treated as a declaration-statement until proven
11434 otherwise.) */
11435 if (cp_parser_error_occurred (parser))
11436 goto done;
11437 /* Handle function definitions specially. */
11438 if (function_definition_p)
11440 /* If the next token is a `,', then we are probably
11441 processing something like:
11443 void f() {}, *p;
11445 which is erroneous. */
11446 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11448 cp_token *token = cp_lexer_peek_token (parser->lexer);
11449 error_at (token->location,
11450 "mixing"
11451 " declarations and function-definitions is forbidden");
11453 /* Otherwise, we're done with the list of declarators. */
11454 else
11456 pop_deferring_access_checks ();
11457 return;
11460 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11461 *maybe_range_for_decl = decl;
11462 /* The next token should be either a `,' or a `;'. */
11463 token = cp_lexer_peek_token (parser->lexer);
11464 /* If it's a `,', there are more declarators to come. */
11465 if (token->type == CPP_COMMA)
11466 /* will be consumed next time around */;
11467 /* If it's a `;', we are done. */
11468 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11469 break;
11470 /* Anything else is an error. */
11471 else
11473 /* If we have already issued an error message we don't need
11474 to issue another one. */
11475 if (decl != error_mark_node
11476 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11477 cp_parser_error (parser, "expected %<,%> or %<;%>");
11478 /* Skip tokens until we reach the end of the statement. */
11479 cp_parser_skip_to_end_of_statement (parser);
11480 /* If the next token is now a `;', consume it. */
11481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11482 cp_lexer_consume_token (parser->lexer);
11483 goto done;
11485 /* After the first time around, a function-definition is not
11486 allowed -- even if it was OK at first. For example:
11488 int i, f() {}
11490 is not valid. */
11491 function_definition_allowed_p = false;
11494 /* Issue an error message if no declarators are present, and the
11495 decl-specifier-seq does not itself declare a class or
11496 enumeration: [dcl.dcl]/3. */
11497 if (!saw_declarator)
11499 if (cp_parser_declares_only_class_p (parser))
11501 if (!declares_class_or_enum
11502 && decl_specifiers.type
11503 && OVERLOAD_TYPE_P (decl_specifiers.type))
11504 /* Ensure an error is issued anyway when finish_decltype_type,
11505 called via cp_parser_decl_specifier_seq, returns a class or
11506 an enumeration (c++/51786). */
11507 decl_specifiers.type = NULL_TREE;
11508 shadow_tag (&decl_specifiers);
11510 /* Perform any deferred access checks. */
11511 perform_deferred_access_checks (tf_warning_or_error);
11514 /* Consume the `;'. */
11515 if (!maybe_range_for_decl)
11516 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11518 done:
11519 pop_deferring_access_checks ();
11522 /* Parse a decl-specifier-seq.
11524 decl-specifier-seq:
11525 decl-specifier-seq [opt] decl-specifier
11526 decl-specifier attribute-specifier-seq [opt] (C++11)
11528 decl-specifier:
11529 storage-class-specifier
11530 type-specifier
11531 function-specifier
11532 friend
11533 typedef
11535 GNU Extension:
11537 decl-specifier:
11538 attributes
11540 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11542 The parser flags FLAGS is used to control type-specifier parsing.
11544 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11545 flags:
11547 1: one of the decl-specifiers is an elaborated-type-specifier
11548 (i.e., a type declaration)
11549 2: one of the decl-specifiers is an enum-specifier or a
11550 class-specifier (i.e., a type definition)
11554 static void
11555 cp_parser_decl_specifier_seq (cp_parser* parser,
11556 cp_parser_flags flags,
11557 cp_decl_specifier_seq *decl_specs,
11558 int* declares_class_or_enum)
11560 bool constructor_possible_p = !parser->in_declarator_p;
11561 bool found_decl_spec = false;
11562 cp_token *start_token = NULL;
11563 cp_decl_spec ds;
11565 /* Clear DECL_SPECS. */
11566 clear_decl_specs (decl_specs);
11568 /* Assume no class or enumeration type is declared. */
11569 *declares_class_or_enum = 0;
11571 /* Keep reading specifiers until there are no more to read. */
11572 while (true)
11574 bool constructor_p;
11575 cp_token *token;
11576 ds = ds_last;
11578 /* Peek at the next token. */
11579 token = cp_lexer_peek_token (parser->lexer);
11581 /* Save the first token of the decl spec list for error
11582 reporting. */
11583 if (!start_token)
11584 start_token = token;
11585 /* Handle attributes. */
11586 if (cp_next_tokens_can_be_attribute_p (parser))
11588 /* Parse the attributes. */
11589 tree attrs = cp_parser_attributes_opt (parser);
11591 /* In a sequence of declaration specifiers, c++11 attributes
11592 appertain to the type that precede them. In that case
11593 [dcl.spec]/1 says:
11595 The attribute-specifier-seq affects the type only for
11596 the declaration it appears in, not other declarations
11597 involving the same type.
11599 But for now let's force the user to position the
11600 attribute either at the beginning of the declaration or
11601 after the declarator-id, which would clearly mean that it
11602 applies to the declarator. */
11603 if (cxx11_attribute_p (attrs))
11605 if (!found_decl_spec)
11606 /* The c++11 attribute is at the beginning of the
11607 declaration. It appertains to the entity being
11608 declared. */;
11609 else
11611 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11613 /* This is an attribute following a
11614 class-specifier. */
11615 if (decl_specs->type_definition_p)
11616 warn_misplaced_attr_for_class_type (token->location,
11617 decl_specs->type);
11618 attrs = NULL_TREE;
11620 else
11622 decl_specs->std_attributes
11623 = chainon (decl_specs->std_attributes,
11624 attrs);
11625 if (decl_specs->locations[ds_std_attribute] == 0)
11626 decl_specs->locations[ds_std_attribute] = token->location;
11628 continue;
11632 decl_specs->attributes
11633 = chainon (decl_specs->attributes,
11634 attrs);
11635 if (decl_specs->locations[ds_attribute] == 0)
11636 decl_specs->locations[ds_attribute] = token->location;
11637 continue;
11639 /* Assume we will find a decl-specifier keyword. */
11640 found_decl_spec = true;
11641 /* If the next token is an appropriate keyword, we can simply
11642 add it to the list. */
11643 switch (token->keyword)
11645 /* decl-specifier:
11646 friend
11647 constexpr */
11648 case RID_FRIEND:
11649 if (!at_class_scope_p ())
11651 error_at (token->location, "%<friend%> used outside of class");
11652 cp_lexer_purge_token (parser->lexer);
11654 else
11656 ds = ds_friend;
11657 /* Consume the token. */
11658 cp_lexer_consume_token (parser->lexer);
11660 break;
11662 case RID_CONSTEXPR:
11663 ds = ds_constexpr;
11664 cp_lexer_consume_token (parser->lexer);
11665 break;
11667 /* function-specifier:
11668 inline
11669 virtual
11670 explicit */
11671 case RID_INLINE:
11672 case RID_VIRTUAL:
11673 case RID_EXPLICIT:
11674 cp_parser_function_specifier_opt (parser, decl_specs);
11675 break;
11677 /* decl-specifier:
11678 typedef */
11679 case RID_TYPEDEF:
11680 ds = ds_typedef;
11681 /* Consume the token. */
11682 cp_lexer_consume_token (parser->lexer);
11683 /* A constructor declarator cannot appear in a typedef. */
11684 constructor_possible_p = false;
11685 /* The "typedef" keyword can only occur in a declaration; we
11686 may as well commit at this point. */
11687 cp_parser_commit_to_tentative_parse (parser);
11689 if (decl_specs->storage_class != sc_none)
11690 decl_specs->conflicting_specifiers_p = true;
11691 break;
11693 /* storage-class-specifier:
11694 auto
11695 register
11696 static
11697 extern
11698 mutable
11700 GNU Extension:
11701 thread */
11702 case RID_AUTO:
11703 if (cxx_dialect == cxx98)
11705 /* Consume the token. */
11706 cp_lexer_consume_token (parser->lexer);
11708 /* Complain about `auto' as a storage specifier, if
11709 we're complaining about C++0x compatibility. */
11710 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11711 " changes meaning in C++11; please remove it");
11713 /* Set the storage class anyway. */
11714 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11715 token);
11717 else
11718 /* C++0x auto type-specifier. */
11719 found_decl_spec = false;
11720 break;
11722 case RID_REGISTER:
11723 case RID_STATIC:
11724 case RID_EXTERN:
11725 case RID_MUTABLE:
11726 /* Consume the token. */
11727 cp_lexer_consume_token (parser->lexer);
11728 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11729 token);
11730 break;
11731 case RID_THREAD:
11732 /* Consume the token. */
11733 ds = ds_thread;
11734 cp_lexer_consume_token (parser->lexer);
11735 break;
11737 default:
11738 /* We did not yet find a decl-specifier yet. */
11739 found_decl_spec = false;
11740 break;
11743 if (found_decl_spec
11744 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11745 && token->keyword != RID_CONSTEXPR)
11746 error ("decl-specifier invalid in condition");
11748 if (ds != ds_last)
11749 set_and_check_decl_spec_loc (decl_specs, ds, token);
11751 /* Constructors are a special case. The `S' in `S()' is not a
11752 decl-specifier; it is the beginning of the declarator. */
11753 constructor_p
11754 = (!found_decl_spec
11755 && constructor_possible_p
11756 && (cp_parser_constructor_declarator_p
11757 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11759 /* If we don't have a DECL_SPEC yet, then we must be looking at
11760 a type-specifier. */
11761 if (!found_decl_spec && !constructor_p)
11763 int decl_spec_declares_class_or_enum;
11764 bool is_cv_qualifier;
11765 tree type_spec;
11767 type_spec
11768 = cp_parser_type_specifier (parser, flags,
11769 decl_specs,
11770 /*is_declaration=*/true,
11771 &decl_spec_declares_class_or_enum,
11772 &is_cv_qualifier);
11773 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11775 /* If this type-specifier referenced a user-defined type
11776 (a typedef, class-name, etc.), then we can't allow any
11777 more such type-specifiers henceforth.
11779 [dcl.spec]
11781 The longest sequence of decl-specifiers that could
11782 possibly be a type name is taken as the
11783 decl-specifier-seq of a declaration. The sequence shall
11784 be self-consistent as described below.
11786 [dcl.type]
11788 As a general rule, at most one type-specifier is allowed
11789 in the complete decl-specifier-seq of a declaration. The
11790 only exceptions are the following:
11792 -- const or volatile can be combined with any other
11793 type-specifier.
11795 -- signed or unsigned can be combined with char, long,
11796 short, or int.
11798 -- ..
11800 Example:
11802 typedef char* Pc;
11803 void g (const int Pc);
11805 Here, Pc is *not* part of the decl-specifier seq; it's
11806 the declarator. Therefore, once we see a type-specifier
11807 (other than a cv-qualifier), we forbid any additional
11808 user-defined types. We *do* still allow things like `int
11809 int' to be considered a decl-specifier-seq, and issue the
11810 error message later. */
11811 if (type_spec && !is_cv_qualifier)
11812 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11813 /* A constructor declarator cannot follow a type-specifier. */
11814 if (type_spec)
11816 constructor_possible_p = false;
11817 found_decl_spec = true;
11818 if (!is_cv_qualifier)
11819 decl_specs->any_type_specifiers_p = true;
11823 /* If we still do not have a DECL_SPEC, then there are no more
11824 decl-specifiers. */
11825 if (!found_decl_spec)
11826 break;
11828 decl_specs->any_specifiers_p = true;
11829 /* After we see one decl-specifier, further decl-specifiers are
11830 always optional. */
11831 flags |= CP_PARSER_FLAGS_OPTIONAL;
11834 /* Don't allow a friend specifier with a class definition. */
11835 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11836 && (*declares_class_or_enum & 2))
11837 error_at (decl_specs->locations[ds_friend],
11838 "class definition may not be declared a friend");
11841 /* Parse an (optional) storage-class-specifier.
11843 storage-class-specifier:
11844 auto
11845 register
11846 static
11847 extern
11848 mutable
11850 GNU Extension:
11852 storage-class-specifier:
11853 thread
11855 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11857 static tree
11858 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11860 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11862 case RID_AUTO:
11863 if (cxx_dialect != cxx98)
11864 return NULL_TREE;
11865 /* Fall through for C++98. */
11867 case RID_REGISTER:
11868 case RID_STATIC:
11869 case RID_EXTERN:
11870 case RID_MUTABLE:
11871 case RID_THREAD:
11872 /* Consume the token. */
11873 return cp_lexer_consume_token (parser->lexer)->u.value;
11875 default:
11876 return NULL_TREE;
11880 /* Parse an (optional) function-specifier.
11882 function-specifier:
11883 inline
11884 virtual
11885 explicit
11887 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11888 Updates DECL_SPECS, if it is non-NULL. */
11890 static tree
11891 cp_parser_function_specifier_opt (cp_parser* parser,
11892 cp_decl_specifier_seq *decl_specs)
11894 cp_token *token = cp_lexer_peek_token (parser->lexer);
11895 switch (token->keyword)
11897 case RID_INLINE:
11898 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11899 break;
11901 case RID_VIRTUAL:
11902 /* 14.5.2.3 [temp.mem]
11904 A member function template shall not be virtual. */
11905 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11906 error_at (token->location, "templates may not be %<virtual%>");
11907 else
11908 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11909 break;
11911 case RID_EXPLICIT:
11912 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11913 break;
11915 default:
11916 return NULL_TREE;
11919 /* Consume the token. */
11920 return cp_lexer_consume_token (parser->lexer)->u.value;
11923 /* Parse a linkage-specification.
11925 linkage-specification:
11926 extern string-literal { declaration-seq [opt] }
11927 extern string-literal declaration */
11929 static void
11930 cp_parser_linkage_specification (cp_parser* parser)
11932 tree linkage;
11934 /* Look for the `extern' keyword. */
11935 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11937 /* Look for the string-literal. */
11938 linkage = cp_parser_string_literal (parser, false, false);
11940 /* Transform the literal into an identifier. If the literal is a
11941 wide-character string, or contains embedded NULs, then we can't
11942 handle it as the user wants. */
11943 if (strlen (TREE_STRING_POINTER (linkage))
11944 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11946 cp_parser_error (parser, "invalid linkage-specification");
11947 /* Assume C++ linkage. */
11948 linkage = lang_name_cplusplus;
11950 else
11951 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11953 /* We're now using the new linkage. */
11954 push_lang_context (linkage);
11956 /* If the next token is a `{', then we're using the first
11957 production. */
11958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11960 cp_ensure_no_omp_declare_simd (parser);
11962 /* Consume the `{' token. */
11963 cp_lexer_consume_token (parser->lexer);
11964 /* Parse the declarations. */
11965 cp_parser_declaration_seq_opt (parser);
11966 /* Look for the closing `}'. */
11967 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11969 /* Otherwise, there's just one declaration. */
11970 else
11972 bool saved_in_unbraced_linkage_specification_p;
11974 saved_in_unbraced_linkage_specification_p
11975 = parser->in_unbraced_linkage_specification_p;
11976 parser->in_unbraced_linkage_specification_p = true;
11977 cp_parser_declaration (parser);
11978 parser->in_unbraced_linkage_specification_p
11979 = saved_in_unbraced_linkage_specification_p;
11982 /* We're done with the linkage-specification. */
11983 pop_lang_context ();
11986 /* Parse a static_assert-declaration.
11988 static_assert-declaration:
11989 static_assert ( constant-expression , string-literal ) ;
11991 If MEMBER_P, this static_assert is a class member. */
11993 static void
11994 cp_parser_static_assert(cp_parser *parser, bool member_p)
11996 tree condition;
11997 tree message;
11998 cp_token *token;
11999 location_t saved_loc;
12000 bool dummy;
12002 /* Peek at the `static_assert' token so we can keep track of exactly
12003 where the static assertion started. */
12004 token = cp_lexer_peek_token (parser->lexer);
12005 saved_loc = token->location;
12007 /* Look for the `static_assert' keyword. */
12008 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12009 RT_STATIC_ASSERT))
12010 return;
12012 /* We know we are in a static assertion; commit to any tentative
12013 parse. */
12014 if (cp_parser_parsing_tentatively (parser))
12015 cp_parser_commit_to_tentative_parse (parser);
12017 /* Parse the `(' starting the static assertion condition. */
12018 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12020 /* Parse the constant-expression. Allow a non-constant expression
12021 here in order to give better diagnostics in finish_static_assert. */
12022 condition =
12023 cp_parser_constant_expression (parser,
12024 /*allow_non_constant_p=*/true,
12025 /*non_constant_p=*/&dummy);
12027 /* Parse the separating `,'. */
12028 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12030 /* Parse the string-literal message. */
12031 message = cp_parser_string_literal (parser,
12032 /*translate=*/false,
12033 /*wide_ok=*/true);
12035 /* A `)' completes the static assertion. */
12036 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12037 cp_parser_skip_to_closing_parenthesis (parser,
12038 /*recovering=*/true,
12039 /*or_comma=*/false,
12040 /*consume_paren=*/true);
12042 /* A semicolon terminates the declaration. */
12043 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12045 /* Complete the static assertion, which may mean either processing
12046 the static assert now or saving it for template instantiation. */
12047 finish_static_assert (condition, message, saved_loc, member_p);
12050 /* Parse the expression in decltype ( expression ). */
12052 static tree
12053 cp_parser_decltype_expr (cp_parser *parser,
12054 bool &id_expression_or_member_access_p)
12056 cp_token *id_expr_start_token;
12057 tree expr;
12059 /* First, try parsing an id-expression. */
12060 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12061 cp_parser_parse_tentatively (parser);
12062 expr = cp_parser_id_expression (parser,
12063 /*template_keyword_p=*/false,
12064 /*check_dependency_p=*/true,
12065 /*template_p=*/NULL,
12066 /*declarator_p=*/false,
12067 /*optional_p=*/false);
12069 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12071 bool non_integral_constant_expression_p = false;
12072 tree id_expression = expr;
12073 cp_id_kind idk;
12074 const char *error_msg;
12076 if (identifier_p (expr))
12077 /* Lookup the name we got back from the id-expression. */
12078 expr = cp_parser_lookup_name_simple (parser, expr,
12079 id_expr_start_token->location);
12081 if (expr
12082 && expr != error_mark_node
12083 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12084 && TREE_CODE (expr) != TYPE_DECL
12085 && (TREE_CODE (expr) != BIT_NOT_EXPR
12086 || !TYPE_P (TREE_OPERAND (expr, 0)))
12087 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12089 /* Complete lookup of the id-expression. */
12090 expr = (finish_id_expression
12091 (id_expression, expr, parser->scope, &idk,
12092 /*integral_constant_expression_p=*/false,
12093 /*allow_non_integral_constant_expression_p=*/true,
12094 &non_integral_constant_expression_p,
12095 /*template_p=*/false,
12096 /*done=*/true,
12097 /*address_p=*/false,
12098 /*template_arg_p=*/false,
12099 &error_msg,
12100 id_expr_start_token->location));
12102 if (expr == error_mark_node)
12103 /* We found an id-expression, but it was something that we
12104 should not have found. This is an error, not something
12105 we can recover from, so note that we found an
12106 id-expression and we'll recover as gracefully as
12107 possible. */
12108 id_expression_or_member_access_p = true;
12111 if (expr
12112 && expr != error_mark_node
12113 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12114 /* We have an id-expression. */
12115 id_expression_or_member_access_p = true;
12118 if (!id_expression_or_member_access_p)
12120 /* Abort the id-expression parse. */
12121 cp_parser_abort_tentative_parse (parser);
12123 /* Parsing tentatively, again. */
12124 cp_parser_parse_tentatively (parser);
12126 /* Parse a class member access. */
12127 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12128 /*cast_p=*/false, /*decltype*/true,
12129 /*member_access_only_p=*/true, NULL);
12131 if (expr
12132 && expr != error_mark_node
12133 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12134 /* We have an id-expression. */
12135 id_expression_or_member_access_p = true;
12138 if (id_expression_or_member_access_p)
12139 /* We have parsed the complete id-expression or member access. */
12140 cp_parser_parse_definitely (parser);
12141 else
12143 /* Abort our attempt to parse an id-expression or member access
12144 expression. */
12145 cp_parser_abort_tentative_parse (parser);
12147 /* Parse a full expression. */
12148 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12149 /*decltype_p=*/true);
12152 return expr;
12155 /* Parse a `decltype' type. Returns the type.
12157 simple-type-specifier:
12158 decltype ( expression )
12159 C++14 proposal:
12160 decltype ( auto ) */
12162 static tree
12163 cp_parser_decltype (cp_parser *parser)
12165 tree expr;
12166 bool id_expression_or_member_access_p = false;
12167 const char *saved_message;
12168 bool saved_integral_constant_expression_p;
12169 bool saved_non_integral_constant_expression_p;
12170 bool saved_greater_than_is_operator_p;
12171 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12173 if (start_token->type == CPP_DECLTYPE)
12175 /* Already parsed. */
12176 cp_lexer_consume_token (parser->lexer);
12177 return start_token->u.value;
12180 /* Look for the `decltype' token. */
12181 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12182 return error_mark_node;
12184 /* Parse the opening `('. */
12185 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12186 return error_mark_node;
12188 /* decltype (auto) */
12189 if (cxx_dialect >= cxx14
12190 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12192 cp_lexer_consume_token (parser->lexer);
12193 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12194 return error_mark_node;
12195 expr = make_decltype_auto ();
12196 AUTO_IS_DECLTYPE (expr) = true;
12197 goto rewrite;
12200 /* Types cannot be defined in a `decltype' expression. Save away the
12201 old message. */
12202 saved_message = parser->type_definition_forbidden_message;
12204 /* And create the new one. */
12205 parser->type_definition_forbidden_message
12206 = G_("types may not be defined in %<decltype%> expressions");
12208 /* The restrictions on constant-expressions do not apply inside
12209 decltype expressions. */
12210 saved_integral_constant_expression_p
12211 = parser->integral_constant_expression_p;
12212 saved_non_integral_constant_expression_p
12213 = parser->non_integral_constant_expression_p;
12214 parser->integral_constant_expression_p = false;
12216 /* Within a parenthesized expression, a `>' token is always
12217 the greater-than operator. */
12218 saved_greater_than_is_operator_p
12219 = parser->greater_than_is_operator_p;
12220 parser->greater_than_is_operator_p = true;
12222 /* Do not actually evaluate the expression. */
12223 ++cp_unevaluated_operand;
12225 /* Do not warn about problems with the expression. */
12226 ++c_inhibit_evaluation_warnings;
12228 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12230 /* Go back to evaluating expressions. */
12231 --cp_unevaluated_operand;
12232 --c_inhibit_evaluation_warnings;
12234 /* The `>' token might be the end of a template-id or
12235 template-parameter-list now. */
12236 parser->greater_than_is_operator_p
12237 = saved_greater_than_is_operator_p;
12239 /* Restore the old message and the integral constant expression
12240 flags. */
12241 parser->type_definition_forbidden_message = saved_message;
12242 parser->integral_constant_expression_p
12243 = saved_integral_constant_expression_p;
12244 parser->non_integral_constant_expression_p
12245 = saved_non_integral_constant_expression_p;
12247 /* Parse to the closing `)'. */
12248 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12250 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12251 /*consume_paren=*/true);
12252 return error_mark_node;
12255 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12256 tf_warning_or_error);
12258 rewrite:
12259 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12260 it again. */
12261 start_token->type = CPP_DECLTYPE;
12262 start_token->u.value = expr;
12263 start_token->keyword = RID_MAX;
12264 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12266 return expr;
12269 /* Special member functions [gram.special] */
12271 /* Parse a conversion-function-id.
12273 conversion-function-id:
12274 operator conversion-type-id
12276 Returns an IDENTIFIER_NODE representing the operator. */
12278 static tree
12279 cp_parser_conversion_function_id (cp_parser* parser)
12281 tree type;
12282 tree saved_scope;
12283 tree saved_qualifying_scope;
12284 tree saved_object_scope;
12285 tree pushed_scope = NULL_TREE;
12287 /* Look for the `operator' token. */
12288 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12289 return error_mark_node;
12290 /* When we parse the conversion-type-id, the current scope will be
12291 reset. However, we need that information in able to look up the
12292 conversion function later, so we save it here. */
12293 saved_scope = parser->scope;
12294 saved_qualifying_scope = parser->qualifying_scope;
12295 saved_object_scope = parser->object_scope;
12296 /* We must enter the scope of the class so that the names of
12297 entities declared within the class are available in the
12298 conversion-type-id. For example, consider:
12300 struct S {
12301 typedef int I;
12302 operator I();
12305 S::operator I() { ... }
12307 In order to see that `I' is a type-name in the definition, we
12308 must be in the scope of `S'. */
12309 if (saved_scope)
12310 pushed_scope = push_scope (saved_scope);
12311 /* Parse the conversion-type-id. */
12312 type = cp_parser_conversion_type_id (parser);
12313 /* Leave the scope of the class, if any. */
12314 if (pushed_scope)
12315 pop_scope (pushed_scope);
12316 /* Restore the saved scope. */
12317 parser->scope = saved_scope;
12318 parser->qualifying_scope = saved_qualifying_scope;
12319 parser->object_scope = saved_object_scope;
12320 /* If the TYPE is invalid, indicate failure. */
12321 if (type == error_mark_node)
12322 return error_mark_node;
12323 return mangle_conv_op_name_for_type (type);
12326 /* Parse a conversion-type-id:
12328 conversion-type-id:
12329 type-specifier-seq conversion-declarator [opt]
12331 Returns the TYPE specified. */
12333 static tree
12334 cp_parser_conversion_type_id (cp_parser* parser)
12336 tree attributes;
12337 cp_decl_specifier_seq type_specifiers;
12338 cp_declarator *declarator;
12339 tree type_specified;
12340 const char *saved_message;
12342 /* Parse the attributes. */
12343 attributes = cp_parser_attributes_opt (parser);
12345 saved_message = parser->type_definition_forbidden_message;
12346 parser->type_definition_forbidden_message
12347 = G_("types may not be defined in a conversion-type-id");
12349 /* Parse the type-specifiers. */
12350 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12351 /*is_trailing_return=*/false,
12352 &type_specifiers);
12354 parser->type_definition_forbidden_message = saved_message;
12356 /* If that didn't work, stop. */
12357 if (type_specifiers.type == error_mark_node)
12358 return error_mark_node;
12359 /* Parse the conversion-declarator. */
12360 declarator = cp_parser_conversion_declarator_opt (parser);
12362 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12363 /*initialized=*/0, &attributes);
12364 if (attributes)
12365 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12367 /* Don't give this error when parsing tentatively. This happens to
12368 work because we always parse this definitively once. */
12369 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12370 && type_uses_auto (type_specified))
12372 if (cxx_dialect < cxx14)
12374 error ("invalid use of %<auto%> in conversion operator");
12375 return error_mark_node;
12377 else if (template_parm_scope_p ())
12378 warning (0, "use of %<auto%> in member template "
12379 "conversion operator can never be deduced");
12382 return type_specified;
12385 /* Parse an (optional) conversion-declarator.
12387 conversion-declarator:
12388 ptr-operator conversion-declarator [opt]
12392 static cp_declarator *
12393 cp_parser_conversion_declarator_opt (cp_parser* parser)
12395 enum tree_code code;
12396 tree class_type, std_attributes = NULL_TREE;
12397 cp_cv_quals cv_quals;
12399 /* We don't know if there's a ptr-operator next, or not. */
12400 cp_parser_parse_tentatively (parser);
12401 /* Try the ptr-operator. */
12402 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12403 &std_attributes);
12404 /* If it worked, look for more conversion-declarators. */
12405 if (cp_parser_parse_definitely (parser))
12407 cp_declarator *declarator;
12409 /* Parse another optional declarator. */
12410 declarator = cp_parser_conversion_declarator_opt (parser);
12412 declarator = cp_parser_make_indirect_declarator
12413 (code, class_type, cv_quals, declarator, std_attributes);
12415 return declarator;
12418 return NULL;
12421 /* Parse an (optional) ctor-initializer.
12423 ctor-initializer:
12424 : mem-initializer-list
12426 Returns TRUE iff the ctor-initializer was actually present. */
12428 static bool
12429 cp_parser_ctor_initializer_opt (cp_parser* parser)
12431 /* If the next token is not a `:', then there is no
12432 ctor-initializer. */
12433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12435 /* Do default initialization of any bases and members. */
12436 if (DECL_CONSTRUCTOR_P (current_function_decl))
12437 finish_mem_initializers (NULL_TREE);
12439 return false;
12442 /* Consume the `:' token. */
12443 cp_lexer_consume_token (parser->lexer);
12444 /* And the mem-initializer-list. */
12445 cp_parser_mem_initializer_list (parser);
12447 return true;
12450 /* Parse a mem-initializer-list.
12452 mem-initializer-list:
12453 mem-initializer ... [opt]
12454 mem-initializer ... [opt] , mem-initializer-list */
12456 static void
12457 cp_parser_mem_initializer_list (cp_parser* parser)
12459 tree mem_initializer_list = NULL_TREE;
12460 tree target_ctor = error_mark_node;
12461 cp_token *token = cp_lexer_peek_token (parser->lexer);
12463 /* Let the semantic analysis code know that we are starting the
12464 mem-initializer-list. */
12465 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12466 error_at (token->location,
12467 "only constructors take member initializers");
12469 /* Loop through the list. */
12470 while (true)
12472 tree mem_initializer;
12474 token = cp_lexer_peek_token (parser->lexer);
12475 /* Parse the mem-initializer. */
12476 mem_initializer = cp_parser_mem_initializer (parser);
12477 /* If the next token is a `...', we're expanding member initializers. */
12478 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12480 /* Consume the `...'. */
12481 cp_lexer_consume_token (parser->lexer);
12483 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12484 can be expanded but members cannot. */
12485 if (mem_initializer != error_mark_node
12486 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12488 error_at (token->location,
12489 "cannot expand initializer for member %<%D%>",
12490 TREE_PURPOSE (mem_initializer));
12491 mem_initializer = error_mark_node;
12494 /* Construct the pack expansion type. */
12495 if (mem_initializer != error_mark_node)
12496 mem_initializer = make_pack_expansion (mem_initializer);
12498 if (target_ctor != error_mark_node
12499 && mem_initializer != error_mark_node)
12501 error ("mem-initializer for %qD follows constructor delegation",
12502 TREE_PURPOSE (mem_initializer));
12503 mem_initializer = error_mark_node;
12505 /* Look for a target constructor. */
12506 if (mem_initializer != error_mark_node
12507 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12508 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12510 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12511 if (mem_initializer_list)
12513 error ("constructor delegation follows mem-initializer for %qD",
12514 TREE_PURPOSE (mem_initializer_list));
12515 mem_initializer = error_mark_node;
12517 target_ctor = mem_initializer;
12519 /* Add it to the list, unless it was erroneous. */
12520 if (mem_initializer != error_mark_node)
12522 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12523 mem_initializer_list = mem_initializer;
12525 /* If the next token is not a `,', we're done. */
12526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12527 break;
12528 /* Consume the `,' token. */
12529 cp_lexer_consume_token (parser->lexer);
12532 /* Perform semantic analysis. */
12533 if (DECL_CONSTRUCTOR_P (current_function_decl))
12534 finish_mem_initializers (mem_initializer_list);
12537 /* Parse a mem-initializer.
12539 mem-initializer:
12540 mem-initializer-id ( expression-list [opt] )
12541 mem-initializer-id braced-init-list
12543 GNU extension:
12545 mem-initializer:
12546 ( expression-list [opt] )
12548 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12549 class) or FIELD_DECL (for a non-static data member) to initialize;
12550 the TREE_VALUE is the expression-list. An empty initialization
12551 list is represented by void_list_node. */
12553 static tree
12554 cp_parser_mem_initializer (cp_parser* parser)
12556 tree mem_initializer_id;
12557 tree expression_list;
12558 tree member;
12559 cp_token *token = cp_lexer_peek_token (parser->lexer);
12561 /* Find out what is being initialized. */
12562 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12564 permerror (token->location,
12565 "anachronistic old-style base class initializer");
12566 mem_initializer_id = NULL_TREE;
12568 else
12570 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12571 if (mem_initializer_id == error_mark_node)
12572 return mem_initializer_id;
12574 member = expand_member_init (mem_initializer_id);
12575 if (member && !DECL_P (member))
12576 in_base_initializer = 1;
12578 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12580 bool expr_non_constant_p;
12581 cp_lexer_set_source_position (parser->lexer);
12582 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12583 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12584 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12585 expression_list = build_tree_list (NULL_TREE, expression_list);
12587 else
12589 vec<tree, va_gc> *vec;
12590 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12591 /*cast_p=*/false,
12592 /*allow_expansion_p=*/true,
12593 /*non_constant_p=*/NULL);
12594 if (vec == NULL)
12595 return error_mark_node;
12596 expression_list = build_tree_list_vec (vec);
12597 release_tree_vector (vec);
12600 if (expression_list == error_mark_node)
12601 return error_mark_node;
12602 if (!expression_list)
12603 expression_list = void_type_node;
12605 in_base_initializer = 0;
12607 return member ? build_tree_list (member, expression_list) : error_mark_node;
12610 /* Parse a mem-initializer-id.
12612 mem-initializer-id:
12613 :: [opt] nested-name-specifier [opt] class-name
12614 identifier
12616 Returns a TYPE indicating the class to be initializer for the first
12617 production. Returns an IDENTIFIER_NODE indicating the data member
12618 to be initialized for the second production. */
12620 static tree
12621 cp_parser_mem_initializer_id (cp_parser* parser)
12623 bool global_scope_p;
12624 bool nested_name_specifier_p;
12625 bool template_p = false;
12626 tree id;
12628 cp_token *token = cp_lexer_peek_token (parser->lexer);
12630 /* `typename' is not allowed in this context ([temp.res]). */
12631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12633 error_at (token->location,
12634 "keyword %<typename%> not allowed in this context (a qualified "
12635 "member initializer is implicitly a type)");
12636 cp_lexer_consume_token (parser->lexer);
12638 /* Look for the optional `::' operator. */
12639 global_scope_p
12640 = (cp_parser_global_scope_opt (parser,
12641 /*current_scope_valid_p=*/false)
12642 != NULL_TREE);
12643 /* Look for the optional nested-name-specifier. The simplest way to
12644 implement:
12646 [temp.res]
12648 The keyword `typename' is not permitted in a base-specifier or
12649 mem-initializer; in these contexts a qualified name that
12650 depends on a template-parameter is implicitly assumed to be a
12651 type name.
12653 is to assume that we have seen the `typename' keyword at this
12654 point. */
12655 nested_name_specifier_p
12656 = (cp_parser_nested_name_specifier_opt (parser,
12657 /*typename_keyword_p=*/true,
12658 /*check_dependency_p=*/true,
12659 /*type_p=*/true,
12660 /*is_declaration=*/true)
12661 != NULL_TREE);
12662 if (nested_name_specifier_p)
12663 template_p = cp_parser_optional_template_keyword (parser);
12664 /* If there is a `::' operator or a nested-name-specifier, then we
12665 are definitely looking for a class-name. */
12666 if (global_scope_p || nested_name_specifier_p)
12667 return cp_parser_class_name (parser,
12668 /*typename_keyword_p=*/true,
12669 /*template_keyword_p=*/template_p,
12670 typename_type,
12671 /*check_dependency_p=*/true,
12672 /*class_head_p=*/false,
12673 /*is_declaration=*/true);
12674 /* Otherwise, we could also be looking for an ordinary identifier. */
12675 cp_parser_parse_tentatively (parser);
12676 /* Try a class-name. */
12677 id = cp_parser_class_name (parser,
12678 /*typename_keyword_p=*/true,
12679 /*template_keyword_p=*/false,
12680 none_type,
12681 /*check_dependency_p=*/true,
12682 /*class_head_p=*/false,
12683 /*is_declaration=*/true);
12684 /* If we found one, we're done. */
12685 if (cp_parser_parse_definitely (parser))
12686 return id;
12687 /* Otherwise, look for an ordinary identifier. */
12688 return cp_parser_identifier (parser);
12691 /* Overloading [gram.over] */
12693 /* Parse an operator-function-id.
12695 operator-function-id:
12696 operator operator
12698 Returns an IDENTIFIER_NODE for the operator which is a
12699 human-readable spelling of the identifier, e.g., `operator +'. */
12701 static tree
12702 cp_parser_operator_function_id (cp_parser* parser)
12704 /* Look for the `operator' keyword. */
12705 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12706 return error_mark_node;
12707 /* And then the name of the operator itself. */
12708 return cp_parser_operator (parser);
12711 /* Return an identifier node for a user-defined literal operator.
12712 The suffix identifier is chained to the operator name identifier. */
12714 static tree
12715 cp_literal_operator_id (const char* name)
12717 tree identifier;
12718 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12719 + strlen (name) + 10);
12720 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12721 identifier = get_identifier (buffer);
12723 return identifier;
12726 /* Parse an operator.
12728 operator:
12729 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12730 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12731 || ++ -- , ->* -> () []
12733 GNU Extensions:
12735 operator:
12736 <? >? <?= >?=
12738 Returns an IDENTIFIER_NODE for the operator which is a
12739 human-readable spelling of the identifier, e.g., `operator +'. */
12741 static tree
12742 cp_parser_operator (cp_parser* parser)
12744 tree id = NULL_TREE;
12745 cp_token *token;
12746 bool utf8 = false;
12748 /* Peek at the next token. */
12749 token = cp_lexer_peek_token (parser->lexer);
12750 /* Figure out which operator we have. */
12751 switch (token->type)
12753 case CPP_KEYWORD:
12755 enum tree_code op;
12757 /* The keyword should be either `new' or `delete'. */
12758 if (token->keyword == RID_NEW)
12759 op = NEW_EXPR;
12760 else if (token->keyword == RID_DELETE)
12761 op = DELETE_EXPR;
12762 else
12763 break;
12765 /* Consume the `new' or `delete' token. */
12766 cp_lexer_consume_token (parser->lexer);
12768 /* Peek at the next token. */
12769 token = cp_lexer_peek_token (parser->lexer);
12770 /* If it's a `[' token then this is the array variant of the
12771 operator. */
12772 if (token->type == CPP_OPEN_SQUARE)
12774 /* Consume the `[' token. */
12775 cp_lexer_consume_token (parser->lexer);
12776 /* Look for the `]' token. */
12777 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12778 id = ansi_opname (op == NEW_EXPR
12779 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12781 /* Otherwise, we have the non-array variant. */
12782 else
12783 id = ansi_opname (op);
12785 return id;
12788 case CPP_PLUS:
12789 id = ansi_opname (PLUS_EXPR);
12790 break;
12792 case CPP_MINUS:
12793 id = ansi_opname (MINUS_EXPR);
12794 break;
12796 case CPP_MULT:
12797 id = ansi_opname (MULT_EXPR);
12798 break;
12800 case CPP_DIV:
12801 id = ansi_opname (TRUNC_DIV_EXPR);
12802 break;
12804 case CPP_MOD:
12805 id = ansi_opname (TRUNC_MOD_EXPR);
12806 break;
12808 case CPP_XOR:
12809 id = ansi_opname (BIT_XOR_EXPR);
12810 break;
12812 case CPP_AND:
12813 id = ansi_opname (BIT_AND_EXPR);
12814 break;
12816 case CPP_OR:
12817 id = ansi_opname (BIT_IOR_EXPR);
12818 break;
12820 case CPP_COMPL:
12821 id = ansi_opname (BIT_NOT_EXPR);
12822 break;
12824 case CPP_NOT:
12825 id = ansi_opname (TRUTH_NOT_EXPR);
12826 break;
12828 case CPP_EQ:
12829 id = ansi_assopname (NOP_EXPR);
12830 break;
12832 case CPP_LESS:
12833 id = ansi_opname (LT_EXPR);
12834 break;
12836 case CPP_GREATER:
12837 id = ansi_opname (GT_EXPR);
12838 break;
12840 case CPP_PLUS_EQ:
12841 id = ansi_assopname (PLUS_EXPR);
12842 break;
12844 case CPP_MINUS_EQ:
12845 id = ansi_assopname (MINUS_EXPR);
12846 break;
12848 case CPP_MULT_EQ:
12849 id = ansi_assopname (MULT_EXPR);
12850 break;
12852 case CPP_DIV_EQ:
12853 id = ansi_assopname (TRUNC_DIV_EXPR);
12854 break;
12856 case CPP_MOD_EQ:
12857 id = ansi_assopname (TRUNC_MOD_EXPR);
12858 break;
12860 case CPP_XOR_EQ:
12861 id = ansi_assopname (BIT_XOR_EXPR);
12862 break;
12864 case CPP_AND_EQ:
12865 id = ansi_assopname (BIT_AND_EXPR);
12866 break;
12868 case CPP_OR_EQ:
12869 id = ansi_assopname (BIT_IOR_EXPR);
12870 break;
12872 case CPP_LSHIFT:
12873 id = ansi_opname (LSHIFT_EXPR);
12874 break;
12876 case CPP_RSHIFT:
12877 id = ansi_opname (RSHIFT_EXPR);
12878 break;
12880 case CPP_LSHIFT_EQ:
12881 id = ansi_assopname (LSHIFT_EXPR);
12882 break;
12884 case CPP_RSHIFT_EQ:
12885 id = ansi_assopname (RSHIFT_EXPR);
12886 break;
12888 case CPP_EQ_EQ:
12889 id = ansi_opname (EQ_EXPR);
12890 break;
12892 case CPP_NOT_EQ:
12893 id = ansi_opname (NE_EXPR);
12894 break;
12896 case CPP_LESS_EQ:
12897 id = ansi_opname (LE_EXPR);
12898 break;
12900 case CPP_GREATER_EQ:
12901 id = ansi_opname (GE_EXPR);
12902 break;
12904 case CPP_AND_AND:
12905 id = ansi_opname (TRUTH_ANDIF_EXPR);
12906 break;
12908 case CPP_OR_OR:
12909 id = ansi_opname (TRUTH_ORIF_EXPR);
12910 break;
12912 case CPP_PLUS_PLUS:
12913 id = ansi_opname (POSTINCREMENT_EXPR);
12914 break;
12916 case CPP_MINUS_MINUS:
12917 id = ansi_opname (PREDECREMENT_EXPR);
12918 break;
12920 case CPP_COMMA:
12921 id = ansi_opname (COMPOUND_EXPR);
12922 break;
12924 case CPP_DEREF_STAR:
12925 id = ansi_opname (MEMBER_REF);
12926 break;
12928 case CPP_DEREF:
12929 id = ansi_opname (COMPONENT_REF);
12930 break;
12932 case CPP_OPEN_PAREN:
12933 /* Consume the `('. */
12934 cp_lexer_consume_token (parser->lexer);
12935 /* Look for the matching `)'. */
12936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12937 return ansi_opname (CALL_EXPR);
12939 case CPP_OPEN_SQUARE:
12940 /* Consume the `['. */
12941 cp_lexer_consume_token (parser->lexer);
12942 /* Look for the matching `]'. */
12943 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12944 return ansi_opname (ARRAY_REF);
12946 case CPP_UTF8STRING:
12947 case CPP_UTF8STRING_USERDEF:
12948 utf8 = true;
12949 case CPP_STRING:
12950 case CPP_WSTRING:
12951 case CPP_STRING16:
12952 case CPP_STRING32:
12953 case CPP_STRING_USERDEF:
12954 case CPP_WSTRING_USERDEF:
12955 case CPP_STRING16_USERDEF:
12956 case CPP_STRING32_USERDEF:
12958 tree str, string_tree;
12959 int sz, len;
12961 if (cxx_dialect == cxx98)
12962 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12964 /* Consume the string. */
12965 str = cp_parser_string_literal (parser, /*translate=*/true,
12966 /*wide_ok=*/true, /*lookup_udlit=*/false);
12967 if (str == error_mark_node)
12968 return error_mark_node;
12969 else if (TREE_CODE (str) == USERDEF_LITERAL)
12971 string_tree = USERDEF_LITERAL_VALUE (str);
12972 id = USERDEF_LITERAL_SUFFIX_ID (str);
12974 else
12976 string_tree = str;
12977 /* Look for the suffix identifier. */
12978 token = cp_lexer_peek_token (parser->lexer);
12979 if (token->type == CPP_NAME)
12980 id = cp_parser_identifier (parser);
12981 else if (token->type == CPP_KEYWORD)
12983 error ("unexpected keyword;"
12984 " remove space between quotes and suffix identifier");
12985 return error_mark_node;
12987 else
12989 error ("expected suffix identifier");
12990 return error_mark_node;
12993 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
12994 (TREE_TYPE (TREE_TYPE (string_tree))));
12995 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
12996 if (len != 0)
12998 error ("expected empty string after %<operator%> keyword");
12999 return error_mark_node;
13001 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13002 != char_type_node)
13004 error ("invalid encoding prefix in literal operator");
13005 return error_mark_node;
13007 if (id != error_mark_node)
13009 const char *name = IDENTIFIER_POINTER (id);
13010 id = cp_literal_operator_id (name);
13012 return id;
13015 default:
13016 /* Anything else is an error. */
13017 break;
13020 /* If we have selected an identifier, we need to consume the
13021 operator token. */
13022 if (id)
13023 cp_lexer_consume_token (parser->lexer);
13024 /* Otherwise, no valid operator name was present. */
13025 else
13027 cp_parser_error (parser, "expected operator");
13028 id = error_mark_node;
13031 return id;
13034 /* Parse a template-declaration.
13036 template-declaration:
13037 export [opt] template < template-parameter-list > declaration
13039 If MEMBER_P is TRUE, this template-declaration occurs within a
13040 class-specifier.
13042 The grammar rule given by the standard isn't correct. What
13043 is really meant is:
13045 template-declaration:
13046 export [opt] template-parameter-list-seq
13047 decl-specifier-seq [opt] init-declarator [opt] ;
13048 export [opt] template-parameter-list-seq
13049 function-definition
13051 template-parameter-list-seq:
13052 template-parameter-list-seq [opt]
13053 template < template-parameter-list > */
13055 static void
13056 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13058 /* Check for `export'. */
13059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13061 /* Consume the `export' token. */
13062 cp_lexer_consume_token (parser->lexer);
13063 /* Warn that we do not support `export'. */
13064 warning (0, "keyword %<export%> not implemented, and will be ignored");
13067 cp_parser_template_declaration_after_export (parser, member_p);
13070 /* Parse a template-parameter-list.
13072 template-parameter-list:
13073 template-parameter
13074 template-parameter-list , template-parameter
13076 Returns a TREE_LIST. Each node represents a template parameter.
13077 The nodes are connected via their TREE_CHAINs. */
13079 static tree
13080 cp_parser_template_parameter_list (cp_parser* parser)
13082 tree parameter_list = NULL_TREE;
13084 begin_template_parm_list ();
13086 /* The loop below parses the template parms. We first need to know
13087 the total number of template parms to be able to compute proper
13088 canonical types of each dependent type. So after the loop, when
13089 we know the total number of template parms,
13090 end_template_parm_list computes the proper canonical types and
13091 fixes up the dependent types accordingly. */
13092 while (true)
13094 tree parameter;
13095 bool is_non_type;
13096 bool is_parameter_pack;
13097 location_t parm_loc;
13099 /* Parse the template-parameter. */
13100 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13101 parameter = cp_parser_template_parameter (parser,
13102 &is_non_type,
13103 &is_parameter_pack);
13104 /* Add it to the list. */
13105 if (parameter != error_mark_node)
13106 parameter_list = process_template_parm (parameter_list,
13107 parm_loc,
13108 parameter,
13109 is_non_type,
13110 is_parameter_pack);
13111 else
13113 tree err_parm = build_tree_list (parameter, parameter);
13114 parameter_list = chainon (parameter_list, err_parm);
13117 /* If the next token is not a `,', we're done. */
13118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13119 break;
13120 /* Otherwise, consume the `,' token. */
13121 cp_lexer_consume_token (parser->lexer);
13124 return end_template_parm_list (parameter_list);
13127 /* Parse a template-parameter.
13129 template-parameter:
13130 type-parameter
13131 parameter-declaration
13133 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13134 the parameter. The TREE_PURPOSE is the default value, if any.
13135 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13136 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13137 set to true iff this parameter is a parameter pack. */
13139 static tree
13140 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13141 bool *is_parameter_pack)
13143 cp_token *token;
13144 cp_parameter_declarator *parameter_declarator;
13145 cp_declarator *id_declarator;
13146 tree parm;
13148 /* Assume it is a type parameter or a template parameter. */
13149 *is_non_type = false;
13150 /* Assume it not a parameter pack. */
13151 *is_parameter_pack = false;
13152 /* Peek at the next token. */
13153 token = cp_lexer_peek_token (parser->lexer);
13154 /* If it is `class' or `template', we have a type-parameter. */
13155 if (token->keyword == RID_TEMPLATE)
13156 return cp_parser_type_parameter (parser, is_parameter_pack);
13157 /* If it is `class' or `typename' we do not know yet whether it is a
13158 type parameter or a non-type parameter. Consider:
13160 template <typename T, typename T::X X> ...
13164 template <class C, class D*> ...
13166 Here, the first parameter is a type parameter, and the second is
13167 a non-type parameter. We can tell by looking at the token after
13168 the identifier -- if it is a `,', `=', or `>' then we have a type
13169 parameter. */
13170 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13172 /* Peek at the token after `class' or `typename'. */
13173 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13174 /* If it's an ellipsis, we have a template type parameter
13175 pack. */
13176 if (token->type == CPP_ELLIPSIS)
13177 return cp_parser_type_parameter (parser, is_parameter_pack);
13178 /* If it's an identifier, skip it. */
13179 if (token->type == CPP_NAME)
13180 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13181 /* Now, see if the token looks like the end of a template
13182 parameter. */
13183 if (token->type == CPP_COMMA
13184 || token->type == CPP_EQ
13185 || token->type == CPP_GREATER)
13186 return cp_parser_type_parameter (parser, is_parameter_pack);
13189 /* Otherwise, it is a non-type parameter.
13191 [temp.param]
13193 When parsing a default template-argument for a non-type
13194 template-parameter, the first non-nested `>' is taken as the end
13195 of the template parameter-list rather than a greater-than
13196 operator. */
13197 *is_non_type = true;
13198 parameter_declarator
13199 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13200 /*parenthesized_p=*/NULL);
13202 if (!parameter_declarator)
13203 return error_mark_node;
13205 /* If the parameter declaration is marked as a parameter pack, set
13206 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13207 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13208 grokdeclarator. */
13209 if (parameter_declarator->declarator
13210 && parameter_declarator->declarator->parameter_pack_p)
13212 *is_parameter_pack = true;
13213 parameter_declarator->declarator->parameter_pack_p = false;
13216 if (parameter_declarator->default_argument)
13218 /* Can happen in some cases of erroneous input (c++/34892). */
13219 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13220 /* Consume the `...' for better error recovery. */
13221 cp_lexer_consume_token (parser->lexer);
13223 /* If the next token is an ellipsis, and we don't already have it
13224 marked as a parameter pack, then we have a parameter pack (that
13225 has no declarator). */
13226 else if (!*is_parameter_pack
13227 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13228 && (declarator_can_be_parameter_pack
13229 (parameter_declarator->declarator)))
13231 /* Consume the `...'. */
13232 cp_lexer_consume_token (parser->lexer);
13233 maybe_warn_variadic_templates ();
13235 *is_parameter_pack = true;
13237 /* We might end up with a pack expansion as the type of the non-type
13238 template parameter, in which case this is a non-type template
13239 parameter pack. */
13240 else if (parameter_declarator->decl_specifiers.type
13241 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13243 *is_parameter_pack = true;
13244 parameter_declarator->decl_specifiers.type =
13245 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13248 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13250 /* Parameter packs cannot have default arguments. However, a
13251 user may try to do so, so we'll parse them and give an
13252 appropriate diagnostic here. */
13254 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13256 /* Find the name of the parameter pack. */
13257 id_declarator = parameter_declarator->declarator;
13258 while (id_declarator && id_declarator->kind != cdk_id)
13259 id_declarator = id_declarator->declarator;
13261 if (id_declarator && id_declarator->kind == cdk_id)
13262 error_at (start_token->location,
13263 "template parameter pack %qD cannot have a default argument",
13264 id_declarator->u.id.unqualified_name);
13265 else
13266 error_at (start_token->location,
13267 "template parameter pack cannot have a default argument");
13269 /* Parse the default argument, but throw away the result. */
13270 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13273 parm = grokdeclarator (parameter_declarator->declarator,
13274 &parameter_declarator->decl_specifiers,
13275 TPARM, /*initialized=*/0,
13276 /*attrlist=*/NULL);
13277 if (parm == error_mark_node)
13278 return error_mark_node;
13280 return build_tree_list (parameter_declarator->default_argument, parm);
13283 /* Parse a type-parameter.
13285 type-parameter:
13286 class identifier [opt]
13287 class identifier [opt] = type-id
13288 typename identifier [opt]
13289 typename identifier [opt] = type-id
13290 template < template-parameter-list > class identifier [opt]
13291 template < template-parameter-list > class identifier [opt]
13292 = id-expression
13294 GNU Extension (variadic templates):
13296 type-parameter:
13297 class ... identifier [opt]
13298 typename ... identifier [opt]
13300 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13301 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13302 the declaration of the parameter.
13304 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13306 static tree
13307 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13309 cp_token *token;
13310 tree parameter;
13312 /* Look for a keyword to tell us what kind of parameter this is. */
13313 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13314 if (!token)
13315 return error_mark_node;
13317 switch (token->keyword)
13319 case RID_CLASS:
13320 case RID_TYPENAME:
13322 tree identifier;
13323 tree default_argument;
13325 /* If the next token is an ellipsis, we have a template
13326 argument pack. */
13327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13329 /* Consume the `...' token. */
13330 cp_lexer_consume_token (parser->lexer);
13331 maybe_warn_variadic_templates ();
13333 *is_parameter_pack = true;
13336 /* If the next token is an identifier, then it names the
13337 parameter. */
13338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13339 identifier = cp_parser_identifier (parser);
13340 else
13341 identifier = NULL_TREE;
13343 /* Create the parameter. */
13344 parameter = finish_template_type_parm (class_type_node, identifier);
13346 /* If the next token is an `=', we have a default argument. */
13347 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13349 /* Consume the `=' token. */
13350 cp_lexer_consume_token (parser->lexer);
13351 /* Parse the default-argument. */
13352 push_deferring_access_checks (dk_no_deferred);
13353 default_argument = cp_parser_type_id (parser);
13355 /* Template parameter packs cannot have default
13356 arguments. */
13357 if (*is_parameter_pack)
13359 if (identifier)
13360 error_at (token->location,
13361 "template parameter pack %qD cannot have a "
13362 "default argument", identifier);
13363 else
13364 error_at (token->location,
13365 "template parameter packs cannot have "
13366 "default arguments");
13367 default_argument = NULL_TREE;
13369 pop_deferring_access_checks ();
13371 else
13372 default_argument = NULL_TREE;
13374 /* Create the combined representation of the parameter and the
13375 default argument. */
13376 parameter = build_tree_list (default_argument, parameter);
13378 break;
13380 case RID_TEMPLATE:
13382 tree identifier;
13383 tree default_argument;
13385 /* Look for the `<'. */
13386 cp_parser_require (parser, CPP_LESS, RT_LESS);
13387 /* Parse the template-parameter-list. */
13388 cp_parser_template_parameter_list (parser);
13389 /* Look for the `>'. */
13390 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13391 /* Look for the `class' or 'typename' keywords. */
13392 cp_parser_type_parameter_key (parser);
13393 /* If the next token is an ellipsis, we have a template
13394 argument pack. */
13395 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13397 /* Consume the `...' token. */
13398 cp_lexer_consume_token (parser->lexer);
13399 maybe_warn_variadic_templates ();
13401 *is_parameter_pack = true;
13403 /* If the next token is an `=', then there is a
13404 default-argument. If the next token is a `>', we are at
13405 the end of the parameter-list. If the next token is a `,',
13406 then we are at the end of this parameter. */
13407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13408 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13409 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13411 identifier = cp_parser_identifier (parser);
13412 /* Treat invalid names as if the parameter were nameless. */
13413 if (identifier == error_mark_node)
13414 identifier = NULL_TREE;
13416 else
13417 identifier = NULL_TREE;
13419 /* Create the template parameter. */
13420 parameter = finish_template_template_parm (class_type_node,
13421 identifier);
13423 /* If the next token is an `=', then there is a
13424 default-argument. */
13425 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13427 bool is_template;
13429 /* Consume the `='. */
13430 cp_lexer_consume_token (parser->lexer);
13431 /* Parse the id-expression. */
13432 push_deferring_access_checks (dk_no_deferred);
13433 /* save token before parsing the id-expression, for error
13434 reporting */
13435 token = cp_lexer_peek_token (parser->lexer);
13436 default_argument
13437 = cp_parser_id_expression (parser,
13438 /*template_keyword_p=*/false,
13439 /*check_dependency_p=*/true,
13440 /*template_p=*/&is_template,
13441 /*declarator_p=*/false,
13442 /*optional_p=*/false);
13443 if (TREE_CODE (default_argument) == TYPE_DECL)
13444 /* If the id-expression was a template-id that refers to
13445 a template-class, we already have the declaration here,
13446 so no further lookup is needed. */
13448 else
13449 /* Look up the name. */
13450 default_argument
13451 = cp_parser_lookup_name (parser, default_argument,
13452 none_type,
13453 /*is_template=*/is_template,
13454 /*is_namespace=*/false,
13455 /*check_dependency=*/true,
13456 /*ambiguous_decls=*/NULL,
13457 token->location);
13458 /* See if the default argument is valid. */
13459 default_argument
13460 = check_template_template_default_arg (default_argument);
13462 /* Template parameter packs cannot have default
13463 arguments. */
13464 if (*is_parameter_pack)
13466 if (identifier)
13467 error_at (token->location,
13468 "template parameter pack %qD cannot "
13469 "have a default argument",
13470 identifier);
13471 else
13472 error_at (token->location, "template parameter packs cannot "
13473 "have default arguments");
13474 default_argument = NULL_TREE;
13476 pop_deferring_access_checks ();
13478 else
13479 default_argument = NULL_TREE;
13481 /* Create the combined representation of the parameter and the
13482 default argument. */
13483 parameter = build_tree_list (default_argument, parameter);
13485 break;
13487 default:
13488 gcc_unreachable ();
13489 break;
13492 return parameter;
13495 /* Parse a template-id.
13497 template-id:
13498 template-name < template-argument-list [opt] >
13500 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13501 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13502 returned. Otherwise, if the template-name names a function, or set
13503 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13504 names a class, returns a TYPE_DECL for the specialization.
13506 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13507 uninstantiated templates. */
13509 static tree
13510 cp_parser_template_id (cp_parser *parser,
13511 bool template_keyword_p,
13512 bool check_dependency_p,
13513 enum tag_types tag_type,
13514 bool is_declaration)
13516 int i;
13517 tree templ;
13518 tree arguments;
13519 tree template_id;
13520 cp_token_position start_of_id = 0;
13521 deferred_access_check *chk;
13522 vec<deferred_access_check, va_gc> *access_check;
13523 cp_token *next_token = NULL, *next_token_2 = NULL;
13524 bool is_identifier;
13526 /* If the next token corresponds to a template-id, there is no need
13527 to reparse it. */
13528 next_token = cp_lexer_peek_token (parser->lexer);
13529 if (next_token->type == CPP_TEMPLATE_ID)
13531 struct tree_check *check_value;
13533 /* Get the stored value. */
13534 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13535 /* Perform any access checks that were deferred. */
13536 access_check = check_value->checks;
13537 if (access_check)
13539 FOR_EACH_VEC_ELT (*access_check, i, chk)
13540 perform_or_defer_access_check (chk->binfo,
13541 chk->decl,
13542 chk->diag_decl,
13543 tf_warning_or_error);
13545 /* Return the stored value. */
13546 return check_value->value;
13549 /* Avoid performing name lookup if there is no possibility of
13550 finding a template-id. */
13551 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13552 || (next_token->type == CPP_NAME
13553 && !cp_parser_nth_token_starts_template_argument_list_p
13554 (parser, 2)))
13556 cp_parser_error (parser, "expected template-id");
13557 return error_mark_node;
13560 /* Remember where the template-id starts. */
13561 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13562 start_of_id = cp_lexer_token_position (parser->lexer, false);
13564 push_deferring_access_checks (dk_deferred);
13566 /* Parse the template-name. */
13567 is_identifier = false;
13568 templ = cp_parser_template_name (parser, template_keyword_p,
13569 check_dependency_p,
13570 is_declaration,
13571 tag_type,
13572 &is_identifier);
13573 if (templ == error_mark_node || is_identifier)
13575 pop_deferring_access_checks ();
13576 return templ;
13579 /* If we find the sequence `[:' after a template-name, it's probably
13580 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13581 parse correctly the argument list. */
13582 next_token = cp_lexer_peek_token (parser->lexer);
13583 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13584 if (next_token->type == CPP_OPEN_SQUARE
13585 && next_token->flags & DIGRAPH
13586 && next_token_2->type == CPP_COLON
13587 && !(next_token_2->flags & PREV_WHITE))
13589 cp_parser_parse_tentatively (parser);
13590 /* Change `:' into `::'. */
13591 next_token_2->type = CPP_SCOPE;
13592 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13593 CPP_LESS. */
13594 cp_lexer_consume_token (parser->lexer);
13596 /* Parse the arguments. */
13597 arguments = cp_parser_enclosed_template_argument_list (parser);
13598 if (!cp_parser_parse_definitely (parser))
13600 /* If we couldn't parse an argument list, then we revert our changes
13601 and return simply an error. Maybe this is not a template-id
13602 after all. */
13603 next_token_2->type = CPP_COLON;
13604 cp_parser_error (parser, "expected %<<%>");
13605 pop_deferring_access_checks ();
13606 return error_mark_node;
13608 /* Otherwise, emit an error about the invalid digraph, but continue
13609 parsing because we got our argument list. */
13610 if (permerror (next_token->location,
13611 "%<<::%> cannot begin a template-argument list"))
13613 static bool hint = false;
13614 inform (next_token->location,
13615 "%<<:%> is an alternate spelling for %<[%>."
13616 " Insert whitespace between %<<%> and %<::%>");
13617 if (!hint && !flag_permissive)
13619 inform (next_token->location, "(if you use %<-fpermissive%> "
13620 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13621 "accept your code)");
13622 hint = true;
13626 else
13628 /* Look for the `<' that starts the template-argument-list. */
13629 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13631 pop_deferring_access_checks ();
13632 return error_mark_node;
13634 /* Parse the arguments. */
13635 arguments = cp_parser_enclosed_template_argument_list (parser);
13638 /* Build a representation of the specialization. */
13639 if (identifier_p (templ))
13640 template_id = build_min_nt_loc (next_token->location,
13641 TEMPLATE_ID_EXPR,
13642 templ, arguments);
13643 else if (DECL_TYPE_TEMPLATE_P (templ)
13644 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13646 bool entering_scope;
13647 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13648 template (rather than some instantiation thereof) only if
13649 is not nested within some other construct. For example, in
13650 "template <typename T> void f(T) { A<T>::", A<T> is just an
13651 instantiation of A. */
13652 entering_scope = (template_parm_scope_p ()
13653 && cp_lexer_next_token_is (parser->lexer,
13654 CPP_SCOPE));
13655 template_id
13656 = finish_template_type (templ, arguments, entering_scope);
13658 else if (variable_template_p (templ))
13660 template_id = lookup_template_variable (templ, arguments);
13662 else
13664 /* If it's not a class-template or a template-template, it should be
13665 a function-template. */
13666 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13667 || TREE_CODE (templ) == OVERLOAD
13668 || BASELINK_P (templ)));
13670 template_id = lookup_template_function (templ, arguments);
13673 /* If parsing tentatively, replace the sequence of tokens that makes
13674 up the template-id with a CPP_TEMPLATE_ID token. That way,
13675 should we re-parse the token stream, we will not have to repeat
13676 the effort required to do the parse, nor will we issue duplicate
13677 error messages about problems during instantiation of the
13678 template. */
13679 if (start_of_id
13680 /* Don't do this if we had a parse error in a declarator; re-parsing
13681 might succeed if a name changes meaning (60361). */
13682 && !(cp_parser_error_occurred (parser)
13683 && cp_parser_parsing_tentatively (parser)
13684 && parser->in_declarator_p))
13686 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13688 /* Reset the contents of the START_OF_ID token. */
13689 token->type = CPP_TEMPLATE_ID;
13690 /* Retrieve any deferred checks. Do not pop this access checks yet
13691 so the memory will not be reclaimed during token replacing below. */
13692 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13693 token->u.tree_check_value->value = template_id;
13694 token->u.tree_check_value->checks = get_deferred_access_checks ();
13695 token->keyword = RID_MAX;
13697 /* Purge all subsequent tokens. */
13698 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13700 /* ??? Can we actually assume that, if template_id ==
13701 error_mark_node, we will have issued a diagnostic to the
13702 user, as opposed to simply marking the tentative parse as
13703 failed? */
13704 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13705 error_at (token->location, "parse error in template argument list");
13708 pop_to_parent_deferring_access_checks ();
13709 return template_id;
13712 /* Parse a template-name.
13714 template-name:
13715 identifier
13717 The standard should actually say:
13719 template-name:
13720 identifier
13721 operator-function-id
13723 A defect report has been filed about this issue.
13725 A conversion-function-id cannot be a template name because they cannot
13726 be part of a template-id. In fact, looking at this code:
13728 a.operator K<int>()
13730 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13731 It is impossible to call a templated conversion-function-id with an
13732 explicit argument list, since the only allowed template parameter is
13733 the type to which it is converting.
13735 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13736 `template' keyword, in a construction like:
13738 T::template f<3>()
13740 In that case `f' is taken to be a template-name, even though there
13741 is no way of knowing for sure.
13743 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13744 name refers to a set of overloaded functions, at least one of which
13745 is a template, or an IDENTIFIER_NODE with the name of the template,
13746 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13747 names are looked up inside uninstantiated templates. */
13749 static tree
13750 cp_parser_template_name (cp_parser* parser,
13751 bool template_keyword_p,
13752 bool check_dependency_p,
13753 bool is_declaration,
13754 enum tag_types tag_type,
13755 bool *is_identifier)
13757 tree identifier;
13758 tree decl;
13759 tree fns;
13760 cp_token *token = cp_lexer_peek_token (parser->lexer);
13762 /* If the next token is `operator', then we have either an
13763 operator-function-id or a conversion-function-id. */
13764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13766 /* We don't know whether we're looking at an
13767 operator-function-id or a conversion-function-id. */
13768 cp_parser_parse_tentatively (parser);
13769 /* Try an operator-function-id. */
13770 identifier = cp_parser_operator_function_id (parser);
13771 /* If that didn't work, try a conversion-function-id. */
13772 if (!cp_parser_parse_definitely (parser))
13774 cp_parser_error (parser, "expected template-name");
13775 return error_mark_node;
13778 /* Look for the identifier. */
13779 else
13780 identifier = cp_parser_identifier (parser);
13782 /* If we didn't find an identifier, we don't have a template-id. */
13783 if (identifier == error_mark_node)
13784 return error_mark_node;
13786 /* If the name immediately followed the `template' keyword, then it
13787 is a template-name. However, if the next token is not `<', then
13788 we do not treat it as a template-name, since it is not being used
13789 as part of a template-id. This enables us to handle constructs
13790 like:
13792 template <typename T> struct S { S(); };
13793 template <typename T> S<T>::S();
13795 correctly. We would treat `S' as a template -- if it were `S<T>'
13796 -- but we do not if there is no `<'. */
13798 if (processing_template_decl
13799 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13801 /* In a declaration, in a dependent context, we pretend that the
13802 "template" keyword was present in order to improve error
13803 recovery. For example, given:
13805 template <typename T> void f(T::X<int>);
13807 we want to treat "X<int>" as a template-id. */
13808 if (is_declaration
13809 && !template_keyword_p
13810 && parser->scope && TYPE_P (parser->scope)
13811 && check_dependency_p
13812 && dependent_scope_p (parser->scope)
13813 /* Do not do this for dtors (or ctors), since they never
13814 need the template keyword before their name. */
13815 && !constructor_name_p (identifier, parser->scope))
13817 cp_token_position start = 0;
13819 /* Explain what went wrong. */
13820 error_at (token->location, "non-template %qD used as template",
13821 identifier);
13822 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13823 parser->scope, identifier);
13824 /* If parsing tentatively, find the location of the "<" token. */
13825 if (cp_parser_simulate_error (parser))
13826 start = cp_lexer_token_position (parser->lexer, true);
13827 /* Parse the template arguments so that we can issue error
13828 messages about them. */
13829 cp_lexer_consume_token (parser->lexer);
13830 cp_parser_enclosed_template_argument_list (parser);
13831 /* Skip tokens until we find a good place from which to
13832 continue parsing. */
13833 cp_parser_skip_to_closing_parenthesis (parser,
13834 /*recovering=*/true,
13835 /*or_comma=*/true,
13836 /*consume_paren=*/false);
13837 /* If parsing tentatively, permanently remove the
13838 template argument list. That will prevent duplicate
13839 error messages from being issued about the missing
13840 "template" keyword. */
13841 if (start)
13842 cp_lexer_purge_tokens_after (parser->lexer, start);
13843 if (is_identifier)
13844 *is_identifier = true;
13845 return identifier;
13848 /* If the "template" keyword is present, then there is generally
13849 no point in doing name-lookup, so we just return IDENTIFIER.
13850 But, if the qualifying scope is non-dependent then we can
13851 (and must) do name-lookup normally. */
13852 if (template_keyword_p
13853 && (!parser->scope
13854 || (TYPE_P (parser->scope)
13855 && dependent_type_p (parser->scope))))
13856 return identifier;
13859 /* Look up the name. */
13860 decl = cp_parser_lookup_name (parser, identifier,
13861 tag_type,
13862 /*is_template=*/true,
13863 /*is_namespace=*/false,
13864 check_dependency_p,
13865 /*ambiguous_decls=*/NULL,
13866 token->location);
13868 /* If DECL is a template, then the name was a template-name. */
13869 if (TREE_CODE (decl) == TEMPLATE_DECL)
13871 else
13873 tree fn = NULL_TREE;
13875 /* The standard does not explicitly indicate whether a name that
13876 names a set of overloaded declarations, some of which are
13877 templates, is a template-name. However, such a name should
13878 be a template-name; otherwise, there is no way to form a
13879 template-id for the overloaded templates. */
13880 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13881 if (TREE_CODE (fns) == OVERLOAD)
13882 for (fn = fns; fn; fn = OVL_NEXT (fn))
13883 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13884 break;
13886 if (!fn)
13888 /* The name does not name a template. */
13889 cp_parser_error (parser, "expected template-name");
13890 return error_mark_node;
13894 /* If DECL is dependent, and refers to a function, then just return
13895 its name; we will look it up again during template instantiation. */
13896 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13898 tree scope = ovl_scope (decl);
13899 if (TYPE_P (scope) && dependent_type_p (scope))
13900 return identifier;
13903 return decl;
13906 /* Parse a template-argument-list.
13908 template-argument-list:
13909 template-argument ... [opt]
13910 template-argument-list , template-argument ... [opt]
13912 Returns a TREE_VEC containing the arguments. */
13914 static tree
13915 cp_parser_template_argument_list (cp_parser* parser)
13917 tree fixed_args[10];
13918 unsigned n_args = 0;
13919 unsigned alloced = 10;
13920 tree *arg_ary = fixed_args;
13921 tree vec;
13922 bool saved_in_template_argument_list_p;
13923 bool saved_ice_p;
13924 bool saved_non_ice_p;
13926 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13927 parser->in_template_argument_list_p = true;
13928 /* Even if the template-id appears in an integral
13929 constant-expression, the contents of the argument list do
13930 not. */
13931 saved_ice_p = parser->integral_constant_expression_p;
13932 parser->integral_constant_expression_p = false;
13933 saved_non_ice_p = parser->non_integral_constant_expression_p;
13934 parser->non_integral_constant_expression_p = false;
13936 /* Parse the arguments. */
13939 tree argument;
13941 if (n_args)
13942 /* Consume the comma. */
13943 cp_lexer_consume_token (parser->lexer);
13945 /* Parse the template-argument. */
13946 argument = cp_parser_template_argument (parser);
13948 /* If the next token is an ellipsis, we're expanding a template
13949 argument pack. */
13950 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13952 if (argument == error_mark_node)
13954 cp_token *token = cp_lexer_peek_token (parser->lexer);
13955 error_at (token->location,
13956 "expected parameter pack before %<...%>");
13958 /* Consume the `...' token. */
13959 cp_lexer_consume_token (parser->lexer);
13961 /* Make the argument into a TYPE_PACK_EXPANSION or
13962 EXPR_PACK_EXPANSION. */
13963 argument = make_pack_expansion (argument);
13966 if (n_args == alloced)
13968 alloced *= 2;
13970 if (arg_ary == fixed_args)
13972 arg_ary = XNEWVEC (tree, alloced);
13973 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13975 else
13976 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13978 arg_ary[n_args++] = argument;
13980 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13982 vec = make_tree_vec (n_args);
13984 while (n_args--)
13985 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13987 if (arg_ary != fixed_args)
13988 free (arg_ary);
13989 parser->non_integral_constant_expression_p = saved_non_ice_p;
13990 parser->integral_constant_expression_p = saved_ice_p;
13991 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13992 #ifdef ENABLE_CHECKING
13993 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13994 #endif
13995 return vec;
13998 /* Parse a template-argument.
14000 template-argument:
14001 assignment-expression
14002 type-id
14003 id-expression
14005 The representation is that of an assignment-expression, type-id, or
14006 id-expression -- except that the qualified id-expression is
14007 evaluated, so that the value returned is either a DECL or an
14008 OVERLOAD.
14010 Although the standard says "assignment-expression", it forbids
14011 throw-expressions or assignments in the template argument.
14012 Therefore, we use "conditional-expression" instead. */
14014 static tree
14015 cp_parser_template_argument (cp_parser* parser)
14017 tree argument;
14018 bool template_p;
14019 bool address_p;
14020 bool maybe_type_id = false;
14021 cp_token *token = NULL, *argument_start_token = NULL;
14022 location_t loc = 0;
14023 cp_id_kind idk;
14025 /* There's really no way to know what we're looking at, so we just
14026 try each alternative in order.
14028 [temp.arg]
14030 In a template-argument, an ambiguity between a type-id and an
14031 expression is resolved to a type-id, regardless of the form of
14032 the corresponding template-parameter.
14034 Therefore, we try a type-id first. */
14035 cp_parser_parse_tentatively (parser);
14036 argument = cp_parser_template_type_arg (parser);
14037 /* If there was no error parsing the type-id but the next token is a
14038 '>>', our behavior depends on which dialect of C++ we're
14039 parsing. In C++98, we probably found a typo for '> >'. But there
14040 are type-id which are also valid expressions. For instance:
14042 struct X { int operator >> (int); };
14043 template <int V> struct Foo {};
14044 Foo<X () >> 5> r;
14046 Here 'X()' is a valid type-id of a function type, but the user just
14047 wanted to write the expression "X() >> 5". Thus, we remember that we
14048 found a valid type-id, but we still try to parse the argument as an
14049 expression to see what happens.
14051 In C++0x, the '>>' will be considered two separate '>'
14052 tokens. */
14053 if (!cp_parser_error_occurred (parser)
14054 && cxx_dialect == cxx98
14055 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14057 maybe_type_id = true;
14058 cp_parser_abort_tentative_parse (parser);
14060 else
14062 /* If the next token isn't a `,' or a `>', then this argument wasn't
14063 really finished. This means that the argument is not a valid
14064 type-id. */
14065 if (!cp_parser_next_token_ends_template_argument_p (parser))
14066 cp_parser_error (parser, "expected template-argument");
14067 /* If that worked, we're done. */
14068 if (cp_parser_parse_definitely (parser))
14069 return argument;
14071 /* We're still not sure what the argument will be. */
14072 cp_parser_parse_tentatively (parser);
14073 /* Try a template. */
14074 argument_start_token = cp_lexer_peek_token (parser->lexer);
14075 argument = cp_parser_id_expression (parser,
14076 /*template_keyword_p=*/false,
14077 /*check_dependency_p=*/true,
14078 &template_p,
14079 /*declarator_p=*/false,
14080 /*optional_p=*/false);
14081 /* If the next token isn't a `,' or a `>', then this argument wasn't
14082 really finished. */
14083 if (!cp_parser_next_token_ends_template_argument_p (parser))
14084 cp_parser_error (parser, "expected template-argument");
14085 if (!cp_parser_error_occurred (parser))
14087 /* Figure out what is being referred to. If the id-expression
14088 was for a class template specialization, then we will have a
14089 TYPE_DECL at this point. There is no need to do name lookup
14090 at this point in that case. */
14091 if (TREE_CODE (argument) != TYPE_DECL)
14092 argument = cp_parser_lookup_name (parser, argument,
14093 none_type,
14094 /*is_template=*/template_p,
14095 /*is_namespace=*/false,
14096 /*check_dependency=*/true,
14097 /*ambiguous_decls=*/NULL,
14098 argument_start_token->location);
14099 if (TREE_CODE (argument) != TEMPLATE_DECL
14100 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14101 cp_parser_error (parser, "expected template-name");
14103 if (cp_parser_parse_definitely (parser))
14104 return argument;
14105 /* It must be a non-type argument. There permitted cases are given
14106 in [temp.arg.nontype]:
14108 -- an integral constant-expression of integral or enumeration
14109 type; or
14111 -- the name of a non-type template-parameter; or
14113 -- the name of an object or function with external linkage...
14115 -- the address of an object or function with external linkage...
14117 -- a pointer to member... */
14118 /* Look for a non-type template parameter. */
14119 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14121 cp_parser_parse_tentatively (parser);
14122 argument = cp_parser_primary_expression (parser,
14123 /*address_p=*/false,
14124 /*cast_p=*/false,
14125 /*template_arg_p=*/true,
14126 &idk);
14127 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14128 || !cp_parser_next_token_ends_template_argument_p (parser))
14129 cp_parser_simulate_error (parser);
14130 if (cp_parser_parse_definitely (parser))
14131 return argument;
14134 /* If the next token is "&", the argument must be the address of an
14135 object or function with external linkage. */
14136 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14137 if (address_p)
14139 loc = cp_lexer_peek_token (parser->lexer)->location;
14140 cp_lexer_consume_token (parser->lexer);
14142 /* See if we might have an id-expression. */
14143 token = cp_lexer_peek_token (parser->lexer);
14144 if (token->type == CPP_NAME
14145 || token->keyword == RID_OPERATOR
14146 || token->type == CPP_SCOPE
14147 || token->type == CPP_TEMPLATE_ID
14148 || token->type == CPP_NESTED_NAME_SPECIFIER)
14150 cp_parser_parse_tentatively (parser);
14151 argument = cp_parser_primary_expression (parser,
14152 address_p,
14153 /*cast_p=*/false,
14154 /*template_arg_p=*/true,
14155 &idk);
14156 if (cp_parser_error_occurred (parser)
14157 || !cp_parser_next_token_ends_template_argument_p (parser))
14158 cp_parser_abort_tentative_parse (parser);
14159 else
14161 tree probe;
14163 if (INDIRECT_REF_P (argument))
14165 /* Strip the dereference temporarily. */
14166 gcc_assert (REFERENCE_REF_P (argument));
14167 argument = TREE_OPERAND (argument, 0);
14170 /* If we're in a template, we represent a qualified-id referring
14171 to a static data member as a SCOPE_REF even if the scope isn't
14172 dependent so that we can check access control later. */
14173 probe = argument;
14174 if (TREE_CODE (probe) == SCOPE_REF)
14175 probe = TREE_OPERAND (probe, 1);
14176 if (VAR_P (probe))
14178 /* A variable without external linkage might still be a
14179 valid constant-expression, so no error is issued here
14180 if the external-linkage check fails. */
14181 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14182 cp_parser_simulate_error (parser);
14184 else if (is_overloaded_fn (argument))
14185 /* All overloaded functions are allowed; if the external
14186 linkage test does not pass, an error will be issued
14187 later. */
14189 else if (address_p
14190 && (TREE_CODE (argument) == OFFSET_REF
14191 || TREE_CODE (argument) == SCOPE_REF))
14192 /* A pointer-to-member. */
14194 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14196 else
14197 cp_parser_simulate_error (parser);
14199 if (cp_parser_parse_definitely (parser))
14201 if (address_p)
14202 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14203 tf_warning_or_error);
14204 else
14205 argument = convert_from_reference (argument);
14206 return argument;
14210 /* If the argument started with "&", there are no other valid
14211 alternatives at this point. */
14212 if (address_p)
14214 cp_parser_error (parser, "invalid non-type template argument");
14215 return error_mark_node;
14218 /* If the argument wasn't successfully parsed as a type-id followed
14219 by '>>', the argument can only be a constant expression now.
14220 Otherwise, we try parsing the constant-expression tentatively,
14221 because the argument could really be a type-id. */
14222 if (maybe_type_id)
14223 cp_parser_parse_tentatively (parser);
14224 argument = cp_parser_constant_expression (parser,
14225 /*allow_non_constant_p=*/false,
14226 /*non_constant_p=*/NULL);
14227 if (!maybe_type_id)
14228 return argument;
14229 if (!cp_parser_next_token_ends_template_argument_p (parser))
14230 cp_parser_error (parser, "expected template-argument");
14231 if (cp_parser_parse_definitely (parser))
14232 return argument;
14233 /* We did our best to parse the argument as a non type-id, but that
14234 was the only alternative that matched (albeit with a '>' after
14235 it). We can assume it's just a typo from the user, and a
14236 diagnostic will then be issued. */
14237 return cp_parser_template_type_arg (parser);
14240 /* Parse an explicit-instantiation.
14242 explicit-instantiation:
14243 template declaration
14245 Although the standard says `declaration', what it really means is:
14247 explicit-instantiation:
14248 template decl-specifier-seq [opt] declarator [opt] ;
14250 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14251 supposed to be allowed. A defect report has been filed about this
14252 issue.
14254 GNU Extension:
14256 explicit-instantiation:
14257 storage-class-specifier template
14258 decl-specifier-seq [opt] declarator [opt] ;
14259 function-specifier template
14260 decl-specifier-seq [opt] declarator [opt] ; */
14262 static void
14263 cp_parser_explicit_instantiation (cp_parser* parser)
14265 int declares_class_or_enum;
14266 cp_decl_specifier_seq decl_specifiers;
14267 tree extension_specifier = NULL_TREE;
14269 timevar_push (TV_TEMPLATE_INST);
14271 /* Look for an (optional) storage-class-specifier or
14272 function-specifier. */
14273 if (cp_parser_allow_gnu_extensions_p (parser))
14275 extension_specifier
14276 = cp_parser_storage_class_specifier_opt (parser);
14277 if (!extension_specifier)
14278 extension_specifier
14279 = cp_parser_function_specifier_opt (parser,
14280 /*decl_specs=*/NULL);
14283 /* Look for the `template' keyword. */
14284 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14285 /* Let the front end know that we are processing an explicit
14286 instantiation. */
14287 begin_explicit_instantiation ();
14288 /* [temp.explicit] says that we are supposed to ignore access
14289 control while processing explicit instantiation directives. */
14290 push_deferring_access_checks (dk_no_check);
14291 /* Parse a decl-specifier-seq. */
14292 cp_parser_decl_specifier_seq (parser,
14293 CP_PARSER_FLAGS_OPTIONAL,
14294 &decl_specifiers,
14295 &declares_class_or_enum);
14296 /* If there was exactly one decl-specifier, and it declared a class,
14297 and there's no declarator, then we have an explicit type
14298 instantiation. */
14299 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14301 tree type;
14303 type = check_tag_decl (&decl_specifiers,
14304 /*explicit_type_instantiation_p=*/true);
14305 /* Turn access control back on for names used during
14306 template instantiation. */
14307 pop_deferring_access_checks ();
14308 if (type)
14309 do_type_instantiation (type, extension_specifier,
14310 /*complain=*/tf_error);
14312 else
14314 cp_declarator *declarator;
14315 tree decl;
14317 /* Parse the declarator. */
14318 declarator
14319 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14320 /*ctor_dtor_or_conv_p=*/NULL,
14321 /*parenthesized_p=*/NULL,
14322 /*member_p=*/false,
14323 /*friend_p=*/false);
14324 if (declares_class_or_enum & 2)
14325 cp_parser_check_for_definition_in_return_type (declarator,
14326 decl_specifiers.type,
14327 decl_specifiers.locations[ds_type_spec]);
14328 if (declarator != cp_error_declarator)
14330 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14331 permerror (decl_specifiers.locations[ds_inline],
14332 "explicit instantiation shall not use"
14333 " %<inline%> specifier");
14334 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14335 permerror (decl_specifiers.locations[ds_constexpr],
14336 "explicit instantiation shall not use"
14337 " %<constexpr%> specifier");
14339 decl = grokdeclarator (declarator, &decl_specifiers,
14340 NORMAL, 0, &decl_specifiers.attributes);
14341 /* Turn access control back on for names used during
14342 template instantiation. */
14343 pop_deferring_access_checks ();
14344 /* Do the explicit instantiation. */
14345 do_decl_instantiation (decl, extension_specifier);
14347 else
14349 pop_deferring_access_checks ();
14350 /* Skip the body of the explicit instantiation. */
14351 cp_parser_skip_to_end_of_statement (parser);
14354 /* We're done with the instantiation. */
14355 end_explicit_instantiation ();
14357 cp_parser_consume_semicolon_at_end_of_statement (parser);
14359 timevar_pop (TV_TEMPLATE_INST);
14362 /* Parse an explicit-specialization.
14364 explicit-specialization:
14365 template < > declaration
14367 Although the standard says `declaration', what it really means is:
14369 explicit-specialization:
14370 template <> decl-specifier [opt] init-declarator [opt] ;
14371 template <> function-definition
14372 template <> explicit-specialization
14373 template <> template-declaration */
14375 static void
14376 cp_parser_explicit_specialization (cp_parser* parser)
14378 bool need_lang_pop;
14379 cp_token *token = cp_lexer_peek_token (parser->lexer);
14381 /* Look for the `template' keyword. */
14382 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14383 /* Look for the `<'. */
14384 cp_parser_require (parser, CPP_LESS, RT_LESS);
14385 /* Look for the `>'. */
14386 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14387 /* We have processed another parameter list. */
14388 ++parser->num_template_parameter_lists;
14389 /* [temp]
14391 A template ... explicit specialization ... shall not have C
14392 linkage. */
14393 if (current_lang_name == lang_name_c)
14395 error_at (token->location, "template specialization with C linkage");
14396 /* Give it C++ linkage to avoid confusing other parts of the
14397 front end. */
14398 push_lang_context (lang_name_cplusplus);
14399 need_lang_pop = true;
14401 else
14402 need_lang_pop = false;
14403 /* Let the front end know that we are beginning a specialization. */
14404 if (!begin_specialization ())
14406 end_specialization ();
14407 return;
14410 /* If the next keyword is `template', we need to figure out whether
14411 or not we're looking a template-declaration. */
14412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14414 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14415 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14416 cp_parser_template_declaration_after_export (parser,
14417 /*member_p=*/false);
14418 else
14419 cp_parser_explicit_specialization (parser);
14421 else
14422 /* Parse the dependent declaration. */
14423 cp_parser_single_declaration (parser,
14424 /*checks=*/NULL,
14425 /*member_p=*/false,
14426 /*explicit_specialization_p=*/true,
14427 /*friend_p=*/NULL);
14428 /* We're done with the specialization. */
14429 end_specialization ();
14430 /* For the erroneous case of a template with C linkage, we pushed an
14431 implicit C++ linkage scope; exit that scope now. */
14432 if (need_lang_pop)
14433 pop_lang_context ();
14434 /* We're done with this parameter list. */
14435 --parser->num_template_parameter_lists;
14438 /* Parse a type-specifier.
14440 type-specifier:
14441 simple-type-specifier
14442 class-specifier
14443 enum-specifier
14444 elaborated-type-specifier
14445 cv-qualifier
14447 GNU Extension:
14449 type-specifier:
14450 __complex__
14452 Returns a representation of the type-specifier. For a
14453 class-specifier, enum-specifier, or elaborated-type-specifier, a
14454 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14456 The parser flags FLAGS is used to control type-specifier parsing.
14458 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14459 in a decl-specifier-seq.
14461 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14462 class-specifier, enum-specifier, or elaborated-type-specifier, then
14463 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14464 if a type is declared; 2 if it is defined. Otherwise, it is set to
14465 zero.
14467 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14468 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14469 is set to FALSE. */
14471 static tree
14472 cp_parser_type_specifier (cp_parser* parser,
14473 cp_parser_flags flags,
14474 cp_decl_specifier_seq *decl_specs,
14475 bool is_declaration,
14476 int* declares_class_or_enum,
14477 bool* is_cv_qualifier)
14479 tree type_spec = NULL_TREE;
14480 cp_token *token;
14481 enum rid keyword;
14482 cp_decl_spec ds = ds_last;
14484 /* Assume this type-specifier does not declare a new type. */
14485 if (declares_class_or_enum)
14486 *declares_class_or_enum = 0;
14487 /* And that it does not specify a cv-qualifier. */
14488 if (is_cv_qualifier)
14489 *is_cv_qualifier = false;
14490 /* Peek at the next token. */
14491 token = cp_lexer_peek_token (parser->lexer);
14493 /* If we're looking at a keyword, we can use that to guide the
14494 production we choose. */
14495 keyword = token->keyword;
14496 switch (keyword)
14498 case RID_ENUM:
14499 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14500 goto elaborated_type_specifier;
14502 /* Look for the enum-specifier. */
14503 type_spec = cp_parser_enum_specifier (parser);
14504 /* If that worked, we're done. */
14505 if (type_spec)
14507 if (declares_class_or_enum)
14508 *declares_class_or_enum = 2;
14509 if (decl_specs)
14510 cp_parser_set_decl_spec_type (decl_specs,
14511 type_spec,
14512 token,
14513 /*type_definition_p=*/true);
14514 return type_spec;
14516 else
14517 goto elaborated_type_specifier;
14519 /* Any of these indicate either a class-specifier, or an
14520 elaborated-type-specifier. */
14521 case RID_CLASS:
14522 case RID_STRUCT:
14523 case RID_UNION:
14524 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14525 goto elaborated_type_specifier;
14527 /* Parse tentatively so that we can back up if we don't find a
14528 class-specifier. */
14529 cp_parser_parse_tentatively (parser);
14530 /* Look for the class-specifier. */
14531 type_spec = cp_parser_class_specifier (parser);
14532 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14533 /* If that worked, we're done. */
14534 if (cp_parser_parse_definitely (parser))
14536 if (declares_class_or_enum)
14537 *declares_class_or_enum = 2;
14538 if (decl_specs)
14539 cp_parser_set_decl_spec_type (decl_specs,
14540 type_spec,
14541 token,
14542 /*type_definition_p=*/true);
14543 return type_spec;
14546 /* Fall through. */
14547 elaborated_type_specifier:
14548 /* We're declaring (not defining) a class or enum. */
14549 if (declares_class_or_enum)
14550 *declares_class_or_enum = 1;
14552 /* Fall through. */
14553 case RID_TYPENAME:
14554 /* Look for an elaborated-type-specifier. */
14555 type_spec
14556 = (cp_parser_elaborated_type_specifier
14557 (parser,
14558 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14559 is_declaration));
14560 if (decl_specs)
14561 cp_parser_set_decl_spec_type (decl_specs,
14562 type_spec,
14563 token,
14564 /*type_definition_p=*/false);
14565 return type_spec;
14567 case RID_CONST:
14568 ds = ds_const;
14569 if (is_cv_qualifier)
14570 *is_cv_qualifier = true;
14571 break;
14573 case RID_VOLATILE:
14574 ds = ds_volatile;
14575 if (is_cv_qualifier)
14576 *is_cv_qualifier = true;
14577 break;
14579 case RID_RESTRICT:
14580 ds = ds_restrict;
14581 if (is_cv_qualifier)
14582 *is_cv_qualifier = true;
14583 break;
14585 case RID_COMPLEX:
14586 /* The `__complex__' keyword is a GNU extension. */
14587 ds = ds_complex;
14588 break;
14590 default:
14591 break;
14594 /* Handle simple keywords. */
14595 if (ds != ds_last)
14597 if (decl_specs)
14599 set_and_check_decl_spec_loc (decl_specs, ds, token);
14600 decl_specs->any_specifiers_p = true;
14602 return cp_lexer_consume_token (parser->lexer)->u.value;
14605 /* If we do not already have a type-specifier, assume we are looking
14606 at a simple-type-specifier. */
14607 type_spec = cp_parser_simple_type_specifier (parser,
14608 decl_specs,
14609 flags);
14611 /* If we didn't find a type-specifier, and a type-specifier was not
14612 optional in this context, issue an error message. */
14613 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14615 cp_parser_error (parser, "expected type specifier");
14616 return error_mark_node;
14619 return type_spec;
14622 /* Parse a simple-type-specifier.
14624 simple-type-specifier:
14625 :: [opt] nested-name-specifier [opt] type-name
14626 :: [opt] nested-name-specifier template template-id
14627 char
14628 wchar_t
14629 bool
14630 short
14632 long
14633 signed
14634 unsigned
14635 float
14636 double
14637 void
14639 C++0x Extension:
14641 simple-type-specifier:
14642 auto
14643 decltype ( expression )
14644 char16_t
14645 char32_t
14646 __underlying_type ( type-id )
14648 GNU Extension:
14650 simple-type-specifier:
14651 __int128
14652 __typeof__ unary-expression
14653 __typeof__ ( type-id )
14654 __typeof__ ( type-id ) { initializer-list , [opt] }
14656 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14657 appropriately updated. */
14659 static tree
14660 cp_parser_simple_type_specifier (cp_parser* parser,
14661 cp_decl_specifier_seq *decl_specs,
14662 cp_parser_flags flags)
14664 tree type = NULL_TREE;
14665 cp_token *token;
14667 /* Peek at the next token. */
14668 token = cp_lexer_peek_token (parser->lexer);
14670 /* If we're looking at a keyword, things are easy. */
14671 switch (token->keyword)
14673 case RID_CHAR:
14674 if (decl_specs)
14675 decl_specs->explicit_char_p = true;
14676 type = char_type_node;
14677 break;
14678 case RID_CHAR16:
14679 type = char16_type_node;
14680 break;
14681 case RID_CHAR32:
14682 type = char32_type_node;
14683 break;
14684 case RID_WCHAR:
14685 type = wchar_type_node;
14686 break;
14687 case RID_BOOL:
14688 type = boolean_type_node;
14689 break;
14690 case RID_SHORT:
14691 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14692 type = short_integer_type_node;
14693 break;
14694 case RID_INT:
14695 if (decl_specs)
14696 decl_specs->explicit_int_p = true;
14697 type = integer_type_node;
14698 break;
14699 case RID_INT128:
14700 if (!int128_integer_type_node)
14701 break;
14702 if (decl_specs)
14703 decl_specs->explicit_int128_p = true;
14704 type = int128_integer_type_node;
14705 break;
14706 case RID_LONG:
14707 if (decl_specs)
14708 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14709 type = long_integer_type_node;
14710 break;
14711 case RID_SIGNED:
14712 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14713 type = integer_type_node;
14714 break;
14715 case RID_UNSIGNED:
14716 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14717 type = unsigned_type_node;
14718 break;
14719 case RID_FLOAT:
14720 type = float_type_node;
14721 break;
14722 case RID_DOUBLE:
14723 type = double_type_node;
14724 break;
14725 case RID_VOID:
14726 type = void_type_node;
14727 break;
14729 case RID_AUTO:
14730 maybe_warn_cpp0x (CPP0X_AUTO);
14731 if (parser->auto_is_implicit_function_template_parm_p)
14733 type = synthesize_implicit_template_parm (parser);
14735 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14737 if (cxx_dialect < cxx14)
14738 pedwarn (location_of (type), 0,
14739 "use of %<auto%> in lambda parameter declaration "
14740 "only available with "
14741 "-std=c++14 or -std=gnu++14");
14743 else if (cxx_dialect < cxx14)
14744 pedwarn (location_of (type), 0,
14745 "use of %<auto%> in parameter declaration "
14746 "only available with "
14747 "-std=c++14 or -std=gnu++14");
14748 else
14749 pedwarn (location_of (type), OPT_Wpedantic,
14750 "ISO C++ forbids use of %<auto%> in parameter "
14751 "declaration");
14753 else
14754 type = make_auto ();
14755 break;
14757 case RID_DECLTYPE:
14758 /* Since DR 743, decltype can either be a simple-type-specifier by
14759 itself or begin a nested-name-specifier. Parsing it will replace
14760 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14761 handling below decide what to do. */
14762 cp_parser_decltype (parser);
14763 cp_lexer_set_token_position (parser->lexer, token);
14764 break;
14766 case RID_TYPEOF:
14767 /* Consume the `typeof' token. */
14768 cp_lexer_consume_token (parser->lexer);
14769 /* Parse the operand to `typeof'. */
14770 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14771 /* If it is not already a TYPE, take its type. */
14772 if (!TYPE_P (type))
14773 type = finish_typeof (type);
14775 if (decl_specs)
14776 cp_parser_set_decl_spec_type (decl_specs, type,
14777 token,
14778 /*type_definition_p=*/false);
14780 return type;
14782 case RID_UNDERLYING_TYPE:
14783 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14784 if (decl_specs)
14785 cp_parser_set_decl_spec_type (decl_specs, type,
14786 token,
14787 /*type_definition_p=*/false);
14789 return type;
14791 case RID_BASES:
14792 case RID_DIRECT_BASES:
14793 type = cp_parser_trait_expr (parser, token->keyword);
14794 if (decl_specs)
14795 cp_parser_set_decl_spec_type (decl_specs, type,
14796 token,
14797 /*type_definition_p=*/false);
14798 return type;
14799 default:
14800 break;
14803 /* If token is an already-parsed decltype not followed by ::,
14804 it's a simple-type-specifier. */
14805 if (token->type == CPP_DECLTYPE
14806 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14808 type = token->u.value;
14809 if (decl_specs)
14810 cp_parser_set_decl_spec_type (decl_specs, type,
14811 token,
14812 /*type_definition_p=*/false);
14813 cp_lexer_consume_token (parser->lexer);
14814 return type;
14817 /* If the type-specifier was for a built-in type, we're done. */
14818 if (type)
14820 /* Record the type. */
14821 if (decl_specs
14822 && (token->keyword != RID_SIGNED
14823 && token->keyword != RID_UNSIGNED
14824 && token->keyword != RID_SHORT
14825 && token->keyword != RID_LONG))
14826 cp_parser_set_decl_spec_type (decl_specs,
14827 type,
14828 token,
14829 /*type_definition_p=*/false);
14830 if (decl_specs)
14831 decl_specs->any_specifiers_p = true;
14833 /* Consume the token. */
14834 cp_lexer_consume_token (parser->lexer);
14836 /* There is no valid C++ program where a non-template type is
14837 followed by a "<". That usually indicates that the user thought
14838 that the type was a template. */
14839 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14840 token->location);
14842 return TYPE_NAME (type);
14845 /* The type-specifier must be a user-defined type. */
14846 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14848 bool qualified_p;
14849 bool global_p;
14851 /* Don't gobble tokens or issue error messages if this is an
14852 optional type-specifier. */
14853 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14854 cp_parser_parse_tentatively (parser);
14856 /* Look for the optional `::' operator. */
14857 global_p
14858 = (cp_parser_global_scope_opt (parser,
14859 /*current_scope_valid_p=*/false)
14860 != NULL_TREE);
14861 /* Look for the nested-name specifier. */
14862 qualified_p
14863 = (cp_parser_nested_name_specifier_opt (parser,
14864 /*typename_keyword_p=*/false,
14865 /*check_dependency_p=*/true,
14866 /*type_p=*/false,
14867 /*is_declaration=*/false)
14868 != NULL_TREE);
14869 token = cp_lexer_peek_token (parser->lexer);
14870 /* If we have seen a nested-name-specifier, and the next token
14871 is `template', then we are using the template-id production. */
14872 if (parser->scope
14873 && cp_parser_optional_template_keyword (parser))
14875 /* Look for the template-id. */
14876 type = cp_parser_template_id (parser,
14877 /*template_keyword_p=*/true,
14878 /*check_dependency_p=*/true,
14879 none_type,
14880 /*is_declaration=*/false);
14881 /* If the template-id did not name a type, we are out of
14882 luck. */
14883 if (TREE_CODE (type) != TYPE_DECL)
14885 cp_parser_error (parser, "expected template-id for type");
14886 type = NULL_TREE;
14889 /* Otherwise, look for a type-name. */
14890 else
14891 type = cp_parser_type_name (parser);
14892 /* Keep track of all name-lookups performed in class scopes. */
14893 if (type
14894 && !global_p
14895 && !qualified_p
14896 && TREE_CODE (type) == TYPE_DECL
14897 && identifier_p (DECL_NAME (type)))
14898 maybe_note_name_used_in_class (DECL_NAME (type), type);
14899 /* If it didn't work out, we don't have a TYPE. */
14900 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14901 && !cp_parser_parse_definitely (parser))
14902 type = NULL_TREE;
14903 if (type && decl_specs)
14904 cp_parser_set_decl_spec_type (decl_specs, type,
14905 token,
14906 /*type_definition_p=*/false);
14909 /* If we didn't get a type-name, issue an error message. */
14910 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14912 cp_parser_error (parser, "expected type-name");
14913 return error_mark_node;
14916 if (type && type != error_mark_node)
14918 /* See if TYPE is an Objective-C type, and if so, parse and
14919 accept any protocol references following it. Do this before
14920 the cp_parser_check_for_invalid_template_id() call, because
14921 Objective-C types can be followed by '<...>' which would
14922 enclose protocol names rather than template arguments, and so
14923 everything is fine. */
14924 if (c_dialect_objc () && !parser->scope
14925 && (objc_is_id (type) || objc_is_class_name (type)))
14927 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14928 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14930 /* Clobber the "unqualified" type previously entered into
14931 DECL_SPECS with the new, improved protocol-qualified version. */
14932 if (decl_specs)
14933 decl_specs->type = qual_type;
14935 return qual_type;
14938 /* There is no valid C++ program where a non-template type is
14939 followed by a "<". That usually indicates that the user
14940 thought that the type was a template. */
14941 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14942 none_type,
14943 token->location);
14946 return type;
14949 /* Parse a type-name.
14951 type-name:
14952 class-name
14953 enum-name
14954 typedef-name
14955 simple-template-id [in c++0x]
14957 enum-name:
14958 identifier
14960 typedef-name:
14961 identifier
14963 Returns a TYPE_DECL for the type. */
14965 static tree
14966 cp_parser_type_name (cp_parser* parser)
14968 tree type_decl;
14970 /* We can't know yet whether it is a class-name or not. */
14971 cp_parser_parse_tentatively (parser);
14972 /* Try a class-name. */
14973 type_decl = cp_parser_class_name (parser,
14974 /*typename_keyword_p=*/false,
14975 /*template_keyword_p=*/false,
14976 none_type,
14977 /*check_dependency_p=*/true,
14978 /*class_head_p=*/false,
14979 /*is_declaration=*/false);
14980 /* If it's not a class-name, keep looking. */
14981 if (!cp_parser_parse_definitely (parser))
14983 if (cxx_dialect < cxx11)
14984 /* It must be a typedef-name or an enum-name. */
14985 return cp_parser_nonclass_name (parser);
14987 cp_parser_parse_tentatively (parser);
14988 /* It is either a simple-template-id representing an
14989 instantiation of an alias template... */
14990 type_decl = cp_parser_template_id (parser,
14991 /*template_keyword_p=*/false,
14992 /*check_dependency_p=*/true,
14993 none_type,
14994 /*is_declaration=*/false);
14995 /* Note that this must be an instantiation of an alias template
14996 because [temp.names]/6 says:
14998 A template-id that names an alias template specialization
14999 is a type-name.
15001 Whereas [temp.names]/7 says:
15003 A simple-template-id that names a class template
15004 specialization is a class-name. */
15005 if (type_decl != NULL_TREE
15006 && TREE_CODE (type_decl) == TYPE_DECL
15007 && TYPE_DECL_ALIAS_P (type_decl))
15008 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15009 else
15010 cp_parser_simulate_error (parser);
15012 if (!cp_parser_parse_definitely (parser))
15013 /* ... Or a typedef-name or an enum-name. */
15014 return cp_parser_nonclass_name (parser);
15017 return type_decl;
15020 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15022 enum-name:
15023 identifier
15025 typedef-name:
15026 identifier
15028 Returns a TYPE_DECL for the type. */
15030 static tree
15031 cp_parser_nonclass_name (cp_parser* parser)
15033 tree type_decl;
15034 tree identifier;
15036 cp_token *token = cp_lexer_peek_token (parser->lexer);
15037 identifier = cp_parser_identifier (parser);
15038 if (identifier == error_mark_node)
15039 return error_mark_node;
15041 /* Look up the type-name. */
15042 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15044 type_decl = strip_using_decl (type_decl);
15046 if (TREE_CODE (type_decl) != TYPE_DECL
15047 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15049 /* See if this is an Objective-C type. */
15050 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15051 tree type = objc_get_protocol_qualified_type (identifier, protos);
15052 if (type)
15053 type_decl = TYPE_NAME (type);
15056 /* Issue an error if we did not find a type-name. */
15057 if (TREE_CODE (type_decl) != TYPE_DECL
15058 /* In Objective-C, we have the complication that class names are
15059 normally type names and start declarations (eg, the
15060 "NSObject" in "NSObject *object;"), but can be used in an
15061 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15062 is an expression. So, a classname followed by a dot is not a
15063 valid type-name. */
15064 || (objc_is_class_name (TREE_TYPE (type_decl))
15065 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15067 if (!cp_parser_simulate_error (parser))
15068 cp_parser_name_lookup_error (parser, identifier, type_decl,
15069 NLE_TYPE, token->location);
15070 return error_mark_node;
15072 /* Remember that the name was used in the definition of the
15073 current class so that we can check later to see if the
15074 meaning would have been different after the class was
15075 entirely defined. */
15076 else if (type_decl != error_mark_node
15077 && !parser->scope)
15078 maybe_note_name_used_in_class (identifier, type_decl);
15080 return type_decl;
15083 /* Parse an elaborated-type-specifier. Note that the grammar given
15084 here incorporates the resolution to DR68.
15086 elaborated-type-specifier:
15087 class-key :: [opt] nested-name-specifier [opt] identifier
15088 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15089 enum-key :: [opt] nested-name-specifier [opt] identifier
15090 typename :: [opt] nested-name-specifier identifier
15091 typename :: [opt] nested-name-specifier template [opt]
15092 template-id
15094 GNU extension:
15096 elaborated-type-specifier:
15097 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15098 class-key attributes :: [opt] nested-name-specifier [opt]
15099 template [opt] template-id
15100 enum attributes :: [opt] nested-name-specifier [opt] identifier
15102 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15103 declared `friend'. If IS_DECLARATION is TRUE, then this
15104 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15105 something is being declared.
15107 Returns the TYPE specified. */
15109 static tree
15110 cp_parser_elaborated_type_specifier (cp_parser* parser,
15111 bool is_friend,
15112 bool is_declaration)
15114 enum tag_types tag_type;
15115 tree identifier;
15116 tree type = NULL_TREE;
15117 tree attributes = NULL_TREE;
15118 tree globalscope;
15119 cp_token *token = NULL;
15121 /* See if we're looking at the `enum' keyword. */
15122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15124 /* Consume the `enum' token. */
15125 cp_lexer_consume_token (parser->lexer);
15126 /* Remember that it's an enumeration type. */
15127 tag_type = enum_type;
15128 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15129 enums) is used here. */
15130 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15131 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15133 pedwarn (input_location, 0, "elaborated-type-specifier "
15134 "for a scoped enum must not use the %<%D%> keyword",
15135 cp_lexer_peek_token (parser->lexer)->u.value);
15136 /* Consume the `struct' or `class' and parse it anyway. */
15137 cp_lexer_consume_token (parser->lexer);
15139 /* Parse the attributes. */
15140 attributes = cp_parser_attributes_opt (parser);
15142 /* Or, it might be `typename'. */
15143 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15144 RID_TYPENAME))
15146 /* Consume the `typename' token. */
15147 cp_lexer_consume_token (parser->lexer);
15148 /* Remember that it's a `typename' type. */
15149 tag_type = typename_type;
15151 /* Otherwise it must be a class-key. */
15152 else
15154 tag_type = cp_parser_class_key (parser);
15155 if (tag_type == none_type)
15156 return error_mark_node;
15157 /* Parse the attributes. */
15158 attributes = cp_parser_attributes_opt (parser);
15161 /* Look for the `::' operator. */
15162 globalscope = cp_parser_global_scope_opt (parser,
15163 /*current_scope_valid_p=*/false);
15164 /* Look for the nested-name-specifier. */
15165 if (tag_type == typename_type && !globalscope)
15167 if (!cp_parser_nested_name_specifier (parser,
15168 /*typename_keyword_p=*/true,
15169 /*check_dependency_p=*/true,
15170 /*type_p=*/true,
15171 is_declaration))
15172 return error_mark_node;
15174 else
15175 /* Even though `typename' is not present, the proposed resolution
15176 to Core Issue 180 says that in `class A<T>::B', `B' should be
15177 considered a type-name, even if `A<T>' is dependent. */
15178 cp_parser_nested_name_specifier_opt (parser,
15179 /*typename_keyword_p=*/true,
15180 /*check_dependency_p=*/true,
15181 /*type_p=*/true,
15182 is_declaration);
15183 /* For everything but enumeration types, consider a template-id.
15184 For an enumeration type, consider only a plain identifier. */
15185 if (tag_type != enum_type)
15187 bool template_p = false;
15188 tree decl;
15190 /* Allow the `template' keyword. */
15191 template_p = cp_parser_optional_template_keyword (parser);
15192 /* If we didn't see `template', we don't know if there's a
15193 template-id or not. */
15194 if (!template_p)
15195 cp_parser_parse_tentatively (parser);
15196 /* Parse the template-id. */
15197 token = cp_lexer_peek_token (parser->lexer);
15198 decl = cp_parser_template_id (parser, template_p,
15199 /*check_dependency_p=*/true,
15200 tag_type,
15201 is_declaration);
15202 /* If we didn't find a template-id, look for an ordinary
15203 identifier. */
15204 if (!template_p && !cp_parser_parse_definitely (parser))
15206 /* We can get here when cp_parser_template_id, called by
15207 cp_parser_class_name with tag_type == none_type, succeeds
15208 and caches a BASELINK. Then, when called again here,
15209 instead of failing and returning an error_mark_node
15210 returns it (see template/typename17.C in C++11).
15211 ??? Could we diagnose this earlier? */
15212 else if (tag_type == typename_type && BASELINK_P (decl))
15214 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15215 type = error_mark_node;
15217 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15218 in effect, then we must assume that, upon instantiation, the
15219 template will correspond to a class. */
15220 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15221 && tag_type == typename_type)
15222 type = make_typename_type (parser->scope, decl,
15223 typename_type,
15224 /*complain=*/tf_error);
15225 /* If the `typename' keyword is in effect and DECL is not a type
15226 decl, then type is non existent. */
15227 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15229 else if (TREE_CODE (decl) == TYPE_DECL)
15230 type = check_elaborated_type_specifier (tag_type, decl,
15231 /*allow_template_p=*/true);
15232 else if (decl == error_mark_node)
15233 type = error_mark_node;
15236 if (!type)
15238 token = cp_lexer_peek_token (parser->lexer);
15239 identifier = cp_parser_identifier (parser);
15241 if (identifier == error_mark_node)
15243 parser->scope = NULL_TREE;
15244 return error_mark_node;
15247 /* For a `typename', we needn't call xref_tag. */
15248 if (tag_type == typename_type
15249 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15250 return cp_parser_make_typename_type (parser, identifier,
15251 token->location);
15253 /* Template parameter lists apply only if we are not within a
15254 function parameter list. */
15255 bool template_parm_lists_apply
15256 = parser->num_template_parameter_lists;
15257 if (template_parm_lists_apply)
15258 for (cp_binding_level *s = current_binding_level;
15259 s && s->kind != sk_template_parms;
15260 s = s->level_chain)
15261 if (s->kind == sk_function_parms)
15262 template_parm_lists_apply = false;
15264 /* Look up a qualified name in the usual way. */
15265 if (parser->scope)
15267 tree decl;
15268 tree ambiguous_decls;
15270 decl = cp_parser_lookup_name (parser, identifier,
15271 tag_type,
15272 /*is_template=*/false,
15273 /*is_namespace=*/false,
15274 /*check_dependency=*/true,
15275 &ambiguous_decls,
15276 token->location);
15278 /* If the lookup was ambiguous, an error will already have been
15279 issued. */
15280 if (ambiguous_decls)
15281 return error_mark_node;
15283 /* If we are parsing friend declaration, DECL may be a
15284 TEMPLATE_DECL tree node here. However, we need to check
15285 whether this TEMPLATE_DECL results in valid code. Consider
15286 the following example:
15288 namespace N {
15289 template <class T> class C {};
15291 class X {
15292 template <class T> friend class N::C; // #1, valid code
15294 template <class T> class Y {
15295 friend class N::C; // #2, invalid code
15298 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15299 name lookup of `N::C'. We see that friend declaration must
15300 be template for the code to be valid. Note that
15301 processing_template_decl does not work here since it is
15302 always 1 for the above two cases. */
15304 decl = (cp_parser_maybe_treat_template_as_class
15305 (decl, /*tag_name_p=*/is_friend
15306 && template_parm_lists_apply));
15308 if (TREE_CODE (decl) != TYPE_DECL)
15310 cp_parser_diagnose_invalid_type_name (parser,
15311 identifier,
15312 token->location);
15313 return error_mark_node;
15316 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15318 bool allow_template = (template_parm_lists_apply
15319 || DECL_SELF_REFERENCE_P (decl));
15320 type = check_elaborated_type_specifier (tag_type, decl,
15321 allow_template);
15323 if (type == error_mark_node)
15324 return error_mark_node;
15327 /* Forward declarations of nested types, such as
15329 class C1::C2;
15330 class C1::C2::C3;
15332 are invalid unless all components preceding the final '::'
15333 are complete. If all enclosing types are complete, these
15334 declarations become merely pointless.
15336 Invalid forward declarations of nested types are errors
15337 caught elsewhere in parsing. Those that are pointless arrive
15338 here. */
15340 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15341 && !is_friend && !processing_explicit_instantiation)
15342 warning (0, "declaration %qD does not declare anything", decl);
15344 type = TREE_TYPE (decl);
15346 else
15348 /* An elaborated-type-specifier sometimes introduces a new type and
15349 sometimes names an existing type. Normally, the rule is that it
15350 introduces a new type only if there is not an existing type of
15351 the same name already in scope. For example, given:
15353 struct S {};
15354 void f() { struct S s; }
15356 the `struct S' in the body of `f' is the same `struct S' as in
15357 the global scope; the existing definition is used. However, if
15358 there were no global declaration, this would introduce a new
15359 local class named `S'.
15361 An exception to this rule applies to the following code:
15363 namespace N { struct S; }
15365 Here, the elaborated-type-specifier names a new type
15366 unconditionally; even if there is already an `S' in the
15367 containing scope this declaration names a new type.
15368 This exception only applies if the elaborated-type-specifier
15369 forms the complete declaration:
15371 [class.name]
15373 A declaration consisting solely of `class-key identifier ;' is
15374 either a redeclaration of the name in the current scope or a
15375 forward declaration of the identifier as a class name. It
15376 introduces the name into the current scope.
15378 We are in this situation precisely when the next token is a `;'.
15380 An exception to the exception is that a `friend' declaration does
15381 *not* name a new type; i.e., given:
15383 struct S { friend struct T; };
15385 `T' is not a new type in the scope of `S'.
15387 Also, `new struct S' or `sizeof (struct S)' never results in the
15388 definition of a new type; a new type can only be declared in a
15389 declaration context. */
15391 tag_scope ts;
15392 bool template_p;
15394 if (is_friend)
15395 /* Friends have special name lookup rules. */
15396 ts = ts_within_enclosing_non_class;
15397 else if (is_declaration
15398 && cp_lexer_next_token_is (parser->lexer,
15399 CPP_SEMICOLON))
15400 /* This is a `class-key identifier ;' */
15401 ts = ts_current;
15402 else
15403 ts = ts_global;
15405 template_p =
15406 (template_parm_lists_apply
15407 && (cp_parser_next_token_starts_class_definition_p (parser)
15408 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15409 /* An unqualified name was used to reference this type, so
15410 there were no qualifying templates. */
15411 if (template_parm_lists_apply
15412 && !cp_parser_check_template_parameters (parser,
15413 /*num_templates=*/0,
15414 token->location,
15415 /*declarator=*/NULL))
15416 return error_mark_node;
15417 type = xref_tag (tag_type, identifier, ts, template_p);
15421 if (type == error_mark_node)
15422 return error_mark_node;
15424 /* Allow attributes on forward declarations of classes. */
15425 if (attributes)
15427 if (TREE_CODE (type) == TYPENAME_TYPE)
15428 warning (OPT_Wattributes,
15429 "attributes ignored on uninstantiated type");
15430 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15431 && ! processing_explicit_instantiation)
15432 warning (OPT_Wattributes,
15433 "attributes ignored on template instantiation");
15434 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15435 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15436 else
15437 warning (OPT_Wattributes,
15438 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15441 if (tag_type != enum_type)
15443 /* Indicate whether this class was declared as a `class' or as a
15444 `struct'. */
15445 if (TREE_CODE (type) == RECORD_TYPE)
15446 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15447 cp_parser_check_class_key (tag_type, type);
15450 /* A "<" cannot follow an elaborated type specifier. If that
15451 happens, the user was probably trying to form a template-id. */
15452 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15453 token->location);
15455 return type;
15458 /* Parse an enum-specifier.
15460 enum-specifier:
15461 enum-head { enumerator-list [opt] }
15462 enum-head { enumerator-list , } [C++0x]
15464 enum-head:
15465 enum-key identifier [opt] enum-base [opt]
15466 enum-key nested-name-specifier identifier enum-base [opt]
15468 enum-key:
15469 enum
15470 enum class [C++0x]
15471 enum struct [C++0x]
15473 enum-base: [C++0x]
15474 : type-specifier-seq
15476 opaque-enum-specifier:
15477 enum-key identifier enum-base [opt] ;
15479 GNU Extensions:
15480 enum-key attributes[opt] identifier [opt] enum-base [opt]
15481 { enumerator-list [opt] }attributes[opt]
15482 enum-key attributes[opt] identifier [opt] enum-base [opt]
15483 { enumerator-list, }attributes[opt] [C++0x]
15485 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15486 if the token stream isn't an enum-specifier after all. */
15488 static tree
15489 cp_parser_enum_specifier (cp_parser* parser)
15491 tree identifier;
15492 tree type = NULL_TREE;
15493 tree prev_scope;
15494 tree nested_name_specifier = NULL_TREE;
15495 tree attributes;
15496 bool scoped_enum_p = false;
15497 bool has_underlying_type = false;
15498 bool nested_being_defined = false;
15499 bool new_value_list = false;
15500 bool is_new_type = false;
15501 bool is_anonymous = false;
15502 tree underlying_type = NULL_TREE;
15503 cp_token *type_start_token = NULL;
15504 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15506 parser->colon_corrects_to_scope_p = false;
15508 /* Parse tentatively so that we can back up if we don't find a
15509 enum-specifier. */
15510 cp_parser_parse_tentatively (parser);
15512 /* Caller guarantees that the current token is 'enum', an identifier
15513 possibly follows, and the token after that is an opening brace.
15514 If we don't have an identifier, fabricate an anonymous name for
15515 the enumeration being defined. */
15516 cp_lexer_consume_token (parser->lexer);
15518 /* Parse the "class" or "struct", which indicates a scoped
15519 enumeration type in C++0x. */
15520 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15521 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15523 if (cxx_dialect < cxx11)
15524 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15526 /* Consume the `struct' or `class' token. */
15527 cp_lexer_consume_token (parser->lexer);
15529 scoped_enum_p = true;
15532 attributes = cp_parser_attributes_opt (parser);
15534 /* Clear the qualification. */
15535 parser->scope = NULL_TREE;
15536 parser->qualifying_scope = NULL_TREE;
15537 parser->object_scope = NULL_TREE;
15539 /* Figure out in what scope the declaration is being placed. */
15540 prev_scope = current_scope ();
15542 type_start_token = cp_lexer_peek_token (parser->lexer);
15544 push_deferring_access_checks (dk_no_check);
15545 nested_name_specifier
15546 = cp_parser_nested_name_specifier_opt (parser,
15547 /*typename_keyword_p=*/true,
15548 /*check_dependency_p=*/false,
15549 /*type_p=*/false,
15550 /*is_declaration=*/false);
15552 if (nested_name_specifier)
15554 tree name;
15556 identifier = cp_parser_identifier (parser);
15557 name = cp_parser_lookup_name (parser, identifier,
15558 enum_type,
15559 /*is_template=*/false,
15560 /*is_namespace=*/false,
15561 /*check_dependency=*/true,
15562 /*ambiguous_decls=*/NULL,
15563 input_location);
15564 if (name && name != error_mark_node)
15566 type = TREE_TYPE (name);
15567 if (TREE_CODE (type) == TYPENAME_TYPE)
15569 /* Are template enums allowed in ISO? */
15570 if (template_parm_scope_p ())
15571 pedwarn (type_start_token->location, OPT_Wpedantic,
15572 "%qD is an enumeration template", name);
15573 /* ignore a typename reference, for it will be solved by name
15574 in start_enum. */
15575 type = NULL_TREE;
15578 else if (nested_name_specifier == error_mark_node)
15579 /* We already issued an error. */;
15580 else
15581 error_at (type_start_token->location,
15582 "%qD is not an enumerator-name", identifier);
15584 else
15586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15587 identifier = cp_parser_identifier (parser);
15588 else
15590 identifier = make_anon_name ();
15591 is_anonymous = true;
15592 if (scoped_enum_p)
15593 error_at (type_start_token->location,
15594 "anonymous scoped enum is not allowed");
15597 pop_deferring_access_checks ();
15599 /* Check for the `:' that denotes a specified underlying type in C++0x.
15600 Note that a ':' could also indicate a bitfield width, however. */
15601 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15603 cp_decl_specifier_seq type_specifiers;
15605 /* Consume the `:'. */
15606 cp_lexer_consume_token (parser->lexer);
15608 /* Parse the type-specifier-seq. */
15609 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15610 /*is_trailing_return=*/false,
15611 &type_specifiers);
15613 /* At this point this is surely not elaborated type specifier. */
15614 if (!cp_parser_parse_definitely (parser))
15615 return NULL_TREE;
15617 if (cxx_dialect < cxx11)
15618 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15620 has_underlying_type = true;
15622 /* If that didn't work, stop. */
15623 if (type_specifiers.type != error_mark_node)
15625 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15626 /*initialized=*/0, NULL);
15627 if (underlying_type == error_mark_node
15628 || check_for_bare_parameter_packs (underlying_type))
15629 underlying_type = NULL_TREE;
15633 /* Look for the `{' but don't consume it yet. */
15634 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15636 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15638 cp_parser_error (parser, "expected %<{%>");
15639 if (has_underlying_type)
15641 type = NULL_TREE;
15642 goto out;
15645 /* An opaque-enum-specifier must have a ';' here. */
15646 if ((scoped_enum_p || underlying_type)
15647 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15649 cp_parser_error (parser, "expected %<;%> or %<{%>");
15650 if (has_underlying_type)
15652 type = NULL_TREE;
15653 goto out;
15658 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15659 return NULL_TREE;
15661 if (nested_name_specifier)
15663 if (CLASS_TYPE_P (nested_name_specifier))
15665 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15666 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15667 push_scope (nested_name_specifier);
15669 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15671 push_nested_namespace (nested_name_specifier);
15675 /* Issue an error message if type-definitions are forbidden here. */
15676 if (!cp_parser_check_type_definition (parser))
15677 type = error_mark_node;
15678 else
15679 /* Create the new type. We do this before consuming the opening
15680 brace so the enum will be recorded as being on the line of its
15681 tag (or the 'enum' keyword, if there is no tag). */
15682 type = start_enum (identifier, type, underlying_type,
15683 scoped_enum_p, &is_new_type);
15685 /* If the next token is not '{' it is an opaque-enum-specifier or an
15686 elaborated-type-specifier. */
15687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15689 timevar_push (TV_PARSE_ENUM);
15690 if (nested_name_specifier
15691 && nested_name_specifier != error_mark_node)
15693 /* The following catches invalid code such as:
15694 enum class S<int>::E { A, B, C }; */
15695 if (!processing_specialization
15696 && CLASS_TYPE_P (nested_name_specifier)
15697 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15698 error_at (type_start_token->location, "cannot add an enumerator "
15699 "list to a template instantiation");
15701 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15703 error_at (type_start_token->location,
15704 "%<%T::%E%> has not been declared",
15705 TYPE_CONTEXT (nested_name_specifier),
15706 nested_name_specifier);
15707 type = error_mark_node;
15709 /* If that scope does not contain the scope in which the
15710 class was originally declared, the program is invalid. */
15711 else if (prev_scope && !is_ancestor (prev_scope,
15712 nested_name_specifier))
15714 if (at_namespace_scope_p ())
15715 error_at (type_start_token->location,
15716 "declaration of %qD in namespace %qD which does not "
15717 "enclose %qD",
15718 type, prev_scope, nested_name_specifier);
15719 else
15720 error_at (type_start_token->location,
15721 "declaration of %qD in %qD which does not "
15722 "enclose %qD",
15723 type, prev_scope, nested_name_specifier);
15724 type = error_mark_node;
15728 if (scoped_enum_p)
15729 begin_scope (sk_scoped_enum, type);
15731 /* Consume the opening brace. */
15732 cp_lexer_consume_token (parser->lexer);
15734 if (type == error_mark_node)
15735 ; /* Nothing to add */
15736 else if (OPAQUE_ENUM_P (type)
15737 || (cxx_dialect > cxx98 && processing_specialization))
15739 new_value_list = true;
15740 SET_OPAQUE_ENUM_P (type, false);
15741 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15743 else
15745 error_at (type_start_token->location,
15746 "multiple definition of %q#T", type);
15747 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15748 "previous definition here");
15749 type = error_mark_node;
15752 if (type == error_mark_node)
15753 cp_parser_skip_to_end_of_block_or_statement (parser);
15754 /* If the next token is not '}', then there are some enumerators. */
15755 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15757 if (is_anonymous && !scoped_enum_p)
15758 pedwarn (type_start_token->location, OPT_Wpedantic,
15759 "ISO C++ forbids empty anonymous enum");
15761 else
15762 cp_parser_enumerator_list (parser, type);
15764 /* Consume the final '}'. */
15765 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15767 if (scoped_enum_p)
15768 finish_scope ();
15769 timevar_pop (TV_PARSE_ENUM);
15771 else
15773 /* If a ';' follows, then it is an opaque-enum-specifier
15774 and additional restrictions apply. */
15775 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15777 if (is_anonymous)
15778 error_at (type_start_token->location,
15779 "opaque-enum-specifier without name");
15780 else if (nested_name_specifier)
15781 error_at (type_start_token->location,
15782 "opaque-enum-specifier must use a simple identifier");
15786 /* Look for trailing attributes to apply to this enumeration, and
15787 apply them if appropriate. */
15788 if (cp_parser_allow_gnu_extensions_p (parser))
15790 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15791 trailing_attr = chainon (trailing_attr, attributes);
15792 cplus_decl_attributes (&type,
15793 trailing_attr,
15794 (int) ATTR_FLAG_TYPE_IN_PLACE);
15797 /* Finish up the enumeration. */
15798 if (type != error_mark_node)
15800 if (new_value_list)
15801 finish_enum_value_list (type);
15802 if (is_new_type)
15803 finish_enum (type);
15806 if (nested_name_specifier)
15808 if (CLASS_TYPE_P (nested_name_specifier))
15810 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15811 pop_scope (nested_name_specifier);
15813 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15815 pop_nested_namespace (nested_name_specifier);
15818 out:
15819 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15820 return type;
15823 /* Parse an enumerator-list. The enumerators all have the indicated
15824 TYPE.
15826 enumerator-list:
15827 enumerator-definition
15828 enumerator-list , enumerator-definition */
15830 static void
15831 cp_parser_enumerator_list (cp_parser* parser, tree type)
15833 while (true)
15835 /* Parse an enumerator-definition. */
15836 cp_parser_enumerator_definition (parser, type);
15838 /* If the next token is not a ',', we've reached the end of
15839 the list. */
15840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15841 break;
15842 /* Otherwise, consume the `,' and keep going. */
15843 cp_lexer_consume_token (parser->lexer);
15844 /* If the next token is a `}', there is a trailing comma. */
15845 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15847 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15848 pedwarn (input_location, OPT_Wpedantic,
15849 "comma at end of enumerator list");
15850 break;
15855 /* Parse an enumerator-definition. The enumerator has the indicated
15856 TYPE.
15858 enumerator-definition:
15859 enumerator
15860 enumerator = constant-expression
15862 enumerator:
15863 identifier */
15865 static void
15866 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15868 tree identifier;
15869 tree value;
15870 location_t loc;
15872 /* Save the input location because we are interested in the location
15873 of the identifier and not the location of the explicit value. */
15874 loc = cp_lexer_peek_token (parser->lexer)->location;
15876 /* Look for the identifier. */
15877 identifier = cp_parser_identifier (parser);
15878 if (identifier == error_mark_node)
15879 return;
15881 /* If the next token is an '=', then there is an explicit value. */
15882 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15884 /* Consume the `=' token. */
15885 cp_lexer_consume_token (parser->lexer);
15886 /* Parse the value. */
15887 value = cp_parser_constant_expression (parser,
15888 /*allow_non_constant_p=*/false,
15889 NULL);
15891 else
15892 value = NULL_TREE;
15894 /* If we are processing a template, make sure the initializer of the
15895 enumerator doesn't contain any bare template parameter pack. */
15896 if (check_for_bare_parameter_packs (value))
15897 value = error_mark_node;
15899 /* integral_constant_value will pull out this expression, so make sure
15900 it's folded as appropriate. */
15901 value = fold_non_dependent_expr (value);
15903 /* Create the enumerator. */
15904 build_enumerator (identifier, value, type, loc);
15907 /* Parse a namespace-name.
15909 namespace-name:
15910 original-namespace-name
15911 namespace-alias
15913 Returns the NAMESPACE_DECL for the namespace. */
15915 static tree
15916 cp_parser_namespace_name (cp_parser* parser)
15918 tree identifier;
15919 tree namespace_decl;
15921 cp_token *token = cp_lexer_peek_token (parser->lexer);
15923 /* Get the name of the namespace. */
15924 identifier = cp_parser_identifier (parser);
15925 if (identifier == error_mark_node)
15926 return error_mark_node;
15928 /* Look up the identifier in the currently active scope. Look only
15929 for namespaces, due to:
15931 [basic.lookup.udir]
15933 When looking up a namespace-name in a using-directive or alias
15934 definition, only namespace names are considered.
15936 And:
15938 [basic.lookup.qual]
15940 During the lookup of a name preceding the :: scope resolution
15941 operator, object, function, and enumerator names are ignored.
15943 (Note that cp_parser_qualifying_entity only calls this
15944 function if the token after the name is the scope resolution
15945 operator.) */
15946 namespace_decl = cp_parser_lookup_name (parser, identifier,
15947 none_type,
15948 /*is_template=*/false,
15949 /*is_namespace=*/true,
15950 /*check_dependency=*/true,
15951 /*ambiguous_decls=*/NULL,
15952 token->location);
15953 /* If it's not a namespace, issue an error. */
15954 if (namespace_decl == error_mark_node
15955 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15957 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15958 error_at (token->location, "%qD is not a namespace-name", identifier);
15959 cp_parser_error (parser, "expected namespace-name");
15960 namespace_decl = error_mark_node;
15963 return namespace_decl;
15966 /* Parse a namespace-definition.
15968 namespace-definition:
15969 named-namespace-definition
15970 unnamed-namespace-definition
15972 named-namespace-definition:
15973 original-namespace-definition
15974 extension-namespace-definition
15976 original-namespace-definition:
15977 namespace identifier { namespace-body }
15979 extension-namespace-definition:
15980 namespace original-namespace-name { namespace-body }
15982 unnamed-namespace-definition:
15983 namespace { namespace-body } */
15985 static void
15986 cp_parser_namespace_definition (cp_parser* parser)
15988 tree identifier, attribs;
15989 bool has_visibility;
15990 bool is_inline;
15992 cp_ensure_no_omp_declare_simd (parser);
15993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15995 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15996 is_inline = true;
15997 cp_lexer_consume_token (parser->lexer);
15999 else
16000 is_inline = false;
16002 /* Look for the `namespace' keyword. */
16003 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16005 /* Get the name of the namespace. We do not attempt to distinguish
16006 between an original-namespace-definition and an
16007 extension-namespace-definition at this point. The semantic
16008 analysis routines are responsible for that. */
16009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16010 identifier = cp_parser_identifier (parser);
16011 else
16012 identifier = NULL_TREE;
16014 /* Parse any specified attributes. */
16015 attribs = cp_parser_attributes_opt (parser);
16017 /* Look for the `{' to start the namespace. */
16018 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16019 /* Start the namespace. */
16020 push_namespace (identifier);
16022 /* "inline namespace" is equivalent to a stub namespace definition
16023 followed by a strong using directive. */
16024 if (is_inline)
16026 tree name_space = current_namespace;
16027 /* Set up namespace association. */
16028 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16029 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16030 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16031 /* Import the contents of the inline namespace. */
16032 pop_namespace ();
16033 do_using_directive (name_space);
16034 push_namespace (identifier);
16037 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16039 /* Parse the body of the namespace. */
16040 cp_parser_namespace_body (parser);
16042 if (has_visibility)
16043 pop_visibility (1);
16045 /* Finish the namespace. */
16046 pop_namespace ();
16047 /* Look for the final `}'. */
16048 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16051 /* Parse a namespace-body.
16053 namespace-body:
16054 declaration-seq [opt] */
16056 static void
16057 cp_parser_namespace_body (cp_parser* parser)
16059 cp_parser_declaration_seq_opt (parser);
16062 /* Parse a namespace-alias-definition.
16064 namespace-alias-definition:
16065 namespace identifier = qualified-namespace-specifier ; */
16067 static void
16068 cp_parser_namespace_alias_definition (cp_parser* parser)
16070 tree identifier;
16071 tree namespace_specifier;
16073 cp_token *token = cp_lexer_peek_token (parser->lexer);
16075 /* Look for the `namespace' keyword. */
16076 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16077 /* Look for the identifier. */
16078 identifier = cp_parser_identifier (parser);
16079 if (identifier == error_mark_node)
16080 return;
16081 /* Look for the `=' token. */
16082 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16083 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16085 error_at (token->location, "%<namespace%> definition is not allowed here");
16086 /* Skip the definition. */
16087 cp_lexer_consume_token (parser->lexer);
16088 if (cp_parser_skip_to_closing_brace (parser))
16089 cp_lexer_consume_token (parser->lexer);
16090 return;
16092 cp_parser_require (parser, CPP_EQ, RT_EQ);
16093 /* Look for the qualified-namespace-specifier. */
16094 namespace_specifier
16095 = cp_parser_qualified_namespace_specifier (parser);
16096 /* Look for the `;' token. */
16097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16099 /* Register the alias in the symbol table. */
16100 do_namespace_alias (identifier, namespace_specifier);
16103 /* Parse a qualified-namespace-specifier.
16105 qualified-namespace-specifier:
16106 :: [opt] nested-name-specifier [opt] namespace-name
16108 Returns a NAMESPACE_DECL corresponding to the specified
16109 namespace. */
16111 static tree
16112 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16114 /* Look for the optional `::'. */
16115 cp_parser_global_scope_opt (parser,
16116 /*current_scope_valid_p=*/false);
16118 /* Look for the optional nested-name-specifier. */
16119 cp_parser_nested_name_specifier_opt (parser,
16120 /*typename_keyword_p=*/false,
16121 /*check_dependency_p=*/true,
16122 /*type_p=*/false,
16123 /*is_declaration=*/true);
16125 return cp_parser_namespace_name (parser);
16128 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16129 access declaration.
16131 using-declaration:
16132 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16133 using :: unqualified-id ;
16135 access-declaration:
16136 qualified-id ;
16140 static bool
16141 cp_parser_using_declaration (cp_parser* parser,
16142 bool access_declaration_p)
16144 cp_token *token;
16145 bool typename_p = false;
16146 bool global_scope_p;
16147 tree decl;
16148 tree identifier;
16149 tree qscope;
16150 int oldcount = errorcount;
16151 cp_token *diag_token = NULL;
16153 if (access_declaration_p)
16155 diag_token = cp_lexer_peek_token (parser->lexer);
16156 cp_parser_parse_tentatively (parser);
16158 else
16160 /* Look for the `using' keyword. */
16161 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16163 /* Peek at the next token. */
16164 token = cp_lexer_peek_token (parser->lexer);
16165 /* See if it's `typename'. */
16166 if (token->keyword == RID_TYPENAME)
16168 /* Remember that we've seen it. */
16169 typename_p = true;
16170 /* Consume the `typename' token. */
16171 cp_lexer_consume_token (parser->lexer);
16175 /* Look for the optional global scope qualification. */
16176 global_scope_p
16177 = (cp_parser_global_scope_opt (parser,
16178 /*current_scope_valid_p=*/false)
16179 != NULL_TREE);
16181 /* If we saw `typename', or didn't see `::', then there must be a
16182 nested-name-specifier present. */
16183 if (typename_p || !global_scope_p)
16185 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16186 /*check_dependency_p=*/true,
16187 /*type_p=*/false,
16188 /*is_declaration=*/true);
16189 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16191 cp_parser_skip_to_end_of_block_or_statement (parser);
16192 return false;
16195 /* Otherwise, we could be in either of the two productions. In that
16196 case, treat the nested-name-specifier as optional. */
16197 else
16198 qscope = cp_parser_nested_name_specifier_opt (parser,
16199 /*typename_keyword_p=*/false,
16200 /*check_dependency_p=*/true,
16201 /*type_p=*/false,
16202 /*is_declaration=*/true);
16203 if (!qscope)
16204 qscope = global_namespace;
16205 else if (UNSCOPED_ENUM_P (qscope))
16206 qscope = CP_TYPE_CONTEXT (qscope);
16208 if (access_declaration_p && cp_parser_error_occurred (parser))
16209 /* Something has already gone wrong; there's no need to parse
16210 further. Since an error has occurred, the return value of
16211 cp_parser_parse_definitely will be false, as required. */
16212 return cp_parser_parse_definitely (parser);
16214 token = cp_lexer_peek_token (parser->lexer);
16215 /* Parse the unqualified-id. */
16216 identifier = cp_parser_unqualified_id (parser,
16217 /*template_keyword_p=*/false,
16218 /*check_dependency_p=*/true,
16219 /*declarator_p=*/true,
16220 /*optional_p=*/false);
16222 if (access_declaration_p)
16224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16225 cp_parser_simulate_error (parser);
16226 if (!cp_parser_parse_definitely (parser))
16227 return false;
16230 /* The function we call to handle a using-declaration is different
16231 depending on what scope we are in. */
16232 if (qscope == error_mark_node || identifier == error_mark_node)
16234 else if (!identifier_p (identifier)
16235 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16236 /* [namespace.udecl]
16238 A using declaration shall not name a template-id. */
16239 error_at (token->location,
16240 "a template-id may not appear in a using-declaration");
16241 else
16243 if (at_class_scope_p ())
16245 /* Create the USING_DECL. */
16246 decl = do_class_using_decl (parser->scope, identifier);
16248 if (decl && typename_p)
16249 USING_DECL_TYPENAME_P (decl) = 1;
16251 if (check_for_bare_parameter_packs (decl))
16253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16254 return false;
16256 else
16257 /* Add it to the list of members in this class. */
16258 finish_member_declaration (decl);
16260 else
16262 decl = cp_parser_lookup_name_simple (parser,
16263 identifier,
16264 token->location);
16265 if (decl == error_mark_node)
16266 cp_parser_name_lookup_error (parser, identifier,
16267 decl, NLE_NULL,
16268 token->location);
16269 else if (check_for_bare_parameter_packs (decl))
16271 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16272 return false;
16274 else if (!at_namespace_scope_p ())
16275 do_local_using_decl (decl, qscope, identifier);
16276 else
16277 do_toplevel_using_decl (decl, qscope, identifier);
16281 /* Look for the final `;'. */
16282 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16284 if (access_declaration_p && errorcount == oldcount)
16285 warning_at (diag_token->location, OPT_Wdeprecated,
16286 "access declarations are deprecated "
16287 "in favour of using-declarations; "
16288 "suggestion: add the %<using%> keyword");
16290 return true;
16293 /* Parse an alias-declaration.
16295 alias-declaration:
16296 using identifier attribute-specifier-seq [opt] = type-id */
16298 static tree
16299 cp_parser_alias_declaration (cp_parser* parser)
16301 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16302 location_t id_location;
16303 cp_declarator *declarator;
16304 cp_decl_specifier_seq decl_specs;
16305 bool member_p;
16306 const char *saved_message = NULL;
16308 /* Look for the `using' keyword. */
16309 cp_token *using_token
16310 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16311 if (using_token == NULL)
16312 return error_mark_node;
16314 id_location = cp_lexer_peek_token (parser->lexer)->location;
16315 id = cp_parser_identifier (parser);
16316 if (id == error_mark_node)
16317 return error_mark_node;
16319 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16320 attributes = cp_parser_attributes_opt (parser);
16321 if (attributes == error_mark_node)
16322 return error_mark_node;
16324 cp_parser_require (parser, CPP_EQ, RT_EQ);
16326 if (cp_parser_error_occurred (parser))
16327 return error_mark_node;
16329 cp_parser_commit_to_tentative_parse (parser);
16331 /* Now we are going to parse the type-id of the declaration. */
16334 [dcl.type]/3 says:
16336 "A type-specifier-seq shall not define a class or enumeration
16337 unless it appears in the type-id of an alias-declaration (7.1.3) that
16338 is not the declaration of a template-declaration."
16340 In other words, if we currently are in an alias template, the
16341 type-id should not define a type.
16343 So let's set parser->type_definition_forbidden_message in that
16344 case; cp_parser_check_type_definition (called by
16345 cp_parser_class_specifier) will then emit an error if a type is
16346 defined in the type-id. */
16347 if (parser->num_template_parameter_lists)
16349 saved_message = parser->type_definition_forbidden_message;
16350 parser->type_definition_forbidden_message =
16351 G_("types may not be defined in alias template declarations");
16354 type = cp_parser_type_id (parser);
16356 /* Restore the error message if need be. */
16357 if (parser->num_template_parameter_lists)
16358 parser->type_definition_forbidden_message = saved_message;
16360 if (type == error_mark_node
16361 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16363 cp_parser_skip_to_end_of_block_or_statement (parser);
16364 return error_mark_node;
16367 /* A typedef-name can also be introduced by an alias-declaration. The
16368 identifier following the using keyword becomes a typedef-name. It has
16369 the same semantics as if it were introduced by the typedef
16370 specifier. In particular, it does not define a new type and it shall
16371 not appear in the type-id. */
16373 clear_decl_specs (&decl_specs);
16374 decl_specs.type = type;
16375 if (attributes != NULL_TREE)
16377 decl_specs.attributes = attributes;
16378 set_and_check_decl_spec_loc (&decl_specs,
16379 ds_attribute,
16380 attrs_token);
16382 set_and_check_decl_spec_loc (&decl_specs,
16383 ds_typedef,
16384 using_token);
16385 set_and_check_decl_spec_loc (&decl_specs,
16386 ds_alias,
16387 using_token);
16389 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16390 declarator->id_loc = id_location;
16392 member_p = at_class_scope_p ();
16393 if (member_p)
16394 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16395 NULL_TREE, attributes);
16396 else
16397 decl = start_decl (declarator, &decl_specs, 0,
16398 attributes, NULL_TREE, &pushed_scope);
16399 if (decl == error_mark_node)
16400 return decl;
16402 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16404 if (pushed_scope)
16405 pop_scope (pushed_scope);
16407 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16408 added into the symbol table; otherwise, return the TYPE_DECL. */
16409 if (DECL_LANG_SPECIFIC (decl)
16410 && DECL_TEMPLATE_INFO (decl)
16411 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16413 decl = DECL_TI_TEMPLATE (decl);
16414 if (member_p)
16415 check_member_template (decl);
16418 return decl;
16421 /* Parse a using-directive.
16423 using-directive:
16424 using namespace :: [opt] nested-name-specifier [opt]
16425 namespace-name ; */
16427 static void
16428 cp_parser_using_directive (cp_parser* parser)
16430 tree namespace_decl;
16431 tree attribs;
16433 /* Look for the `using' keyword. */
16434 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16435 /* And the `namespace' keyword. */
16436 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16437 /* Look for the optional `::' operator. */
16438 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16439 /* And the optional nested-name-specifier. */
16440 cp_parser_nested_name_specifier_opt (parser,
16441 /*typename_keyword_p=*/false,
16442 /*check_dependency_p=*/true,
16443 /*type_p=*/false,
16444 /*is_declaration=*/true);
16445 /* Get the namespace being used. */
16446 namespace_decl = cp_parser_namespace_name (parser);
16447 /* And any specified attributes. */
16448 attribs = cp_parser_attributes_opt (parser);
16449 /* Update the symbol table. */
16450 parse_using_directive (namespace_decl, attribs);
16451 /* Look for the final `;'. */
16452 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16455 /* Parse an asm-definition.
16457 asm-definition:
16458 asm ( string-literal ) ;
16460 GNU Extension:
16462 asm-definition:
16463 asm volatile [opt] ( string-literal ) ;
16464 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16465 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16466 : asm-operand-list [opt] ) ;
16467 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16468 : asm-operand-list [opt]
16469 : asm-clobber-list [opt] ) ;
16470 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16471 : asm-clobber-list [opt]
16472 : asm-goto-list ) ; */
16474 static void
16475 cp_parser_asm_definition (cp_parser* parser)
16477 tree string;
16478 tree outputs = NULL_TREE;
16479 tree inputs = NULL_TREE;
16480 tree clobbers = NULL_TREE;
16481 tree labels = NULL_TREE;
16482 tree asm_stmt;
16483 bool volatile_p = false;
16484 bool extended_p = false;
16485 bool invalid_inputs_p = false;
16486 bool invalid_outputs_p = false;
16487 bool goto_p = false;
16488 required_token missing = RT_NONE;
16490 /* Look for the `asm' keyword. */
16491 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16492 /* See if the next token is `volatile'. */
16493 if (cp_parser_allow_gnu_extensions_p (parser)
16494 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16496 /* Remember that we saw the `volatile' keyword. */
16497 volatile_p = true;
16498 /* Consume the token. */
16499 cp_lexer_consume_token (parser->lexer);
16501 if (cp_parser_allow_gnu_extensions_p (parser)
16502 && parser->in_function_body
16503 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16505 /* Remember that we saw the `goto' keyword. */
16506 goto_p = true;
16507 /* Consume the token. */
16508 cp_lexer_consume_token (parser->lexer);
16510 /* Look for the opening `('. */
16511 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16512 return;
16513 /* Look for the string. */
16514 string = cp_parser_string_literal (parser, false, false);
16515 if (string == error_mark_node)
16517 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16518 /*consume_paren=*/true);
16519 return;
16522 /* If we're allowing GNU extensions, check for the extended assembly
16523 syntax. Unfortunately, the `:' tokens need not be separated by
16524 a space in C, and so, for compatibility, we tolerate that here
16525 too. Doing that means that we have to treat the `::' operator as
16526 two `:' tokens. */
16527 if (cp_parser_allow_gnu_extensions_p (parser)
16528 && parser->in_function_body
16529 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16530 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16532 bool inputs_p = false;
16533 bool clobbers_p = false;
16534 bool labels_p = false;
16536 /* The extended syntax was used. */
16537 extended_p = true;
16539 /* Look for outputs. */
16540 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16542 /* Consume the `:'. */
16543 cp_lexer_consume_token (parser->lexer);
16544 /* Parse the output-operands. */
16545 if (cp_lexer_next_token_is_not (parser->lexer,
16546 CPP_COLON)
16547 && cp_lexer_next_token_is_not (parser->lexer,
16548 CPP_SCOPE)
16549 && cp_lexer_next_token_is_not (parser->lexer,
16550 CPP_CLOSE_PAREN)
16551 && !goto_p)
16552 outputs = cp_parser_asm_operand_list (parser);
16554 if (outputs == error_mark_node)
16555 invalid_outputs_p = true;
16557 /* If the next token is `::', there are no outputs, and the
16558 next token is the beginning of the inputs. */
16559 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16560 /* The inputs are coming next. */
16561 inputs_p = true;
16563 /* Look for inputs. */
16564 if (inputs_p
16565 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16567 /* Consume the `:' or `::'. */
16568 cp_lexer_consume_token (parser->lexer);
16569 /* Parse the output-operands. */
16570 if (cp_lexer_next_token_is_not (parser->lexer,
16571 CPP_COLON)
16572 && cp_lexer_next_token_is_not (parser->lexer,
16573 CPP_SCOPE)
16574 && cp_lexer_next_token_is_not (parser->lexer,
16575 CPP_CLOSE_PAREN))
16576 inputs = cp_parser_asm_operand_list (parser);
16578 if (inputs == error_mark_node)
16579 invalid_inputs_p = true;
16581 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16582 /* The clobbers are coming next. */
16583 clobbers_p = true;
16585 /* Look for clobbers. */
16586 if (clobbers_p
16587 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16589 clobbers_p = true;
16590 /* Consume the `:' or `::'. */
16591 cp_lexer_consume_token (parser->lexer);
16592 /* Parse the clobbers. */
16593 if (cp_lexer_next_token_is_not (parser->lexer,
16594 CPP_COLON)
16595 && cp_lexer_next_token_is_not (parser->lexer,
16596 CPP_CLOSE_PAREN))
16597 clobbers = cp_parser_asm_clobber_list (parser);
16599 else if (goto_p
16600 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16601 /* The labels are coming next. */
16602 labels_p = true;
16604 /* Look for labels. */
16605 if (labels_p
16606 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16608 labels_p = true;
16609 /* Consume the `:' or `::'. */
16610 cp_lexer_consume_token (parser->lexer);
16611 /* Parse the labels. */
16612 labels = cp_parser_asm_label_list (parser);
16615 if (goto_p && !labels_p)
16616 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16618 else if (goto_p)
16619 missing = RT_COLON_SCOPE;
16621 /* Look for the closing `)'. */
16622 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16623 missing ? missing : RT_CLOSE_PAREN))
16624 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16625 /*consume_paren=*/true);
16626 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16628 if (!invalid_inputs_p && !invalid_outputs_p)
16630 /* Create the ASM_EXPR. */
16631 if (parser->in_function_body)
16633 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16634 inputs, clobbers, labels);
16635 /* If the extended syntax was not used, mark the ASM_EXPR. */
16636 if (!extended_p)
16638 tree temp = asm_stmt;
16639 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16640 temp = TREE_OPERAND (temp, 0);
16642 ASM_INPUT_P (temp) = 1;
16645 else
16646 symtab->finalize_toplevel_asm (string);
16650 /* Declarators [gram.dcl.decl] */
16652 /* Parse an init-declarator.
16654 init-declarator:
16655 declarator initializer [opt]
16657 GNU Extension:
16659 init-declarator:
16660 declarator asm-specification [opt] attributes [opt] initializer [opt]
16662 function-definition:
16663 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16664 function-body
16665 decl-specifier-seq [opt] declarator function-try-block
16667 GNU Extension:
16669 function-definition:
16670 __extension__ function-definition
16672 TM Extension:
16674 function-definition:
16675 decl-specifier-seq [opt] declarator function-transaction-block
16677 The DECL_SPECIFIERS apply to this declarator. Returns a
16678 representation of the entity declared. If MEMBER_P is TRUE, then
16679 this declarator appears in a class scope. The new DECL created by
16680 this declarator is returned.
16682 The CHECKS are access checks that should be performed once we know
16683 what entity is being declared (and, therefore, what classes have
16684 befriended it).
16686 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16687 for a function-definition here as well. If the declarator is a
16688 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16689 be TRUE upon return. By that point, the function-definition will
16690 have been completely parsed.
16692 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16693 is FALSE.
16695 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16696 parsed declaration if it is an uninitialized single declarator not followed
16697 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16698 if present, will not be consumed. If returned, this declarator will be
16699 created with SD_INITIALIZED but will not call cp_finish_decl. */
16701 static tree
16702 cp_parser_init_declarator (cp_parser* parser,
16703 cp_decl_specifier_seq *decl_specifiers,
16704 vec<deferred_access_check, va_gc> *checks,
16705 bool function_definition_allowed_p,
16706 bool member_p,
16707 int declares_class_or_enum,
16708 bool* function_definition_p,
16709 tree* maybe_range_for_decl)
16711 cp_token *token = NULL, *asm_spec_start_token = NULL,
16712 *attributes_start_token = NULL;
16713 cp_declarator *declarator;
16714 tree prefix_attributes;
16715 tree attributes = NULL;
16716 tree asm_specification;
16717 tree initializer;
16718 tree decl = NULL_TREE;
16719 tree scope;
16720 int is_initialized;
16721 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16722 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16723 "(...)". */
16724 enum cpp_ttype initialization_kind;
16725 bool is_direct_init = false;
16726 bool is_non_constant_init;
16727 int ctor_dtor_or_conv_p;
16728 bool friend_p = cp_parser_friend_p (decl_specifiers);
16729 tree pushed_scope = NULL_TREE;
16730 bool range_for_decl_p = false;
16731 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16733 /* Gather the attributes that were provided with the
16734 decl-specifiers. */
16735 prefix_attributes = decl_specifiers->attributes;
16737 /* Assume that this is not the declarator for a function
16738 definition. */
16739 if (function_definition_p)
16740 *function_definition_p = false;
16742 /* Default arguments are only permitted for function parameters. */
16743 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16744 parser->default_arg_ok_p = false;
16746 /* Defer access checks while parsing the declarator; we cannot know
16747 what names are accessible until we know what is being
16748 declared. */
16749 resume_deferring_access_checks ();
16751 /* Parse the declarator. */
16752 token = cp_lexer_peek_token (parser->lexer);
16753 declarator
16754 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16755 &ctor_dtor_or_conv_p,
16756 /*parenthesized_p=*/NULL,
16757 member_p, friend_p);
16758 /* Gather up the deferred checks. */
16759 stop_deferring_access_checks ();
16761 parser->default_arg_ok_p = saved_default_arg_ok_p;
16763 /* If the DECLARATOR was erroneous, there's no need to go
16764 further. */
16765 if (declarator == cp_error_declarator)
16766 return error_mark_node;
16768 /* Check that the number of template-parameter-lists is OK. */
16769 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16770 token->location))
16771 return error_mark_node;
16773 if (declares_class_or_enum & 2)
16774 cp_parser_check_for_definition_in_return_type (declarator,
16775 decl_specifiers->type,
16776 decl_specifiers->locations[ds_type_spec]);
16778 /* Figure out what scope the entity declared by the DECLARATOR is
16779 located in. `grokdeclarator' sometimes changes the scope, so
16780 we compute it now. */
16781 scope = get_scope_of_declarator (declarator);
16783 /* Perform any lookups in the declared type which were thought to be
16784 dependent, but are not in the scope of the declarator. */
16785 decl_specifiers->type
16786 = maybe_update_decl_type (decl_specifiers->type, scope);
16788 /* If we're allowing GNU extensions, look for an
16789 asm-specification. */
16790 if (cp_parser_allow_gnu_extensions_p (parser))
16792 /* Look for an asm-specification. */
16793 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16794 asm_specification = cp_parser_asm_specification_opt (parser);
16796 else
16797 asm_specification = NULL_TREE;
16799 /* Look for attributes. */
16800 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16801 attributes = cp_parser_attributes_opt (parser);
16803 /* Peek at the next token. */
16804 token = cp_lexer_peek_token (parser->lexer);
16806 bool bogus_implicit_tmpl = false;
16808 if (function_declarator_p (declarator))
16810 /* Check to see if the token indicates the start of a
16811 function-definition. */
16812 if (cp_parser_token_starts_function_definition_p (token))
16814 if (!function_definition_allowed_p)
16816 /* If a function-definition should not appear here, issue an
16817 error message. */
16818 cp_parser_error (parser,
16819 "a function-definition is not allowed here");
16820 return error_mark_node;
16823 location_t func_brace_location
16824 = cp_lexer_peek_token (parser->lexer)->location;
16826 /* Neither attributes nor an asm-specification are allowed
16827 on a function-definition. */
16828 if (asm_specification)
16829 error_at (asm_spec_start_token->location,
16830 "an asm-specification is not allowed "
16831 "on a function-definition");
16832 if (attributes)
16833 error_at (attributes_start_token->location,
16834 "attributes are not allowed "
16835 "on a function-definition");
16836 /* This is a function-definition. */
16837 *function_definition_p = true;
16839 /* Parse the function definition. */
16840 if (member_p)
16841 decl = cp_parser_save_member_function_body (parser,
16842 decl_specifiers,
16843 declarator,
16844 prefix_attributes);
16845 else
16846 decl =
16847 (cp_parser_function_definition_from_specifiers_and_declarator
16848 (parser, decl_specifiers, prefix_attributes, declarator));
16850 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16852 /* This is where the prologue starts... */
16853 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16854 = func_brace_location;
16857 return decl;
16860 else if (parser->fully_implicit_function_template_p)
16862 /* A non-template declaration involving a function parameter list
16863 containing an implicit template parameter will be made into a
16864 template. If the resulting declaration is not going to be an
16865 actual function then finish the template scope here to prevent it.
16866 An error message will be issued once we have a decl to talk about.
16868 FIXME probably we should do type deduction rather than create an
16869 implicit template, but the standard currently doesn't allow it. */
16870 bogus_implicit_tmpl = true;
16871 finish_fully_implicit_template (parser, NULL_TREE);
16874 /* [dcl.dcl]
16876 Only in function declarations for constructors, destructors, and
16877 type conversions can the decl-specifier-seq be omitted.
16879 We explicitly postpone this check past the point where we handle
16880 function-definitions because we tolerate function-definitions
16881 that are missing their return types in some modes. */
16882 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16884 cp_parser_error (parser,
16885 "expected constructor, destructor, or type conversion");
16886 return error_mark_node;
16889 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16890 if (token->type == CPP_EQ
16891 || token->type == CPP_OPEN_PAREN
16892 || token->type == CPP_OPEN_BRACE)
16894 is_initialized = SD_INITIALIZED;
16895 initialization_kind = token->type;
16896 if (maybe_range_for_decl)
16897 *maybe_range_for_decl = error_mark_node;
16899 if (token->type == CPP_EQ
16900 && function_declarator_p (declarator))
16902 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16903 if (t2->keyword == RID_DEFAULT)
16904 is_initialized = SD_DEFAULTED;
16905 else if (t2->keyword == RID_DELETE)
16906 is_initialized = SD_DELETED;
16909 else
16911 /* If the init-declarator isn't initialized and isn't followed by a
16912 `,' or `;', it's not a valid init-declarator. */
16913 if (token->type != CPP_COMMA
16914 && token->type != CPP_SEMICOLON)
16916 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16917 range_for_decl_p = true;
16918 else
16920 cp_parser_error (parser, "expected initializer");
16921 return error_mark_node;
16924 is_initialized = SD_UNINITIALIZED;
16925 initialization_kind = CPP_EOF;
16928 /* Because start_decl has side-effects, we should only call it if we
16929 know we're going ahead. By this point, we know that we cannot
16930 possibly be looking at any other construct. */
16931 cp_parser_commit_to_tentative_parse (parser);
16933 /* Enter the newly declared entry in the symbol table. If we're
16934 processing a declaration in a class-specifier, we wait until
16935 after processing the initializer. */
16936 if (!member_p)
16938 if (parser->in_unbraced_linkage_specification_p)
16939 decl_specifiers->storage_class = sc_extern;
16940 decl = start_decl (declarator, decl_specifiers,
16941 range_for_decl_p? SD_INITIALIZED : is_initialized,
16942 attributes, prefix_attributes, &pushed_scope);
16943 cp_finalize_omp_declare_simd (parser, decl);
16944 /* Adjust location of decl if declarator->id_loc is more appropriate:
16945 set, and decl wasn't merged with another decl, in which case its
16946 location would be different from input_location, and more accurate. */
16947 if (DECL_P (decl)
16948 && declarator->id_loc != UNKNOWN_LOCATION
16949 && DECL_SOURCE_LOCATION (decl) == input_location)
16950 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16952 else if (scope)
16953 /* Enter the SCOPE. That way unqualified names appearing in the
16954 initializer will be looked up in SCOPE. */
16955 pushed_scope = push_scope (scope);
16957 /* Perform deferred access control checks, now that we know in which
16958 SCOPE the declared entity resides. */
16959 if (!member_p && decl)
16961 tree saved_current_function_decl = NULL_TREE;
16963 /* If the entity being declared is a function, pretend that we
16964 are in its scope. If it is a `friend', it may have access to
16965 things that would not otherwise be accessible. */
16966 if (TREE_CODE (decl) == FUNCTION_DECL)
16968 saved_current_function_decl = current_function_decl;
16969 current_function_decl = decl;
16972 /* Perform access checks for template parameters. */
16973 cp_parser_perform_template_parameter_access_checks (checks);
16975 /* Perform the access control checks for the declarator and the
16976 decl-specifiers. */
16977 perform_deferred_access_checks (tf_warning_or_error);
16979 /* Restore the saved value. */
16980 if (TREE_CODE (decl) == FUNCTION_DECL)
16981 current_function_decl = saved_current_function_decl;
16984 /* Parse the initializer. */
16985 initializer = NULL_TREE;
16986 is_direct_init = false;
16987 is_non_constant_init = true;
16988 if (is_initialized)
16990 if (function_declarator_p (declarator))
16992 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16993 if (initialization_kind == CPP_EQ)
16994 initializer = cp_parser_pure_specifier (parser);
16995 else
16997 /* If the declaration was erroneous, we don't really
16998 know what the user intended, so just silently
16999 consume the initializer. */
17000 if (decl != error_mark_node)
17001 error_at (initializer_start_token->location,
17002 "initializer provided for function");
17003 cp_parser_skip_to_closing_parenthesis (parser,
17004 /*recovering=*/true,
17005 /*or_comma=*/false,
17006 /*consume_paren=*/true);
17009 else
17011 /* We want to record the extra mangling scope for in-class
17012 initializers of class members and initializers of static data
17013 member templates. The former involves deferring
17014 parsing of the initializer until end of class as with default
17015 arguments. So right here we only handle the latter. */
17016 if (!member_p && processing_template_decl)
17017 start_lambda_scope (decl);
17018 initializer = cp_parser_initializer (parser,
17019 &is_direct_init,
17020 &is_non_constant_init);
17021 if (!member_p && processing_template_decl)
17022 finish_lambda_scope ();
17023 if (initializer == error_mark_node)
17024 cp_parser_skip_to_end_of_statement (parser);
17028 /* The old parser allows attributes to appear after a parenthesized
17029 initializer. Mark Mitchell proposed removing this functionality
17030 on the GCC mailing lists on 2002-08-13. This parser accepts the
17031 attributes -- but ignores them. */
17032 if (cp_parser_allow_gnu_extensions_p (parser)
17033 && initialization_kind == CPP_OPEN_PAREN)
17034 if (cp_parser_attributes_opt (parser))
17035 warning (OPT_Wattributes,
17036 "attributes after parenthesized initializer ignored");
17038 /* And now complain about a non-function implicit template. */
17039 if (bogus_implicit_tmpl)
17040 error_at (DECL_SOURCE_LOCATION (decl),
17041 "non-function %qD declared as implicit template", decl);
17043 /* For an in-class declaration, use `grokfield' to create the
17044 declaration. */
17045 if (member_p)
17047 if (pushed_scope)
17049 pop_scope (pushed_scope);
17050 pushed_scope = NULL_TREE;
17052 decl = grokfield (declarator, decl_specifiers,
17053 initializer, !is_non_constant_init,
17054 /*asmspec=*/NULL_TREE,
17055 chainon (attributes, prefix_attributes));
17056 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17057 cp_parser_save_default_args (parser, decl);
17058 cp_finalize_omp_declare_simd (parser, decl);
17061 /* Finish processing the declaration. But, skip member
17062 declarations. */
17063 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17065 cp_finish_decl (decl,
17066 initializer, !is_non_constant_init,
17067 asm_specification,
17068 /* If the initializer is in parentheses, then this is
17069 a direct-initialization, which means that an
17070 `explicit' constructor is OK. Otherwise, an
17071 `explicit' constructor cannot be used. */
17072 ((is_direct_init || !is_initialized)
17073 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17075 else if ((cxx_dialect != cxx98) && friend_p
17076 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17077 /* Core issue #226 (C++0x only): A default template-argument
17078 shall not be specified in a friend class template
17079 declaration. */
17080 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17081 /*is_partial=*/false, /*is_friend_decl=*/1);
17083 if (!friend_p && pushed_scope)
17084 pop_scope (pushed_scope);
17086 if (function_declarator_p (declarator)
17087 && parser->fully_implicit_function_template_p)
17089 if (member_p)
17090 decl = finish_fully_implicit_template (parser, decl);
17091 else
17092 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17095 return decl;
17098 /* Parse a declarator.
17100 declarator:
17101 direct-declarator
17102 ptr-operator declarator
17104 abstract-declarator:
17105 ptr-operator abstract-declarator [opt]
17106 direct-abstract-declarator
17108 GNU Extensions:
17110 declarator:
17111 attributes [opt] direct-declarator
17112 attributes [opt] ptr-operator declarator
17114 abstract-declarator:
17115 attributes [opt] ptr-operator abstract-declarator [opt]
17116 attributes [opt] direct-abstract-declarator
17118 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17119 detect constructor, destructor or conversion operators. It is set
17120 to -1 if the declarator is a name, and +1 if it is a
17121 function. Otherwise it is set to zero. Usually you just want to
17122 test for >0, but internally the negative value is used.
17124 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17125 a decl-specifier-seq unless it declares a constructor, destructor,
17126 or conversion. It might seem that we could check this condition in
17127 semantic analysis, rather than parsing, but that makes it difficult
17128 to handle something like `f()'. We want to notice that there are
17129 no decl-specifiers, and therefore realize that this is an
17130 expression, not a declaration.)
17132 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17133 the declarator is a direct-declarator of the form "(...)".
17135 MEMBER_P is true iff this declarator is a member-declarator.
17137 FRIEND_P is true iff this declarator is a friend. */
17139 static cp_declarator *
17140 cp_parser_declarator (cp_parser* parser,
17141 cp_parser_declarator_kind dcl_kind,
17142 int* ctor_dtor_or_conv_p,
17143 bool* parenthesized_p,
17144 bool member_p, bool friend_p)
17146 cp_declarator *declarator;
17147 enum tree_code code;
17148 cp_cv_quals cv_quals;
17149 tree class_type;
17150 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17152 /* Assume this is not a constructor, destructor, or type-conversion
17153 operator. */
17154 if (ctor_dtor_or_conv_p)
17155 *ctor_dtor_or_conv_p = 0;
17157 if (cp_parser_allow_gnu_extensions_p (parser))
17158 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17160 /* Check for the ptr-operator production. */
17161 cp_parser_parse_tentatively (parser);
17162 /* Parse the ptr-operator. */
17163 code = cp_parser_ptr_operator (parser,
17164 &class_type,
17165 &cv_quals,
17166 &std_attributes);
17168 /* If that worked, then we have a ptr-operator. */
17169 if (cp_parser_parse_definitely (parser))
17171 /* If a ptr-operator was found, then this declarator was not
17172 parenthesized. */
17173 if (parenthesized_p)
17174 *parenthesized_p = true;
17175 /* The dependent declarator is optional if we are parsing an
17176 abstract-declarator. */
17177 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17178 cp_parser_parse_tentatively (parser);
17180 /* Parse the dependent declarator. */
17181 declarator = cp_parser_declarator (parser, dcl_kind,
17182 /*ctor_dtor_or_conv_p=*/NULL,
17183 /*parenthesized_p=*/NULL,
17184 /*member_p=*/false,
17185 friend_p);
17187 /* If we are parsing an abstract-declarator, we must handle the
17188 case where the dependent declarator is absent. */
17189 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17190 && !cp_parser_parse_definitely (parser))
17191 declarator = NULL;
17193 declarator = cp_parser_make_indirect_declarator
17194 (code, class_type, cv_quals, declarator, std_attributes);
17196 /* Everything else is a direct-declarator. */
17197 else
17199 if (parenthesized_p)
17200 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17201 CPP_OPEN_PAREN);
17202 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17203 ctor_dtor_or_conv_p,
17204 member_p, friend_p);
17207 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17208 declarator->attributes = gnu_attributes;
17209 return declarator;
17212 /* Parse a direct-declarator or direct-abstract-declarator.
17214 direct-declarator:
17215 declarator-id
17216 direct-declarator ( parameter-declaration-clause )
17217 cv-qualifier-seq [opt]
17218 ref-qualifier [opt]
17219 exception-specification [opt]
17220 direct-declarator [ constant-expression [opt] ]
17221 ( declarator )
17223 direct-abstract-declarator:
17224 direct-abstract-declarator [opt]
17225 ( parameter-declaration-clause )
17226 cv-qualifier-seq [opt]
17227 ref-qualifier [opt]
17228 exception-specification [opt]
17229 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17230 ( abstract-declarator )
17232 Returns a representation of the declarator. DCL_KIND is
17233 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17234 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17235 we are parsing a direct-declarator. It is
17236 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17237 of ambiguity we prefer an abstract declarator, as per
17238 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17239 as for cp_parser_declarator. */
17241 static cp_declarator *
17242 cp_parser_direct_declarator (cp_parser* parser,
17243 cp_parser_declarator_kind dcl_kind,
17244 int* ctor_dtor_or_conv_p,
17245 bool member_p, bool friend_p)
17247 cp_token *token;
17248 cp_declarator *declarator = NULL;
17249 tree scope = NULL_TREE;
17250 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17251 bool saved_in_declarator_p = parser->in_declarator_p;
17252 bool first = true;
17253 tree pushed_scope = NULL_TREE;
17255 while (true)
17257 /* Peek at the next token. */
17258 token = cp_lexer_peek_token (parser->lexer);
17259 if (token->type == CPP_OPEN_PAREN)
17261 /* This is either a parameter-declaration-clause, or a
17262 parenthesized declarator. When we know we are parsing a
17263 named declarator, it must be a parenthesized declarator
17264 if FIRST is true. For instance, `(int)' is a
17265 parameter-declaration-clause, with an omitted
17266 direct-abstract-declarator. But `((*))', is a
17267 parenthesized abstract declarator. Finally, when T is a
17268 template parameter `(T)' is a
17269 parameter-declaration-clause, and not a parenthesized
17270 named declarator.
17272 We first try and parse a parameter-declaration-clause,
17273 and then try a nested declarator (if FIRST is true).
17275 It is not an error for it not to be a
17276 parameter-declaration-clause, even when FIRST is
17277 false. Consider,
17279 int i (int);
17280 int i (3);
17282 The first is the declaration of a function while the
17283 second is the definition of a variable, including its
17284 initializer.
17286 Having seen only the parenthesis, we cannot know which of
17287 these two alternatives should be selected. Even more
17288 complex are examples like:
17290 int i (int (a));
17291 int i (int (3));
17293 The former is a function-declaration; the latter is a
17294 variable initialization.
17296 Thus again, we try a parameter-declaration-clause, and if
17297 that fails, we back out and return. */
17299 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17301 tree params;
17302 bool is_declarator = false;
17304 /* In a member-declarator, the only valid interpretation
17305 of a parenthesis is the start of a
17306 parameter-declaration-clause. (It is invalid to
17307 initialize a static data member with a parenthesized
17308 initializer; only the "=" form of initialization is
17309 permitted.) */
17310 if (!member_p)
17311 cp_parser_parse_tentatively (parser);
17313 /* Consume the `('. */
17314 cp_lexer_consume_token (parser->lexer);
17315 if (first)
17317 /* If this is going to be an abstract declarator, we're
17318 in a declarator and we can't have default args. */
17319 parser->default_arg_ok_p = false;
17320 parser->in_declarator_p = true;
17323 begin_scope (sk_function_parms, NULL_TREE);
17325 /* Parse the parameter-declaration-clause. */
17326 params = cp_parser_parameter_declaration_clause (parser);
17328 /* Consume the `)'. */
17329 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17331 /* If all went well, parse the cv-qualifier-seq,
17332 ref-qualifier and the exception-specification. */
17333 if (member_p || cp_parser_parse_definitely (parser))
17335 cp_cv_quals cv_quals;
17336 cp_virt_specifiers virt_specifiers;
17337 cp_ref_qualifier ref_qual;
17338 tree exception_specification;
17339 tree late_return;
17340 tree attrs;
17341 bool memfn = (member_p || (pushed_scope
17342 && CLASS_TYPE_P (pushed_scope)));
17344 is_declarator = true;
17346 if (ctor_dtor_or_conv_p)
17347 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17348 first = false;
17350 /* Parse the cv-qualifier-seq. */
17351 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17352 /* Parse the ref-qualifier. */
17353 ref_qual = cp_parser_ref_qualifier_opt (parser);
17354 /* And the exception-specification. */
17355 exception_specification
17356 = cp_parser_exception_specification_opt (parser);
17358 attrs = cp_parser_std_attribute_spec_seq (parser);
17360 /* In here, we handle cases where attribute is used after
17361 the function declaration. For example:
17362 void func (int x) __attribute__((vector(..))); */
17363 if (flag_cilkplus
17364 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17366 cp_parser_parse_tentatively (parser);
17367 tree attr = cp_parser_gnu_attributes_opt (parser);
17368 if (cp_lexer_next_token_is_not (parser->lexer,
17369 CPP_SEMICOLON)
17370 && cp_lexer_next_token_is_not (parser->lexer,
17371 CPP_OPEN_BRACE))
17372 cp_parser_abort_tentative_parse (parser);
17373 else if (!cp_parser_parse_definitely (parser))
17375 else
17376 attrs = chainon (attr, attrs);
17378 late_return = (cp_parser_late_return_type_opt
17379 (parser, declarator,
17380 memfn ? cv_quals : -1));
17383 /* Parse the virt-specifier-seq. */
17384 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17386 /* Create the function-declarator. */
17387 declarator = make_call_declarator (declarator,
17388 params,
17389 cv_quals,
17390 virt_specifiers,
17391 ref_qual,
17392 exception_specification,
17393 late_return);
17394 declarator->std_attributes = attrs;
17395 /* Any subsequent parameter lists are to do with
17396 return type, so are not those of the declared
17397 function. */
17398 parser->default_arg_ok_p = false;
17401 /* Remove the function parms from scope. */
17402 pop_bindings_and_leave_scope ();
17404 if (is_declarator)
17405 /* Repeat the main loop. */
17406 continue;
17409 /* If this is the first, we can try a parenthesized
17410 declarator. */
17411 if (first)
17413 bool saved_in_type_id_in_expr_p;
17415 parser->default_arg_ok_p = saved_default_arg_ok_p;
17416 parser->in_declarator_p = saved_in_declarator_p;
17418 /* Consume the `('. */
17419 cp_lexer_consume_token (parser->lexer);
17420 /* Parse the nested declarator. */
17421 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17422 parser->in_type_id_in_expr_p = true;
17423 declarator
17424 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17425 /*parenthesized_p=*/NULL,
17426 member_p, friend_p);
17427 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17428 first = false;
17429 /* Expect a `)'. */
17430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17431 declarator = cp_error_declarator;
17432 if (declarator == cp_error_declarator)
17433 break;
17435 goto handle_declarator;
17437 /* Otherwise, we must be done. */
17438 else
17439 break;
17441 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17442 && token->type == CPP_OPEN_SQUARE
17443 && !cp_next_tokens_can_be_attribute_p (parser))
17445 /* Parse an array-declarator. */
17446 tree bounds, attrs;
17448 if (ctor_dtor_or_conv_p)
17449 *ctor_dtor_or_conv_p = 0;
17451 first = false;
17452 parser->default_arg_ok_p = false;
17453 parser->in_declarator_p = true;
17454 /* Consume the `['. */
17455 cp_lexer_consume_token (parser->lexer);
17456 /* Peek at the next token. */
17457 token = cp_lexer_peek_token (parser->lexer);
17458 /* If the next token is `]', then there is no
17459 constant-expression. */
17460 if (token->type != CPP_CLOSE_SQUARE)
17462 bool non_constant_p;
17463 bounds
17464 = cp_parser_constant_expression (parser,
17465 /*allow_non_constant=*/true,
17466 &non_constant_p);
17467 if (!non_constant_p)
17468 /* OK */;
17469 else if (error_operand_p (bounds))
17470 /* Already gave an error. */;
17471 else if (!parser->in_function_body
17472 || current_binding_level->kind == sk_function_parms)
17474 /* Normally, the array bound must be an integral constant
17475 expression. However, as an extension, we allow VLAs
17476 in function scopes as long as they aren't part of a
17477 parameter declaration. */
17478 cp_parser_error (parser,
17479 "array bound is not an integer constant");
17480 bounds = error_mark_node;
17482 else if (processing_template_decl
17483 && !type_dependent_expression_p (bounds))
17485 /* Remember this wasn't a constant-expression. */
17486 bounds = build_nop (TREE_TYPE (bounds), bounds);
17487 TREE_SIDE_EFFECTS (bounds) = 1;
17490 else
17491 bounds = NULL_TREE;
17492 /* Look for the closing `]'. */
17493 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17495 declarator = cp_error_declarator;
17496 break;
17499 attrs = cp_parser_std_attribute_spec_seq (parser);
17500 declarator = make_array_declarator (declarator, bounds);
17501 declarator->std_attributes = attrs;
17503 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17506 tree qualifying_scope;
17507 tree unqualified_name;
17508 tree attrs;
17509 special_function_kind sfk;
17510 bool abstract_ok;
17511 bool pack_expansion_p = false;
17512 cp_token *declarator_id_start_token;
17514 /* Parse a declarator-id */
17515 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17516 if (abstract_ok)
17518 cp_parser_parse_tentatively (parser);
17520 /* If we see an ellipsis, we should be looking at a
17521 parameter pack. */
17522 if (token->type == CPP_ELLIPSIS)
17524 /* Consume the `...' */
17525 cp_lexer_consume_token (parser->lexer);
17527 pack_expansion_p = true;
17531 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17532 unqualified_name
17533 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17534 qualifying_scope = parser->scope;
17535 if (abstract_ok)
17537 bool okay = false;
17539 if (!unqualified_name && pack_expansion_p)
17541 /* Check whether an error occurred. */
17542 okay = !cp_parser_error_occurred (parser);
17544 /* We already consumed the ellipsis to mark a
17545 parameter pack, but we have no way to report it,
17546 so abort the tentative parse. We will be exiting
17547 immediately anyway. */
17548 cp_parser_abort_tentative_parse (parser);
17550 else
17551 okay = cp_parser_parse_definitely (parser);
17553 if (!okay)
17554 unqualified_name = error_mark_node;
17555 else if (unqualified_name
17556 && (qualifying_scope
17557 || (!identifier_p (unqualified_name))))
17559 cp_parser_error (parser, "expected unqualified-id");
17560 unqualified_name = error_mark_node;
17564 if (!unqualified_name)
17565 return NULL;
17566 if (unqualified_name == error_mark_node)
17568 declarator = cp_error_declarator;
17569 pack_expansion_p = false;
17570 declarator->parameter_pack_p = false;
17571 break;
17574 attrs = cp_parser_std_attribute_spec_seq (parser);
17576 if (qualifying_scope && at_namespace_scope_p ()
17577 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17579 /* In the declaration of a member of a template class
17580 outside of the class itself, the SCOPE will sometimes
17581 be a TYPENAME_TYPE. For example, given:
17583 template <typename T>
17584 int S<T>::R::i = 3;
17586 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17587 this context, we must resolve S<T>::R to an ordinary
17588 type, rather than a typename type.
17590 The reason we normally avoid resolving TYPENAME_TYPEs
17591 is that a specialization of `S' might render
17592 `S<T>::R' not a type. However, if `S' is
17593 specialized, then this `i' will not be used, so there
17594 is no harm in resolving the types here. */
17595 tree type;
17597 /* Resolve the TYPENAME_TYPE. */
17598 type = resolve_typename_type (qualifying_scope,
17599 /*only_current_p=*/false);
17600 /* If that failed, the declarator is invalid. */
17601 if (TREE_CODE (type) == TYPENAME_TYPE)
17603 if (typedef_variant_p (type))
17604 error_at (declarator_id_start_token->location,
17605 "cannot define member of dependent typedef "
17606 "%qT", type);
17607 else
17608 error_at (declarator_id_start_token->location,
17609 "%<%T::%E%> is not a type",
17610 TYPE_CONTEXT (qualifying_scope),
17611 TYPE_IDENTIFIER (qualifying_scope));
17613 qualifying_scope = type;
17616 sfk = sfk_none;
17618 if (unqualified_name)
17620 tree class_type;
17622 if (qualifying_scope
17623 && CLASS_TYPE_P (qualifying_scope))
17624 class_type = qualifying_scope;
17625 else
17626 class_type = current_class_type;
17628 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17630 tree name_type = TREE_TYPE (unqualified_name);
17631 if (class_type && same_type_p (name_type, class_type))
17633 if (qualifying_scope
17634 && CLASSTYPE_USE_TEMPLATE (name_type))
17636 error_at (declarator_id_start_token->location,
17637 "invalid use of constructor as a template");
17638 inform (declarator_id_start_token->location,
17639 "use %<%T::%D%> instead of %<%T::%D%> to "
17640 "name the constructor in a qualified name",
17641 class_type,
17642 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17643 class_type, name_type);
17644 declarator = cp_error_declarator;
17645 break;
17647 else
17648 unqualified_name = constructor_name (class_type);
17650 else
17652 /* We do not attempt to print the declarator
17653 here because we do not have enough
17654 information about its original syntactic
17655 form. */
17656 cp_parser_error (parser, "invalid declarator");
17657 declarator = cp_error_declarator;
17658 break;
17662 if (class_type)
17664 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17665 sfk = sfk_destructor;
17666 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17667 sfk = sfk_conversion;
17668 else if (/* There's no way to declare a constructor
17669 for an anonymous type, even if the type
17670 got a name for linkage purposes. */
17671 !TYPE_WAS_ANONYMOUS (class_type)
17672 /* Handle correctly (c++/19200):
17674 struct S {
17675 struct T{};
17676 friend void S(T);
17679 and also:
17681 namespace N {
17682 void S();
17685 struct S {
17686 friend void N::S();
17687 }; */
17688 && !(friend_p
17689 && class_type != qualifying_scope)
17690 && constructor_name_p (unqualified_name,
17691 class_type))
17693 unqualified_name = constructor_name (class_type);
17694 sfk = sfk_constructor;
17696 else if (is_overloaded_fn (unqualified_name)
17697 && DECL_CONSTRUCTOR_P (get_first_fn
17698 (unqualified_name)))
17699 sfk = sfk_constructor;
17701 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17702 *ctor_dtor_or_conv_p = -1;
17705 declarator = make_id_declarator (qualifying_scope,
17706 unqualified_name,
17707 sfk);
17708 declarator->std_attributes = attrs;
17709 declarator->id_loc = token->location;
17710 declarator->parameter_pack_p = pack_expansion_p;
17712 if (pack_expansion_p)
17713 maybe_warn_variadic_templates ();
17716 handle_declarator:;
17717 scope = get_scope_of_declarator (declarator);
17718 if (scope)
17720 /* Any names that appear after the declarator-id for a
17721 member are looked up in the containing scope. */
17722 if (at_function_scope_p ())
17724 /* But declarations with qualified-ids can't appear in a
17725 function. */
17726 cp_parser_error (parser, "qualified-id in declaration");
17727 declarator = cp_error_declarator;
17728 break;
17730 pushed_scope = push_scope (scope);
17732 parser->in_declarator_p = true;
17733 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17734 || (declarator && declarator->kind == cdk_id))
17735 /* Default args are only allowed on function
17736 declarations. */
17737 parser->default_arg_ok_p = saved_default_arg_ok_p;
17738 else
17739 parser->default_arg_ok_p = false;
17741 first = false;
17743 /* We're done. */
17744 else
17745 break;
17748 /* For an abstract declarator, we might wind up with nothing at this
17749 point. That's an error; the declarator is not optional. */
17750 if (!declarator)
17751 cp_parser_error (parser, "expected declarator");
17753 /* If we entered a scope, we must exit it now. */
17754 if (pushed_scope)
17755 pop_scope (pushed_scope);
17757 parser->default_arg_ok_p = saved_default_arg_ok_p;
17758 parser->in_declarator_p = saved_in_declarator_p;
17760 return declarator;
17763 /* Parse a ptr-operator.
17765 ptr-operator:
17766 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17767 * cv-qualifier-seq [opt]
17769 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17770 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17772 GNU Extension:
17774 ptr-operator:
17775 & cv-qualifier-seq [opt]
17777 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17778 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17779 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17780 filled in with the TYPE containing the member. *CV_QUALS is
17781 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17782 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17783 Note that the tree codes returned by this function have nothing
17784 to do with the types of trees that will be eventually be created
17785 to represent the pointer or reference type being parsed. They are
17786 just constants with suggestive names. */
17787 static enum tree_code
17788 cp_parser_ptr_operator (cp_parser* parser,
17789 tree* type,
17790 cp_cv_quals *cv_quals,
17791 tree *attributes)
17793 enum tree_code code = ERROR_MARK;
17794 cp_token *token;
17795 tree attrs = NULL_TREE;
17797 /* Assume that it's not a pointer-to-member. */
17798 *type = NULL_TREE;
17799 /* And that there are no cv-qualifiers. */
17800 *cv_quals = TYPE_UNQUALIFIED;
17802 /* Peek at the next token. */
17803 token = cp_lexer_peek_token (parser->lexer);
17805 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17806 if (token->type == CPP_MULT)
17807 code = INDIRECT_REF;
17808 else if (token->type == CPP_AND)
17809 code = ADDR_EXPR;
17810 else if ((cxx_dialect != cxx98) &&
17811 token->type == CPP_AND_AND) /* C++0x only */
17812 code = NON_LVALUE_EXPR;
17814 if (code != ERROR_MARK)
17816 /* Consume the `*', `&' or `&&'. */
17817 cp_lexer_consume_token (parser->lexer);
17819 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17820 `&', if we are allowing GNU extensions. (The only qualifier
17821 that can legally appear after `&' is `restrict', but that is
17822 enforced during semantic analysis. */
17823 if (code == INDIRECT_REF
17824 || cp_parser_allow_gnu_extensions_p (parser))
17825 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17827 attrs = cp_parser_std_attribute_spec_seq (parser);
17828 if (attributes != NULL)
17829 *attributes = attrs;
17831 else
17833 /* Try the pointer-to-member case. */
17834 cp_parser_parse_tentatively (parser);
17835 /* Look for the optional `::' operator. */
17836 cp_parser_global_scope_opt (parser,
17837 /*current_scope_valid_p=*/false);
17838 /* Look for the nested-name specifier. */
17839 token = cp_lexer_peek_token (parser->lexer);
17840 cp_parser_nested_name_specifier (parser,
17841 /*typename_keyword_p=*/false,
17842 /*check_dependency_p=*/true,
17843 /*type_p=*/false,
17844 /*is_declaration=*/false);
17845 /* If we found it, and the next token is a `*', then we are
17846 indeed looking at a pointer-to-member operator. */
17847 if (!cp_parser_error_occurred (parser)
17848 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17850 /* Indicate that the `*' operator was used. */
17851 code = INDIRECT_REF;
17853 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17854 error_at (token->location, "%qD is a namespace", parser->scope);
17855 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17856 error_at (token->location, "cannot form pointer to member of "
17857 "non-class %q#T", parser->scope);
17858 else
17860 /* The type of which the member is a member is given by the
17861 current SCOPE. */
17862 *type = parser->scope;
17863 /* The next name will not be qualified. */
17864 parser->scope = NULL_TREE;
17865 parser->qualifying_scope = NULL_TREE;
17866 parser->object_scope = NULL_TREE;
17867 /* Look for optional c++11 attributes. */
17868 attrs = cp_parser_std_attribute_spec_seq (parser);
17869 if (attributes != NULL)
17870 *attributes = attrs;
17871 /* Look for the optional cv-qualifier-seq. */
17872 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17875 /* If that didn't work we don't have a ptr-operator. */
17876 if (!cp_parser_parse_definitely (parser))
17877 cp_parser_error (parser, "expected ptr-operator");
17880 return code;
17883 /* Parse an (optional) cv-qualifier-seq.
17885 cv-qualifier-seq:
17886 cv-qualifier cv-qualifier-seq [opt]
17888 cv-qualifier:
17889 const
17890 volatile
17892 GNU Extension:
17894 cv-qualifier:
17895 __restrict__
17897 Returns a bitmask representing the cv-qualifiers. */
17899 static cp_cv_quals
17900 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17902 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17904 while (true)
17906 cp_token *token;
17907 cp_cv_quals cv_qualifier;
17909 /* Peek at the next token. */
17910 token = cp_lexer_peek_token (parser->lexer);
17911 /* See if it's a cv-qualifier. */
17912 switch (token->keyword)
17914 case RID_CONST:
17915 cv_qualifier = TYPE_QUAL_CONST;
17916 break;
17918 case RID_VOLATILE:
17919 cv_qualifier = TYPE_QUAL_VOLATILE;
17920 break;
17922 case RID_RESTRICT:
17923 cv_qualifier = TYPE_QUAL_RESTRICT;
17924 break;
17926 default:
17927 cv_qualifier = TYPE_UNQUALIFIED;
17928 break;
17931 if (!cv_qualifier)
17932 break;
17934 if (cv_quals & cv_qualifier)
17936 error_at (token->location, "duplicate cv-qualifier");
17937 cp_lexer_purge_token (parser->lexer);
17939 else
17941 cp_lexer_consume_token (parser->lexer);
17942 cv_quals |= cv_qualifier;
17946 return cv_quals;
17949 /* Parse an (optional) ref-qualifier
17951 ref-qualifier:
17955 Returns cp_ref_qualifier representing ref-qualifier. */
17957 static cp_ref_qualifier
17958 cp_parser_ref_qualifier_opt (cp_parser* parser)
17960 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17962 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17963 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17964 return ref_qual;
17966 while (true)
17968 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17969 cp_token *token = cp_lexer_peek_token (parser->lexer);
17971 switch (token->type)
17973 case CPP_AND:
17974 curr_ref_qual = REF_QUAL_LVALUE;
17975 break;
17977 case CPP_AND_AND:
17978 curr_ref_qual = REF_QUAL_RVALUE;
17979 break;
17981 default:
17982 curr_ref_qual = REF_QUAL_NONE;
17983 break;
17986 if (!curr_ref_qual)
17987 break;
17988 else if (ref_qual)
17990 error_at (token->location, "multiple ref-qualifiers");
17991 cp_lexer_purge_token (parser->lexer);
17993 else
17995 ref_qual = curr_ref_qual;
17996 cp_lexer_consume_token (parser->lexer);
18000 return ref_qual;
18003 /* Parse an (optional) virt-specifier-seq.
18005 virt-specifier-seq:
18006 virt-specifier virt-specifier-seq [opt]
18008 virt-specifier:
18009 override
18010 final
18012 Returns a bitmask representing the virt-specifiers. */
18014 static cp_virt_specifiers
18015 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18017 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18019 while (true)
18021 cp_token *token;
18022 cp_virt_specifiers virt_specifier;
18024 /* Peek at the next token. */
18025 token = cp_lexer_peek_token (parser->lexer);
18026 /* See if it's a virt-specifier-qualifier. */
18027 if (token->type != CPP_NAME)
18028 break;
18029 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18031 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18032 virt_specifier = VIRT_SPEC_OVERRIDE;
18034 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18036 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18037 virt_specifier = VIRT_SPEC_FINAL;
18039 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18041 virt_specifier = VIRT_SPEC_FINAL;
18043 else
18044 break;
18046 if (virt_specifiers & virt_specifier)
18048 error_at (token->location, "duplicate virt-specifier");
18049 cp_lexer_purge_token (parser->lexer);
18051 else
18053 cp_lexer_consume_token (parser->lexer);
18054 virt_specifiers |= virt_specifier;
18057 return virt_specifiers;
18060 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18061 is in scope even though it isn't real. */
18063 void
18064 inject_this_parameter (tree ctype, cp_cv_quals quals)
18066 tree this_parm;
18068 if (current_class_ptr)
18070 /* We don't clear this between NSDMIs. Is it already what we want? */
18071 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18072 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18073 && cp_type_quals (type) == quals)
18074 return;
18077 this_parm = build_this_parm (ctype, quals);
18078 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18079 current_class_ptr = NULL_TREE;
18080 current_class_ref
18081 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18082 current_class_ptr = this_parm;
18085 /* Return true iff our current scope is a non-static data member
18086 initializer. */
18088 bool
18089 parsing_nsdmi (void)
18091 /* We recognize NSDMI context by the context-less 'this' pointer set up
18092 by the function above. */
18093 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18094 return true;
18095 return false;
18098 /* Parse a late-specified return type, if any. This is not a separate
18099 non-terminal, but part of a function declarator, which looks like
18101 -> trailing-type-specifier-seq abstract-declarator(opt)
18103 Returns the type indicated by the type-id.
18105 In addition to this this parses any queued up omp declare simd
18106 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18108 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18109 function. */
18111 static tree
18112 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18113 cp_cv_quals quals)
18115 cp_token *token;
18116 tree type = NULL_TREE;
18117 bool declare_simd_p = (parser->omp_declare_simd
18118 && declarator
18119 && declarator->kind == cdk_id);
18121 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18122 && declarator && declarator->kind == cdk_id);
18124 /* Peek at the next token. */
18125 token = cp_lexer_peek_token (parser->lexer);
18126 /* A late-specified return type is indicated by an initial '->'. */
18127 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18128 return NULL_TREE;
18130 tree save_ccp = current_class_ptr;
18131 tree save_ccr = current_class_ref;
18132 if (quals >= 0)
18134 /* DR 1207: 'this' is in scope in the trailing return type. */
18135 inject_this_parameter (current_class_type, quals);
18138 if (token->type == CPP_DEREF)
18140 /* Consume the ->. */
18141 cp_lexer_consume_token (parser->lexer);
18143 type = cp_parser_trailing_type_id (parser);
18146 if (cilk_simd_fn_vector_p)
18147 declarator->std_attributes
18148 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18149 declarator->std_attributes);
18150 if (declare_simd_p)
18151 declarator->std_attributes
18152 = cp_parser_late_parsing_omp_declare_simd (parser,
18153 declarator->std_attributes);
18155 if (quals >= 0)
18157 current_class_ptr = save_ccp;
18158 current_class_ref = save_ccr;
18161 return type;
18164 /* Parse a declarator-id.
18166 declarator-id:
18167 id-expression
18168 :: [opt] nested-name-specifier [opt] type-name
18170 In the `id-expression' case, the value returned is as for
18171 cp_parser_id_expression if the id-expression was an unqualified-id.
18172 If the id-expression was a qualified-id, then a SCOPE_REF is
18173 returned. The first operand is the scope (either a NAMESPACE_DECL
18174 or TREE_TYPE), but the second is still just a representation of an
18175 unqualified-id. */
18177 static tree
18178 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18180 tree id;
18181 /* The expression must be an id-expression. Assume that qualified
18182 names are the names of types so that:
18184 template <class T>
18185 int S<T>::R::i = 3;
18187 will work; we must treat `S<T>::R' as the name of a type.
18188 Similarly, assume that qualified names are templates, where
18189 required, so that:
18191 template <class T>
18192 int S<T>::R<T>::i = 3;
18194 will work, too. */
18195 id = cp_parser_id_expression (parser,
18196 /*template_keyword_p=*/false,
18197 /*check_dependency_p=*/false,
18198 /*template_p=*/NULL,
18199 /*declarator_p=*/true,
18200 optional_p);
18201 if (id && BASELINK_P (id))
18202 id = BASELINK_FUNCTIONS (id);
18203 return id;
18206 /* Parse a type-id.
18208 type-id:
18209 type-specifier-seq abstract-declarator [opt]
18211 Returns the TYPE specified. */
18213 static tree
18214 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18215 bool is_trailing_return)
18217 cp_decl_specifier_seq type_specifier_seq;
18218 cp_declarator *abstract_declarator;
18220 /* Parse the type-specifier-seq. */
18221 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18222 is_trailing_return,
18223 &type_specifier_seq);
18224 if (type_specifier_seq.type == error_mark_node)
18225 return error_mark_node;
18227 /* There might or might not be an abstract declarator. */
18228 cp_parser_parse_tentatively (parser);
18229 /* Look for the declarator. */
18230 abstract_declarator
18231 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18232 /*parenthesized_p=*/NULL,
18233 /*member_p=*/false,
18234 /*friend_p=*/false);
18235 /* Check to see if there really was a declarator. */
18236 if (!cp_parser_parse_definitely (parser))
18237 abstract_declarator = NULL;
18239 if (type_specifier_seq.type
18240 /* None of the valid uses of 'auto' in C++14 involve the type-id
18241 nonterminal, but it is valid in a trailing-return-type. */
18242 && !(cxx_dialect >= cxx14 && is_trailing_return)
18243 && type_uses_auto (type_specifier_seq.type))
18245 /* A type-id with type 'auto' is only ok if the abstract declarator
18246 is a function declarator with a late-specified return type. */
18247 if (abstract_declarator
18248 && abstract_declarator->kind == cdk_function
18249 && abstract_declarator->u.function.late_return_type)
18250 /* OK */;
18251 else
18253 error ("invalid use of %<auto%>");
18254 return error_mark_node;
18258 return groktypename (&type_specifier_seq, abstract_declarator,
18259 is_template_arg);
18262 static tree cp_parser_type_id (cp_parser *parser)
18264 return cp_parser_type_id_1 (parser, false, false);
18267 static tree cp_parser_template_type_arg (cp_parser *parser)
18269 tree r;
18270 const char *saved_message = parser->type_definition_forbidden_message;
18271 parser->type_definition_forbidden_message
18272 = G_("types may not be defined in template arguments");
18273 r = cp_parser_type_id_1 (parser, true, false);
18274 parser->type_definition_forbidden_message = saved_message;
18275 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18277 error ("invalid use of %<auto%> in template argument");
18278 r = error_mark_node;
18280 return r;
18283 static tree cp_parser_trailing_type_id (cp_parser *parser)
18285 return cp_parser_type_id_1 (parser, false, true);
18288 /* Parse a type-specifier-seq.
18290 type-specifier-seq:
18291 type-specifier type-specifier-seq [opt]
18293 GNU extension:
18295 type-specifier-seq:
18296 attributes type-specifier-seq [opt]
18298 If IS_DECLARATION is true, we are at the start of a "condition" or
18299 exception-declaration, so we might be followed by a declarator-id.
18301 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18302 i.e. we've just seen "->".
18304 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18306 static void
18307 cp_parser_type_specifier_seq (cp_parser* parser,
18308 bool is_declaration,
18309 bool is_trailing_return,
18310 cp_decl_specifier_seq *type_specifier_seq)
18312 bool seen_type_specifier = false;
18313 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18314 cp_token *start_token = NULL;
18316 /* Clear the TYPE_SPECIFIER_SEQ. */
18317 clear_decl_specs (type_specifier_seq);
18319 /* In the context of a trailing return type, enum E { } is an
18320 elaborated-type-specifier followed by a function-body, not an
18321 enum-specifier. */
18322 if (is_trailing_return)
18323 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18325 /* Parse the type-specifiers and attributes. */
18326 while (true)
18328 tree type_specifier;
18329 bool is_cv_qualifier;
18331 /* Check for attributes first. */
18332 if (cp_next_tokens_can_be_attribute_p (parser))
18334 type_specifier_seq->attributes =
18335 chainon (type_specifier_seq->attributes,
18336 cp_parser_attributes_opt (parser));
18337 continue;
18340 /* record the token of the beginning of the type specifier seq,
18341 for error reporting purposes*/
18342 if (!start_token)
18343 start_token = cp_lexer_peek_token (parser->lexer);
18345 /* Look for the type-specifier. */
18346 type_specifier = cp_parser_type_specifier (parser,
18347 flags,
18348 type_specifier_seq,
18349 /*is_declaration=*/false,
18350 NULL,
18351 &is_cv_qualifier);
18352 if (!type_specifier)
18354 /* If the first type-specifier could not be found, this is not a
18355 type-specifier-seq at all. */
18356 if (!seen_type_specifier)
18358 /* Set in_declarator_p to avoid skipping to the semicolon. */
18359 int in_decl = parser->in_declarator_p;
18360 parser->in_declarator_p = true;
18362 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18363 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18364 cp_parser_error (parser, "expected type-specifier");
18366 parser->in_declarator_p = in_decl;
18368 type_specifier_seq->type = error_mark_node;
18369 return;
18371 /* If subsequent type-specifiers could not be found, the
18372 type-specifier-seq is complete. */
18373 break;
18376 seen_type_specifier = true;
18377 /* The standard says that a condition can be:
18379 type-specifier-seq declarator = assignment-expression
18381 However, given:
18383 struct S {};
18384 if (int S = ...)
18386 we should treat the "S" as a declarator, not as a
18387 type-specifier. The standard doesn't say that explicitly for
18388 type-specifier-seq, but it does say that for
18389 decl-specifier-seq in an ordinary declaration. Perhaps it
18390 would be clearer just to allow a decl-specifier-seq here, and
18391 then add a semantic restriction that if any decl-specifiers
18392 that are not type-specifiers appear, the program is invalid. */
18393 if (is_declaration && !is_cv_qualifier)
18394 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18398 /* Return whether the function currently being declared has an associated
18399 template parameter list. */
18401 static bool
18402 function_being_declared_is_template_p (cp_parser* parser)
18404 if (!current_template_parms || processing_template_parmlist)
18405 return false;
18407 if (parser->implicit_template_scope)
18408 return true;
18410 if (at_class_scope_p ()
18411 && TYPE_BEING_DEFINED (current_class_type))
18412 return parser->num_template_parameter_lists != 0;
18414 return ((int) parser->num_template_parameter_lists > template_class_depth
18415 (current_class_type));
18418 /* Parse a parameter-declaration-clause.
18420 parameter-declaration-clause:
18421 parameter-declaration-list [opt] ... [opt]
18422 parameter-declaration-list , ...
18424 Returns a representation for the parameter declarations. A return
18425 value of NULL indicates a parameter-declaration-clause consisting
18426 only of an ellipsis. */
18428 static tree
18429 cp_parser_parameter_declaration_clause (cp_parser* parser)
18431 tree parameters;
18432 cp_token *token;
18433 bool ellipsis_p;
18434 bool is_error;
18436 struct cleanup {
18437 cp_parser* parser;
18438 int auto_is_implicit_function_template_parm_p;
18439 ~cleanup() {
18440 parser->auto_is_implicit_function_template_parm_p
18441 = auto_is_implicit_function_template_parm_p;
18443 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18445 (void) cleanup;
18447 if (!processing_specialization
18448 && !processing_template_parmlist
18449 && !processing_explicit_instantiation)
18450 if (!current_function_decl
18451 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18452 parser->auto_is_implicit_function_template_parm_p = true;
18454 /* Peek at the next token. */
18455 token = cp_lexer_peek_token (parser->lexer);
18456 /* Check for trivial parameter-declaration-clauses. */
18457 if (token->type == CPP_ELLIPSIS)
18459 /* Consume the `...' token. */
18460 cp_lexer_consume_token (parser->lexer);
18461 return NULL_TREE;
18463 else if (token->type == CPP_CLOSE_PAREN)
18464 /* There are no parameters. */
18466 #ifndef NO_IMPLICIT_EXTERN_C
18467 if (in_system_header_at (input_location)
18468 && current_class_type == NULL
18469 && current_lang_name == lang_name_c)
18470 return NULL_TREE;
18471 else
18472 #endif
18473 return void_list_node;
18475 /* Check for `(void)', too, which is a special case. */
18476 else if (token->keyword == RID_VOID
18477 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18478 == CPP_CLOSE_PAREN))
18480 /* Consume the `void' token. */
18481 cp_lexer_consume_token (parser->lexer);
18482 /* There are no parameters. */
18483 return void_list_node;
18486 /* Parse the parameter-declaration-list. */
18487 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18488 /* If a parse error occurred while parsing the
18489 parameter-declaration-list, then the entire
18490 parameter-declaration-clause is erroneous. */
18491 if (is_error)
18492 return NULL;
18494 /* Peek at the next token. */
18495 token = cp_lexer_peek_token (parser->lexer);
18496 /* If it's a `,', the clause should terminate with an ellipsis. */
18497 if (token->type == CPP_COMMA)
18499 /* Consume the `,'. */
18500 cp_lexer_consume_token (parser->lexer);
18501 /* Expect an ellipsis. */
18502 ellipsis_p
18503 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18505 /* It might also be `...' if the optional trailing `,' was
18506 omitted. */
18507 else if (token->type == CPP_ELLIPSIS)
18509 /* Consume the `...' token. */
18510 cp_lexer_consume_token (parser->lexer);
18511 /* And remember that we saw it. */
18512 ellipsis_p = true;
18514 else
18515 ellipsis_p = false;
18517 /* Finish the parameter list. */
18518 if (!ellipsis_p)
18519 parameters = chainon (parameters, void_list_node);
18521 return parameters;
18524 /* Parse a parameter-declaration-list.
18526 parameter-declaration-list:
18527 parameter-declaration
18528 parameter-declaration-list , parameter-declaration
18530 Returns a representation of the parameter-declaration-list, as for
18531 cp_parser_parameter_declaration_clause. However, the
18532 `void_list_node' is never appended to the list. Upon return,
18533 *IS_ERROR will be true iff an error occurred. */
18535 static tree
18536 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18538 tree parameters = NULL_TREE;
18539 tree *tail = &parameters;
18540 bool saved_in_unbraced_linkage_specification_p;
18541 int index = 0;
18543 /* Assume all will go well. */
18544 *is_error = false;
18545 /* The special considerations that apply to a function within an
18546 unbraced linkage specifications do not apply to the parameters
18547 to the function. */
18548 saved_in_unbraced_linkage_specification_p
18549 = parser->in_unbraced_linkage_specification_p;
18550 parser->in_unbraced_linkage_specification_p = false;
18552 /* Look for more parameters. */
18553 while (true)
18555 cp_parameter_declarator *parameter;
18556 tree decl = error_mark_node;
18557 bool parenthesized_p = false;
18558 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18559 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18560 (current_template_parms)) : 0);
18562 /* Parse the parameter. */
18563 parameter
18564 = cp_parser_parameter_declaration (parser,
18565 /*template_parm_p=*/false,
18566 &parenthesized_p);
18568 /* We don't know yet if the enclosing context is deprecated, so wait
18569 and warn in grokparms if appropriate. */
18570 deprecated_state = DEPRECATED_SUPPRESS;
18572 if (parameter)
18574 /* If a function parameter pack was specified and an implicit template
18575 parameter was introduced during cp_parser_parameter_declaration,
18576 change any implicit parameters introduced into packs. */
18577 if (parser->implicit_template_parms
18578 && parameter->declarator
18579 && parameter->declarator->parameter_pack_p)
18581 int latest_template_parm_idx = TREE_VEC_LENGTH
18582 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18584 if (latest_template_parm_idx != template_parm_idx)
18585 parameter->decl_specifiers.type = convert_generic_types_to_packs
18586 (parameter->decl_specifiers.type,
18587 template_parm_idx, latest_template_parm_idx);
18590 decl = grokdeclarator (parameter->declarator,
18591 &parameter->decl_specifiers,
18592 PARM,
18593 parameter->default_argument != NULL_TREE,
18594 &parameter->decl_specifiers.attributes);
18597 deprecated_state = DEPRECATED_NORMAL;
18599 /* If a parse error occurred parsing the parameter declaration,
18600 then the entire parameter-declaration-list is erroneous. */
18601 if (decl == error_mark_node)
18603 *is_error = true;
18604 parameters = error_mark_node;
18605 break;
18608 if (parameter->decl_specifiers.attributes)
18609 cplus_decl_attributes (&decl,
18610 parameter->decl_specifiers.attributes,
18612 if (DECL_NAME (decl))
18613 decl = pushdecl (decl);
18615 if (decl != error_mark_node)
18617 retrofit_lang_decl (decl);
18618 DECL_PARM_INDEX (decl) = ++index;
18619 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18622 /* Add the new parameter to the list. */
18623 *tail = build_tree_list (parameter->default_argument, decl);
18624 tail = &TREE_CHAIN (*tail);
18626 /* Peek at the next token. */
18627 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18628 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18629 /* These are for Objective-C++ */
18630 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18631 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18632 /* The parameter-declaration-list is complete. */
18633 break;
18634 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18636 cp_token *token;
18638 /* Peek at the next token. */
18639 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18640 /* If it's an ellipsis, then the list is complete. */
18641 if (token->type == CPP_ELLIPSIS)
18642 break;
18643 /* Otherwise, there must be more parameters. Consume the
18644 `,'. */
18645 cp_lexer_consume_token (parser->lexer);
18646 /* When parsing something like:
18648 int i(float f, double d)
18650 we can tell after seeing the declaration for "f" that we
18651 are not looking at an initialization of a variable "i",
18652 but rather at the declaration of a function "i".
18654 Due to the fact that the parsing of template arguments
18655 (as specified to a template-id) requires backtracking we
18656 cannot use this technique when inside a template argument
18657 list. */
18658 if (!parser->in_template_argument_list_p
18659 && !parser->in_type_id_in_expr_p
18660 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18661 /* However, a parameter-declaration of the form
18662 "float(f)" (which is a valid declaration of a
18663 parameter "f") can also be interpreted as an
18664 expression (the conversion of "f" to "float"). */
18665 && !parenthesized_p)
18666 cp_parser_commit_to_tentative_parse (parser);
18668 else
18670 cp_parser_error (parser, "expected %<,%> or %<...%>");
18671 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18672 cp_parser_skip_to_closing_parenthesis (parser,
18673 /*recovering=*/true,
18674 /*or_comma=*/false,
18675 /*consume_paren=*/false);
18676 break;
18680 parser->in_unbraced_linkage_specification_p
18681 = saved_in_unbraced_linkage_specification_p;
18683 /* Reset implicit_template_scope if we are about to leave the function
18684 parameter list that introduced it. Note that for out-of-line member
18685 definitions, there will be one or more class scopes before we get to
18686 the template parameter scope. */
18688 if (cp_binding_level *its = parser->implicit_template_scope)
18689 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18691 while (maybe_its->kind == sk_class)
18692 maybe_its = maybe_its->level_chain;
18693 if (maybe_its == its)
18695 parser->implicit_template_parms = 0;
18696 parser->implicit_template_scope = 0;
18700 return parameters;
18703 /* Parse a parameter declaration.
18705 parameter-declaration:
18706 decl-specifier-seq ... [opt] declarator
18707 decl-specifier-seq declarator = assignment-expression
18708 decl-specifier-seq ... [opt] abstract-declarator [opt]
18709 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18711 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18712 declares a template parameter. (In that case, a non-nested `>'
18713 token encountered during the parsing of the assignment-expression
18714 is not interpreted as a greater-than operator.)
18716 Returns a representation of the parameter, or NULL if an error
18717 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18718 true iff the declarator is of the form "(p)". */
18720 static cp_parameter_declarator *
18721 cp_parser_parameter_declaration (cp_parser *parser,
18722 bool template_parm_p,
18723 bool *parenthesized_p)
18725 int declares_class_or_enum;
18726 cp_decl_specifier_seq decl_specifiers;
18727 cp_declarator *declarator;
18728 tree default_argument;
18729 cp_token *token = NULL, *declarator_token_start = NULL;
18730 const char *saved_message;
18732 /* In a template parameter, `>' is not an operator.
18734 [temp.param]
18736 When parsing a default template-argument for a non-type
18737 template-parameter, the first non-nested `>' is taken as the end
18738 of the template parameter-list rather than a greater-than
18739 operator. */
18741 /* Type definitions may not appear in parameter types. */
18742 saved_message = parser->type_definition_forbidden_message;
18743 parser->type_definition_forbidden_message
18744 = G_("types may not be defined in parameter types");
18746 /* Parse the declaration-specifiers. */
18747 cp_parser_decl_specifier_seq (parser,
18748 CP_PARSER_FLAGS_NONE,
18749 &decl_specifiers,
18750 &declares_class_or_enum);
18752 /* Complain about missing 'typename' or other invalid type names. */
18753 if (!decl_specifiers.any_type_specifiers_p
18754 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18755 decl_specifiers.type = error_mark_node;
18757 /* If an error occurred, there's no reason to attempt to parse the
18758 rest of the declaration. */
18759 if (cp_parser_error_occurred (parser))
18761 parser->type_definition_forbidden_message = saved_message;
18762 return NULL;
18765 /* Peek at the next token. */
18766 token = cp_lexer_peek_token (parser->lexer);
18768 /* If the next token is a `)', `,', `=', `>', or `...', then there
18769 is no declarator. However, when variadic templates are enabled,
18770 there may be a declarator following `...'. */
18771 if (token->type == CPP_CLOSE_PAREN
18772 || token->type == CPP_COMMA
18773 || token->type == CPP_EQ
18774 || token->type == CPP_GREATER)
18776 declarator = NULL;
18777 if (parenthesized_p)
18778 *parenthesized_p = false;
18780 /* Otherwise, there should be a declarator. */
18781 else
18783 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18784 parser->default_arg_ok_p = false;
18786 /* After seeing a decl-specifier-seq, if the next token is not a
18787 "(", there is no possibility that the code is a valid
18788 expression. Therefore, if parsing tentatively, we commit at
18789 this point. */
18790 if (!parser->in_template_argument_list_p
18791 /* In an expression context, having seen:
18793 (int((char ...
18795 we cannot be sure whether we are looking at a
18796 function-type (taking a "char" as a parameter) or a cast
18797 of some object of type "char" to "int". */
18798 && !parser->in_type_id_in_expr_p
18799 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18800 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18801 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18802 cp_parser_commit_to_tentative_parse (parser);
18803 /* Parse the declarator. */
18804 declarator_token_start = token;
18805 declarator = cp_parser_declarator (parser,
18806 CP_PARSER_DECLARATOR_EITHER,
18807 /*ctor_dtor_or_conv_p=*/NULL,
18808 parenthesized_p,
18809 /*member_p=*/false,
18810 /*friend_p=*/false);
18811 parser->default_arg_ok_p = saved_default_arg_ok_p;
18812 /* After the declarator, allow more attributes. */
18813 decl_specifiers.attributes
18814 = chainon (decl_specifiers.attributes,
18815 cp_parser_attributes_opt (parser));
18818 /* If the next token is an ellipsis, and we have not seen a
18819 declarator name, and the type of the declarator contains parameter
18820 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18821 a parameter pack expansion expression. Otherwise, leave the
18822 ellipsis for a C-style variadic function. */
18823 token = cp_lexer_peek_token (parser->lexer);
18824 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18826 tree type = decl_specifiers.type;
18828 if (type && DECL_P (type))
18829 type = TREE_TYPE (type);
18831 if (type
18832 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18833 && declarator_can_be_parameter_pack (declarator)
18834 && (!declarator || !declarator->parameter_pack_p)
18835 && uses_parameter_packs (type))
18837 /* Consume the `...'. */
18838 cp_lexer_consume_token (parser->lexer);
18839 maybe_warn_variadic_templates ();
18841 /* Build a pack expansion type */
18842 if (declarator)
18843 declarator->parameter_pack_p = true;
18844 else
18845 decl_specifiers.type = make_pack_expansion (type);
18849 /* The restriction on defining new types applies only to the type
18850 of the parameter, not to the default argument. */
18851 parser->type_definition_forbidden_message = saved_message;
18853 /* If the next token is `=', then process a default argument. */
18854 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18856 token = cp_lexer_peek_token (parser->lexer);
18857 /* If we are defining a class, then the tokens that make up the
18858 default argument must be saved and processed later. */
18859 if (!template_parm_p && at_class_scope_p ()
18860 && TYPE_BEING_DEFINED (current_class_type)
18861 && !LAMBDA_TYPE_P (current_class_type))
18862 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18863 /* Outside of a class definition, we can just parse the
18864 assignment-expression. */
18865 else
18866 default_argument
18867 = cp_parser_default_argument (parser, template_parm_p);
18869 if (!parser->default_arg_ok_p)
18871 if (flag_permissive)
18872 warning (0, "deprecated use of default argument for parameter of non-function");
18873 else
18875 error_at (token->location,
18876 "default arguments are only "
18877 "permitted for function parameters");
18878 default_argument = NULL_TREE;
18881 else if ((declarator && declarator->parameter_pack_p)
18882 || (decl_specifiers.type
18883 && PACK_EXPANSION_P (decl_specifiers.type)))
18885 /* Find the name of the parameter pack. */
18886 cp_declarator *id_declarator = declarator;
18887 while (id_declarator && id_declarator->kind != cdk_id)
18888 id_declarator = id_declarator->declarator;
18890 if (id_declarator && id_declarator->kind == cdk_id)
18891 error_at (declarator_token_start->location,
18892 template_parm_p
18893 ? G_("template parameter pack %qD "
18894 "cannot have a default argument")
18895 : G_("parameter pack %qD cannot have "
18896 "a default argument"),
18897 id_declarator->u.id.unqualified_name);
18898 else
18899 error_at (declarator_token_start->location,
18900 template_parm_p
18901 ? G_("template parameter pack cannot have "
18902 "a default argument")
18903 : G_("parameter pack cannot have a "
18904 "default argument"));
18906 default_argument = NULL_TREE;
18909 else
18910 default_argument = NULL_TREE;
18912 return make_parameter_declarator (&decl_specifiers,
18913 declarator,
18914 default_argument);
18917 /* Parse a default argument and return it.
18919 TEMPLATE_PARM_P is true if this is a default argument for a
18920 non-type template parameter. */
18921 static tree
18922 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18924 tree default_argument = NULL_TREE;
18925 bool saved_greater_than_is_operator_p;
18926 bool saved_local_variables_forbidden_p;
18927 bool non_constant_p, is_direct_init;
18929 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18930 set correctly. */
18931 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18932 parser->greater_than_is_operator_p = !template_parm_p;
18933 /* Local variable names (and the `this' keyword) may not
18934 appear in a default argument. */
18935 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18936 parser->local_variables_forbidden_p = true;
18937 /* Parse the assignment-expression. */
18938 if (template_parm_p)
18939 push_deferring_access_checks (dk_no_deferred);
18940 tree saved_class_ptr = NULL_TREE;
18941 tree saved_class_ref = NULL_TREE;
18942 /* The "this" pointer is not valid in a default argument. */
18943 if (cfun)
18945 saved_class_ptr = current_class_ptr;
18946 cp_function_chain->x_current_class_ptr = NULL_TREE;
18947 saved_class_ref = current_class_ref;
18948 cp_function_chain->x_current_class_ref = NULL_TREE;
18950 default_argument
18951 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18952 /* Restore the "this" pointer. */
18953 if (cfun)
18955 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18956 cp_function_chain->x_current_class_ref = saved_class_ref;
18958 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18959 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18960 if (template_parm_p)
18961 pop_deferring_access_checks ();
18962 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18963 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18965 return default_argument;
18968 /* Parse a function-body.
18970 function-body:
18971 compound_statement */
18973 static void
18974 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18976 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18979 /* Parse a ctor-initializer-opt followed by a function-body. Return
18980 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18981 is true we are parsing a function-try-block. */
18983 static bool
18984 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18985 bool in_function_try_block)
18987 tree body, list;
18988 bool ctor_initializer_p;
18989 const bool check_body_p =
18990 DECL_CONSTRUCTOR_P (current_function_decl)
18991 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18992 tree last = NULL;
18994 /* Begin the function body. */
18995 body = begin_function_body ();
18996 /* Parse the optional ctor-initializer. */
18997 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18999 /* If we're parsing a constexpr constructor definition, we need
19000 to check that the constructor body is indeed empty. However,
19001 before we get to cp_parser_function_body lot of junk has been
19002 generated, so we can't just check that we have an empty block.
19003 Rather we take a snapshot of the outermost block, and check whether
19004 cp_parser_function_body changed its state. */
19005 if (check_body_p)
19007 list = cur_stmt_list;
19008 if (STATEMENT_LIST_TAIL (list))
19009 last = STATEMENT_LIST_TAIL (list)->stmt;
19011 /* Parse the function-body. */
19012 cp_parser_function_body (parser, in_function_try_block);
19013 if (check_body_p)
19014 check_constexpr_ctor_body (last, list);
19015 /* Finish the function body. */
19016 finish_function_body (body);
19018 return ctor_initializer_p;
19021 /* Parse an initializer.
19023 initializer:
19024 = initializer-clause
19025 ( expression-list )
19027 Returns an expression representing the initializer. If no
19028 initializer is present, NULL_TREE is returned.
19030 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19031 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19032 set to TRUE if there is no initializer present. If there is an
19033 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19034 is set to true; otherwise it is set to false. */
19036 static tree
19037 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19038 bool* non_constant_p)
19040 cp_token *token;
19041 tree init;
19043 /* Peek at the next token. */
19044 token = cp_lexer_peek_token (parser->lexer);
19046 /* Let our caller know whether or not this initializer was
19047 parenthesized. */
19048 *is_direct_init = (token->type != CPP_EQ);
19049 /* Assume that the initializer is constant. */
19050 *non_constant_p = false;
19052 if (token->type == CPP_EQ)
19054 /* Consume the `='. */
19055 cp_lexer_consume_token (parser->lexer);
19056 /* Parse the initializer-clause. */
19057 init = cp_parser_initializer_clause (parser, non_constant_p);
19059 else if (token->type == CPP_OPEN_PAREN)
19061 vec<tree, va_gc> *vec;
19062 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19063 /*cast_p=*/false,
19064 /*allow_expansion_p=*/true,
19065 non_constant_p);
19066 if (vec == NULL)
19067 return error_mark_node;
19068 init = build_tree_list_vec (vec);
19069 release_tree_vector (vec);
19071 else if (token->type == CPP_OPEN_BRACE)
19073 cp_lexer_set_source_position (parser->lexer);
19074 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19075 init = cp_parser_braced_list (parser, non_constant_p);
19076 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19078 else
19080 /* Anything else is an error. */
19081 cp_parser_error (parser, "expected initializer");
19082 init = error_mark_node;
19085 return init;
19088 /* Parse an initializer-clause.
19090 initializer-clause:
19091 assignment-expression
19092 braced-init-list
19094 Returns an expression representing the initializer.
19096 If the `assignment-expression' production is used the value
19097 returned is simply a representation for the expression.
19099 Otherwise, calls cp_parser_braced_list. */
19101 static tree
19102 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19104 tree initializer;
19106 /* Assume the expression is constant. */
19107 *non_constant_p = false;
19109 /* If it is not a `{', then we are looking at an
19110 assignment-expression. */
19111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19113 initializer
19114 = cp_parser_constant_expression (parser,
19115 /*allow_non_constant_p=*/true,
19116 non_constant_p);
19118 else
19119 initializer = cp_parser_braced_list (parser, non_constant_p);
19121 return initializer;
19124 /* Parse a brace-enclosed initializer list.
19126 braced-init-list:
19127 { initializer-list , [opt] }
19130 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19131 the elements of the initializer-list (or NULL, if the last
19132 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19133 NULL_TREE. There is no way to detect whether or not the optional
19134 trailing `,' was provided. NON_CONSTANT_P is as for
19135 cp_parser_initializer. */
19137 static tree
19138 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19140 tree initializer;
19142 /* Consume the `{' token. */
19143 cp_lexer_consume_token (parser->lexer);
19144 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19145 initializer = make_node (CONSTRUCTOR);
19146 /* If it's not a `}', then there is a non-trivial initializer. */
19147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19149 /* Parse the initializer list. */
19150 CONSTRUCTOR_ELTS (initializer)
19151 = cp_parser_initializer_list (parser, non_constant_p);
19152 /* A trailing `,' token is allowed. */
19153 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19154 cp_lexer_consume_token (parser->lexer);
19156 else
19157 *non_constant_p = false;
19158 /* Now, there should be a trailing `}'. */
19159 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19160 TREE_TYPE (initializer) = init_list_type_node;
19161 return initializer;
19164 /* Consume tokens up to, and including, the next non-nested closing `]'.
19165 Returns true iff we found a closing `]'. */
19167 static bool
19168 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19170 unsigned square_depth = 0;
19172 while (true)
19174 cp_token * token = cp_lexer_peek_token (parser->lexer);
19176 switch (token->type)
19178 case CPP_EOF:
19179 case CPP_PRAGMA_EOL:
19180 /* If we've run out of tokens, then there is no closing `]'. */
19181 return false;
19183 case CPP_OPEN_SQUARE:
19184 ++square_depth;
19185 break;
19187 case CPP_CLOSE_SQUARE:
19188 if (!square_depth--)
19190 cp_lexer_consume_token (parser->lexer);
19191 return true;
19193 break;
19195 default:
19196 break;
19199 /* Consume the token. */
19200 cp_lexer_consume_token (parser->lexer);
19204 /* Return true if we are looking at an array-designator, false otherwise. */
19206 static bool
19207 cp_parser_array_designator_p (cp_parser *parser)
19209 /* Consume the `['. */
19210 cp_lexer_consume_token (parser->lexer);
19212 cp_lexer_save_tokens (parser->lexer);
19214 /* Skip tokens until the next token is a closing square bracket.
19215 If we find the closing `]', and the next token is a `=', then
19216 we are looking at an array designator. */
19217 bool array_designator_p
19218 = (cp_parser_skip_to_closing_square_bracket (parser)
19219 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19221 /* Roll back the tokens we skipped. */
19222 cp_lexer_rollback_tokens (parser->lexer);
19224 return array_designator_p;
19227 /* Parse an initializer-list.
19229 initializer-list:
19230 initializer-clause ... [opt]
19231 initializer-list , initializer-clause ... [opt]
19233 GNU Extension:
19235 initializer-list:
19236 designation initializer-clause ...[opt]
19237 initializer-list , designation initializer-clause ...[opt]
19239 designation:
19240 . identifier =
19241 identifier :
19242 [ constant-expression ] =
19244 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19245 for the initializer. If the INDEX of the elt is non-NULL, it is the
19246 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19247 as for cp_parser_initializer. */
19249 static vec<constructor_elt, va_gc> *
19250 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19252 vec<constructor_elt, va_gc> *v = NULL;
19254 /* Assume all of the expressions are constant. */
19255 *non_constant_p = false;
19257 /* Parse the rest of the list. */
19258 while (true)
19260 cp_token *token;
19261 tree designator;
19262 tree initializer;
19263 bool clause_non_constant_p;
19265 /* If the next token is an identifier and the following one is a
19266 colon, we are looking at the GNU designated-initializer
19267 syntax. */
19268 if (cp_parser_allow_gnu_extensions_p (parser)
19269 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19270 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19272 /* Warn the user that they are using an extension. */
19273 pedwarn (input_location, OPT_Wpedantic,
19274 "ISO C++ does not allow designated initializers");
19275 /* Consume the identifier. */
19276 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19277 /* Consume the `:'. */
19278 cp_lexer_consume_token (parser->lexer);
19280 /* Also handle the C99 syntax, '. id ='. */
19281 else if (cp_parser_allow_gnu_extensions_p (parser)
19282 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19283 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19284 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19286 /* Warn the user that they are using an extension. */
19287 pedwarn (input_location, OPT_Wpedantic,
19288 "ISO C++ does not allow C99 designated initializers");
19289 /* Consume the `.'. */
19290 cp_lexer_consume_token (parser->lexer);
19291 /* Consume the identifier. */
19292 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19293 /* Consume the `='. */
19294 cp_lexer_consume_token (parser->lexer);
19296 /* Also handle C99 array designators, '[ const ] ='. */
19297 else if (cp_parser_allow_gnu_extensions_p (parser)
19298 && !c_dialect_objc ()
19299 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19301 /* In C++11, [ could start a lambda-introducer. */
19302 bool non_const = false;
19304 cp_parser_parse_tentatively (parser);
19306 if (!cp_parser_array_designator_p (parser))
19308 cp_parser_simulate_error (parser);
19309 designator = NULL_TREE;
19311 else
19313 designator = cp_parser_constant_expression (parser, true,
19314 &non_const);
19315 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19316 cp_parser_require (parser, CPP_EQ, RT_EQ);
19319 if (!cp_parser_parse_definitely (parser))
19320 designator = NULL_TREE;
19321 else if (non_const)
19322 require_potential_rvalue_constant_expression (designator);
19324 else
19325 designator = NULL_TREE;
19327 /* Parse the initializer. */
19328 initializer = cp_parser_initializer_clause (parser,
19329 &clause_non_constant_p);
19330 /* If any clause is non-constant, so is the entire initializer. */
19331 if (clause_non_constant_p)
19332 *non_constant_p = true;
19334 /* If we have an ellipsis, this is an initializer pack
19335 expansion. */
19336 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19338 /* Consume the `...'. */
19339 cp_lexer_consume_token (parser->lexer);
19341 /* Turn the initializer into an initializer expansion. */
19342 initializer = make_pack_expansion (initializer);
19345 /* Add it to the vector. */
19346 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19348 /* If the next token is not a comma, we have reached the end of
19349 the list. */
19350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19351 break;
19353 /* Peek at the next token. */
19354 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19355 /* If the next token is a `}', then we're still done. An
19356 initializer-clause can have a trailing `,' after the
19357 initializer-list and before the closing `}'. */
19358 if (token->type == CPP_CLOSE_BRACE)
19359 break;
19361 /* Consume the `,' token. */
19362 cp_lexer_consume_token (parser->lexer);
19365 return v;
19368 /* Classes [gram.class] */
19370 /* Parse a class-name.
19372 class-name:
19373 identifier
19374 template-id
19376 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19377 to indicate that names looked up in dependent types should be
19378 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19379 keyword has been used to indicate that the name that appears next
19380 is a template. TAG_TYPE indicates the explicit tag given before
19381 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19382 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19383 is the class being defined in a class-head.
19385 Returns the TYPE_DECL representing the class. */
19387 static tree
19388 cp_parser_class_name (cp_parser *parser,
19389 bool typename_keyword_p,
19390 bool template_keyword_p,
19391 enum tag_types tag_type,
19392 bool check_dependency_p,
19393 bool class_head_p,
19394 bool is_declaration)
19396 tree decl;
19397 tree scope;
19398 bool typename_p;
19399 cp_token *token;
19400 tree identifier = NULL_TREE;
19402 /* All class-names start with an identifier. */
19403 token = cp_lexer_peek_token (parser->lexer);
19404 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19406 cp_parser_error (parser, "expected class-name");
19407 return error_mark_node;
19410 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19411 to a template-id, so we save it here. */
19412 scope = parser->scope;
19413 if (scope == error_mark_node)
19414 return error_mark_node;
19416 /* Any name names a type if we're following the `typename' keyword
19417 in a qualified name where the enclosing scope is type-dependent. */
19418 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19419 && dependent_type_p (scope));
19420 /* Handle the common case (an identifier, but not a template-id)
19421 efficiently. */
19422 if (token->type == CPP_NAME
19423 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19425 cp_token *identifier_token;
19426 bool ambiguous_p;
19428 /* Look for the identifier. */
19429 identifier_token = cp_lexer_peek_token (parser->lexer);
19430 ambiguous_p = identifier_token->error_reported;
19431 identifier = cp_parser_identifier (parser);
19432 /* If the next token isn't an identifier, we are certainly not
19433 looking at a class-name. */
19434 if (identifier == error_mark_node)
19435 decl = error_mark_node;
19436 /* If we know this is a type-name, there's no need to look it
19437 up. */
19438 else if (typename_p)
19439 decl = identifier;
19440 else
19442 tree ambiguous_decls;
19443 /* If we already know that this lookup is ambiguous, then
19444 we've already issued an error message; there's no reason
19445 to check again. */
19446 if (ambiguous_p)
19448 cp_parser_simulate_error (parser);
19449 return error_mark_node;
19451 /* If the next token is a `::', then the name must be a type
19452 name.
19454 [basic.lookup.qual]
19456 During the lookup for a name preceding the :: scope
19457 resolution operator, object, function, and enumerator
19458 names are ignored. */
19459 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19460 tag_type = typename_type;
19461 /* Look up the name. */
19462 decl = cp_parser_lookup_name (parser, identifier,
19463 tag_type,
19464 /*is_template=*/false,
19465 /*is_namespace=*/false,
19466 check_dependency_p,
19467 &ambiguous_decls,
19468 identifier_token->location);
19469 if (ambiguous_decls)
19471 if (cp_parser_parsing_tentatively (parser))
19472 cp_parser_simulate_error (parser);
19473 return error_mark_node;
19477 else
19479 /* Try a template-id. */
19480 decl = cp_parser_template_id (parser, template_keyword_p,
19481 check_dependency_p,
19482 tag_type,
19483 is_declaration);
19484 if (decl == error_mark_node)
19485 return error_mark_node;
19488 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19490 /* If this is a typename, create a TYPENAME_TYPE. */
19491 if (typename_p && decl != error_mark_node)
19493 decl = make_typename_type (scope, decl, typename_type,
19494 /*complain=*/tf_error);
19495 if (decl != error_mark_node)
19496 decl = TYPE_NAME (decl);
19499 decl = strip_using_decl (decl);
19501 /* Check to see that it is really the name of a class. */
19502 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19503 && identifier_p (TREE_OPERAND (decl, 0))
19504 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19505 /* Situations like this:
19507 template <typename T> struct A {
19508 typename T::template X<int>::I i;
19511 are problematic. Is `T::template X<int>' a class-name? The
19512 standard does not seem to be definitive, but there is no other
19513 valid interpretation of the following `::'. Therefore, those
19514 names are considered class-names. */
19516 decl = make_typename_type (scope, decl, tag_type, tf_error);
19517 if (decl != error_mark_node)
19518 decl = TYPE_NAME (decl);
19520 else if (TREE_CODE (decl) != TYPE_DECL
19521 || TREE_TYPE (decl) == error_mark_node
19522 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19523 /* In Objective-C 2.0, a classname followed by '.' starts a
19524 dot-syntax expression, and it's not a type-name. */
19525 || (c_dialect_objc ()
19526 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19527 && objc_is_class_name (decl)))
19528 decl = error_mark_node;
19530 if (decl == error_mark_node)
19531 cp_parser_error (parser, "expected class-name");
19532 else if (identifier && !parser->scope)
19533 maybe_note_name_used_in_class (identifier, decl);
19535 return decl;
19538 /* Parse a class-specifier.
19540 class-specifier:
19541 class-head { member-specification [opt] }
19543 Returns the TREE_TYPE representing the class. */
19545 static tree
19546 cp_parser_class_specifier_1 (cp_parser* parser)
19548 tree type;
19549 tree attributes = NULL_TREE;
19550 bool nested_name_specifier_p;
19551 unsigned saved_num_template_parameter_lists;
19552 bool saved_in_function_body;
19553 unsigned char in_statement;
19554 bool in_switch_statement_p;
19555 bool saved_in_unbraced_linkage_specification_p;
19556 tree old_scope = NULL_TREE;
19557 tree scope = NULL_TREE;
19558 cp_token *closing_brace;
19560 push_deferring_access_checks (dk_no_deferred);
19562 /* Parse the class-head. */
19563 type = cp_parser_class_head (parser,
19564 &nested_name_specifier_p);
19565 /* If the class-head was a semantic disaster, skip the entire body
19566 of the class. */
19567 if (!type)
19569 cp_parser_skip_to_end_of_block_or_statement (parser);
19570 pop_deferring_access_checks ();
19571 return error_mark_node;
19574 /* Look for the `{'. */
19575 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19577 pop_deferring_access_checks ();
19578 return error_mark_node;
19581 cp_ensure_no_omp_declare_simd (parser);
19583 /* Issue an error message if type-definitions are forbidden here. */
19584 cp_parser_check_type_definition (parser);
19585 /* Remember that we are defining one more class. */
19586 ++parser->num_classes_being_defined;
19587 /* Inside the class, surrounding template-parameter-lists do not
19588 apply. */
19589 saved_num_template_parameter_lists
19590 = parser->num_template_parameter_lists;
19591 parser->num_template_parameter_lists = 0;
19592 /* We are not in a function body. */
19593 saved_in_function_body = parser->in_function_body;
19594 parser->in_function_body = false;
19595 /* Or in a loop. */
19596 in_statement = parser->in_statement;
19597 parser->in_statement = 0;
19598 /* Or in a switch. */
19599 in_switch_statement_p = parser->in_switch_statement_p;
19600 parser->in_switch_statement_p = false;
19601 /* We are not immediately inside an extern "lang" block. */
19602 saved_in_unbraced_linkage_specification_p
19603 = parser->in_unbraced_linkage_specification_p;
19604 parser->in_unbraced_linkage_specification_p = false;
19606 /* Start the class. */
19607 if (nested_name_specifier_p)
19609 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19610 old_scope = push_inner_scope (scope);
19612 type = begin_class_definition (type);
19614 if (type == error_mark_node)
19615 /* If the type is erroneous, skip the entire body of the class. */
19616 cp_parser_skip_to_closing_brace (parser);
19617 else
19618 /* Parse the member-specification. */
19619 cp_parser_member_specification_opt (parser);
19621 /* Look for the trailing `}'. */
19622 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19623 /* Look for trailing attributes to apply to this class. */
19624 if (cp_parser_allow_gnu_extensions_p (parser))
19625 attributes = cp_parser_gnu_attributes_opt (parser);
19626 if (type != error_mark_node)
19627 type = finish_struct (type, attributes);
19628 if (nested_name_specifier_p)
19629 pop_inner_scope (old_scope, scope);
19631 /* We've finished a type definition. Check for the common syntax
19632 error of forgetting a semicolon after the definition. We need to
19633 be careful, as we can't just check for not-a-semicolon and be done
19634 with it; the user might have typed:
19636 class X { } c = ...;
19637 class X { } *p = ...;
19639 and so forth. Instead, enumerate all the possible tokens that
19640 might follow this production; if we don't see one of them, then
19641 complain and silently insert the semicolon. */
19643 cp_token *token = cp_lexer_peek_token (parser->lexer);
19644 bool want_semicolon = true;
19646 if (cp_next_tokens_can_be_std_attribute_p (parser))
19647 /* Don't try to parse c++11 attributes here. As per the
19648 grammar, that should be a task for
19649 cp_parser_decl_specifier_seq. */
19650 want_semicolon = false;
19652 switch (token->type)
19654 case CPP_NAME:
19655 case CPP_SEMICOLON:
19656 case CPP_MULT:
19657 case CPP_AND:
19658 case CPP_OPEN_PAREN:
19659 case CPP_CLOSE_PAREN:
19660 case CPP_COMMA:
19661 want_semicolon = false;
19662 break;
19664 /* While it's legal for type qualifiers and storage class
19665 specifiers to follow type definitions in the grammar, only
19666 compiler testsuites contain code like that. Assume that if
19667 we see such code, then what we're really seeing is a case
19668 like:
19670 class X { }
19671 const <type> var = ...;
19675 class Y { }
19676 static <type> func (...) ...
19678 i.e. the qualifier or specifier applies to the next
19679 declaration. To do so, however, we need to look ahead one
19680 more token to see if *that* token is a type specifier.
19682 This code could be improved to handle:
19684 class Z { }
19685 static const <type> var = ...; */
19686 case CPP_KEYWORD:
19687 if (keyword_is_decl_specifier (token->keyword))
19689 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19691 /* Handling user-defined types here would be nice, but very
19692 tricky. */
19693 want_semicolon
19694 = (lookahead->type == CPP_KEYWORD
19695 && keyword_begins_type_specifier (lookahead->keyword));
19697 break;
19698 default:
19699 break;
19702 /* If we don't have a type, then something is very wrong and we
19703 shouldn't try to do anything clever. Likewise for not seeing the
19704 closing brace. */
19705 if (closing_brace && TYPE_P (type) && want_semicolon)
19707 cp_token_position prev
19708 = cp_lexer_previous_token_position (parser->lexer);
19709 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19710 location_t loc = prev_token->location;
19712 if (CLASSTYPE_DECLARED_CLASS (type))
19713 error_at (loc, "expected %<;%> after class definition");
19714 else if (TREE_CODE (type) == RECORD_TYPE)
19715 error_at (loc, "expected %<;%> after struct definition");
19716 else if (TREE_CODE (type) == UNION_TYPE)
19717 error_at (loc, "expected %<;%> after union definition");
19718 else
19719 gcc_unreachable ();
19721 /* Unget one token and smash it to look as though we encountered
19722 a semicolon in the input stream. */
19723 cp_lexer_set_token_position (parser->lexer, prev);
19724 token = cp_lexer_peek_token (parser->lexer);
19725 token->type = CPP_SEMICOLON;
19726 token->keyword = RID_MAX;
19730 /* If this class is not itself within the scope of another class,
19731 then we need to parse the bodies of all of the queued function
19732 definitions. Note that the queued functions defined in a class
19733 are not always processed immediately following the
19734 class-specifier for that class. Consider:
19736 struct A {
19737 struct B { void f() { sizeof (A); } };
19740 If `f' were processed before the processing of `A' were
19741 completed, there would be no way to compute the size of `A'.
19742 Note that the nesting we are interested in here is lexical --
19743 not the semantic nesting given by TYPE_CONTEXT. In particular,
19744 for:
19746 struct A { struct B; };
19747 struct A::B { void f() { } };
19749 there is no need to delay the parsing of `A::B::f'. */
19750 if (--parser->num_classes_being_defined == 0)
19752 tree decl;
19753 tree class_type = NULL_TREE;
19754 tree pushed_scope = NULL_TREE;
19755 unsigned ix;
19756 cp_default_arg_entry *e;
19757 tree save_ccp, save_ccr;
19759 /* In a first pass, parse default arguments to the functions.
19760 Then, in a second pass, parse the bodies of the functions.
19761 This two-phased approach handles cases like:
19763 struct S {
19764 void f() { g(); }
19765 void g(int i = 3);
19769 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19771 decl = e->decl;
19772 /* If there are default arguments that have not yet been processed,
19773 take care of them now. */
19774 if (class_type != e->class_type)
19776 if (pushed_scope)
19777 pop_scope (pushed_scope);
19778 class_type = e->class_type;
19779 pushed_scope = push_scope (class_type);
19781 /* Make sure that any template parameters are in scope. */
19782 maybe_begin_member_template_processing (decl);
19783 /* Parse the default argument expressions. */
19784 cp_parser_late_parsing_default_args (parser, decl);
19785 /* Remove any template parameters from the symbol table. */
19786 maybe_end_member_template_processing ();
19788 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19789 /* Now parse any NSDMIs. */
19790 save_ccp = current_class_ptr;
19791 save_ccr = current_class_ref;
19792 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19794 if (class_type != DECL_CONTEXT (decl))
19796 if (pushed_scope)
19797 pop_scope (pushed_scope);
19798 class_type = DECL_CONTEXT (decl);
19799 pushed_scope = push_scope (class_type);
19801 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19802 cp_parser_late_parsing_nsdmi (parser, decl);
19804 vec_safe_truncate (unparsed_nsdmis, 0);
19805 current_class_ptr = save_ccp;
19806 current_class_ref = save_ccr;
19807 if (pushed_scope)
19808 pop_scope (pushed_scope);
19810 /* Now do some post-NSDMI bookkeeping. */
19811 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19812 after_nsdmi_defaulted_late_checks (class_type);
19813 vec_safe_truncate (unparsed_classes, 0);
19814 after_nsdmi_defaulted_late_checks (type);
19816 /* Now parse the body of the functions. */
19817 if (flag_openmp)
19819 /* OpenMP UDRs need to be parsed before all other functions. */
19820 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19821 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19822 cp_parser_late_parsing_for_member (parser, decl);
19823 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19824 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19825 cp_parser_late_parsing_for_member (parser, decl);
19827 else
19828 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19829 cp_parser_late_parsing_for_member (parser, decl);
19830 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19832 else
19833 vec_safe_push (unparsed_classes, type);
19835 /* Put back any saved access checks. */
19836 pop_deferring_access_checks ();
19838 /* Restore saved state. */
19839 parser->in_switch_statement_p = in_switch_statement_p;
19840 parser->in_statement = in_statement;
19841 parser->in_function_body = saved_in_function_body;
19842 parser->num_template_parameter_lists
19843 = saved_num_template_parameter_lists;
19844 parser->in_unbraced_linkage_specification_p
19845 = saved_in_unbraced_linkage_specification_p;
19847 return type;
19850 static tree
19851 cp_parser_class_specifier (cp_parser* parser)
19853 tree ret;
19854 timevar_push (TV_PARSE_STRUCT);
19855 ret = cp_parser_class_specifier_1 (parser);
19856 timevar_pop (TV_PARSE_STRUCT);
19857 return ret;
19860 /* Parse a class-head.
19862 class-head:
19863 class-key identifier [opt] base-clause [opt]
19864 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19865 class-key nested-name-specifier [opt] template-id
19866 base-clause [opt]
19868 class-virt-specifier:
19869 final
19871 GNU Extensions:
19872 class-key attributes identifier [opt] base-clause [opt]
19873 class-key attributes nested-name-specifier identifier base-clause [opt]
19874 class-key attributes nested-name-specifier [opt] template-id
19875 base-clause [opt]
19877 Upon return BASES is initialized to the list of base classes (or
19878 NULL, if there are none) in the same form returned by
19879 cp_parser_base_clause.
19881 Returns the TYPE of the indicated class. Sets
19882 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19883 involving a nested-name-specifier was used, and FALSE otherwise.
19885 Returns error_mark_node if this is not a class-head.
19887 Returns NULL_TREE if the class-head is syntactically valid, but
19888 semantically invalid in a way that means we should skip the entire
19889 body of the class. */
19891 static tree
19892 cp_parser_class_head (cp_parser* parser,
19893 bool* nested_name_specifier_p)
19895 tree nested_name_specifier;
19896 enum tag_types class_key;
19897 tree id = NULL_TREE;
19898 tree type = NULL_TREE;
19899 tree attributes;
19900 tree bases;
19901 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19902 bool template_id_p = false;
19903 bool qualified_p = false;
19904 bool invalid_nested_name_p = false;
19905 bool invalid_explicit_specialization_p = false;
19906 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19907 tree pushed_scope = NULL_TREE;
19908 unsigned num_templates;
19909 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19910 /* Assume no nested-name-specifier will be present. */
19911 *nested_name_specifier_p = false;
19912 /* Assume no template parameter lists will be used in defining the
19913 type. */
19914 num_templates = 0;
19915 parser->colon_corrects_to_scope_p = false;
19917 /* Look for the class-key. */
19918 class_key = cp_parser_class_key (parser);
19919 if (class_key == none_type)
19920 return error_mark_node;
19922 /* Parse the attributes. */
19923 attributes = cp_parser_attributes_opt (parser);
19925 /* If the next token is `::', that is invalid -- but sometimes
19926 people do try to write:
19928 struct ::S {};
19930 Handle this gracefully by accepting the extra qualifier, and then
19931 issuing an error about it later if this really is a
19932 class-head. If it turns out just to be an elaborated type
19933 specifier, remain silent. */
19934 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19935 qualified_p = true;
19937 push_deferring_access_checks (dk_no_check);
19939 /* Determine the name of the class. Begin by looking for an
19940 optional nested-name-specifier. */
19941 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19942 nested_name_specifier
19943 = cp_parser_nested_name_specifier_opt (parser,
19944 /*typename_keyword_p=*/false,
19945 /*check_dependency_p=*/false,
19946 /*type_p=*/true,
19947 /*is_declaration=*/false);
19948 /* If there was a nested-name-specifier, then there *must* be an
19949 identifier. */
19950 if (nested_name_specifier)
19952 type_start_token = cp_lexer_peek_token (parser->lexer);
19953 /* Although the grammar says `identifier', it really means
19954 `class-name' or `template-name'. You are only allowed to
19955 define a class that has already been declared with this
19956 syntax.
19958 The proposed resolution for Core Issue 180 says that wherever
19959 you see `class T::X' you should treat `X' as a type-name.
19961 It is OK to define an inaccessible class; for example:
19963 class A { class B; };
19964 class A::B {};
19966 We do not know if we will see a class-name, or a
19967 template-name. We look for a class-name first, in case the
19968 class-name is a template-id; if we looked for the
19969 template-name first we would stop after the template-name. */
19970 cp_parser_parse_tentatively (parser);
19971 type = cp_parser_class_name (parser,
19972 /*typename_keyword_p=*/false,
19973 /*template_keyword_p=*/false,
19974 class_type,
19975 /*check_dependency_p=*/false,
19976 /*class_head_p=*/true,
19977 /*is_declaration=*/false);
19978 /* If that didn't work, ignore the nested-name-specifier. */
19979 if (!cp_parser_parse_definitely (parser))
19981 invalid_nested_name_p = true;
19982 type_start_token = cp_lexer_peek_token (parser->lexer);
19983 id = cp_parser_identifier (parser);
19984 if (id == error_mark_node)
19985 id = NULL_TREE;
19987 /* If we could not find a corresponding TYPE, treat this
19988 declaration like an unqualified declaration. */
19989 if (type == error_mark_node)
19990 nested_name_specifier = NULL_TREE;
19991 /* Otherwise, count the number of templates used in TYPE and its
19992 containing scopes. */
19993 else
19995 tree scope;
19997 for (scope = TREE_TYPE (type);
19998 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19999 scope = get_containing_scope (scope))
20000 if (TYPE_P (scope)
20001 && CLASS_TYPE_P (scope)
20002 && CLASSTYPE_TEMPLATE_INFO (scope)
20003 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20004 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20005 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20006 ++num_templates;
20009 /* Otherwise, the identifier is optional. */
20010 else
20012 /* We don't know whether what comes next is a template-id,
20013 an identifier, or nothing at all. */
20014 cp_parser_parse_tentatively (parser);
20015 /* Check for a template-id. */
20016 type_start_token = cp_lexer_peek_token (parser->lexer);
20017 id = cp_parser_template_id (parser,
20018 /*template_keyword_p=*/false,
20019 /*check_dependency_p=*/true,
20020 class_key,
20021 /*is_declaration=*/true);
20022 /* If that didn't work, it could still be an identifier. */
20023 if (!cp_parser_parse_definitely (parser))
20025 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20027 type_start_token = cp_lexer_peek_token (parser->lexer);
20028 id = cp_parser_identifier (parser);
20030 else
20031 id = NULL_TREE;
20033 else
20035 template_id_p = true;
20036 ++num_templates;
20040 pop_deferring_access_checks ();
20042 if (id)
20044 cp_parser_check_for_invalid_template_id (parser, id,
20045 class_key,
20046 type_start_token->location);
20048 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20050 /* If it's not a `:' or a `{' then we can't really be looking at a
20051 class-head, since a class-head only appears as part of a
20052 class-specifier. We have to detect this situation before calling
20053 xref_tag, since that has irreversible side-effects. */
20054 if (!cp_parser_next_token_starts_class_definition_p (parser))
20056 cp_parser_error (parser, "expected %<{%> or %<:%>");
20057 type = error_mark_node;
20058 goto out;
20061 /* At this point, we're going ahead with the class-specifier, even
20062 if some other problem occurs. */
20063 cp_parser_commit_to_tentative_parse (parser);
20064 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20066 cp_parser_error (parser,
20067 "cannot specify %<override%> for a class");
20068 type = error_mark_node;
20069 goto out;
20071 /* Issue the error about the overly-qualified name now. */
20072 if (qualified_p)
20074 cp_parser_error (parser,
20075 "global qualification of class name is invalid");
20076 type = error_mark_node;
20077 goto out;
20079 else if (invalid_nested_name_p)
20081 cp_parser_error (parser,
20082 "qualified name does not name a class");
20083 type = error_mark_node;
20084 goto out;
20086 else if (nested_name_specifier)
20088 tree scope;
20090 /* Reject typedef-names in class heads. */
20091 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20093 error_at (type_start_token->location,
20094 "invalid class name in declaration of %qD",
20095 type);
20096 type = NULL_TREE;
20097 goto done;
20100 /* Figure out in what scope the declaration is being placed. */
20101 scope = current_scope ();
20102 /* If that scope does not contain the scope in which the
20103 class was originally declared, the program is invalid. */
20104 if (scope && !is_ancestor (scope, nested_name_specifier))
20106 if (at_namespace_scope_p ())
20107 error_at (type_start_token->location,
20108 "declaration of %qD in namespace %qD which does not "
20109 "enclose %qD",
20110 type, scope, nested_name_specifier);
20111 else
20112 error_at (type_start_token->location,
20113 "declaration of %qD in %qD which does not enclose %qD",
20114 type, scope, nested_name_specifier);
20115 type = NULL_TREE;
20116 goto done;
20118 /* [dcl.meaning]
20120 A declarator-id shall not be qualified except for the
20121 definition of a ... nested class outside of its class
20122 ... [or] the definition or explicit instantiation of a
20123 class member of a namespace outside of its namespace. */
20124 if (scope == nested_name_specifier)
20126 permerror (nested_name_specifier_token_start->location,
20127 "extra qualification not allowed");
20128 nested_name_specifier = NULL_TREE;
20129 num_templates = 0;
20132 /* An explicit-specialization must be preceded by "template <>". If
20133 it is not, try to recover gracefully. */
20134 if (at_namespace_scope_p ()
20135 && parser->num_template_parameter_lists == 0
20136 && template_id_p)
20138 error_at (type_start_token->location,
20139 "an explicit specialization must be preceded by %<template <>%>");
20140 invalid_explicit_specialization_p = true;
20141 /* Take the same action that would have been taken by
20142 cp_parser_explicit_specialization. */
20143 ++parser->num_template_parameter_lists;
20144 begin_specialization ();
20146 /* There must be no "return" statements between this point and the
20147 end of this function; set "type "to the correct return value and
20148 use "goto done;" to return. */
20149 /* Make sure that the right number of template parameters were
20150 present. */
20151 if (!cp_parser_check_template_parameters (parser, num_templates,
20152 type_start_token->location,
20153 /*declarator=*/NULL))
20155 /* If something went wrong, there is no point in even trying to
20156 process the class-definition. */
20157 type = NULL_TREE;
20158 goto done;
20161 /* Look up the type. */
20162 if (template_id_p)
20164 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20165 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20166 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20168 error_at (type_start_token->location,
20169 "function template %qD redeclared as a class template", id);
20170 type = error_mark_node;
20172 else
20174 type = TREE_TYPE (id);
20175 type = maybe_process_partial_specialization (type);
20177 if (nested_name_specifier)
20178 pushed_scope = push_scope (nested_name_specifier);
20180 else if (nested_name_specifier)
20182 tree class_type;
20184 /* Given:
20186 template <typename T> struct S { struct T };
20187 template <typename T> struct S<T>::T { };
20189 we will get a TYPENAME_TYPE when processing the definition of
20190 `S::T'. We need to resolve it to the actual type before we
20191 try to define it. */
20192 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20194 class_type = resolve_typename_type (TREE_TYPE (type),
20195 /*only_current_p=*/false);
20196 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20197 type = TYPE_NAME (class_type);
20198 else
20200 cp_parser_error (parser, "could not resolve typename type");
20201 type = error_mark_node;
20205 if (maybe_process_partial_specialization (TREE_TYPE (type))
20206 == error_mark_node)
20208 type = NULL_TREE;
20209 goto done;
20212 class_type = current_class_type;
20213 /* Enter the scope indicated by the nested-name-specifier. */
20214 pushed_scope = push_scope (nested_name_specifier);
20215 /* Get the canonical version of this type. */
20216 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20217 /* Call push_template_decl if it seems like we should be defining a
20218 template either from the template headers or the type we're
20219 defining, so that we diagnose both extra and missing headers. */
20220 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20221 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
20222 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
20223 (TREE_TYPE (type)))))
20224 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20226 type = push_template_decl (type);
20227 if (type == error_mark_node)
20229 type = NULL_TREE;
20230 goto done;
20234 type = TREE_TYPE (type);
20235 *nested_name_specifier_p = true;
20237 else /* The name is not a nested name. */
20239 /* If the class was unnamed, create a dummy name. */
20240 if (!id)
20241 id = make_anon_name ();
20242 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20243 parser->num_template_parameter_lists);
20246 /* Indicate whether this class was declared as a `class' or as a
20247 `struct'. */
20248 if (TREE_CODE (type) == RECORD_TYPE)
20249 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20250 cp_parser_check_class_key (class_key, type);
20252 /* If this type was already complete, and we see another definition,
20253 that's an error. */
20254 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20256 error_at (type_start_token->location, "redefinition of %q#T",
20257 type);
20258 error_at (type_start_token->location, "previous definition of %q+#T",
20259 type);
20260 type = NULL_TREE;
20261 goto done;
20263 else if (type == error_mark_node)
20264 type = NULL_TREE;
20266 if (type)
20268 /* Apply attributes now, before any use of the class as a template
20269 argument in its base list. */
20270 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20271 fixup_attribute_variants (type);
20274 /* We will have entered the scope containing the class; the names of
20275 base classes should be looked up in that context. For example:
20277 struct A { struct B {}; struct C; };
20278 struct A::C : B {};
20280 is valid. */
20282 /* Get the list of base-classes, if there is one. */
20283 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20285 /* PR59482: enter the class scope so that base-specifiers are looked
20286 up correctly. */
20287 if (type)
20288 pushclass (type);
20289 bases = cp_parser_base_clause (parser);
20290 /* PR59482: get out of the previously pushed class scope so that the
20291 subsequent pops pop the right thing. */
20292 if (type)
20293 popclass ();
20295 else
20296 bases = NULL_TREE;
20298 /* If we're really defining a class, process the base classes.
20299 If they're invalid, fail. */
20300 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20301 && !xref_basetypes (type, bases))
20302 type = NULL_TREE;
20304 done:
20305 /* Leave the scope given by the nested-name-specifier. We will
20306 enter the class scope itself while processing the members. */
20307 if (pushed_scope)
20308 pop_scope (pushed_scope);
20310 if (invalid_explicit_specialization_p)
20312 end_specialization ();
20313 --parser->num_template_parameter_lists;
20316 if (type)
20317 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20318 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20319 CLASSTYPE_FINAL (type) = 1;
20320 out:
20321 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20322 return type;
20325 /* Parse a class-key.
20327 class-key:
20328 class
20329 struct
20330 union
20332 Returns the kind of class-key specified, or none_type to indicate
20333 error. */
20335 static enum tag_types
20336 cp_parser_class_key (cp_parser* parser)
20338 cp_token *token;
20339 enum tag_types tag_type;
20341 /* Look for the class-key. */
20342 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20343 if (!token)
20344 return none_type;
20346 /* Check to see if the TOKEN is a class-key. */
20347 tag_type = cp_parser_token_is_class_key (token);
20348 if (!tag_type)
20349 cp_parser_error (parser, "expected class-key");
20350 return tag_type;
20353 /* Parse a type-parameter-key.
20355 type-parameter-key:
20356 class
20357 typename
20360 static void
20361 cp_parser_type_parameter_key (cp_parser* parser)
20363 /* Look for the type-parameter-key. */
20364 enum tag_types tag_type = none_type;
20365 cp_token *token = cp_lexer_peek_token (parser->lexer);
20366 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20368 cp_lexer_consume_token (parser->lexer);
20369 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20370 /* typename is not allowed in a template template parameter
20371 by the standard until C++1Z. */
20372 pedwarn (token->location, OPT_Wpedantic,
20373 "ISO C++ forbids typename key in template template parameter;"
20374 " use -std=c++1z or -std=gnu++1z");
20376 else
20377 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20379 return;
20382 /* Parse an (optional) member-specification.
20384 member-specification:
20385 member-declaration member-specification [opt]
20386 access-specifier : member-specification [opt] */
20388 static void
20389 cp_parser_member_specification_opt (cp_parser* parser)
20391 while (true)
20393 cp_token *token;
20394 enum rid keyword;
20396 /* Peek at the next token. */
20397 token = cp_lexer_peek_token (parser->lexer);
20398 /* If it's a `}', or EOF then we've seen all the members. */
20399 if (token->type == CPP_CLOSE_BRACE
20400 || token->type == CPP_EOF
20401 || token->type == CPP_PRAGMA_EOL)
20402 break;
20404 /* See if this token is a keyword. */
20405 keyword = token->keyword;
20406 switch (keyword)
20408 case RID_PUBLIC:
20409 case RID_PROTECTED:
20410 case RID_PRIVATE:
20411 /* Consume the access-specifier. */
20412 cp_lexer_consume_token (parser->lexer);
20413 /* Remember which access-specifier is active. */
20414 current_access_specifier = token->u.value;
20415 /* Look for the `:'. */
20416 cp_parser_require (parser, CPP_COLON, RT_COLON);
20417 break;
20419 default:
20420 /* Accept #pragmas at class scope. */
20421 if (token->type == CPP_PRAGMA)
20423 cp_parser_pragma (parser, pragma_member);
20424 break;
20427 /* Otherwise, the next construction must be a
20428 member-declaration. */
20429 cp_parser_member_declaration (parser);
20434 /* Parse a member-declaration.
20436 member-declaration:
20437 decl-specifier-seq [opt] member-declarator-list [opt] ;
20438 function-definition ; [opt]
20439 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20440 using-declaration
20441 template-declaration
20442 alias-declaration
20444 member-declarator-list:
20445 member-declarator
20446 member-declarator-list , member-declarator
20448 member-declarator:
20449 declarator pure-specifier [opt]
20450 declarator constant-initializer [opt]
20451 identifier [opt] : constant-expression
20453 GNU Extensions:
20455 member-declaration:
20456 __extension__ member-declaration
20458 member-declarator:
20459 declarator attributes [opt] pure-specifier [opt]
20460 declarator attributes [opt] constant-initializer [opt]
20461 identifier [opt] attributes [opt] : constant-expression
20463 C++0x Extensions:
20465 member-declaration:
20466 static_assert-declaration */
20468 static void
20469 cp_parser_member_declaration (cp_parser* parser)
20471 cp_decl_specifier_seq decl_specifiers;
20472 tree prefix_attributes;
20473 tree decl;
20474 int declares_class_or_enum;
20475 bool friend_p;
20476 cp_token *token = NULL;
20477 cp_token *decl_spec_token_start = NULL;
20478 cp_token *initializer_token_start = NULL;
20479 int saved_pedantic;
20480 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20482 /* Check for the `__extension__' keyword. */
20483 if (cp_parser_extension_opt (parser, &saved_pedantic))
20485 /* Recurse. */
20486 cp_parser_member_declaration (parser);
20487 /* Restore the old value of the PEDANTIC flag. */
20488 pedantic = saved_pedantic;
20490 return;
20493 /* Check for a template-declaration. */
20494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20496 /* An explicit specialization here is an error condition, and we
20497 expect the specialization handler to detect and report this. */
20498 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20499 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20500 cp_parser_explicit_specialization (parser);
20501 else
20502 cp_parser_template_declaration (parser, /*member_p=*/true);
20504 return;
20507 /* Check for a using-declaration. */
20508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20510 if (cxx_dialect < cxx11)
20512 /* Parse the using-declaration. */
20513 cp_parser_using_declaration (parser,
20514 /*access_declaration_p=*/false);
20515 return;
20517 else
20519 tree decl;
20520 bool alias_decl_expected;
20521 cp_parser_parse_tentatively (parser);
20522 decl = cp_parser_alias_declaration (parser);
20523 /* Note that if we actually see the '=' token after the
20524 identifier, cp_parser_alias_declaration commits the
20525 tentative parse. In that case, we really expects an
20526 alias-declaration. Otherwise, we expect a using
20527 declaration. */
20528 alias_decl_expected =
20529 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20530 cp_parser_parse_definitely (parser);
20532 if (alias_decl_expected)
20533 finish_member_declaration (decl);
20534 else
20535 cp_parser_using_declaration (parser,
20536 /*access_declaration_p=*/false);
20537 return;
20541 /* Check for @defs. */
20542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20544 tree ivar, member;
20545 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20546 ivar = ivar_chains;
20547 while (ivar)
20549 member = ivar;
20550 ivar = TREE_CHAIN (member);
20551 TREE_CHAIN (member) = NULL_TREE;
20552 finish_member_declaration (member);
20554 return;
20557 /* If the next token is `static_assert' we have a static assertion. */
20558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20560 cp_parser_static_assert (parser, /*member_p=*/true);
20561 return;
20564 parser->colon_corrects_to_scope_p = false;
20566 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20567 goto out;
20569 /* Parse the decl-specifier-seq. */
20570 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20571 cp_parser_decl_specifier_seq (parser,
20572 CP_PARSER_FLAGS_OPTIONAL,
20573 &decl_specifiers,
20574 &declares_class_or_enum);
20575 /* Check for an invalid type-name. */
20576 if (!decl_specifiers.any_type_specifiers_p
20577 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20578 goto out;
20579 /* If there is no declarator, then the decl-specifier-seq should
20580 specify a type. */
20581 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20583 /* If there was no decl-specifier-seq, and the next token is a
20584 `;', then we have something like:
20586 struct S { ; };
20588 [class.mem]
20590 Each member-declaration shall declare at least one member
20591 name of the class. */
20592 if (!decl_specifiers.any_specifiers_p)
20594 cp_token *token = cp_lexer_peek_token (parser->lexer);
20595 if (!in_system_header_at (token->location))
20596 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20598 else
20600 tree type;
20602 /* See if this declaration is a friend. */
20603 friend_p = cp_parser_friend_p (&decl_specifiers);
20604 /* If there were decl-specifiers, check to see if there was
20605 a class-declaration. */
20606 type = check_tag_decl (&decl_specifiers,
20607 /*explicit_type_instantiation_p=*/false);
20608 /* Nested classes have already been added to the class, but
20609 a `friend' needs to be explicitly registered. */
20610 if (friend_p)
20612 /* If the `friend' keyword was present, the friend must
20613 be introduced with a class-key. */
20614 if (!declares_class_or_enum && cxx_dialect < cxx11)
20615 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20616 "in C++03 a class-key must be used "
20617 "when declaring a friend");
20618 /* In this case:
20620 template <typename T> struct A {
20621 friend struct A<T>::B;
20624 A<T>::B will be represented by a TYPENAME_TYPE, and
20625 therefore not recognized by check_tag_decl. */
20626 if (!type)
20628 type = decl_specifiers.type;
20629 if (type && TREE_CODE (type) == TYPE_DECL)
20630 type = TREE_TYPE (type);
20632 if (!type || !TYPE_P (type))
20633 error_at (decl_spec_token_start->location,
20634 "friend declaration does not name a class or "
20635 "function");
20636 else
20637 make_friend_class (current_class_type, type,
20638 /*complain=*/true);
20640 /* If there is no TYPE, an error message will already have
20641 been issued. */
20642 else if (!type || type == error_mark_node)
20644 /* An anonymous aggregate has to be handled specially; such
20645 a declaration really declares a data member (with a
20646 particular type), as opposed to a nested class. */
20647 else if (ANON_AGGR_TYPE_P (type))
20649 /* C++11 9.5/6. */
20650 if (decl_specifiers.storage_class != sc_none)
20651 error_at (decl_spec_token_start->location,
20652 "a storage class on an anonymous aggregate "
20653 "in class scope is not allowed");
20655 /* Remove constructors and such from TYPE, now that we
20656 know it is an anonymous aggregate. */
20657 fixup_anonymous_aggr (type);
20658 /* And make the corresponding data member. */
20659 decl = build_decl (decl_spec_token_start->location,
20660 FIELD_DECL, NULL_TREE, type);
20661 /* Add it to the class. */
20662 finish_member_declaration (decl);
20664 else
20665 cp_parser_check_access_in_redeclaration
20666 (TYPE_NAME (type),
20667 decl_spec_token_start->location);
20670 else
20672 bool assume_semicolon = false;
20674 /* Clear attributes from the decl_specifiers but keep them
20675 around as prefix attributes that apply them to the entity
20676 being declared. */
20677 prefix_attributes = decl_specifiers.attributes;
20678 decl_specifiers.attributes = NULL_TREE;
20680 /* See if these declarations will be friends. */
20681 friend_p = cp_parser_friend_p (&decl_specifiers);
20683 /* Keep going until we hit the `;' at the end of the
20684 declaration. */
20685 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20687 tree attributes = NULL_TREE;
20688 tree first_attribute;
20690 /* Peek at the next token. */
20691 token = cp_lexer_peek_token (parser->lexer);
20693 /* Check for a bitfield declaration. */
20694 if (token->type == CPP_COLON
20695 || (token->type == CPP_NAME
20696 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20697 == CPP_COLON))
20699 tree identifier;
20700 tree width;
20702 /* Get the name of the bitfield. Note that we cannot just
20703 check TOKEN here because it may have been invalidated by
20704 the call to cp_lexer_peek_nth_token above. */
20705 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20706 identifier = cp_parser_identifier (parser);
20707 else
20708 identifier = NULL_TREE;
20710 /* Consume the `:' token. */
20711 cp_lexer_consume_token (parser->lexer);
20712 /* Get the width of the bitfield. */
20713 width
20714 = cp_parser_constant_expression (parser,
20715 /*allow_non_constant=*/false,
20716 NULL);
20718 /* Look for attributes that apply to the bitfield. */
20719 attributes = cp_parser_attributes_opt (parser);
20720 /* Remember which attributes are prefix attributes and
20721 which are not. */
20722 first_attribute = attributes;
20723 /* Combine the attributes. */
20724 attributes = chainon (prefix_attributes, attributes);
20726 /* Create the bitfield declaration. */
20727 decl = grokbitfield (identifier
20728 ? make_id_declarator (NULL_TREE,
20729 identifier,
20730 sfk_none)
20731 : NULL,
20732 &decl_specifiers,
20733 width,
20734 attributes);
20736 else
20738 cp_declarator *declarator;
20739 tree initializer;
20740 tree asm_specification;
20741 int ctor_dtor_or_conv_p;
20743 /* Parse the declarator. */
20744 declarator
20745 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20746 &ctor_dtor_or_conv_p,
20747 /*parenthesized_p=*/NULL,
20748 /*member_p=*/true,
20749 friend_p);
20751 /* If something went wrong parsing the declarator, make sure
20752 that we at least consume some tokens. */
20753 if (declarator == cp_error_declarator)
20755 /* Skip to the end of the statement. */
20756 cp_parser_skip_to_end_of_statement (parser);
20757 /* If the next token is not a semicolon, that is
20758 probably because we just skipped over the body of
20759 a function. So, we consume a semicolon if
20760 present, but do not issue an error message if it
20761 is not present. */
20762 if (cp_lexer_next_token_is (parser->lexer,
20763 CPP_SEMICOLON))
20764 cp_lexer_consume_token (parser->lexer);
20765 goto out;
20768 if (declares_class_or_enum & 2)
20769 cp_parser_check_for_definition_in_return_type
20770 (declarator, decl_specifiers.type,
20771 decl_specifiers.locations[ds_type_spec]);
20773 /* Look for an asm-specification. */
20774 asm_specification = cp_parser_asm_specification_opt (parser);
20775 /* Look for attributes that apply to the declaration. */
20776 attributes = cp_parser_attributes_opt (parser);
20777 /* Remember which attributes are prefix attributes and
20778 which are not. */
20779 first_attribute = attributes;
20780 /* Combine the attributes. */
20781 attributes = chainon (prefix_attributes, attributes);
20783 /* If it's an `=', then we have a constant-initializer or a
20784 pure-specifier. It is not correct to parse the
20785 initializer before registering the member declaration
20786 since the member declaration should be in scope while
20787 its initializer is processed. However, the rest of the
20788 front end does not yet provide an interface that allows
20789 us to handle this correctly. */
20790 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20792 /* In [class.mem]:
20794 A pure-specifier shall be used only in the declaration of
20795 a virtual function.
20797 A member-declarator can contain a constant-initializer
20798 only if it declares a static member of integral or
20799 enumeration type.
20801 Therefore, if the DECLARATOR is for a function, we look
20802 for a pure-specifier; otherwise, we look for a
20803 constant-initializer. When we call `grokfield', it will
20804 perform more stringent semantics checks. */
20805 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20806 if (function_declarator_p (declarator)
20807 || (decl_specifiers.type
20808 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20809 && declarator->kind == cdk_id
20810 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20811 == FUNCTION_TYPE)))
20812 initializer = cp_parser_pure_specifier (parser);
20813 else if (decl_specifiers.storage_class != sc_static)
20814 initializer = cp_parser_save_nsdmi (parser);
20815 else if (cxx_dialect >= cxx11)
20817 bool nonconst;
20818 /* Don't require a constant rvalue in C++11, since we
20819 might want a reference constant. We'll enforce
20820 constancy later. */
20821 cp_lexer_consume_token (parser->lexer);
20822 /* Parse the initializer. */
20823 initializer = cp_parser_initializer_clause (parser,
20824 &nonconst);
20826 else
20827 /* Parse the initializer. */
20828 initializer = cp_parser_constant_initializer (parser);
20830 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20831 && !function_declarator_p (declarator))
20833 bool x;
20834 if (decl_specifiers.storage_class != sc_static)
20835 initializer = cp_parser_save_nsdmi (parser);
20836 else
20837 initializer = cp_parser_initializer (parser, &x, &x);
20839 /* Otherwise, there is no initializer. */
20840 else
20841 initializer = NULL_TREE;
20843 /* See if we are probably looking at a function
20844 definition. We are certainly not looking at a
20845 member-declarator. Calling `grokfield' has
20846 side-effects, so we must not do it unless we are sure
20847 that we are looking at a member-declarator. */
20848 if (cp_parser_token_starts_function_definition_p
20849 (cp_lexer_peek_token (parser->lexer)))
20851 /* The grammar does not allow a pure-specifier to be
20852 used when a member function is defined. (It is
20853 possible that this fact is an oversight in the
20854 standard, since a pure function may be defined
20855 outside of the class-specifier. */
20856 if (initializer && initializer_token_start)
20857 error_at (initializer_token_start->location,
20858 "pure-specifier on function-definition");
20859 decl = cp_parser_save_member_function_body (parser,
20860 &decl_specifiers,
20861 declarator,
20862 attributes);
20863 if (parser->fully_implicit_function_template_p)
20864 decl = finish_fully_implicit_template (parser, decl);
20865 /* If the member was not a friend, declare it here. */
20866 if (!friend_p)
20867 finish_member_declaration (decl);
20868 /* Peek at the next token. */
20869 token = cp_lexer_peek_token (parser->lexer);
20870 /* If the next token is a semicolon, consume it. */
20871 if (token->type == CPP_SEMICOLON)
20872 cp_lexer_consume_token (parser->lexer);
20873 goto out;
20875 else
20876 if (declarator->kind == cdk_function)
20877 declarator->id_loc = token->location;
20878 /* Create the declaration. */
20879 decl = grokfield (declarator, &decl_specifiers,
20880 initializer, /*init_const_expr_p=*/true,
20881 asm_specification, attributes);
20882 if (parser->fully_implicit_function_template_p)
20884 if (friend_p)
20885 finish_fully_implicit_template (parser, 0);
20886 else
20887 decl = finish_fully_implicit_template (parser, decl);
20891 cp_finalize_omp_declare_simd (parser, decl);
20893 /* Reset PREFIX_ATTRIBUTES. */
20894 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20895 attributes = TREE_CHAIN (attributes);
20896 if (attributes)
20897 TREE_CHAIN (attributes) = NULL_TREE;
20899 /* If there is any qualification still in effect, clear it
20900 now; we will be starting fresh with the next declarator. */
20901 parser->scope = NULL_TREE;
20902 parser->qualifying_scope = NULL_TREE;
20903 parser->object_scope = NULL_TREE;
20904 /* If it's a `,', then there are more declarators. */
20905 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20907 cp_lexer_consume_token (parser->lexer);
20908 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20910 cp_token *token = cp_lexer_previous_token (parser->lexer);
20911 error_at (token->location,
20912 "stray %<,%> at end of member declaration");
20915 /* If the next token isn't a `;', then we have a parse error. */
20916 else if (cp_lexer_next_token_is_not (parser->lexer,
20917 CPP_SEMICOLON))
20919 /* The next token might be a ways away from where the
20920 actual semicolon is missing. Find the previous token
20921 and use that for our error position. */
20922 cp_token *token = cp_lexer_previous_token (parser->lexer);
20923 error_at (token->location,
20924 "expected %<;%> at end of member declaration");
20926 /* Assume that the user meant to provide a semicolon. If
20927 we were to cp_parser_skip_to_end_of_statement, we might
20928 skip to a semicolon inside a member function definition
20929 and issue nonsensical error messages. */
20930 assume_semicolon = true;
20933 if (decl)
20935 /* Add DECL to the list of members. */
20936 if (!friend_p)
20937 finish_member_declaration (decl);
20939 if (TREE_CODE (decl) == FUNCTION_DECL)
20940 cp_parser_save_default_args (parser, decl);
20941 else if (TREE_CODE (decl) == FIELD_DECL
20942 && !DECL_C_BIT_FIELD (decl)
20943 && DECL_INITIAL (decl))
20944 /* Add DECL to the queue of NSDMI to be parsed later. */
20945 vec_safe_push (unparsed_nsdmis, decl);
20948 if (assume_semicolon)
20949 goto out;
20953 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20954 out:
20955 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20958 /* Parse a pure-specifier.
20960 pure-specifier:
20963 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20964 Otherwise, ERROR_MARK_NODE is returned. */
20966 static tree
20967 cp_parser_pure_specifier (cp_parser* parser)
20969 cp_token *token;
20971 /* Look for the `=' token. */
20972 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20973 return error_mark_node;
20974 /* Look for the `0' token. */
20975 token = cp_lexer_peek_token (parser->lexer);
20977 if (token->type == CPP_EOF
20978 || token->type == CPP_PRAGMA_EOL)
20979 return error_mark_node;
20981 cp_lexer_consume_token (parser->lexer);
20983 /* Accept = default or = delete in c++0x mode. */
20984 if (token->keyword == RID_DEFAULT
20985 || token->keyword == RID_DELETE)
20987 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20988 return token->u.value;
20991 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20992 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20994 cp_parser_error (parser,
20995 "invalid pure specifier (only %<= 0%> is allowed)");
20996 cp_parser_skip_to_end_of_statement (parser);
20997 return error_mark_node;
20999 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21001 error_at (token->location, "templates may not be %<virtual%>");
21002 return error_mark_node;
21005 return integer_zero_node;
21008 /* Parse a constant-initializer.
21010 constant-initializer:
21011 = constant-expression
21013 Returns a representation of the constant-expression. */
21015 static tree
21016 cp_parser_constant_initializer (cp_parser* parser)
21018 /* Look for the `=' token. */
21019 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21020 return error_mark_node;
21022 /* It is invalid to write:
21024 struct S { static const int i = { 7 }; };
21027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21029 cp_parser_error (parser,
21030 "a brace-enclosed initializer is not allowed here");
21031 /* Consume the opening brace. */
21032 cp_lexer_consume_token (parser->lexer);
21033 /* Skip the initializer. */
21034 cp_parser_skip_to_closing_brace (parser);
21035 /* Look for the trailing `}'. */
21036 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21038 return error_mark_node;
21041 return cp_parser_constant_expression (parser,
21042 /*allow_non_constant=*/false,
21043 NULL);
21046 /* Derived classes [gram.class.derived] */
21048 /* Parse a base-clause.
21050 base-clause:
21051 : base-specifier-list
21053 base-specifier-list:
21054 base-specifier ... [opt]
21055 base-specifier-list , base-specifier ... [opt]
21057 Returns a TREE_LIST representing the base-classes, in the order in
21058 which they were declared. The representation of each node is as
21059 described by cp_parser_base_specifier.
21061 In the case that no bases are specified, this function will return
21062 NULL_TREE, not ERROR_MARK_NODE. */
21064 static tree
21065 cp_parser_base_clause (cp_parser* parser)
21067 tree bases = NULL_TREE;
21069 /* Look for the `:' that begins the list. */
21070 cp_parser_require (parser, CPP_COLON, RT_COLON);
21072 /* Scan the base-specifier-list. */
21073 while (true)
21075 cp_token *token;
21076 tree base;
21077 bool pack_expansion_p = false;
21079 /* Look for the base-specifier. */
21080 base = cp_parser_base_specifier (parser);
21081 /* Look for the (optional) ellipsis. */
21082 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21084 /* Consume the `...'. */
21085 cp_lexer_consume_token (parser->lexer);
21087 pack_expansion_p = true;
21090 /* Add BASE to the front of the list. */
21091 if (base && base != error_mark_node)
21093 if (pack_expansion_p)
21094 /* Make this a pack expansion type. */
21095 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21097 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21099 TREE_CHAIN (base) = bases;
21100 bases = base;
21103 /* Peek at the next token. */
21104 token = cp_lexer_peek_token (parser->lexer);
21105 /* If it's not a comma, then the list is complete. */
21106 if (token->type != CPP_COMMA)
21107 break;
21108 /* Consume the `,'. */
21109 cp_lexer_consume_token (parser->lexer);
21112 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21113 base class had a qualified name. However, the next name that
21114 appears is certainly not qualified. */
21115 parser->scope = NULL_TREE;
21116 parser->qualifying_scope = NULL_TREE;
21117 parser->object_scope = NULL_TREE;
21119 return nreverse (bases);
21122 /* Parse a base-specifier.
21124 base-specifier:
21125 :: [opt] nested-name-specifier [opt] class-name
21126 virtual access-specifier [opt] :: [opt] nested-name-specifier
21127 [opt] class-name
21128 access-specifier virtual [opt] :: [opt] nested-name-specifier
21129 [opt] class-name
21131 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21132 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21133 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21134 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21136 static tree
21137 cp_parser_base_specifier (cp_parser* parser)
21139 cp_token *token;
21140 bool done = false;
21141 bool virtual_p = false;
21142 bool duplicate_virtual_error_issued_p = false;
21143 bool duplicate_access_error_issued_p = false;
21144 bool class_scope_p, template_p;
21145 tree access = access_default_node;
21146 tree type;
21148 /* Process the optional `virtual' and `access-specifier'. */
21149 while (!done)
21151 /* Peek at the next token. */
21152 token = cp_lexer_peek_token (parser->lexer);
21153 /* Process `virtual'. */
21154 switch (token->keyword)
21156 case RID_VIRTUAL:
21157 /* If `virtual' appears more than once, issue an error. */
21158 if (virtual_p && !duplicate_virtual_error_issued_p)
21160 cp_parser_error (parser,
21161 "%<virtual%> specified more than once in base-specified");
21162 duplicate_virtual_error_issued_p = true;
21165 virtual_p = true;
21167 /* Consume the `virtual' token. */
21168 cp_lexer_consume_token (parser->lexer);
21170 break;
21172 case RID_PUBLIC:
21173 case RID_PROTECTED:
21174 case RID_PRIVATE:
21175 /* If more than one access specifier appears, issue an
21176 error. */
21177 if (access != access_default_node
21178 && !duplicate_access_error_issued_p)
21180 cp_parser_error (parser,
21181 "more than one access specifier in base-specified");
21182 duplicate_access_error_issued_p = true;
21185 access = ridpointers[(int) token->keyword];
21187 /* Consume the access-specifier. */
21188 cp_lexer_consume_token (parser->lexer);
21190 break;
21192 default:
21193 done = true;
21194 break;
21197 /* It is not uncommon to see programs mechanically, erroneously, use
21198 the 'typename' keyword to denote (dependent) qualified types
21199 as base classes. */
21200 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21202 token = cp_lexer_peek_token (parser->lexer);
21203 if (!processing_template_decl)
21204 error_at (token->location,
21205 "keyword %<typename%> not allowed outside of templates");
21206 else
21207 error_at (token->location,
21208 "keyword %<typename%> not allowed in this context "
21209 "(the base class is implicitly a type)");
21210 cp_lexer_consume_token (parser->lexer);
21213 /* Look for the optional `::' operator. */
21214 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21215 /* Look for the nested-name-specifier. The simplest way to
21216 implement:
21218 [temp.res]
21220 The keyword `typename' is not permitted in a base-specifier or
21221 mem-initializer; in these contexts a qualified name that
21222 depends on a template-parameter is implicitly assumed to be a
21223 type name.
21225 is to pretend that we have seen the `typename' keyword at this
21226 point. */
21227 cp_parser_nested_name_specifier_opt (parser,
21228 /*typename_keyword_p=*/true,
21229 /*check_dependency_p=*/true,
21230 typename_type,
21231 /*is_declaration=*/true);
21232 /* If the base class is given by a qualified name, assume that names
21233 we see are type names or templates, as appropriate. */
21234 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21235 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21237 if (!parser->scope
21238 && cp_lexer_next_token_is_decltype (parser->lexer))
21239 /* DR 950 allows decltype as a base-specifier. */
21240 type = cp_parser_decltype (parser);
21241 else
21243 /* Otherwise, look for the class-name. */
21244 type = cp_parser_class_name (parser,
21245 class_scope_p,
21246 template_p,
21247 typename_type,
21248 /*check_dependency_p=*/true,
21249 /*class_head_p=*/false,
21250 /*is_declaration=*/true);
21251 type = TREE_TYPE (type);
21254 if (type == error_mark_node)
21255 return error_mark_node;
21257 return finish_base_specifier (type, access, virtual_p);
21260 /* Exception handling [gram.exception] */
21262 /* Parse an (optional) noexcept-specification.
21264 noexcept-specification:
21265 noexcept ( constant-expression ) [opt]
21267 If no noexcept-specification is present, returns NULL_TREE.
21268 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21269 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21270 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21271 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21272 in which case a boolean condition is returned instead. */
21274 static tree
21275 cp_parser_noexcept_specification_opt (cp_parser* parser,
21276 bool require_constexpr,
21277 bool* consumed_expr,
21278 bool return_cond)
21280 cp_token *token;
21281 const char *saved_message;
21283 /* Peek at the next token. */
21284 token = cp_lexer_peek_token (parser->lexer);
21286 /* Is it a noexcept-specification? */
21287 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21289 tree expr;
21290 cp_lexer_consume_token (parser->lexer);
21292 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21294 cp_lexer_consume_token (parser->lexer);
21296 if (require_constexpr)
21298 /* Types may not be defined in an exception-specification. */
21299 saved_message = parser->type_definition_forbidden_message;
21300 parser->type_definition_forbidden_message
21301 = G_("types may not be defined in an exception-specification");
21303 expr = cp_parser_constant_expression (parser, false, NULL);
21305 /* Restore the saved message. */
21306 parser->type_definition_forbidden_message = saved_message;
21308 else
21310 expr = cp_parser_expression (parser);
21311 *consumed_expr = true;
21314 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21316 else
21318 expr = boolean_true_node;
21319 if (!require_constexpr)
21320 *consumed_expr = false;
21323 /* We cannot build a noexcept-spec right away because this will check
21324 that expr is a constexpr. */
21325 if (!return_cond)
21326 return build_noexcept_spec (expr, tf_warning_or_error);
21327 else
21328 return expr;
21330 else
21331 return NULL_TREE;
21334 /* Parse an (optional) exception-specification.
21336 exception-specification:
21337 throw ( type-id-list [opt] )
21339 Returns a TREE_LIST representing the exception-specification. The
21340 TREE_VALUE of each node is a type. */
21342 static tree
21343 cp_parser_exception_specification_opt (cp_parser* parser)
21345 cp_token *token;
21346 tree type_id_list;
21347 const char *saved_message;
21349 /* Peek at the next token. */
21350 token = cp_lexer_peek_token (parser->lexer);
21352 /* Is it a noexcept-specification? */
21353 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21354 false);
21355 if (type_id_list != NULL_TREE)
21356 return type_id_list;
21358 /* If it's not `throw', then there's no exception-specification. */
21359 if (!cp_parser_is_keyword (token, RID_THROW))
21360 return NULL_TREE;
21362 #if 0
21363 /* Enable this once a lot of code has transitioned to noexcept? */
21364 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21365 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21366 "deprecated in C++0x; use %<noexcept%> instead");
21367 #endif
21369 /* Consume the `throw'. */
21370 cp_lexer_consume_token (parser->lexer);
21372 /* Look for the `('. */
21373 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21375 /* Peek at the next token. */
21376 token = cp_lexer_peek_token (parser->lexer);
21377 /* If it's not a `)', then there is a type-id-list. */
21378 if (token->type != CPP_CLOSE_PAREN)
21380 /* Types may not be defined in an exception-specification. */
21381 saved_message = parser->type_definition_forbidden_message;
21382 parser->type_definition_forbidden_message
21383 = G_("types may not be defined in an exception-specification");
21384 /* Parse the type-id-list. */
21385 type_id_list = cp_parser_type_id_list (parser);
21386 /* Restore the saved message. */
21387 parser->type_definition_forbidden_message = saved_message;
21389 else
21390 type_id_list = empty_except_spec;
21392 /* Look for the `)'. */
21393 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21395 return type_id_list;
21398 /* Parse an (optional) type-id-list.
21400 type-id-list:
21401 type-id ... [opt]
21402 type-id-list , type-id ... [opt]
21404 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21405 in the order that the types were presented. */
21407 static tree
21408 cp_parser_type_id_list (cp_parser* parser)
21410 tree types = NULL_TREE;
21412 while (true)
21414 cp_token *token;
21415 tree type;
21417 /* Get the next type-id. */
21418 type = cp_parser_type_id (parser);
21419 /* Parse the optional ellipsis. */
21420 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21422 /* Consume the `...'. */
21423 cp_lexer_consume_token (parser->lexer);
21425 /* Turn the type into a pack expansion expression. */
21426 type = make_pack_expansion (type);
21428 /* Add it to the list. */
21429 types = add_exception_specifier (types, type, /*complain=*/1);
21430 /* Peek at the next token. */
21431 token = cp_lexer_peek_token (parser->lexer);
21432 /* If it is not a `,', we are done. */
21433 if (token->type != CPP_COMMA)
21434 break;
21435 /* Consume the `,'. */
21436 cp_lexer_consume_token (parser->lexer);
21439 return nreverse (types);
21442 /* Parse a try-block.
21444 try-block:
21445 try compound-statement handler-seq */
21447 static tree
21448 cp_parser_try_block (cp_parser* parser)
21450 tree try_block;
21452 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21453 try_block = begin_try_block ();
21454 cp_parser_compound_statement (parser, NULL, true, false);
21455 finish_try_block (try_block);
21456 cp_parser_handler_seq (parser);
21457 finish_handler_sequence (try_block);
21459 return try_block;
21462 /* Parse a function-try-block.
21464 function-try-block:
21465 try ctor-initializer [opt] function-body handler-seq */
21467 static bool
21468 cp_parser_function_try_block (cp_parser* parser)
21470 tree compound_stmt;
21471 tree try_block;
21472 bool ctor_initializer_p;
21474 /* Look for the `try' keyword. */
21475 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21476 return false;
21477 /* Let the rest of the front end know where we are. */
21478 try_block = begin_function_try_block (&compound_stmt);
21479 /* Parse the function-body. */
21480 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21481 (parser, /*in_function_try_block=*/true);
21482 /* We're done with the `try' part. */
21483 finish_function_try_block (try_block);
21484 /* Parse the handlers. */
21485 cp_parser_handler_seq (parser);
21486 /* We're done with the handlers. */
21487 finish_function_handler_sequence (try_block, compound_stmt);
21489 return ctor_initializer_p;
21492 /* Parse a handler-seq.
21494 handler-seq:
21495 handler handler-seq [opt] */
21497 static void
21498 cp_parser_handler_seq (cp_parser* parser)
21500 while (true)
21502 cp_token *token;
21504 /* Parse the handler. */
21505 cp_parser_handler (parser);
21506 /* Peek at the next token. */
21507 token = cp_lexer_peek_token (parser->lexer);
21508 /* If it's not `catch' then there are no more handlers. */
21509 if (!cp_parser_is_keyword (token, RID_CATCH))
21510 break;
21514 /* Parse a handler.
21516 handler:
21517 catch ( exception-declaration ) compound-statement */
21519 static void
21520 cp_parser_handler (cp_parser* parser)
21522 tree handler;
21523 tree declaration;
21525 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21526 handler = begin_handler ();
21527 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21528 declaration = cp_parser_exception_declaration (parser);
21529 finish_handler_parms (declaration, handler);
21530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21531 cp_parser_compound_statement (parser, NULL, false, false);
21532 finish_handler (handler);
21535 /* Parse an exception-declaration.
21537 exception-declaration:
21538 type-specifier-seq declarator
21539 type-specifier-seq abstract-declarator
21540 type-specifier-seq
21543 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21544 ellipsis variant is used. */
21546 static tree
21547 cp_parser_exception_declaration (cp_parser* parser)
21549 cp_decl_specifier_seq type_specifiers;
21550 cp_declarator *declarator;
21551 const char *saved_message;
21553 /* If it's an ellipsis, it's easy to handle. */
21554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21556 /* Consume the `...' token. */
21557 cp_lexer_consume_token (parser->lexer);
21558 return NULL_TREE;
21561 /* Types may not be defined in exception-declarations. */
21562 saved_message = parser->type_definition_forbidden_message;
21563 parser->type_definition_forbidden_message
21564 = G_("types may not be defined in exception-declarations");
21566 /* Parse the type-specifier-seq. */
21567 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21568 /*is_trailing_return=*/false,
21569 &type_specifiers);
21570 /* If it's a `)', then there is no declarator. */
21571 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21572 declarator = NULL;
21573 else
21574 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21575 /*ctor_dtor_or_conv_p=*/NULL,
21576 /*parenthesized_p=*/NULL,
21577 /*member_p=*/false,
21578 /*friend_p=*/false);
21580 /* Restore the saved message. */
21581 parser->type_definition_forbidden_message = saved_message;
21583 if (!type_specifiers.any_specifiers_p)
21584 return error_mark_node;
21586 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21589 /* Parse a throw-expression.
21591 throw-expression:
21592 throw assignment-expression [opt]
21594 Returns a THROW_EXPR representing the throw-expression. */
21596 static tree
21597 cp_parser_throw_expression (cp_parser* parser)
21599 tree expression;
21600 cp_token* token;
21602 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21603 token = cp_lexer_peek_token (parser->lexer);
21604 /* Figure out whether or not there is an assignment-expression
21605 following the "throw" keyword. */
21606 if (token->type == CPP_COMMA
21607 || token->type == CPP_SEMICOLON
21608 || token->type == CPP_CLOSE_PAREN
21609 || token->type == CPP_CLOSE_SQUARE
21610 || token->type == CPP_CLOSE_BRACE
21611 || token->type == CPP_COLON)
21612 expression = NULL_TREE;
21613 else
21614 expression = cp_parser_assignment_expression (parser,
21615 /*cast_p=*/false, NULL);
21617 return build_throw (expression);
21620 /* GNU Extensions */
21622 /* Parse an (optional) asm-specification.
21624 asm-specification:
21625 asm ( string-literal )
21627 If the asm-specification is present, returns a STRING_CST
21628 corresponding to the string-literal. Otherwise, returns
21629 NULL_TREE. */
21631 static tree
21632 cp_parser_asm_specification_opt (cp_parser* parser)
21634 cp_token *token;
21635 tree asm_specification;
21637 /* Peek at the next token. */
21638 token = cp_lexer_peek_token (parser->lexer);
21639 /* If the next token isn't the `asm' keyword, then there's no
21640 asm-specification. */
21641 if (!cp_parser_is_keyword (token, RID_ASM))
21642 return NULL_TREE;
21644 /* Consume the `asm' token. */
21645 cp_lexer_consume_token (parser->lexer);
21646 /* Look for the `('. */
21647 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21649 /* Look for the string-literal. */
21650 asm_specification = cp_parser_string_literal (parser, false, false);
21652 /* Look for the `)'. */
21653 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21655 return asm_specification;
21658 /* Parse an asm-operand-list.
21660 asm-operand-list:
21661 asm-operand
21662 asm-operand-list , asm-operand
21664 asm-operand:
21665 string-literal ( expression )
21666 [ string-literal ] string-literal ( expression )
21668 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21669 each node is the expression. The TREE_PURPOSE is itself a
21670 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21671 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21672 is a STRING_CST for the string literal before the parenthesis. Returns
21673 ERROR_MARK_NODE if any of the operands are invalid. */
21675 static tree
21676 cp_parser_asm_operand_list (cp_parser* parser)
21678 tree asm_operands = NULL_TREE;
21679 bool invalid_operands = false;
21681 while (true)
21683 tree string_literal;
21684 tree expression;
21685 tree name;
21687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21689 /* Consume the `[' token. */
21690 cp_lexer_consume_token (parser->lexer);
21691 /* Read the operand name. */
21692 name = cp_parser_identifier (parser);
21693 if (name != error_mark_node)
21694 name = build_string (IDENTIFIER_LENGTH (name),
21695 IDENTIFIER_POINTER (name));
21696 /* Look for the closing `]'. */
21697 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21699 else
21700 name = NULL_TREE;
21701 /* Look for the string-literal. */
21702 string_literal = cp_parser_string_literal (parser, false, false);
21704 /* Look for the `('. */
21705 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21706 /* Parse the expression. */
21707 expression = cp_parser_expression (parser);
21708 /* Look for the `)'. */
21709 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21711 if (name == error_mark_node
21712 || string_literal == error_mark_node
21713 || expression == error_mark_node)
21714 invalid_operands = true;
21716 /* Add this operand to the list. */
21717 asm_operands = tree_cons (build_tree_list (name, string_literal),
21718 expression,
21719 asm_operands);
21720 /* If the next token is not a `,', there are no more
21721 operands. */
21722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21723 break;
21724 /* Consume the `,'. */
21725 cp_lexer_consume_token (parser->lexer);
21728 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21731 /* Parse an asm-clobber-list.
21733 asm-clobber-list:
21734 string-literal
21735 asm-clobber-list , string-literal
21737 Returns a TREE_LIST, indicating the clobbers in the order that they
21738 appeared. The TREE_VALUE of each node is a STRING_CST. */
21740 static tree
21741 cp_parser_asm_clobber_list (cp_parser* parser)
21743 tree clobbers = NULL_TREE;
21745 while (true)
21747 tree string_literal;
21749 /* Look for the string literal. */
21750 string_literal = cp_parser_string_literal (parser, false, false);
21751 /* Add it to the list. */
21752 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21753 /* If the next token is not a `,', then the list is
21754 complete. */
21755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21756 break;
21757 /* Consume the `,' token. */
21758 cp_lexer_consume_token (parser->lexer);
21761 return clobbers;
21764 /* Parse an asm-label-list.
21766 asm-label-list:
21767 identifier
21768 asm-label-list , identifier
21770 Returns a TREE_LIST, indicating the labels in the order that they
21771 appeared. The TREE_VALUE of each node is a label. */
21773 static tree
21774 cp_parser_asm_label_list (cp_parser* parser)
21776 tree labels = NULL_TREE;
21778 while (true)
21780 tree identifier, label, name;
21782 /* Look for the identifier. */
21783 identifier = cp_parser_identifier (parser);
21784 if (!error_operand_p (identifier))
21786 label = lookup_label (identifier);
21787 if (TREE_CODE (label) == LABEL_DECL)
21789 TREE_USED (label) = 1;
21790 check_goto (label);
21791 name = build_string (IDENTIFIER_LENGTH (identifier),
21792 IDENTIFIER_POINTER (identifier));
21793 labels = tree_cons (name, label, labels);
21796 /* If the next token is not a `,', then the list is
21797 complete. */
21798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21799 break;
21800 /* Consume the `,' token. */
21801 cp_lexer_consume_token (parser->lexer);
21804 return nreverse (labels);
21807 /* Return TRUE iff the next tokens in the stream are possibly the
21808 beginning of a GNU extension attribute. */
21810 static bool
21811 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21813 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21816 /* Return TRUE iff the next tokens in the stream are possibly the
21817 beginning of a standard C++-11 attribute specifier. */
21819 static bool
21820 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21822 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21825 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21826 beginning of a standard C++-11 attribute specifier. */
21828 static bool
21829 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21831 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21833 return (cxx_dialect >= cxx11
21834 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21835 || (token->type == CPP_OPEN_SQUARE
21836 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21837 && token->type == CPP_OPEN_SQUARE)));
21840 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21841 beginning of a GNU extension attribute. */
21843 static bool
21844 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21846 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21848 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21851 /* Return true iff the next tokens can be the beginning of either a
21852 GNU attribute list, or a standard C++11 attribute sequence. */
21854 static bool
21855 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21857 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21858 || cp_next_tokens_can_be_std_attribute_p (parser));
21861 /* Return true iff the next Nth tokens can be the beginning of either
21862 a GNU attribute list, or a standard C++11 attribute sequence. */
21864 static bool
21865 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21867 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21868 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21871 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21872 of GNU attributes, or return NULL. */
21874 static tree
21875 cp_parser_attributes_opt (cp_parser *parser)
21877 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21878 return cp_parser_gnu_attributes_opt (parser);
21879 return cp_parser_std_attribute_spec_seq (parser);
21882 #define CILK_SIMD_FN_CLAUSE_MASK \
21883 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21884 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21885 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21886 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21887 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21889 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21890 vector [(<clauses>)] */
21892 static void
21893 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21895 bool first_p = parser->cilk_simd_fn_info == NULL;
21896 cp_token *token = v_token;
21897 if (first_p)
21899 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21900 parser->cilk_simd_fn_info->error_seen = false;
21901 parser->cilk_simd_fn_info->fndecl_seen = false;
21902 parser->cilk_simd_fn_info->tokens = vNULL;
21904 int paren_scope = 0;
21905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21907 cp_lexer_consume_token (parser->lexer);
21908 v_token = cp_lexer_peek_token (parser->lexer);
21909 paren_scope++;
21911 while (paren_scope > 0)
21913 token = cp_lexer_peek_token (parser->lexer);
21914 if (token->type == CPP_OPEN_PAREN)
21915 paren_scope++;
21916 else if (token->type == CPP_CLOSE_PAREN)
21917 paren_scope--;
21918 /* Do not push the last ')' */
21919 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21920 cp_lexer_consume_token (parser->lexer);
21923 token->type = CPP_PRAGMA_EOL;
21924 parser->lexer->next_token = token;
21925 cp_lexer_consume_token (parser->lexer);
21927 struct cp_token_cache *cp
21928 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21929 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21932 /* Parse an (optional) series of attributes.
21934 attributes:
21935 attributes attribute
21937 attribute:
21938 __attribute__ (( attribute-list [opt] ))
21940 The return value is as for cp_parser_gnu_attribute_list. */
21942 static tree
21943 cp_parser_gnu_attributes_opt (cp_parser* parser)
21945 tree attributes = NULL_TREE;
21947 while (true)
21949 cp_token *token;
21950 tree attribute_list;
21951 bool ok = true;
21953 /* Peek at the next token. */
21954 token = cp_lexer_peek_token (parser->lexer);
21955 /* If it's not `__attribute__', then we're done. */
21956 if (token->keyword != RID_ATTRIBUTE)
21957 break;
21959 /* Consume the `__attribute__' keyword. */
21960 cp_lexer_consume_token (parser->lexer);
21961 /* Look for the two `(' tokens. */
21962 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21963 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21965 /* Peek at the next token. */
21966 token = cp_lexer_peek_token (parser->lexer);
21967 if (token->type != CPP_CLOSE_PAREN)
21968 /* Parse the attribute-list. */
21969 attribute_list = cp_parser_gnu_attribute_list (parser);
21970 else
21971 /* If the next token is a `)', then there is no attribute
21972 list. */
21973 attribute_list = NULL;
21975 /* Look for the two `)' tokens. */
21976 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21977 ok = false;
21978 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21979 ok = false;
21980 if (!ok)
21981 cp_parser_skip_to_end_of_statement (parser);
21983 /* Add these new attributes to the list. */
21984 attributes = chainon (attributes, attribute_list);
21987 return attributes;
21990 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21991 "__vector" or "__vector__." */
21993 static inline bool
21994 is_cilkplus_vector_p (tree name)
21996 if (flag_cilkplus && is_attribute_p ("vector", name))
21997 return true;
21998 return false;
22001 /* Parse a GNU attribute-list.
22003 attribute-list:
22004 attribute
22005 attribute-list , attribute
22007 attribute:
22008 identifier
22009 identifier ( identifier )
22010 identifier ( identifier , expression-list )
22011 identifier ( expression-list )
22013 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22014 to an attribute. The TREE_PURPOSE of each node is the identifier
22015 indicating which attribute is in use. The TREE_VALUE represents
22016 the arguments, if any. */
22018 static tree
22019 cp_parser_gnu_attribute_list (cp_parser* parser)
22021 tree attribute_list = NULL_TREE;
22022 bool save_translate_strings_p = parser->translate_strings_p;
22024 parser->translate_strings_p = false;
22025 while (true)
22027 cp_token *token;
22028 tree identifier;
22029 tree attribute;
22031 /* Look for the identifier. We also allow keywords here; for
22032 example `__attribute__ ((const))' is legal. */
22033 token = cp_lexer_peek_token (parser->lexer);
22034 if (token->type == CPP_NAME
22035 || token->type == CPP_KEYWORD)
22037 tree arguments = NULL_TREE;
22039 /* Consume the token, but save it since we need it for the
22040 SIMD enabled function parsing. */
22041 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22043 /* Save away the identifier that indicates which attribute
22044 this is. */
22045 identifier = (token->type == CPP_KEYWORD)
22046 /* For keywords, use the canonical spelling, not the
22047 parsed identifier. */
22048 ? ridpointers[(int) token->keyword]
22049 : id_token->u.value;
22051 attribute = build_tree_list (identifier, NULL_TREE);
22053 /* Peek at the next token. */
22054 token = cp_lexer_peek_token (parser->lexer);
22055 /* If it's an `(', then parse the attribute arguments. */
22056 if (token->type == CPP_OPEN_PAREN)
22058 vec<tree, va_gc> *vec;
22059 int attr_flag = (attribute_takes_identifier_p (identifier)
22060 ? id_attr : normal_attr);
22061 if (is_cilkplus_vector_p (identifier))
22063 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22064 continue;
22066 else
22067 vec = cp_parser_parenthesized_expression_list
22068 (parser, attr_flag, /*cast_p=*/false,
22069 /*allow_expansion_p=*/false,
22070 /*non_constant_p=*/NULL);
22071 if (vec == NULL)
22072 arguments = error_mark_node;
22073 else
22075 arguments = build_tree_list_vec (vec);
22076 release_tree_vector (vec);
22078 /* Save the arguments away. */
22079 TREE_VALUE (attribute) = arguments;
22081 else if (is_cilkplus_vector_p (identifier))
22083 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22084 continue;
22087 if (arguments != error_mark_node)
22089 /* Add this attribute to the list. */
22090 TREE_CHAIN (attribute) = attribute_list;
22091 attribute_list = attribute;
22094 token = cp_lexer_peek_token (parser->lexer);
22096 /* Now, look for more attributes. If the next token isn't a
22097 `,', we're done. */
22098 if (token->type != CPP_COMMA)
22099 break;
22101 /* Consume the comma and keep going. */
22102 cp_lexer_consume_token (parser->lexer);
22104 parser->translate_strings_p = save_translate_strings_p;
22106 /* We built up the list in reverse order. */
22107 return nreverse (attribute_list);
22110 /* Parse a standard C++11 attribute.
22112 The returned representation is a TREE_LIST which TREE_PURPOSE is
22113 the scoped name of the attribute, and the TREE_VALUE is its
22114 arguments list.
22116 Note that the scoped name of the attribute is itself a TREE_LIST
22117 which TREE_PURPOSE is the namespace of the attribute, and
22118 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22119 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22120 and which TREE_PURPOSE is directly the attribute name.
22122 Clients of the attribute code should use get_attribute_namespace
22123 and get_attribute_name to get the actual namespace and name of
22124 attributes, regardless of their being GNU or C++11 attributes.
22126 attribute:
22127 attribute-token attribute-argument-clause [opt]
22129 attribute-token:
22130 identifier
22131 attribute-scoped-token
22133 attribute-scoped-token:
22134 attribute-namespace :: identifier
22136 attribute-namespace:
22137 identifier
22139 attribute-argument-clause:
22140 ( balanced-token-seq )
22142 balanced-token-seq:
22143 balanced-token [opt]
22144 balanced-token-seq balanced-token
22146 balanced-token:
22147 ( balanced-token-seq )
22148 [ balanced-token-seq ]
22149 { balanced-token-seq }. */
22151 static tree
22152 cp_parser_std_attribute (cp_parser *parser)
22154 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22155 cp_token *token;
22157 /* First, parse name of the the attribute, a.k.a
22158 attribute-token. */
22160 token = cp_lexer_peek_token (parser->lexer);
22161 if (token->type == CPP_NAME)
22162 attr_id = token->u.value;
22163 else if (token->type == CPP_KEYWORD)
22164 attr_id = ridpointers[(int) token->keyword];
22165 else if (token->flags & NAMED_OP)
22166 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22168 if (attr_id == NULL_TREE)
22169 return NULL_TREE;
22171 cp_lexer_consume_token (parser->lexer);
22173 token = cp_lexer_peek_token (parser->lexer);
22174 if (token->type == CPP_SCOPE)
22176 /* We are seeing a scoped attribute token. */
22178 cp_lexer_consume_token (parser->lexer);
22179 attr_ns = attr_id;
22181 token = cp_lexer_consume_token (parser->lexer);
22182 if (token->type == CPP_NAME)
22183 attr_id = token->u.value;
22184 else if (token->type == CPP_KEYWORD)
22185 attr_id = ridpointers[(int) token->keyword];
22186 else
22188 error_at (token->location,
22189 "expected an identifier for the attribute name");
22190 return error_mark_node;
22192 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22193 NULL_TREE);
22194 token = cp_lexer_peek_token (parser->lexer);
22196 else
22198 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22199 NULL_TREE);
22200 /* C++11 noreturn attribute is equivalent to GNU's. */
22201 if (is_attribute_p ("noreturn", attr_id))
22202 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22203 /* C++14 deprecated attribute is equivalent to GNU's. */
22204 else if (cxx_dialect >= cxx14 && is_attribute_p ("deprecated", attr_id))
22205 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22208 /* Now parse the optional argument clause of the attribute. */
22210 if (token->type != CPP_OPEN_PAREN)
22211 return attribute;
22214 vec<tree, va_gc> *vec;
22215 int attr_flag = normal_attr;
22217 if (attr_ns == get_identifier ("gnu")
22218 && attribute_takes_identifier_p (attr_id))
22219 /* A GNU attribute that takes an identifier in parameter. */
22220 attr_flag = id_attr;
22222 vec = cp_parser_parenthesized_expression_list
22223 (parser, attr_flag, /*cast_p=*/false,
22224 /*allow_expansion_p=*/true,
22225 /*non_constant_p=*/NULL);
22226 if (vec == NULL)
22227 arguments = error_mark_node;
22228 else
22230 arguments = build_tree_list_vec (vec);
22231 release_tree_vector (vec);
22234 if (arguments == error_mark_node)
22235 attribute = error_mark_node;
22236 else
22237 TREE_VALUE (attribute) = arguments;
22240 return attribute;
22243 /* Parse a list of standard C++-11 attributes.
22245 attribute-list:
22246 attribute [opt]
22247 attribute-list , attribute[opt]
22248 attribute ...
22249 attribute-list , attribute ...
22252 static tree
22253 cp_parser_std_attribute_list (cp_parser *parser)
22255 tree attributes = NULL_TREE, attribute = NULL_TREE;
22256 cp_token *token = NULL;
22258 while (true)
22260 attribute = cp_parser_std_attribute (parser);
22261 if (attribute == error_mark_node)
22262 break;
22263 if (attribute != NULL_TREE)
22265 TREE_CHAIN (attribute) = attributes;
22266 attributes = attribute;
22268 token = cp_lexer_peek_token (parser->lexer);
22269 if (token->type != CPP_COMMA)
22270 break;
22271 cp_lexer_consume_token (parser->lexer);
22273 attributes = nreverse (attributes);
22274 return attributes;
22277 /* Parse a standard C++-11 attribute specifier.
22279 attribute-specifier:
22280 [ [ attribute-list ] ]
22281 alignment-specifier
22283 alignment-specifier:
22284 alignas ( type-id ... [opt] )
22285 alignas ( alignment-expression ... [opt] ). */
22287 static tree
22288 cp_parser_std_attribute_spec (cp_parser *parser)
22290 tree attributes = NULL_TREE;
22291 cp_token *token = cp_lexer_peek_token (parser->lexer);
22293 if (token->type == CPP_OPEN_SQUARE
22294 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22296 cp_lexer_consume_token (parser->lexer);
22297 cp_lexer_consume_token (parser->lexer);
22299 attributes = cp_parser_std_attribute_list (parser);
22301 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22302 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22303 cp_parser_skip_to_end_of_statement (parser);
22304 else
22305 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22306 when we are sure that we have actually parsed them. */
22307 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22309 else
22311 tree alignas_expr;
22313 /* Look for an alignment-specifier. */
22315 token = cp_lexer_peek_token (parser->lexer);
22317 if (token->type != CPP_KEYWORD
22318 || token->keyword != RID_ALIGNAS)
22319 return NULL_TREE;
22321 cp_lexer_consume_token (parser->lexer);
22322 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22324 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22326 cp_parser_error (parser, "expected %<(%>");
22327 return error_mark_node;
22330 cp_parser_parse_tentatively (parser);
22331 alignas_expr = cp_parser_type_id (parser);
22333 if (!cp_parser_parse_definitely (parser))
22335 gcc_assert (alignas_expr == error_mark_node
22336 || alignas_expr == NULL_TREE);
22338 alignas_expr =
22339 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22340 /**cp_id_kind=*/NULL);
22341 if (alignas_expr == error_mark_node)
22342 cp_parser_skip_to_end_of_statement (parser);
22343 if (alignas_expr == NULL_TREE
22344 || alignas_expr == error_mark_node)
22345 return alignas_expr;
22348 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22350 cp_parser_error (parser, "expected %<)%>");
22351 return error_mark_node;
22354 alignas_expr = cxx_alignas_expr (alignas_expr);
22356 /* Build the C++-11 representation of an 'aligned'
22357 attribute. */
22358 attributes =
22359 build_tree_list (build_tree_list (get_identifier ("gnu"),
22360 get_identifier ("aligned")),
22361 build_tree_list (NULL_TREE, alignas_expr));
22364 return attributes;
22367 /* Parse a standard C++-11 attribute-specifier-seq.
22369 attribute-specifier-seq:
22370 attribute-specifier-seq [opt] attribute-specifier
22373 static tree
22374 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22376 tree attr_specs = NULL;
22378 while (true)
22380 tree attr_spec = cp_parser_std_attribute_spec (parser);
22381 if (attr_spec == NULL_TREE)
22382 break;
22383 if (attr_spec == error_mark_node)
22384 return error_mark_node;
22386 TREE_CHAIN (attr_spec) = attr_specs;
22387 attr_specs = attr_spec;
22390 attr_specs = nreverse (attr_specs);
22391 return attr_specs;
22394 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22395 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22396 current value of the PEDANTIC flag, regardless of whether or not
22397 the `__extension__' keyword is present. The caller is responsible
22398 for restoring the value of the PEDANTIC flag. */
22400 static bool
22401 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22403 /* Save the old value of the PEDANTIC flag. */
22404 *saved_pedantic = pedantic;
22406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22408 /* Consume the `__extension__' token. */
22409 cp_lexer_consume_token (parser->lexer);
22410 /* We're not being pedantic while the `__extension__' keyword is
22411 in effect. */
22412 pedantic = 0;
22414 return true;
22417 return false;
22420 /* Parse a label declaration.
22422 label-declaration:
22423 __label__ label-declarator-seq ;
22425 label-declarator-seq:
22426 identifier , label-declarator-seq
22427 identifier */
22429 static void
22430 cp_parser_label_declaration (cp_parser* parser)
22432 /* Look for the `__label__' keyword. */
22433 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22435 while (true)
22437 tree identifier;
22439 /* Look for an identifier. */
22440 identifier = cp_parser_identifier (parser);
22441 /* If we failed, stop. */
22442 if (identifier == error_mark_node)
22443 break;
22444 /* Declare it as a label. */
22445 finish_label_decl (identifier);
22446 /* If the next token is a `;', stop. */
22447 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22448 break;
22449 /* Look for the `,' separating the label declarations. */
22450 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22453 /* Look for the final `;'. */
22454 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22457 /* Support Functions */
22459 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22460 NAME should have one of the representations used for an
22461 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22462 is returned. If PARSER->SCOPE is a dependent type, then a
22463 SCOPE_REF is returned.
22465 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22466 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22467 was formed. Abstractly, such entities should not be passed to this
22468 function, because they do not need to be looked up, but it is
22469 simpler to check for this special case here, rather than at the
22470 call-sites.
22472 In cases not explicitly covered above, this function returns a
22473 DECL, OVERLOAD, or baselink representing the result of the lookup.
22474 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22475 is returned.
22477 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22478 (e.g., "struct") that was used. In that case bindings that do not
22479 refer to types are ignored.
22481 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22482 ignored.
22484 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22485 are ignored.
22487 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22488 types.
22490 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22491 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22492 NULL_TREE otherwise. */
22494 static tree
22495 cp_parser_lookup_name (cp_parser *parser, tree name,
22496 enum tag_types tag_type,
22497 bool is_template,
22498 bool is_namespace,
22499 bool check_dependency,
22500 tree *ambiguous_decls,
22501 location_t name_location)
22503 tree decl;
22504 tree object_type = parser->context->object_type;
22506 /* Assume that the lookup will be unambiguous. */
22507 if (ambiguous_decls)
22508 *ambiguous_decls = NULL_TREE;
22510 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22511 no longer valid. Note that if we are parsing tentatively, and
22512 the parse fails, OBJECT_TYPE will be automatically restored. */
22513 parser->context->object_type = NULL_TREE;
22515 if (name == error_mark_node)
22516 return error_mark_node;
22518 /* A template-id has already been resolved; there is no lookup to
22519 do. */
22520 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22521 return name;
22522 if (BASELINK_P (name))
22524 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22525 == TEMPLATE_ID_EXPR);
22526 return name;
22529 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22530 it should already have been checked to make sure that the name
22531 used matches the type being destroyed. */
22532 if (TREE_CODE (name) == BIT_NOT_EXPR)
22534 tree type;
22536 /* Figure out to which type this destructor applies. */
22537 if (parser->scope)
22538 type = parser->scope;
22539 else if (object_type)
22540 type = object_type;
22541 else
22542 type = current_class_type;
22543 /* If that's not a class type, there is no destructor. */
22544 if (!type || !CLASS_TYPE_P (type))
22545 return error_mark_node;
22546 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22547 lazily_declare_fn (sfk_destructor, type);
22548 if (!CLASSTYPE_DESTRUCTORS (type))
22549 return error_mark_node;
22550 /* If it was a class type, return the destructor. */
22551 return CLASSTYPE_DESTRUCTORS (type);
22554 /* By this point, the NAME should be an ordinary identifier. If
22555 the id-expression was a qualified name, the qualifying scope is
22556 stored in PARSER->SCOPE at this point. */
22557 gcc_assert (identifier_p (name));
22559 /* Perform the lookup. */
22560 if (parser->scope)
22562 bool dependent_p;
22564 if (parser->scope == error_mark_node)
22565 return error_mark_node;
22567 /* If the SCOPE is dependent, the lookup must be deferred until
22568 the template is instantiated -- unless we are explicitly
22569 looking up names in uninstantiated templates. Even then, we
22570 cannot look up the name if the scope is not a class type; it
22571 might, for example, be a template type parameter. */
22572 dependent_p = (TYPE_P (parser->scope)
22573 && dependent_scope_p (parser->scope));
22574 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22575 && dependent_p)
22576 /* Defer lookup. */
22577 decl = error_mark_node;
22578 else
22580 tree pushed_scope = NULL_TREE;
22582 /* If PARSER->SCOPE is a dependent type, then it must be a
22583 class type, and we must not be checking dependencies;
22584 otherwise, we would have processed this lookup above. So
22585 that PARSER->SCOPE is not considered a dependent base by
22586 lookup_member, we must enter the scope here. */
22587 if (dependent_p)
22588 pushed_scope = push_scope (parser->scope);
22590 /* If the PARSER->SCOPE is a template specialization, it
22591 may be instantiated during name lookup. In that case,
22592 errors may be issued. Even if we rollback the current
22593 tentative parse, those errors are valid. */
22594 decl = lookup_qualified_name (parser->scope, name,
22595 tag_type != none_type,
22596 /*complain=*/true);
22598 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22599 lookup result and the nested-name-specifier nominates a class C:
22600 * if the name specified after the nested-name-specifier, when
22601 looked up in C, is the injected-class-name of C (Clause 9), or
22602 * if the name specified after the nested-name-specifier is the
22603 same as the identifier or the simple-template-id's template-
22604 name in the last component of the nested-name-specifier,
22605 the name is instead considered to name the constructor of
22606 class C. [ Note: for example, the constructor is not an
22607 acceptable lookup result in an elaborated-type-specifier so
22608 the constructor would not be used in place of the
22609 injected-class-name. --end note ] Such a constructor name
22610 shall be used only in the declarator-id of a declaration that
22611 names a constructor or in a using-declaration. */
22612 if (tag_type == none_type
22613 && DECL_SELF_REFERENCE_P (decl)
22614 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22615 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22616 tag_type != none_type,
22617 /*complain=*/true);
22619 /* If we have a single function from a using decl, pull it out. */
22620 if (TREE_CODE (decl) == OVERLOAD
22621 && !really_overloaded_fn (decl))
22622 decl = OVL_FUNCTION (decl);
22624 if (pushed_scope)
22625 pop_scope (pushed_scope);
22628 /* If the scope is a dependent type and either we deferred lookup or
22629 we did lookup but didn't find the name, rememeber the name. */
22630 if (decl == error_mark_node && TYPE_P (parser->scope)
22631 && dependent_type_p (parser->scope))
22633 if (tag_type)
22635 tree type;
22637 /* The resolution to Core Issue 180 says that `struct
22638 A::B' should be considered a type-name, even if `A'
22639 is dependent. */
22640 type = make_typename_type (parser->scope, name, tag_type,
22641 /*complain=*/tf_error);
22642 if (type != error_mark_node)
22643 decl = TYPE_NAME (type);
22645 else if (is_template
22646 && (cp_parser_next_token_ends_template_argument_p (parser)
22647 || cp_lexer_next_token_is (parser->lexer,
22648 CPP_CLOSE_PAREN)))
22649 decl = make_unbound_class_template (parser->scope,
22650 name, NULL_TREE,
22651 /*complain=*/tf_error);
22652 else
22653 decl = build_qualified_name (/*type=*/NULL_TREE,
22654 parser->scope, name,
22655 is_template);
22657 parser->qualifying_scope = parser->scope;
22658 parser->object_scope = NULL_TREE;
22660 else if (object_type)
22662 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22663 OBJECT_TYPE is not a class. */
22664 if (CLASS_TYPE_P (object_type))
22665 /* If the OBJECT_TYPE is a template specialization, it may
22666 be instantiated during name lookup. In that case, errors
22667 may be issued. Even if we rollback the current tentative
22668 parse, those errors are valid. */
22669 decl = lookup_member (object_type,
22670 name,
22671 /*protect=*/0,
22672 tag_type != none_type,
22673 tf_warning_or_error);
22674 else
22675 decl = NULL_TREE;
22677 if (!decl)
22678 /* Look it up in the enclosing context. */
22679 decl = lookup_name_real (name, tag_type != none_type,
22680 /*nonclass=*/0,
22681 /*block_p=*/true, is_namespace, 0);
22682 parser->object_scope = object_type;
22683 parser->qualifying_scope = NULL_TREE;
22685 else
22687 decl = lookup_name_real (name, tag_type != none_type,
22688 /*nonclass=*/0,
22689 /*block_p=*/true, is_namespace, 0);
22690 parser->qualifying_scope = NULL_TREE;
22691 parser->object_scope = NULL_TREE;
22694 /* If the lookup failed, let our caller know. */
22695 if (!decl || decl == error_mark_node)
22696 return error_mark_node;
22698 /* Pull out the template from an injected-class-name (or multiple). */
22699 if (is_template)
22700 decl = maybe_get_template_decl_from_type_decl (decl);
22702 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22703 if (TREE_CODE (decl) == TREE_LIST)
22705 if (ambiguous_decls)
22706 *ambiguous_decls = decl;
22707 /* The error message we have to print is too complicated for
22708 cp_parser_error, so we incorporate its actions directly. */
22709 if (!cp_parser_simulate_error (parser))
22711 error_at (name_location, "reference to %qD is ambiguous",
22712 name);
22713 print_candidates (decl);
22715 return error_mark_node;
22718 gcc_assert (DECL_P (decl)
22719 || TREE_CODE (decl) == OVERLOAD
22720 || TREE_CODE (decl) == SCOPE_REF
22721 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22722 || BASELINK_P (decl));
22724 /* If we have resolved the name of a member declaration, check to
22725 see if the declaration is accessible. When the name resolves to
22726 set of overloaded functions, accessibility is checked when
22727 overload resolution is done.
22729 During an explicit instantiation, access is not checked at all,
22730 as per [temp.explicit]. */
22731 if (DECL_P (decl))
22732 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22734 maybe_record_typedef_use (decl);
22736 return decl;
22739 /* Like cp_parser_lookup_name, but for use in the typical case where
22740 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22741 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22743 static tree
22744 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22746 return cp_parser_lookup_name (parser, name,
22747 none_type,
22748 /*is_template=*/false,
22749 /*is_namespace=*/false,
22750 /*check_dependency=*/true,
22751 /*ambiguous_decls=*/NULL,
22752 location);
22755 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22756 the current context, return the TYPE_DECL. If TAG_NAME_P is
22757 true, the DECL indicates the class being defined in a class-head,
22758 or declared in an elaborated-type-specifier.
22760 Otherwise, return DECL. */
22762 static tree
22763 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22765 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22766 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22768 struct A {
22769 template <typename T> struct B;
22772 template <typename T> struct A::B {};
22774 Similarly, in an elaborated-type-specifier:
22776 namespace N { struct X{}; }
22778 struct A {
22779 template <typename T> friend struct N::X;
22782 However, if the DECL refers to a class type, and we are in
22783 the scope of the class, then the name lookup automatically
22784 finds the TYPE_DECL created by build_self_reference rather
22785 than a TEMPLATE_DECL. For example, in:
22787 template <class T> struct S {
22788 S s;
22791 there is no need to handle such case. */
22793 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22794 return DECL_TEMPLATE_RESULT (decl);
22796 return decl;
22799 /* If too many, or too few, template-parameter lists apply to the
22800 declarator, issue an error message. Returns TRUE if all went well,
22801 and FALSE otherwise. */
22803 static bool
22804 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22805 cp_declarator *declarator,
22806 location_t declarator_location)
22808 switch (declarator->kind)
22810 case cdk_id:
22812 unsigned num_templates = 0;
22813 tree scope = declarator->u.id.qualifying_scope;
22815 if (scope)
22816 num_templates = num_template_headers_for_class (scope);
22817 else if (TREE_CODE (declarator->u.id.unqualified_name)
22818 == TEMPLATE_ID_EXPR)
22819 /* If the DECLARATOR has the form `X<y>' then it uses one
22820 additional level of template parameters. */
22821 ++num_templates;
22823 return cp_parser_check_template_parameters
22824 (parser, num_templates, declarator_location, declarator);
22827 case cdk_function:
22828 case cdk_array:
22829 case cdk_pointer:
22830 case cdk_reference:
22831 case cdk_ptrmem:
22832 return (cp_parser_check_declarator_template_parameters
22833 (parser, declarator->declarator, declarator_location));
22835 case cdk_error:
22836 return true;
22838 default:
22839 gcc_unreachable ();
22841 return false;
22844 /* NUM_TEMPLATES were used in the current declaration. If that is
22845 invalid, return FALSE and issue an error messages. Otherwise,
22846 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22847 declarator and we can print more accurate diagnostics. */
22849 static bool
22850 cp_parser_check_template_parameters (cp_parser* parser,
22851 unsigned num_templates,
22852 location_t location,
22853 cp_declarator *declarator)
22855 /* If there are the same number of template classes and parameter
22856 lists, that's OK. */
22857 if (parser->num_template_parameter_lists == num_templates)
22858 return true;
22859 /* If there are more, but only one more, then we are referring to a
22860 member template. That's OK too. */
22861 if (parser->num_template_parameter_lists == num_templates + 1)
22862 return true;
22863 /* If there are more template classes than parameter lists, we have
22864 something like:
22866 template <class T> void S<T>::R<T>::f (); */
22867 if (parser->num_template_parameter_lists < num_templates)
22869 if (declarator && !current_function_decl)
22870 error_at (location, "specializing member %<%T::%E%> "
22871 "requires %<template<>%> syntax",
22872 declarator->u.id.qualifying_scope,
22873 declarator->u.id.unqualified_name);
22874 else if (declarator)
22875 error_at (location, "invalid declaration of %<%T::%E%>",
22876 declarator->u.id.qualifying_scope,
22877 declarator->u.id.unqualified_name);
22878 else
22879 error_at (location, "too few template-parameter-lists");
22880 return false;
22882 /* Otherwise, there are too many template parameter lists. We have
22883 something like:
22885 template <class T> template <class U> void S::f(); */
22886 error_at (location, "too many template-parameter-lists");
22887 return false;
22890 /* Parse an optional `::' token indicating that the following name is
22891 from the global namespace. If so, PARSER->SCOPE is set to the
22892 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22893 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22894 Returns the new value of PARSER->SCOPE, if the `::' token is
22895 present, and NULL_TREE otherwise. */
22897 static tree
22898 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22900 cp_token *token;
22902 /* Peek at the next token. */
22903 token = cp_lexer_peek_token (parser->lexer);
22904 /* If we're looking at a `::' token then we're starting from the
22905 global namespace, not our current location. */
22906 if (token->type == CPP_SCOPE)
22908 /* Consume the `::' token. */
22909 cp_lexer_consume_token (parser->lexer);
22910 /* Set the SCOPE so that we know where to start the lookup. */
22911 parser->scope = global_namespace;
22912 parser->qualifying_scope = global_namespace;
22913 parser->object_scope = NULL_TREE;
22915 return parser->scope;
22917 else if (!current_scope_valid_p)
22919 parser->scope = NULL_TREE;
22920 parser->qualifying_scope = NULL_TREE;
22921 parser->object_scope = NULL_TREE;
22924 return NULL_TREE;
22927 /* Returns TRUE if the upcoming token sequence is the start of a
22928 constructor declarator. If FRIEND_P is true, the declarator is
22929 preceded by the `friend' specifier. */
22931 static bool
22932 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22934 bool constructor_p;
22935 bool outside_class_specifier_p;
22936 tree nested_name_specifier;
22937 cp_token *next_token;
22939 /* The common case is that this is not a constructor declarator, so
22940 try to avoid doing lots of work if at all possible. It's not
22941 valid declare a constructor at function scope. */
22942 if (parser->in_function_body)
22943 return false;
22944 /* And only certain tokens can begin a constructor declarator. */
22945 next_token = cp_lexer_peek_token (parser->lexer);
22946 if (next_token->type != CPP_NAME
22947 && next_token->type != CPP_SCOPE
22948 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22949 && next_token->type != CPP_TEMPLATE_ID)
22950 return false;
22952 /* Parse tentatively; we are going to roll back all of the tokens
22953 consumed here. */
22954 cp_parser_parse_tentatively (parser);
22955 /* Assume that we are looking at a constructor declarator. */
22956 constructor_p = true;
22958 /* Look for the optional `::' operator. */
22959 cp_parser_global_scope_opt (parser,
22960 /*current_scope_valid_p=*/false);
22961 /* Look for the nested-name-specifier. */
22962 nested_name_specifier
22963 = (cp_parser_nested_name_specifier_opt (parser,
22964 /*typename_keyword_p=*/false,
22965 /*check_dependency_p=*/false,
22966 /*type_p=*/false,
22967 /*is_declaration=*/false));
22969 outside_class_specifier_p = (!at_class_scope_p ()
22970 || !TYPE_BEING_DEFINED (current_class_type)
22971 || friend_p);
22973 /* Outside of a class-specifier, there must be a
22974 nested-name-specifier. */
22975 if (!nested_name_specifier && outside_class_specifier_p)
22976 constructor_p = false;
22977 else if (nested_name_specifier == error_mark_node)
22978 constructor_p = false;
22980 /* If we have a class scope, this is easy; DR 147 says that S::S always
22981 names the constructor, and no other qualified name could. */
22982 if (constructor_p && nested_name_specifier
22983 && CLASS_TYPE_P (nested_name_specifier))
22985 tree id = cp_parser_unqualified_id (parser,
22986 /*template_keyword_p=*/false,
22987 /*check_dependency_p=*/false,
22988 /*declarator_p=*/true,
22989 /*optional_p=*/false);
22990 if (is_overloaded_fn (id))
22991 id = DECL_NAME (get_first_fn (id));
22992 if (!constructor_name_p (id, nested_name_specifier))
22993 constructor_p = false;
22995 /* If we still think that this might be a constructor-declarator,
22996 look for a class-name. */
22997 else if (constructor_p)
22999 /* If we have:
23001 template <typename T> struct S {
23002 S();
23005 we must recognize that the nested `S' names a class. */
23006 tree type_decl;
23007 type_decl = cp_parser_class_name (parser,
23008 /*typename_keyword_p=*/false,
23009 /*template_keyword_p=*/false,
23010 none_type,
23011 /*check_dependency_p=*/false,
23012 /*class_head_p=*/false,
23013 /*is_declaration=*/false);
23014 /* If there was no class-name, then this is not a constructor.
23015 Otherwise, if we are in a class-specifier and we aren't
23016 handling a friend declaration, check that its type matches
23017 current_class_type (c++/38313). Note: error_mark_node
23018 is left alone for error recovery purposes. */
23019 constructor_p = (!cp_parser_error_occurred (parser)
23020 && (outside_class_specifier_p
23021 || type_decl == error_mark_node
23022 || same_type_p (current_class_type,
23023 TREE_TYPE (type_decl))));
23025 /* If we're still considering a constructor, we have to see a `(',
23026 to begin the parameter-declaration-clause, followed by either a
23027 `)', an `...', or a decl-specifier. We need to check for a
23028 type-specifier to avoid being fooled into thinking that:
23030 S (f) (int);
23032 is a constructor. (It is actually a function named `f' that
23033 takes one parameter (of type `int') and returns a value of type
23034 `S'. */
23035 if (constructor_p
23036 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23037 constructor_p = false;
23039 if (constructor_p
23040 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23041 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23042 /* A parameter declaration begins with a decl-specifier,
23043 which is either the "attribute" keyword, a storage class
23044 specifier, or (usually) a type-specifier. */
23045 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23047 tree type;
23048 tree pushed_scope = NULL_TREE;
23049 unsigned saved_num_template_parameter_lists;
23051 /* Names appearing in the type-specifier should be looked up
23052 in the scope of the class. */
23053 if (current_class_type)
23054 type = NULL_TREE;
23055 else
23057 type = TREE_TYPE (type_decl);
23058 if (TREE_CODE (type) == TYPENAME_TYPE)
23060 type = resolve_typename_type (type,
23061 /*only_current_p=*/false);
23062 if (TREE_CODE (type) == TYPENAME_TYPE)
23064 cp_parser_abort_tentative_parse (parser);
23065 return false;
23068 pushed_scope = push_scope (type);
23071 /* Inside the constructor parameter list, surrounding
23072 template-parameter-lists do not apply. */
23073 saved_num_template_parameter_lists
23074 = parser->num_template_parameter_lists;
23075 parser->num_template_parameter_lists = 0;
23077 /* Look for the type-specifier. */
23078 cp_parser_type_specifier (parser,
23079 CP_PARSER_FLAGS_NONE,
23080 /*decl_specs=*/NULL,
23081 /*is_declarator=*/true,
23082 /*declares_class_or_enum=*/NULL,
23083 /*is_cv_qualifier=*/NULL);
23085 parser->num_template_parameter_lists
23086 = saved_num_template_parameter_lists;
23088 /* Leave the scope of the class. */
23089 if (pushed_scope)
23090 pop_scope (pushed_scope);
23092 constructor_p = !cp_parser_error_occurred (parser);
23096 /* We did not really want to consume any tokens. */
23097 cp_parser_abort_tentative_parse (parser);
23099 return constructor_p;
23102 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23103 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23104 they must be performed once we are in the scope of the function.
23106 Returns the function defined. */
23108 static tree
23109 cp_parser_function_definition_from_specifiers_and_declarator
23110 (cp_parser* parser,
23111 cp_decl_specifier_seq *decl_specifiers,
23112 tree attributes,
23113 const cp_declarator *declarator)
23115 tree fn;
23116 bool success_p;
23118 /* Begin the function-definition. */
23119 success_p = start_function (decl_specifiers, declarator, attributes);
23121 /* The things we're about to see are not directly qualified by any
23122 template headers we've seen thus far. */
23123 reset_specialization ();
23125 /* If there were names looked up in the decl-specifier-seq that we
23126 did not check, check them now. We must wait until we are in the
23127 scope of the function to perform the checks, since the function
23128 might be a friend. */
23129 perform_deferred_access_checks (tf_warning_or_error);
23131 if (success_p)
23133 cp_finalize_omp_declare_simd (parser, current_function_decl);
23134 parser->omp_declare_simd = NULL;
23137 if (!success_p)
23139 /* Skip the entire function. */
23140 cp_parser_skip_to_end_of_block_or_statement (parser);
23141 fn = error_mark_node;
23143 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23145 /* Seen already, skip it. An error message has already been output. */
23146 cp_parser_skip_to_end_of_block_or_statement (parser);
23147 fn = current_function_decl;
23148 current_function_decl = NULL_TREE;
23149 /* If this is a function from a class, pop the nested class. */
23150 if (current_class_name)
23151 pop_nested_class ();
23153 else
23155 timevar_id_t tv;
23156 if (DECL_DECLARED_INLINE_P (current_function_decl))
23157 tv = TV_PARSE_INLINE;
23158 else
23159 tv = TV_PARSE_FUNC;
23160 timevar_push (tv);
23161 fn = cp_parser_function_definition_after_declarator (parser,
23162 /*inline_p=*/false);
23163 timevar_pop (tv);
23166 return fn;
23169 /* Parse the part of a function-definition that follows the
23170 declarator. INLINE_P is TRUE iff this function is an inline
23171 function defined within a class-specifier.
23173 Returns the function defined. */
23175 static tree
23176 cp_parser_function_definition_after_declarator (cp_parser* parser,
23177 bool inline_p)
23179 tree fn;
23180 bool ctor_initializer_p = false;
23181 bool saved_in_unbraced_linkage_specification_p;
23182 bool saved_in_function_body;
23183 unsigned saved_num_template_parameter_lists;
23184 cp_token *token;
23185 bool fully_implicit_function_template_p
23186 = parser->fully_implicit_function_template_p;
23187 parser->fully_implicit_function_template_p = false;
23188 tree implicit_template_parms
23189 = parser->implicit_template_parms;
23190 parser->implicit_template_parms = 0;
23191 cp_binding_level* implicit_template_scope
23192 = parser->implicit_template_scope;
23193 parser->implicit_template_scope = 0;
23195 saved_in_function_body = parser->in_function_body;
23196 parser->in_function_body = true;
23197 /* If the next token is `return', then the code may be trying to
23198 make use of the "named return value" extension that G++ used to
23199 support. */
23200 token = cp_lexer_peek_token (parser->lexer);
23201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23203 /* Consume the `return' keyword. */
23204 cp_lexer_consume_token (parser->lexer);
23205 /* Look for the identifier that indicates what value is to be
23206 returned. */
23207 cp_parser_identifier (parser);
23208 /* Issue an error message. */
23209 error_at (token->location,
23210 "named return values are no longer supported");
23211 /* Skip tokens until we reach the start of the function body. */
23212 while (true)
23214 cp_token *token = cp_lexer_peek_token (parser->lexer);
23215 if (token->type == CPP_OPEN_BRACE
23216 || token->type == CPP_EOF
23217 || token->type == CPP_PRAGMA_EOL)
23218 break;
23219 cp_lexer_consume_token (parser->lexer);
23222 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23223 anything declared inside `f'. */
23224 saved_in_unbraced_linkage_specification_p
23225 = parser->in_unbraced_linkage_specification_p;
23226 parser->in_unbraced_linkage_specification_p = false;
23227 /* Inside the function, surrounding template-parameter-lists do not
23228 apply. */
23229 saved_num_template_parameter_lists
23230 = parser->num_template_parameter_lists;
23231 parser->num_template_parameter_lists = 0;
23233 start_lambda_scope (current_function_decl);
23235 /* If the next token is `try', `__transaction_atomic', or
23236 `__transaction_relaxed`, then we are looking at either function-try-block
23237 or function-transaction-block. Note that all of these include the
23238 function-body. */
23239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23240 ctor_initializer_p = cp_parser_function_transaction (parser,
23241 RID_TRANSACTION_ATOMIC);
23242 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23243 RID_TRANSACTION_RELAXED))
23244 ctor_initializer_p = cp_parser_function_transaction (parser,
23245 RID_TRANSACTION_RELAXED);
23246 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23247 ctor_initializer_p = cp_parser_function_try_block (parser);
23248 else
23249 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23250 (parser, /*in_function_try_block=*/false);
23252 finish_lambda_scope ();
23254 /* Finish the function. */
23255 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23256 (inline_p ? 2 : 0));
23257 /* Generate code for it, if necessary. */
23258 expand_or_defer_fn (fn);
23259 /* Restore the saved values. */
23260 parser->in_unbraced_linkage_specification_p
23261 = saved_in_unbraced_linkage_specification_p;
23262 parser->num_template_parameter_lists
23263 = saved_num_template_parameter_lists;
23264 parser->in_function_body = saved_in_function_body;
23266 parser->fully_implicit_function_template_p
23267 = fully_implicit_function_template_p;
23268 parser->implicit_template_parms
23269 = implicit_template_parms;
23270 parser->implicit_template_scope
23271 = implicit_template_scope;
23273 if (parser->fully_implicit_function_template_p)
23274 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23276 return fn;
23279 /* Parse a template-declaration, assuming that the `export' (and
23280 `extern') keywords, if present, has already been scanned. MEMBER_P
23281 is as for cp_parser_template_declaration. */
23283 static void
23284 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23286 tree decl = NULL_TREE;
23287 vec<deferred_access_check, va_gc> *checks;
23288 tree parameter_list;
23289 bool friend_p = false;
23290 bool need_lang_pop;
23291 cp_token *token;
23293 /* Look for the `template' keyword. */
23294 token = cp_lexer_peek_token (parser->lexer);
23295 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23296 return;
23298 /* And the `<'. */
23299 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23300 return;
23301 if (at_class_scope_p () && current_function_decl)
23303 /* 14.5.2.2 [temp.mem]
23305 A local class shall not have member templates. */
23306 error_at (token->location,
23307 "invalid declaration of member template in local class");
23308 cp_parser_skip_to_end_of_block_or_statement (parser);
23309 return;
23311 /* [temp]
23313 A template ... shall not have C linkage. */
23314 if (current_lang_name == lang_name_c)
23316 error_at (token->location, "template with C linkage");
23317 /* Give it C++ linkage to avoid confusing other parts of the
23318 front end. */
23319 push_lang_context (lang_name_cplusplus);
23320 need_lang_pop = true;
23322 else
23323 need_lang_pop = false;
23325 /* We cannot perform access checks on the template parameter
23326 declarations until we know what is being declared, just as we
23327 cannot check the decl-specifier list. */
23328 push_deferring_access_checks (dk_deferred);
23330 /* If the next token is `>', then we have an invalid
23331 specialization. Rather than complain about an invalid template
23332 parameter, issue an error message here. */
23333 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23335 cp_parser_error (parser, "invalid explicit specialization");
23336 begin_specialization ();
23337 parameter_list = NULL_TREE;
23339 else
23341 /* Parse the template parameters. */
23342 parameter_list = cp_parser_template_parameter_list (parser);
23345 /* Get the deferred access checks from the parameter list. These
23346 will be checked once we know what is being declared, as for a
23347 member template the checks must be performed in the scope of the
23348 class containing the member. */
23349 checks = get_deferred_access_checks ();
23351 /* Look for the `>'. */
23352 cp_parser_skip_to_end_of_template_parameter_list (parser);
23353 /* We just processed one more parameter list. */
23354 ++parser->num_template_parameter_lists;
23355 /* If the next token is `template', there are more template
23356 parameters. */
23357 if (cp_lexer_next_token_is_keyword (parser->lexer,
23358 RID_TEMPLATE))
23359 cp_parser_template_declaration_after_export (parser, member_p);
23360 else if (cxx_dialect >= cxx11
23361 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23362 decl = cp_parser_alias_declaration (parser);
23363 else
23365 /* There are no access checks when parsing a template, as we do not
23366 know if a specialization will be a friend. */
23367 push_deferring_access_checks (dk_no_check);
23368 token = cp_lexer_peek_token (parser->lexer);
23369 decl = cp_parser_single_declaration (parser,
23370 checks,
23371 member_p,
23372 /*explicit_specialization_p=*/false,
23373 &friend_p);
23374 pop_deferring_access_checks ();
23376 /* If this is a member template declaration, let the front
23377 end know. */
23378 if (member_p && !friend_p && decl)
23380 if (TREE_CODE (decl) == TYPE_DECL)
23381 cp_parser_check_access_in_redeclaration (decl, token->location);
23383 decl = finish_member_template_decl (decl);
23385 else if (friend_p && decl
23386 && DECL_DECLARES_TYPE_P (decl))
23387 make_friend_class (current_class_type, TREE_TYPE (decl),
23388 /*complain=*/true);
23390 /* We are done with the current parameter list. */
23391 --parser->num_template_parameter_lists;
23393 pop_deferring_access_checks ();
23395 /* Finish up. */
23396 finish_template_decl (parameter_list);
23398 /* Check the template arguments for a literal operator template. */
23399 if (decl
23400 && DECL_DECLARES_FUNCTION_P (decl)
23401 && UDLIT_OPER_P (DECL_NAME (decl)))
23403 bool ok = true;
23404 if (parameter_list == NULL_TREE)
23405 ok = false;
23406 else
23408 int num_parms = TREE_VEC_LENGTH (parameter_list);
23409 if (num_parms == 1)
23411 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23412 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23413 if (TREE_TYPE (parm) != char_type_node
23414 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23415 ok = false;
23417 else if (num_parms == 2 && cxx_dialect >= cxx14)
23419 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23420 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23421 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23422 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23423 if (TREE_TYPE (parm) != TREE_TYPE (type)
23424 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23425 ok = false;
23427 else
23428 ok = false;
23430 if (!ok)
23432 if (cxx_dialect >= cxx14)
23433 error ("literal operator template %qD has invalid parameter list."
23434 " Expected non-type template argument pack <char...>"
23435 " or <typename CharT, CharT...>",
23436 decl);
23437 else
23438 error ("literal operator template %qD has invalid parameter list."
23439 " Expected non-type template argument pack <char...>",
23440 decl);
23443 /* Register member declarations. */
23444 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23445 finish_member_declaration (decl);
23446 /* For the erroneous case of a template with C linkage, we pushed an
23447 implicit C++ linkage scope; exit that scope now. */
23448 if (need_lang_pop)
23449 pop_lang_context ();
23450 /* If DECL is a function template, we must return to parse it later.
23451 (Even though there is no definition, there might be default
23452 arguments that need handling.) */
23453 if (member_p && decl
23454 && DECL_DECLARES_FUNCTION_P (decl))
23455 vec_safe_push (unparsed_funs_with_definitions, decl);
23458 /* Perform the deferred access checks from a template-parameter-list.
23459 CHECKS is a TREE_LIST of access checks, as returned by
23460 get_deferred_access_checks. */
23462 static void
23463 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23465 ++processing_template_parmlist;
23466 perform_access_checks (checks, tf_warning_or_error);
23467 --processing_template_parmlist;
23470 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23471 `function-definition' sequence that follows a template header.
23472 If MEMBER_P is true, this declaration appears in a class scope.
23474 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23475 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23477 static tree
23478 cp_parser_single_declaration (cp_parser* parser,
23479 vec<deferred_access_check, va_gc> *checks,
23480 bool member_p,
23481 bool explicit_specialization_p,
23482 bool* friend_p)
23484 int declares_class_or_enum;
23485 tree decl = NULL_TREE;
23486 cp_decl_specifier_seq decl_specifiers;
23487 bool function_definition_p = false;
23488 cp_token *decl_spec_token_start;
23490 /* This function is only used when processing a template
23491 declaration. */
23492 gcc_assert (innermost_scope_kind () == sk_template_parms
23493 || innermost_scope_kind () == sk_template_spec);
23495 /* Defer access checks until we know what is being declared. */
23496 push_deferring_access_checks (dk_deferred);
23498 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23499 alternative. */
23500 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23501 cp_parser_decl_specifier_seq (parser,
23502 CP_PARSER_FLAGS_OPTIONAL,
23503 &decl_specifiers,
23504 &declares_class_or_enum);
23505 if (friend_p)
23506 *friend_p = cp_parser_friend_p (&decl_specifiers);
23508 /* There are no template typedefs. */
23509 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23511 error_at (decl_spec_token_start->location,
23512 "template declaration of %<typedef%>");
23513 decl = error_mark_node;
23516 /* Gather up the access checks that occurred the
23517 decl-specifier-seq. */
23518 stop_deferring_access_checks ();
23520 /* Check for the declaration of a template class. */
23521 if (declares_class_or_enum)
23523 if (cp_parser_declares_only_class_p (parser))
23525 decl = shadow_tag (&decl_specifiers);
23527 /* In this case:
23529 struct C {
23530 friend template <typename T> struct A<T>::B;
23533 A<T>::B will be represented by a TYPENAME_TYPE, and
23534 therefore not recognized by shadow_tag. */
23535 if (friend_p && *friend_p
23536 && !decl
23537 && decl_specifiers.type
23538 && TYPE_P (decl_specifiers.type))
23539 decl = decl_specifiers.type;
23541 if (decl && decl != error_mark_node)
23542 decl = TYPE_NAME (decl);
23543 else
23544 decl = error_mark_node;
23546 /* Perform access checks for template parameters. */
23547 cp_parser_perform_template_parameter_access_checks (checks);
23551 /* Complain about missing 'typename' or other invalid type names. */
23552 if (!decl_specifiers.any_type_specifiers_p
23553 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23555 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23556 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23557 the rest of this declaration. */
23558 decl = error_mark_node;
23559 goto out;
23562 /* If it's not a template class, try for a template function. If
23563 the next token is a `;', then this declaration does not declare
23564 anything. But, if there were errors in the decl-specifiers, then
23565 the error might well have come from an attempted class-specifier.
23566 In that case, there's no need to warn about a missing declarator. */
23567 if (!decl
23568 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23569 || decl_specifiers.type != error_mark_node))
23571 decl = cp_parser_init_declarator (parser,
23572 &decl_specifiers,
23573 checks,
23574 /*function_definition_allowed_p=*/true,
23575 member_p,
23576 declares_class_or_enum,
23577 &function_definition_p,
23578 NULL);
23580 /* 7.1.1-1 [dcl.stc]
23582 A storage-class-specifier shall not be specified in an explicit
23583 specialization... */
23584 if (decl
23585 && explicit_specialization_p
23586 && decl_specifiers.storage_class != sc_none)
23588 error_at (decl_spec_token_start->location,
23589 "explicit template specialization cannot have a storage class");
23590 decl = error_mark_node;
23593 if (decl && VAR_P (decl))
23594 check_template_variable (decl);
23597 /* Look for a trailing `;' after the declaration. */
23598 if (!function_definition_p
23599 && (decl == error_mark_node
23600 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23601 cp_parser_skip_to_end_of_block_or_statement (parser);
23603 out:
23604 pop_deferring_access_checks ();
23606 /* Clear any current qualification; whatever comes next is the start
23607 of something new. */
23608 parser->scope = NULL_TREE;
23609 parser->qualifying_scope = NULL_TREE;
23610 parser->object_scope = NULL_TREE;
23612 return decl;
23615 /* Parse a cast-expression that is not the operand of a unary "&". */
23617 static tree
23618 cp_parser_simple_cast_expression (cp_parser *parser)
23620 return cp_parser_cast_expression (parser, /*address_p=*/false,
23621 /*cast_p=*/false, /*decltype*/false, NULL);
23624 /* Parse a functional cast to TYPE. Returns an expression
23625 representing the cast. */
23627 static tree
23628 cp_parser_functional_cast (cp_parser* parser, tree type)
23630 vec<tree, va_gc> *vec;
23631 tree expression_list;
23632 tree cast;
23633 bool nonconst_p;
23635 if (!type)
23636 type = error_mark_node;
23638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23640 cp_lexer_set_source_position (parser->lexer);
23641 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23642 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23643 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23644 if (TREE_CODE (type) == TYPE_DECL)
23645 type = TREE_TYPE (type);
23646 return finish_compound_literal (type, expression_list,
23647 tf_warning_or_error);
23651 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23652 /*cast_p=*/true,
23653 /*allow_expansion_p=*/true,
23654 /*non_constant_p=*/NULL);
23655 if (vec == NULL)
23656 expression_list = error_mark_node;
23657 else
23659 expression_list = build_tree_list_vec (vec);
23660 release_tree_vector (vec);
23663 cast = build_functional_cast (type, expression_list,
23664 tf_warning_or_error);
23665 /* [expr.const]/1: In an integral constant expression "only type
23666 conversions to integral or enumeration type can be used". */
23667 if (TREE_CODE (type) == TYPE_DECL)
23668 type = TREE_TYPE (type);
23669 if (cast != error_mark_node
23670 && !cast_valid_in_integral_constant_expression_p (type)
23671 && cp_parser_non_integral_constant_expression (parser,
23672 NIC_CONSTRUCTOR))
23673 return error_mark_node;
23674 return cast;
23677 /* Save the tokens that make up the body of a member function defined
23678 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23679 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23680 specifiers applied to the declaration. Returns the FUNCTION_DECL
23681 for the member function. */
23683 static tree
23684 cp_parser_save_member_function_body (cp_parser* parser,
23685 cp_decl_specifier_seq *decl_specifiers,
23686 cp_declarator *declarator,
23687 tree attributes)
23689 cp_token *first;
23690 cp_token *last;
23691 tree fn;
23693 /* Create the FUNCTION_DECL. */
23694 fn = grokmethod (decl_specifiers, declarator, attributes);
23695 cp_finalize_omp_declare_simd (parser, fn);
23696 /* If something went badly wrong, bail out now. */
23697 if (fn == error_mark_node)
23699 /* If there's a function-body, skip it. */
23700 if (cp_parser_token_starts_function_definition_p
23701 (cp_lexer_peek_token (parser->lexer)))
23702 cp_parser_skip_to_end_of_block_or_statement (parser);
23703 return error_mark_node;
23706 /* Remember it, if there default args to post process. */
23707 cp_parser_save_default_args (parser, fn);
23709 /* Save away the tokens that make up the body of the
23710 function. */
23711 first = parser->lexer->next_token;
23712 /* Handle function try blocks. */
23713 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23714 cp_lexer_consume_token (parser->lexer);
23715 /* We can have braced-init-list mem-initializers before the fn body. */
23716 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23718 cp_lexer_consume_token (parser->lexer);
23719 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23721 /* cache_group will stop after an un-nested { } pair, too. */
23722 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23723 break;
23725 /* variadic mem-inits have ... after the ')'. */
23726 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23727 cp_lexer_consume_token (parser->lexer);
23730 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23731 /* Handle function try blocks. */
23732 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23733 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23734 last = parser->lexer->next_token;
23736 /* Save away the inline definition; we will process it when the
23737 class is complete. */
23738 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23739 DECL_PENDING_INLINE_P (fn) = 1;
23741 /* We need to know that this was defined in the class, so that
23742 friend templates are handled correctly. */
23743 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23745 /* Add FN to the queue of functions to be parsed later. */
23746 vec_safe_push (unparsed_funs_with_definitions, fn);
23748 return fn;
23751 /* Save the tokens that make up the in-class initializer for a non-static
23752 data member. Returns a DEFAULT_ARG. */
23754 static tree
23755 cp_parser_save_nsdmi (cp_parser* parser)
23757 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23760 /* Parse a template-argument-list, as well as the trailing ">" (but
23761 not the opening "<"). See cp_parser_template_argument_list for the
23762 return value. */
23764 static tree
23765 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23767 tree arguments;
23768 tree saved_scope;
23769 tree saved_qualifying_scope;
23770 tree saved_object_scope;
23771 bool saved_greater_than_is_operator_p;
23772 int saved_unevaluated_operand;
23773 int saved_inhibit_evaluation_warnings;
23775 /* [temp.names]
23777 When parsing a template-id, the first non-nested `>' is taken as
23778 the end of the template-argument-list rather than a greater-than
23779 operator. */
23780 saved_greater_than_is_operator_p
23781 = parser->greater_than_is_operator_p;
23782 parser->greater_than_is_operator_p = false;
23783 /* Parsing the argument list may modify SCOPE, so we save it
23784 here. */
23785 saved_scope = parser->scope;
23786 saved_qualifying_scope = parser->qualifying_scope;
23787 saved_object_scope = parser->object_scope;
23788 /* We need to evaluate the template arguments, even though this
23789 template-id may be nested within a "sizeof". */
23790 saved_unevaluated_operand = cp_unevaluated_operand;
23791 cp_unevaluated_operand = 0;
23792 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23793 c_inhibit_evaluation_warnings = 0;
23794 /* Parse the template-argument-list itself. */
23795 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23796 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23797 arguments = NULL_TREE;
23798 else
23799 arguments = cp_parser_template_argument_list (parser);
23800 /* Look for the `>' that ends the template-argument-list. If we find
23801 a '>>' instead, it's probably just a typo. */
23802 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23804 if (cxx_dialect != cxx98)
23806 /* In C++0x, a `>>' in a template argument list or cast
23807 expression is considered to be two separate `>'
23808 tokens. So, change the current token to a `>', but don't
23809 consume it: it will be consumed later when the outer
23810 template argument list (or cast expression) is parsed.
23811 Note that this replacement of `>' for `>>' is necessary
23812 even if we are parsing tentatively: in the tentative
23813 case, after calling
23814 cp_parser_enclosed_template_argument_list we will always
23815 throw away all of the template arguments and the first
23816 closing `>', either because the template argument list
23817 was erroneous or because we are replacing those tokens
23818 with a CPP_TEMPLATE_ID token. The second `>' (which will
23819 not have been thrown away) is needed either to close an
23820 outer template argument list or to complete a new-style
23821 cast. */
23822 cp_token *token = cp_lexer_peek_token (parser->lexer);
23823 token->type = CPP_GREATER;
23825 else if (!saved_greater_than_is_operator_p)
23827 /* If we're in a nested template argument list, the '>>' has
23828 to be a typo for '> >'. We emit the error message, but we
23829 continue parsing and we push a '>' as next token, so that
23830 the argument list will be parsed correctly. Note that the
23831 global source location is still on the token before the
23832 '>>', so we need to say explicitly where we want it. */
23833 cp_token *token = cp_lexer_peek_token (parser->lexer);
23834 error_at (token->location, "%<>>%> should be %<> >%> "
23835 "within a nested template argument list");
23837 token->type = CPP_GREATER;
23839 else
23841 /* If this is not a nested template argument list, the '>>'
23842 is a typo for '>'. Emit an error message and continue.
23843 Same deal about the token location, but here we can get it
23844 right by consuming the '>>' before issuing the diagnostic. */
23845 cp_token *token = cp_lexer_consume_token (parser->lexer);
23846 error_at (token->location,
23847 "spurious %<>>%>, use %<>%> to terminate "
23848 "a template argument list");
23851 else
23852 cp_parser_skip_to_end_of_template_parameter_list (parser);
23853 /* The `>' token might be a greater-than operator again now. */
23854 parser->greater_than_is_operator_p
23855 = saved_greater_than_is_operator_p;
23856 /* Restore the SAVED_SCOPE. */
23857 parser->scope = saved_scope;
23858 parser->qualifying_scope = saved_qualifying_scope;
23859 parser->object_scope = saved_object_scope;
23860 cp_unevaluated_operand = saved_unevaluated_operand;
23861 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23863 return arguments;
23866 /* MEMBER_FUNCTION is a member function, or a friend. If default
23867 arguments, or the body of the function have not yet been parsed,
23868 parse them now. */
23870 static void
23871 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23873 timevar_push (TV_PARSE_INMETH);
23874 /* If this member is a template, get the underlying
23875 FUNCTION_DECL. */
23876 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23877 member_function = DECL_TEMPLATE_RESULT (member_function);
23879 /* There should not be any class definitions in progress at this
23880 point; the bodies of members are only parsed outside of all class
23881 definitions. */
23882 gcc_assert (parser->num_classes_being_defined == 0);
23883 /* While we're parsing the member functions we might encounter more
23884 classes. We want to handle them right away, but we don't want
23885 them getting mixed up with functions that are currently in the
23886 queue. */
23887 push_unparsed_function_queues (parser);
23889 /* Make sure that any template parameters are in scope. */
23890 maybe_begin_member_template_processing (member_function);
23892 /* If the body of the function has not yet been parsed, parse it
23893 now. */
23894 if (DECL_PENDING_INLINE_P (member_function))
23896 tree function_scope;
23897 cp_token_cache *tokens;
23899 /* The function is no longer pending; we are processing it. */
23900 tokens = DECL_PENDING_INLINE_INFO (member_function);
23901 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23902 DECL_PENDING_INLINE_P (member_function) = 0;
23904 /* If this is a local class, enter the scope of the containing
23905 function. */
23906 function_scope = current_function_decl;
23907 if (function_scope)
23908 push_function_context ();
23910 /* Push the body of the function onto the lexer stack. */
23911 cp_parser_push_lexer_for_tokens (parser, tokens);
23913 /* Let the front end know that we going to be defining this
23914 function. */
23915 start_preparsed_function (member_function, NULL_TREE,
23916 SF_PRE_PARSED | SF_INCLASS_INLINE);
23918 /* Don't do access checking if it is a templated function. */
23919 if (processing_template_decl)
23920 push_deferring_access_checks (dk_no_check);
23922 /* #pragma omp declare reduction needs special parsing. */
23923 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23925 parser->lexer->in_pragma = true;
23926 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23927 finish_function (/*inline*/2);
23928 cp_check_omp_declare_reduction (member_function);
23930 else
23931 /* Now, parse the body of the function. */
23932 cp_parser_function_definition_after_declarator (parser,
23933 /*inline_p=*/true);
23935 if (processing_template_decl)
23936 pop_deferring_access_checks ();
23938 /* Leave the scope of the containing function. */
23939 if (function_scope)
23940 pop_function_context ();
23941 cp_parser_pop_lexer (parser);
23944 /* Remove any template parameters from the symbol table. */
23945 maybe_end_member_template_processing ();
23947 /* Restore the queue. */
23948 pop_unparsed_function_queues (parser);
23949 timevar_pop (TV_PARSE_INMETH);
23952 /* If DECL contains any default args, remember it on the unparsed
23953 functions queue. */
23955 static void
23956 cp_parser_save_default_args (cp_parser* parser, tree decl)
23958 tree probe;
23960 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23961 probe;
23962 probe = TREE_CHAIN (probe))
23963 if (TREE_PURPOSE (probe))
23965 cp_default_arg_entry entry = {current_class_type, decl};
23966 vec_safe_push (unparsed_funs_with_default_args, entry);
23967 break;
23971 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23972 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23973 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23974 from the parameter-type-list. */
23976 static tree
23977 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23978 tree default_arg, tree parmtype)
23980 cp_token_cache *tokens;
23981 tree parsed_arg;
23982 bool dummy;
23984 if (default_arg == error_mark_node)
23985 return error_mark_node;
23987 /* Push the saved tokens for the default argument onto the parser's
23988 lexer stack. */
23989 tokens = DEFARG_TOKENS (default_arg);
23990 cp_parser_push_lexer_for_tokens (parser, tokens);
23992 start_lambda_scope (decl);
23994 /* Parse the default argument. */
23995 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23996 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23997 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23999 finish_lambda_scope ();
24001 if (parsed_arg == error_mark_node)
24002 cp_parser_skip_to_end_of_statement (parser);
24004 if (!processing_template_decl)
24006 /* In a non-template class, check conversions now. In a template,
24007 we'll wait and instantiate these as needed. */
24008 if (TREE_CODE (decl) == PARM_DECL)
24009 parsed_arg = check_default_argument (parmtype, parsed_arg,
24010 tf_warning_or_error);
24011 else
24012 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24015 /* If the token stream has not been completely used up, then
24016 there was extra junk after the end of the default
24017 argument. */
24018 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24020 if (TREE_CODE (decl) == PARM_DECL)
24021 cp_parser_error (parser, "expected %<,%>");
24022 else
24023 cp_parser_error (parser, "expected %<;%>");
24026 /* Revert to the main lexer. */
24027 cp_parser_pop_lexer (parser);
24029 return parsed_arg;
24032 /* FIELD is a non-static data member with an initializer which we saved for
24033 later; parse it now. */
24035 static void
24036 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24038 tree def;
24040 maybe_begin_member_template_processing (field);
24042 push_unparsed_function_queues (parser);
24043 def = cp_parser_late_parse_one_default_arg (parser, field,
24044 DECL_INITIAL (field),
24045 NULL_TREE);
24046 pop_unparsed_function_queues (parser);
24048 maybe_end_member_template_processing ();
24050 DECL_INITIAL (field) = def;
24053 /* FN is a FUNCTION_DECL which may contains a parameter with an
24054 unparsed DEFAULT_ARG. Parse the default args now. This function
24055 assumes that the current scope is the scope in which the default
24056 argument should be processed. */
24058 static void
24059 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24061 bool saved_local_variables_forbidden_p;
24062 tree parm, parmdecl;
24064 /* While we're parsing the default args, we might (due to the
24065 statement expression extension) encounter more classes. We want
24066 to handle them right away, but we don't want them getting mixed
24067 up with default args that are currently in the queue. */
24068 push_unparsed_function_queues (parser);
24070 /* Local variable names (and the `this' keyword) may not appear
24071 in a default argument. */
24072 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24073 parser->local_variables_forbidden_p = true;
24075 push_defarg_context (fn);
24077 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24078 parmdecl = DECL_ARGUMENTS (fn);
24079 parm && parm != void_list_node;
24080 parm = TREE_CHAIN (parm),
24081 parmdecl = DECL_CHAIN (parmdecl))
24083 tree default_arg = TREE_PURPOSE (parm);
24084 tree parsed_arg;
24085 vec<tree, va_gc> *insts;
24086 tree copy;
24087 unsigned ix;
24089 if (!default_arg)
24090 continue;
24092 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24093 /* This can happen for a friend declaration for a function
24094 already declared with default arguments. */
24095 continue;
24097 parsed_arg
24098 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24099 default_arg,
24100 TREE_VALUE (parm));
24101 if (parsed_arg == error_mark_node)
24103 continue;
24106 TREE_PURPOSE (parm) = parsed_arg;
24108 /* Update any instantiations we've already created. */
24109 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24110 vec_safe_iterate (insts, ix, &copy); ix++)
24111 TREE_PURPOSE (copy) = parsed_arg;
24114 pop_defarg_context ();
24116 /* Make sure no default arg is missing. */
24117 check_default_args (fn);
24119 /* Restore the state of local_variables_forbidden_p. */
24120 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24122 /* Restore the queue. */
24123 pop_unparsed_function_queues (parser);
24126 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24128 sizeof ... ( identifier )
24130 where the 'sizeof' token has already been consumed. */
24132 static tree
24133 cp_parser_sizeof_pack (cp_parser *parser)
24135 /* Consume the `...'. */
24136 cp_lexer_consume_token (parser->lexer);
24137 maybe_warn_variadic_templates ();
24139 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24140 if (paren)
24141 cp_lexer_consume_token (parser->lexer);
24142 else
24143 permerror (cp_lexer_peek_token (parser->lexer)->location,
24144 "%<sizeof...%> argument must be surrounded by parentheses");
24146 cp_token *token = cp_lexer_peek_token (parser->lexer);
24147 tree name = cp_parser_identifier (parser);
24148 if (name == error_mark_node)
24149 return error_mark_node;
24150 /* The name is not qualified. */
24151 parser->scope = NULL_TREE;
24152 parser->qualifying_scope = NULL_TREE;
24153 parser->object_scope = NULL_TREE;
24154 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24155 if (expr == error_mark_node)
24156 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24157 token->location);
24158 if (TREE_CODE (expr) == TYPE_DECL)
24159 expr = TREE_TYPE (expr);
24160 else if (TREE_CODE (expr) == CONST_DECL)
24161 expr = DECL_INITIAL (expr);
24162 expr = make_pack_expansion (expr);
24164 if (paren)
24165 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24167 return expr;
24170 /* Parse the operand of `sizeof' (or a similar operator). Returns
24171 either a TYPE or an expression, depending on the form of the
24172 input. The KEYWORD indicates which kind of expression we have
24173 encountered. */
24175 static tree
24176 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24178 tree expr = NULL_TREE;
24179 const char *saved_message;
24180 char *tmp;
24181 bool saved_integral_constant_expression_p;
24182 bool saved_non_integral_constant_expression_p;
24184 /* If it's a `...', then we are computing the length of a parameter
24185 pack. */
24186 if (keyword == RID_SIZEOF
24187 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24188 return cp_parser_sizeof_pack (parser);
24190 /* Types cannot be defined in a `sizeof' expression. Save away the
24191 old message. */
24192 saved_message = parser->type_definition_forbidden_message;
24193 /* And create the new one. */
24194 tmp = concat ("types may not be defined in %<",
24195 IDENTIFIER_POINTER (ridpointers[keyword]),
24196 "%> expressions", NULL);
24197 parser->type_definition_forbidden_message = tmp;
24199 /* The restrictions on constant-expressions do not apply inside
24200 sizeof expressions. */
24201 saved_integral_constant_expression_p
24202 = parser->integral_constant_expression_p;
24203 saved_non_integral_constant_expression_p
24204 = parser->non_integral_constant_expression_p;
24205 parser->integral_constant_expression_p = false;
24207 /* Do not actually evaluate the expression. */
24208 ++cp_unevaluated_operand;
24209 ++c_inhibit_evaluation_warnings;
24210 /* If it's a `(', then we might be looking at the type-id
24211 construction. */
24212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24214 tree type = NULL_TREE;
24216 /* We can't be sure yet whether we're looking at a type-id or an
24217 expression. */
24218 cp_parser_parse_tentatively (parser);
24219 /* Note: as a GNU Extension, compound literals are considered
24220 postfix-expressions as they are in C99, so they are valid
24221 arguments to sizeof. See comment in cp_parser_cast_expression
24222 for details. */
24223 if (cp_parser_compound_literal_p (parser))
24224 cp_parser_simulate_error (parser);
24225 else
24227 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24228 parser->in_type_id_in_expr_p = true;
24229 /* Look for the type-id. */
24230 type = cp_parser_type_id (parser);
24231 /* Look for the closing `)'. */
24232 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24233 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24236 /* If all went well, then we're done. */
24237 if (cp_parser_parse_definitely (parser))
24239 cp_decl_specifier_seq decl_specs;
24241 /* Build a trivial decl-specifier-seq. */
24242 clear_decl_specs (&decl_specs);
24243 decl_specs.type = type;
24245 /* Call grokdeclarator to figure out what type this is. */
24246 expr = grokdeclarator (NULL,
24247 &decl_specs,
24248 TYPENAME,
24249 /*initialized=*/0,
24250 /*attrlist=*/NULL);
24254 /* If the type-id production did not work out, then we must be
24255 looking at the unary-expression production. */
24256 if (!expr)
24257 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24258 /*cast_p=*/false, NULL);
24260 /* Go back to evaluating expressions. */
24261 --cp_unevaluated_operand;
24262 --c_inhibit_evaluation_warnings;
24264 /* Free the message we created. */
24265 free (tmp);
24266 /* And restore the old one. */
24267 parser->type_definition_forbidden_message = saved_message;
24268 parser->integral_constant_expression_p
24269 = saved_integral_constant_expression_p;
24270 parser->non_integral_constant_expression_p
24271 = saved_non_integral_constant_expression_p;
24273 return expr;
24276 /* If the current declaration has no declarator, return true. */
24278 static bool
24279 cp_parser_declares_only_class_p (cp_parser *parser)
24281 /* If the next token is a `;' or a `,' then there is no
24282 declarator. */
24283 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24284 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24287 /* Update the DECL_SPECS to reflect the storage class indicated by
24288 KEYWORD. */
24290 static void
24291 cp_parser_set_storage_class (cp_parser *parser,
24292 cp_decl_specifier_seq *decl_specs,
24293 enum rid keyword,
24294 cp_token *token)
24296 cp_storage_class storage_class;
24298 if (parser->in_unbraced_linkage_specification_p)
24300 error_at (token->location, "invalid use of %qD in linkage specification",
24301 ridpointers[keyword]);
24302 return;
24304 else if (decl_specs->storage_class != sc_none)
24306 decl_specs->conflicting_specifiers_p = true;
24307 return;
24310 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24311 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24312 && decl_specs->gnu_thread_keyword_p)
24314 pedwarn (decl_specs->locations[ds_thread], 0,
24315 "%<__thread%> before %qD", ridpointers[keyword]);
24318 switch (keyword)
24320 case RID_AUTO:
24321 storage_class = sc_auto;
24322 break;
24323 case RID_REGISTER:
24324 storage_class = sc_register;
24325 break;
24326 case RID_STATIC:
24327 storage_class = sc_static;
24328 break;
24329 case RID_EXTERN:
24330 storage_class = sc_extern;
24331 break;
24332 case RID_MUTABLE:
24333 storage_class = sc_mutable;
24334 break;
24335 default:
24336 gcc_unreachable ();
24338 decl_specs->storage_class = storage_class;
24339 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24341 /* A storage class specifier cannot be applied alongside a typedef
24342 specifier. If there is a typedef specifier present then set
24343 conflicting_specifiers_p which will trigger an error later
24344 on in grokdeclarator. */
24345 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24346 decl_specs->conflicting_specifiers_p = true;
24349 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24350 is true, the type is a class or enum definition. */
24352 static void
24353 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24354 tree type_spec,
24355 cp_token *token,
24356 bool type_definition_p)
24358 decl_specs->any_specifiers_p = true;
24360 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24361 (with, for example, in "typedef int wchar_t;") we remember that
24362 this is what happened. In system headers, we ignore these
24363 declarations so that G++ can work with system headers that are not
24364 C++-safe. */
24365 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24366 && !type_definition_p
24367 && (type_spec == boolean_type_node
24368 || type_spec == char16_type_node
24369 || type_spec == char32_type_node
24370 || type_spec == wchar_type_node)
24371 && (decl_specs->type
24372 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24373 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24374 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24375 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24377 decl_specs->redefined_builtin_type = type_spec;
24378 set_and_check_decl_spec_loc (decl_specs,
24379 ds_redefined_builtin_type_spec,
24380 token);
24381 if (!decl_specs->type)
24383 decl_specs->type = type_spec;
24384 decl_specs->type_definition_p = false;
24385 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24388 else if (decl_specs->type)
24389 decl_specs->multiple_types_p = true;
24390 else
24392 decl_specs->type = type_spec;
24393 decl_specs->type_definition_p = type_definition_p;
24394 decl_specs->redefined_builtin_type = NULL_TREE;
24395 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24399 /* True iff TOKEN is the GNU keyword __thread. */
24401 static bool
24402 token_is__thread (cp_token *token)
24404 gcc_assert (token->keyword == RID_THREAD);
24405 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24408 /* Set the location for a declarator specifier and check if it is
24409 duplicated.
24411 DECL_SPECS is the sequence of declarator specifiers onto which to
24412 set the location.
24414 DS is the single declarator specifier to set which location is to
24415 be set onto the existing sequence of declarators.
24417 LOCATION is the location for the declarator specifier to
24418 consider. */
24420 static void
24421 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24422 cp_decl_spec ds, cp_token *token)
24424 gcc_assert (ds < ds_last);
24426 if (decl_specs == NULL)
24427 return;
24429 source_location location = token->location;
24431 if (decl_specs->locations[ds] == 0)
24433 decl_specs->locations[ds] = location;
24434 if (ds == ds_thread)
24435 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24437 else
24439 if (ds == ds_long)
24441 if (decl_specs->locations[ds_long_long] != 0)
24442 error_at (location,
24443 "%<long long long%> is too long for GCC");
24444 else
24446 decl_specs->locations[ds_long_long] = location;
24447 pedwarn_cxx98 (location,
24448 OPT_Wlong_long,
24449 "ISO C++ 1998 does not support %<long long%>");
24452 else if (ds == ds_thread)
24454 bool gnu = token_is__thread (token);
24455 if (gnu != decl_specs->gnu_thread_keyword_p)
24456 error_at (location,
24457 "both %<__thread%> and %<thread_local%> specified");
24458 else
24459 error_at (location, "duplicate %qD", token->u.value);
24461 else
24463 static const char *const decl_spec_names[] = {
24464 "signed",
24465 "unsigned",
24466 "short",
24467 "long",
24468 "const",
24469 "volatile",
24470 "restrict",
24471 "inline",
24472 "virtual",
24473 "explicit",
24474 "friend",
24475 "typedef",
24476 "using",
24477 "constexpr",
24478 "__complex"
24480 error_at (location,
24481 "duplicate %qs", decl_spec_names[ds]);
24486 /* Return true iff the declarator specifier DS is present in the
24487 sequence of declarator specifiers DECL_SPECS. */
24489 bool
24490 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24491 cp_decl_spec ds)
24493 gcc_assert (ds < ds_last);
24495 if (decl_specs == NULL)
24496 return false;
24498 return decl_specs->locations[ds] != 0;
24501 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24502 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24504 static bool
24505 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24507 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24510 /* Issue an error message indicating that TOKEN_DESC was expected.
24511 If KEYWORD is true, it indicated this function is called by
24512 cp_parser_require_keword and the required token can only be
24513 a indicated keyword. */
24515 static void
24516 cp_parser_required_error (cp_parser *parser,
24517 required_token token_desc,
24518 bool keyword)
24520 switch (token_desc)
24522 case RT_NEW:
24523 cp_parser_error (parser, "expected %<new%>");
24524 return;
24525 case RT_DELETE:
24526 cp_parser_error (parser, "expected %<delete%>");
24527 return;
24528 case RT_RETURN:
24529 cp_parser_error (parser, "expected %<return%>");
24530 return;
24531 case RT_WHILE:
24532 cp_parser_error (parser, "expected %<while%>");
24533 return;
24534 case RT_EXTERN:
24535 cp_parser_error (parser, "expected %<extern%>");
24536 return;
24537 case RT_STATIC_ASSERT:
24538 cp_parser_error (parser, "expected %<static_assert%>");
24539 return;
24540 case RT_DECLTYPE:
24541 cp_parser_error (parser, "expected %<decltype%>");
24542 return;
24543 case RT_OPERATOR:
24544 cp_parser_error (parser, "expected %<operator%>");
24545 return;
24546 case RT_CLASS:
24547 cp_parser_error (parser, "expected %<class%>");
24548 return;
24549 case RT_TEMPLATE:
24550 cp_parser_error (parser, "expected %<template%>");
24551 return;
24552 case RT_NAMESPACE:
24553 cp_parser_error (parser, "expected %<namespace%>");
24554 return;
24555 case RT_USING:
24556 cp_parser_error (parser, "expected %<using%>");
24557 return;
24558 case RT_ASM:
24559 cp_parser_error (parser, "expected %<asm%>");
24560 return;
24561 case RT_TRY:
24562 cp_parser_error (parser, "expected %<try%>");
24563 return;
24564 case RT_CATCH:
24565 cp_parser_error (parser, "expected %<catch%>");
24566 return;
24567 case RT_THROW:
24568 cp_parser_error (parser, "expected %<throw%>");
24569 return;
24570 case RT_LABEL:
24571 cp_parser_error (parser, "expected %<__label__%>");
24572 return;
24573 case RT_AT_TRY:
24574 cp_parser_error (parser, "expected %<@try%>");
24575 return;
24576 case RT_AT_SYNCHRONIZED:
24577 cp_parser_error (parser, "expected %<@synchronized%>");
24578 return;
24579 case RT_AT_THROW:
24580 cp_parser_error (parser, "expected %<@throw%>");
24581 return;
24582 case RT_TRANSACTION_ATOMIC:
24583 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24584 return;
24585 case RT_TRANSACTION_RELAXED:
24586 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24587 return;
24588 default:
24589 break;
24591 if (!keyword)
24593 switch (token_desc)
24595 case RT_SEMICOLON:
24596 cp_parser_error (parser, "expected %<;%>");
24597 return;
24598 case RT_OPEN_PAREN:
24599 cp_parser_error (parser, "expected %<(%>");
24600 return;
24601 case RT_CLOSE_BRACE:
24602 cp_parser_error (parser, "expected %<}%>");
24603 return;
24604 case RT_OPEN_BRACE:
24605 cp_parser_error (parser, "expected %<{%>");
24606 return;
24607 case RT_CLOSE_SQUARE:
24608 cp_parser_error (parser, "expected %<]%>");
24609 return;
24610 case RT_OPEN_SQUARE:
24611 cp_parser_error (parser, "expected %<[%>");
24612 return;
24613 case RT_COMMA:
24614 cp_parser_error (parser, "expected %<,%>");
24615 return;
24616 case RT_SCOPE:
24617 cp_parser_error (parser, "expected %<::%>");
24618 return;
24619 case RT_LESS:
24620 cp_parser_error (parser, "expected %<<%>");
24621 return;
24622 case RT_GREATER:
24623 cp_parser_error (parser, "expected %<>%>");
24624 return;
24625 case RT_EQ:
24626 cp_parser_error (parser, "expected %<=%>");
24627 return;
24628 case RT_ELLIPSIS:
24629 cp_parser_error (parser, "expected %<...%>");
24630 return;
24631 case RT_MULT:
24632 cp_parser_error (parser, "expected %<*%>");
24633 return;
24634 case RT_COMPL:
24635 cp_parser_error (parser, "expected %<~%>");
24636 return;
24637 case RT_COLON:
24638 cp_parser_error (parser, "expected %<:%>");
24639 return;
24640 case RT_COLON_SCOPE:
24641 cp_parser_error (parser, "expected %<:%> or %<::%>");
24642 return;
24643 case RT_CLOSE_PAREN:
24644 cp_parser_error (parser, "expected %<)%>");
24645 return;
24646 case RT_COMMA_CLOSE_PAREN:
24647 cp_parser_error (parser, "expected %<,%> or %<)%>");
24648 return;
24649 case RT_PRAGMA_EOL:
24650 cp_parser_error (parser, "expected end of line");
24651 return;
24652 case RT_NAME:
24653 cp_parser_error (parser, "expected identifier");
24654 return;
24655 case RT_SELECT:
24656 cp_parser_error (parser, "expected selection-statement");
24657 return;
24658 case RT_INTERATION:
24659 cp_parser_error (parser, "expected iteration-statement");
24660 return;
24661 case RT_JUMP:
24662 cp_parser_error (parser, "expected jump-statement");
24663 return;
24664 case RT_CLASS_KEY:
24665 cp_parser_error (parser, "expected class-key");
24666 return;
24667 case RT_CLASS_TYPENAME_TEMPLATE:
24668 cp_parser_error (parser,
24669 "expected %<class%>, %<typename%>, or %<template%>");
24670 return;
24671 default:
24672 gcc_unreachable ();
24675 else
24676 gcc_unreachable ();
24681 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24682 issue an error message indicating that TOKEN_DESC was expected.
24684 Returns the token consumed, if the token had the appropriate type.
24685 Otherwise, returns NULL. */
24687 static cp_token *
24688 cp_parser_require (cp_parser* parser,
24689 enum cpp_ttype type,
24690 required_token token_desc)
24692 if (cp_lexer_next_token_is (parser->lexer, type))
24693 return cp_lexer_consume_token (parser->lexer);
24694 else
24696 /* Output the MESSAGE -- unless we're parsing tentatively. */
24697 if (!cp_parser_simulate_error (parser))
24698 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24699 return NULL;
24703 /* An error message is produced if the next token is not '>'.
24704 All further tokens are skipped until the desired token is
24705 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24707 static void
24708 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24710 /* Current level of '< ... >'. */
24711 unsigned level = 0;
24712 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24713 unsigned nesting_depth = 0;
24715 /* Are we ready, yet? If not, issue error message. */
24716 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24717 return;
24719 /* Skip tokens until the desired token is found. */
24720 while (true)
24722 /* Peek at the next token. */
24723 switch (cp_lexer_peek_token (parser->lexer)->type)
24725 case CPP_LESS:
24726 if (!nesting_depth)
24727 ++level;
24728 break;
24730 case CPP_RSHIFT:
24731 if (cxx_dialect == cxx98)
24732 /* C++0x views the `>>' operator as two `>' tokens, but
24733 C++98 does not. */
24734 break;
24735 else if (!nesting_depth && level-- == 0)
24737 /* We've hit a `>>' where the first `>' closes the
24738 template argument list, and the second `>' is
24739 spurious. Just consume the `>>' and stop; we've
24740 already produced at least one error. */
24741 cp_lexer_consume_token (parser->lexer);
24742 return;
24744 /* Fall through for C++0x, so we handle the second `>' in
24745 the `>>'. */
24747 case CPP_GREATER:
24748 if (!nesting_depth && level-- == 0)
24750 /* We've reached the token we want, consume it and stop. */
24751 cp_lexer_consume_token (parser->lexer);
24752 return;
24754 break;
24756 case CPP_OPEN_PAREN:
24757 case CPP_OPEN_SQUARE:
24758 ++nesting_depth;
24759 break;
24761 case CPP_CLOSE_PAREN:
24762 case CPP_CLOSE_SQUARE:
24763 if (nesting_depth-- == 0)
24764 return;
24765 break;
24767 case CPP_EOF:
24768 case CPP_PRAGMA_EOL:
24769 case CPP_SEMICOLON:
24770 case CPP_OPEN_BRACE:
24771 case CPP_CLOSE_BRACE:
24772 /* The '>' was probably forgotten, don't look further. */
24773 return;
24775 default:
24776 break;
24779 /* Consume this token. */
24780 cp_lexer_consume_token (parser->lexer);
24784 /* If the next token is the indicated keyword, consume it. Otherwise,
24785 issue an error message indicating that TOKEN_DESC was expected.
24787 Returns the token consumed, if the token had the appropriate type.
24788 Otherwise, returns NULL. */
24790 static cp_token *
24791 cp_parser_require_keyword (cp_parser* parser,
24792 enum rid keyword,
24793 required_token token_desc)
24795 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24797 if (token && token->keyword != keyword)
24799 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24800 return NULL;
24803 return token;
24806 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24807 function-definition. */
24809 static bool
24810 cp_parser_token_starts_function_definition_p (cp_token* token)
24812 return (/* An ordinary function-body begins with an `{'. */
24813 token->type == CPP_OPEN_BRACE
24814 /* A ctor-initializer begins with a `:'. */
24815 || token->type == CPP_COLON
24816 /* A function-try-block begins with `try'. */
24817 || token->keyword == RID_TRY
24818 /* A function-transaction-block begins with `__transaction_atomic'
24819 or `__transaction_relaxed'. */
24820 || token->keyword == RID_TRANSACTION_ATOMIC
24821 || token->keyword == RID_TRANSACTION_RELAXED
24822 /* The named return value extension begins with `return'. */
24823 || token->keyword == RID_RETURN);
24826 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24827 definition. */
24829 static bool
24830 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24832 cp_token *token;
24834 token = cp_lexer_peek_token (parser->lexer);
24835 return (token->type == CPP_OPEN_BRACE
24836 || (token->type == CPP_COLON
24837 && !parser->colon_doesnt_start_class_def_p));
24840 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24841 C++0x) ending a template-argument. */
24843 static bool
24844 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24846 cp_token *token;
24848 token = cp_lexer_peek_token (parser->lexer);
24849 return (token->type == CPP_COMMA
24850 || token->type == CPP_GREATER
24851 || token->type == CPP_ELLIPSIS
24852 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24855 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24856 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24858 static bool
24859 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24860 size_t n)
24862 cp_token *token;
24864 token = cp_lexer_peek_nth_token (parser->lexer, n);
24865 if (token->type == CPP_LESS)
24866 return true;
24867 /* Check for the sequence `<::' in the original code. It would be lexed as
24868 `[:', where `[' is a digraph, and there is no whitespace before
24869 `:'. */
24870 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24872 cp_token *token2;
24873 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24874 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24875 return true;
24877 return false;
24880 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24881 or none_type otherwise. */
24883 static enum tag_types
24884 cp_parser_token_is_class_key (cp_token* token)
24886 switch (token->keyword)
24888 case RID_CLASS:
24889 return class_type;
24890 case RID_STRUCT:
24891 return record_type;
24892 case RID_UNION:
24893 return union_type;
24895 default:
24896 return none_type;
24900 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
24901 or none_type otherwise or if the token is null. */
24903 static enum tag_types
24904 cp_parser_token_is_type_parameter_key (cp_token* token)
24906 if (!token)
24907 return none_type;
24909 switch (token->keyword)
24911 case RID_CLASS:
24912 return class_type;
24913 case RID_TYPENAME:
24914 return typename_type;
24916 default:
24917 return none_type;
24921 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24923 static void
24924 cp_parser_check_class_key (enum tag_types class_key, tree type)
24926 if (type == error_mark_node)
24927 return;
24928 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24930 if (permerror (input_location, "%qs tag used in naming %q#T",
24931 class_key == union_type ? "union"
24932 : class_key == record_type ? "struct" : "class",
24933 type))
24934 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24935 "%q#T was previously declared here", type);
24939 /* Issue an error message if DECL is redeclared with different
24940 access than its original declaration [class.access.spec/3].
24941 This applies to nested classes and nested class templates.
24942 [class.mem/1]. */
24944 static void
24945 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24947 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24948 return;
24950 if ((TREE_PRIVATE (decl)
24951 != (current_access_specifier == access_private_node))
24952 || (TREE_PROTECTED (decl)
24953 != (current_access_specifier == access_protected_node)))
24954 error_at (location, "%qD redeclared with different access", decl);
24957 /* Look for the `template' keyword, as a syntactic disambiguator.
24958 Return TRUE iff it is present, in which case it will be
24959 consumed. */
24961 static bool
24962 cp_parser_optional_template_keyword (cp_parser *parser)
24964 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24966 /* In C++98 the `template' keyword can only be used within templates;
24967 outside templates the parser can always figure out what is a
24968 template and what is not. In C++11, per the resolution of DR 468,
24969 `template' is allowed in cases where it is not strictly necessary. */
24970 if (!processing_template_decl
24971 && pedantic && cxx_dialect == cxx98)
24973 cp_token *token = cp_lexer_peek_token (parser->lexer);
24974 pedwarn (token->location, OPT_Wpedantic,
24975 "in C++98 %<template%> (as a disambiguator) is only "
24976 "allowed within templates");
24977 /* If this part of the token stream is rescanned, the same
24978 error message would be generated. So, we purge the token
24979 from the stream. */
24980 cp_lexer_purge_token (parser->lexer);
24981 return false;
24983 else
24985 /* Consume the `template' keyword. */
24986 cp_lexer_consume_token (parser->lexer);
24987 return true;
24990 return false;
24993 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24994 set PARSER->SCOPE, and perform other related actions. */
24996 static void
24997 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24999 int i;
25000 struct tree_check *check_value;
25001 deferred_access_check *chk;
25002 vec<deferred_access_check, va_gc> *checks;
25004 /* Get the stored value. */
25005 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25006 /* Perform any access checks that were deferred. */
25007 checks = check_value->checks;
25008 if (checks)
25010 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25011 perform_or_defer_access_check (chk->binfo,
25012 chk->decl,
25013 chk->diag_decl, tf_warning_or_error);
25015 /* Set the scope from the stored value. */
25016 parser->scope = check_value->value;
25017 parser->qualifying_scope = check_value->qualifying_scope;
25018 parser->object_scope = NULL_TREE;
25021 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25022 encounter the end of a block before what we were looking for. */
25024 static bool
25025 cp_parser_cache_group (cp_parser *parser,
25026 enum cpp_ttype end,
25027 unsigned depth)
25029 while (true)
25031 cp_token *token = cp_lexer_peek_token (parser->lexer);
25033 /* Abort a parenthesized expression if we encounter a semicolon. */
25034 if ((end == CPP_CLOSE_PAREN || depth == 0)
25035 && token->type == CPP_SEMICOLON)
25036 return true;
25037 /* If we've reached the end of the file, stop. */
25038 if (token->type == CPP_EOF
25039 || (end != CPP_PRAGMA_EOL
25040 && token->type == CPP_PRAGMA_EOL))
25041 return true;
25042 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25043 /* We've hit the end of an enclosing block, so there's been some
25044 kind of syntax error. */
25045 return true;
25047 /* Consume the token. */
25048 cp_lexer_consume_token (parser->lexer);
25049 /* See if it starts a new group. */
25050 if (token->type == CPP_OPEN_BRACE)
25052 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25053 /* In theory this should probably check end == '}', but
25054 cp_parser_save_member_function_body needs it to exit
25055 after either '}' or ')' when called with ')'. */
25056 if (depth == 0)
25057 return false;
25059 else if (token->type == CPP_OPEN_PAREN)
25061 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25062 if (depth == 0 && end == CPP_CLOSE_PAREN)
25063 return false;
25065 else if (token->type == CPP_PRAGMA)
25066 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25067 else if (token->type == end)
25068 return false;
25072 /* Like above, for caching a default argument or NSDMI. Both of these are
25073 terminated by a non-nested comma, but it can be unclear whether or not a
25074 comma is nested in a template argument list unless we do more parsing.
25075 In order to handle this ambiguity, when we encounter a ',' after a '<'
25076 we try to parse what follows as a parameter-declaration-list (in the
25077 case of a default argument) or a member-declarator (in the case of an
25078 NSDMI). If that succeeds, then we stop caching. */
25080 static tree
25081 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25083 unsigned depth = 0;
25084 int maybe_template_id = 0;
25085 cp_token *first_token;
25086 cp_token *token;
25087 tree default_argument;
25089 /* Add tokens until we have processed the entire default
25090 argument. We add the range [first_token, token). */
25091 first_token = cp_lexer_peek_token (parser->lexer);
25092 if (first_token->type == CPP_OPEN_BRACE)
25094 /* For list-initialization, this is straightforward. */
25095 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25096 token = cp_lexer_peek_token (parser->lexer);
25098 else while (true)
25100 bool done = false;
25102 /* Peek at the next token. */
25103 token = cp_lexer_peek_token (parser->lexer);
25104 /* What we do depends on what token we have. */
25105 switch (token->type)
25107 /* In valid code, a default argument must be
25108 immediately followed by a `,' `)', or `...'. */
25109 case CPP_COMMA:
25110 if (depth == 0 && maybe_template_id)
25112 /* If we've seen a '<', we might be in a
25113 template-argument-list. Until Core issue 325 is
25114 resolved, we don't know how this situation ought
25115 to be handled, so try to DTRT. We check whether
25116 what comes after the comma is a valid parameter
25117 declaration list. If it is, then the comma ends
25118 the default argument; otherwise the default
25119 argument continues. */
25120 bool error = false;
25122 /* Set ITALP so cp_parser_parameter_declaration_list
25123 doesn't decide to commit to this parse. */
25124 bool saved_italp = parser->in_template_argument_list_p;
25125 parser->in_template_argument_list_p = true;
25127 cp_parser_parse_tentatively (parser);
25128 cp_lexer_consume_token (parser->lexer);
25130 if (nsdmi)
25132 int ctor_dtor_or_conv_p;
25133 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25134 &ctor_dtor_or_conv_p,
25135 /*parenthesized_p=*/NULL,
25136 /*member_p=*/true,
25137 /*friend_p=*/false);
25139 else
25141 begin_scope (sk_function_parms, NULL_TREE);
25142 cp_parser_parameter_declaration_list (parser, &error);
25143 pop_bindings_and_leave_scope ();
25145 if (!cp_parser_error_occurred (parser) && !error)
25146 done = true;
25147 cp_parser_abort_tentative_parse (parser);
25149 parser->in_template_argument_list_p = saved_italp;
25150 break;
25152 case CPP_CLOSE_PAREN:
25153 case CPP_ELLIPSIS:
25154 /* If we run into a non-nested `;', `}', or `]',
25155 then the code is invalid -- but the default
25156 argument is certainly over. */
25157 case CPP_SEMICOLON:
25158 case CPP_CLOSE_BRACE:
25159 case CPP_CLOSE_SQUARE:
25160 if (depth == 0
25161 /* Handle correctly int n = sizeof ... ( p ); */
25162 && token->type != CPP_ELLIPSIS)
25163 done = true;
25164 /* Update DEPTH, if necessary. */
25165 else if (token->type == CPP_CLOSE_PAREN
25166 || token->type == CPP_CLOSE_BRACE
25167 || token->type == CPP_CLOSE_SQUARE)
25168 --depth;
25169 break;
25171 case CPP_OPEN_PAREN:
25172 case CPP_OPEN_SQUARE:
25173 case CPP_OPEN_BRACE:
25174 ++depth;
25175 break;
25177 case CPP_LESS:
25178 if (depth == 0)
25179 /* This might be the comparison operator, or it might
25180 start a template argument list. */
25181 ++maybe_template_id;
25182 break;
25184 case CPP_RSHIFT:
25185 if (cxx_dialect == cxx98)
25186 break;
25187 /* Fall through for C++0x, which treats the `>>'
25188 operator like two `>' tokens in certain
25189 cases. */
25191 case CPP_GREATER:
25192 if (depth == 0)
25194 /* This might be an operator, or it might close a
25195 template argument list. But if a previous '<'
25196 started a template argument list, this will have
25197 closed it, so we can't be in one anymore. */
25198 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25199 if (maybe_template_id < 0)
25200 maybe_template_id = 0;
25202 break;
25204 /* If we run out of tokens, issue an error message. */
25205 case CPP_EOF:
25206 case CPP_PRAGMA_EOL:
25207 error_at (token->location, "file ends in default argument");
25208 done = true;
25209 break;
25211 case CPP_NAME:
25212 case CPP_SCOPE:
25213 /* In these cases, we should look for template-ids.
25214 For example, if the default argument is
25215 `X<int, double>()', we need to do name lookup to
25216 figure out whether or not `X' is a template; if
25217 so, the `,' does not end the default argument.
25219 That is not yet done. */
25220 break;
25222 default:
25223 break;
25226 /* If we've reached the end, stop. */
25227 if (done)
25228 break;
25230 /* Add the token to the token block. */
25231 token = cp_lexer_consume_token (parser->lexer);
25234 /* Create a DEFAULT_ARG to represent the unparsed default
25235 argument. */
25236 default_argument = make_node (DEFAULT_ARG);
25237 DEFARG_TOKENS (default_argument)
25238 = cp_token_cache_new (first_token, token);
25239 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25241 return default_argument;
25244 /* Begin parsing tentatively. We always save tokens while parsing
25245 tentatively so that if the tentative parsing fails we can restore the
25246 tokens. */
25248 static void
25249 cp_parser_parse_tentatively (cp_parser* parser)
25251 /* Enter a new parsing context. */
25252 parser->context = cp_parser_context_new (parser->context);
25253 /* Begin saving tokens. */
25254 cp_lexer_save_tokens (parser->lexer);
25255 /* In order to avoid repetitive access control error messages,
25256 access checks are queued up until we are no longer parsing
25257 tentatively. */
25258 push_deferring_access_checks (dk_deferred);
25261 /* Commit to the currently active tentative parse. */
25263 static void
25264 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25266 cp_parser_context *context;
25267 cp_lexer *lexer;
25269 /* Mark all of the levels as committed. */
25270 lexer = parser->lexer;
25271 for (context = parser->context; context->next; context = context->next)
25273 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25274 break;
25275 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25276 while (!cp_lexer_saving_tokens (lexer))
25277 lexer = lexer->next;
25278 cp_lexer_commit_tokens (lexer);
25282 /* Commit to the topmost currently active tentative parse.
25284 Note that this function shouldn't be called when there are
25285 irreversible side-effects while in a tentative state. For
25286 example, we shouldn't create a permanent entry in the symbol
25287 table, or issue an error message that might not apply if the
25288 tentative parse is aborted. */
25290 static void
25291 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25293 cp_parser_context *context = parser->context;
25294 cp_lexer *lexer = parser->lexer;
25296 if (context)
25298 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25299 return;
25300 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25302 while (!cp_lexer_saving_tokens (lexer))
25303 lexer = lexer->next;
25304 cp_lexer_commit_tokens (lexer);
25308 /* Abort the currently active tentative parse. All consumed tokens
25309 will be rolled back, and no diagnostics will be issued. */
25311 static void
25312 cp_parser_abort_tentative_parse (cp_parser* parser)
25314 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25315 || errorcount > 0);
25316 cp_parser_simulate_error (parser);
25317 /* Now, pretend that we want to see if the construct was
25318 successfully parsed. */
25319 cp_parser_parse_definitely (parser);
25322 /* Stop parsing tentatively. If a parse error has occurred, restore the
25323 token stream. Otherwise, commit to the tokens we have consumed.
25324 Returns true if no error occurred; false otherwise. */
25326 static bool
25327 cp_parser_parse_definitely (cp_parser* parser)
25329 bool error_occurred;
25330 cp_parser_context *context;
25332 /* Remember whether or not an error occurred, since we are about to
25333 destroy that information. */
25334 error_occurred = cp_parser_error_occurred (parser);
25335 /* Remove the topmost context from the stack. */
25336 context = parser->context;
25337 parser->context = context->next;
25338 /* If no parse errors occurred, commit to the tentative parse. */
25339 if (!error_occurred)
25341 /* Commit to the tokens read tentatively, unless that was
25342 already done. */
25343 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25344 cp_lexer_commit_tokens (parser->lexer);
25346 pop_to_parent_deferring_access_checks ();
25348 /* Otherwise, if errors occurred, roll back our state so that things
25349 are just as they were before we began the tentative parse. */
25350 else
25352 cp_lexer_rollback_tokens (parser->lexer);
25353 pop_deferring_access_checks ();
25355 /* Add the context to the front of the free list. */
25356 context->next = cp_parser_context_free_list;
25357 cp_parser_context_free_list = context;
25359 return !error_occurred;
25362 /* Returns true if we are parsing tentatively and are not committed to
25363 this tentative parse. */
25365 static bool
25366 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25368 return (cp_parser_parsing_tentatively (parser)
25369 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25372 /* Returns nonzero iff an error has occurred during the most recent
25373 tentative parse. */
25375 static bool
25376 cp_parser_error_occurred (cp_parser* parser)
25378 return (cp_parser_parsing_tentatively (parser)
25379 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25382 /* Returns nonzero if GNU extensions are allowed. */
25384 static bool
25385 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25387 return parser->allow_gnu_extensions_p;
25390 /* Objective-C++ Productions */
25393 /* Parse an Objective-C expression, which feeds into a primary-expression
25394 above.
25396 objc-expression:
25397 objc-message-expression
25398 objc-string-literal
25399 objc-encode-expression
25400 objc-protocol-expression
25401 objc-selector-expression
25403 Returns a tree representation of the expression. */
25405 static tree
25406 cp_parser_objc_expression (cp_parser* parser)
25408 /* Try to figure out what kind of declaration is present. */
25409 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25411 switch (kwd->type)
25413 case CPP_OPEN_SQUARE:
25414 return cp_parser_objc_message_expression (parser);
25416 case CPP_OBJC_STRING:
25417 kwd = cp_lexer_consume_token (parser->lexer);
25418 return objc_build_string_object (kwd->u.value);
25420 case CPP_KEYWORD:
25421 switch (kwd->keyword)
25423 case RID_AT_ENCODE:
25424 return cp_parser_objc_encode_expression (parser);
25426 case RID_AT_PROTOCOL:
25427 return cp_parser_objc_protocol_expression (parser);
25429 case RID_AT_SELECTOR:
25430 return cp_parser_objc_selector_expression (parser);
25432 default:
25433 break;
25435 default:
25436 error_at (kwd->location,
25437 "misplaced %<@%D%> Objective-C++ construct",
25438 kwd->u.value);
25439 cp_parser_skip_to_end_of_block_or_statement (parser);
25442 return error_mark_node;
25445 /* Parse an Objective-C message expression.
25447 objc-message-expression:
25448 [ objc-message-receiver objc-message-args ]
25450 Returns a representation of an Objective-C message. */
25452 static tree
25453 cp_parser_objc_message_expression (cp_parser* parser)
25455 tree receiver, messageargs;
25457 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25458 receiver = cp_parser_objc_message_receiver (parser);
25459 messageargs = cp_parser_objc_message_args (parser);
25460 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25462 return objc_build_message_expr (receiver, messageargs);
25465 /* Parse an objc-message-receiver.
25467 objc-message-receiver:
25468 expression
25469 simple-type-specifier
25471 Returns a representation of the type or expression. */
25473 static tree
25474 cp_parser_objc_message_receiver (cp_parser* parser)
25476 tree rcv;
25478 /* An Objective-C message receiver may be either (1) a type
25479 or (2) an expression. */
25480 cp_parser_parse_tentatively (parser);
25481 rcv = cp_parser_expression (parser);
25483 if (cp_parser_parse_definitely (parser))
25484 return rcv;
25486 rcv = cp_parser_simple_type_specifier (parser,
25487 /*decl_specs=*/NULL,
25488 CP_PARSER_FLAGS_NONE);
25490 return objc_get_class_reference (rcv);
25493 /* Parse the arguments and selectors comprising an Objective-C message.
25495 objc-message-args:
25496 objc-selector
25497 objc-selector-args
25498 objc-selector-args , objc-comma-args
25500 objc-selector-args:
25501 objc-selector [opt] : assignment-expression
25502 objc-selector-args objc-selector [opt] : assignment-expression
25504 objc-comma-args:
25505 assignment-expression
25506 objc-comma-args , assignment-expression
25508 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25509 selector arguments and TREE_VALUE containing a list of comma
25510 arguments. */
25512 static tree
25513 cp_parser_objc_message_args (cp_parser* parser)
25515 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25516 bool maybe_unary_selector_p = true;
25517 cp_token *token = cp_lexer_peek_token (parser->lexer);
25519 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25521 tree selector = NULL_TREE, arg;
25523 if (token->type != CPP_COLON)
25524 selector = cp_parser_objc_selector (parser);
25526 /* Detect if we have a unary selector. */
25527 if (maybe_unary_selector_p
25528 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25529 return build_tree_list (selector, NULL_TREE);
25531 maybe_unary_selector_p = false;
25532 cp_parser_require (parser, CPP_COLON, RT_COLON);
25533 arg = cp_parser_assignment_expression (parser, false, NULL);
25535 sel_args
25536 = chainon (sel_args,
25537 build_tree_list (selector, arg));
25539 token = cp_lexer_peek_token (parser->lexer);
25542 /* Handle non-selector arguments, if any. */
25543 while (token->type == CPP_COMMA)
25545 tree arg;
25547 cp_lexer_consume_token (parser->lexer);
25548 arg = cp_parser_assignment_expression (parser, false, NULL);
25550 addl_args
25551 = chainon (addl_args,
25552 build_tree_list (NULL_TREE, arg));
25554 token = cp_lexer_peek_token (parser->lexer);
25557 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25559 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25560 return build_tree_list (error_mark_node, error_mark_node);
25563 return build_tree_list (sel_args, addl_args);
25566 /* Parse an Objective-C encode expression.
25568 objc-encode-expression:
25569 @encode objc-typename
25571 Returns an encoded representation of the type argument. */
25573 static tree
25574 cp_parser_objc_encode_expression (cp_parser* parser)
25576 tree type;
25577 cp_token *token;
25579 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25580 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25581 token = cp_lexer_peek_token (parser->lexer);
25582 type = complete_type (cp_parser_type_id (parser));
25583 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25585 if (!type)
25587 error_at (token->location,
25588 "%<@encode%> must specify a type as an argument");
25589 return error_mark_node;
25592 /* This happens if we find @encode(T) (where T is a template
25593 typename or something dependent on a template typename) when
25594 parsing a template. In that case, we can't compile it
25595 immediately, but we rather create an AT_ENCODE_EXPR which will
25596 need to be instantiated when the template is used.
25598 if (dependent_type_p (type))
25600 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25601 TREE_READONLY (value) = 1;
25602 return value;
25605 return objc_build_encode_expr (type);
25608 /* Parse an Objective-C @defs expression. */
25610 static tree
25611 cp_parser_objc_defs_expression (cp_parser *parser)
25613 tree name;
25615 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25616 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25617 name = cp_parser_identifier (parser);
25618 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25620 return objc_get_class_ivars (name);
25623 /* Parse an Objective-C protocol expression.
25625 objc-protocol-expression:
25626 @protocol ( identifier )
25628 Returns a representation of the protocol expression. */
25630 static tree
25631 cp_parser_objc_protocol_expression (cp_parser* parser)
25633 tree proto;
25635 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25636 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25637 proto = cp_parser_identifier (parser);
25638 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25640 return objc_build_protocol_expr (proto);
25643 /* Parse an Objective-C selector expression.
25645 objc-selector-expression:
25646 @selector ( objc-method-signature )
25648 objc-method-signature:
25649 objc-selector
25650 objc-selector-seq
25652 objc-selector-seq:
25653 objc-selector :
25654 objc-selector-seq objc-selector :
25656 Returns a representation of the method selector. */
25658 static tree
25659 cp_parser_objc_selector_expression (cp_parser* parser)
25661 tree sel_seq = NULL_TREE;
25662 bool maybe_unary_selector_p = true;
25663 cp_token *token;
25664 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25666 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25667 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25668 token = cp_lexer_peek_token (parser->lexer);
25670 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25671 || token->type == CPP_SCOPE)
25673 tree selector = NULL_TREE;
25675 if (token->type != CPP_COLON
25676 || token->type == CPP_SCOPE)
25677 selector = cp_parser_objc_selector (parser);
25679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25680 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25682 /* Detect if we have a unary selector. */
25683 if (maybe_unary_selector_p)
25685 sel_seq = selector;
25686 goto finish_selector;
25688 else
25690 cp_parser_error (parser, "expected %<:%>");
25693 maybe_unary_selector_p = false;
25694 token = cp_lexer_consume_token (parser->lexer);
25696 if (token->type == CPP_SCOPE)
25698 sel_seq
25699 = chainon (sel_seq,
25700 build_tree_list (selector, NULL_TREE));
25701 sel_seq
25702 = chainon (sel_seq,
25703 build_tree_list (NULL_TREE, NULL_TREE));
25705 else
25706 sel_seq
25707 = chainon (sel_seq,
25708 build_tree_list (selector, NULL_TREE));
25710 token = cp_lexer_peek_token (parser->lexer);
25713 finish_selector:
25714 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25716 return objc_build_selector_expr (loc, sel_seq);
25719 /* Parse a list of identifiers.
25721 objc-identifier-list:
25722 identifier
25723 objc-identifier-list , identifier
25725 Returns a TREE_LIST of identifier nodes. */
25727 static tree
25728 cp_parser_objc_identifier_list (cp_parser* parser)
25730 tree identifier;
25731 tree list;
25732 cp_token *sep;
25734 identifier = cp_parser_identifier (parser);
25735 if (identifier == error_mark_node)
25736 return error_mark_node;
25738 list = build_tree_list (NULL_TREE, identifier);
25739 sep = cp_lexer_peek_token (parser->lexer);
25741 while (sep->type == CPP_COMMA)
25743 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25744 identifier = cp_parser_identifier (parser);
25745 if (identifier == error_mark_node)
25746 return list;
25748 list = chainon (list, build_tree_list (NULL_TREE,
25749 identifier));
25750 sep = cp_lexer_peek_token (parser->lexer);
25753 return list;
25756 /* Parse an Objective-C alias declaration.
25758 objc-alias-declaration:
25759 @compatibility_alias identifier identifier ;
25761 This function registers the alias mapping with the Objective-C front end.
25762 It returns nothing. */
25764 static void
25765 cp_parser_objc_alias_declaration (cp_parser* parser)
25767 tree alias, orig;
25769 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25770 alias = cp_parser_identifier (parser);
25771 orig = cp_parser_identifier (parser);
25772 objc_declare_alias (alias, orig);
25773 cp_parser_consume_semicolon_at_end_of_statement (parser);
25776 /* Parse an Objective-C class forward-declaration.
25778 objc-class-declaration:
25779 @class objc-identifier-list ;
25781 The function registers the forward declarations with the Objective-C
25782 front end. It returns nothing. */
25784 static void
25785 cp_parser_objc_class_declaration (cp_parser* parser)
25787 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25788 while (true)
25790 tree id;
25792 id = cp_parser_identifier (parser);
25793 if (id == error_mark_node)
25794 break;
25796 objc_declare_class (id);
25798 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25799 cp_lexer_consume_token (parser->lexer);
25800 else
25801 break;
25803 cp_parser_consume_semicolon_at_end_of_statement (parser);
25806 /* Parse a list of Objective-C protocol references.
25808 objc-protocol-refs-opt:
25809 objc-protocol-refs [opt]
25811 objc-protocol-refs:
25812 < objc-identifier-list >
25814 Returns a TREE_LIST of identifiers, if any. */
25816 static tree
25817 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25819 tree protorefs = NULL_TREE;
25821 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25823 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25824 protorefs = cp_parser_objc_identifier_list (parser);
25825 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25828 return protorefs;
25831 /* Parse a Objective-C visibility specification. */
25833 static void
25834 cp_parser_objc_visibility_spec (cp_parser* parser)
25836 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25838 switch (vis->keyword)
25840 case RID_AT_PRIVATE:
25841 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25842 break;
25843 case RID_AT_PROTECTED:
25844 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25845 break;
25846 case RID_AT_PUBLIC:
25847 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25848 break;
25849 case RID_AT_PACKAGE:
25850 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25851 break;
25852 default:
25853 return;
25856 /* Eat '@private'/'@protected'/'@public'. */
25857 cp_lexer_consume_token (parser->lexer);
25860 /* Parse an Objective-C method type. Return 'true' if it is a class
25861 (+) method, and 'false' if it is an instance (-) method. */
25863 static inline bool
25864 cp_parser_objc_method_type (cp_parser* parser)
25866 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25867 return true;
25868 else
25869 return false;
25872 /* Parse an Objective-C protocol qualifier. */
25874 static tree
25875 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25877 tree quals = NULL_TREE, node;
25878 cp_token *token = cp_lexer_peek_token (parser->lexer);
25880 node = token->u.value;
25882 while (node && identifier_p (node)
25883 && (node == ridpointers [(int) RID_IN]
25884 || node == ridpointers [(int) RID_OUT]
25885 || node == ridpointers [(int) RID_INOUT]
25886 || node == ridpointers [(int) RID_BYCOPY]
25887 || node == ridpointers [(int) RID_BYREF]
25888 || node == ridpointers [(int) RID_ONEWAY]))
25890 quals = tree_cons (NULL_TREE, node, quals);
25891 cp_lexer_consume_token (parser->lexer);
25892 token = cp_lexer_peek_token (parser->lexer);
25893 node = token->u.value;
25896 return quals;
25899 /* Parse an Objective-C typename. */
25901 static tree
25902 cp_parser_objc_typename (cp_parser* parser)
25904 tree type_name = NULL_TREE;
25906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25908 tree proto_quals, cp_type = NULL_TREE;
25910 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25911 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25913 /* An ObjC type name may consist of just protocol qualifiers, in which
25914 case the type shall default to 'id'. */
25915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25917 cp_type = cp_parser_type_id (parser);
25919 /* If the type could not be parsed, an error has already
25920 been produced. For error recovery, behave as if it had
25921 not been specified, which will use the default type
25922 'id'. */
25923 if (cp_type == error_mark_node)
25925 cp_type = NULL_TREE;
25926 /* We need to skip to the closing parenthesis as
25927 cp_parser_type_id() does not seem to do it for
25928 us. */
25929 cp_parser_skip_to_closing_parenthesis (parser,
25930 /*recovering=*/true,
25931 /*or_comma=*/false,
25932 /*consume_paren=*/false);
25936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25937 type_name = build_tree_list (proto_quals, cp_type);
25940 return type_name;
25943 /* Check to see if TYPE refers to an Objective-C selector name. */
25945 static bool
25946 cp_parser_objc_selector_p (enum cpp_ttype type)
25948 return (type == CPP_NAME || type == CPP_KEYWORD
25949 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25950 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25951 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25952 || type == CPP_XOR || type == CPP_XOR_EQ);
25955 /* Parse an Objective-C selector. */
25957 static tree
25958 cp_parser_objc_selector (cp_parser* parser)
25960 cp_token *token = cp_lexer_consume_token (parser->lexer);
25962 if (!cp_parser_objc_selector_p (token->type))
25964 error_at (token->location, "invalid Objective-C++ selector name");
25965 return error_mark_node;
25968 /* C++ operator names are allowed to appear in ObjC selectors. */
25969 switch (token->type)
25971 case CPP_AND_AND: return get_identifier ("and");
25972 case CPP_AND_EQ: return get_identifier ("and_eq");
25973 case CPP_AND: return get_identifier ("bitand");
25974 case CPP_OR: return get_identifier ("bitor");
25975 case CPP_COMPL: return get_identifier ("compl");
25976 case CPP_NOT: return get_identifier ("not");
25977 case CPP_NOT_EQ: return get_identifier ("not_eq");
25978 case CPP_OR_OR: return get_identifier ("or");
25979 case CPP_OR_EQ: return get_identifier ("or_eq");
25980 case CPP_XOR: return get_identifier ("xor");
25981 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25982 default: return token->u.value;
25986 /* Parse an Objective-C params list. */
25988 static tree
25989 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25991 tree params = NULL_TREE;
25992 bool maybe_unary_selector_p = true;
25993 cp_token *token = cp_lexer_peek_token (parser->lexer);
25995 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25997 tree selector = NULL_TREE, type_name, identifier;
25998 tree parm_attr = NULL_TREE;
26000 if (token->keyword == RID_ATTRIBUTE)
26001 break;
26003 if (token->type != CPP_COLON)
26004 selector = cp_parser_objc_selector (parser);
26006 /* Detect if we have a unary selector. */
26007 if (maybe_unary_selector_p
26008 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26010 params = selector; /* Might be followed by attributes. */
26011 break;
26014 maybe_unary_selector_p = false;
26015 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26017 /* Something went quite wrong. There should be a colon
26018 here, but there is not. Stop parsing parameters. */
26019 break;
26021 type_name = cp_parser_objc_typename (parser);
26022 /* New ObjC allows attributes on parameters too. */
26023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26024 parm_attr = cp_parser_attributes_opt (parser);
26025 identifier = cp_parser_identifier (parser);
26027 params
26028 = chainon (params,
26029 objc_build_keyword_decl (selector,
26030 type_name,
26031 identifier,
26032 parm_attr));
26034 token = cp_lexer_peek_token (parser->lexer);
26037 if (params == NULL_TREE)
26039 cp_parser_error (parser, "objective-c++ method declaration is expected");
26040 return error_mark_node;
26043 /* We allow tail attributes for the method. */
26044 if (token->keyword == RID_ATTRIBUTE)
26046 *attributes = cp_parser_attributes_opt (parser);
26047 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26048 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26049 return params;
26050 cp_parser_error (parser,
26051 "method attributes must be specified at the end");
26052 return error_mark_node;
26055 if (params == NULL_TREE)
26057 cp_parser_error (parser, "objective-c++ method declaration is expected");
26058 return error_mark_node;
26060 return params;
26063 /* Parse the non-keyword Objective-C params. */
26065 static tree
26066 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26067 tree* attributes)
26069 tree params = make_node (TREE_LIST);
26070 cp_token *token = cp_lexer_peek_token (parser->lexer);
26071 *ellipsisp = false; /* Initially, assume no ellipsis. */
26073 while (token->type == CPP_COMMA)
26075 cp_parameter_declarator *parmdecl;
26076 tree parm;
26078 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26079 token = cp_lexer_peek_token (parser->lexer);
26081 if (token->type == CPP_ELLIPSIS)
26083 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26084 *ellipsisp = true;
26085 token = cp_lexer_peek_token (parser->lexer);
26086 break;
26089 /* TODO: parse attributes for tail parameters. */
26090 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26091 parm = grokdeclarator (parmdecl->declarator,
26092 &parmdecl->decl_specifiers,
26093 PARM, /*initialized=*/0,
26094 /*attrlist=*/NULL);
26096 chainon (params, build_tree_list (NULL_TREE, parm));
26097 token = cp_lexer_peek_token (parser->lexer);
26100 /* We allow tail attributes for the method. */
26101 if (token->keyword == RID_ATTRIBUTE)
26103 if (*attributes == NULL_TREE)
26105 *attributes = cp_parser_attributes_opt (parser);
26106 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26107 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26108 return params;
26110 else
26111 /* We have an error, but parse the attributes, so that we can
26112 carry on. */
26113 *attributes = cp_parser_attributes_opt (parser);
26115 cp_parser_error (parser,
26116 "method attributes must be specified at the end");
26117 return error_mark_node;
26120 return params;
26123 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26125 static void
26126 cp_parser_objc_interstitial_code (cp_parser* parser)
26128 cp_token *token = cp_lexer_peek_token (parser->lexer);
26130 /* If the next token is `extern' and the following token is a string
26131 literal, then we have a linkage specification. */
26132 if (token->keyword == RID_EXTERN
26133 && cp_parser_is_pure_string_literal
26134 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26135 cp_parser_linkage_specification (parser);
26136 /* Handle #pragma, if any. */
26137 else if (token->type == CPP_PRAGMA)
26138 cp_parser_pragma (parser, pragma_objc_icode);
26139 /* Allow stray semicolons. */
26140 else if (token->type == CPP_SEMICOLON)
26141 cp_lexer_consume_token (parser->lexer);
26142 /* Mark methods as optional or required, when building protocols. */
26143 else if (token->keyword == RID_AT_OPTIONAL)
26145 cp_lexer_consume_token (parser->lexer);
26146 objc_set_method_opt (true);
26148 else if (token->keyword == RID_AT_REQUIRED)
26150 cp_lexer_consume_token (parser->lexer);
26151 objc_set_method_opt (false);
26153 else if (token->keyword == RID_NAMESPACE)
26154 cp_parser_namespace_definition (parser);
26155 /* Other stray characters must generate errors. */
26156 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26158 cp_lexer_consume_token (parser->lexer);
26159 error ("stray %qs between Objective-C++ methods",
26160 token->type == CPP_OPEN_BRACE ? "{" : "}");
26162 /* Finally, try to parse a block-declaration, or a function-definition. */
26163 else
26164 cp_parser_block_declaration (parser, /*statement_p=*/false);
26167 /* Parse a method signature. */
26169 static tree
26170 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26172 tree rettype, kwdparms, optparms;
26173 bool ellipsis = false;
26174 bool is_class_method;
26176 is_class_method = cp_parser_objc_method_type (parser);
26177 rettype = cp_parser_objc_typename (parser);
26178 *attributes = NULL_TREE;
26179 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26180 if (kwdparms == error_mark_node)
26181 return error_mark_node;
26182 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26183 if (optparms == error_mark_node)
26184 return error_mark_node;
26186 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26189 static bool
26190 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26192 tree tattr;
26193 cp_lexer_save_tokens (parser->lexer);
26194 tattr = cp_parser_attributes_opt (parser);
26195 gcc_assert (tattr) ;
26197 /* If the attributes are followed by a method introducer, this is not allowed.
26198 Dump the attributes and flag the situation. */
26199 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26200 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26201 return true;
26203 /* Otherwise, the attributes introduce some interstitial code, possibly so
26204 rewind to allow that check. */
26205 cp_lexer_rollback_tokens (parser->lexer);
26206 return false;
26209 /* Parse an Objective-C method prototype list. */
26211 static void
26212 cp_parser_objc_method_prototype_list (cp_parser* parser)
26214 cp_token *token = cp_lexer_peek_token (parser->lexer);
26216 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26218 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26220 tree attributes, sig;
26221 bool is_class_method;
26222 if (token->type == CPP_PLUS)
26223 is_class_method = true;
26224 else
26225 is_class_method = false;
26226 sig = cp_parser_objc_method_signature (parser, &attributes);
26227 if (sig == error_mark_node)
26229 cp_parser_skip_to_end_of_block_or_statement (parser);
26230 token = cp_lexer_peek_token (parser->lexer);
26231 continue;
26233 objc_add_method_declaration (is_class_method, sig, attributes);
26234 cp_parser_consume_semicolon_at_end_of_statement (parser);
26236 else if (token->keyword == RID_AT_PROPERTY)
26237 cp_parser_objc_at_property_declaration (parser);
26238 else if (token->keyword == RID_ATTRIBUTE
26239 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26240 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26241 OPT_Wattributes,
26242 "prefix attributes are ignored for methods");
26243 else
26244 /* Allow for interspersed non-ObjC++ code. */
26245 cp_parser_objc_interstitial_code (parser);
26247 token = cp_lexer_peek_token (parser->lexer);
26250 if (token->type != CPP_EOF)
26251 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26252 else
26253 cp_parser_error (parser, "expected %<@end%>");
26255 objc_finish_interface ();
26258 /* Parse an Objective-C method definition list. */
26260 static void
26261 cp_parser_objc_method_definition_list (cp_parser* parser)
26263 cp_token *token = cp_lexer_peek_token (parser->lexer);
26265 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26267 tree meth;
26269 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26271 cp_token *ptk;
26272 tree sig, attribute;
26273 bool is_class_method;
26274 if (token->type == CPP_PLUS)
26275 is_class_method = true;
26276 else
26277 is_class_method = false;
26278 push_deferring_access_checks (dk_deferred);
26279 sig = cp_parser_objc_method_signature (parser, &attribute);
26280 if (sig == error_mark_node)
26282 cp_parser_skip_to_end_of_block_or_statement (parser);
26283 token = cp_lexer_peek_token (parser->lexer);
26284 continue;
26286 objc_start_method_definition (is_class_method, sig, attribute,
26287 NULL_TREE);
26289 /* For historical reasons, we accept an optional semicolon. */
26290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26291 cp_lexer_consume_token (parser->lexer);
26293 ptk = cp_lexer_peek_token (parser->lexer);
26294 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26295 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26297 perform_deferred_access_checks (tf_warning_or_error);
26298 stop_deferring_access_checks ();
26299 meth = cp_parser_function_definition_after_declarator (parser,
26300 false);
26301 pop_deferring_access_checks ();
26302 objc_finish_method_definition (meth);
26305 /* The following case will be removed once @synthesize is
26306 completely implemented. */
26307 else if (token->keyword == RID_AT_PROPERTY)
26308 cp_parser_objc_at_property_declaration (parser);
26309 else if (token->keyword == RID_AT_SYNTHESIZE)
26310 cp_parser_objc_at_synthesize_declaration (parser);
26311 else if (token->keyword == RID_AT_DYNAMIC)
26312 cp_parser_objc_at_dynamic_declaration (parser);
26313 else if (token->keyword == RID_ATTRIBUTE
26314 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26315 warning_at (token->location, OPT_Wattributes,
26316 "prefix attributes are ignored for methods");
26317 else
26318 /* Allow for interspersed non-ObjC++ code. */
26319 cp_parser_objc_interstitial_code (parser);
26321 token = cp_lexer_peek_token (parser->lexer);
26324 if (token->type != CPP_EOF)
26325 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26326 else
26327 cp_parser_error (parser, "expected %<@end%>");
26329 objc_finish_implementation ();
26332 /* Parse Objective-C ivars. */
26334 static void
26335 cp_parser_objc_class_ivars (cp_parser* parser)
26337 cp_token *token = cp_lexer_peek_token (parser->lexer);
26339 if (token->type != CPP_OPEN_BRACE)
26340 return; /* No ivars specified. */
26342 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26343 token = cp_lexer_peek_token (parser->lexer);
26345 while (token->type != CPP_CLOSE_BRACE
26346 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26348 cp_decl_specifier_seq declspecs;
26349 int decl_class_or_enum_p;
26350 tree prefix_attributes;
26352 cp_parser_objc_visibility_spec (parser);
26354 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26355 break;
26357 cp_parser_decl_specifier_seq (parser,
26358 CP_PARSER_FLAGS_OPTIONAL,
26359 &declspecs,
26360 &decl_class_or_enum_p);
26362 /* auto, register, static, extern, mutable. */
26363 if (declspecs.storage_class != sc_none)
26365 cp_parser_error (parser, "invalid type for instance variable");
26366 declspecs.storage_class = sc_none;
26369 /* thread_local. */
26370 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26372 cp_parser_error (parser, "invalid type for instance variable");
26373 declspecs.locations[ds_thread] = 0;
26376 /* typedef. */
26377 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26379 cp_parser_error (parser, "invalid type for instance variable");
26380 declspecs.locations[ds_typedef] = 0;
26383 prefix_attributes = declspecs.attributes;
26384 declspecs.attributes = NULL_TREE;
26386 /* Keep going until we hit the `;' at the end of the
26387 declaration. */
26388 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26390 tree width = NULL_TREE, attributes, first_attribute, decl;
26391 cp_declarator *declarator = NULL;
26392 int ctor_dtor_or_conv_p;
26394 /* Check for a (possibly unnamed) bitfield declaration. */
26395 token = cp_lexer_peek_token (parser->lexer);
26396 if (token->type == CPP_COLON)
26397 goto eat_colon;
26399 if (token->type == CPP_NAME
26400 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26401 == CPP_COLON))
26403 /* Get the name of the bitfield. */
26404 declarator = make_id_declarator (NULL_TREE,
26405 cp_parser_identifier (parser),
26406 sfk_none);
26408 eat_colon:
26409 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26410 /* Get the width of the bitfield. */
26411 width
26412 = cp_parser_constant_expression (parser,
26413 /*allow_non_constant=*/false,
26414 NULL);
26416 else
26418 /* Parse the declarator. */
26419 declarator
26420 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26421 &ctor_dtor_or_conv_p,
26422 /*parenthesized_p=*/NULL,
26423 /*member_p=*/false,
26424 /*friend_p=*/false);
26427 /* Look for attributes that apply to the ivar. */
26428 attributes = cp_parser_attributes_opt (parser);
26429 /* Remember which attributes are prefix attributes and
26430 which are not. */
26431 first_attribute = attributes;
26432 /* Combine the attributes. */
26433 attributes = chainon (prefix_attributes, attributes);
26435 if (width)
26436 /* Create the bitfield declaration. */
26437 decl = grokbitfield (declarator, &declspecs,
26438 width,
26439 attributes);
26440 else
26441 decl = grokfield (declarator, &declspecs,
26442 NULL_TREE, /*init_const_expr_p=*/false,
26443 NULL_TREE, attributes);
26445 /* Add the instance variable. */
26446 if (decl != error_mark_node && decl != NULL_TREE)
26447 objc_add_instance_variable (decl);
26449 /* Reset PREFIX_ATTRIBUTES. */
26450 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26451 attributes = TREE_CHAIN (attributes);
26452 if (attributes)
26453 TREE_CHAIN (attributes) = NULL_TREE;
26455 token = cp_lexer_peek_token (parser->lexer);
26457 if (token->type == CPP_COMMA)
26459 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26460 continue;
26462 break;
26465 cp_parser_consume_semicolon_at_end_of_statement (parser);
26466 token = cp_lexer_peek_token (parser->lexer);
26469 if (token->keyword == RID_AT_END)
26470 cp_parser_error (parser, "expected %<}%>");
26472 /* Do not consume the RID_AT_END, so it will be read again as terminating
26473 the @interface of @implementation. */
26474 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26475 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26477 /* For historical reasons, we accept an optional semicolon. */
26478 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26479 cp_lexer_consume_token (parser->lexer);
26482 /* Parse an Objective-C protocol declaration. */
26484 static void
26485 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26487 tree proto, protorefs;
26488 cp_token *tok;
26490 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26493 tok = cp_lexer_peek_token (parser->lexer);
26494 error_at (tok->location, "identifier expected after %<@protocol%>");
26495 cp_parser_consume_semicolon_at_end_of_statement (parser);
26496 return;
26499 /* See if we have a forward declaration or a definition. */
26500 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26502 /* Try a forward declaration first. */
26503 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26505 while (true)
26507 tree id;
26509 id = cp_parser_identifier (parser);
26510 if (id == error_mark_node)
26511 break;
26513 objc_declare_protocol (id, attributes);
26515 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26516 cp_lexer_consume_token (parser->lexer);
26517 else
26518 break;
26520 cp_parser_consume_semicolon_at_end_of_statement (parser);
26523 /* Ok, we got a full-fledged definition (or at least should). */
26524 else
26526 proto = cp_parser_identifier (parser);
26527 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26528 objc_start_protocol (proto, protorefs, attributes);
26529 cp_parser_objc_method_prototype_list (parser);
26533 /* Parse an Objective-C superclass or category. */
26535 static void
26536 cp_parser_objc_superclass_or_category (cp_parser *parser,
26537 bool iface_p,
26538 tree *super,
26539 tree *categ, bool *is_class_extension)
26541 cp_token *next = cp_lexer_peek_token (parser->lexer);
26543 *super = *categ = NULL_TREE;
26544 *is_class_extension = false;
26545 if (next->type == CPP_COLON)
26547 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26548 *super = cp_parser_identifier (parser);
26550 else if (next->type == CPP_OPEN_PAREN)
26552 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26554 /* If there is no category name, and this is an @interface, we
26555 have a class extension. */
26556 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26558 *categ = NULL_TREE;
26559 *is_class_extension = true;
26561 else
26562 *categ = cp_parser_identifier (parser);
26564 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26568 /* Parse an Objective-C class interface. */
26570 static void
26571 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26573 tree name, super, categ, protos;
26574 bool is_class_extension;
26576 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26577 name = cp_parser_identifier (parser);
26578 if (name == error_mark_node)
26580 /* It's hard to recover because even if valid @interface stuff
26581 is to follow, we can't compile it (or validate it) if we
26582 don't even know which class it refers to. Let's assume this
26583 was a stray '@interface' token in the stream and skip it.
26585 return;
26587 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26588 &is_class_extension);
26589 protos = cp_parser_objc_protocol_refs_opt (parser);
26591 /* We have either a class or a category on our hands. */
26592 if (categ || is_class_extension)
26593 objc_start_category_interface (name, categ, protos, attributes);
26594 else
26596 objc_start_class_interface (name, super, protos, attributes);
26597 /* Handle instance variable declarations, if any. */
26598 cp_parser_objc_class_ivars (parser);
26599 objc_continue_interface ();
26602 cp_parser_objc_method_prototype_list (parser);
26605 /* Parse an Objective-C class implementation. */
26607 static void
26608 cp_parser_objc_class_implementation (cp_parser* parser)
26610 tree name, super, categ;
26611 bool is_class_extension;
26613 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26614 name = cp_parser_identifier (parser);
26615 if (name == error_mark_node)
26617 /* It's hard to recover because even if valid @implementation
26618 stuff is to follow, we can't compile it (or validate it) if
26619 we don't even know which class it refers to. Let's assume
26620 this was a stray '@implementation' token in the stream and
26621 skip it.
26623 return;
26625 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26626 &is_class_extension);
26628 /* We have either a class or a category on our hands. */
26629 if (categ)
26630 objc_start_category_implementation (name, categ);
26631 else
26633 objc_start_class_implementation (name, super);
26634 /* Handle instance variable declarations, if any. */
26635 cp_parser_objc_class_ivars (parser);
26636 objc_continue_implementation ();
26639 cp_parser_objc_method_definition_list (parser);
26642 /* Consume the @end token and finish off the implementation. */
26644 static void
26645 cp_parser_objc_end_implementation (cp_parser* parser)
26647 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26648 objc_finish_implementation ();
26651 /* Parse an Objective-C declaration. */
26653 static void
26654 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26656 /* Try to figure out what kind of declaration is present. */
26657 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26659 if (attributes)
26660 switch (kwd->keyword)
26662 case RID_AT_ALIAS:
26663 case RID_AT_CLASS:
26664 case RID_AT_END:
26665 error_at (kwd->location, "attributes may not be specified before"
26666 " the %<@%D%> Objective-C++ keyword",
26667 kwd->u.value);
26668 attributes = NULL;
26669 break;
26670 case RID_AT_IMPLEMENTATION:
26671 warning_at (kwd->location, OPT_Wattributes,
26672 "prefix attributes are ignored before %<@%D%>",
26673 kwd->u.value);
26674 attributes = NULL;
26675 default:
26676 break;
26679 switch (kwd->keyword)
26681 case RID_AT_ALIAS:
26682 cp_parser_objc_alias_declaration (parser);
26683 break;
26684 case RID_AT_CLASS:
26685 cp_parser_objc_class_declaration (parser);
26686 break;
26687 case RID_AT_PROTOCOL:
26688 cp_parser_objc_protocol_declaration (parser, attributes);
26689 break;
26690 case RID_AT_INTERFACE:
26691 cp_parser_objc_class_interface (parser, attributes);
26692 break;
26693 case RID_AT_IMPLEMENTATION:
26694 cp_parser_objc_class_implementation (parser);
26695 break;
26696 case RID_AT_END:
26697 cp_parser_objc_end_implementation (parser);
26698 break;
26699 default:
26700 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26701 kwd->u.value);
26702 cp_parser_skip_to_end_of_block_or_statement (parser);
26706 /* Parse an Objective-C try-catch-finally statement.
26708 objc-try-catch-finally-stmt:
26709 @try compound-statement objc-catch-clause-seq [opt]
26710 objc-finally-clause [opt]
26712 objc-catch-clause-seq:
26713 objc-catch-clause objc-catch-clause-seq [opt]
26715 objc-catch-clause:
26716 @catch ( objc-exception-declaration ) compound-statement
26718 objc-finally-clause:
26719 @finally compound-statement
26721 objc-exception-declaration:
26722 parameter-declaration
26723 '...'
26725 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26727 Returns NULL_TREE.
26729 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26730 for C. Keep them in sync. */
26732 static tree
26733 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26735 location_t location;
26736 tree stmt;
26738 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26739 location = cp_lexer_peek_token (parser->lexer)->location;
26740 objc_maybe_warn_exceptions (location);
26741 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26742 node, lest it get absorbed into the surrounding block. */
26743 stmt = push_stmt_list ();
26744 cp_parser_compound_statement (parser, NULL, false, false);
26745 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26747 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26749 cp_parameter_declarator *parm;
26750 tree parameter_declaration = error_mark_node;
26751 bool seen_open_paren = false;
26753 cp_lexer_consume_token (parser->lexer);
26754 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26755 seen_open_paren = true;
26756 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26758 /* We have "@catch (...)" (where the '...' are literally
26759 what is in the code). Skip the '...'.
26760 parameter_declaration is set to NULL_TREE, and
26761 objc_being_catch_clauses() knows that that means
26762 '...'. */
26763 cp_lexer_consume_token (parser->lexer);
26764 parameter_declaration = NULL_TREE;
26766 else
26768 /* We have "@catch (NSException *exception)" or something
26769 like that. Parse the parameter declaration. */
26770 parm = cp_parser_parameter_declaration (parser, false, NULL);
26771 if (parm == NULL)
26772 parameter_declaration = error_mark_node;
26773 else
26774 parameter_declaration = grokdeclarator (parm->declarator,
26775 &parm->decl_specifiers,
26776 PARM, /*initialized=*/0,
26777 /*attrlist=*/NULL);
26779 if (seen_open_paren)
26780 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26781 else
26783 /* If there was no open parenthesis, we are recovering from
26784 an error, and we are trying to figure out what mistake
26785 the user has made. */
26787 /* If there is an immediate closing parenthesis, the user
26788 probably forgot the opening one (ie, they typed "@catch
26789 NSException *e)". Parse the closing parenthesis and keep
26790 going. */
26791 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26792 cp_lexer_consume_token (parser->lexer);
26794 /* If these is no immediate closing parenthesis, the user
26795 probably doesn't know that parenthesis are required at
26796 all (ie, they typed "@catch NSException *e"). So, just
26797 forget about the closing parenthesis and keep going. */
26799 objc_begin_catch_clause (parameter_declaration);
26800 cp_parser_compound_statement (parser, NULL, false, false);
26801 objc_finish_catch_clause ();
26803 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26805 cp_lexer_consume_token (parser->lexer);
26806 location = cp_lexer_peek_token (parser->lexer)->location;
26807 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26808 node, lest it get absorbed into the surrounding block. */
26809 stmt = push_stmt_list ();
26810 cp_parser_compound_statement (parser, NULL, false, false);
26811 objc_build_finally_clause (location, pop_stmt_list (stmt));
26814 return objc_finish_try_stmt ();
26817 /* Parse an Objective-C synchronized statement.
26819 objc-synchronized-stmt:
26820 @synchronized ( expression ) compound-statement
26822 Returns NULL_TREE. */
26824 static tree
26825 cp_parser_objc_synchronized_statement (cp_parser *parser)
26827 location_t location;
26828 tree lock, stmt;
26830 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26832 location = cp_lexer_peek_token (parser->lexer)->location;
26833 objc_maybe_warn_exceptions (location);
26834 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26835 lock = cp_parser_expression (parser);
26836 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26838 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26839 node, lest it get absorbed into the surrounding block. */
26840 stmt = push_stmt_list ();
26841 cp_parser_compound_statement (parser, NULL, false, false);
26843 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26846 /* Parse an Objective-C throw statement.
26848 objc-throw-stmt:
26849 @throw assignment-expression [opt] ;
26851 Returns a constructed '@throw' statement. */
26853 static tree
26854 cp_parser_objc_throw_statement (cp_parser *parser)
26856 tree expr = NULL_TREE;
26857 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26859 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26862 expr = cp_parser_expression (parser);
26864 cp_parser_consume_semicolon_at_end_of_statement (parser);
26866 return objc_build_throw_stmt (loc, expr);
26869 /* Parse an Objective-C statement. */
26871 static tree
26872 cp_parser_objc_statement (cp_parser * parser)
26874 /* Try to figure out what kind of declaration is present. */
26875 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26877 switch (kwd->keyword)
26879 case RID_AT_TRY:
26880 return cp_parser_objc_try_catch_finally_statement (parser);
26881 case RID_AT_SYNCHRONIZED:
26882 return cp_parser_objc_synchronized_statement (parser);
26883 case RID_AT_THROW:
26884 return cp_parser_objc_throw_statement (parser);
26885 default:
26886 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26887 kwd->u.value);
26888 cp_parser_skip_to_end_of_block_or_statement (parser);
26891 return error_mark_node;
26894 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26895 look ahead to see if an objc keyword follows the attributes. This
26896 is to detect the use of prefix attributes on ObjC @interface and
26897 @protocol. */
26899 static bool
26900 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26902 cp_lexer_save_tokens (parser->lexer);
26903 *attrib = cp_parser_attributes_opt (parser);
26904 gcc_assert (*attrib);
26905 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26907 cp_lexer_commit_tokens (parser->lexer);
26908 return true;
26910 cp_lexer_rollback_tokens (parser->lexer);
26911 return false;
26914 /* This routine is a minimal replacement for
26915 c_parser_struct_declaration () used when parsing the list of
26916 types/names or ObjC++ properties. For example, when parsing the
26917 code
26919 @property (readonly) int a, b, c;
26921 this function is responsible for parsing "int a, int b, int c" and
26922 returning the declarations as CHAIN of DECLs.
26924 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26925 similar parsing. */
26926 static tree
26927 cp_parser_objc_struct_declaration (cp_parser *parser)
26929 tree decls = NULL_TREE;
26930 cp_decl_specifier_seq declspecs;
26931 int decl_class_or_enum_p;
26932 tree prefix_attributes;
26934 cp_parser_decl_specifier_seq (parser,
26935 CP_PARSER_FLAGS_NONE,
26936 &declspecs,
26937 &decl_class_or_enum_p);
26939 if (declspecs.type == error_mark_node)
26940 return error_mark_node;
26942 /* auto, register, static, extern, mutable. */
26943 if (declspecs.storage_class != sc_none)
26945 cp_parser_error (parser, "invalid type for property");
26946 declspecs.storage_class = sc_none;
26949 /* thread_local. */
26950 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26952 cp_parser_error (parser, "invalid type for property");
26953 declspecs.locations[ds_thread] = 0;
26956 /* typedef. */
26957 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26959 cp_parser_error (parser, "invalid type for property");
26960 declspecs.locations[ds_typedef] = 0;
26963 prefix_attributes = declspecs.attributes;
26964 declspecs.attributes = NULL_TREE;
26966 /* Keep going until we hit the `;' at the end of the declaration. */
26967 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26969 tree attributes, first_attribute, decl;
26970 cp_declarator *declarator;
26971 cp_token *token;
26973 /* Parse the declarator. */
26974 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26975 NULL, NULL, false, false);
26977 /* Look for attributes that apply to the ivar. */
26978 attributes = cp_parser_attributes_opt (parser);
26979 /* Remember which attributes are prefix attributes and
26980 which are not. */
26981 first_attribute = attributes;
26982 /* Combine the attributes. */
26983 attributes = chainon (prefix_attributes, attributes);
26985 decl = grokfield (declarator, &declspecs,
26986 NULL_TREE, /*init_const_expr_p=*/false,
26987 NULL_TREE, attributes);
26989 if (decl == error_mark_node || decl == NULL_TREE)
26990 return error_mark_node;
26992 /* Reset PREFIX_ATTRIBUTES. */
26993 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26994 attributes = TREE_CHAIN (attributes);
26995 if (attributes)
26996 TREE_CHAIN (attributes) = NULL_TREE;
26998 DECL_CHAIN (decl) = decls;
26999 decls = decl;
27001 token = cp_lexer_peek_token (parser->lexer);
27002 if (token->type == CPP_COMMA)
27004 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27005 continue;
27007 else
27008 break;
27010 return decls;
27013 /* Parse an Objective-C @property declaration. The syntax is:
27015 objc-property-declaration:
27016 '@property' objc-property-attributes[opt] struct-declaration ;
27018 objc-property-attributes:
27019 '(' objc-property-attribute-list ')'
27021 objc-property-attribute-list:
27022 objc-property-attribute
27023 objc-property-attribute-list, objc-property-attribute
27025 objc-property-attribute
27026 'getter' = identifier
27027 'setter' = identifier
27028 'readonly'
27029 'readwrite'
27030 'assign'
27031 'retain'
27032 'copy'
27033 'nonatomic'
27035 For example:
27036 @property NSString *name;
27037 @property (readonly) id object;
27038 @property (retain, nonatomic, getter=getTheName) id name;
27039 @property int a, b, c;
27041 PS: This function is identical to
27042 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27043 static void
27044 cp_parser_objc_at_property_declaration (cp_parser *parser)
27046 /* The following variables hold the attributes of the properties as
27047 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27048 seen. When we see an attribute, we set them to 'true' (if they
27049 are boolean properties) or to the identifier (if they have an
27050 argument, ie, for getter and setter). Note that here we only
27051 parse the list of attributes, check the syntax and accumulate the
27052 attributes that we find. objc_add_property_declaration() will
27053 then process the information. */
27054 bool property_assign = false;
27055 bool property_copy = false;
27056 tree property_getter_ident = NULL_TREE;
27057 bool property_nonatomic = false;
27058 bool property_readonly = false;
27059 bool property_readwrite = false;
27060 bool property_retain = false;
27061 tree property_setter_ident = NULL_TREE;
27063 /* 'properties' is the list of properties that we read. Usually a
27064 single one, but maybe more (eg, in "@property int a, b, c;" there
27065 are three). */
27066 tree properties;
27067 location_t loc;
27069 loc = cp_lexer_peek_token (parser->lexer)->location;
27071 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27073 /* Parse the optional attribute list... */
27074 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27076 /* Eat the '('. */
27077 cp_lexer_consume_token (parser->lexer);
27079 while (true)
27081 bool syntax_error = false;
27082 cp_token *token = cp_lexer_peek_token (parser->lexer);
27083 enum rid keyword;
27085 if (token->type != CPP_NAME)
27087 cp_parser_error (parser, "expected identifier");
27088 break;
27090 keyword = C_RID_CODE (token->u.value);
27091 cp_lexer_consume_token (parser->lexer);
27092 switch (keyword)
27094 case RID_ASSIGN: property_assign = true; break;
27095 case RID_COPY: property_copy = true; break;
27096 case RID_NONATOMIC: property_nonatomic = true; break;
27097 case RID_READONLY: property_readonly = true; break;
27098 case RID_READWRITE: property_readwrite = true; break;
27099 case RID_RETAIN: property_retain = true; break;
27101 case RID_GETTER:
27102 case RID_SETTER:
27103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27105 if (keyword == RID_GETTER)
27106 cp_parser_error (parser,
27107 "missing %<=%> (after %<getter%> attribute)");
27108 else
27109 cp_parser_error (parser,
27110 "missing %<=%> (after %<setter%> attribute)");
27111 syntax_error = true;
27112 break;
27114 cp_lexer_consume_token (parser->lexer); /* eat the = */
27115 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27117 cp_parser_error (parser, "expected identifier");
27118 syntax_error = true;
27119 break;
27121 if (keyword == RID_SETTER)
27123 if (property_setter_ident != NULL_TREE)
27125 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27126 cp_lexer_consume_token (parser->lexer);
27128 else
27129 property_setter_ident = cp_parser_objc_selector (parser);
27130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27131 cp_parser_error (parser, "setter name must terminate with %<:%>");
27132 else
27133 cp_lexer_consume_token (parser->lexer);
27135 else
27137 if (property_getter_ident != NULL_TREE)
27139 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27140 cp_lexer_consume_token (parser->lexer);
27142 else
27143 property_getter_ident = cp_parser_objc_selector (parser);
27145 break;
27146 default:
27147 cp_parser_error (parser, "unknown property attribute");
27148 syntax_error = true;
27149 break;
27152 if (syntax_error)
27153 break;
27155 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27156 cp_lexer_consume_token (parser->lexer);
27157 else
27158 break;
27161 /* FIXME: "@property (setter, assign);" will generate a spurious
27162 "error: expected ‘)’ before ‘,’ token". This is because
27163 cp_parser_require, unlike the C counterpart, will produce an
27164 error even if we are in error recovery. */
27165 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27167 cp_parser_skip_to_closing_parenthesis (parser,
27168 /*recovering=*/true,
27169 /*or_comma=*/false,
27170 /*consume_paren=*/true);
27174 /* ... and the property declaration(s). */
27175 properties = cp_parser_objc_struct_declaration (parser);
27177 if (properties == error_mark_node)
27179 cp_parser_skip_to_end_of_statement (parser);
27180 /* If the next token is now a `;', consume it. */
27181 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27182 cp_lexer_consume_token (parser->lexer);
27183 return;
27186 if (properties == NULL_TREE)
27187 cp_parser_error (parser, "expected identifier");
27188 else
27190 /* Comma-separated properties are chained together in
27191 reverse order; add them one by one. */
27192 properties = nreverse (properties);
27194 for (; properties; properties = TREE_CHAIN (properties))
27195 objc_add_property_declaration (loc, copy_node (properties),
27196 property_readonly, property_readwrite,
27197 property_assign, property_retain,
27198 property_copy, property_nonatomic,
27199 property_getter_ident, property_setter_ident);
27202 cp_parser_consume_semicolon_at_end_of_statement (parser);
27205 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27207 objc-synthesize-declaration:
27208 @synthesize objc-synthesize-identifier-list ;
27210 objc-synthesize-identifier-list:
27211 objc-synthesize-identifier
27212 objc-synthesize-identifier-list, objc-synthesize-identifier
27214 objc-synthesize-identifier
27215 identifier
27216 identifier = identifier
27218 For example:
27219 @synthesize MyProperty;
27220 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27222 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27223 for C. Keep them in sync.
27225 static void
27226 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27228 tree list = NULL_TREE;
27229 location_t loc;
27230 loc = cp_lexer_peek_token (parser->lexer)->location;
27232 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27233 while (true)
27235 tree property, ivar;
27236 property = cp_parser_identifier (parser);
27237 if (property == error_mark_node)
27239 cp_parser_consume_semicolon_at_end_of_statement (parser);
27240 return;
27242 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27244 cp_lexer_consume_token (parser->lexer);
27245 ivar = cp_parser_identifier (parser);
27246 if (ivar == error_mark_node)
27248 cp_parser_consume_semicolon_at_end_of_statement (parser);
27249 return;
27252 else
27253 ivar = NULL_TREE;
27254 list = chainon (list, build_tree_list (ivar, property));
27255 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27256 cp_lexer_consume_token (parser->lexer);
27257 else
27258 break;
27260 cp_parser_consume_semicolon_at_end_of_statement (parser);
27261 objc_add_synthesize_declaration (loc, list);
27264 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27266 objc-dynamic-declaration:
27267 @dynamic identifier-list ;
27269 For example:
27270 @dynamic MyProperty;
27271 @dynamic MyProperty, AnotherProperty;
27273 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27274 for C. Keep them in sync.
27276 static void
27277 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27279 tree list = NULL_TREE;
27280 location_t loc;
27281 loc = cp_lexer_peek_token (parser->lexer)->location;
27283 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27284 while (true)
27286 tree property;
27287 property = cp_parser_identifier (parser);
27288 if (property == error_mark_node)
27290 cp_parser_consume_semicolon_at_end_of_statement (parser);
27291 return;
27293 list = chainon (list, build_tree_list (NULL, property));
27294 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27295 cp_lexer_consume_token (parser->lexer);
27296 else
27297 break;
27299 cp_parser_consume_semicolon_at_end_of_statement (parser);
27300 objc_add_dynamic_declaration (loc, list);
27304 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27306 /* Returns name of the next clause.
27307 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27308 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27309 returned and the token is consumed. */
27311 static pragma_omp_clause
27312 cp_parser_omp_clause_name (cp_parser *parser)
27314 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27316 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27317 result = PRAGMA_OMP_CLAUSE_IF;
27318 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27319 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27320 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27321 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27322 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27323 result = PRAGMA_OMP_CLAUSE_FOR;
27324 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27326 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27327 const char *p = IDENTIFIER_POINTER (id);
27329 switch (p[0])
27331 case 'a':
27332 if (!strcmp ("aligned", p))
27333 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27334 break;
27335 case 'c':
27336 if (!strcmp ("collapse", p))
27337 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27338 else if (!strcmp ("copyin", p))
27339 result = PRAGMA_OMP_CLAUSE_COPYIN;
27340 else if (!strcmp ("copyprivate", p))
27341 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27342 break;
27343 case 'd':
27344 if (!strcmp ("depend", p))
27345 result = PRAGMA_OMP_CLAUSE_DEPEND;
27346 else if (!strcmp ("device", p))
27347 result = PRAGMA_OMP_CLAUSE_DEVICE;
27348 else if (!strcmp ("dist_schedule", p))
27349 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27350 break;
27351 case 'f':
27352 if (!strcmp ("final", p))
27353 result = PRAGMA_OMP_CLAUSE_FINAL;
27354 else if (!strcmp ("firstprivate", p))
27355 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27356 else if (!strcmp ("from", p))
27357 result = PRAGMA_OMP_CLAUSE_FROM;
27358 break;
27359 case 'i':
27360 if (!strcmp ("inbranch", p))
27361 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27362 break;
27363 case 'l':
27364 if (!strcmp ("lastprivate", p))
27365 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27366 else if (!strcmp ("linear", p))
27367 result = PRAGMA_OMP_CLAUSE_LINEAR;
27368 break;
27369 case 'm':
27370 if (!strcmp ("map", p))
27371 result = PRAGMA_OMP_CLAUSE_MAP;
27372 else if (!strcmp ("mergeable", p))
27373 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27374 else if (flag_cilkplus && !strcmp ("mask", p))
27375 result = PRAGMA_CILK_CLAUSE_MASK;
27376 break;
27377 case 'n':
27378 if (!strcmp ("notinbranch", p))
27379 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27380 else if (!strcmp ("nowait", p))
27381 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27382 else if (flag_cilkplus && !strcmp ("nomask", p))
27383 result = PRAGMA_CILK_CLAUSE_NOMASK;
27384 else if (!strcmp ("num_teams", p))
27385 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27386 else if (!strcmp ("num_threads", p))
27387 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27388 break;
27389 case 'o':
27390 if (!strcmp ("ordered", p))
27391 result = PRAGMA_OMP_CLAUSE_ORDERED;
27392 break;
27393 case 'p':
27394 if (!strcmp ("parallel", p))
27395 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27396 else if (!strcmp ("proc_bind", p))
27397 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27398 break;
27399 case 'r':
27400 if (!strcmp ("reduction", p))
27401 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27402 break;
27403 case 's':
27404 if (!strcmp ("safelen", p))
27405 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27406 else if (!strcmp ("schedule", p))
27407 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27408 else if (!strcmp ("sections", p))
27409 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27410 else if (!strcmp ("shared", p))
27411 result = PRAGMA_OMP_CLAUSE_SHARED;
27412 else if (!strcmp ("simdlen", p))
27413 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27414 break;
27415 case 't':
27416 if (!strcmp ("taskgroup", p))
27417 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27418 else if (!strcmp ("thread_limit", p))
27419 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27420 else if (!strcmp ("to", p))
27421 result = PRAGMA_OMP_CLAUSE_TO;
27422 break;
27423 case 'u':
27424 if (!strcmp ("uniform", p))
27425 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27426 else if (!strcmp ("untied", p))
27427 result = PRAGMA_OMP_CLAUSE_UNTIED;
27428 break;
27429 case 'v':
27430 if (flag_cilkplus && !strcmp ("vectorlength", p))
27431 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27432 break;
27436 if (result != PRAGMA_OMP_CLAUSE_NONE)
27437 cp_lexer_consume_token (parser->lexer);
27439 return result;
27442 /* Validate that a clause of the given type does not already exist. */
27444 static void
27445 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27446 const char *name, location_t location)
27448 tree c;
27450 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27451 if (OMP_CLAUSE_CODE (c) == code)
27453 error_at (location, "too many %qs clauses", name);
27454 break;
27458 /* OpenMP 2.5:
27459 variable-list:
27460 identifier
27461 variable-list , identifier
27463 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27464 colon). An opening parenthesis will have been consumed by the caller.
27466 If KIND is nonzero, create the appropriate node and install the decl
27467 in OMP_CLAUSE_DECL and add the node to the head of the list.
27469 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27470 return the list created.
27472 COLON can be NULL if only closing parenthesis should end the list,
27473 or pointer to bool which will receive false if the list is terminated
27474 by closing parenthesis or true if the list is terminated by colon. */
27476 static tree
27477 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27478 tree list, bool *colon)
27480 cp_token *token;
27481 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27482 if (colon)
27484 parser->colon_corrects_to_scope_p = false;
27485 *colon = false;
27487 while (1)
27489 tree name, decl;
27491 token = cp_lexer_peek_token (parser->lexer);
27492 name = cp_parser_id_expression (parser, /*template_p=*/false,
27493 /*check_dependency_p=*/true,
27494 /*template_p=*/NULL,
27495 /*declarator_p=*/false,
27496 /*optional_p=*/false);
27497 if (name == error_mark_node)
27498 goto skip_comma;
27500 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27501 if (decl == error_mark_node)
27502 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27503 token->location);
27504 else if (kind != 0)
27506 switch (kind)
27508 case OMP_CLAUSE_MAP:
27509 case OMP_CLAUSE_FROM:
27510 case OMP_CLAUSE_TO:
27511 case OMP_CLAUSE_DEPEND:
27512 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27514 tree low_bound = NULL_TREE, length = NULL_TREE;
27516 parser->colon_corrects_to_scope_p = false;
27517 cp_lexer_consume_token (parser->lexer);
27518 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27519 low_bound = cp_parser_expression (parser);
27520 if (!colon)
27521 parser->colon_corrects_to_scope_p
27522 = saved_colon_corrects_to_scope_p;
27523 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27524 length = integer_one_node;
27525 else
27527 /* Look for `:'. */
27528 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27529 goto skip_comma;
27530 if (!cp_lexer_next_token_is (parser->lexer,
27531 CPP_CLOSE_SQUARE))
27532 length = cp_parser_expression (parser);
27534 /* Look for the closing `]'. */
27535 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27536 RT_CLOSE_SQUARE))
27537 goto skip_comma;
27538 decl = tree_cons (low_bound, length, decl);
27540 break;
27541 default:
27542 break;
27545 tree u = build_omp_clause (token->location, kind);
27546 OMP_CLAUSE_DECL (u) = decl;
27547 OMP_CLAUSE_CHAIN (u) = list;
27548 list = u;
27550 else
27551 list = tree_cons (decl, NULL_TREE, list);
27553 get_comma:
27554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27555 break;
27556 cp_lexer_consume_token (parser->lexer);
27559 if (colon)
27560 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27562 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27564 *colon = true;
27565 cp_parser_require (parser, CPP_COLON, RT_COLON);
27566 return list;
27569 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27571 int ending;
27573 /* Try to resync to an unnested comma. Copied from
27574 cp_parser_parenthesized_expression_list. */
27575 skip_comma:
27576 if (colon)
27577 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27578 ending = cp_parser_skip_to_closing_parenthesis (parser,
27579 /*recovering=*/true,
27580 /*or_comma=*/true,
27581 /*consume_paren=*/true);
27582 if (ending < 0)
27583 goto get_comma;
27586 return list;
27589 /* Similarly, but expect leading and trailing parenthesis. This is a very
27590 common case for omp clauses. */
27592 static tree
27593 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27595 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27596 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27597 return list;
27600 /* OpenMP 3.0:
27601 collapse ( constant-expression ) */
27603 static tree
27604 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27606 tree c, num;
27607 location_t loc;
27608 HOST_WIDE_INT n;
27610 loc = cp_lexer_peek_token (parser->lexer)->location;
27611 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27612 return list;
27614 num = cp_parser_constant_expression (parser, false, NULL);
27616 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27618 /*or_comma=*/false,
27619 /*consume_paren=*/true);
27621 if (num == error_mark_node)
27622 return list;
27623 num = fold_non_dependent_expr (num);
27624 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27625 || !tree_fits_shwi_p (num)
27626 || (n = tree_to_shwi (num)) <= 0
27627 || (int) n != n)
27629 error_at (loc, "collapse argument needs positive constant integer expression");
27630 return list;
27633 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27634 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27635 OMP_CLAUSE_CHAIN (c) = list;
27636 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27638 return c;
27641 /* OpenMP 2.5:
27642 default ( shared | none ) */
27644 static tree
27645 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27647 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27648 tree c;
27650 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27651 return list;
27652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27655 const char *p = IDENTIFIER_POINTER (id);
27657 switch (p[0])
27659 case 'n':
27660 if (strcmp ("none", p) != 0)
27661 goto invalid_kind;
27662 kind = OMP_CLAUSE_DEFAULT_NONE;
27663 break;
27665 case 's':
27666 if (strcmp ("shared", p) != 0)
27667 goto invalid_kind;
27668 kind = OMP_CLAUSE_DEFAULT_SHARED;
27669 break;
27671 default:
27672 goto invalid_kind;
27675 cp_lexer_consume_token (parser->lexer);
27677 else
27679 invalid_kind:
27680 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27683 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27684 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27685 /*or_comma=*/false,
27686 /*consume_paren=*/true);
27688 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27689 return list;
27691 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27692 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27693 OMP_CLAUSE_CHAIN (c) = list;
27694 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27696 return c;
27699 /* OpenMP 3.1:
27700 final ( expression ) */
27702 static tree
27703 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27705 tree t, c;
27707 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27708 return list;
27710 t = cp_parser_condition (parser);
27712 if (t == error_mark_node
27713 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27714 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27715 /*or_comma=*/false,
27716 /*consume_paren=*/true);
27718 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27720 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27721 OMP_CLAUSE_FINAL_EXPR (c) = t;
27722 OMP_CLAUSE_CHAIN (c) = list;
27724 return c;
27727 /* OpenMP 2.5:
27728 if ( expression ) */
27730 static tree
27731 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27733 tree t, c;
27735 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27736 return list;
27738 t = cp_parser_condition (parser);
27740 if (t == error_mark_node
27741 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27742 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27743 /*or_comma=*/false,
27744 /*consume_paren=*/true);
27746 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27748 c = build_omp_clause (location, OMP_CLAUSE_IF);
27749 OMP_CLAUSE_IF_EXPR (c) = t;
27750 OMP_CLAUSE_CHAIN (c) = list;
27752 return c;
27755 /* OpenMP 3.1:
27756 mergeable */
27758 static tree
27759 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27760 tree list, location_t location)
27762 tree c;
27764 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27765 location);
27767 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27768 OMP_CLAUSE_CHAIN (c) = list;
27769 return c;
27772 /* OpenMP 2.5:
27773 nowait */
27775 static tree
27776 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27777 tree list, location_t location)
27779 tree c;
27781 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27783 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27784 OMP_CLAUSE_CHAIN (c) = list;
27785 return c;
27788 /* OpenMP 2.5:
27789 num_threads ( expression ) */
27791 static tree
27792 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27793 location_t location)
27795 tree t, c;
27797 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27798 return list;
27800 t = cp_parser_expression (parser);
27802 if (t == error_mark_node
27803 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27804 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27805 /*or_comma=*/false,
27806 /*consume_paren=*/true);
27808 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27809 "num_threads", location);
27811 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27812 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27813 OMP_CLAUSE_CHAIN (c) = list;
27815 return c;
27818 /* OpenMP 2.5:
27819 ordered */
27821 static tree
27822 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27823 tree list, location_t location)
27825 tree c;
27827 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27828 "ordered", location);
27830 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27831 OMP_CLAUSE_CHAIN (c) = list;
27832 return c;
27835 /* OpenMP 2.5:
27836 reduction ( reduction-operator : variable-list )
27838 reduction-operator:
27839 One of: + * - & ^ | && ||
27841 OpenMP 3.1:
27843 reduction-operator:
27844 One of: + * - & ^ | && || min max
27846 OpenMP 4.0:
27848 reduction-operator:
27849 One of: + * - & ^ | && ||
27850 id-expression */
27852 static tree
27853 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27855 enum tree_code code = ERROR_MARK;
27856 tree nlist, c, id = NULL_TREE;
27858 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27859 return list;
27861 switch (cp_lexer_peek_token (parser->lexer)->type)
27863 case CPP_PLUS: code = PLUS_EXPR; break;
27864 case CPP_MULT: code = MULT_EXPR; break;
27865 case CPP_MINUS: code = MINUS_EXPR; break;
27866 case CPP_AND: code = BIT_AND_EXPR; break;
27867 case CPP_XOR: code = BIT_XOR_EXPR; break;
27868 case CPP_OR: code = BIT_IOR_EXPR; break;
27869 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27870 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27871 default: break;
27874 if (code != ERROR_MARK)
27875 cp_lexer_consume_token (parser->lexer);
27876 else
27878 bool saved_colon_corrects_to_scope_p;
27879 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27880 parser->colon_corrects_to_scope_p = false;
27881 id = cp_parser_id_expression (parser, /*template_p=*/false,
27882 /*check_dependency_p=*/true,
27883 /*template_p=*/NULL,
27884 /*declarator_p=*/false,
27885 /*optional_p=*/false);
27886 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27887 if (identifier_p (id))
27889 const char *p = IDENTIFIER_POINTER (id);
27891 if (strcmp (p, "min") == 0)
27892 code = MIN_EXPR;
27893 else if (strcmp (p, "max") == 0)
27894 code = MAX_EXPR;
27895 else if (id == ansi_opname (PLUS_EXPR))
27896 code = PLUS_EXPR;
27897 else if (id == ansi_opname (MULT_EXPR))
27898 code = MULT_EXPR;
27899 else if (id == ansi_opname (MINUS_EXPR))
27900 code = MINUS_EXPR;
27901 else if (id == ansi_opname (BIT_AND_EXPR))
27902 code = BIT_AND_EXPR;
27903 else if (id == ansi_opname (BIT_IOR_EXPR))
27904 code = BIT_IOR_EXPR;
27905 else if (id == ansi_opname (BIT_XOR_EXPR))
27906 code = BIT_XOR_EXPR;
27907 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27908 code = TRUTH_ANDIF_EXPR;
27909 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27910 code = TRUTH_ORIF_EXPR;
27911 id = omp_reduction_id (code, id, NULL_TREE);
27912 tree scope = parser->scope;
27913 if (scope)
27914 id = build_qualified_name (NULL_TREE, scope, id, false);
27915 parser->scope = NULL_TREE;
27916 parser->qualifying_scope = NULL_TREE;
27917 parser->object_scope = NULL_TREE;
27919 else
27921 error ("invalid reduction-identifier");
27922 resync_fail:
27923 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27924 /*or_comma=*/false,
27925 /*consume_paren=*/true);
27926 return list;
27930 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27931 goto resync_fail;
27933 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27934 NULL);
27935 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27937 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27938 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27941 return nlist;
27944 /* OpenMP 2.5:
27945 schedule ( schedule-kind )
27946 schedule ( schedule-kind , expression )
27948 schedule-kind:
27949 static | dynamic | guided | runtime | auto */
27951 static tree
27952 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27954 tree c, t;
27956 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27957 return list;
27959 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27961 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27963 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27964 const char *p = IDENTIFIER_POINTER (id);
27966 switch (p[0])
27968 case 'd':
27969 if (strcmp ("dynamic", p) != 0)
27970 goto invalid_kind;
27971 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27972 break;
27974 case 'g':
27975 if (strcmp ("guided", p) != 0)
27976 goto invalid_kind;
27977 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27978 break;
27980 case 'r':
27981 if (strcmp ("runtime", p) != 0)
27982 goto invalid_kind;
27983 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27984 break;
27986 default:
27987 goto invalid_kind;
27990 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27991 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27992 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27993 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27994 else
27995 goto invalid_kind;
27996 cp_lexer_consume_token (parser->lexer);
27998 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28000 cp_token *token;
28001 cp_lexer_consume_token (parser->lexer);
28003 token = cp_lexer_peek_token (parser->lexer);
28004 t = cp_parser_assignment_expression (parser, false, NULL);
28006 if (t == error_mark_node)
28007 goto resync_fail;
28008 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28009 error_at (token->location, "schedule %<runtime%> does not take "
28010 "a %<chunk_size%> parameter");
28011 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28012 error_at (token->location, "schedule %<auto%> does not take "
28013 "a %<chunk_size%> parameter");
28014 else
28015 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28017 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28018 goto resync_fail;
28020 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28021 goto resync_fail;
28023 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28024 OMP_CLAUSE_CHAIN (c) = list;
28025 return c;
28027 invalid_kind:
28028 cp_parser_error (parser, "invalid schedule kind");
28029 resync_fail:
28030 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28031 /*or_comma=*/false,
28032 /*consume_paren=*/true);
28033 return list;
28036 /* OpenMP 3.0:
28037 untied */
28039 static tree
28040 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28041 tree list, location_t location)
28043 tree c;
28045 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28047 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28048 OMP_CLAUSE_CHAIN (c) = list;
28049 return c;
28052 /* OpenMP 4.0:
28053 inbranch
28054 notinbranch */
28056 static tree
28057 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28058 tree list, location_t location)
28060 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28061 tree c = build_omp_clause (location, code);
28062 OMP_CLAUSE_CHAIN (c) = list;
28063 return c;
28066 /* OpenMP 4.0:
28067 parallel
28069 sections
28070 taskgroup */
28072 static tree
28073 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28074 enum omp_clause_code code,
28075 tree list, location_t location)
28077 tree c = build_omp_clause (location, code);
28078 OMP_CLAUSE_CHAIN (c) = list;
28079 return c;
28082 /* OpenMP 4.0:
28083 num_teams ( expression ) */
28085 static tree
28086 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28087 location_t location)
28089 tree t, c;
28091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28092 return list;
28094 t = cp_parser_expression (parser);
28096 if (t == error_mark_node
28097 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28099 /*or_comma=*/false,
28100 /*consume_paren=*/true);
28102 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28103 "num_teams", location);
28105 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28106 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28107 OMP_CLAUSE_CHAIN (c) = list;
28109 return c;
28112 /* OpenMP 4.0:
28113 thread_limit ( expression ) */
28115 static tree
28116 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28117 location_t location)
28119 tree t, c;
28121 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28122 return list;
28124 t = cp_parser_expression (parser);
28126 if (t == error_mark_node
28127 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28128 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28129 /*or_comma=*/false,
28130 /*consume_paren=*/true);
28132 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28133 "thread_limit", location);
28135 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28136 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28137 OMP_CLAUSE_CHAIN (c) = list;
28139 return c;
28142 /* OpenMP 4.0:
28143 aligned ( variable-list )
28144 aligned ( variable-list : constant-expression ) */
28146 static tree
28147 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28149 tree nlist, c, alignment = NULL_TREE;
28150 bool colon;
28152 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28153 return list;
28155 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28156 &colon);
28158 if (colon)
28160 alignment = cp_parser_constant_expression (parser, false, NULL);
28162 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28163 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28164 /*or_comma=*/false,
28165 /*consume_paren=*/true);
28167 if (alignment == error_mark_node)
28168 alignment = NULL_TREE;
28171 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28172 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28174 return nlist;
28177 /* OpenMP 4.0:
28178 linear ( variable-list )
28179 linear ( variable-list : expression ) */
28181 static tree
28182 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28183 bool is_cilk_simd_fn)
28185 tree nlist, c, step = integer_one_node;
28186 bool colon;
28188 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28189 return list;
28191 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28192 &colon);
28194 if (colon)
28196 step = cp_parser_expression (parser);
28198 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28200 sorry ("using parameters for %<linear%> step is not supported yet");
28201 step = integer_one_node;
28203 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28204 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28205 /*or_comma=*/false,
28206 /*consume_paren=*/true);
28208 if (step == error_mark_node)
28209 return list;
28212 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28213 OMP_CLAUSE_LINEAR_STEP (c) = step;
28215 return nlist;
28218 /* OpenMP 4.0:
28219 safelen ( constant-expression ) */
28221 static tree
28222 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28223 location_t location)
28225 tree t, c;
28227 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28228 return list;
28230 t = cp_parser_constant_expression (parser, false, NULL);
28232 if (t == error_mark_node
28233 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28234 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28235 /*or_comma=*/false,
28236 /*consume_paren=*/true);
28238 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28240 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28241 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28242 OMP_CLAUSE_CHAIN (c) = list;
28244 return c;
28247 /* OpenMP 4.0:
28248 simdlen ( constant-expression ) */
28250 static tree
28251 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28252 location_t location)
28254 tree t, c;
28256 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28257 return list;
28259 t = cp_parser_constant_expression (parser, false, NULL);
28261 if (t == error_mark_node
28262 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28263 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28264 /*or_comma=*/false,
28265 /*consume_paren=*/true);
28267 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28269 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28270 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28271 OMP_CLAUSE_CHAIN (c) = list;
28273 return c;
28276 /* OpenMP 4.0:
28277 depend ( depend-kind : variable-list )
28279 depend-kind:
28280 in | out | inout */
28282 static tree
28283 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28285 tree nlist, c;
28286 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28288 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28289 return list;
28291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28293 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28294 const char *p = IDENTIFIER_POINTER (id);
28296 if (strcmp ("in", p) == 0)
28297 kind = OMP_CLAUSE_DEPEND_IN;
28298 else if (strcmp ("inout", p) == 0)
28299 kind = OMP_CLAUSE_DEPEND_INOUT;
28300 else if (strcmp ("out", p) == 0)
28301 kind = OMP_CLAUSE_DEPEND_OUT;
28302 else
28303 goto invalid_kind;
28305 else
28306 goto invalid_kind;
28308 cp_lexer_consume_token (parser->lexer);
28309 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28310 goto resync_fail;
28312 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28313 NULL);
28315 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28316 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28318 return nlist;
28320 invalid_kind:
28321 cp_parser_error (parser, "invalid depend kind");
28322 resync_fail:
28323 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28324 /*or_comma=*/false,
28325 /*consume_paren=*/true);
28326 return list;
28329 /* OpenMP 4.0:
28330 map ( map-kind : variable-list )
28331 map ( variable-list )
28333 map-kind:
28334 alloc | to | from | tofrom */
28336 static tree
28337 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28339 tree nlist, c;
28340 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28342 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28343 return list;
28345 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28346 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28348 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28349 const char *p = IDENTIFIER_POINTER (id);
28351 if (strcmp ("alloc", p) == 0)
28352 kind = OMP_CLAUSE_MAP_ALLOC;
28353 else if (strcmp ("to", p) == 0)
28354 kind = OMP_CLAUSE_MAP_TO;
28355 else if (strcmp ("from", p) == 0)
28356 kind = OMP_CLAUSE_MAP_FROM;
28357 else if (strcmp ("tofrom", p) == 0)
28358 kind = OMP_CLAUSE_MAP_TOFROM;
28359 else
28361 cp_parser_error (parser, "invalid map kind");
28362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28363 /*or_comma=*/false,
28364 /*consume_paren=*/true);
28365 return list;
28367 cp_lexer_consume_token (parser->lexer);
28368 cp_lexer_consume_token (parser->lexer);
28371 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28372 NULL);
28374 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28375 OMP_CLAUSE_MAP_KIND (c) = kind;
28377 return nlist;
28380 /* OpenMP 4.0:
28381 device ( expression ) */
28383 static tree
28384 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28385 location_t location)
28387 tree t, c;
28389 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28390 return list;
28392 t = cp_parser_expression (parser);
28394 if (t == error_mark_node
28395 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28396 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28397 /*or_comma=*/false,
28398 /*consume_paren=*/true);
28400 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28401 "device", location);
28403 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28404 OMP_CLAUSE_DEVICE_ID (c) = t;
28405 OMP_CLAUSE_CHAIN (c) = list;
28407 return c;
28410 /* OpenMP 4.0:
28411 dist_schedule ( static )
28412 dist_schedule ( static , expression ) */
28414 static tree
28415 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28416 location_t location)
28418 tree c, t;
28420 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28421 return list;
28423 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28425 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28426 goto invalid_kind;
28427 cp_lexer_consume_token (parser->lexer);
28429 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28431 cp_lexer_consume_token (parser->lexer);
28433 t = cp_parser_assignment_expression (parser, false, NULL);
28435 if (t == error_mark_node)
28436 goto resync_fail;
28437 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28440 goto resync_fail;
28442 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28443 goto resync_fail;
28445 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28446 location);
28447 OMP_CLAUSE_CHAIN (c) = list;
28448 return c;
28450 invalid_kind:
28451 cp_parser_error (parser, "invalid dist_schedule kind");
28452 resync_fail:
28453 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28454 /*or_comma=*/false,
28455 /*consume_paren=*/true);
28456 return list;
28459 /* OpenMP 4.0:
28460 proc_bind ( proc-bind-kind )
28462 proc-bind-kind:
28463 master | close | spread */
28465 static tree
28466 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28467 location_t location)
28469 tree c;
28470 enum omp_clause_proc_bind_kind kind;
28472 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28473 return list;
28475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28478 const char *p = IDENTIFIER_POINTER (id);
28480 if (strcmp ("master", p) == 0)
28481 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28482 else if (strcmp ("close", p) == 0)
28483 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28484 else if (strcmp ("spread", p) == 0)
28485 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28486 else
28487 goto invalid_kind;
28489 else
28490 goto invalid_kind;
28492 cp_lexer_consume_token (parser->lexer);
28493 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28494 goto resync_fail;
28496 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28497 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28498 location);
28499 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28500 OMP_CLAUSE_CHAIN (c) = list;
28501 return c;
28503 invalid_kind:
28504 cp_parser_error (parser, "invalid depend kind");
28505 resync_fail:
28506 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28507 /*or_comma=*/false,
28508 /*consume_paren=*/true);
28509 return list;
28512 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28513 is a bitmask in MASK. Return the list of clauses found; the result
28514 of clause default goes in *pdefault. */
28516 static tree
28517 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28518 const char *where, cp_token *pragma_tok,
28519 bool finish_p = true)
28521 tree clauses = NULL;
28522 bool first = true;
28523 cp_token *token = NULL;
28524 bool cilk_simd_fn = false;
28526 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28528 pragma_omp_clause c_kind;
28529 const char *c_name;
28530 tree prev = clauses;
28532 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28533 cp_lexer_consume_token (parser->lexer);
28535 token = cp_lexer_peek_token (parser->lexer);
28536 c_kind = cp_parser_omp_clause_name (parser);
28538 switch (c_kind)
28540 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28541 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28542 token->location);
28543 c_name = "collapse";
28544 break;
28545 case PRAGMA_OMP_CLAUSE_COPYIN:
28546 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28547 c_name = "copyin";
28548 break;
28549 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28550 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28551 clauses);
28552 c_name = "copyprivate";
28553 break;
28554 case PRAGMA_OMP_CLAUSE_DEFAULT:
28555 clauses = cp_parser_omp_clause_default (parser, clauses,
28556 token->location);
28557 c_name = "default";
28558 break;
28559 case PRAGMA_OMP_CLAUSE_FINAL:
28560 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28561 c_name = "final";
28562 break;
28563 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28564 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28565 clauses);
28566 c_name = "firstprivate";
28567 break;
28568 case PRAGMA_OMP_CLAUSE_IF:
28569 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28570 c_name = "if";
28571 break;
28572 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28573 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28574 clauses);
28575 c_name = "lastprivate";
28576 break;
28577 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28578 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28579 token->location);
28580 c_name = "mergeable";
28581 break;
28582 case PRAGMA_OMP_CLAUSE_NOWAIT:
28583 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28584 c_name = "nowait";
28585 break;
28586 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28587 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28588 token->location);
28589 c_name = "num_threads";
28590 break;
28591 case PRAGMA_OMP_CLAUSE_ORDERED:
28592 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28593 token->location);
28594 c_name = "ordered";
28595 break;
28596 case PRAGMA_OMP_CLAUSE_PRIVATE:
28597 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28598 clauses);
28599 c_name = "private";
28600 break;
28601 case PRAGMA_OMP_CLAUSE_REDUCTION:
28602 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28603 c_name = "reduction";
28604 break;
28605 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28606 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28607 token->location);
28608 c_name = "schedule";
28609 break;
28610 case PRAGMA_OMP_CLAUSE_SHARED:
28611 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28612 clauses);
28613 c_name = "shared";
28614 break;
28615 case PRAGMA_OMP_CLAUSE_UNTIED:
28616 clauses = cp_parser_omp_clause_untied (parser, clauses,
28617 token->location);
28618 c_name = "untied";
28619 break;
28620 case PRAGMA_OMP_CLAUSE_INBRANCH:
28621 case PRAGMA_CILK_CLAUSE_MASK:
28622 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28623 clauses, token->location);
28624 c_name = "inbranch";
28625 break;
28626 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28627 case PRAGMA_CILK_CLAUSE_NOMASK:
28628 clauses = cp_parser_omp_clause_branch (parser,
28629 OMP_CLAUSE_NOTINBRANCH,
28630 clauses, token->location);
28631 c_name = "notinbranch";
28632 break;
28633 case PRAGMA_OMP_CLAUSE_PARALLEL:
28634 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28635 clauses, token->location);
28636 c_name = "parallel";
28637 if (!first)
28639 clause_not_first:
28640 error_at (token->location, "%qs must be the first clause of %qs",
28641 c_name, where);
28642 clauses = prev;
28644 break;
28645 case PRAGMA_OMP_CLAUSE_FOR:
28646 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28647 clauses, token->location);
28648 c_name = "for";
28649 if (!first)
28650 goto clause_not_first;
28651 break;
28652 case PRAGMA_OMP_CLAUSE_SECTIONS:
28653 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28654 clauses, token->location);
28655 c_name = "sections";
28656 if (!first)
28657 goto clause_not_first;
28658 break;
28659 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28660 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28661 clauses, token->location);
28662 c_name = "taskgroup";
28663 if (!first)
28664 goto clause_not_first;
28665 break;
28666 case PRAGMA_OMP_CLAUSE_TO:
28667 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28668 clauses);
28669 c_name = "to";
28670 break;
28671 case PRAGMA_OMP_CLAUSE_FROM:
28672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28673 clauses);
28674 c_name = "from";
28675 break;
28676 case PRAGMA_OMP_CLAUSE_UNIFORM:
28677 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28678 clauses);
28679 c_name = "uniform";
28680 break;
28681 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28682 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28683 token->location);
28684 c_name = "num_teams";
28685 break;
28686 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28687 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28688 token->location);
28689 c_name = "thread_limit";
28690 break;
28691 case PRAGMA_OMP_CLAUSE_ALIGNED:
28692 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28693 c_name = "aligned";
28694 break;
28695 case PRAGMA_OMP_CLAUSE_LINEAR:
28696 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28697 cilk_simd_fn = true;
28698 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28699 c_name = "linear";
28700 break;
28701 case PRAGMA_OMP_CLAUSE_DEPEND:
28702 clauses = cp_parser_omp_clause_depend (parser, clauses);
28703 c_name = "depend";
28704 break;
28705 case PRAGMA_OMP_CLAUSE_MAP:
28706 clauses = cp_parser_omp_clause_map (parser, clauses);
28707 c_name = "map";
28708 break;
28709 case PRAGMA_OMP_CLAUSE_DEVICE:
28710 clauses = cp_parser_omp_clause_device (parser, clauses,
28711 token->location);
28712 c_name = "device";
28713 break;
28714 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28715 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28716 token->location);
28717 c_name = "dist_schedule";
28718 break;
28719 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28720 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28721 token->location);
28722 c_name = "proc_bind";
28723 break;
28724 case PRAGMA_OMP_CLAUSE_SAFELEN:
28725 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28726 token->location);
28727 c_name = "safelen";
28728 break;
28729 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28730 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28731 token->location);
28732 c_name = "simdlen";
28733 break;
28734 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28735 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28736 c_name = "simdlen";
28737 break;
28738 default:
28739 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28740 goto saw_error;
28743 first = false;
28745 if (((mask >> c_kind) & 1) == 0)
28747 /* Remove the invalid clause(s) from the list to avoid
28748 confusing the rest of the compiler. */
28749 clauses = prev;
28750 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28753 saw_error:
28754 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28755 no reason to skip to the end. */
28756 if (!(flag_cilkplus && pragma_tok == NULL))
28757 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28758 if (finish_p)
28759 return finish_omp_clauses (clauses);
28760 return clauses;
28763 /* OpenMP 2.5:
28764 structured-block:
28765 statement
28767 In practice, we're also interested in adding the statement to an
28768 outer node. So it is convenient if we work around the fact that
28769 cp_parser_statement calls add_stmt. */
28771 static unsigned
28772 cp_parser_begin_omp_structured_block (cp_parser *parser)
28774 unsigned save = parser->in_statement;
28776 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28777 This preserves the "not within loop or switch" style error messages
28778 for nonsense cases like
28779 void foo() {
28780 #pragma omp single
28781 break;
28784 if (parser->in_statement)
28785 parser->in_statement = IN_OMP_BLOCK;
28787 return save;
28790 static void
28791 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28793 parser->in_statement = save;
28796 static tree
28797 cp_parser_omp_structured_block (cp_parser *parser)
28799 tree stmt = begin_omp_structured_block ();
28800 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28802 cp_parser_statement (parser, NULL_TREE, false, NULL);
28804 cp_parser_end_omp_structured_block (parser, save);
28805 return finish_omp_structured_block (stmt);
28808 /* OpenMP 2.5:
28809 # pragma omp atomic new-line
28810 expression-stmt
28812 expression-stmt:
28813 x binop= expr | x++ | ++x | x-- | --x
28814 binop:
28815 +, *, -, /, &, ^, |, <<, >>
28817 where x is an lvalue expression with scalar type.
28819 OpenMP 3.1:
28820 # pragma omp atomic new-line
28821 update-stmt
28823 # pragma omp atomic read new-line
28824 read-stmt
28826 # pragma omp atomic write new-line
28827 write-stmt
28829 # pragma omp atomic update new-line
28830 update-stmt
28832 # pragma omp atomic capture new-line
28833 capture-stmt
28835 # pragma omp atomic capture new-line
28836 capture-block
28838 read-stmt:
28839 v = x
28840 write-stmt:
28841 x = expr
28842 update-stmt:
28843 expression-stmt | x = x binop expr
28844 capture-stmt:
28845 v = expression-stmt
28846 capture-block:
28847 { v = x; update-stmt; } | { update-stmt; v = x; }
28849 OpenMP 4.0:
28850 update-stmt:
28851 expression-stmt | x = x binop expr | x = expr binop x
28852 capture-stmt:
28853 v = update-stmt
28854 capture-block:
28855 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28857 where x and v are lvalue expressions with scalar type. */
28859 static void
28860 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28862 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28863 tree rhs1 = NULL_TREE, orig_lhs;
28864 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28865 bool structured_block = false;
28866 bool seq_cst = false;
28868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28870 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28871 const char *p = IDENTIFIER_POINTER (id);
28873 if (!strcmp (p, "seq_cst"))
28875 seq_cst = true;
28876 cp_lexer_consume_token (parser->lexer);
28877 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28878 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28879 cp_lexer_consume_token (parser->lexer);
28882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28884 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28885 const char *p = IDENTIFIER_POINTER (id);
28887 if (!strcmp (p, "read"))
28888 code = OMP_ATOMIC_READ;
28889 else if (!strcmp (p, "write"))
28890 code = NOP_EXPR;
28891 else if (!strcmp (p, "update"))
28892 code = OMP_ATOMIC;
28893 else if (!strcmp (p, "capture"))
28894 code = OMP_ATOMIC_CAPTURE_NEW;
28895 else
28896 p = NULL;
28897 if (p)
28898 cp_lexer_consume_token (parser->lexer);
28900 if (!seq_cst)
28902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28903 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28904 cp_lexer_consume_token (parser->lexer);
28906 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28908 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28909 const char *p = IDENTIFIER_POINTER (id);
28911 if (!strcmp (p, "seq_cst"))
28913 seq_cst = true;
28914 cp_lexer_consume_token (parser->lexer);
28918 cp_parser_require_pragma_eol (parser, pragma_tok);
28920 switch (code)
28922 case OMP_ATOMIC_READ:
28923 case NOP_EXPR: /* atomic write */
28924 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28925 /*cast_p=*/false, NULL);
28926 if (v == error_mark_node)
28927 goto saw_error;
28928 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28929 goto saw_error;
28930 if (code == NOP_EXPR)
28931 lhs = cp_parser_expression (parser);
28932 else
28933 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28934 /*cast_p=*/false, NULL);
28935 if (lhs == error_mark_node)
28936 goto saw_error;
28937 if (code == NOP_EXPR)
28939 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28940 opcode. */
28941 code = OMP_ATOMIC;
28942 rhs = lhs;
28943 lhs = v;
28944 v = NULL_TREE;
28946 goto done;
28947 case OMP_ATOMIC_CAPTURE_NEW:
28948 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28950 cp_lexer_consume_token (parser->lexer);
28951 structured_block = true;
28953 else
28955 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28956 /*cast_p=*/false, NULL);
28957 if (v == error_mark_node)
28958 goto saw_error;
28959 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28960 goto saw_error;
28962 default:
28963 break;
28966 restart:
28967 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28968 /*cast_p=*/false, NULL);
28969 orig_lhs = lhs;
28970 switch (TREE_CODE (lhs))
28972 case ERROR_MARK:
28973 goto saw_error;
28975 case POSTINCREMENT_EXPR:
28976 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28977 code = OMP_ATOMIC_CAPTURE_OLD;
28978 /* FALLTHROUGH */
28979 case PREINCREMENT_EXPR:
28980 lhs = TREE_OPERAND (lhs, 0);
28981 opcode = PLUS_EXPR;
28982 rhs = integer_one_node;
28983 break;
28985 case POSTDECREMENT_EXPR:
28986 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28987 code = OMP_ATOMIC_CAPTURE_OLD;
28988 /* FALLTHROUGH */
28989 case PREDECREMENT_EXPR:
28990 lhs = TREE_OPERAND (lhs, 0);
28991 opcode = MINUS_EXPR;
28992 rhs = integer_one_node;
28993 break;
28995 case COMPOUND_EXPR:
28996 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28997 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28998 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28999 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29000 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29001 (TREE_OPERAND (lhs, 1), 0), 0)))
29002 == BOOLEAN_TYPE)
29003 /* Undo effects of boolean_increment for post {in,de}crement. */
29004 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29005 /* FALLTHRU */
29006 case MODIFY_EXPR:
29007 if (TREE_CODE (lhs) == MODIFY_EXPR
29008 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29010 /* Undo effects of boolean_increment. */
29011 if (integer_onep (TREE_OPERAND (lhs, 1)))
29013 /* This is pre or post increment. */
29014 rhs = TREE_OPERAND (lhs, 1);
29015 lhs = TREE_OPERAND (lhs, 0);
29016 opcode = NOP_EXPR;
29017 if (code == OMP_ATOMIC_CAPTURE_NEW
29018 && !structured_block
29019 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29020 code = OMP_ATOMIC_CAPTURE_OLD;
29021 break;
29024 /* FALLTHRU */
29025 default:
29026 switch (cp_lexer_peek_token (parser->lexer)->type)
29028 case CPP_MULT_EQ:
29029 opcode = MULT_EXPR;
29030 break;
29031 case CPP_DIV_EQ:
29032 opcode = TRUNC_DIV_EXPR;
29033 break;
29034 case CPP_PLUS_EQ:
29035 opcode = PLUS_EXPR;
29036 break;
29037 case CPP_MINUS_EQ:
29038 opcode = MINUS_EXPR;
29039 break;
29040 case CPP_LSHIFT_EQ:
29041 opcode = LSHIFT_EXPR;
29042 break;
29043 case CPP_RSHIFT_EQ:
29044 opcode = RSHIFT_EXPR;
29045 break;
29046 case CPP_AND_EQ:
29047 opcode = BIT_AND_EXPR;
29048 break;
29049 case CPP_OR_EQ:
29050 opcode = BIT_IOR_EXPR;
29051 break;
29052 case CPP_XOR_EQ:
29053 opcode = BIT_XOR_EXPR;
29054 break;
29055 case CPP_EQ:
29056 enum cp_parser_prec oprec;
29057 cp_token *token;
29058 cp_lexer_consume_token (parser->lexer);
29059 cp_parser_parse_tentatively (parser);
29060 rhs1 = cp_parser_simple_cast_expression (parser);
29061 if (rhs1 == error_mark_node)
29063 cp_parser_abort_tentative_parse (parser);
29064 cp_parser_simple_cast_expression (parser);
29065 goto saw_error;
29067 token = cp_lexer_peek_token (parser->lexer);
29068 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29070 cp_parser_abort_tentative_parse (parser);
29071 cp_parser_parse_tentatively (parser);
29072 rhs = cp_parser_binary_expression (parser, false, true,
29073 PREC_NOT_OPERATOR, NULL);
29074 if (rhs == error_mark_node)
29076 cp_parser_abort_tentative_parse (parser);
29077 cp_parser_binary_expression (parser, false, true,
29078 PREC_NOT_OPERATOR, NULL);
29079 goto saw_error;
29081 switch (TREE_CODE (rhs))
29083 case MULT_EXPR:
29084 case TRUNC_DIV_EXPR:
29085 case PLUS_EXPR:
29086 case MINUS_EXPR:
29087 case LSHIFT_EXPR:
29088 case RSHIFT_EXPR:
29089 case BIT_AND_EXPR:
29090 case BIT_IOR_EXPR:
29091 case BIT_XOR_EXPR:
29092 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29094 if (cp_parser_parse_definitely (parser))
29096 opcode = TREE_CODE (rhs);
29097 rhs1 = TREE_OPERAND (rhs, 0);
29098 rhs = TREE_OPERAND (rhs, 1);
29099 goto stmt_done;
29101 else
29102 goto saw_error;
29104 break;
29105 default:
29106 break;
29108 cp_parser_abort_tentative_parse (parser);
29109 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29111 rhs = cp_parser_expression (parser);
29112 if (rhs == error_mark_node)
29113 goto saw_error;
29114 opcode = NOP_EXPR;
29115 rhs1 = NULL_TREE;
29116 goto stmt_done;
29118 cp_parser_error (parser,
29119 "invalid form of %<#pragma omp atomic%>");
29120 goto saw_error;
29122 if (!cp_parser_parse_definitely (parser))
29123 goto saw_error;
29124 switch (token->type)
29126 case CPP_SEMICOLON:
29127 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29129 code = OMP_ATOMIC_CAPTURE_OLD;
29130 v = lhs;
29131 lhs = NULL_TREE;
29132 lhs1 = rhs1;
29133 rhs1 = NULL_TREE;
29134 cp_lexer_consume_token (parser->lexer);
29135 goto restart;
29137 else if (structured_block)
29139 opcode = NOP_EXPR;
29140 rhs = rhs1;
29141 rhs1 = NULL_TREE;
29142 goto stmt_done;
29144 cp_parser_error (parser,
29145 "invalid form of %<#pragma omp atomic%>");
29146 goto saw_error;
29147 case CPP_MULT:
29148 opcode = MULT_EXPR;
29149 break;
29150 case CPP_DIV:
29151 opcode = TRUNC_DIV_EXPR;
29152 break;
29153 case CPP_PLUS:
29154 opcode = PLUS_EXPR;
29155 break;
29156 case CPP_MINUS:
29157 opcode = MINUS_EXPR;
29158 break;
29159 case CPP_LSHIFT:
29160 opcode = LSHIFT_EXPR;
29161 break;
29162 case CPP_RSHIFT:
29163 opcode = RSHIFT_EXPR;
29164 break;
29165 case CPP_AND:
29166 opcode = BIT_AND_EXPR;
29167 break;
29168 case CPP_OR:
29169 opcode = BIT_IOR_EXPR;
29170 break;
29171 case CPP_XOR:
29172 opcode = BIT_XOR_EXPR;
29173 break;
29174 default:
29175 cp_parser_error (parser,
29176 "invalid operator for %<#pragma omp atomic%>");
29177 goto saw_error;
29179 oprec = TOKEN_PRECEDENCE (token);
29180 gcc_assert (oprec != PREC_NOT_OPERATOR);
29181 if (commutative_tree_code (opcode))
29182 oprec = (enum cp_parser_prec) (oprec - 1);
29183 cp_lexer_consume_token (parser->lexer);
29184 rhs = cp_parser_binary_expression (parser, false, false,
29185 oprec, NULL);
29186 if (rhs == error_mark_node)
29187 goto saw_error;
29188 goto stmt_done;
29189 /* FALLTHROUGH */
29190 default:
29191 cp_parser_error (parser,
29192 "invalid operator for %<#pragma omp atomic%>");
29193 goto saw_error;
29195 cp_lexer_consume_token (parser->lexer);
29197 rhs = cp_parser_expression (parser);
29198 if (rhs == error_mark_node)
29199 goto saw_error;
29200 break;
29202 stmt_done:
29203 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29205 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29206 goto saw_error;
29207 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29208 /*cast_p=*/false, NULL);
29209 if (v == error_mark_node)
29210 goto saw_error;
29211 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29212 goto saw_error;
29213 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29214 /*cast_p=*/false, NULL);
29215 if (lhs1 == error_mark_node)
29216 goto saw_error;
29218 if (structured_block)
29220 cp_parser_consume_semicolon_at_end_of_statement (parser);
29221 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29223 done:
29224 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29225 if (!structured_block)
29226 cp_parser_consume_semicolon_at_end_of_statement (parser);
29227 return;
29229 saw_error:
29230 cp_parser_skip_to_end_of_block_or_statement (parser);
29231 if (structured_block)
29233 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29234 cp_lexer_consume_token (parser->lexer);
29235 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29237 cp_parser_skip_to_end_of_block_or_statement (parser);
29238 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29239 cp_lexer_consume_token (parser->lexer);
29245 /* OpenMP 2.5:
29246 # pragma omp barrier new-line */
29248 static void
29249 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29251 cp_parser_require_pragma_eol (parser, pragma_tok);
29252 finish_omp_barrier ();
29255 /* OpenMP 2.5:
29256 # pragma omp critical [(name)] new-line
29257 structured-block */
29259 static tree
29260 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29262 tree stmt, name = NULL;
29264 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29266 cp_lexer_consume_token (parser->lexer);
29268 name = cp_parser_identifier (parser);
29270 if (name == error_mark_node
29271 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29273 /*or_comma=*/false,
29274 /*consume_paren=*/true);
29275 if (name == error_mark_node)
29276 name = NULL;
29278 cp_parser_require_pragma_eol (parser, pragma_tok);
29280 stmt = cp_parser_omp_structured_block (parser);
29281 return c_finish_omp_critical (input_location, stmt, name);
29284 /* OpenMP 2.5:
29285 # pragma omp flush flush-vars[opt] new-line
29287 flush-vars:
29288 ( variable-list ) */
29290 static void
29291 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29293 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29294 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29295 cp_parser_require_pragma_eol (parser, pragma_tok);
29297 finish_omp_flush ();
29300 /* Helper function, to parse omp for increment expression. */
29302 static tree
29303 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29305 tree cond = cp_parser_binary_expression (parser, false, true,
29306 PREC_NOT_OPERATOR, NULL);
29307 if (cond == error_mark_node
29308 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29310 cp_parser_skip_to_end_of_statement (parser);
29311 return error_mark_node;
29314 switch (TREE_CODE (cond))
29316 case GT_EXPR:
29317 case GE_EXPR:
29318 case LT_EXPR:
29319 case LE_EXPR:
29320 break;
29321 case NE_EXPR:
29322 if (code == CILK_SIMD || code == CILK_FOR)
29323 break;
29324 /* Fall through: OpenMP disallows NE_EXPR. */
29325 default:
29326 return error_mark_node;
29329 /* If decl is an iterator, preserve LHS and RHS of the relational
29330 expr until finish_omp_for. */
29331 if (decl
29332 && (type_dependent_expression_p (decl)
29333 || CLASS_TYPE_P (TREE_TYPE (decl))))
29334 return cond;
29336 return build_x_binary_op (input_location, TREE_CODE (cond),
29337 TREE_OPERAND (cond, 0), ERROR_MARK,
29338 TREE_OPERAND (cond, 1), ERROR_MARK,
29339 /*overload=*/NULL, tf_warning_or_error);
29342 /* Helper function, to parse omp for increment expression. */
29344 static tree
29345 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29347 cp_token *token = cp_lexer_peek_token (parser->lexer);
29348 enum tree_code op;
29349 tree lhs, rhs;
29350 cp_id_kind idk;
29351 bool decl_first;
29353 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29355 op = (token->type == CPP_PLUS_PLUS
29356 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29357 cp_lexer_consume_token (parser->lexer);
29358 lhs = cp_parser_simple_cast_expression (parser);
29359 if (lhs != decl)
29360 return error_mark_node;
29361 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29364 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29365 if (lhs != decl)
29366 return error_mark_node;
29368 token = cp_lexer_peek_token (parser->lexer);
29369 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29371 op = (token->type == CPP_PLUS_PLUS
29372 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29373 cp_lexer_consume_token (parser->lexer);
29374 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29377 op = cp_parser_assignment_operator_opt (parser);
29378 if (op == ERROR_MARK)
29379 return error_mark_node;
29381 if (op != NOP_EXPR)
29383 rhs = cp_parser_assignment_expression (parser, false, NULL);
29384 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29385 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29388 lhs = cp_parser_binary_expression (parser, false, false,
29389 PREC_ADDITIVE_EXPRESSION, NULL);
29390 token = cp_lexer_peek_token (parser->lexer);
29391 decl_first = lhs == decl;
29392 if (decl_first)
29393 lhs = NULL_TREE;
29394 if (token->type != CPP_PLUS
29395 && token->type != CPP_MINUS)
29396 return error_mark_node;
29400 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29401 cp_lexer_consume_token (parser->lexer);
29402 rhs = cp_parser_binary_expression (parser, false, false,
29403 PREC_ADDITIVE_EXPRESSION, NULL);
29404 token = cp_lexer_peek_token (parser->lexer);
29405 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29407 if (lhs == NULL_TREE)
29409 if (op == PLUS_EXPR)
29410 lhs = rhs;
29411 else
29412 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29413 tf_warning_or_error);
29415 else
29416 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29417 ERROR_MARK, NULL, tf_warning_or_error);
29420 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29422 if (!decl_first)
29424 if (rhs != decl || op == MINUS_EXPR)
29425 return error_mark_node;
29426 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29428 else
29429 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29431 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29434 /* Parse the initialization statement of either an OpenMP for loop or
29435 a Cilk Plus for loop.
29437 Return true if the resulting construct should have an
29438 OMP_CLAUSE_PRIVATE added to it. */
29440 static bool
29441 cp_parser_omp_for_loop_init (cp_parser *parser,
29442 enum tree_code code,
29443 tree &this_pre_body,
29444 vec<tree, va_gc> *for_block,
29445 tree &init,
29446 tree &decl,
29447 tree &real_decl)
29449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29450 return false;
29452 bool add_private_clause = false;
29454 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29456 init-expr:
29457 var = lb
29458 integer-type var = lb
29459 random-access-iterator-type var = lb
29460 pointer-type var = lb
29462 cp_decl_specifier_seq type_specifiers;
29464 /* First, try to parse as an initialized declaration. See
29465 cp_parser_condition, from whence the bulk of this is copied. */
29467 cp_parser_parse_tentatively (parser);
29468 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29469 /*is_trailing_return=*/false,
29470 &type_specifiers);
29471 if (cp_parser_parse_definitely (parser))
29473 /* If parsing a type specifier seq succeeded, then this
29474 MUST be a initialized declaration. */
29475 tree asm_specification, attributes;
29476 cp_declarator *declarator;
29478 declarator = cp_parser_declarator (parser,
29479 CP_PARSER_DECLARATOR_NAMED,
29480 /*ctor_dtor_or_conv_p=*/NULL,
29481 /*parenthesized_p=*/NULL,
29482 /*member_p=*/false,
29483 /*friend_p=*/false);
29484 attributes = cp_parser_attributes_opt (parser);
29485 asm_specification = cp_parser_asm_specification_opt (parser);
29487 if (declarator == cp_error_declarator)
29488 cp_parser_skip_to_end_of_statement (parser);
29490 else
29492 tree pushed_scope, auto_node;
29494 decl = start_decl (declarator, &type_specifiers,
29495 SD_INITIALIZED, attributes,
29496 /*prefix_attributes=*/NULL_TREE,
29497 &pushed_scope);
29499 auto_node = type_uses_auto (TREE_TYPE (decl));
29500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29502 if (cp_lexer_next_token_is (parser->lexer,
29503 CPP_OPEN_PAREN))
29505 if (code != CILK_SIMD && code != CILK_FOR)
29506 error ("parenthesized initialization is not allowed in "
29507 "OpenMP %<for%> loop");
29508 else
29509 error ("parenthesized initialization is "
29510 "not allowed in for-loop");
29512 else
29513 /* Trigger an error. */
29514 cp_parser_require (parser, CPP_EQ, RT_EQ);
29516 init = error_mark_node;
29517 cp_parser_skip_to_end_of_statement (parser);
29519 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29520 || type_dependent_expression_p (decl)
29521 || auto_node)
29523 bool is_direct_init, is_non_constant_init;
29525 init = cp_parser_initializer (parser,
29526 &is_direct_init,
29527 &is_non_constant_init);
29529 if (auto_node)
29531 TREE_TYPE (decl)
29532 = do_auto_deduction (TREE_TYPE (decl), init,
29533 auto_node);
29535 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29536 && !type_dependent_expression_p (decl))
29537 goto non_class;
29540 cp_finish_decl (decl, init, !is_non_constant_init,
29541 asm_specification,
29542 LOOKUP_ONLYCONVERTING);
29543 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29545 vec_safe_push (for_block, this_pre_body);
29546 init = NULL_TREE;
29548 else
29549 init = pop_stmt_list (this_pre_body);
29550 this_pre_body = NULL_TREE;
29552 else
29554 /* Consume '='. */
29555 cp_lexer_consume_token (parser->lexer);
29556 init = cp_parser_assignment_expression (parser, false, NULL);
29558 non_class:
29559 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29560 init = error_mark_node;
29561 else
29562 cp_finish_decl (decl, NULL_TREE,
29563 /*init_const_expr_p=*/false,
29564 asm_specification,
29565 LOOKUP_ONLYCONVERTING);
29568 if (pushed_scope)
29569 pop_scope (pushed_scope);
29572 else
29574 cp_id_kind idk;
29575 /* If parsing a type specifier sequence failed, then
29576 this MUST be a simple expression. */
29577 if (code == CILK_FOR)
29578 error ("%<_Cilk_for%> allows expression instead of declaration only "
29579 "in C, not in C++");
29580 cp_parser_parse_tentatively (parser);
29581 decl = cp_parser_primary_expression (parser, false, false,
29582 false, &idk);
29583 if (!cp_parser_error_occurred (parser)
29584 && decl
29585 && DECL_P (decl)
29586 && CLASS_TYPE_P (TREE_TYPE (decl)))
29588 tree rhs;
29590 cp_parser_parse_definitely (parser);
29591 cp_parser_require (parser, CPP_EQ, RT_EQ);
29592 rhs = cp_parser_assignment_expression (parser, false, NULL);
29593 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29594 decl, NOP_EXPR,
29595 rhs,
29596 tf_warning_or_error));
29597 add_private_clause = true;
29599 else
29601 decl = NULL;
29602 cp_parser_abort_tentative_parse (parser);
29603 init = cp_parser_expression (parser);
29604 if (init)
29606 if (TREE_CODE (init) == MODIFY_EXPR
29607 || TREE_CODE (init) == MODOP_EXPR)
29608 real_decl = TREE_OPERAND (init, 0);
29612 return add_private_clause;
29615 /* Parse the restricted form of the for statement allowed by OpenMP. */
29617 static tree
29618 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29619 tree *cclauses)
29621 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29622 tree real_decl, initv, condv, incrv, declv;
29623 tree this_pre_body, cl;
29624 location_t loc_first;
29625 bool collapse_err = false;
29626 int i, collapse = 1, nbraces = 0;
29627 vec<tree, va_gc> *for_block = make_tree_vector ();
29629 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29630 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29631 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29633 gcc_assert (collapse >= 1);
29635 declv = make_tree_vec (collapse);
29636 initv = make_tree_vec (collapse);
29637 condv = make_tree_vec (collapse);
29638 incrv = make_tree_vec (collapse);
29640 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29642 for (i = 0; i < collapse; i++)
29644 int bracecount = 0;
29645 bool add_private_clause = false;
29646 location_t loc;
29648 if (code != CILK_FOR
29649 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29651 cp_parser_error (parser, "for statement expected");
29652 return NULL;
29654 if (code == CILK_FOR
29655 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29657 cp_parser_error (parser, "_Cilk_for statement expected");
29658 return NULL;
29660 loc = cp_lexer_consume_token (parser->lexer)->location;
29662 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29663 return NULL;
29665 init = decl = real_decl = NULL;
29666 this_pre_body = push_stmt_list ();
29668 add_private_clause
29669 |= cp_parser_omp_for_loop_init (parser, code,
29670 this_pre_body, for_block,
29671 init, decl, real_decl);
29673 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29674 if (this_pre_body)
29676 this_pre_body = pop_stmt_list (this_pre_body);
29677 if (pre_body)
29679 tree t = pre_body;
29680 pre_body = push_stmt_list ();
29681 add_stmt (t);
29682 add_stmt (this_pre_body);
29683 pre_body = pop_stmt_list (pre_body);
29685 else
29686 pre_body = this_pre_body;
29689 if (decl)
29690 real_decl = decl;
29691 if (cclauses != NULL
29692 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29693 && real_decl != NULL_TREE)
29695 tree *c;
29696 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29697 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29698 && OMP_CLAUSE_DECL (*c) == real_decl)
29700 error_at (loc, "iteration variable %qD"
29701 " should not be firstprivate", real_decl);
29702 *c = OMP_CLAUSE_CHAIN (*c);
29704 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29705 && OMP_CLAUSE_DECL (*c) == real_decl)
29707 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29708 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29709 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29710 OMP_CLAUSE_DECL (l) = real_decl;
29711 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29712 if (code == OMP_SIMD)
29714 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29715 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29717 else
29719 OMP_CLAUSE_CHAIN (l) = clauses;
29720 clauses = l;
29722 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29723 CP_OMP_CLAUSE_INFO (*c) = NULL;
29724 add_private_clause = false;
29726 else
29728 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29729 && OMP_CLAUSE_DECL (*c) == real_decl)
29730 add_private_clause = false;
29731 c = &OMP_CLAUSE_CHAIN (*c);
29735 if (add_private_clause)
29737 tree c;
29738 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29740 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29741 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29742 && OMP_CLAUSE_DECL (c) == decl)
29743 break;
29744 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29745 && OMP_CLAUSE_DECL (c) == decl)
29746 error_at (loc, "iteration variable %qD "
29747 "should not be firstprivate",
29748 decl);
29749 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29750 && OMP_CLAUSE_DECL (c) == decl)
29751 error_at (loc, "iteration variable %qD should not be reduction",
29752 decl);
29754 if (c == NULL)
29756 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29757 OMP_CLAUSE_DECL (c) = decl;
29758 c = finish_omp_clauses (c);
29759 if (c)
29761 OMP_CLAUSE_CHAIN (c) = clauses;
29762 clauses = c;
29767 cond = NULL;
29768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29769 cond = cp_parser_omp_for_cond (parser, decl, code);
29770 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29772 incr = NULL;
29773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29775 /* If decl is an iterator, preserve the operator on decl
29776 until finish_omp_for. */
29777 if (real_decl
29778 && ((processing_template_decl
29779 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29780 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29781 incr = cp_parser_omp_for_incr (parser, real_decl);
29782 else
29783 incr = cp_parser_expression (parser);
29784 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29785 SET_EXPR_LOCATION (incr, input_location);
29788 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29789 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29790 /*or_comma=*/false,
29791 /*consume_paren=*/true);
29793 TREE_VEC_ELT (declv, i) = decl;
29794 TREE_VEC_ELT (initv, i) = init;
29795 TREE_VEC_ELT (condv, i) = cond;
29796 TREE_VEC_ELT (incrv, i) = incr;
29798 if (i == collapse - 1)
29799 break;
29801 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29802 in between the collapsed for loops to be still considered perfectly
29803 nested. Hopefully the final version clarifies this.
29804 For now handle (multiple) {'s and empty statements. */
29805 cp_parser_parse_tentatively (parser);
29808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29809 break;
29810 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29812 cp_lexer_consume_token (parser->lexer);
29813 bracecount++;
29815 else if (bracecount
29816 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29817 cp_lexer_consume_token (parser->lexer);
29818 else
29820 loc = cp_lexer_peek_token (parser->lexer)->location;
29821 error_at (loc, "not enough collapsed for loops");
29822 collapse_err = true;
29823 cp_parser_abort_tentative_parse (parser);
29824 declv = NULL_TREE;
29825 break;
29828 while (1);
29830 if (declv)
29832 cp_parser_parse_definitely (parser);
29833 nbraces += bracecount;
29837 /* Note that we saved the original contents of this flag when we entered
29838 the structured block, and so we don't need to re-save it here. */
29839 if (code == CILK_SIMD || code == CILK_FOR)
29840 parser->in_statement = IN_CILK_SIMD_FOR;
29841 else
29842 parser->in_statement = IN_OMP_FOR;
29844 /* Note that the grammar doesn't call for a structured block here,
29845 though the loop as a whole is a structured block. */
29846 body = push_stmt_list ();
29847 cp_parser_statement (parser, NULL_TREE, false, NULL);
29848 body = pop_stmt_list (body);
29850 if (declv == NULL_TREE)
29851 ret = NULL_TREE;
29852 else
29853 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29854 pre_body, clauses);
29856 while (nbraces)
29858 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29860 cp_lexer_consume_token (parser->lexer);
29861 nbraces--;
29863 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29864 cp_lexer_consume_token (parser->lexer);
29865 else
29867 if (!collapse_err)
29869 error_at (cp_lexer_peek_token (parser->lexer)->location,
29870 "collapsed loops not perfectly nested");
29872 collapse_err = true;
29873 cp_parser_statement_seq_opt (parser, NULL);
29874 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29875 break;
29879 while (!for_block->is_empty ())
29880 add_stmt (pop_stmt_list (for_block->pop ()));
29881 release_tree_vector (for_block);
29883 return ret;
29886 /* Helper function for OpenMP parsing, split clauses and call
29887 finish_omp_clauses on each of the set of clauses afterwards. */
29889 static void
29890 cp_omp_split_clauses (location_t loc, enum tree_code code,
29891 omp_clause_mask mask, tree clauses, tree *cclauses)
29893 int i;
29894 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29895 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29896 if (cclauses[i])
29897 cclauses[i] = finish_omp_clauses (cclauses[i]);
29900 /* OpenMP 4.0:
29901 #pragma omp simd simd-clause[optseq] new-line
29902 for-loop */
29904 #define OMP_SIMD_CLAUSE_MASK \
29905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29913 static tree
29914 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29915 char *p_name, omp_clause_mask mask, tree *cclauses)
29917 tree clauses, sb, ret;
29918 unsigned int save;
29919 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29921 strcat (p_name, " simd");
29922 mask |= OMP_SIMD_CLAUSE_MASK;
29923 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29925 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29926 cclauses == NULL);
29927 if (cclauses)
29929 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29930 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29933 sb = begin_omp_structured_block ();
29934 save = cp_parser_begin_omp_structured_block (parser);
29936 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29938 cp_parser_end_omp_structured_block (parser, save);
29939 add_stmt (finish_omp_structured_block (sb));
29941 return ret;
29944 /* OpenMP 2.5:
29945 #pragma omp for for-clause[optseq] new-line
29946 for-loop
29948 OpenMP 4.0:
29949 #pragma omp for simd for-simd-clause[optseq] new-line
29950 for-loop */
29952 #define OMP_FOR_CLAUSE_MASK \
29953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29962 static tree
29963 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29964 char *p_name, omp_clause_mask mask, tree *cclauses)
29966 tree clauses, sb, ret;
29967 unsigned int save;
29968 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29970 strcat (p_name, " for");
29971 mask |= OMP_FOR_CLAUSE_MASK;
29972 if (cclauses)
29973 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29975 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29977 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29978 const char *p = IDENTIFIER_POINTER (id);
29980 if (strcmp (p, "simd") == 0)
29982 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29983 if (cclauses == NULL)
29984 cclauses = cclauses_buf;
29986 cp_lexer_consume_token (parser->lexer);
29987 if (!flag_openmp) /* flag_openmp_simd */
29988 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29989 cclauses);
29990 sb = begin_omp_structured_block ();
29991 save = cp_parser_begin_omp_structured_block (parser);
29992 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29993 cclauses);
29994 cp_parser_end_omp_structured_block (parser, save);
29995 tree body = finish_omp_structured_block (sb);
29996 if (ret == NULL)
29997 return ret;
29998 ret = make_node (OMP_FOR);
29999 TREE_TYPE (ret) = void_type_node;
30000 OMP_FOR_BODY (ret) = body;
30001 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30002 SET_EXPR_LOCATION (ret, loc);
30003 add_stmt (ret);
30004 return ret;
30007 if (!flag_openmp) /* flag_openmp_simd */
30009 cp_parser_require_pragma_eol (parser, pragma_tok);
30010 return NULL_TREE;
30013 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30014 cclauses == NULL);
30015 if (cclauses)
30017 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30018 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30021 sb = begin_omp_structured_block ();
30022 save = cp_parser_begin_omp_structured_block (parser);
30024 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30026 cp_parser_end_omp_structured_block (parser, save);
30027 add_stmt (finish_omp_structured_block (sb));
30029 return ret;
30032 /* OpenMP 2.5:
30033 # pragma omp master new-line
30034 structured-block */
30036 static tree
30037 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30039 cp_parser_require_pragma_eol (parser, pragma_tok);
30040 return c_finish_omp_master (input_location,
30041 cp_parser_omp_structured_block (parser));
30044 /* OpenMP 2.5:
30045 # pragma omp ordered new-line
30046 structured-block */
30048 static tree
30049 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30051 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30052 cp_parser_require_pragma_eol (parser, pragma_tok);
30053 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30056 /* OpenMP 2.5:
30058 section-scope:
30059 { section-sequence }
30061 section-sequence:
30062 section-directive[opt] structured-block
30063 section-sequence section-directive structured-block */
30065 static tree
30066 cp_parser_omp_sections_scope (cp_parser *parser)
30068 tree stmt, substmt;
30069 bool error_suppress = false;
30070 cp_token *tok;
30072 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30073 return NULL_TREE;
30075 stmt = push_stmt_list ();
30077 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30079 substmt = cp_parser_omp_structured_block (parser);
30080 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30081 add_stmt (substmt);
30084 while (1)
30086 tok = cp_lexer_peek_token (parser->lexer);
30087 if (tok->type == CPP_CLOSE_BRACE)
30088 break;
30089 if (tok->type == CPP_EOF)
30090 break;
30092 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30094 cp_lexer_consume_token (parser->lexer);
30095 cp_parser_require_pragma_eol (parser, tok);
30096 error_suppress = false;
30098 else if (!error_suppress)
30100 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30101 error_suppress = true;
30104 substmt = cp_parser_omp_structured_block (parser);
30105 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30106 add_stmt (substmt);
30108 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30110 substmt = pop_stmt_list (stmt);
30112 stmt = make_node (OMP_SECTIONS);
30113 TREE_TYPE (stmt) = void_type_node;
30114 OMP_SECTIONS_BODY (stmt) = substmt;
30116 add_stmt (stmt);
30117 return stmt;
30120 /* OpenMP 2.5:
30121 # pragma omp sections sections-clause[optseq] newline
30122 sections-scope */
30124 #define OMP_SECTIONS_CLAUSE_MASK \
30125 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30131 static tree
30132 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30133 char *p_name, omp_clause_mask mask, tree *cclauses)
30135 tree clauses, ret;
30136 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30138 strcat (p_name, " sections");
30139 mask |= OMP_SECTIONS_CLAUSE_MASK;
30140 if (cclauses)
30141 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30143 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30144 cclauses == NULL);
30145 if (cclauses)
30147 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30148 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30151 ret = cp_parser_omp_sections_scope (parser);
30152 if (ret)
30153 OMP_SECTIONS_CLAUSES (ret) = clauses;
30155 return ret;
30158 /* OpenMP 2.5:
30159 # pragma omp parallel parallel-clause[optseq] new-line
30160 structured-block
30161 # pragma omp parallel for parallel-for-clause[optseq] new-line
30162 structured-block
30163 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30164 structured-block
30166 OpenMP 4.0:
30167 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30168 structured-block */
30170 #define OMP_PARALLEL_CLAUSE_MASK \
30171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30181 static tree
30182 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30183 char *p_name, omp_clause_mask mask, tree *cclauses)
30185 tree stmt, clauses, block;
30186 unsigned int save;
30187 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30189 strcat (p_name, " parallel");
30190 mask |= OMP_PARALLEL_CLAUSE_MASK;
30192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30194 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30195 if (cclauses == NULL)
30196 cclauses = cclauses_buf;
30198 cp_lexer_consume_token (parser->lexer);
30199 if (!flag_openmp) /* flag_openmp_simd */
30200 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30201 block = begin_omp_parallel ();
30202 save = cp_parser_begin_omp_structured_block (parser);
30203 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30204 cp_parser_end_omp_structured_block (parser, save);
30205 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30206 block);
30207 if (ret == NULL_TREE)
30208 return ret;
30209 OMP_PARALLEL_COMBINED (stmt) = 1;
30210 return stmt;
30212 else if (cclauses)
30214 error_at (loc, "expected %<for%> after %qs", p_name);
30215 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30216 return NULL_TREE;
30218 else if (!flag_openmp) /* flag_openmp_simd */
30220 cp_parser_require_pragma_eol (parser, pragma_tok);
30221 return NULL_TREE;
30223 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30226 const char *p = IDENTIFIER_POINTER (id);
30227 if (strcmp (p, "sections") == 0)
30229 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30230 cclauses = cclauses_buf;
30232 cp_lexer_consume_token (parser->lexer);
30233 block = begin_omp_parallel ();
30234 save = cp_parser_begin_omp_structured_block (parser);
30235 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30236 cp_parser_end_omp_structured_block (parser, save);
30237 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30238 block);
30239 OMP_PARALLEL_COMBINED (stmt) = 1;
30240 return stmt;
30244 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30246 block = begin_omp_parallel ();
30247 save = cp_parser_begin_omp_structured_block (parser);
30248 cp_parser_statement (parser, NULL_TREE, false, NULL);
30249 cp_parser_end_omp_structured_block (parser, save);
30250 stmt = finish_omp_parallel (clauses, block);
30251 return stmt;
30254 /* OpenMP 2.5:
30255 # pragma omp single single-clause[optseq] new-line
30256 structured-block */
30258 #define OMP_SINGLE_CLAUSE_MASK \
30259 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30264 static tree
30265 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30267 tree stmt = make_node (OMP_SINGLE);
30268 TREE_TYPE (stmt) = void_type_node;
30270 OMP_SINGLE_CLAUSES (stmt)
30271 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30272 "#pragma omp single", pragma_tok);
30273 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30275 return add_stmt (stmt);
30278 /* OpenMP 3.0:
30279 # pragma omp task task-clause[optseq] new-line
30280 structured-block */
30282 #define OMP_TASK_CLAUSE_MASK \
30283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30293 static tree
30294 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30296 tree clauses, block;
30297 unsigned int save;
30299 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30300 "#pragma omp task", pragma_tok);
30301 block = begin_omp_task ();
30302 save = cp_parser_begin_omp_structured_block (parser);
30303 cp_parser_statement (parser, NULL_TREE, false, NULL);
30304 cp_parser_end_omp_structured_block (parser, save);
30305 return finish_omp_task (clauses, block);
30308 /* OpenMP 3.0:
30309 # pragma omp taskwait new-line */
30311 static void
30312 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30314 cp_parser_require_pragma_eol (parser, pragma_tok);
30315 finish_omp_taskwait ();
30318 /* OpenMP 3.1:
30319 # pragma omp taskyield new-line */
30321 static void
30322 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30324 cp_parser_require_pragma_eol (parser, pragma_tok);
30325 finish_omp_taskyield ();
30328 /* OpenMP 4.0:
30329 # pragma omp taskgroup new-line
30330 structured-block */
30332 static tree
30333 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30335 cp_parser_require_pragma_eol (parser, pragma_tok);
30336 return c_finish_omp_taskgroup (input_location,
30337 cp_parser_omp_structured_block (parser));
30341 /* OpenMP 2.5:
30342 # pragma omp threadprivate (variable-list) */
30344 static void
30345 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30347 tree vars;
30349 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30350 cp_parser_require_pragma_eol (parser, pragma_tok);
30352 finish_omp_threadprivate (vars);
30355 /* OpenMP 4.0:
30356 # pragma omp cancel cancel-clause[optseq] new-line */
30358 #define OMP_CANCEL_CLAUSE_MASK \
30359 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30365 static void
30366 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30368 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30369 "#pragma omp cancel", pragma_tok);
30370 finish_omp_cancel (clauses);
30373 /* OpenMP 4.0:
30374 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30376 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30382 static void
30383 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30385 tree clauses;
30386 bool point_seen = false;
30388 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30390 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30391 const char *p = IDENTIFIER_POINTER (id);
30393 if (strcmp (p, "point") == 0)
30395 cp_lexer_consume_token (parser->lexer);
30396 point_seen = true;
30399 if (!point_seen)
30401 cp_parser_error (parser, "expected %<point%>");
30402 cp_parser_require_pragma_eol (parser, pragma_tok);
30403 return;
30406 clauses = cp_parser_omp_all_clauses (parser,
30407 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30408 "#pragma omp cancellation point",
30409 pragma_tok);
30410 finish_omp_cancellation_point (clauses);
30413 /* OpenMP 4.0:
30414 #pragma omp distribute distribute-clause[optseq] new-line
30415 for-loop */
30417 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30423 static tree
30424 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30425 char *p_name, omp_clause_mask mask, tree *cclauses)
30427 tree clauses, sb, ret;
30428 unsigned int save;
30429 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30431 strcat (p_name, " distribute");
30432 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30434 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30436 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30437 const char *p = IDENTIFIER_POINTER (id);
30438 bool simd = false;
30439 bool parallel = false;
30441 if (strcmp (p, "simd") == 0)
30442 simd = true;
30443 else
30444 parallel = strcmp (p, "parallel") == 0;
30445 if (parallel || simd)
30447 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30448 if (cclauses == NULL)
30449 cclauses = cclauses_buf;
30450 cp_lexer_consume_token (parser->lexer);
30451 if (!flag_openmp) /* flag_openmp_simd */
30453 if (simd)
30454 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30455 cclauses);
30456 else
30457 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30458 cclauses);
30460 sb = begin_omp_structured_block ();
30461 save = cp_parser_begin_omp_structured_block (parser);
30462 if (simd)
30463 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30464 cclauses);
30465 else
30466 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30467 cclauses);
30468 cp_parser_end_omp_structured_block (parser, save);
30469 tree body = finish_omp_structured_block (sb);
30470 if (ret == NULL)
30471 return ret;
30472 ret = make_node (OMP_DISTRIBUTE);
30473 TREE_TYPE (ret) = void_type_node;
30474 OMP_FOR_BODY (ret) = body;
30475 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30476 SET_EXPR_LOCATION (ret, loc);
30477 add_stmt (ret);
30478 return ret;
30481 if (!flag_openmp) /* flag_openmp_simd */
30483 cp_parser_require_pragma_eol (parser, pragma_tok);
30484 return NULL_TREE;
30487 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30488 cclauses == NULL);
30489 if (cclauses)
30491 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30492 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30495 sb = begin_omp_structured_block ();
30496 save = cp_parser_begin_omp_structured_block (parser);
30498 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30500 cp_parser_end_omp_structured_block (parser, save);
30501 add_stmt (finish_omp_structured_block (sb));
30503 return ret;
30506 /* OpenMP 4.0:
30507 # pragma omp teams teams-clause[optseq] new-line
30508 structured-block */
30510 #define OMP_TEAMS_CLAUSE_MASK \
30511 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30519 static tree
30520 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30521 char *p_name, omp_clause_mask mask, tree *cclauses)
30523 tree clauses, sb, ret;
30524 unsigned int save;
30525 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30527 strcat (p_name, " teams");
30528 mask |= OMP_TEAMS_CLAUSE_MASK;
30530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30533 const char *p = IDENTIFIER_POINTER (id);
30534 if (strcmp (p, "distribute") == 0)
30536 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30537 if (cclauses == NULL)
30538 cclauses = cclauses_buf;
30540 cp_lexer_consume_token (parser->lexer);
30541 if (!flag_openmp) /* flag_openmp_simd */
30542 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30543 cclauses);
30544 sb = begin_omp_structured_block ();
30545 save = cp_parser_begin_omp_structured_block (parser);
30546 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30547 cclauses);
30548 cp_parser_end_omp_structured_block (parser, save);
30549 tree body = finish_omp_structured_block (sb);
30550 if (ret == NULL)
30551 return ret;
30552 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30553 ret = make_node (OMP_TEAMS);
30554 TREE_TYPE (ret) = void_type_node;
30555 OMP_TEAMS_CLAUSES (ret) = clauses;
30556 OMP_TEAMS_BODY (ret) = body;
30557 return add_stmt (ret);
30560 if (!flag_openmp) /* flag_openmp_simd */
30562 cp_parser_require_pragma_eol (parser, pragma_tok);
30563 return NULL_TREE;
30566 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30567 cclauses == NULL);
30568 if (cclauses)
30570 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30571 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30574 tree stmt = make_node (OMP_TEAMS);
30575 TREE_TYPE (stmt) = void_type_node;
30576 OMP_TEAMS_CLAUSES (stmt) = clauses;
30577 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30579 return add_stmt (stmt);
30582 /* OpenMP 4.0:
30583 # pragma omp target data target-data-clause[optseq] new-line
30584 structured-block */
30586 #define OMP_TARGET_DATA_CLAUSE_MASK \
30587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30591 static tree
30592 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30594 tree stmt = make_node (OMP_TARGET_DATA);
30595 TREE_TYPE (stmt) = void_type_node;
30597 OMP_TARGET_DATA_CLAUSES (stmt)
30598 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30599 "#pragma omp target data", pragma_tok);
30600 keep_next_level (true);
30601 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30603 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30604 return add_stmt (stmt);
30607 /* OpenMP 4.0:
30608 # pragma omp target update target-update-clause[optseq] new-line */
30610 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30616 static bool
30617 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30618 enum pragma_context context)
30620 if (context == pragma_stmt)
30622 error_at (pragma_tok->location,
30623 "%<#pragma omp target update%> may only be "
30624 "used in compound statements");
30625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30626 return false;
30629 tree clauses
30630 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30631 "#pragma omp target update", pragma_tok);
30632 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30633 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30635 error_at (pragma_tok->location,
30636 "%<#pragma omp target update must contain at least one "
30637 "%<from%> or %<to%> clauses");
30638 return false;
30641 tree stmt = make_node (OMP_TARGET_UPDATE);
30642 TREE_TYPE (stmt) = void_type_node;
30643 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30644 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30645 add_stmt (stmt);
30646 return false;
30649 /* OpenMP 4.0:
30650 # pragma omp target target-clause[optseq] new-line
30651 structured-block */
30653 #define OMP_TARGET_CLAUSE_MASK \
30654 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30658 static bool
30659 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30660 enum pragma_context context)
30662 if (context != pragma_stmt && context != pragma_compound)
30664 cp_parser_error (parser, "expected declaration specifiers");
30665 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30666 return false;
30669 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30671 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30672 const char *p = IDENTIFIER_POINTER (id);
30674 if (strcmp (p, "teams") == 0)
30676 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30677 char p_name[sizeof ("#pragma omp target teams distribute "
30678 "parallel for simd")];
30680 cp_lexer_consume_token (parser->lexer);
30681 strcpy (p_name, "#pragma omp target");
30682 if (!flag_openmp) /* flag_openmp_simd */
30684 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30685 OMP_TARGET_CLAUSE_MASK,
30686 cclauses);
30687 return stmt != NULL_TREE;
30689 keep_next_level (true);
30690 tree sb = begin_omp_structured_block ();
30691 unsigned save = cp_parser_begin_omp_structured_block (parser);
30692 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30693 OMP_TARGET_CLAUSE_MASK, cclauses);
30694 cp_parser_end_omp_structured_block (parser, save);
30695 tree body = finish_omp_structured_block (sb);
30696 if (ret == NULL_TREE)
30697 return false;
30698 tree stmt = make_node (OMP_TARGET);
30699 TREE_TYPE (stmt) = void_type_node;
30700 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30701 OMP_TARGET_BODY (stmt) = body;
30702 add_stmt (stmt);
30703 return true;
30705 else if (!flag_openmp) /* flag_openmp_simd */
30707 cp_parser_require_pragma_eol (parser, pragma_tok);
30708 return false;
30710 else if (strcmp (p, "data") == 0)
30712 cp_lexer_consume_token (parser->lexer);
30713 cp_parser_omp_target_data (parser, pragma_tok);
30714 return true;
30716 else if (strcmp (p, "update") == 0)
30718 cp_lexer_consume_token (parser->lexer);
30719 return cp_parser_omp_target_update (parser, pragma_tok, context);
30723 tree stmt = make_node (OMP_TARGET);
30724 TREE_TYPE (stmt) = void_type_node;
30726 OMP_TARGET_CLAUSES (stmt)
30727 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30728 "#pragma omp target", pragma_tok);
30729 keep_next_level (true);
30730 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30732 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30733 add_stmt (stmt);
30734 return true;
30737 /* OpenMP 4.0:
30738 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30740 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30741 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30748 static void
30749 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30750 enum pragma_context context)
30752 bool first_p = parser->omp_declare_simd == NULL;
30753 cp_omp_declare_simd_data data;
30754 if (first_p)
30756 data.error_seen = false;
30757 data.fndecl_seen = false;
30758 data.tokens = vNULL;
30759 parser->omp_declare_simd = &data;
30761 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30762 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30763 cp_lexer_consume_token (parser->lexer);
30764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30765 parser->omp_declare_simd->error_seen = true;
30766 cp_parser_require_pragma_eol (parser, pragma_tok);
30767 struct cp_token_cache *cp
30768 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30769 parser->omp_declare_simd->tokens.safe_push (cp);
30770 if (first_p)
30772 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30773 cp_parser_pragma (parser, context);
30774 switch (context)
30776 case pragma_external:
30777 cp_parser_declaration (parser);
30778 break;
30779 case pragma_member:
30780 cp_parser_member_declaration (parser);
30781 break;
30782 case pragma_objc_icode:
30783 cp_parser_block_declaration (parser, /*statement_p=*/false);
30784 break;
30785 default:
30786 cp_parser_declaration_statement (parser);
30787 break;
30789 if (parser->omp_declare_simd
30790 && !parser->omp_declare_simd->error_seen
30791 && !parser->omp_declare_simd->fndecl_seen)
30792 error_at (pragma_tok->location,
30793 "%<#pragma omp declare simd%> not immediately followed by "
30794 "function declaration or definition");
30795 data.tokens.release ();
30796 parser->omp_declare_simd = NULL;
30800 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30801 This function is modelled similar to the late parsing of omp declare
30802 simd. */
30804 static tree
30805 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30807 struct cp_token_cache *ce;
30808 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30809 int ii = 0;
30811 if (parser->omp_declare_simd != NULL)
30813 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30814 " marked as a Cilk Plus SIMD-enabled function");
30815 XDELETE (parser->cilk_simd_fn_info);
30816 parser->cilk_simd_fn_info = NULL;
30817 return attrs;
30819 if (!info->error_seen && info->fndecl_seen)
30821 error ("vector attribute not immediately followed by a single function"
30822 " declaration or definition");
30823 info->error_seen = true;
30825 if (info->error_seen)
30826 return attrs;
30828 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30830 tree c, cl;
30832 cp_parser_push_lexer_for_tokens (parser, ce);
30833 parser->lexer->in_pragma = true;
30834 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30835 "SIMD-enabled functions attribute",
30836 NULL);
30837 cp_parser_pop_lexer (parser);
30838 if (cl)
30839 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30841 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30842 TREE_CHAIN (c) = attrs;
30843 attrs = c;
30845 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30846 TREE_CHAIN (c) = attrs;
30847 if (processing_template_decl)
30848 ATTR_IS_DEPENDENT (c) = 1;
30849 attrs = c;
30851 info->fndecl_seen = true;
30852 XDELETE (parser->cilk_simd_fn_info);
30853 parser->cilk_simd_fn_info = NULL;
30854 return attrs;
30857 /* Finalize #pragma omp declare simd clauses after direct declarator has
30858 been parsed, and put that into "omp declare simd" attribute. */
30860 static tree
30861 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30863 struct cp_token_cache *ce;
30864 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30865 int i;
30867 if (!data->error_seen && data->fndecl_seen)
30869 error ("%<#pragma omp declare simd%> not immediately followed by "
30870 "a single function declaration or definition");
30871 data->error_seen = true;
30872 return attrs;
30874 if (data->error_seen)
30875 return attrs;
30877 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30879 tree c, cl;
30881 cp_parser_push_lexer_for_tokens (parser, ce);
30882 parser->lexer->in_pragma = true;
30883 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30884 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30885 cp_lexer_consume_token (parser->lexer);
30886 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30887 "#pragma omp declare simd", pragma_tok);
30888 cp_parser_pop_lexer (parser);
30889 if (cl)
30890 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30891 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30892 TREE_CHAIN (c) = attrs;
30893 if (processing_template_decl)
30894 ATTR_IS_DEPENDENT (c) = 1;
30895 attrs = c;
30898 data->fndecl_seen = true;
30899 return attrs;
30903 /* OpenMP 4.0:
30904 # pragma omp declare target new-line
30905 declarations and definitions
30906 # pragma omp end declare target new-line */
30908 static void
30909 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30911 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30912 scope_chain->omp_declare_target_attribute++;
30915 static void
30916 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
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);
30924 if (strcmp (p, "declare") == 0)
30926 cp_lexer_consume_token (parser->lexer);
30927 p = "";
30928 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30930 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30931 p = IDENTIFIER_POINTER (id);
30933 if (strcmp (p, "target") == 0)
30934 cp_lexer_consume_token (parser->lexer);
30935 else
30937 cp_parser_error (parser, "expected %<target%>");
30938 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30939 return;
30942 else
30944 cp_parser_error (parser, "expected %<declare%>");
30945 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30946 return;
30948 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30949 if (!scope_chain->omp_declare_target_attribute)
30950 error_at (pragma_tok->location,
30951 "%<#pragma omp end declare target%> without corresponding "
30952 "%<#pragma omp declare target%>");
30953 else
30954 scope_chain->omp_declare_target_attribute--;
30957 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30958 expression and optional initializer clause of
30959 #pragma omp declare reduction. We store the expression(s) as
30960 either 3, 6 or 7 special statements inside of the artificial function's
30961 body. The first two statements are DECL_EXPRs for the artificial
30962 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30963 expression that uses those variables.
30964 If there was any INITIALIZER clause, this is followed by further statements,
30965 the fourth and fifth statements are DECL_EXPRs for the artificial
30966 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30967 constructor variant (first token after open paren is not omp_priv),
30968 then the sixth statement is a statement with the function call expression
30969 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30970 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30971 to initialize the OMP_PRIV artificial variable and there is seventh
30972 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30974 static bool
30975 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30977 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30978 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30979 type = TREE_TYPE (type);
30980 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30981 DECL_ARTIFICIAL (omp_out) = 1;
30982 pushdecl (omp_out);
30983 add_decl_expr (omp_out);
30984 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30985 DECL_ARTIFICIAL (omp_in) = 1;
30986 pushdecl (omp_in);
30987 add_decl_expr (omp_in);
30988 tree combiner;
30989 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30991 keep_next_level (true);
30992 tree block = begin_omp_structured_block ();
30993 combiner = cp_parser_expression (parser);
30994 finish_expr_stmt (combiner);
30995 block = finish_omp_structured_block (block);
30996 add_stmt (block);
30998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30999 return false;
31001 const char *p = "";
31002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31004 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31005 p = IDENTIFIER_POINTER (id);
31008 if (strcmp (p, "initializer") == 0)
31010 cp_lexer_consume_token (parser->lexer);
31011 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31012 return false;
31014 p = "";
31015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31017 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31018 p = IDENTIFIER_POINTER (id);
31021 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31022 DECL_ARTIFICIAL (omp_priv) = 1;
31023 pushdecl (omp_priv);
31024 add_decl_expr (omp_priv);
31025 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31026 DECL_ARTIFICIAL (omp_orig) = 1;
31027 pushdecl (omp_orig);
31028 add_decl_expr (omp_orig);
31030 keep_next_level (true);
31031 block = begin_omp_structured_block ();
31033 bool ctor = false;
31034 if (strcmp (p, "omp_priv") == 0)
31036 bool is_direct_init, is_non_constant_init;
31037 ctor = true;
31038 cp_lexer_consume_token (parser->lexer);
31039 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31040 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31041 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31042 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31043 == CPP_CLOSE_PAREN
31044 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31045 == CPP_CLOSE_PAREN))
31047 finish_omp_structured_block (block);
31048 error ("invalid initializer clause");
31049 return false;
31051 initializer = cp_parser_initializer (parser, &is_direct_init,
31052 &is_non_constant_init);
31053 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31054 NULL_TREE, LOOKUP_ONLYCONVERTING);
31056 else
31058 cp_parser_parse_tentatively (parser);
31059 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31060 /*check_dependency_p=*/true,
31061 /*template_p=*/NULL,
31062 /*declarator_p=*/false,
31063 /*optional_p=*/false);
31064 vec<tree, va_gc> *args;
31065 if (fn_name == error_mark_node
31066 || cp_parser_error_occurred (parser)
31067 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31068 || ((args = cp_parser_parenthesized_expression_list
31069 (parser, non_attr, /*cast_p=*/false,
31070 /*allow_expansion_p=*/true,
31071 /*non_constant_p=*/NULL)),
31072 cp_parser_error_occurred (parser)))
31074 finish_omp_structured_block (block);
31075 cp_parser_abort_tentative_parse (parser);
31076 cp_parser_error (parser, "expected id-expression (arguments)");
31077 return false;
31079 unsigned int i;
31080 tree arg;
31081 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31082 if (arg == omp_priv
31083 || (TREE_CODE (arg) == ADDR_EXPR
31084 && TREE_OPERAND (arg, 0) == omp_priv))
31085 break;
31086 cp_parser_abort_tentative_parse (parser);
31087 if (arg == NULL_TREE)
31088 error ("one of the initializer call arguments should be %<omp_priv%>"
31089 " or %<&omp_priv%>");
31090 initializer = cp_parser_postfix_expression (parser, false, false, false,
31091 false, NULL);
31092 finish_expr_stmt (initializer);
31095 block = finish_omp_structured_block (block);
31096 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31097 finish_expr_stmt (block);
31099 if (ctor)
31100 add_decl_expr (omp_orig);
31102 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31103 return false;
31106 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31107 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31109 return true;
31112 /* OpenMP 4.0
31113 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31114 initializer-clause[opt] new-line
31116 initializer-clause:
31117 initializer (omp_priv initializer)
31118 initializer (function-name (argument-list)) */
31120 static void
31121 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31122 enum pragma_context)
31124 auto_vec<tree> types;
31125 enum tree_code reduc_code = ERROR_MARK;
31126 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31127 unsigned int i;
31128 cp_token *first_token;
31129 cp_token_cache *cp;
31130 int errs;
31131 void *p;
31133 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31134 p = obstack_alloc (&declarator_obstack, 0);
31136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31137 goto fail;
31139 switch (cp_lexer_peek_token (parser->lexer)->type)
31141 case CPP_PLUS:
31142 reduc_code = PLUS_EXPR;
31143 break;
31144 case CPP_MULT:
31145 reduc_code = MULT_EXPR;
31146 break;
31147 case CPP_MINUS:
31148 reduc_code = MINUS_EXPR;
31149 break;
31150 case CPP_AND:
31151 reduc_code = BIT_AND_EXPR;
31152 break;
31153 case CPP_XOR:
31154 reduc_code = BIT_XOR_EXPR;
31155 break;
31156 case CPP_OR:
31157 reduc_code = BIT_IOR_EXPR;
31158 break;
31159 case CPP_AND_AND:
31160 reduc_code = TRUTH_ANDIF_EXPR;
31161 break;
31162 case CPP_OR_OR:
31163 reduc_code = TRUTH_ORIF_EXPR;
31164 break;
31165 case CPP_NAME:
31166 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31167 break;
31168 default:
31169 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31170 "%<|%>, %<&&%>, %<||%> or identifier");
31171 goto fail;
31174 if (reduc_code != ERROR_MARK)
31175 cp_lexer_consume_token (parser->lexer);
31177 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31178 if (reduc_id == error_mark_node)
31179 goto fail;
31181 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31182 goto fail;
31184 /* Types may not be defined in declare reduction type list. */
31185 const char *saved_message;
31186 saved_message = parser->type_definition_forbidden_message;
31187 parser->type_definition_forbidden_message
31188 = G_("types may not be defined in declare reduction type list");
31189 bool saved_colon_corrects_to_scope_p;
31190 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31191 parser->colon_corrects_to_scope_p = false;
31192 bool saved_colon_doesnt_start_class_def_p;
31193 saved_colon_doesnt_start_class_def_p
31194 = parser->colon_doesnt_start_class_def_p;
31195 parser->colon_doesnt_start_class_def_p = true;
31197 while (true)
31199 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31200 type = cp_parser_type_id (parser);
31201 if (type == error_mark_node)
31203 else if (ARITHMETIC_TYPE_P (type)
31204 && (orig_reduc_id == NULL_TREE
31205 || (TREE_CODE (type) != COMPLEX_TYPE
31206 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31207 "min") == 0
31208 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31209 "max") == 0))))
31210 error_at (loc, "predeclared arithmetic type %qT in "
31211 "%<#pragma omp declare reduction%>", type);
31212 else if (TREE_CODE (type) == FUNCTION_TYPE
31213 || TREE_CODE (type) == METHOD_TYPE
31214 || TREE_CODE (type) == ARRAY_TYPE)
31215 error_at (loc, "function or array type %qT in "
31216 "%<#pragma omp declare reduction%>", type);
31217 else if (TREE_CODE (type) == REFERENCE_TYPE)
31218 error_at (loc, "reference type %qT in "
31219 "%<#pragma omp declare reduction%>", type);
31220 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31221 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31222 "%<#pragma omp declare reduction%>", type);
31223 else
31224 types.safe_push (type);
31226 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31227 cp_lexer_consume_token (parser->lexer);
31228 else
31229 break;
31232 /* Restore the saved message. */
31233 parser->type_definition_forbidden_message = saved_message;
31234 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31235 parser->colon_doesnt_start_class_def_p
31236 = saved_colon_doesnt_start_class_def_p;
31238 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31239 || types.is_empty ())
31241 fail:
31242 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31243 goto done;
31246 first_token = cp_lexer_peek_token (parser->lexer);
31247 cp = NULL;
31248 errs = errorcount;
31249 FOR_EACH_VEC_ELT (types, i, type)
31251 tree fntype
31252 = build_function_type_list (void_type_node,
31253 cp_build_reference_type (type, false),
31254 NULL_TREE);
31255 tree this_reduc_id = reduc_id;
31256 if (!dependent_type_p (type))
31257 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31258 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31259 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31260 DECL_ARTIFICIAL (fndecl) = 1;
31261 DECL_EXTERNAL (fndecl) = 1;
31262 DECL_DECLARED_INLINE_P (fndecl) = 1;
31263 DECL_IGNORED_P (fndecl) = 1;
31264 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31265 DECL_ATTRIBUTES (fndecl)
31266 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31267 DECL_ATTRIBUTES (fndecl));
31268 if (processing_template_decl)
31269 fndecl = push_template_decl (fndecl);
31270 bool block_scope = false;
31271 tree block = NULL_TREE;
31272 if (current_function_decl)
31274 block_scope = true;
31275 DECL_CONTEXT (fndecl) = global_namespace;
31276 if (!processing_template_decl)
31277 pushdecl (fndecl);
31279 else if (current_class_type)
31281 if (cp == NULL)
31283 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31284 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31285 cp_lexer_consume_token (parser->lexer);
31286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31287 goto fail;
31288 cp = cp_token_cache_new (first_token,
31289 cp_lexer_peek_nth_token (parser->lexer,
31290 2));
31292 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31293 finish_member_declaration (fndecl);
31294 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31295 DECL_PENDING_INLINE_P (fndecl) = 1;
31296 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31297 continue;
31299 else
31301 DECL_CONTEXT (fndecl) = current_namespace;
31302 pushdecl (fndecl);
31304 if (!block_scope)
31305 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31306 else
31307 block = begin_omp_structured_block ();
31308 if (cp)
31310 cp_parser_push_lexer_for_tokens (parser, cp);
31311 parser->lexer->in_pragma = true;
31313 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31315 if (!block_scope)
31316 finish_function (0);
31317 else
31318 DECL_CONTEXT (fndecl) = current_function_decl;
31319 if (cp)
31320 cp_parser_pop_lexer (parser);
31321 goto fail;
31323 if (cp)
31324 cp_parser_pop_lexer (parser);
31325 if (!block_scope)
31326 finish_function (0);
31327 else
31329 DECL_CONTEXT (fndecl) = current_function_decl;
31330 block = finish_omp_structured_block (block);
31331 if (TREE_CODE (block) == BIND_EXPR)
31332 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31333 else if (TREE_CODE (block) == STATEMENT_LIST)
31334 DECL_SAVED_TREE (fndecl) = block;
31335 if (processing_template_decl)
31336 add_decl_expr (fndecl);
31338 cp_check_omp_declare_reduction (fndecl);
31339 if (cp == NULL && types.length () > 1)
31340 cp = cp_token_cache_new (first_token,
31341 cp_lexer_peek_nth_token (parser->lexer, 2));
31342 if (errs != errorcount)
31343 break;
31346 cp_parser_require_pragma_eol (parser, pragma_tok);
31348 done:
31349 /* Free any declarators allocated. */
31350 obstack_free (&declarator_obstack, p);
31353 /* OpenMP 4.0
31354 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31355 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31356 initializer-clause[opt] new-line
31357 #pragma omp declare target new-line */
31359 static void
31360 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31361 enum pragma_context context)
31363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31365 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31366 const char *p = IDENTIFIER_POINTER (id);
31368 if (strcmp (p, "simd") == 0)
31370 cp_lexer_consume_token (parser->lexer);
31371 cp_parser_omp_declare_simd (parser, pragma_tok,
31372 context);
31373 return;
31375 cp_ensure_no_omp_declare_simd (parser);
31376 if (strcmp (p, "reduction") == 0)
31378 cp_lexer_consume_token (parser->lexer);
31379 cp_parser_omp_declare_reduction (parser, pragma_tok,
31380 context);
31381 return;
31383 if (!flag_openmp) /* flag_openmp_simd */
31385 cp_parser_require_pragma_eol (parser, pragma_tok);
31386 return;
31388 if (strcmp (p, "target") == 0)
31390 cp_lexer_consume_token (parser->lexer);
31391 cp_parser_omp_declare_target (parser, pragma_tok);
31392 return;
31395 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31396 "or %<target%>");
31397 cp_parser_require_pragma_eol (parser, pragma_tok);
31400 /* Main entry point to OpenMP statement pragmas. */
31402 static void
31403 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31405 tree stmt;
31406 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31407 omp_clause_mask mask (0);
31409 switch (pragma_tok->pragma_kind)
31411 case PRAGMA_OMP_ATOMIC:
31412 cp_parser_omp_atomic (parser, pragma_tok);
31413 return;
31414 case PRAGMA_OMP_CRITICAL:
31415 stmt = cp_parser_omp_critical (parser, pragma_tok);
31416 break;
31417 case PRAGMA_OMP_DISTRIBUTE:
31418 strcpy (p_name, "#pragma omp");
31419 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31420 break;
31421 case PRAGMA_OMP_FOR:
31422 strcpy (p_name, "#pragma omp");
31423 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31424 break;
31425 case PRAGMA_OMP_MASTER:
31426 stmt = cp_parser_omp_master (parser, pragma_tok);
31427 break;
31428 case PRAGMA_OMP_ORDERED:
31429 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31430 break;
31431 case PRAGMA_OMP_PARALLEL:
31432 strcpy (p_name, "#pragma omp");
31433 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31434 break;
31435 case PRAGMA_OMP_SECTIONS:
31436 strcpy (p_name, "#pragma omp");
31437 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31438 break;
31439 case PRAGMA_OMP_SIMD:
31440 strcpy (p_name, "#pragma omp");
31441 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31442 break;
31443 case PRAGMA_OMP_SINGLE:
31444 stmt = cp_parser_omp_single (parser, pragma_tok);
31445 break;
31446 case PRAGMA_OMP_TASK:
31447 stmt = cp_parser_omp_task (parser, pragma_tok);
31448 break;
31449 case PRAGMA_OMP_TASKGROUP:
31450 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31451 break;
31452 case PRAGMA_OMP_TEAMS:
31453 strcpy (p_name, "#pragma omp");
31454 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31455 break;
31456 default:
31457 gcc_unreachable ();
31460 if (stmt)
31461 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31464 /* Transactional Memory parsing routines. */
31466 /* Parse a transaction attribute.
31468 txn-attribute:
31469 attribute
31470 [ [ identifier ] ]
31472 ??? Simplify this when C++0x bracket attributes are
31473 implemented properly. */
31475 static tree
31476 cp_parser_txn_attribute_opt (cp_parser *parser)
31478 cp_token *token;
31479 tree attr_name, attr = NULL;
31481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31482 return cp_parser_attributes_opt (parser);
31484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31485 return NULL_TREE;
31486 cp_lexer_consume_token (parser->lexer);
31487 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31488 goto error1;
31490 token = cp_lexer_peek_token (parser->lexer);
31491 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31493 token = cp_lexer_consume_token (parser->lexer);
31495 attr_name = (token->type == CPP_KEYWORD
31496 /* For keywords, use the canonical spelling,
31497 not the parsed identifier. */
31498 ? ridpointers[(int) token->keyword]
31499 : token->u.value);
31500 attr = build_tree_list (attr_name, NULL_TREE);
31502 else
31503 cp_parser_error (parser, "expected identifier");
31505 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31506 error1:
31507 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31508 return attr;
31511 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31513 transaction-statement:
31514 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31515 compound-statement
31516 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31519 static tree
31520 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31522 unsigned char old_in = parser->in_transaction;
31523 unsigned char this_in = 1, new_in;
31524 cp_token *token;
31525 tree stmt, attrs, noex;
31527 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31528 || keyword == RID_TRANSACTION_RELAXED);
31529 token = cp_parser_require_keyword (parser, keyword,
31530 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31531 : RT_TRANSACTION_RELAXED));
31532 gcc_assert (token != NULL);
31534 if (keyword == RID_TRANSACTION_RELAXED)
31535 this_in |= TM_STMT_ATTR_RELAXED;
31536 else
31538 attrs = cp_parser_txn_attribute_opt (parser);
31539 if (attrs)
31540 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31543 /* Parse a noexcept specification. */
31544 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31546 /* Keep track if we're in the lexical scope of an outer transaction. */
31547 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31549 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31551 parser->in_transaction = new_in;
31552 cp_parser_compound_statement (parser, NULL, false, false);
31553 parser->in_transaction = old_in;
31555 finish_transaction_stmt (stmt, NULL, this_in, noex);
31557 return stmt;
31560 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31562 transaction-expression:
31563 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31564 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31567 static tree
31568 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31570 unsigned char old_in = parser->in_transaction;
31571 unsigned char this_in = 1;
31572 cp_token *token;
31573 tree expr, noex;
31574 bool noex_expr;
31576 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31577 || keyword == RID_TRANSACTION_RELAXED);
31579 if (!flag_tm)
31580 error (keyword == RID_TRANSACTION_RELAXED
31581 ? G_("%<__transaction_relaxed%> without transactional memory "
31582 "support enabled")
31583 : G_("%<__transaction_atomic%> without transactional memory "
31584 "support enabled"));
31586 token = cp_parser_require_keyword (parser, keyword,
31587 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31588 : RT_TRANSACTION_RELAXED));
31589 gcc_assert (token != NULL);
31591 if (keyword == RID_TRANSACTION_RELAXED)
31592 this_in |= TM_STMT_ATTR_RELAXED;
31594 /* Set this early. This might mean that we allow transaction_cancel in
31595 an expression that we find out later actually has to be a constexpr.
31596 However, we expect that cxx_constant_value will be able to deal with
31597 this; also, if the noexcept has no constexpr, then what we parse next
31598 really is a transaction's body. */
31599 parser->in_transaction = this_in;
31601 /* Parse a noexcept specification. */
31602 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31603 true);
31605 if (!noex || !noex_expr
31606 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31608 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31610 expr = cp_parser_expression (parser);
31611 expr = finish_parenthesized_expr (expr);
31613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31615 else
31617 /* The only expression that is available got parsed for the noexcept
31618 already. noexcept is true then. */
31619 expr = noex;
31620 noex = boolean_true_node;
31623 expr = build_transaction_expr (token->location, expr, this_in, noex);
31624 parser->in_transaction = old_in;
31626 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31627 return error_mark_node;
31629 return (flag_tm ? expr : error_mark_node);
31632 /* Parse a function-transaction-block.
31634 function-transaction-block:
31635 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31636 function-body
31637 __transaction_atomic txn-attribute[opt] function-try-block
31638 __transaction_relaxed ctor-initializer[opt] function-body
31639 __transaction_relaxed function-try-block
31642 static bool
31643 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31645 unsigned char old_in = parser->in_transaction;
31646 unsigned char new_in = 1;
31647 tree compound_stmt, stmt, attrs;
31648 bool ctor_initializer_p;
31649 cp_token *token;
31651 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31652 || keyword == RID_TRANSACTION_RELAXED);
31653 token = cp_parser_require_keyword (parser, keyword,
31654 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31655 : RT_TRANSACTION_RELAXED));
31656 gcc_assert (token != NULL);
31658 if (keyword == RID_TRANSACTION_RELAXED)
31659 new_in |= TM_STMT_ATTR_RELAXED;
31660 else
31662 attrs = cp_parser_txn_attribute_opt (parser);
31663 if (attrs)
31664 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31667 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31669 parser->in_transaction = new_in;
31671 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31672 ctor_initializer_p = cp_parser_function_try_block (parser);
31673 else
31674 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31675 (parser, /*in_function_try_block=*/false);
31677 parser->in_transaction = old_in;
31679 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31681 return ctor_initializer_p;
31684 /* Parse a __transaction_cancel statement.
31686 cancel-statement:
31687 __transaction_cancel txn-attribute[opt] ;
31688 __transaction_cancel txn-attribute[opt] throw-expression ;
31690 ??? Cancel and throw is not yet implemented. */
31692 static tree
31693 cp_parser_transaction_cancel (cp_parser *parser)
31695 cp_token *token;
31696 bool is_outer = false;
31697 tree stmt, attrs;
31699 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31700 RT_TRANSACTION_CANCEL);
31701 gcc_assert (token != NULL);
31703 attrs = cp_parser_txn_attribute_opt (parser);
31704 if (attrs)
31705 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31707 /* ??? Parse cancel-and-throw here. */
31709 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31711 if (!flag_tm)
31713 error_at (token->location, "%<__transaction_cancel%> without "
31714 "transactional memory support enabled");
31715 return error_mark_node;
31717 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31719 error_at (token->location, "%<__transaction_cancel%> within a "
31720 "%<__transaction_relaxed%>");
31721 return error_mark_node;
31723 else if (is_outer)
31725 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31726 && !is_tm_may_cancel_outer (current_function_decl))
31728 error_at (token->location, "outer %<__transaction_cancel%> not "
31729 "within outer %<__transaction_atomic%>");
31730 error_at (token->location,
31731 " or a %<transaction_may_cancel_outer%> function");
31732 return error_mark_node;
31735 else if (parser->in_transaction == 0)
31737 error_at (token->location, "%<__transaction_cancel%> not within "
31738 "%<__transaction_atomic%>");
31739 return error_mark_node;
31742 stmt = build_tm_abort_call (token->location, is_outer);
31743 add_stmt (stmt);
31745 return stmt;
31748 /* The parser. */
31750 static GTY (()) cp_parser *the_parser;
31753 /* Special handling for the first token or line in the file. The first
31754 thing in the file might be #pragma GCC pch_preprocess, which loads a
31755 PCH file, which is a GC collection point. So we need to handle this
31756 first pragma without benefit of an existing lexer structure.
31758 Always returns one token to the caller in *FIRST_TOKEN. This is
31759 either the true first token of the file, or the first token after
31760 the initial pragma. */
31762 static void
31763 cp_parser_initial_pragma (cp_token *first_token)
31765 tree name = NULL;
31767 cp_lexer_get_preprocessor_token (NULL, first_token);
31768 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31769 return;
31771 cp_lexer_get_preprocessor_token (NULL, first_token);
31772 if (first_token->type == CPP_STRING)
31774 name = first_token->u.value;
31776 cp_lexer_get_preprocessor_token (NULL, first_token);
31777 if (first_token->type != CPP_PRAGMA_EOL)
31778 error_at (first_token->location,
31779 "junk at end of %<#pragma GCC pch_preprocess%>");
31781 else
31782 error_at (first_token->location, "expected string literal");
31784 /* Skip to the end of the pragma. */
31785 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31786 cp_lexer_get_preprocessor_token (NULL, first_token);
31788 /* Now actually load the PCH file. */
31789 if (name)
31790 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31792 /* Read one more token to return to our caller. We have to do this
31793 after reading the PCH file in, since its pointers have to be
31794 live. */
31795 cp_lexer_get_preprocessor_token (NULL, first_token);
31798 /* Parses the grainsize pragma for the _Cilk_for statement.
31799 Syntax:
31800 #pragma cilk grainsize = <VALUE>. */
31802 static void
31803 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31805 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31807 tree exp = cp_parser_binary_expression (parser, false, false,
31808 PREC_NOT_OPERATOR, NULL);
31809 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31810 if (!exp || exp == error_mark_node)
31812 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31813 return;
31816 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31818 cp_parser_cilk_for (parser, exp);
31819 else
31820 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31821 "%<#pragma cilk grainsize%> is not followed by "
31822 "%<_Cilk_for%>");
31823 return;
31825 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31828 /* Normal parsing of a pragma token. Here we can (and must) use the
31829 regular lexer. */
31831 static bool
31832 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31834 cp_token *pragma_tok;
31835 unsigned int id;
31837 pragma_tok = cp_lexer_consume_token (parser->lexer);
31838 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31839 parser->lexer->in_pragma = true;
31841 id = pragma_tok->pragma_kind;
31842 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31843 cp_ensure_no_omp_declare_simd (parser);
31844 switch (id)
31846 case PRAGMA_GCC_PCH_PREPROCESS:
31847 error_at (pragma_tok->location,
31848 "%<#pragma GCC pch_preprocess%> must be first");
31849 break;
31851 case PRAGMA_OMP_BARRIER:
31852 switch (context)
31854 case pragma_compound:
31855 cp_parser_omp_barrier (parser, pragma_tok);
31856 return false;
31857 case pragma_stmt:
31858 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31859 "used in compound statements");
31860 break;
31861 default:
31862 goto bad_stmt;
31864 break;
31866 case PRAGMA_OMP_FLUSH:
31867 switch (context)
31869 case pragma_compound:
31870 cp_parser_omp_flush (parser, pragma_tok);
31871 return false;
31872 case pragma_stmt:
31873 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31874 "used in compound statements");
31875 break;
31876 default:
31877 goto bad_stmt;
31879 break;
31881 case PRAGMA_OMP_TASKWAIT:
31882 switch (context)
31884 case pragma_compound:
31885 cp_parser_omp_taskwait (parser, pragma_tok);
31886 return false;
31887 case pragma_stmt:
31888 error_at (pragma_tok->location,
31889 "%<#pragma omp taskwait%> may only be "
31890 "used in compound statements");
31891 break;
31892 default:
31893 goto bad_stmt;
31895 break;
31897 case PRAGMA_OMP_TASKYIELD:
31898 switch (context)
31900 case pragma_compound:
31901 cp_parser_omp_taskyield (parser, pragma_tok);
31902 return false;
31903 case pragma_stmt:
31904 error_at (pragma_tok->location,
31905 "%<#pragma omp taskyield%> may only be "
31906 "used in compound statements");
31907 break;
31908 default:
31909 goto bad_stmt;
31911 break;
31913 case PRAGMA_OMP_CANCEL:
31914 switch (context)
31916 case pragma_compound:
31917 cp_parser_omp_cancel (parser, pragma_tok);
31918 return false;
31919 case pragma_stmt:
31920 error_at (pragma_tok->location,
31921 "%<#pragma omp cancel%> may only be "
31922 "used in compound statements");
31923 break;
31924 default:
31925 goto bad_stmt;
31927 break;
31929 case PRAGMA_OMP_CANCELLATION_POINT:
31930 switch (context)
31932 case pragma_compound:
31933 cp_parser_omp_cancellation_point (parser, pragma_tok);
31934 return false;
31935 case pragma_stmt:
31936 error_at (pragma_tok->location,
31937 "%<#pragma omp cancellation point%> may only be "
31938 "used in compound statements");
31939 break;
31940 default:
31941 goto bad_stmt;
31943 break;
31945 case PRAGMA_OMP_THREADPRIVATE:
31946 cp_parser_omp_threadprivate (parser, pragma_tok);
31947 return false;
31949 case PRAGMA_OMP_DECLARE_REDUCTION:
31950 cp_parser_omp_declare (parser, pragma_tok, context);
31951 return false;
31953 case PRAGMA_OMP_ATOMIC:
31954 case PRAGMA_OMP_CRITICAL:
31955 case PRAGMA_OMP_DISTRIBUTE:
31956 case PRAGMA_OMP_FOR:
31957 case PRAGMA_OMP_MASTER:
31958 case PRAGMA_OMP_ORDERED:
31959 case PRAGMA_OMP_PARALLEL:
31960 case PRAGMA_OMP_SECTIONS:
31961 case PRAGMA_OMP_SIMD:
31962 case PRAGMA_OMP_SINGLE:
31963 case PRAGMA_OMP_TASK:
31964 case PRAGMA_OMP_TASKGROUP:
31965 case PRAGMA_OMP_TEAMS:
31966 if (context != pragma_stmt && context != pragma_compound)
31967 goto bad_stmt;
31968 cp_parser_omp_construct (parser, pragma_tok);
31969 return true;
31971 case PRAGMA_OMP_TARGET:
31972 return cp_parser_omp_target (parser, pragma_tok, context);
31974 case PRAGMA_OMP_END_DECLARE_TARGET:
31975 cp_parser_omp_end_declare_target (parser, pragma_tok);
31976 return false;
31978 case PRAGMA_OMP_SECTION:
31979 error_at (pragma_tok->location,
31980 "%<#pragma omp section%> may only be used in "
31981 "%<#pragma omp sections%> construct");
31982 break;
31984 case PRAGMA_IVDEP:
31986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31987 cp_token *tok;
31988 tok = cp_lexer_peek_token (the_parser->lexer);
31989 if (tok->type != CPP_KEYWORD
31990 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31991 && tok->keyword != RID_DO))
31993 cp_parser_error (parser, "for, while or do statement expected");
31994 return false;
31996 cp_parser_iteration_statement (parser, true);
31997 return true;
32000 case PRAGMA_CILK_SIMD:
32001 if (context == pragma_external)
32003 error_at (pragma_tok->location,
32004 "%<#pragma simd%> must be inside a function");
32005 break;
32007 cp_parser_cilk_simd (parser, pragma_tok);
32008 return true;
32010 case PRAGMA_CILK_GRAINSIZE:
32011 if (context == pragma_external)
32013 error_at (pragma_tok->location,
32014 "%<#pragma cilk grainsize%> must be inside a function");
32015 break;
32018 /* Ignore the pragma if Cilk Plus is not enabled. */
32019 if (flag_cilkplus)
32021 cp_parser_cilk_grainsize (parser, pragma_tok);
32022 return true;
32024 else
32026 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32027 "%<#pragma cilk grainsize%>");
32028 break;
32031 default:
32032 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32033 c_invoke_pragma_handler (id);
32034 break;
32036 bad_stmt:
32037 cp_parser_error (parser, "expected declaration specifiers");
32038 break;
32041 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32042 return false;
32045 /* The interface the pragma parsers have to the lexer. */
32047 enum cpp_ttype
32048 pragma_lex (tree *value)
32050 cp_token *tok;
32051 enum cpp_ttype ret;
32053 tok = cp_lexer_peek_token (the_parser->lexer);
32055 ret = tok->type;
32056 *value = tok->u.value;
32058 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32059 ret = CPP_EOF;
32060 else if (ret == CPP_STRING)
32061 *value = cp_parser_string_literal (the_parser, false, false);
32062 else
32064 cp_lexer_consume_token (the_parser->lexer);
32065 if (ret == CPP_KEYWORD)
32066 ret = CPP_NAME;
32069 return ret;
32073 /* External interface. */
32075 /* Parse one entire translation unit. */
32077 void
32078 c_parse_file (void)
32080 static bool already_called = false;
32082 if (already_called)
32083 fatal_error ("inter-module optimizations not implemented for C++");
32084 already_called = true;
32086 the_parser = cp_parser_new ();
32087 push_deferring_access_checks (flag_access_control
32088 ? dk_no_deferred : dk_no_check);
32089 cp_parser_translation_unit (the_parser);
32090 the_parser = NULL;
32093 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32094 vectorlength clause:
32095 Syntax:
32096 vectorlength ( constant-expression ) */
32098 static tree
32099 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32100 bool is_simd_fn)
32102 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32103 tree expr;
32104 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32105 safelen clause. Thus, vectorlength is represented as OMP 4.0
32106 safelen. For SIMD-enabled function it is represented by OMP 4.0
32107 simdlen. */
32108 if (!is_simd_fn)
32109 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32110 loc);
32111 else
32112 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32113 loc);
32115 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32116 return error_mark_node;
32118 expr = cp_parser_constant_expression (parser, false, NULL);
32119 expr = maybe_constant_value (expr);
32121 /* If expr == error_mark_node, then don't emit any errors nor
32122 create a clause. if any of the above functions returns
32123 error mark node then they would have emitted an error message. */
32124 if (expr == error_mark_node)
32126 else if (!TREE_TYPE (expr)
32127 || !TREE_CONSTANT (expr)
32128 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32129 error_at (loc, "vectorlength must be an integer constant");
32130 else if (TREE_CONSTANT (expr)
32131 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32132 error_at (loc, "vectorlength must be a power of 2");
32133 else
32135 tree c;
32136 if (!is_simd_fn)
32138 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32139 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32140 OMP_CLAUSE_CHAIN (c) = clauses;
32141 clauses = c;
32143 else
32145 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32146 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32147 OMP_CLAUSE_CHAIN (c) = clauses;
32148 clauses = c;
32152 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32153 return error_mark_node;
32154 return clauses;
32157 /* Handles the Cilk Plus #pragma simd linear clause.
32158 Syntax:
32159 linear ( simd-linear-variable-list )
32161 simd-linear-variable-list:
32162 simd-linear-variable
32163 simd-linear-variable-list , simd-linear-variable
32165 simd-linear-variable:
32166 id-expression
32167 id-expression : simd-linear-step
32169 simd-linear-step:
32170 conditional-expression */
32172 static tree
32173 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32175 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32177 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32178 return clauses;
32179 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32181 cp_parser_error (parser, "expected identifier");
32182 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32183 return error_mark_node;
32186 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32187 parser->colon_corrects_to_scope_p = false;
32188 while (1)
32190 cp_token *token = cp_lexer_peek_token (parser->lexer);
32191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32193 cp_parser_error (parser, "expected variable-name");
32194 clauses = error_mark_node;
32195 break;
32198 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32199 false, false);
32200 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32201 token->location);
32202 if (decl == error_mark_node)
32204 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32205 token->location);
32206 clauses = error_mark_node;
32208 else
32210 tree e = NULL_TREE;
32211 tree step_size = integer_one_node;
32213 /* If present, parse the linear step. Otherwise, assume the default
32214 value of 1. */
32215 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32217 cp_lexer_consume_token (parser->lexer);
32219 e = cp_parser_assignment_expression (parser, false, NULL);
32220 e = maybe_constant_value (e);
32222 if (e == error_mark_node)
32224 /* If an error has occurred, then the whole pragma is
32225 considered ill-formed. Thus, no reason to keep
32226 parsing. */
32227 clauses = error_mark_node;
32228 break;
32230 else if (type_dependent_expression_p (e)
32231 || value_dependent_expression_p (e)
32232 || (TREE_TYPE (e)
32233 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32234 && (TREE_CONSTANT (e)
32235 || DECL_P (e))))
32236 step_size = e;
32237 else
32238 cp_parser_error (parser,
32239 "step size must be an integer constant "
32240 "expression or an integer variable");
32243 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32244 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32245 OMP_CLAUSE_DECL (l) = decl;
32246 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32247 OMP_CLAUSE_CHAIN (l) = clauses;
32248 clauses = l;
32250 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32251 cp_lexer_consume_token (parser->lexer);
32252 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32253 break;
32254 else
32256 error_at (cp_lexer_peek_token (parser->lexer)->location,
32257 "expected %<,%> or %<)%> after %qE", decl);
32258 clauses = error_mark_node;
32259 break;
32262 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32263 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32264 return clauses;
32267 /* Returns the name of the next clause. If the clause is not
32268 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32269 token is not consumed. Otherwise, the appropriate enum from the
32270 pragma_simd_clause is returned and the token is consumed. */
32272 static pragma_omp_clause
32273 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32275 pragma_omp_clause clause_type;
32276 cp_token *token = cp_lexer_peek_token (parser->lexer);
32278 if (token->keyword == RID_PRIVATE)
32279 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32280 else if (!token->u.value || token->type != CPP_NAME)
32281 return PRAGMA_CILK_CLAUSE_NONE;
32282 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32283 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32284 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32285 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32286 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32287 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32288 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32289 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32290 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32291 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32292 else
32293 return PRAGMA_CILK_CLAUSE_NONE;
32295 cp_lexer_consume_token (parser->lexer);
32296 return clause_type;
32299 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32301 static tree
32302 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32304 tree clauses = NULL_TREE;
32306 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32307 && clauses != error_mark_node)
32309 pragma_omp_clause c_kind;
32310 c_kind = cp_parser_cilk_simd_clause_name (parser);
32311 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32312 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32313 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32314 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32315 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32316 /* Use the OpenMP 4.0 equivalent function. */
32317 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32318 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32319 /* Use the OpenMP 4.0 equivalent function. */
32320 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32321 clauses);
32322 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32323 /* Use the OMP 4.0 equivalent function. */
32324 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32325 clauses);
32326 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32327 /* Use the OMP 4.0 equivalent function. */
32328 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32329 else
32331 clauses = error_mark_node;
32332 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32333 break;
32337 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32339 if (clauses == error_mark_node)
32340 return error_mark_node;
32341 else
32342 return c_finish_cilk_clauses (clauses);
32345 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32347 static void
32348 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32350 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32352 if (clauses == error_mark_node)
32353 return;
32355 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32357 error_at (cp_lexer_peek_token (parser->lexer)->location,
32358 "for statement expected");
32359 return;
32362 tree sb = begin_omp_structured_block ();
32363 int save = cp_parser_begin_omp_structured_block (parser);
32364 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32365 if (ret)
32366 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32367 cp_parser_end_omp_structured_block (parser, save);
32368 add_stmt (finish_omp_structured_block (sb));
32371 /* Main entry-point for parsing Cilk Plus _Cilk_for
32372 loops. The return value is error_mark_node
32373 when errors happen and CILK_FOR tree on success. */
32375 static tree
32376 cp_parser_cilk_for (cp_parser *parser, tree grain)
32378 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32379 gcc_unreachable ();
32381 tree sb = begin_omp_structured_block ();
32382 int save = cp_parser_begin_omp_structured_block (parser);
32384 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32385 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32386 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32387 clauses = finish_omp_clauses (clauses);
32389 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32390 if (ret)
32391 cpp_validate_cilk_plus_loop (ret);
32392 else
32393 ret = error_mark_node;
32395 cp_parser_end_omp_structured_block (parser, save);
32396 add_stmt (finish_omp_structured_block (sb));
32397 return ret;
32400 /* Create an identifier for a generic parameter type (a synthesized
32401 template parameter implied by `auto' or a concept identifier). */
32403 static GTY(()) int generic_parm_count;
32404 static tree
32405 make_generic_type_name ()
32407 char buf[32];
32408 sprintf (buf, "auto:%d", ++generic_parm_count);
32409 return get_identifier (buf);
32412 /* Predicate that behaves as is_auto_or_concept but matches the parent
32413 node of the generic type rather than the generic type itself. This
32414 allows for type transformation in add_implicit_template_parms. */
32416 static inline bool
32417 tree_type_is_auto_or_concept (const_tree t)
32419 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32422 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32423 (creating a new template parameter list if necessary). Returns the newly
32424 created template type parm. */
32426 tree
32427 synthesize_implicit_template_parm (cp_parser *parser)
32429 gcc_assert (current_binding_level->kind == sk_function_parms);
32431 /* We are either continuing a function template that already contains implicit
32432 template parameters, creating a new fully-implicit function template, or
32433 extending an existing explicit function template with implicit template
32434 parameters. */
32436 cp_binding_level *const entry_scope = current_binding_level;
32438 bool become_template = false;
32439 cp_binding_level *parent_scope = 0;
32441 if (parser->implicit_template_scope)
32443 gcc_assert (parser->implicit_template_parms);
32445 current_binding_level = parser->implicit_template_scope;
32447 else
32449 /* Roll back to the existing template parameter scope (in the case of
32450 extending an explicit function template) or introduce a new template
32451 parameter scope ahead of the function parameter scope (or class scope
32452 in the case of out-of-line member definitions). The function scope is
32453 added back after template parameter synthesis below. */
32455 cp_binding_level *scope = entry_scope;
32457 while (scope->kind == sk_function_parms)
32459 parent_scope = scope;
32460 scope = scope->level_chain;
32462 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32464 /* If not defining a class, then any class scope is a scope level in
32465 an out-of-line member definition. In this case simply wind back
32466 beyond the first such scope to inject the template parameter list.
32467 Otherwise wind back to the class being defined. The latter can
32468 occur in class member friend declarations such as:
32470 class A {
32471 void foo (auto);
32473 class B {
32474 friend void A::foo (auto);
32477 The template parameter list synthesized for the friend declaration
32478 must be injected in the scope of 'B'. This can also occur in
32479 erroneous cases such as:
32481 struct A {
32482 struct B {
32483 void foo (auto);
32485 void B::foo (auto) {}
32488 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32489 but, nevertheless, the template parameter list synthesized for the
32490 declarator should be injected into the scope of 'A' as if the
32491 ill-formed template was specified explicitly. */
32493 while (scope->kind == sk_class && !scope->defining_class_p)
32495 parent_scope = scope;
32496 scope = scope->level_chain;
32500 current_binding_level = scope;
32502 if (scope->kind != sk_template_parms
32503 || !function_being_declared_is_template_p (parser))
32505 /* Introduce a new template parameter list for implicit template
32506 parameters. */
32508 become_template = true;
32510 parser->implicit_template_scope
32511 = begin_scope (sk_template_parms, NULL);
32513 ++processing_template_decl;
32515 parser->fully_implicit_function_template_p = true;
32516 ++parser->num_template_parameter_lists;
32518 else
32520 /* Synthesize implicit template parameters at the end of the explicit
32521 template parameter list. */
32523 gcc_assert (current_template_parms);
32525 parser->implicit_template_scope = scope;
32527 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32528 parser->implicit_template_parms
32529 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32533 /* Synthesize a new template parameter and track the current template
32534 parameter chain with implicit_template_parms. */
32536 tree synth_id = make_generic_type_name ();
32537 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32538 synth_id);
32539 tree new_parm
32540 = process_template_parm (parser->implicit_template_parms,
32541 input_location,
32542 build_tree_list (NULL_TREE, synth_tmpl_parm),
32543 /*non_type=*/false,
32544 /*param_pack=*/false);
32547 if (parser->implicit_template_parms)
32548 parser->implicit_template_parms
32549 = TREE_CHAIN (parser->implicit_template_parms);
32550 else
32551 parser->implicit_template_parms = new_parm;
32553 tree new_type = TREE_TYPE (getdecls ());
32555 /* If creating a fully implicit function template, start the new implicit
32556 template parameter list with this synthesized type, otherwise grow the
32557 current template parameter list. */
32559 if (become_template)
32561 parent_scope->level_chain = current_binding_level;
32563 tree new_parms = make_tree_vec (1);
32564 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32565 current_template_parms = tree_cons (size_int (processing_template_decl),
32566 new_parms, current_template_parms);
32568 else
32570 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32571 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32572 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32573 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32576 current_binding_level = entry_scope;
32578 return new_type;
32581 /* Finish the declaration of a fully implicit function template. Such a
32582 template has no explicit template parameter list so has not been through the
32583 normal template head and tail processing. synthesize_implicit_template_parm
32584 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32585 provided if the declaration is a class member such that its template
32586 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32587 form is returned. Otherwise NULL_TREE is returned. */
32589 tree
32590 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32592 gcc_assert (parser->fully_implicit_function_template_p);
32594 if (member_decl_opt && member_decl_opt != error_mark_node
32595 && DECL_VIRTUAL_P (member_decl_opt))
32597 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32598 "implicit templates may not be %<virtual%>");
32599 DECL_VIRTUAL_P (member_decl_opt) = false;
32602 if (member_decl_opt)
32603 member_decl_opt = finish_member_template_decl (member_decl_opt);
32604 end_template_decl ();
32606 parser->fully_implicit_function_template_p = false;
32607 --parser->num_template_parameter_lists;
32609 return member_decl_opt;
32612 #include "gt-cp-parser.h"