Merge from trunk.
[official-gcc.git] / gcc / cp / parser.c
blobd4f8d13151359213abccc591e7d0c02cd43e2e14
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"
50 namespace {
51 // A helper function. Returns the object pointed to by P
52 // and sets P to NULL.
53 template<typename T>
54 inline T* release(T*& p)
56 T* q = p;
57 p = NULL;
58 return q;
60 } // namespace
63 /* The lexer. */
65 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
66 and c-lex.c) and the C++ parser. */
68 static cp_token eof_token =
70 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
73 /* The various kinds of non integral constant we encounter. */
74 typedef enum non_integral_constant {
75 NIC_NONE,
76 /* floating-point literal */
77 NIC_FLOAT,
78 /* %<this%> */
79 NIC_THIS,
80 /* %<__FUNCTION__%> */
81 NIC_FUNC_NAME,
82 /* %<__PRETTY_FUNCTION__%> */
83 NIC_PRETTY_FUNC,
84 /* %<__func__%> */
85 NIC_C99_FUNC,
86 /* "%<va_arg%> */
87 NIC_VA_ARG,
88 /* a cast */
89 NIC_CAST,
90 /* %<typeid%> operator */
91 NIC_TYPEID,
92 /* non-constant compound literals */
93 NIC_NCC,
94 /* a function call */
95 NIC_FUNC_CALL,
96 /* an increment */
97 NIC_INC,
98 /* an decrement */
99 NIC_DEC,
100 /* an array reference */
101 NIC_ARRAY_REF,
102 /* %<->%> */
103 NIC_ARROW,
104 /* %<.%> */
105 NIC_POINT,
106 /* the address of a label */
107 NIC_ADDR_LABEL,
108 /* %<*%> */
109 NIC_STAR,
110 /* %<&%> */
111 NIC_ADDR,
112 /* %<++%> */
113 NIC_PREINCREMENT,
114 /* %<--%> */
115 NIC_PREDECREMENT,
116 /* %<new%> */
117 NIC_NEW,
118 /* %<delete%> */
119 NIC_DEL,
120 /* calls to overloaded operators */
121 NIC_OVERLOADED,
122 /* an assignment */
123 NIC_ASSIGNMENT,
124 /* a comma operator */
125 NIC_COMMA,
126 /* a call to a constructor */
127 NIC_CONSTRUCTOR,
128 /* a transaction expression */
129 NIC_TRANSACTION
130 } non_integral_constant;
132 /* The various kinds of errors about name-lookup failing. */
133 typedef enum name_lookup_error {
134 /* NULL */
135 NLE_NULL,
136 /* is not a type */
137 NLE_TYPE,
138 /* is not a class or namespace */
139 NLE_CXX98,
140 /* is not a class, namespace, or enumeration */
141 NLE_NOT_CXX98
142 } name_lookup_error;
144 /* The various kinds of required token */
145 typedef enum required_token {
146 RT_NONE,
147 RT_SEMICOLON, /* ';' */
148 RT_OPEN_PAREN, /* '(' */
149 RT_CLOSE_BRACE, /* '}' */
150 RT_OPEN_BRACE, /* '{' */
151 RT_CLOSE_SQUARE, /* ']' */
152 RT_OPEN_SQUARE, /* '[' */
153 RT_COMMA, /* ',' */
154 RT_SCOPE, /* '::' */
155 RT_LESS, /* '<' */
156 RT_GREATER, /* '>' */
157 RT_EQ, /* '=' */
158 RT_ELLIPSIS, /* '...' */
159 RT_MULT, /* '*' */
160 RT_COMPL, /* '~' */
161 RT_COLON, /* ':' */
162 RT_COLON_SCOPE, /* ':' or '::' */
163 RT_CLOSE_PAREN, /* ')' */
164 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
165 RT_PRAGMA_EOL, /* end of line */
166 RT_NAME, /* identifier */
168 /* The type is CPP_KEYWORD */
169 RT_NEW, /* new */
170 RT_DELETE, /* delete */
171 RT_RETURN, /* return */
172 RT_WHILE, /* while */
173 RT_EXTERN, /* extern */
174 RT_STATIC_ASSERT, /* static_assert */
175 RT_DECLTYPE, /* decltype */
176 RT_OPERATOR, /* operator */
177 RT_CLASS, /* class */
178 RT_TEMPLATE, /* template */
179 RT_NAMESPACE, /* namespace */
180 RT_USING, /* using */
181 RT_ASM, /* asm */
182 RT_TRY, /* try */
183 RT_CATCH, /* catch */
184 RT_THROW, /* throw */
185 RT_LABEL, /* __label__ */
186 RT_AT_TRY, /* @try */
187 RT_AT_SYNCHRONIZED, /* @synchronized */
188 RT_AT_THROW, /* @throw */
190 RT_SELECT, /* selection-statement */
191 RT_INTERATION, /* iteration-statement */
192 RT_JUMP, /* jump-statement */
193 RT_CLASS_KEY, /* class-key */
194 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
195 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
196 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
197 RT_TRANSACTION_CANCEL /* __transaction_cancel */
198 } required_token;
200 /* Prototypes. */
202 static cp_lexer *cp_lexer_new_main
203 (void);
204 static cp_lexer *cp_lexer_new_from_tokens
205 (cp_token_cache *tokens);
206 static void cp_lexer_destroy
207 (cp_lexer *);
208 static int cp_lexer_saving_tokens
209 (const cp_lexer *);
210 static cp_token *cp_lexer_token_at
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_get_preprocessor_token
213 (cp_lexer *, cp_token *);
214 static inline cp_token *cp_lexer_peek_token
215 (cp_lexer *);
216 static cp_token *cp_lexer_peek_nth_token
217 (cp_lexer *, size_t);
218 static inline bool cp_lexer_next_token_is
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_not
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_keyword
223 (cp_lexer *, enum rid);
224 static cp_token *cp_lexer_consume_token
225 (cp_lexer *);
226 static void cp_lexer_purge_token
227 (cp_lexer *);
228 static void cp_lexer_purge_tokens_after
229 (cp_lexer *, cp_token_position);
230 static void cp_lexer_save_tokens
231 (cp_lexer *);
232 static void cp_lexer_commit_tokens
233 (cp_lexer *);
234 static void cp_lexer_rollback_tokens
235 (cp_lexer *);
236 static void cp_lexer_print_token
237 (FILE *, cp_token *);
238 static inline bool cp_lexer_debugging_p
239 (cp_lexer *);
240 static void cp_lexer_start_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static void cp_lexer_stop_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static cp_token_cache *cp_token_cache_new
246 (cp_token *, cp_token *);
248 static void cp_parser_initial_pragma
249 (cp_token *);
251 static tree cp_literal_operator_id
252 (const char *);
254 static void cp_parser_cilk_simd
255 (cp_parser *, cp_token *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
261 /* Manifest constants. */
262 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
263 #define CP_SAVED_TOKEN_STACK 5
265 /* Variables. */
267 /* The stream to which debugging output should be written. */
268 static FILE *cp_lexer_debug_stream;
270 /* Nonzero if we are parsing an unevaluated operand: an operand to
271 sizeof, typeof, or alignof. */
272 int cp_unevaluated_operand;
274 /* Dump up to NUM tokens in BUFFER to FILE starting with token
275 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
276 first token in BUFFER. If NUM is 0, dump all the tokens. If
277 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
278 highlighted by surrounding it in [[ ]]. */
280 static void
281 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
282 cp_token *start_token, unsigned num,
283 cp_token *curr_token)
285 unsigned i, nprinted;
286 cp_token *token;
287 bool do_print;
289 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
291 if (buffer == NULL)
292 return;
294 if (num == 0)
295 num = buffer->length ();
297 if (start_token == NULL)
298 start_token = buffer->address ();
300 if (start_token > buffer->address ())
302 cp_lexer_print_token (file, &(*buffer)[0]);
303 fprintf (file, " ... ");
306 do_print = false;
307 nprinted = 0;
308 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
310 if (token == start_token)
311 do_print = true;
313 if (!do_print)
314 continue;
316 nprinted++;
317 if (token == curr_token)
318 fprintf (file, "[[");
320 cp_lexer_print_token (file, token);
322 if (token == curr_token)
323 fprintf (file, "]]");
325 switch (token->type)
327 case CPP_SEMICOLON:
328 case CPP_OPEN_BRACE:
329 case CPP_CLOSE_BRACE:
330 case CPP_EOF:
331 fputc ('\n', file);
332 break;
334 default:
335 fputc (' ', file);
339 if (i == num && i < buffer->length ())
341 fprintf (file, " ... ");
342 cp_lexer_print_token (file, &buffer->last ());
345 fprintf (file, "\n");
349 /* Dump all tokens in BUFFER to stderr. */
351 void
352 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
354 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
357 DEBUG_FUNCTION void
358 debug (vec<cp_token, va_gc> &ref)
360 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
363 DEBUG_FUNCTION void
364 debug (vec<cp_token, va_gc> *ptr)
366 if (ptr)
367 debug (*ptr);
368 else
369 fprintf (stderr, "<nil>\n");
373 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
374 description for T. */
376 static void
377 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
379 if (t)
381 fprintf (file, "%s: ", desc);
382 print_node_brief (file, "", t, 0);
387 /* Dump parser context C to FILE. */
389 static void
390 cp_debug_print_context (FILE *file, cp_parser_context *c)
392 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
393 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
394 print_node_brief (file, "", c->object_type, 0);
395 fprintf (file, "}\n");
399 /* Print the stack of parsing contexts to FILE starting with FIRST. */
401 static void
402 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
404 unsigned i;
405 cp_parser_context *c;
407 fprintf (file, "Parsing context stack:\n");
408 for (i = 0, c = first; c; c = c->next, i++)
410 fprintf (file, "\t#%u: ", i);
411 cp_debug_print_context (file, c);
416 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
418 static void
419 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
421 if (flag)
422 fprintf (file, "%s: true\n", desc);
426 /* Print an unparsed function entry UF to FILE. */
428 static void
429 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
431 unsigned i;
432 cp_default_arg_entry *default_arg_fn;
433 tree fn;
435 fprintf (file, "\tFunctions with default args:\n");
436 for (i = 0;
437 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
438 i++)
440 fprintf (file, "\t\tClass type: ");
441 print_node_brief (file, "", default_arg_fn->class_type, 0);
442 fprintf (file, "\t\tDeclaration: ");
443 print_node_brief (file, "", default_arg_fn->decl, 0);
444 fprintf (file, "\n");
447 fprintf (file, "\n\tFunctions with definitions that require "
448 "post-processing\n\t\t");
449 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
451 print_node_brief (file, "", fn, 0);
452 fprintf (file, " ");
454 fprintf (file, "\n");
456 fprintf (file, "\n\tNon-static data members with initializers that require "
457 "post-processing\n\t\t");
458 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
460 print_node_brief (file, "", fn, 0);
461 fprintf (file, " ");
463 fprintf (file, "\n");
467 /* Print the stack of unparsed member functions S to FILE. */
469 static void
470 cp_debug_print_unparsed_queues (FILE *file,
471 vec<cp_unparsed_functions_entry, va_gc> *s)
473 unsigned i;
474 cp_unparsed_functions_entry *uf;
476 fprintf (file, "Unparsed functions\n");
477 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
479 fprintf (file, "#%u:\n", i);
480 cp_debug_print_unparsed_function (file, uf);
485 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
486 the given PARSER. If FILE is NULL, the output is printed on stderr. */
488 static void
489 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
491 cp_token *next_token, *first_token, *start_token;
493 if (file == NULL)
494 file = stderr;
496 next_token = parser->lexer->next_token;
497 first_token = parser->lexer->buffer->address ();
498 start_token = (next_token > first_token + window_size / 2)
499 ? next_token - window_size / 2
500 : first_token;
501 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
502 next_token);
506 /* Dump debugging information for the given PARSER. If FILE is NULL,
507 the output is printed on stderr. */
509 void
510 cp_debug_parser (FILE *file, cp_parser *parser)
512 const size_t window_size = 20;
513 cp_token *token;
514 expanded_location eloc;
516 if (file == NULL)
517 file = stderr;
519 fprintf (file, "Parser state\n\n");
520 fprintf (file, "Number of tokens: %u\n",
521 vec_safe_length (parser->lexer->buffer));
522 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
523 cp_debug_print_tree_if_set (file, "Object scope",
524 parser->object_scope);
525 cp_debug_print_tree_if_set (file, "Qualifying scope",
526 parser->qualifying_scope);
527 cp_debug_print_context_stack (file, parser->context);
528 cp_debug_print_flag (file, "Allow GNU extensions",
529 parser->allow_gnu_extensions_p);
530 cp_debug_print_flag (file, "'>' token is greater-than",
531 parser->greater_than_is_operator_p);
532 cp_debug_print_flag (file, "Default args allowed in current "
533 "parameter list", parser->default_arg_ok_p);
534 cp_debug_print_flag (file, "Parsing integral constant-expression",
535 parser->integral_constant_expression_p);
536 cp_debug_print_flag (file, "Allow non-constant expression in current "
537 "constant-expression",
538 parser->allow_non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Seen non-constant expression",
540 parser->non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
542 "current context",
543 parser->local_variables_forbidden_p);
544 cp_debug_print_flag (file, "In unbraced linkage specification",
545 parser->in_unbraced_linkage_specification_p);
546 cp_debug_print_flag (file, "Parsing a declarator",
547 parser->in_declarator_p);
548 cp_debug_print_flag (file, "In template argument list",
549 parser->in_template_argument_list_p);
550 cp_debug_print_flag (file, "Parsing an iteration statement",
551 parser->in_statement & IN_ITERATION_STMT);
552 cp_debug_print_flag (file, "Parsing a switch statement",
553 parser->in_statement & IN_SWITCH_STMT);
554 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
555 parser->in_statement & IN_OMP_BLOCK);
556 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
557 parser->in_statement & IN_CILK_SIMD_FOR);
558 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
559 parser->in_statement & IN_OMP_FOR);
560 cp_debug_print_flag (file, "Parsing an if statement",
561 parser->in_statement & IN_IF_STMT);
562 cp_debug_print_flag (file, "Parsing a type-id in an expression "
563 "context", parser->in_type_id_in_expr_p);
564 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
565 parser->implicit_extern_c);
566 cp_debug_print_flag (file, "String expressions should be translated "
567 "to execution character set",
568 parser->translate_strings_p);
569 cp_debug_print_flag (file, "Parsing function body outside of a "
570 "local class", parser->in_function_body);
571 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572 parser->colon_corrects_to_scope_p);
573 cp_debug_print_flag (file, "Colon doesn't start a class definition",
574 parser->colon_doesnt_start_class_def_p);
575 if (parser->type_definition_forbidden_message)
576 fprintf (file, "Error message for forbidden type definitions: %s\n",
577 parser->type_definition_forbidden_message);
578 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
579 fprintf (file, "Number of class definitions in progress: %u\n",
580 parser->num_classes_being_defined);
581 fprintf (file, "Number of template parameter lists for the current "
582 "declaration: %u\n", parser->num_template_parameter_lists);
583 cp_debug_parser_tokens (file, parser, window_size);
584 token = parser->lexer->next_token;
585 fprintf (file, "Next token to parse:\n");
586 fprintf (file, "\tToken: ");
587 cp_lexer_print_token (file, token);
588 eloc = expand_location (token->location);
589 fprintf (file, "\n\tFile: %s\n", eloc.file);
590 fprintf (file, "\tLine: %d\n", eloc.line);
591 fprintf (file, "\tColumn: %d\n", eloc.column);
594 DEBUG_FUNCTION void
595 debug (cp_parser &ref)
597 cp_debug_parser (stderr, &ref);
600 DEBUG_FUNCTION void
601 debug (cp_parser *ptr)
603 if (ptr)
604 debug (*ptr);
605 else
606 fprintf (stderr, "<nil>\n");
609 /* Allocate memory for a new lexer object and return it. */
611 static cp_lexer *
612 cp_lexer_alloc (void)
614 cp_lexer *lexer;
616 c_common_no_more_pch ();
618 /* Allocate the memory. */
619 lexer = ggc_cleared_alloc<cp_lexer> ();
621 /* Initially we are not debugging. */
622 lexer->debugging_p = false;
624 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
626 /* Create the buffer. */
627 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
629 return lexer;
633 /* Create a new main C++ lexer, the lexer that gets tokens from the
634 preprocessor. */
636 static cp_lexer *
637 cp_lexer_new_main (void)
639 cp_lexer *lexer;
640 cp_token token;
642 /* It's possible that parsing the first pragma will load a PCH file,
643 which is a GC collection point. So we have to do that before
644 allocating any memory. */
645 cp_parser_initial_pragma (&token);
647 lexer = cp_lexer_alloc ();
649 /* Put the first token in the buffer. */
650 lexer->buffer->quick_push (token);
652 /* Get the remaining tokens from the preprocessor. */
653 while (token.type != CPP_EOF)
655 cp_lexer_get_preprocessor_token (lexer, &token);
656 vec_safe_push (lexer->buffer, token);
659 lexer->last_token = lexer->buffer->address ()
660 + lexer->buffer->length ()
661 - 1;
662 lexer->next_token = lexer->buffer->length ()
663 ? lexer->buffer->address ()
664 : &eof_token;
666 /* Subsequent preprocessor diagnostics should use compiler
667 diagnostic functions to get the compiler source location. */
668 done_lexing = true;
670 gcc_assert (!lexer->next_token->purged_p);
671 return lexer;
674 /* Create a new lexer whose token stream is primed with the tokens in
675 CACHE. When these tokens are exhausted, no new tokens will be read. */
677 static cp_lexer *
678 cp_lexer_new_from_tokens (cp_token_cache *cache)
680 cp_token *first = cache->first;
681 cp_token *last = cache->last;
682 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
684 /* We do not own the buffer. */
685 lexer->buffer = NULL;
686 lexer->next_token = first == last ? &eof_token : first;
687 lexer->last_token = last;
689 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
691 /* Initially we are not debugging. */
692 lexer->debugging_p = false;
694 gcc_assert (!lexer->next_token->purged_p);
695 return lexer;
698 /* Frees all resources associated with LEXER. */
700 static void
701 cp_lexer_destroy (cp_lexer *lexer)
703 vec_free (lexer->buffer);
704 lexer->saved_tokens.release ();
705 ggc_free (lexer);
708 /* Returns nonzero if debugging information should be output. */
710 static inline bool
711 cp_lexer_debugging_p (cp_lexer *lexer)
713 return lexer->debugging_p;
717 static inline cp_token_position
718 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
720 gcc_assert (!previous_p || lexer->next_token != &eof_token);
722 return lexer->next_token - previous_p;
725 static inline cp_token *
726 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
728 return pos;
731 static inline void
732 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
734 lexer->next_token = cp_lexer_token_at (lexer, pos);
737 static inline cp_token_position
738 cp_lexer_previous_token_position (cp_lexer *lexer)
740 if (lexer->next_token == &eof_token)
741 return lexer->last_token - 1;
742 else
743 return cp_lexer_token_position (lexer, true);
746 static inline cp_token *
747 cp_lexer_previous_token (cp_lexer *lexer)
749 cp_token_position tp = cp_lexer_previous_token_position (lexer);
751 return cp_lexer_token_at (lexer, tp);
754 /* nonzero if we are presently saving tokens. */
756 static inline int
757 cp_lexer_saving_tokens (const cp_lexer* lexer)
759 return lexer->saved_tokens.length () != 0;
762 /* Store the next token from the preprocessor in *TOKEN. Return true
763 if we reach EOF. If LEXER is NULL, assume we are handling an
764 initial #pragma pch_preprocess, and thus want the lexer to return
765 processed strings. */
767 static void
768 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
770 static int is_extern_c = 0;
772 /* Get a new token from the preprocessor. */
773 token->type
774 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
775 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
776 token->keyword = RID_MAX;
777 token->pragma_kind = PRAGMA_NONE;
778 token->purged_p = false;
779 token->error_reported = false;
781 /* On some systems, some header files are surrounded by an
782 implicit extern "C" block. Set a flag in the token if it
783 comes from such a header. */
784 is_extern_c += pending_lang_change;
785 pending_lang_change = 0;
786 token->implicit_extern_c = is_extern_c > 0;
788 /* Check to see if this token is a keyword. */
789 if (token->type == CPP_NAME)
791 if (C_IS_RESERVED_WORD (token->u.value))
793 /* Mark this token as a keyword. */
794 token->type = CPP_KEYWORD;
795 /* Record which keyword. */
796 token->keyword = C_RID_CODE (token->u.value);
798 else
800 if (warn_cxx0x_compat
801 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
802 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
804 /* Warn about the C++0x keyword (but still treat it as
805 an identifier). */
806 warning (OPT_Wc__0x_compat,
807 "identifier %qE is a keyword in C++11",
808 token->u.value);
810 /* Clear out the C_RID_CODE so we don't warn about this
811 particular identifier-turned-keyword again. */
812 C_SET_RID_CODE (token->u.value, RID_MAX);
815 token->keyword = RID_MAX;
818 else if (token->type == CPP_AT_NAME)
820 /* This only happens in Objective-C++; it must be a keyword. */
821 token->type = CPP_KEYWORD;
822 switch (C_RID_CODE (token->u.value))
824 /* Replace 'class' with '@class', 'private' with '@private',
825 etc. This prevents confusion with the C++ keyword
826 'class', and makes the tokens consistent with other
827 Objective-C 'AT' keywords. For example '@class' is
828 reported as RID_AT_CLASS which is consistent with
829 '@synchronized', which is reported as
830 RID_AT_SYNCHRONIZED.
832 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
833 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
834 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
835 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
836 case RID_THROW: token->keyword = RID_AT_THROW; break;
837 case RID_TRY: token->keyword = RID_AT_TRY; break;
838 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
839 default: token->keyword = C_RID_CODE (token->u.value);
842 else if (token->type == CPP_PRAGMA)
844 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
845 token->pragma_kind = ((enum pragma_kind)
846 TREE_INT_CST_LOW (token->u.value));
847 token->u.value = NULL_TREE;
851 /* Update the globals input_location and the input file stack from TOKEN. */
852 static inline void
853 cp_lexer_set_source_position_from_token (cp_token *token)
855 if (token->type != CPP_EOF)
857 input_location = token->location;
861 /* Update the globals input_location and the input file stack from LEXER. */
862 static inline void
863 cp_lexer_set_source_position (cp_lexer *lexer)
865 cp_token *token = cp_lexer_peek_token (lexer);
866 cp_lexer_set_source_position_from_token (token);
869 /* Return a pointer to the next token in the token stream, but do not
870 consume it. */
872 static inline cp_token *
873 cp_lexer_peek_token (cp_lexer *lexer)
875 if (cp_lexer_debugging_p (lexer))
877 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
878 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
879 putc ('\n', cp_lexer_debug_stream);
881 return lexer->next_token;
884 /* Return true if the next token has the indicated TYPE. */
886 static inline bool
887 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
889 return cp_lexer_peek_token (lexer)->type == type;
892 /* Return true if the next token does not have the indicated TYPE. */
894 static inline bool
895 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
897 return !cp_lexer_next_token_is (lexer, type);
900 /* Return true if the next token is the indicated KEYWORD. */
902 static inline bool
903 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
905 return cp_lexer_peek_token (lexer)->keyword == keyword;
908 static inline bool
909 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
911 return cp_lexer_peek_nth_token (lexer, n)->type == type;
914 static inline bool
915 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
917 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
920 /* Return true if the next token is not the indicated KEYWORD. */
922 static inline bool
923 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
925 return cp_lexer_peek_token (lexer)->keyword != keyword;
928 /* Return true if the next token is a keyword for a decl-specifier. */
930 static bool
931 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
933 cp_token *token;
935 token = cp_lexer_peek_token (lexer);
936 switch (token->keyword)
938 /* auto specifier: storage-class-specifier in C++,
939 simple-type-specifier in C++0x. */
940 case RID_AUTO:
941 /* Storage classes. */
942 case RID_REGISTER:
943 case RID_STATIC:
944 case RID_EXTERN:
945 case RID_MUTABLE:
946 case RID_THREAD:
947 /* Elaborated type specifiers. */
948 case RID_ENUM:
949 case RID_CLASS:
950 case RID_STRUCT:
951 case RID_UNION:
952 case RID_TYPENAME:
953 /* Simple type specifiers. */
954 case RID_CHAR:
955 case RID_CHAR16:
956 case RID_CHAR32:
957 case RID_WCHAR:
958 case RID_BOOL:
959 case RID_SHORT:
960 case RID_INT:
961 case RID_LONG:
962 case RID_INT128:
963 case RID_SIGNED:
964 case RID_UNSIGNED:
965 case RID_FLOAT:
966 case RID_DOUBLE:
967 case RID_VOID:
968 /* GNU extensions. */
969 case RID_ATTRIBUTE:
970 case RID_TYPEOF:
971 /* C++0x extensions. */
972 case RID_DECLTYPE:
973 case RID_UNDERLYING_TYPE:
974 return true;
976 default:
977 return false;
981 /* Returns TRUE iff the token T begins a decltype type. */
983 static bool
984 token_is_decltype (cp_token *t)
986 return (t->keyword == RID_DECLTYPE
987 || t->type == CPP_DECLTYPE);
990 /* Returns TRUE iff the next token begins a decltype type. */
992 static bool
993 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
995 cp_token *t = cp_lexer_peek_token (lexer);
996 return token_is_decltype (t);
999 /* Return a pointer to the Nth token in the token stream. If N is 1,
1000 then this is precisely equivalent to cp_lexer_peek_token (except
1001 that it is not inline). One would like to disallow that case, but
1002 there is one case (cp_parser_nth_token_starts_template_id) where
1003 the caller passes a variable for N and it might be 1. */
1005 static cp_token *
1006 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1008 cp_token *token;
1010 /* N is 1-based, not zero-based. */
1011 gcc_assert (n > 0);
1013 if (cp_lexer_debugging_p (lexer))
1014 fprintf (cp_lexer_debug_stream,
1015 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1017 --n;
1018 token = lexer->next_token;
1019 gcc_assert (!n || token != &eof_token);
1020 while (n != 0)
1022 ++token;
1023 if (token == lexer->last_token)
1025 token = &eof_token;
1026 break;
1029 if (!token->purged_p)
1030 --n;
1033 if (cp_lexer_debugging_p (lexer))
1035 cp_lexer_print_token (cp_lexer_debug_stream, token);
1036 putc ('\n', cp_lexer_debug_stream);
1039 return token;
1042 /* Return the next token, and advance the lexer's next_token pointer
1043 to point to the next non-purged token. */
1045 static cp_token *
1046 cp_lexer_consume_token (cp_lexer* lexer)
1048 cp_token *token = lexer->next_token;
1050 gcc_assert (token != &eof_token);
1051 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1055 lexer->next_token++;
1056 if (lexer->next_token == lexer->last_token)
1058 lexer->next_token = &eof_token;
1059 break;
1063 while (lexer->next_token->purged_p);
1065 cp_lexer_set_source_position_from_token (token);
1067 /* Provide debugging output. */
1068 if (cp_lexer_debugging_p (lexer))
1070 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1071 cp_lexer_print_token (cp_lexer_debug_stream, token);
1072 putc ('\n', cp_lexer_debug_stream);
1075 return token;
1078 /* Permanently remove the next token from the token stream, and
1079 advance the next_token pointer to refer to the next non-purged
1080 token. */
1082 static void
1083 cp_lexer_purge_token (cp_lexer *lexer)
1085 cp_token *tok = lexer->next_token;
1087 gcc_assert (tok != &eof_token);
1088 tok->purged_p = true;
1089 tok->location = UNKNOWN_LOCATION;
1090 tok->u.value = NULL_TREE;
1091 tok->keyword = RID_MAX;
1095 tok++;
1096 if (tok == lexer->last_token)
1098 tok = &eof_token;
1099 break;
1102 while (tok->purged_p);
1103 lexer->next_token = tok;
1106 /* Permanently remove all tokens after TOK, up to, but not
1107 including, the token that will be returned next by
1108 cp_lexer_peek_token. */
1110 static void
1111 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1113 cp_token *peek = lexer->next_token;
1115 if (peek == &eof_token)
1116 peek = lexer->last_token;
1118 gcc_assert (tok < peek);
1120 for ( tok += 1; tok != peek; tok += 1)
1122 tok->purged_p = true;
1123 tok->location = UNKNOWN_LOCATION;
1124 tok->u.value = NULL_TREE;
1125 tok->keyword = RID_MAX;
1129 /* Begin saving tokens. All tokens consumed after this point will be
1130 preserved. */
1132 static void
1133 cp_lexer_save_tokens (cp_lexer* lexer)
1135 /* Provide debugging output. */
1136 if (cp_lexer_debugging_p (lexer))
1137 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1139 lexer->saved_tokens.safe_push (lexer->next_token);
1142 /* Commit to the portion of the token stream most recently saved. */
1144 static void
1145 cp_lexer_commit_tokens (cp_lexer* lexer)
1147 /* Provide debugging output. */
1148 if (cp_lexer_debugging_p (lexer))
1149 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1151 lexer->saved_tokens.pop ();
1154 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1155 to the token stream. Stop saving tokens. */
1157 static void
1158 cp_lexer_rollback_tokens (cp_lexer* lexer)
1160 /* Provide debugging output. */
1161 if (cp_lexer_debugging_p (lexer))
1162 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1164 lexer->next_token = lexer->saved_tokens.pop ();
1167 /* Print a representation of the TOKEN on the STREAM. */
1169 static void
1170 cp_lexer_print_token (FILE * stream, cp_token *token)
1172 /* We don't use cpp_type2name here because the parser defines
1173 a few tokens of its own. */
1174 static const char *const token_names[] = {
1175 /* cpplib-defined token types */
1176 #define OP(e, s) #e,
1177 #define TK(e, s) #e,
1178 TTYPE_TABLE
1179 #undef OP
1180 #undef TK
1181 /* C++ parser token types - see "Manifest constants", above. */
1182 "KEYWORD",
1183 "TEMPLATE_ID",
1184 "NESTED_NAME_SPECIFIER",
1187 /* For some tokens, print the associated data. */
1188 switch (token->type)
1190 case CPP_KEYWORD:
1191 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1192 For example, `struct' is mapped to an INTEGER_CST. */
1193 if (!identifier_p (token->u.value))
1194 break;
1195 /* else fall through */
1196 case CPP_NAME:
1197 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1198 break;
1200 case CPP_STRING:
1201 case CPP_STRING16:
1202 case CPP_STRING32:
1203 case CPP_WSTRING:
1204 case CPP_UTF8STRING:
1205 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1206 break;
1208 case CPP_NUMBER:
1209 print_generic_expr (stream, token->u.value, 0);
1210 break;
1212 default:
1213 /* If we have a name for the token, print it out. Otherwise, we
1214 simply give the numeric code. */
1215 if (token->type < ARRAY_SIZE(token_names))
1216 fputs (token_names[token->type], stream);
1217 else
1218 fprintf (stream, "[%d]", token->type);
1219 break;
1223 DEBUG_FUNCTION void
1224 debug (cp_token &ref)
1226 cp_lexer_print_token (stderr, &ref);
1227 fprintf (stderr, "\n");
1230 DEBUG_FUNCTION void
1231 debug (cp_token *ptr)
1233 if (ptr)
1234 debug (*ptr);
1235 else
1236 fprintf (stderr, "<nil>\n");
1240 /* Start emitting debugging information. */
1242 static void
1243 cp_lexer_start_debugging (cp_lexer* lexer)
1245 lexer->debugging_p = true;
1246 cp_lexer_debug_stream = stderr;
1249 /* Stop emitting debugging information. */
1251 static void
1252 cp_lexer_stop_debugging (cp_lexer* lexer)
1254 lexer->debugging_p = false;
1255 cp_lexer_debug_stream = NULL;
1258 /* Create a new cp_token_cache, representing a range of tokens. */
1260 static cp_token_cache *
1261 cp_token_cache_new (cp_token *first, cp_token *last)
1263 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1264 cache->first = first;
1265 cache->last = last;
1266 return cache;
1269 /* Diagnose if #pragma omp declare simd isn't followed immediately
1270 by function declaration or definition. */
1272 static inline void
1273 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1275 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1277 error ("%<#pragma omp declare simd%> not immediately followed by "
1278 "function declaration or definition");
1279 parser->omp_declare_simd = NULL;
1283 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1284 and put that into "omp declare simd" attribute. */
1286 static inline void
1287 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1289 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1291 if (fndecl == error_mark_node)
1293 parser->omp_declare_simd = NULL;
1294 return;
1296 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1298 cp_ensure_no_omp_declare_simd (parser);
1299 return;
1304 /* Decl-specifiers. */
1306 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1308 static void
1309 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1311 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1314 /* Declarators. */
1316 /* Nothing other than the parser should be creating declarators;
1317 declarators are a semi-syntactic representation of C++ entities.
1318 Other parts of the front end that need to create entities (like
1319 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1321 static cp_declarator *make_call_declarator
1322 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1323 static cp_declarator *make_array_declarator
1324 (cp_declarator *, tree);
1325 static cp_declarator *make_pointer_declarator
1326 (cp_cv_quals, cp_declarator *, tree);
1327 static cp_declarator *make_reference_declarator
1328 (cp_cv_quals, cp_declarator *, bool, tree);
1329 static cp_parameter_declarator *make_parameter_declarator
1330 (cp_decl_specifier_seq *, cp_declarator *, tree);
1331 static cp_declarator *make_ptrmem_declarator
1332 (cp_cv_quals, tree, cp_declarator *, tree);
1334 /* An erroneous declarator. */
1335 static cp_declarator *cp_error_declarator;
1337 /* The obstack on which declarators and related data structures are
1338 allocated. */
1339 static struct obstack declarator_obstack;
1341 /* Alloc BYTES from the declarator memory pool. */
1343 static inline void *
1344 alloc_declarator (size_t bytes)
1346 return obstack_alloc (&declarator_obstack, bytes);
1349 /* Allocate a declarator of the indicated KIND. Clear fields that are
1350 common to all declarators. */
1352 static cp_declarator *
1353 make_declarator (cp_declarator_kind kind)
1355 cp_declarator *declarator;
1357 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1358 declarator->kind = kind;
1359 declarator->attributes = NULL_TREE;
1360 declarator->std_attributes = NULL_TREE;
1361 declarator->declarator = NULL;
1362 declarator->parameter_pack_p = false;
1363 declarator->id_loc = UNKNOWN_LOCATION;
1365 return declarator;
1368 /* Make a declarator for a generalized identifier. If
1369 QUALIFYING_SCOPE is non-NULL, the identifier is
1370 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1371 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1372 is, if any. */
1374 static cp_declarator *
1375 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1376 special_function_kind sfk)
1378 cp_declarator *declarator;
1380 /* It is valid to write:
1382 class C { void f(); };
1383 typedef C D;
1384 void D::f();
1386 The standard is not clear about whether `typedef const C D' is
1387 legal; as of 2002-09-15 the committee is considering that
1388 question. EDG 3.0 allows that syntax. Therefore, we do as
1389 well. */
1390 if (qualifying_scope && TYPE_P (qualifying_scope))
1391 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1393 gcc_assert (identifier_p (unqualified_name)
1394 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1395 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1397 declarator = make_declarator (cdk_id);
1398 declarator->u.id.qualifying_scope = qualifying_scope;
1399 declarator->u.id.unqualified_name = unqualified_name;
1400 declarator->u.id.sfk = sfk;
1402 return declarator;
1405 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1406 of modifiers such as const or volatile to apply to the pointer
1407 type, represented as identifiers. ATTRIBUTES represent the attributes that
1408 appertain to the pointer or reference. */
1410 cp_declarator *
1411 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1412 tree attributes)
1414 cp_declarator *declarator;
1416 declarator = make_declarator (cdk_pointer);
1417 declarator->declarator = target;
1418 declarator->u.pointer.qualifiers = cv_qualifiers;
1419 declarator->u.pointer.class_type = NULL_TREE;
1420 if (target)
1422 declarator->id_loc = target->id_loc;
1423 declarator->parameter_pack_p = target->parameter_pack_p;
1424 target->parameter_pack_p = false;
1426 else
1427 declarator->parameter_pack_p = false;
1429 declarator->std_attributes = attributes;
1431 return declarator;
1434 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1435 represent the attributes that appertain to the pointer or
1436 reference. */
1438 cp_declarator *
1439 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1440 bool rvalue_ref, tree attributes)
1442 cp_declarator *declarator;
1444 declarator = make_declarator (cdk_reference);
1445 declarator->declarator = target;
1446 declarator->u.reference.qualifiers = cv_qualifiers;
1447 declarator->u.reference.rvalue_ref = rvalue_ref;
1448 if (target)
1450 declarator->id_loc = target->id_loc;
1451 declarator->parameter_pack_p = target->parameter_pack_p;
1452 target->parameter_pack_p = false;
1454 else
1455 declarator->parameter_pack_p = false;
1457 declarator->std_attributes = attributes;
1459 return declarator;
1462 /* Like make_pointer_declarator -- but for a pointer to a non-static
1463 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1464 appertain to the pointer or reference. */
1466 cp_declarator *
1467 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1468 cp_declarator *pointee,
1469 tree attributes)
1471 cp_declarator *declarator;
1473 declarator = make_declarator (cdk_ptrmem);
1474 declarator->declarator = pointee;
1475 declarator->u.pointer.qualifiers = cv_qualifiers;
1476 declarator->u.pointer.class_type = class_type;
1478 if (pointee)
1480 declarator->parameter_pack_p = pointee->parameter_pack_p;
1481 pointee->parameter_pack_p = false;
1483 else
1484 declarator->parameter_pack_p = false;
1486 declarator->std_attributes = attributes;
1488 return declarator;
1491 /* Make a declarator for the function given by TARGET, with the
1492 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1493 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1494 indicates what exceptions can be thrown. */
1496 cp_declarator *
1497 make_call_declarator (cp_declarator *target,
1498 tree parms,
1499 cp_cv_quals cv_qualifiers,
1500 cp_virt_specifiers virt_specifiers,
1501 cp_ref_qualifier ref_qualifier,
1502 tree exception_specification,
1503 tree late_return_type)
1505 cp_declarator *declarator;
1507 declarator = make_declarator (cdk_function);
1508 declarator->declarator = target;
1509 declarator->u.function.parameters = parms;
1510 declarator->u.function.qualifiers = cv_qualifiers;
1511 declarator->u.function.virt_specifiers = virt_specifiers;
1512 declarator->u.function.ref_qualifier = ref_qualifier;
1513 declarator->u.function.exception_specification = exception_specification;
1514 declarator->u.function.late_return_type = late_return_type;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 return declarator;
1527 /* Make a declarator for an array of BOUNDS elements, each of which is
1528 defined by ELEMENT. */
1530 cp_declarator *
1531 make_array_declarator (cp_declarator *element, tree bounds)
1533 cp_declarator *declarator;
1535 declarator = make_declarator (cdk_array);
1536 declarator->declarator = element;
1537 declarator->u.array.bounds = bounds;
1538 if (element)
1540 declarator->id_loc = element->id_loc;
1541 declarator->parameter_pack_p = element->parameter_pack_p;
1542 element->parameter_pack_p = false;
1544 else
1545 declarator->parameter_pack_p = false;
1547 return declarator;
1550 /* Determine whether the declarator we've seen so far can be a
1551 parameter pack, when followed by an ellipsis. */
1552 static bool
1553 declarator_can_be_parameter_pack (cp_declarator *declarator)
1555 /* Search for a declarator name, or any other declarator that goes
1556 after the point where the ellipsis could appear in a parameter
1557 pack. If we find any of these, then this declarator can not be
1558 made into a parameter pack. */
1559 bool found = false;
1560 while (declarator && !found)
1562 switch ((int)declarator->kind)
1564 case cdk_id:
1565 case cdk_array:
1566 found = true;
1567 break;
1569 case cdk_error:
1570 return true;
1572 default:
1573 declarator = declarator->declarator;
1574 break;
1578 return !found;
1581 cp_parameter_declarator *no_parameters;
1583 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1584 DECLARATOR and DEFAULT_ARGUMENT. */
1586 cp_parameter_declarator *
1587 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1588 cp_declarator *declarator,
1589 tree default_argument)
1591 cp_parameter_declarator *parameter;
1593 parameter = ((cp_parameter_declarator *)
1594 alloc_declarator (sizeof (cp_parameter_declarator)));
1595 parameter->next = NULL;
1596 if (decl_specifiers)
1597 parameter->decl_specifiers = *decl_specifiers;
1598 else
1599 clear_decl_specs (&parameter->decl_specifiers);
1600 parameter->declarator = declarator;
1601 parameter->default_argument = default_argument;
1602 parameter->ellipsis_p = false;
1604 return parameter;
1607 /* Returns true iff DECLARATOR is a declaration for a function. */
1609 static bool
1610 function_declarator_p (const cp_declarator *declarator)
1612 while (declarator)
1614 if (declarator->kind == cdk_function
1615 && declarator->declarator->kind == cdk_id)
1616 return true;
1617 if (declarator->kind == cdk_id
1618 || declarator->kind == cdk_error)
1619 return false;
1620 declarator = declarator->declarator;
1622 return false;
1625 /* The parser. */
1627 /* Overview
1628 --------
1630 A cp_parser parses the token stream as specified by the C++
1631 grammar. Its job is purely parsing, not semantic analysis. For
1632 example, the parser breaks the token stream into declarators,
1633 expressions, statements, and other similar syntactic constructs.
1634 It does not check that the types of the expressions on either side
1635 of an assignment-statement are compatible, or that a function is
1636 not declared with a parameter of type `void'.
1638 The parser invokes routines elsewhere in the compiler to perform
1639 semantic analysis and to build up the abstract syntax tree for the
1640 code processed.
1642 The parser (and the template instantiation code, which is, in a
1643 way, a close relative of parsing) are the only parts of the
1644 compiler that should be calling push_scope and pop_scope, or
1645 related functions. The parser (and template instantiation code)
1646 keeps track of what scope is presently active; everything else
1647 should simply honor that. (The code that generates static
1648 initializers may also need to set the scope, in order to check
1649 access control correctly when emitting the initializers.)
1651 Methodology
1652 -----------
1654 The parser is of the standard recursive-descent variety. Upcoming
1655 tokens in the token stream are examined in order to determine which
1656 production to use when parsing a non-terminal. Some C++ constructs
1657 require arbitrary look ahead to disambiguate. For example, it is
1658 impossible, in the general case, to tell whether a statement is an
1659 expression or declaration without scanning the entire statement.
1660 Therefore, the parser is capable of "parsing tentatively." When the
1661 parser is not sure what construct comes next, it enters this mode.
1662 Then, while we attempt to parse the construct, the parser queues up
1663 error messages, rather than issuing them immediately, and saves the
1664 tokens it consumes. If the construct is parsed successfully, the
1665 parser "commits", i.e., it issues any queued error messages and
1666 the tokens that were being preserved are permanently discarded.
1667 If, however, the construct is not parsed successfully, the parser
1668 rolls back its state completely so that it can resume parsing using
1669 a different alternative.
1671 Future Improvements
1672 -------------------
1674 The performance of the parser could probably be improved substantially.
1675 We could often eliminate the need to parse tentatively by looking ahead
1676 a little bit. In some places, this approach might not entirely eliminate
1677 the need to parse tentatively, but it might still speed up the average
1678 case. */
1680 /* Flags that are passed to some parsing functions. These values can
1681 be bitwise-ored together. */
1683 enum
1685 /* No flags. */
1686 CP_PARSER_FLAGS_NONE = 0x0,
1687 /* The construct is optional. If it is not present, then no error
1688 should be issued. */
1689 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1690 /* When parsing a type-specifier, treat user-defined type-names
1691 as non-type identifiers. */
1692 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1693 /* When parsing a type-specifier, do not try to parse a class-specifier
1694 or enum-specifier. */
1695 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1696 /* When parsing a decl-specifier-seq, only allow type-specifier or
1697 constexpr. */
1698 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1701 /* This type is used for parameters and variables which hold
1702 combinations of the above flags. */
1703 typedef int cp_parser_flags;
1705 /* The different kinds of declarators we want to parse. */
1707 typedef enum cp_parser_declarator_kind
1709 /* We want an abstract declarator. */
1710 CP_PARSER_DECLARATOR_ABSTRACT,
1711 /* We want a named declarator. */
1712 CP_PARSER_DECLARATOR_NAMED,
1713 /* We don't mind, but the name must be an unqualified-id. */
1714 CP_PARSER_DECLARATOR_EITHER
1715 } cp_parser_declarator_kind;
1717 /* The precedence values used to parse binary expressions. The minimum value
1718 of PREC must be 1, because zero is reserved to quickly discriminate
1719 binary operators from other tokens. */
1721 enum cp_parser_prec
1723 PREC_NOT_OPERATOR,
1724 PREC_LOGICAL_OR_EXPRESSION,
1725 PREC_LOGICAL_AND_EXPRESSION,
1726 PREC_INCLUSIVE_OR_EXPRESSION,
1727 PREC_EXCLUSIVE_OR_EXPRESSION,
1728 PREC_AND_EXPRESSION,
1729 PREC_EQUALITY_EXPRESSION,
1730 PREC_RELATIONAL_EXPRESSION,
1731 PREC_SHIFT_EXPRESSION,
1732 PREC_ADDITIVE_EXPRESSION,
1733 PREC_MULTIPLICATIVE_EXPRESSION,
1734 PREC_PM_EXPRESSION,
1735 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1738 /* A mapping from a token type to a corresponding tree node type, with a
1739 precedence value. */
1741 typedef struct cp_parser_binary_operations_map_node
1743 /* The token type. */
1744 enum cpp_ttype token_type;
1745 /* The corresponding tree code. */
1746 enum tree_code tree_type;
1747 /* The precedence of this operator. */
1748 enum cp_parser_prec prec;
1749 } cp_parser_binary_operations_map_node;
1751 typedef struct cp_parser_expression_stack_entry
1753 /* Left hand side of the binary operation we are currently
1754 parsing. */
1755 tree lhs;
1756 /* Original tree code for left hand side, if it was a binary
1757 expression itself (used for -Wparentheses). */
1758 enum tree_code lhs_type;
1759 /* Tree code for the binary operation we are parsing. */
1760 enum tree_code tree_type;
1761 /* Precedence of the binary operation we are parsing. */
1762 enum cp_parser_prec prec;
1763 /* Location of the binary operation we are parsing. */
1764 location_t loc;
1765 } cp_parser_expression_stack_entry;
1767 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1768 entries because precedence levels on the stack are monotonically
1769 increasing. */
1770 typedef struct cp_parser_expression_stack_entry
1771 cp_parser_expression_stack[NUM_PREC_VALUES];
1773 /* Prototypes. */
1775 /* Constructors and destructors. */
1777 static cp_parser_context *cp_parser_context_new
1778 (cp_parser_context *);
1780 /* Class variables. */
1782 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1784 /* The operator-precedence table used by cp_parser_binary_expression.
1785 Transformed into an associative array (binops_by_token) by
1786 cp_parser_new. */
1788 static const cp_parser_binary_operations_map_node binops[] = {
1789 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1790 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1792 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1793 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1794 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1796 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1797 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1799 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1800 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1802 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1803 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1804 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1805 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1807 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1808 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1810 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1812 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1814 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1816 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1818 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1821 /* The same as binops, but initialized by cp_parser_new so that
1822 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1823 for speed. */
1824 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1826 /* Constructors and destructors. */
1828 /* Construct a new context. The context below this one on the stack
1829 is given by NEXT. */
1831 static cp_parser_context *
1832 cp_parser_context_new (cp_parser_context* next)
1834 cp_parser_context *context;
1836 /* Allocate the storage. */
1837 if (cp_parser_context_free_list != NULL)
1839 /* Pull the first entry from the free list. */
1840 context = cp_parser_context_free_list;
1841 cp_parser_context_free_list = context->next;
1842 memset (context, 0, sizeof (*context));
1844 else
1845 context = ggc_cleared_alloc<cp_parser_context> ();
1847 /* No errors have occurred yet in this context. */
1848 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1849 /* If this is not the bottommost context, copy information that we
1850 need from the previous context. */
1851 if (next)
1853 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1854 expression, then we are parsing one in this context, too. */
1855 context->object_type = next->object_type;
1856 /* Thread the stack. */
1857 context->next = next;
1860 return context;
1863 /* Managing the unparsed function queues. */
1865 #define unparsed_funs_with_default_args \
1866 parser->unparsed_queues->last ().funs_with_default_args
1867 #define unparsed_funs_with_definitions \
1868 parser->unparsed_queues->last ().funs_with_definitions
1869 #define unparsed_nsdmis \
1870 parser->unparsed_queues->last ().nsdmis
1871 #define unparsed_classes \
1872 parser->unparsed_queues->last ().classes
1874 static void
1875 push_unparsed_function_queues (cp_parser *parser)
1877 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1878 vec_safe_push (parser->unparsed_queues, e);
1881 static void
1882 pop_unparsed_function_queues (cp_parser *parser)
1884 release_tree_vector (unparsed_funs_with_definitions);
1885 parser->unparsed_queues->pop ();
1888 /* Prototypes. */
1890 /* Constructors and destructors. */
1892 static cp_parser *cp_parser_new
1893 (void);
1895 /* Routines to parse various constructs.
1897 Those that return `tree' will return the error_mark_node (rather
1898 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1899 Sometimes, they will return an ordinary node if error-recovery was
1900 attempted, even though a parse error occurred. So, to check
1901 whether or not a parse error occurred, you should always use
1902 cp_parser_error_occurred. If the construct is optional (indicated
1903 either by an `_opt' in the name of the function that does the
1904 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1905 the construct is not present. */
1907 /* Lexical conventions [gram.lex] */
1909 static tree cp_parser_identifier
1910 (cp_parser *);
1911 static tree cp_parser_string_literal
1912 (cp_parser *, bool, bool);
1913 static tree cp_parser_userdef_char_literal
1914 (cp_parser *);
1915 static tree cp_parser_userdef_string_literal
1916 (cp_token *);
1917 static tree cp_parser_userdef_numeric_literal
1918 (cp_parser *);
1920 /* Basic concepts [gram.basic] */
1922 static bool cp_parser_translation_unit
1923 (cp_parser *);
1925 /* Expressions [gram.expr] */
1927 static tree cp_parser_primary_expression
1928 (cp_parser *, bool, bool, bool, cp_id_kind *);
1929 static tree cp_parser_id_expression
1930 (cp_parser *, bool, bool, bool *, bool, bool);
1931 static tree cp_parser_unqualified_id
1932 (cp_parser *, bool, bool, bool, bool);
1933 static tree cp_parser_nested_name_specifier_opt
1934 (cp_parser *, bool, bool, bool, bool);
1935 static tree cp_parser_nested_name_specifier
1936 (cp_parser *, bool, bool, bool, bool);
1937 static tree cp_parser_qualifying_entity
1938 (cp_parser *, bool, bool, bool, bool, bool);
1939 static tree cp_parser_postfix_expression
1940 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1941 static tree cp_parser_postfix_open_square_expression
1942 (cp_parser *, tree, bool, bool);
1943 static tree cp_parser_postfix_dot_deref_expression
1944 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1945 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1946 (cp_parser *, int, bool, bool, bool *);
1947 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1948 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1949 static void cp_parser_pseudo_destructor_name
1950 (cp_parser *, tree, tree *, tree *);
1951 static tree cp_parser_unary_expression
1952 (cp_parser *, bool, bool, cp_id_kind *);
1953 static enum tree_code cp_parser_unary_operator
1954 (cp_token *);
1955 static tree cp_parser_new_expression
1956 (cp_parser *);
1957 static vec<tree, va_gc> *cp_parser_new_placement
1958 (cp_parser *);
1959 static tree cp_parser_new_type_id
1960 (cp_parser *, tree *);
1961 static cp_declarator *cp_parser_new_declarator_opt
1962 (cp_parser *);
1963 static cp_declarator *cp_parser_direct_new_declarator
1964 (cp_parser *);
1965 static vec<tree, va_gc> *cp_parser_new_initializer
1966 (cp_parser *);
1967 static tree cp_parser_delete_expression
1968 (cp_parser *);
1969 static tree cp_parser_cast_expression
1970 (cp_parser *, bool, bool, bool, cp_id_kind *);
1971 static tree cp_parser_binary_expression
1972 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1973 static tree cp_parser_question_colon_clause
1974 (cp_parser *, tree);
1975 static tree cp_parser_assignment_expression
1976 (cp_parser *, bool, cp_id_kind *);
1977 static enum tree_code cp_parser_assignment_operator_opt
1978 (cp_parser *);
1979 static tree cp_parser_expression
1980 (cp_parser *, bool, cp_id_kind *);
1981 static tree cp_parser_expression
1982 (cp_parser *, bool, bool, cp_id_kind *);
1983 static tree cp_parser_constant_expression
1984 (cp_parser *, bool, bool *);
1985 static tree cp_parser_builtin_offsetof
1986 (cp_parser *);
1987 static tree cp_parser_lambda_expression
1988 (cp_parser *);
1989 static void cp_parser_lambda_introducer
1990 (cp_parser *, tree);
1991 static bool cp_parser_lambda_declarator_opt
1992 (cp_parser *, tree);
1993 static void cp_parser_lambda_body
1994 (cp_parser *, tree);
1996 /* Statements [gram.stmt.stmt] */
1998 static void cp_parser_statement
1999 (cp_parser *, tree, bool, bool *);
2000 static void cp_parser_label_for_labeled_statement
2001 (cp_parser *, tree);
2002 static tree cp_parser_expression_statement
2003 (cp_parser *, tree);
2004 static tree cp_parser_compound_statement
2005 (cp_parser *, tree, bool, bool);
2006 static void cp_parser_statement_seq_opt
2007 (cp_parser *, tree);
2008 static tree cp_parser_selection_statement
2009 (cp_parser *, bool *);
2010 static tree cp_parser_condition
2011 (cp_parser *);
2012 static tree cp_parser_iteration_statement
2013 (cp_parser *, bool);
2014 static bool cp_parser_for_init_statement
2015 (cp_parser *, tree *decl);
2016 static tree cp_parser_for
2017 (cp_parser *, bool);
2018 static tree cp_parser_c_for
2019 (cp_parser *, tree, tree, bool);
2020 static tree cp_parser_range_for
2021 (cp_parser *, tree, tree, tree, bool);
2022 static void do_range_for_auto_deduction
2023 (tree, tree);
2024 static tree cp_parser_perform_range_for_lookup
2025 (tree, tree *, tree *);
2026 static tree cp_parser_range_for_member_function
2027 (tree, tree);
2028 static tree cp_parser_jump_statement
2029 (cp_parser *);
2030 static void cp_parser_declaration_statement
2031 (cp_parser *);
2033 static tree cp_parser_implicitly_scoped_statement
2034 (cp_parser *, bool *);
2035 static void cp_parser_already_scoped_statement
2036 (cp_parser *);
2038 /* Declarations [gram.dcl.dcl] */
2040 static void cp_parser_declaration_seq_opt
2041 (cp_parser *);
2042 static void cp_parser_declaration
2043 (cp_parser *);
2044 static void cp_parser_block_declaration
2045 (cp_parser *, bool);
2046 static void cp_parser_simple_declaration
2047 (cp_parser *, bool, tree *);
2048 static void cp_parser_decl_specifier_seq
2049 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2050 static tree cp_parser_storage_class_specifier_opt
2051 (cp_parser *);
2052 static tree cp_parser_function_specifier_opt
2053 (cp_parser *, cp_decl_specifier_seq *);
2054 static tree cp_parser_type_specifier
2055 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2056 int *, bool *);
2057 static tree cp_parser_simple_type_specifier
2058 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2059 static tree cp_parser_type_name
2060 (cp_parser *);
2061 static tree cp_parser_nonclass_name
2062 (cp_parser* parser);
2063 static tree cp_parser_elaborated_type_specifier
2064 (cp_parser *, bool, bool);
2065 static tree cp_parser_enum_specifier
2066 (cp_parser *);
2067 static void cp_parser_enumerator_list
2068 (cp_parser *, tree);
2069 static void cp_parser_enumerator_definition
2070 (cp_parser *, tree);
2071 static tree cp_parser_namespace_name
2072 (cp_parser *);
2073 static void cp_parser_namespace_definition
2074 (cp_parser *);
2075 static void cp_parser_namespace_body
2076 (cp_parser *);
2077 static tree cp_parser_qualified_namespace_specifier
2078 (cp_parser *);
2079 static void cp_parser_namespace_alias_definition
2080 (cp_parser *);
2081 static bool cp_parser_using_declaration
2082 (cp_parser *, bool);
2083 static void cp_parser_using_directive
2084 (cp_parser *);
2085 static tree cp_parser_alias_declaration
2086 (cp_parser *);
2087 static void cp_parser_asm_definition
2088 (cp_parser *);
2089 static void cp_parser_linkage_specification
2090 (cp_parser *);
2091 static void cp_parser_static_assert
2092 (cp_parser *, bool);
2093 static tree cp_parser_decltype
2094 (cp_parser *);
2096 /* Declarators [gram.dcl.decl] */
2098 static tree cp_parser_init_declarator
2099 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2100 static cp_declarator *cp_parser_declarator
2101 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2102 static cp_declarator *cp_parser_direct_declarator
2103 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2104 static enum tree_code cp_parser_ptr_operator
2105 (cp_parser *, tree *, cp_cv_quals *, tree *);
2106 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2107 (cp_parser *);
2108 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2109 (cp_parser *);
2110 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2111 (cp_parser *);
2112 static tree cp_parser_late_return_type_opt
2113 (cp_parser *, cp_declarator *, cp_cv_quals);
2114 static tree cp_parser_declarator_id
2115 (cp_parser *, bool);
2116 static tree cp_parser_type_id
2117 (cp_parser *);
2118 static tree cp_parser_template_type_arg
2119 (cp_parser *);
2120 static tree cp_parser_trailing_type_id (cp_parser *);
2121 static tree cp_parser_type_id_1
2122 (cp_parser *, bool, bool);
2123 static void cp_parser_type_specifier_seq
2124 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2125 static tree cp_parser_parameter_declaration_clause
2126 (cp_parser *);
2127 static tree cp_parser_parameter_declaration_list
2128 (cp_parser *, bool *);
2129 static cp_parameter_declarator *cp_parser_parameter_declaration
2130 (cp_parser *, bool, bool *);
2131 static tree cp_parser_default_argument
2132 (cp_parser *, bool);
2133 static void cp_parser_function_body
2134 (cp_parser *, bool);
2135 static tree cp_parser_initializer
2136 (cp_parser *, bool *, bool *);
2137 static tree cp_parser_initializer_clause
2138 (cp_parser *, bool *);
2139 static tree cp_parser_braced_list
2140 (cp_parser*, bool*);
2141 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2142 (cp_parser *, bool *);
2144 static bool cp_parser_ctor_initializer_opt_and_function_body
2145 (cp_parser *, bool);
2147 static tree cp_parser_late_parsing_omp_declare_simd
2148 (cp_parser *, tree);
2150 static tree cp_parser_late_parsing_cilk_simd_fn_info
2151 (cp_parser *, tree);
2153 static tree synthesize_implicit_template_parm
2154 (cp_parser *, tree);
2155 static tree finish_fully_implicit_template
2156 (cp_parser *, tree);
2158 /* Classes [gram.class] */
2160 static tree cp_parser_class_name
2161 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2162 static tree cp_parser_class_specifier
2163 (cp_parser *);
2164 static tree cp_parser_class_head
2165 (cp_parser *, bool *);
2166 static enum tag_types cp_parser_class_key
2167 (cp_parser *);
2168 static void cp_parser_member_specification_opt
2169 (cp_parser *);
2170 static void cp_parser_member_declaration
2171 (cp_parser *);
2172 static tree cp_parser_pure_specifier
2173 (cp_parser *);
2174 static tree cp_parser_constant_initializer
2175 (cp_parser *);
2177 /* Derived classes [gram.class.derived] */
2179 static tree cp_parser_base_clause
2180 (cp_parser *);
2181 static tree cp_parser_base_specifier
2182 (cp_parser *);
2184 /* Special member functions [gram.special] */
2186 static tree cp_parser_conversion_function_id
2187 (cp_parser *);
2188 static tree cp_parser_conversion_type_id
2189 (cp_parser *);
2190 static cp_declarator *cp_parser_conversion_declarator_opt
2191 (cp_parser *);
2192 static bool cp_parser_ctor_initializer_opt
2193 (cp_parser *);
2194 static void cp_parser_mem_initializer_list
2195 (cp_parser *);
2196 static tree cp_parser_mem_initializer
2197 (cp_parser *);
2198 static tree cp_parser_mem_initializer_id
2199 (cp_parser *);
2201 /* Overloading [gram.over] */
2203 static tree cp_parser_operator_function_id
2204 (cp_parser *);
2205 static tree cp_parser_operator
2206 (cp_parser *);
2208 /* Templates [gram.temp] */
2210 static void cp_parser_template_declaration
2211 (cp_parser *, bool);
2212 static tree cp_parser_template_parameter_list
2213 (cp_parser *);
2214 static tree cp_parser_template_parameter
2215 (cp_parser *, bool *, bool *);
2216 static tree cp_parser_type_parameter
2217 (cp_parser *, bool *);
2218 static tree cp_parser_template_id
2219 (cp_parser *, bool, bool, enum tag_types, bool);
2220 static tree cp_parser_template_name
2221 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2222 static tree cp_parser_template_argument_list
2223 (cp_parser *);
2224 static tree cp_parser_template_argument
2225 (cp_parser *);
2226 static void cp_parser_explicit_instantiation
2227 (cp_parser *);
2228 static void cp_parser_explicit_specialization
2229 (cp_parser *);
2231 /* Exception handling [gram.exception] */
2233 static tree cp_parser_try_block
2234 (cp_parser *);
2235 static bool cp_parser_function_try_block
2236 (cp_parser *);
2237 static void cp_parser_handler_seq
2238 (cp_parser *);
2239 static void cp_parser_handler
2240 (cp_parser *);
2241 static tree cp_parser_exception_declaration
2242 (cp_parser *);
2243 static tree cp_parser_throw_expression
2244 (cp_parser *);
2245 static tree cp_parser_exception_specification_opt
2246 (cp_parser *);
2247 static tree cp_parser_type_id_list
2248 (cp_parser *);
2250 /* GNU Extensions */
2252 static tree cp_parser_asm_specification_opt
2253 (cp_parser *);
2254 static tree cp_parser_asm_operand_list
2255 (cp_parser *);
2256 static tree cp_parser_asm_clobber_list
2257 (cp_parser *);
2258 static tree cp_parser_asm_label_list
2259 (cp_parser *);
2260 static bool cp_next_tokens_can_be_attribute_p
2261 (cp_parser *);
2262 static bool cp_next_tokens_can_be_gnu_attribute_p
2263 (cp_parser *);
2264 static bool cp_next_tokens_can_be_std_attribute_p
2265 (cp_parser *);
2266 static bool cp_nth_tokens_can_be_std_attribute_p
2267 (cp_parser *, size_t);
2268 static bool cp_nth_tokens_can_be_gnu_attribute_p
2269 (cp_parser *, size_t);
2270 static bool cp_nth_tokens_can_be_attribute_p
2271 (cp_parser *, size_t);
2272 static tree cp_parser_attributes_opt
2273 (cp_parser *);
2274 static tree cp_parser_gnu_attributes_opt
2275 (cp_parser *);
2276 static tree cp_parser_gnu_attribute_list
2277 (cp_parser *);
2278 static tree cp_parser_std_attribute
2279 (cp_parser *);
2280 static tree cp_parser_std_attribute_spec
2281 (cp_parser *);
2282 static tree cp_parser_std_attribute_spec_seq
2283 (cp_parser *);
2284 static bool cp_parser_extension_opt
2285 (cp_parser *, int *);
2286 static void cp_parser_label_declaration
2287 (cp_parser *);
2289 /* Concept Extensions */
2291 static tree cp_parser_requires_clause
2292 (cp_parser *);
2293 static tree cp_parser_requires_clause_opt
2294 (cp_parser *);
2295 static tree cp_parser_requires_expression
2296 (cp_parser *);
2297 static tree cp_parser_requirement_parameter_list
2298 (cp_parser *);
2299 static tree cp_parser_requirement_body
2300 (cp_parser *);
2301 static tree cp_parser_requirement_list
2302 (cp_parser *);
2303 static tree cp_parser_requirement
2304 (cp_parser *);
2305 static tree cp_parser_simple_requirement
2306 (cp_parser *);
2307 static tree cp_parser_compound_requirement
2308 (cp_parser *);
2309 static tree cp_parser_type_requirement
2310 (cp_parser *);
2311 static tree cp_parser_nested_requirement
2312 (cp_parser *);
2313 static tree cp_parser_constexpr_constraint_spec
2314 (cp_parser *, tree);
2315 static tree cp_parser_noexcept_constraint_spec
2316 (cp_parser *, tree);
2317 static tree cp_parser_constraint_spec
2318 (cp_parser *, tree);
2319 static tree cp_parser_constraint_specifier_seq
2320 (cp_parser *, tree);
2322 /* Transactional Memory Extensions */
2324 static tree cp_parser_transaction
2325 (cp_parser *, enum rid);
2326 static tree cp_parser_transaction_expression
2327 (cp_parser *, enum rid);
2328 static bool cp_parser_function_transaction
2329 (cp_parser *, enum rid);
2330 static tree cp_parser_transaction_cancel
2331 (cp_parser *);
2333 enum pragma_context {
2334 pragma_external,
2335 pragma_member,
2336 pragma_objc_icode,
2337 pragma_stmt,
2338 pragma_compound
2340 static bool cp_parser_pragma
2341 (cp_parser *, enum pragma_context);
2343 /* Objective-C++ Productions */
2345 static tree cp_parser_objc_message_receiver
2346 (cp_parser *);
2347 static tree cp_parser_objc_message_args
2348 (cp_parser *);
2349 static tree cp_parser_objc_message_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_encode_expression
2352 (cp_parser *);
2353 static tree cp_parser_objc_defs_expression
2354 (cp_parser *);
2355 static tree cp_parser_objc_protocol_expression
2356 (cp_parser *);
2357 static tree cp_parser_objc_selector_expression
2358 (cp_parser *);
2359 static tree cp_parser_objc_expression
2360 (cp_parser *);
2361 static bool cp_parser_objc_selector_p
2362 (enum cpp_ttype);
2363 static tree cp_parser_objc_selector
2364 (cp_parser *);
2365 static tree cp_parser_objc_protocol_refs_opt
2366 (cp_parser *);
2367 static void cp_parser_objc_declaration
2368 (cp_parser *, tree);
2369 static tree cp_parser_objc_statement
2370 (cp_parser *);
2371 static bool cp_parser_objc_valid_prefix_attributes
2372 (cp_parser *, tree *);
2373 static void cp_parser_objc_at_property_declaration
2374 (cp_parser *) ;
2375 static void cp_parser_objc_at_synthesize_declaration
2376 (cp_parser *) ;
2377 static void cp_parser_objc_at_dynamic_declaration
2378 (cp_parser *) ;
2379 static tree cp_parser_objc_struct_declaration
2380 (cp_parser *) ;
2382 /* Utility Routines */
2384 static tree cp_parser_lookup_name
2385 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2386 static tree cp_parser_lookup_name_simple
2387 (cp_parser *, tree, location_t);
2388 static tree cp_parser_maybe_treat_template_as_class
2389 (tree, bool);
2390 static bool cp_parser_check_declarator_template_parameters
2391 (cp_parser *, cp_declarator *, location_t);
2392 static bool cp_parser_check_template_parameters
2393 (cp_parser *, unsigned, location_t, cp_declarator *);
2394 static tree cp_parser_simple_cast_expression
2395 (cp_parser *);
2396 static tree cp_parser_global_scope_opt
2397 (cp_parser *, bool);
2398 static bool cp_parser_constructor_declarator_p
2399 (cp_parser *, bool);
2400 static tree cp_parser_function_definition_from_specifiers_and_declarator
2401 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2402 static tree cp_parser_function_definition_after_declarator
2403 (cp_parser *, bool);
2404 static void cp_parser_template_declaration_after_export
2405 (cp_parser *, bool);
2406 static void cp_parser_perform_template_parameter_access_checks
2407 (vec<deferred_access_check, va_gc> *);
2408 static tree cp_parser_single_declaration
2409 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2410 static tree cp_parser_functional_cast
2411 (cp_parser *, tree);
2412 static tree cp_parser_save_member_function_body
2413 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2414 static tree cp_parser_save_nsdmi
2415 (cp_parser *);
2416 static tree cp_parser_enclosed_template_argument_list
2417 (cp_parser *);
2418 static void cp_parser_save_default_args
2419 (cp_parser *, tree);
2420 static void cp_parser_late_parsing_for_member
2421 (cp_parser *, tree);
2422 static tree cp_parser_late_parse_one_default_arg
2423 (cp_parser *, tree, tree, tree);
2424 static void cp_parser_late_parsing_nsdmi
2425 (cp_parser *, tree);
2426 static void cp_parser_late_parsing_default_args
2427 (cp_parser *, tree);
2428 static tree cp_parser_sizeof_operand
2429 (cp_parser *, enum rid);
2430 static tree cp_parser_trait_expr
2431 (cp_parser *, enum rid);
2432 static bool cp_parser_declares_only_class_p
2433 (cp_parser *);
2434 static void cp_parser_set_storage_class
2435 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2436 static void cp_parser_set_decl_spec_type
2437 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2438 static void set_and_check_decl_spec_loc
2439 (cp_decl_specifier_seq *decl_specs,
2440 cp_decl_spec ds, cp_token *);
2441 static bool cp_parser_friend_p
2442 (const cp_decl_specifier_seq *);
2443 static void cp_parser_required_error
2444 (cp_parser *, required_token, bool);
2445 static cp_token *cp_parser_require
2446 (cp_parser *, enum cpp_ttype, required_token);
2447 static cp_token *cp_parser_require_keyword
2448 (cp_parser *, enum rid, required_token);
2449 static bool cp_parser_token_starts_function_definition_p
2450 (cp_token *);
2451 static bool cp_parser_next_token_starts_class_definition_p
2452 (cp_parser *);
2453 static bool cp_parser_next_token_ends_template_argument_p
2454 (cp_parser *);
2455 static bool cp_parser_nth_token_starts_template_argument_list_p
2456 (cp_parser *, size_t);
2457 static enum tag_types cp_parser_token_is_class_key
2458 (cp_token *);
2459 static void cp_parser_check_class_key
2460 (enum tag_types, tree type);
2461 static void cp_parser_check_access_in_redeclaration
2462 (tree type, location_t location);
2463 static bool cp_parser_optional_template_keyword
2464 (cp_parser *);
2465 static void cp_parser_pre_parsed_nested_name_specifier
2466 (cp_parser *);
2467 static bool cp_parser_cache_group
2468 (cp_parser *, enum cpp_ttype, unsigned);
2469 static tree cp_parser_cache_defarg
2470 (cp_parser *parser, bool nsdmi);
2471 static void cp_parser_parse_tentatively
2472 (cp_parser *);
2473 static void cp_parser_commit_to_tentative_parse
2474 (cp_parser *);
2475 static void cp_parser_commit_to_topmost_tentative_parse
2476 (cp_parser *);
2477 static void cp_parser_abort_tentative_parse
2478 (cp_parser *);
2479 static bool cp_parser_parse_definitely
2480 (cp_parser *);
2481 static inline bool cp_parser_parsing_tentatively
2482 (cp_parser *);
2483 static bool cp_parser_uncommitted_to_tentative_parse_p
2484 (cp_parser *);
2485 static void cp_parser_error
2486 (cp_parser *, const char *);
2487 static void cp_parser_name_lookup_error
2488 (cp_parser *, tree, tree, name_lookup_error, location_t);
2489 static bool cp_parser_simulate_error
2490 (cp_parser *);
2491 static bool cp_parser_check_type_definition
2492 (cp_parser *);
2493 static void cp_parser_check_for_definition_in_return_type
2494 (cp_declarator *, tree, location_t type_location);
2495 static void cp_parser_check_for_invalid_template_id
2496 (cp_parser *, tree, enum tag_types, location_t location);
2497 static bool cp_parser_non_integral_constant_expression
2498 (cp_parser *, non_integral_constant);
2499 static void cp_parser_diagnose_invalid_type_name
2500 (cp_parser *, tree, tree, location_t);
2501 static bool cp_parser_parse_and_diagnose_invalid_type_name
2502 (cp_parser *);
2503 static int cp_parser_skip_to_closing_parenthesis
2504 (cp_parser *, bool, bool, bool);
2505 static void cp_parser_skip_to_end_of_statement
2506 (cp_parser *);
2507 static void cp_parser_consume_semicolon_at_end_of_statement
2508 (cp_parser *);
2509 static void cp_parser_skip_to_end_of_block_or_statement
2510 (cp_parser *);
2511 static bool cp_parser_skip_to_closing_brace
2512 (cp_parser *);
2513 static void cp_parser_skip_to_end_of_template_parameter_list
2514 (cp_parser *);
2515 static void cp_parser_skip_to_pragma_eol
2516 (cp_parser*, cp_token *);
2517 static bool cp_parser_error_occurred
2518 (cp_parser *);
2519 static bool cp_parser_allow_gnu_extensions_p
2520 (cp_parser *);
2521 static bool cp_parser_is_pure_string_literal
2522 (cp_token *);
2523 static bool cp_parser_is_string_literal
2524 (cp_token *);
2525 static bool cp_parser_is_keyword
2526 (cp_token *, enum rid);
2527 static tree cp_parser_make_typename_type
2528 (cp_parser *, tree, tree, location_t location);
2529 static cp_declarator * cp_parser_make_indirect_declarator
2530 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2531 static bool cp_parser_compound_literal_p
2532 (cp_parser *);
2534 /* Concept-related syntactic transformations */
2536 static tree cp_maybe_concept_name (cp_parser *, tree);
2537 static tree cp_maybe_partial_concept_id (cp_parser *, tree, tree);
2539 // -------------------------------------------------------------------------- //
2540 // Unevaluated Operand Guard
2542 // Implementation of an RAII helper for unevaluated operand parsing.
2543 cp_unevaluated::cp_unevaluated ()
2545 ++cp_unevaluated_operand;
2546 ++c_inhibit_evaluation_warnings;
2549 cp_unevaluated::~cp_unevaluated ()
2551 --c_inhibit_evaluation_warnings;
2552 --cp_unevaluated_operand;
2555 // -------------------------------------------------------------------------- //
2556 // Tentative Parsing
2558 /* Returns nonzero if we are parsing tentatively. */
2560 static inline bool
2561 cp_parser_parsing_tentatively (cp_parser* parser)
2563 return parser->context->next != NULL;
2566 /* Returns nonzero if TOKEN is a string literal. */
2568 static bool
2569 cp_parser_is_pure_string_literal (cp_token* token)
2571 return (token->type == CPP_STRING ||
2572 token->type == CPP_STRING16 ||
2573 token->type == CPP_STRING32 ||
2574 token->type == CPP_WSTRING ||
2575 token->type == CPP_UTF8STRING);
2578 /* Returns nonzero if TOKEN is a string literal
2579 of a user-defined string literal. */
2581 static bool
2582 cp_parser_is_string_literal (cp_token* token)
2584 return (cp_parser_is_pure_string_literal (token) ||
2585 token->type == CPP_STRING_USERDEF ||
2586 token->type == CPP_STRING16_USERDEF ||
2587 token->type == CPP_STRING32_USERDEF ||
2588 token->type == CPP_WSTRING_USERDEF ||
2589 token->type == CPP_UTF8STRING_USERDEF);
2592 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2594 static bool
2595 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2597 return token->keyword == keyword;
2600 /* If not parsing tentatively, issue a diagnostic of the form
2601 FILE:LINE: MESSAGE before TOKEN
2602 where TOKEN is the next token in the input stream. MESSAGE
2603 (specified by the caller) is usually of the form "expected
2604 OTHER-TOKEN". */
2606 static void
2607 cp_parser_error (cp_parser* parser, const char* gmsgid)
2609 if (!cp_parser_simulate_error (parser))
2611 cp_token *token = cp_lexer_peek_token (parser->lexer);
2612 /* This diagnostic makes more sense if it is tagged to the line
2613 of the token we just peeked at. */
2614 cp_lexer_set_source_position_from_token (token);
2616 if (token->type == CPP_PRAGMA)
2618 error_at (token->location,
2619 "%<#pragma%> is not allowed here");
2620 cp_parser_skip_to_pragma_eol (parser, token);
2621 return;
2624 c_parse_error (gmsgid,
2625 /* Because c_parser_error does not understand
2626 CPP_KEYWORD, keywords are treated like
2627 identifiers. */
2628 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2629 token->u.value, token->flags);
2633 /* Issue an error about name-lookup failing. NAME is the
2634 IDENTIFIER_NODE DECL is the result of
2635 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2636 the thing that we hoped to find. */
2638 static void
2639 cp_parser_name_lookup_error (cp_parser* parser,
2640 tree name,
2641 tree decl,
2642 name_lookup_error desired,
2643 location_t location)
2645 /* If name lookup completely failed, tell the user that NAME was not
2646 declared. */
2647 if (decl == error_mark_node)
2649 if (parser->scope && parser->scope != global_namespace)
2650 error_at (location, "%<%E::%E%> has not been declared",
2651 parser->scope, name);
2652 else if (parser->scope == global_namespace)
2653 error_at (location, "%<::%E%> has not been declared", name);
2654 else if (parser->object_scope
2655 && !CLASS_TYPE_P (parser->object_scope))
2656 error_at (location, "request for member %qE in non-class type %qT",
2657 name, parser->object_scope);
2658 else if (parser->object_scope)
2659 error_at (location, "%<%T::%E%> has not been declared",
2660 parser->object_scope, name);
2661 else
2662 error_at (location, "%qE has not been declared", name);
2664 else if (parser->scope && parser->scope != global_namespace)
2666 switch (desired)
2668 case NLE_TYPE:
2669 error_at (location, "%<%E::%E%> is not a type",
2670 parser->scope, name);
2671 break;
2672 case NLE_CXX98:
2673 error_at (location, "%<%E::%E%> is not a class or namespace",
2674 parser->scope, name);
2675 break;
2676 case NLE_NOT_CXX98:
2677 error_at (location,
2678 "%<%E::%E%> is not a class, namespace, or enumeration",
2679 parser->scope, name);
2680 break;
2681 default:
2682 gcc_unreachable ();
2686 else if (parser->scope == global_namespace)
2688 switch (desired)
2690 case NLE_TYPE:
2691 error_at (location, "%<::%E%> is not a type", name);
2692 break;
2693 case NLE_CXX98:
2694 error_at (location, "%<::%E%> is not a class or namespace", name);
2695 break;
2696 case NLE_NOT_CXX98:
2697 error_at (location,
2698 "%<::%E%> is not a class, namespace, or enumeration",
2699 name);
2700 break;
2701 default:
2702 gcc_unreachable ();
2705 else
2707 switch (desired)
2709 case NLE_TYPE:
2710 error_at (location, "%qE is not a type", name);
2711 break;
2712 case NLE_CXX98:
2713 error_at (location, "%qE is not a class or namespace", name);
2714 break;
2715 case NLE_NOT_CXX98:
2716 error_at (location,
2717 "%qE is not a class, namespace, or enumeration", name);
2718 break;
2719 default:
2720 gcc_unreachable ();
2725 /* If we are parsing tentatively, remember that an error has occurred
2726 during this tentative parse. Returns true if the error was
2727 simulated; false if a message should be issued by the caller. */
2729 static bool
2730 cp_parser_simulate_error (cp_parser* parser)
2732 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2734 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2735 return true;
2737 return false;
2740 /* This function is called when a type is defined. If type
2741 definitions are forbidden at this point, an error message is
2742 issued. */
2744 static bool
2745 cp_parser_check_type_definition (cp_parser* parser)
2747 /* If types are forbidden here, issue a message. */
2748 if (parser->type_definition_forbidden_message)
2750 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2751 in the message need to be interpreted. */
2752 error (parser->type_definition_forbidden_message);
2753 return false;
2755 return true;
2758 /* This function is called when the DECLARATOR is processed. The TYPE
2759 was a type defined in the decl-specifiers. If it is invalid to
2760 define a type in the decl-specifiers for DECLARATOR, an error is
2761 issued. TYPE_LOCATION is the location of TYPE and is used
2762 for error reporting. */
2764 static void
2765 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2766 tree type, location_t type_location)
2768 /* [dcl.fct] forbids type definitions in return types.
2769 Unfortunately, it's not easy to know whether or not we are
2770 processing a return type until after the fact. */
2771 while (declarator
2772 && (declarator->kind == cdk_pointer
2773 || declarator->kind == cdk_reference
2774 || declarator->kind == cdk_ptrmem))
2775 declarator = declarator->declarator;
2776 if (declarator
2777 && declarator->kind == cdk_function)
2779 error_at (type_location,
2780 "new types may not be defined in a return type");
2781 inform (type_location,
2782 "(perhaps a semicolon is missing after the definition of %qT)",
2783 type);
2787 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2788 "<" in any valid C++ program. If the next token is indeed "<",
2789 issue a message warning the user about what appears to be an
2790 invalid attempt to form a template-id. LOCATION is the location
2791 of the type-specifier (TYPE) */
2793 static void
2794 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2795 tree type,
2796 enum tag_types tag_type,
2797 location_t location)
2799 cp_token_position start = 0;
2801 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2803 if (TYPE_P (type))
2804 error_at (location, "%qT is not a template", type);
2805 else if (identifier_p (type))
2807 if (tag_type != none_type)
2808 error_at (location, "%qE is not a class template", type);
2809 else
2810 error_at (location, "%qE is not a template", type);
2812 else
2813 error_at (location, "invalid template-id");
2814 /* Remember the location of the invalid "<". */
2815 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2816 start = cp_lexer_token_position (parser->lexer, true);
2817 /* Consume the "<". */
2818 cp_lexer_consume_token (parser->lexer);
2819 /* Parse the template arguments. */
2820 cp_parser_enclosed_template_argument_list (parser);
2821 /* Permanently remove the invalid template arguments so that
2822 this error message is not issued again. */
2823 if (start)
2824 cp_lexer_purge_tokens_after (parser->lexer, start);
2828 /* If parsing an integral constant-expression, issue an error message
2829 about the fact that THING appeared and return true. Otherwise,
2830 return false. In either case, set
2831 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2833 static bool
2834 cp_parser_non_integral_constant_expression (cp_parser *parser,
2835 non_integral_constant thing)
2837 parser->non_integral_constant_expression_p = true;
2838 if (parser->integral_constant_expression_p)
2840 if (!parser->allow_non_integral_constant_expression_p)
2842 const char *msg = NULL;
2843 switch (thing)
2845 case NIC_FLOAT:
2846 error ("floating-point literal "
2847 "cannot appear in a constant-expression");
2848 return true;
2849 case NIC_CAST:
2850 error ("a cast to a type other than an integral or "
2851 "enumeration type cannot appear in a "
2852 "constant-expression");
2853 return true;
2854 case NIC_TYPEID:
2855 error ("%<typeid%> operator "
2856 "cannot appear in a constant-expression");
2857 return true;
2858 case NIC_NCC:
2859 error ("non-constant compound literals "
2860 "cannot appear in a constant-expression");
2861 return true;
2862 case NIC_FUNC_CALL:
2863 error ("a function call "
2864 "cannot appear in a constant-expression");
2865 return true;
2866 case NIC_INC:
2867 error ("an increment "
2868 "cannot appear in a constant-expression");
2869 return true;
2870 case NIC_DEC:
2871 error ("an decrement "
2872 "cannot appear in a constant-expression");
2873 return true;
2874 case NIC_ARRAY_REF:
2875 error ("an array reference "
2876 "cannot appear in a constant-expression");
2877 return true;
2878 case NIC_ADDR_LABEL:
2879 error ("the address of a label "
2880 "cannot appear in a constant-expression");
2881 return true;
2882 case NIC_OVERLOADED:
2883 error ("calls to overloaded operators "
2884 "cannot appear in a constant-expression");
2885 return true;
2886 case NIC_ASSIGNMENT:
2887 error ("an assignment cannot appear in a constant-expression");
2888 return true;
2889 case NIC_COMMA:
2890 error ("a comma operator "
2891 "cannot appear in a constant-expression");
2892 return true;
2893 case NIC_CONSTRUCTOR:
2894 error ("a call to a constructor "
2895 "cannot appear in a constant-expression");
2896 return true;
2897 case NIC_TRANSACTION:
2898 error ("a transaction expression "
2899 "cannot appear in a constant-expression");
2900 return true;
2901 case NIC_THIS:
2902 msg = "this";
2903 break;
2904 case NIC_FUNC_NAME:
2905 msg = "__FUNCTION__";
2906 break;
2907 case NIC_PRETTY_FUNC:
2908 msg = "__PRETTY_FUNCTION__";
2909 break;
2910 case NIC_C99_FUNC:
2911 msg = "__func__";
2912 break;
2913 case NIC_VA_ARG:
2914 msg = "va_arg";
2915 break;
2916 case NIC_ARROW:
2917 msg = "->";
2918 break;
2919 case NIC_POINT:
2920 msg = ".";
2921 break;
2922 case NIC_STAR:
2923 msg = "*";
2924 break;
2925 case NIC_ADDR:
2926 msg = "&";
2927 break;
2928 case NIC_PREINCREMENT:
2929 msg = "++";
2930 break;
2931 case NIC_PREDECREMENT:
2932 msg = "--";
2933 break;
2934 case NIC_NEW:
2935 msg = "new";
2936 break;
2937 case NIC_DEL:
2938 msg = "delete";
2939 break;
2940 default:
2941 gcc_unreachable ();
2943 if (msg)
2944 error ("%qs cannot appear in a constant-expression", msg);
2945 return true;
2948 return false;
2951 /* Emit a diagnostic for an invalid type name. SCOPE is the
2952 qualifying scope (or NULL, if none) for ID. This function commits
2953 to the current active tentative parse, if any. (Otherwise, the
2954 problematic construct might be encountered again later, resulting
2955 in duplicate error messages.) LOCATION is the location of ID. */
2957 static void
2958 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2959 tree scope, tree id,
2960 location_t location)
2962 tree decl, old_scope, ambiguous_decls;
2963 cp_parser_commit_to_tentative_parse (parser);
2964 /* Try to lookup the identifier. */
2965 old_scope = parser->scope;
2966 parser->scope = scope;
2967 decl = cp_parser_lookup_name (parser, id, none_type,
2968 /*is_template=*/false,
2969 /*is_namespace=*/false,
2970 /*check_dependency=*/true,
2971 &ambiguous_decls, location);
2972 parser->scope = old_scope;
2973 if (ambiguous_decls)
2974 /* If the lookup was ambiguous, an error will already have
2975 been issued. */
2976 return;
2977 /* If the lookup found a template-name, it means that the user forgot
2978 to specify an argument list. Emit a useful error message. */
2979 if (TREE_CODE (decl) == TEMPLATE_DECL)
2980 error_at (location,
2981 "invalid use of template-name %qE without an argument list",
2982 decl);
2983 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2984 error_at (location, "invalid use of destructor %qD as a type", id);
2985 else if (TREE_CODE (decl) == TYPE_DECL)
2986 /* Something like 'unsigned A a;' */
2987 error_at (location, "invalid combination of multiple type-specifiers");
2988 else if (!parser->scope)
2990 /* Issue an error message. */
2991 error_at (location, "%qE does not name a type", id);
2992 /* If we're in a template class, it's possible that the user was
2993 referring to a type from a base class. For example:
2995 template <typename T> struct A { typedef T X; };
2996 template <typename T> struct B : public A<T> { X x; };
2998 The user should have said "typename A<T>::X". */
2999 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3000 inform (location, "C++11 %<constexpr%> only available with "
3001 "-std=c++11 or -std=gnu++11");
3002 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3003 inform (location, "C++11 %<noexcept%> only available with "
3004 "-std=c++11 or -std=gnu++11");
3005 else if (cxx_dialect < cxx11
3006 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3007 inform (location, "C++11 %<thread_local%> only available with "
3008 "-std=c++11 or -std=gnu++11");
3009 else if (processing_template_decl && current_class_type
3010 && TYPE_BINFO (current_class_type))
3012 tree b;
3014 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3016 b = TREE_CHAIN (b))
3018 tree base_type = BINFO_TYPE (b);
3019 if (CLASS_TYPE_P (base_type)
3020 && dependent_type_p (base_type))
3022 tree field;
3023 /* Go from a particular instantiation of the
3024 template (which will have an empty TYPE_FIELDs),
3025 to the main version. */
3026 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3027 for (field = TYPE_FIELDS (base_type);
3028 field;
3029 field = DECL_CHAIN (field))
3030 if (TREE_CODE (field) == TYPE_DECL
3031 && DECL_NAME (field) == id)
3033 inform (location,
3034 "(perhaps %<typename %T::%E%> was intended)",
3035 BINFO_TYPE (b), id);
3036 break;
3038 if (field)
3039 break;
3044 /* Here we diagnose qualified-ids where the scope is actually correct,
3045 but the identifier does not resolve to a valid type name. */
3046 else if (parser->scope != error_mark_node)
3048 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3050 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3051 error_at (location_of (id),
3052 "%qE in namespace %qE does not name a template type",
3053 id, parser->scope);
3054 else
3055 error_at (location_of (id),
3056 "%qE in namespace %qE does not name a type",
3057 id, parser->scope);
3059 else if (CLASS_TYPE_P (parser->scope)
3060 && constructor_name_p (id, parser->scope))
3062 /* A<T>::A<T>() */
3063 error_at (location, "%<%T::%E%> names the constructor, not"
3064 " the type", parser->scope, id);
3065 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3066 error_at (location, "and %qT has no template constructors",
3067 parser->scope);
3069 else if (TYPE_P (parser->scope)
3070 && dependent_scope_p (parser->scope))
3071 error_at (location, "need %<typename%> before %<%T::%E%> because "
3072 "%qT is a dependent scope",
3073 parser->scope, id, parser->scope);
3074 else if (TYPE_P (parser->scope))
3076 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3077 error_at (location_of (id),
3078 "%qE in %q#T does not name a template type",
3079 id, parser->scope);
3080 else
3081 error_at (location_of (id),
3082 "%qE in %q#T does not name a type",
3083 id, parser->scope);
3085 else
3086 gcc_unreachable ();
3090 /* Check for a common situation where a type-name should be present,
3091 but is not, and issue a sensible error message. Returns true if an
3092 invalid type-name was detected.
3094 The situation handled by this function are variable declarations of the
3095 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3096 Usually, `ID' should name a type, but if we got here it means that it
3097 does not. We try to emit the best possible error message depending on
3098 how exactly the id-expression looks like. */
3100 static bool
3101 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3103 tree id;
3104 cp_token *token = cp_lexer_peek_token (parser->lexer);
3106 /* Avoid duplicate error about ambiguous lookup. */
3107 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3109 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3110 if (next->type == CPP_NAME && next->error_reported)
3111 goto out;
3114 cp_parser_parse_tentatively (parser);
3115 id = cp_parser_id_expression (parser,
3116 /*template_keyword_p=*/false,
3117 /*check_dependency_p=*/true,
3118 /*template_p=*/NULL,
3119 /*declarator_p=*/true,
3120 /*optional_p=*/false);
3121 /* If the next token is a (, this is a function with no explicit return
3122 type, i.e. constructor, destructor or conversion op. */
3123 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3124 || TREE_CODE (id) == TYPE_DECL)
3126 cp_parser_abort_tentative_parse (parser);
3127 return false;
3129 if (!cp_parser_parse_definitely (parser))
3130 return false;
3132 /* Emit a diagnostic for the invalid type. */
3133 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3134 id, token->location);
3135 out:
3136 /* If we aren't in the middle of a declarator (i.e. in a
3137 parameter-declaration-clause), skip to the end of the declaration;
3138 there's no point in trying to process it. */
3139 if (!parser->in_declarator_p)
3140 cp_parser_skip_to_end_of_block_or_statement (parser);
3141 return true;
3144 /* Consume tokens up to, and including, the next non-nested closing `)'.
3145 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3146 are doing error recovery. Returns -1 if OR_COMMA is true and we
3147 found an unnested comma. */
3149 static int
3150 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3151 bool recovering,
3152 bool or_comma,
3153 bool consume_paren)
3155 unsigned paren_depth = 0;
3156 unsigned brace_depth = 0;
3157 unsigned square_depth = 0;
3159 if (recovering && !or_comma
3160 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3161 return 0;
3163 while (true)
3165 cp_token * token = cp_lexer_peek_token (parser->lexer);
3167 switch (token->type)
3169 case CPP_EOF:
3170 case CPP_PRAGMA_EOL:
3171 /* If we've run out of tokens, then there is no closing `)'. */
3172 return 0;
3174 /* This is good for lambda expression capture-lists. */
3175 case CPP_OPEN_SQUARE:
3176 ++square_depth;
3177 break;
3178 case CPP_CLOSE_SQUARE:
3179 if (!square_depth--)
3180 return 0;
3181 break;
3183 case CPP_SEMICOLON:
3184 /* This matches the processing in skip_to_end_of_statement. */
3185 if (!brace_depth)
3186 return 0;
3187 break;
3189 case CPP_OPEN_BRACE:
3190 ++brace_depth;
3191 break;
3192 case CPP_CLOSE_BRACE:
3193 if (!brace_depth--)
3194 return 0;
3195 break;
3197 case CPP_COMMA:
3198 if (recovering && or_comma && !brace_depth && !paren_depth
3199 && !square_depth)
3200 return -1;
3201 break;
3203 case CPP_OPEN_PAREN:
3204 if (!brace_depth)
3205 ++paren_depth;
3206 break;
3208 case CPP_CLOSE_PAREN:
3209 if (!brace_depth && !paren_depth--)
3211 if (consume_paren)
3212 cp_lexer_consume_token (parser->lexer);
3213 return 1;
3215 break;
3217 default:
3218 break;
3221 /* Consume the token. */
3222 cp_lexer_consume_token (parser->lexer);
3226 /* Consume tokens until we reach the end of the current statement.
3227 Normally, that will be just before consuming a `;'. However, if a
3228 non-nested `}' comes first, then we stop before consuming that. */
3230 static void
3231 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3233 unsigned nesting_depth = 0;
3235 /* Unwind generic function template scope if necessary. */
3236 if (parser->fully_implicit_function_template_p)
3237 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3239 while (true)
3241 cp_token *token = cp_lexer_peek_token (parser->lexer);
3243 switch (token->type)
3245 case CPP_EOF:
3246 case CPP_PRAGMA_EOL:
3247 /* If we've run out of tokens, stop. */
3248 return;
3250 case CPP_SEMICOLON:
3251 /* If the next token is a `;', we have reached the end of the
3252 statement. */
3253 if (!nesting_depth)
3254 return;
3255 break;
3257 case CPP_CLOSE_BRACE:
3258 /* If this is a non-nested '}', stop before consuming it.
3259 That way, when confronted with something like:
3261 { 3 + }
3263 we stop before consuming the closing '}', even though we
3264 have not yet reached a `;'. */
3265 if (nesting_depth == 0)
3266 return;
3268 /* If it is the closing '}' for a block that we have
3269 scanned, stop -- but only after consuming the token.
3270 That way given:
3272 void f g () { ... }
3273 typedef int I;
3275 we will stop after the body of the erroneously declared
3276 function, but before consuming the following `typedef'
3277 declaration. */
3278 if (--nesting_depth == 0)
3280 cp_lexer_consume_token (parser->lexer);
3281 return;
3284 case CPP_OPEN_BRACE:
3285 ++nesting_depth;
3286 break;
3288 default:
3289 break;
3292 /* Consume the token. */
3293 cp_lexer_consume_token (parser->lexer);
3297 /* This function is called at the end of a statement or declaration.
3298 If the next token is a semicolon, it is consumed; otherwise, error
3299 recovery is attempted. */
3301 static void
3302 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3304 /* Look for the trailing `;'. */
3305 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3307 /* If there is additional (erroneous) input, skip to the end of
3308 the statement. */
3309 cp_parser_skip_to_end_of_statement (parser);
3310 /* If the next token is now a `;', consume it. */
3311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3312 cp_lexer_consume_token (parser->lexer);
3316 /* Skip tokens until we have consumed an entire block, or until we
3317 have consumed a non-nested `;'. */
3319 static void
3320 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3322 int nesting_depth = 0;
3324 /* Unwind generic function template scope if necessary. */
3325 if (parser->fully_implicit_function_template_p)
3326 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3328 while (nesting_depth >= 0)
3330 cp_token *token = cp_lexer_peek_token (parser->lexer);
3332 switch (token->type)
3334 case CPP_EOF:
3335 case CPP_PRAGMA_EOL:
3336 /* If we've run out of tokens, stop. */
3337 return;
3339 case CPP_SEMICOLON:
3340 /* Stop if this is an unnested ';'. */
3341 if (!nesting_depth)
3342 nesting_depth = -1;
3343 break;
3345 case CPP_CLOSE_BRACE:
3346 /* Stop if this is an unnested '}', or closes the outermost
3347 nesting level. */
3348 nesting_depth--;
3349 if (nesting_depth < 0)
3350 return;
3351 if (!nesting_depth)
3352 nesting_depth = -1;
3353 break;
3355 case CPP_OPEN_BRACE:
3356 /* Nest. */
3357 nesting_depth++;
3358 break;
3360 default:
3361 break;
3364 /* Consume the token. */
3365 cp_lexer_consume_token (parser->lexer);
3369 /* Skip tokens until a non-nested closing curly brace is the next
3370 token, or there are no more tokens. Return true in the first case,
3371 false otherwise. */
3373 static bool
3374 cp_parser_skip_to_closing_brace (cp_parser *parser)
3376 unsigned nesting_depth = 0;
3378 while (true)
3380 cp_token *token = cp_lexer_peek_token (parser->lexer);
3382 switch (token->type)
3384 case CPP_EOF:
3385 case CPP_PRAGMA_EOL:
3386 /* If we've run out of tokens, stop. */
3387 return false;
3389 case CPP_CLOSE_BRACE:
3390 /* If the next token is a non-nested `}', then we have reached
3391 the end of the current block. */
3392 if (nesting_depth-- == 0)
3393 return true;
3394 break;
3396 case CPP_OPEN_BRACE:
3397 /* If it the next token is a `{', then we are entering a new
3398 block. Consume the entire block. */
3399 ++nesting_depth;
3400 break;
3402 default:
3403 break;
3406 /* Consume the token. */
3407 cp_lexer_consume_token (parser->lexer);
3411 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3412 parameter is the PRAGMA token, allowing us to purge the entire pragma
3413 sequence. */
3415 static void
3416 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3418 cp_token *token;
3420 parser->lexer->in_pragma = false;
3423 token = cp_lexer_consume_token (parser->lexer);
3424 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3426 /* Ensure that the pragma is not parsed again. */
3427 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3430 /* Require pragma end of line, resyncing with it as necessary. The
3431 arguments are as for cp_parser_skip_to_pragma_eol. */
3433 static void
3434 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3436 parser->lexer->in_pragma = false;
3437 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3438 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3441 /* This is a simple wrapper around make_typename_type. When the id is
3442 an unresolved identifier node, we can provide a superior diagnostic
3443 using cp_parser_diagnose_invalid_type_name. */
3445 static tree
3446 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3447 tree id, location_t id_location)
3449 tree result;
3450 if (identifier_p (id))
3452 result = make_typename_type (scope, id, typename_type,
3453 /*complain=*/tf_none);
3454 if (result == error_mark_node)
3455 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3456 return result;
3458 return make_typename_type (scope, id, typename_type, tf_error);
3461 /* This is a wrapper around the
3462 make_{pointer,ptrmem,reference}_declarator functions that decides
3463 which one to call based on the CODE and CLASS_TYPE arguments. The
3464 CODE argument should be one of the values returned by
3465 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3466 appertain to the pointer or reference. */
3468 static cp_declarator *
3469 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3470 cp_cv_quals cv_qualifiers,
3471 cp_declarator *target,
3472 tree attributes)
3474 if (code == ERROR_MARK)
3475 return cp_error_declarator;
3477 if (code == INDIRECT_REF)
3478 if (class_type == NULL_TREE)
3479 return make_pointer_declarator (cv_qualifiers, target, attributes);
3480 else
3481 return make_ptrmem_declarator (cv_qualifiers, class_type,
3482 target, attributes);
3483 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3484 return make_reference_declarator (cv_qualifiers, target,
3485 false, attributes);
3486 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3487 return make_reference_declarator (cv_qualifiers, target,
3488 true, attributes);
3489 gcc_unreachable ();
3492 /* Create a new C++ parser. */
3494 static cp_parser *
3495 cp_parser_new (void)
3497 cp_parser *parser;
3498 cp_lexer *lexer;
3499 unsigned i;
3501 /* cp_lexer_new_main is called before doing GC allocation because
3502 cp_lexer_new_main might load a PCH file. */
3503 lexer = cp_lexer_new_main ();
3505 /* Initialize the binops_by_token so that we can get the tree
3506 directly from the token. */
3507 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3508 binops_by_token[binops[i].token_type] = binops[i];
3510 parser = ggc_cleared_alloc<cp_parser> ();
3511 parser->lexer = lexer;
3512 parser->context = cp_parser_context_new (NULL);
3514 /* For now, we always accept GNU extensions. */
3515 parser->allow_gnu_extensions_p = 1;
3517 /* The `>' token is a greater-than operator, not the end of a
3518 template-id. */
3519 parser->greater_than_is_operator_p = true;
3521 parser->default_arg_ok_p = true;
3523 /* We are not parsing a constant-expression. */
3524 parser->integral_constant_expression_p = false;
3525 parser->allow_non_integral_constant_expression_p = false;
3526 parser->non_integral_constant_expression_p = false;
3528 /* Local variable names are not forbidden. */
3529 parser->local_variables_forbidden_p = false;
3531 /* We are not processing an `extern "C"' declaration. */
3532 parser->in_unbraced_linkage_specification_p = false;
3534 /* We are not processing a declarator. */
3535 parser->in_declarator_p = false;
3537 /* We are not processing a template-argument-list. */
3538 parser->in_template_argument_list_p = false;
3540 /* We are not in an iteration statement. */
3541 parser->in_statement = 0;
3543 /* We are not in a switch statement. */
3544 parser->in_switch_statement_p = false;
3546 /* We are not parsing a type-id inside an expression. */
3547 parser->in_type_id_in_expr_p = false;
3549 /* Declarations aren't implicitly extern "C". */
3550 parser->implicit_extern_c = false;
3552 /* String literals should be translated to the execution character set. */
3553 parser->translate_strings_p = true;
3555 /* We are not parsing a function body. */
3556 parser->in_function_body = false;
3558 /* We can correct until told otherwise. */
3559 parser->colon_corrects_to_scope_p = true;
3561 /* The unparsed function queue is empty. */
3562 push_unparsed_function_queues (parser);
3564 /* There are no classes being defined. */
3565 parser->num_classes_being_defined = 0;
3567 /* No template parameters apply. */
3568 parser->num_template_parameter_lists = 0;
3570 /* Not declaring an implicit function template. */
3571 parser->auto_is_implicit_function_template_parm_p = false;
3572 parser->fully_implicit_function_template_p = false;
3573 parser->implicit_template_parms = 0;
3574 parser->implicit_template_scope = 0;
3576 return parser;
3579 /* Create a cp_lexer structure which will emit the tokens in CACHE
3580 and push it onto the parser's lexer stack. This is used for delayed
3581 parsing of in-class method bodies and default arguments, and should
3582 not be confused with tentative parsing. */
3583 static void
3584 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3586 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3587 lexer->next = parser->lexer;
3588 parser->lexer = lexer;
3590 /* Move the current source position to that of the first token in the
3591 new lexer. */
3592 cp_lexer_set_source_position_from_token (lexer->next_token);
3595 /* Pop the top lexer off the parser stack. This is never used for the
3596 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3597 static void
3598 cp_parser_pop_lexer (cp_parser *parser)
3600 cp_lexer *lexer = parser->lexer;
3601 parser->lexer = lexer->next;
3602 cp_lexer_destroy (lexer);
3604 /* Put the current source position back where it was before this
3605 lexer was pushed. */
3606 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3609 /* Lexical conventions [gram.lex] */
3611 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3612 identifier. */
3614 static tree
3615 cp_parser_identifier (cp_parser* parser)
3617 cp_token *token;
3619 /* Look for the identifier. */
3620 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3621 /* Return the value. */
3622 return token ? token->u.value : error_mark_node;
3625 /* Parse a sequence of adjacent string constants. Returns a
3626 TREE_STRING representing the combined, nul-terminated string
3627 constant. If TRANSLATE is true, translate the string to the
3628 execution character set. If WIDE_OK is true, a wide string is
3629 invalid here.
3631 C++98 [lex.string] says that if a narrow string literal token is
3632 adjacent to a wide string literal token, the behavior is undefined.
3633 However, C99 6.4.5p4 says that this results in a wide string literal.
3634 We follow C99 here, for consistency with the C front end.
3636 This code is largely lifted from lex_string() in c-lex.c.
3638 FUTURE: ObjC++ will need to handle @-strings here. */
3639 static tree
3640 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3642 tree value;
3643 size_t count;
3644 struct obstack str_ob;
3645 cpp_string str, istr, *strs;
3646 cp_token *tok;
3647 enum cpp_ttype type, curr_type;
3648 int have_suffix_p = 0;
3649 tree string_tree;
3650 tree suffix_id = NULL_TREE;
3651 bool curr_tok_is_userdef_p = false;
3653 tok = cp_lexer_peek_token (parser->lexer);
3654 if (!cp_parser_is_string_literal (tok))
3656 cp_parser_error (parser, "expected string-literal");
3657 return error_mark_node;
3660 if (cpp_userdef_string_p (tok->type))
3662 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3663 curr_type = cpp_userdef_string_remove_type (tok->type);
3664 curr_tok_is_userdef_p = true;
3666 else
3668 string_tree = tok->u.value;
3669 curr_type = tok->type;
3671 type = curr_type;
3673 /* Try to avoid the overhead of creating and destroying an obstack
3674 for the common case of just one string. */
3675 if (!cp_parser_is_string_literal
3676 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3678 cp_lexer_consume_token (parser->lexer);
3680 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3681 str.len = TREE_STRING_LENGTH (string_tree);
3682 count = 1;
3684 if (curr_tok_is_userdef_p)
3686 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3687 have_suffix_p = 1;
3688 curr_type = cpp_userdef_string_remove_type (tok->type);
3690 else
3691 curr_type = tok->type;
3693 strs = &str;
3695 else
3697 gcc_obstack_init (&str_ob);
3698 count = 0;
3702 cp_lexer_consume_token (parser->lexer);
3703 count++;
3704 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3705 str.len = TREE_STRING_LENGTH (string_tree);
3707 if (curr_tok_is_userdef_p)
3709 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3710 if (have_suffix_p == 0)
3712 suffix_id = curr_suffix_id;
3713 have_suffix_p = 1;
3715 else if (have_suffix_p == 1
3716 && curr_suffix_id != suffix_id)
3718 error ("inconsistent user-defined literal suffixes"
3719 " %qD and %qD in string literal",
3720 suffix_id, curr_suffix_id);
3721 have_suffix_p = -1;
3723 curr_type = cpp_userdef_string_remove_type (tok->type);
3725 else
3726 curr_type = tok->type;
3728 if (type != curr_type)
3730 if (type == CPP_STRING)
3731 type = curr_type;
3732 else if (curr_type != CPP_STRING)
3733 error_at (tok->location,
3734 "unsupported non-standard concatenation "
3735 "of string literals");
3738 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3740 tok = cp_lexer_peek_token (parser->lexer);
3741 if (cpp_userdef_string_p (tok->type))
3743 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3744 curr_type = cpp_userdef_string_remove_type (tok->type);
3745 curr_tok_is_userdef_p = true;
3747 else
3749 string_tree = tok->u.value;
3750 curr_type = tok->type;
3751 curr_tok_is_userdef_p = false;
3754 while (cp_parser_is_string_literal (tok));
3756 strs = (cpp_string *) obstack_finish (&str_ob);
3759 if (type != CPP_STRING && !wide_ok)
3761 cp_parser_error (parser, "a wide string is invalid in this context");
3762 type = CPP_STRING;
3765 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3766 (parse_in, strs, count, &istr, type))
3768 value = build_string (istr.len, (const char *)istr.text);
3769 free (CONST_CAST (unsigned char *, istr.text));
3771 switch (type)
3773 default:
3774 case CPP_STRING:
3775 case CPP_UTF8STRING:
3776 TREE_TYPE (value) = char_array_type_node;
3777 break;
3778 case CPP_STRING16:
3779 TREE_TYPE (value) = char16_array_type_node;
3780 break;
3781 case CPP_STRING32:
3782 TREE_TYPE (value) = char32_array_type_node;
3783 break;
3784 case CPP_WSTRING:
3785 TREE_TYPE (value) = wchar_array_type_node;
3786 break;
3789 value = fix_string_type (value);
3791 if (have_suffix_p)
3793 tree literal = build_userdef_literal (suffix_id, value,
3794 OT_NONE, NULL_TREE);
3795 tok->u.value = literal;
3796 return cp_parser_userdef_string_literal (tok);
3799 else
3800 /* cpp_interpret_string has issued an error. */
3801 value = error_mark_node;
3803 if (count > 1)
3804 obstack_free (&str_ob, 0);
3806 return value;
3809 /* Look up a literal operator with the name and the exact arguments. */
3811 static tree
3812 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3814 tree decl, fns;
3815 decl = lookup_name (name);
3816 if (!decl || !is_overloaded_fn (decl))
3817 return error_mark_node;
3819 for (fns = decl; fns; fns = OVL_NEXT (fns))
3821 unsigned int ix;
3822 bool found = true;
3823 tree fn = OVL_CURRENT (fns);
3824 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3825 if (parmtypes != NULL_TREE)
3827 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3828 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3830 tree tparm = TREE_VALUE (parmtypes);
3831 tree targ = TREE_TYPE ((*args)[ix]);
3832 bool ptr = TYPE_PTR_P (tparm);
3833 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3834 if ((ptr || arr || !same_type_p (tparm, targ))
3835 && (!ptr || !arr
3836 || !same_type_p (TREE_TYPE (tparm),
3837 TREE_TYPE (targ))))
3838 found = false;
3840 if (found
3841 && ix == vec_safe_length (args)
3842 /* May be this should be sufficient_parms_p instead,
3843 depending on how exactly should user-defined literals
3844 work in presence of default arguments on the literal
3845 operator parameters. */
3846 && parmtypes == void_list_node)
3847 return fn;
3851 return error_mark_node;
3854 /* Parse a user-defined char constant. Returns a call to a user-defined
3855 literal operator taking the character as an argument. */
3857 static tree
3858 cp_parser_userdef_char_literal (cp_parser *parser)
3860 cp_token *token = cp_lexer_consume_token (parser->lexer);
3861 tree literal = token->u.value;
3862 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3863 tree value = USERDEF_LITERAL_VALUE (literal);
3864 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3865 tree decl, result;
3867 /* Build up a call to the user-defined operator */
3868 /* Lookup the name we got back from the id-expression. */
3869 vec<tree, va_gc> *args = make_tree_vector ();
3870 vec_safe_push (args, value);
3871 decl = lookup_literal_operator (name, args);
3872 if (!decl || decl == error_mark_node)
3874 error ("unable to find character literal operator %qD with %qT argument",
3875 name, TREE_TYPE (value));
3876 release_tree_vector (args);
3877 return error_mark_node;
3879 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3880 release_tree_vector (args);
3881 if (result != error_mark_node)
3882 return result;
3884 error ("unable to find character literal operator %qD with %qT argument",
3885 name, TREE_TYPE (value));
3886 return error_mark_node;
3889 /* A subroutine of cp_parser_userdef_numeric_literal to
3890 create a char... template parameter pack from a string node. */
3892 static tree
3893 make_char_string_pack (tree value)
3895 tree charvec;
3896 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3897 const char *str = TREE_STRING_POINTER (value);
3898 int i, len = TREE_STRING_LENGTH (value) - 1;
3899 tree argvec = make_tree_vec (1);
3901 /* Fill in CHARVEC with all of the parameters. */
3902 charvec = make_tree_vec (len);
3903 for (i = 0; i < len; ++i)
3904 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3906 /* Build the argument packs. */
3907 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3908 TREE_TYPE (argpack) = char_type_node;
3910 TREE_VEC_ELT (argvec, 0) = argpack;
3912 return argvec;
3915 /* A subroutine of cp_parser_userdef_numeric_literal to
3916 create a char... template parameter pack from a string node. */
3918 static tree
3919 make_string_pack (tree value)
3921 tree charvec;
3922 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3923 const unsigned char *str
3924 = (const unsigned char *) TREE_STRING_POINTER (value);
3925 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3926 int len = TREE_STRING_LENGTH (value) / sz - 1;
3927 tree argvec = make_tree_vec (2);
3929 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3930 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3932 /* First template parm is character type. */
3933 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3935 /* Fill in CHARVEC with all of the parameters. */
3936 charvec = make_tree_vec (len);
3937 for (int i = 0; i < len; ++i)
3938 TREE_VEC_ELT (charvec, i)
3939 = double_int_to_tree (str_char_type_node,
3940 double_int::from_buffer (str + i * sz, sz));
3942 /* Build the argument packs. */
3943 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3944 TREE_TYPE (argpack) = str_char_type_node;
3946 TREE_VEC_ELT (argvec, 1) = argpack;
3948 return argvec;
3951 /* Parse a user-defined numeric constant. returns a call to a user-defined
3952 literal operator. */
3954 static tree
3955 cp_parser_userdef_numeric_literal (cp_parser *parser)
3957 cp_token *token = cp_lexer_consume_token (parser->lexer);
3958 tree literal = token->u.value;
3959 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3960 tree value = USERDEF_LITERAL_VALUE (literal);
3961 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3962 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3963 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3964 tree decl, result;
3965 vec<tree, va_gc> *args;
3967 /* Look for a literal operator taking the exact type of numeric argument
3968 as the literal value. */
3969 args = make_tree_vector ();
3970 vec_safe_push (args, value);
3971 decl = lookup_literal_operator (name, args);
3972 if (decl && decl != error_mark_node)
3974 result = finish_call_expr (decl, &args, false, true, tf_none);
3975 if (result != error_mark_node)
3977 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3978 warning_at (token->location, OPT_Woverflow,
3979 "integer literal exceeds range of %qT type",
3980 long_long_unsigned_type_node);
3981 else
3983 if (overflow > 0)
3984 warning_at (token->location, OPT_Woverflow,
3985 "floating literal exceeds range of %qT type",
3986 long_double_type_node);
3987 else if (overflow < 0)
3988 warning_at (token->location, OPT_Woverflow,
3989 "floating literal truncated to zero");
3991 release_tree_vector (args);
3992 return result;
3995 release_tree_vector (args);
3997 /* If the numeric argument didn't work, look for a raw literal
3998 operator taking a const char* argument consisting of the number
3999 in string format. */
4000 args = make_tree_vector ();
4001 vec_safe_push (args, num_string);
4002 decl = lookup_literal_operator (name, args);
4003 if (decl && decl != error_mark_node)
4005 result = finish_call_expr (decl, &args, false, true, tf_none);
4006 if (result != error_mark_node)
4008 release_tree_vector (args);
4009 return result;
4012 release_tree_vector (args);
4014 /* If the raw literal didn't work, look for a non-type template
4015 function with parameter pack char.... Call the function with
4016 template parameter characters representing the number. */
4017 args = make_tree_vector ();
4018 decl = lookup_literal_operator (name, args);
4019 if (decl && decl != error_mark_node)
4021 tree tmpl_args = make_char_string_pack (num_string);
4022 decl = lookup_template_function (decl, tmpl_args);
4023 result = finish_call_expr (decl, &args, false, true, tf_none);
4024 if (result != error_mark_node)
4026 release_tree_vector (args);
4027 return result;
4030 release_tree_vector (args);
4032 error ("unable to find numeric literal operator %qD", name);
4033 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4034 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4035 "to enable more built-in suffixes");
4036 return error_mark_node;
4039 /* Parse a user-defined string constant. Returns a call to a user-defined
4040 literal operator taking a character pointer and the length of the string
4041 as arguments. */
4043 static tree
4044 cp_parser_userdef_string_literal (cp_token *token)
4046 tree literal = token->u.value;
4047 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4048 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4049 tree value = USERDEF_LITERAL_VALUE (literal);
4050 int len = TREE_STRING_LENGTH (value)
4051 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4052 tree decl, result;
4053 vec<tree, va_gc> *args;
4055 /* Look for a template function with typename parameter CharT
4056 and parameter pack CharT... Call the function with
4057 template parameter characters representing the string. */
4058 args = make_tree_vector ();
4059 decl = lookup_literal_operator (name, args);
4060 if (decl && decl != error_mark_node)
4062 tree tmpl_args = make_string_pack (value);
4063 decl = lookup_template_function (decl, tmpl_args);
4064 result = finish_call_expr (decl, &args, false, true, tf_none);
4065 if (result != error_mark_node)
4067 release_tree_vector (args);
4068 return result;
4071 release_tree_vector (args);
4073 /* Build up a call to the user-defined operator */
4074 /* Lookup the name we got back from the id-expression. */
4075 args = make_tree_vector ();
4076 vec_safe_push (args, value);
4077 vec_safe_push (args, build_int_cst (size_type_node, len));
4078 decl = lookup_name (name);
4079 if (!decl || decl == error_mark_node)
4081 error ("unable to find string literal operator %qD", name);
4082 release_tree_vector (args);
4083 return error_mark_node;
4085 result = finish_call_expr (decl, &args, false, true, tf_none);
4086 release_tree_vector (args);
4087 if (result != error_mark_node)
4088 return result;
4090 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4091 name, TREE_TYPE (value), size_type_node);
4092 return error_mark_node;
4096 /* Basic concepts [gram.basic] */
4098 /* Parse a translation-unit.
4100 translation-unit:
4101 declaration-seq [opt]
4103 Returns TRUE if all went well. */
4105 static bool
4106 cp_parser_translation_unit (cp_parser* parser)
4108 /* The address of the first non-permanent object on the declarator
4109 obstack. */
4110 static void *declarator_obstack_base;
4112 bool success;
4114 /* Create the declarator obstack, if necessary. */
4115 if (!cp_error_declarator)
4117 gcc_obstack_init (&declarator_obstack);
4118 /* Create the error declarator. */
4119 cp_error_declarator = make_declarator (cdk_error);
4120 /* Create the empty parameter list. */
4121 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4122 /* Remember where the base of the declarator obstack lies. */
4123 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4126 cp_parser_declaration_seq_opt (parser);
4128 /* If there are no tokens left then all went well. */
4129 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4131 /* Get rid of the token array; we don't need it any more. */
4132 cp_lexer_destroy (parser->lexer);
4133 parser->lexer = NULL;
4135 /* This file might have been a context that's implicitly extern
4136 "C". If so, pop the lang context. (Only relevant for PCH.) */
4137 if (parser->implicit_extern_c)
4139 pop_lang_context ();
4140 parser->implicit_extern_c = false;
4143 /* Finish up. */
4144 finish_translation_unit ();
4146 success = true;
4148 else
4150 cp_parser_error (parser, "expected declaration");
4151 success = false;
4154 /* Make sure the declarator obstack was fully cleaned up. */
4155 gcc_assert (obstack_next_free (&declarator_obstack)
4156 == declarator_obstack_base);
4158 /* All went well. */
4159 return success;
4162 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4163 decltype context. */
4165 static inline tsubst_flags_t
4166 complain_flags (bool decltype_p)
4168 tsubst_flags_t complain = tf_warning_or_error;
4169 if (decltype_p)
4170 complain |= tf_decltype;
4171 return complain;
4174 /* Expressions [gram.expr] */
4176 /* Parse a primary-expression.
4178 primary-expression:
4179 literal
4180 this
4181 ( expression )
4182 id-expression
4184 GNU Extensions:
4186 primary-expression:
4187 ( compound-statement )
4188 __builtin_va_arg ( assignment-expression , type-id )
4189 __builtin_offsetof ( type-id , offsetof-expression )
4191 C++ Extensions:
4192 __has_nothrow_assign ( type-id )
4193 __has_nothrow_constructor ( type-id )
4194 __has_nothrow_copy ( type-id )
4195 __has_trivial_assign ( type-id )
4196 __has_trivial_constructor ( type-id )
4197 __has_trivial_copy ( type-id )
4198 __has_trivial_destructor ( type-id )
4199 __has_virtual_destructor ( type-id )
4200 __is_abstract ( type-id )
4201 __is_base_of ( type-id , type-id )
4202 __is_class ( type-id )
4203 __is_convertible_to ( type-id , type-id )
4204 __is_empty ( type-id )
4205 __is_enum ( type-id )
4206 __is_final ( type-id )
4207 __is_literal_type ( type-id )
4208 __is_pod ( type-id )
4209 __is_polymorphic ( type-id )
4210 __is_std_layout ( type-id )
4211 __is_trivial ( type-id )
4212 __is_union ( type-id )
4214 Objective-C++ Extension:
4216 primary-expression:
4217 objc-expression
4219 literal:
4220 __null
4222 ADDRESS_P is true iff this expression was immediately preceded by
4223 "&" and therefore might denote a pointer-to-member. CAST_P is true
4224 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4225 true iff this expression is a template argument.
4227 Returns a representation of the expression. Upon return, *IDK
4228 indicates what kind of id-expression (if any) was present. */
4230 static tree
4231 cp_parser_primary_expression (cp_parser *parser,
4232 bool address_p,
4233 bool cast_p,
4234 bool template_arg_p,
4235 bool decltype_p,
4236 cp_id_kind *idk)
4238 cp_token *token = NULL;
4240 /* Assume the primary expression is not an id-expression. */
4241 *idk = CP_ID_KIND_NONE;
4243 /* Peek at the next token. */
4244 token = cp_lexer_peek_token (parser->lexer);
4245 switch (token->type)
4247 /* literal:
4248 integer-literal
4249 character-literal
4250 floating-literal
4251 string-literal
4252 boolean-literal
4253 pointer-literal
4254 user-defined-literal */
4255 case CPP_CHAR:
4256 case CPP_CHAR16:
4257 case CPP_CHAR32:
4258 case CPP_WCHAR:
4259 case CPP_NUMBER:
4260 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4261 return cp_parser_userdef_numeric_literal (parser);
4262 token = cp_lexer_consume_token (parser->lexer);
4263 if (TREE_CODE (token->u.value) == FIXED_CST)
4265 error_at (token->location,
4266 "fixed-point types not supported in C++");
4267 return error_mark_node;
4269 /* Floating-point literals are only allowed in an integral
4270 constant expression if they are cast to an integral or
4271 enumeration type. */
4272 if (TREE_CODE (token->u.value) == REAL_CST
4273 && parser->integral_constant_expression_p
4274 && pedantic)
4276 /* CAST_P will be set even in invalid code like "int(2.7 +
4277 ...)". Therefore, we have to check that the next token
4278 is sure to end the cast. */
4279 if (cast_p)
4281 cp_token *next_token;
4283 next_token = cp_lexer_peek_token (parser->lexer);
4284 if (/* The comma at the end of an
4285 enumerator-definition. */
4286 next_token->type != CPP_COMMA
4287 /* The curly brace at the end of an enum-specifier. */
4288 && next_token->type != CPP_CLOSE_BRACE
4289 /* The end of a statement. */
4290 && next_token->type != CPP_SEMICOLON
4291 /* The end of the cast-expression. */
4292 && next_token->type != CPP_CLOSE_PAREN
4293 /* The end of an array bound. */
4294 && next_token->type != CPP_CLOSE_SQUARE
4295 /* The closing ">" in a template-argument-list. */
4296 && (next_token->type != CPP_GREATER
4297 || parser->greater_than_is_operator_p)
4298 /* C++0x only: A ">>" treated like two ">" tokens,
4299 in a template-argument-list. */
4300 && (next_token->type != CPP_RSHIFT
4301 || (cxx_dialect == cxx98)
4302 || parser->greater_than_is_operator_p))
4303 cast_p = false;
4306 /* If we are within a cast, then the constraint that the
4307 cast is to an integral or enumeration type will be
4308 checked at that point. If we are not within a cast, then
4309 this code is invalid. */
4310 if (!cast_p)
4311 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4313 return token->u.value;
4315 case CPP_CHAR_USERDEF:
4316 case CPP_CHAR16_USERDEF:
4317 case CPP_CHAR32_USERDEF:
4318 case CPP_WCHAR_USERDEF:
4319 return cp_parser_userdef_char_literal (parser);
4321 case CPP_STRING:
4322 case CPP_STRING16:
4323 case CPP_STRING32:
4324 case CPP_WSTRING:
4325 case CPP_UTF8STRING:
4326 case CPP_STRING_USERDEF:
4327 case CPP_STRING16_USERDEF:
4328 case CPP_STRING32_USERDEF:
4329 case CPP_WSTRING_USERDEF:
4330 case CPP_UTF8STRING_USERDEF:
4331 /* ??? Should wide strings be allowed when parser->translate_strings_p
4332 is false (i.e. in attributes)? If not, we can kill the third
4333 argument to cp_parser_string_literal. */
4334 return cp_parser_string_literal (parser,
4335 parser->translate_strings_p,
4336 true);
4338 case CPP_OPEN_PAREN:
4340 tree expr;
4341 bool saved_greater_than_is_operator_p;
4343 /* Consume the `('. */
4344 cp_lexer_consume_token (parser->lexer);
4345 /* Within a parenthesized expression, a `>' token is always
4346 the greater-than operator. */
4347 saved_greater_than_is_operator_p
4348 = parser->greater_than_is_operator_p;
4349 parser->greater_than_is_operator_p = true;
4350 /* If we see `( { ' then we are looking at the beginning of
4351 a GNU statement-expression. */
4352 if (cp_parser_allow_gnu_extensions_p (parser)
4353 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4355 /* Statement-expressions are not allowed by the standard. */
4356 pedwarn (token->location, OPT_Wpedantic,
4357 "ISO C++ forbids braced-groups within expressions");
4359 /* And they're not allowed outside of a function-body; you
4360 cannot, for example, write:
4362 int i = ({ int j = 3; j + 1; });
4364 at class or namespace scope. */
4365 if (!parser->in_function_body
4366 || parser->in_template_argument_list_p)
4368 error_at (token->location,
4369 "statement-expressions are not allowed outside "
4370 "functions nor in template-argument lists");
4371 cp_parser_skip_to_end_of_block_or_statement (parser);
4372 expr = error_mark_node;
4374 else
4376 /* Start the statement-expression. */
4377 expr = begin_stmt_expr ();
4378 /* Parse the compound-statement. */
4379 cp_parser_compound_statement (parser, expr, false, false);
4380 /* Finish up. */
4381 expr = finish_stmt_expr (expr, false);
4384 else
4386 /* Parse the parenthesized expression. */
4387 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4388 /* Let the front end know that this expression was
4389 enclosed in parentheses. This matters in case, for
4390 example, the expression is of the form `A::B', since
4391 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4392 not. */
4393 expr = finish_parenthesized_expr (expr);
4394 /* DR 705: Wrapping an unqualified name in parentheses
4395 suppresses arg-dependent lookup. We want to pass back
4396 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4397 (c++/37862), but none of the others. */
4398 if (*idk != CP_ID_KIND_QUALIFIED)
4399 *idk = CP_ID_KIND_NONE;
4401 /* The `>' token might be the end of a template-id or
4402 template-parameter-list now. */
4403 parser->greater_than_is_operator_p
4404 = saved_greater_than_is_operator_p;
4405 /* Consume the `)'. */
4406 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4407 cp_parser_skip_to_end_of_statement (parser);
4409 return expr;
4412 case CPP_OPEN_SQUARE:
4413 if (c_dialect_objc ())
4414 /* We have an Objective-C++ message. */
4415 return cp_parser_objc_expression (parser);
4417 tree lam = cp_parser_lambda_expression (parser);
4418 /* Don't warn about a failed tentative parse. */
4419 if (cp_parser_error_occurred (parser))
4420 return error_mark_node;
4421 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4422 return lam;
4425 case CPP_OBJC_STRING:
4426 if (c_dialect_objc ())
4427 /* We have an Objective-C++ string literal. */
4428 return cp_parser_objc_expression (parser);
4429 cp_parser_error (parser, "expected primary-expression");
4430 return error_mark_node;
4432 case CPP_KEYWORD:
4433 switch (token->keyword)
4435 /* These two are the boolean literals. */
4436 case RID_TRUE:
4437 cp_lexer_consume_token (parser->lexer);
4438 return boolean_true_node;
4439 case RID_FALSE:
4440 cp_lexer_consume_token (parser->lexer);
4441 return boolean_false_node;
4443 /* The `__null' literal. */
4444 case RID_NULL:
4445 cp_lexer_consume_token (parser->lexer);
4446 return null_node;
4448 /* The `nullptr' literal. */
4449 case RID_NULLPTR:
4450 cp_lexer_consume_token (parser->lexer);
4451 return nullptr_node;
4453 /* Recognize the `this' keyword. */
4454 case RID_THIS:
4455 cp_lexer_consume_token (parser->lexer);
4456 if (parser->local_variables_forbidden_p)
4458 error_at (token->location,
4459 "%<this%> may not be used in this context");
4460 return error_mark_node;
4462 /* Pointers cannot appear in constant-expressions. */
4463 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4464 return error_mark_node;
4465 return finish_this_expr ();
4467 /* The `operator' keyword can be the beginning of an
4468 id-expression. */
4469 case RID_OPERATOR:
4470 goto id_expression;
4472 case RID_FUNCTION_NAME:
4473 case RID_PRETTY_FUNCTION_NAME:
4474 case RID_C99_FUNCTION_NAME:
4476 non_integral_constant name;
4478 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4479 __func__ are the names of variables -- but they are
4480 treated specially. Therefore, they are handled here,
4481 rather than relying on the generic id-expression logic
4482 below. Grammatically, these names are id-expressions.
4484 Consume the token. */
4485 token = cp_lexer_consume_token (parser->lexer);
4487 switch (token->keyword)
4489 case RID_FUNCTION_NAME:
4490 name = NIC_FUNC_NAME;
4491 break;
4492 case RID_PRETTY_FUNCTION_NAME:
4493 name = NIC_PRETTY_FUNC;
4494 break;
4495 case RID_C99_FUNCTION_NAME:
4496 name = NIC_C99_FUNC;
4497 break;
4498 default:
4499 gcc_unreachable ();
4502 if (cp_parser_non_integral_constant_expression (parser, name))
4503 return error_mark_node;
4505 /* Look up the name. */
4506 return finish_fname (token->u.value);
4509 case RID_VA_ARG:
4511 tree expression;
4512 tree type;
4513 source_location type_location;
4515 /* The `__builtin_va_arg' construct is used to handle
4516 `va_arg'. Consume the `__builtin_va_arg' token. */
4517 cp_lexer_consume_token (parser->lexer);
4518 /* Look for the opening `('. */
4519 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4520 /* Now, parse the assignment-expression. */
4521 expression = cp_parser_assignment_expression (parser,
4522 /*cast_p=*/false, NULL);
4523 /* Look for the `,'. */
4524 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4525 type_location = cp_lexer_peek_token (parser->lexer)->location;
4526 /* Parse the type-id. */
4527 type = cp_parser_type_id (parser);
4528 /* Look for the closing `)'. */
4529 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4530 /* Using `va_arg' in a constant-expression is not
4531 allowed. */
4532 if (cp_parser_non_integral_constant_expression (parser,
4533 NIC_VA_ARG))
4534 return error_mark_node;
4535 return build_x_va_arg (type_location, expression, type);
4538 case RID_OFFSETOF:
4539 return cp_parser_builtin_offsetof (parser);
4541 case RID_HAS_NOTHROW_ASSIGN:
4542 case RID_HAS_NOTHROW_CONSTRUCTOR:
4543 case RID_HAS_NOTHROW_COPY:
4544 case RID_HAS_TRIVIAL_ASSIGN:
4545 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4546 case RID_HAS_TRIVIAL_COPY:
4547 case RID_HAS_TRIVIAL_DESTRUCTOR:
4548 case RID_HAS_VIRTUAL_DESTRUCTOR:
4549 case RID_IS_ABSTRACT:
4550 case RID_IS_BASE_OF:
4551 case RID_IS_CLASS:
4552 case RID_IS_CONVERTIBLE_TO:
4553 case RID_IS_EMPTY:
4554 case RID_IS_ENUM:
4555 case RID_IS_FINAL:
4556 case RID_IS_LITERAL_TYPE:
4557 case RID_IS_POD:
4558 case RID_IS_POLYMORPHIC:
4559 case RID_IS_SAME_AS:
4560 case RID_IS_STD_LAYOUT:
4561 case RID_IS_TRIVIAL:
4562 case RID_IS_UNION:
4563 return cp_parser_trait_expr (parser, token->keyword);
4565 // C++ concepts
4566 case RID_REQUIRES:
4567 return cp_parser_requires_expression (parser);
4569 /* Objective-C++ expressions. */
4570 case RID_AT_ENCODE:
4571 case RID_AT_PROTOCOL:
4572 case RID_AT_SELECTOR:
4573 return cp_parser_objc_expression (parser);
4575 case RID_TEMPLATE:
4576 if (parser->in_function_body
4577 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4578 == CPP_LESS))
4580 error_at (token->location,
4581 "a template declaration cannot appear at block scope");
4582 cp_parser_skip_to_end_of_block_or_statement (parser);
4583 return error_mark_node;
4585 default:
4586 cp_parser_error (parser, "expected primary-expression");
4587 return error_mark_node;
4590 /* An id-expression can start with either an identifier, a
4591 `::' as the beginning of a qualified-id, or the "operator"
4592 keyword. */
4593 case CPP_NAME:
4594 case CPP_SCOPE:
4595 case CPP_TEMPLATE_ID:
4596 case CPP_NESTED_NAME_SPECIFIER:
4598 tree id_expression;
4599 tree decl;
4600 const char *error_msg;
4601 bool template_p;
4602 bool done;
4603 cp_token *id_expr_token;
4605 id_expression:
4606 /* Parse the id-expression. */
4607 id_expression
4608 = cp_parser_id_expression (parser,
4609 /*template_keyword_p=*/false,
4610 /*check_dependency_p=*/true,
4611 &template_p,
4612 /*declarator_p=*/false,
4613 /*optional_p=*/false);
4614 if (id_expression == error_mark_node)
4615 return error_mark_node;
4616 id_expr_token = token;
4617 token = cp_lexer_peek_token (parser->lexer);
4618 done = (token->type != CPP_OPEN_SQUARE
4619 && token->type != CPP_OPEN_PAREN
4620 && token->type != CPP_DOT
4621 && token->type != CPP_DEREF
4622 && token->type != CPP_PLUS_PLUS
4623 && token->type != CPP_MINUS_MINUS);
4624 /* If we have a template-id, then no further lookup is
4625 required. If the template-id was for a template-class, we
4626 will sometimes have a TYPE_DECL at this point. */
4627 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4628 || TREE_CODE (id_expression) == TYPE_DECL)
4629 decl = id_expression;
4630 /* Look up the name. */
4631 else
4633 tree ambiguous_decls;
4635 /* If we already know that this lookup is ambiguous, then
4636 we've already issued an error message; there's no reason
4637 to check again. */
4638 if (id_expr_token->type == CPP_NAME
4639 && id_expr_token->error_reported)
4641 cp_parser_simulate_error (parser);
4642 return error_mark_node;
4645 decl = cp_parser_lookup_name (parser, id_expression,
4646 none_type,
4647 template_p,
4648 /*is_namespace=*/false,
4649 /*check_dependency=*/true,
4650 &ambiguous_decls,
4651 id_expr_token->location);
4652 /* If the lookup was ambiguous, an error will already have
4653 been issued. */
4654 if (ambiguous_decls)
4655 return error_mark_node;
4657 /* In Objective-C++, we may have an Objective-C 2.0
4658 dot-syntax for classes here. */
4659 if (c_dialect_objc ()
4660 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4661 && TREE_CODE (decl) == TYPE_DECL
4662 && objc_is_class_name (decl))
4664 tree component;
4665 cp_lexer_consume_token (parser->lexer);
4666 component = cp_parser_identifier (parser);
4667 if (component == error_mark_node)
4668 return error_mark_node;
4670 return objc_build_class_component_ref (id_expression, component);
4673 /* In Objective-C++, an instance variable (ivar) may be preferred
4674 to whatever cp_parser_lookup_name() found. */
4675 decl = objc_lookup_ivar (decl, id_expression);
4677 /* If name lookup gives us a SCOPE_REF, then the
4678 qualifying scope was dependent. */
4679 if (TREE_CODE (decl) == SCOPE_REF)
4681 /* At this point, we do not know if DECL is a valid
4682 integral constant expression. We assume that it is
4683 in fact such an expression, so that code like:
4685 template <int N> struct A {
4686 int a[B<N>::i];
4689 is accepted. At template-instantiation time, we
4690 will check that B<N>::i is actually a constant. */
4691 return decl;
4693 /* Check to see if DECL is a local variable in a context
4694 where that is forbidden. */
4695 if (parser->local_variables_forbidden_p
4696 && local_variable_p (decl))
4698 /* It might be that we only found DECL because we are
4699 trying to be generous with pre-ISO scoping rules.
4700 For example, consider:
4702 int i;
4703 void g() {
4704 for (int i = 0; i < 10; ++i) {}
4705 extern void f(int j = i);
4708 Here, name look up will originally find the out
4709 of scope `i'. We need to issue a warning message,
4710 but then use the global `i'. */
4711 decl = check_for_out_of_scope_variable (decl);
4712 if (local_variable_p (decl))
4714 error_at (id_expr_token->location,
4715 "local variable %qD may not appear in this context",
4716 decl);
4717 return error_mark_node;
4722 decl = (finish_id_expression
4723 (id_expression, decl, parser->scope,
4724 idk,
4725 parser->integral_constant_expression_p,
4726 parser->allow_non_integral_constant_expression_p,
4727 &parser->non_integral_constant_expression_p,
4728 template_p, done, address_p,
4729 template_arg_p,
4730 &error_msg,
4731 id_expr_token->location));
4732 if (error_msg)
4733 cp_parser_error (parser, error_msg);
4734 return decl;
4737 /* Anything else is an error. */
4738 default:
4739 cp_parser_error (parser, "expected primary-expression");
4740 return error_mark_node;
4744 static inline tree
4745 cp_parser_primary_expression (cp_parser *parser,
4746 bool address_p,
4747 bool cast_p,
4748 bool template_arg_p,
4749 cp_id_kind *idk)
4751 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4752 /*decltype*/false, idk);
4755 /* Parse an id-expression.
4757 id-expression:
4758 unqualified-id
4759 qualified-id
4761 qualified-id:
4762 :: [opt] nested-name-specifier template [opt] unqualified-id
4763 :: identifier
4764 :: operator-function-id
4765 :: template-id
4767 Return a representation of the unqualified portion of the
4768 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4769 a `::' or nested-name-specifier.
4771 Often, if the id-expression was a qualified-id, the caller will
4772 want to make a SCOPE_REF to represent the qualified-id. This
4773 function does not do this in order to avoid wastefully creating
4774 SCOPE_REFs when they are not required.
4776 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4777 `template' keyword.
4779 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4780 uninstantiated templates.
4782 If *TEMPLATE_P is non-NULL, it is set to true iff the
4783 `template' keyword is used to explicitly indicate that the entity
4784 named is a template.
4786 If DECLARATOR_P is true, the id-expression is appearing as part of
4787 a declarator, rather than as part of an expression. */
4789 static tree
4790 cp_parser_id_expression (cp_parser *parser,
4791 bool template_keyword_p,
4792 bool check_dependency_p,
4793 bool *template_p,
4794 bool declarator_p,
4795 bool optional_p)
4797 bool global_scope_p;
4798 bool nested_name_specifier_p;
4800 /* Assume the `template' keyword was not used. */
4801 if (template_p)
4802 *template_p = template_keyword_p;
4804 /* Look for the optional `::' operator. */
4805 global_scope_p
4806 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4807 != NULL_TREE);
4808 /* Look for the optional nested-name-specifier. */
4809 nested_name_specifier_p
4810 = (cp_parser_nested_name_specifier_opt (parser,
4811 /*typename_keyword_p=*/false,
4812 check_dependency_p,
4813 /*type_p=*/false,
4814 declarator_p)
4815 != NULL_TREE);
4816 /* If there is a nested-name-specifier, then we are looking at
4817 the first qualified-id production. */
4818 if (nested_name_specifier_p)
4820 tree saved_scope;
4821 tree saved_object_scope;
4822 tree saved_qualifying_scope;
4823 tree unqualified_id;
4824 bool is_template;
4826 /* See if the next token is the `template' keyword. */
4827 if (!template_p)
4828 template_p = &is_template;
4829 *template_p = cp_parser_optional_template_keyword (parser);
4830 /* Name lookup we do during the processing of the
4831 unqualified-id might obliterate SCOPE. */
4832 saved_scope = parser->scope;
4833 saved_object_scope = parser->object_scope;
4834 saved_qualifying_scope = parser->qualifying_scope;
4835 /* Process the final unqualified-id. */
4836 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4837 check_dependency_p,
4838 declarator_p,
4839 /*optional_p=*/false);
4840 /* Restore the SAVED_SCOPE for our caller. */
4841 parser->scope = saved_scope;
4842 parser->object_scope = saved_object_scope;
4843 parser->qualifying_scope = saved_qualifying_scope;
4845 return unqualified_id;
4847 /* Otherwise, if we are in global scope, then we are looking at one
4848 of the other qualified-id productions. */
4849 else if (global_scope_p)
4851 cp_token *token;
4852 tree id;
4854 /* Peek at the next token. */
4855 token = cp_lexer_peek_token (parser->lexer);
4857 /* If it's an identifier, and the next token is not a "<", then
4858 we can avoid the template-id case. This is an optimization
4859 for this common case. */
4860 if (token->type == CPP_NAME
4861 && !cp_parser_nth_token_starts_template_argument_list_p
4862 (parser, 2))
4863 return cp_parser_identifier (parser);
4865 cp_parser_parse_tentatively (parser);
4866 /* Try a template-id. */
4867 id = cp_parser_template_id (parser,
4868 /*template_keyword_p=*/false,
4869 /*check_dependency_p=*/true,
4870 none_type,
4871 declarator_p);
4872 /* If that worked, we're done. */
4873 if (cp_parser_parse_definitely (parser))
4874 return id;
4876 /* Peek at the next token. (Changes in the token buffer may
4877 have invalidated the pointer obtained above.) */
4878 token = cp_lexer_peek_token (parser->lexer);
4880 switch (token->type)
4882 case CPP_NAME:
4883 return cp_parser_identifier (parser);
4885 case CPP_KEYWORD:
4886 if (token->keyword == RID_OPERATOR)
4887 return cp_parser_operator_function_id (parser);
4888 /* Fall through. */
4890 default:
4891 cp_parser_error (parser, "expected id-expression");
4892 return error_mark_node;
4895 else
4896 return cp_parser_unqualified_id (parser, template_keyword_p,
4897 /*check_dependency_p=*/true,
4898 declarator_p,
4899 optional_p);
4902 /* Parse an unqualified-id.
4904 unqualified-id:
4905 identifier
4906 operator-function-id
4907 conversion-function-id
4908 ~ class-name
4909 template-id
4911 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4912 keyword, in a construct like `A::template ...'.
4914 Returns a representation of unqualified-id. For the `identifier'
4915 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4916 production a BIT_NOT_EXPR is returned; the operand of the
4917 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4918 other productions, see the documentation accompanying the
4919 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4920 names are looked up in uninstantiated templates. If DECLARATOR_P
4921 is true, the unqualified-id is appearing as part of a declarator,
4922 rather than as part of an expression. */
4924 static tree
4925 cp_parser_unqualified_id (cp_parser* parser,
4926 bool template_keyword_p,
4927 bool check_dependency_p,
4928 bool declarator_p,
4929 bool optional_p)
4931 cp_token *token;
4933 /* Peek at the next token. */
4934 token = cp_lexer_peek_token (parser->lexer);
4936 switch (token->type)
4938 case CPP_NAME:
4940 tree id;
4942 /* We don't know yet whether or not this will be a
4943 template-id. */
4944 cp_parser_parse_tentatively (parser);
4945 /* Try a template-id. */
4946 id = cp_parser_template_id (parser, template_keyword_p,
4947 check_dependency_p,
4948 none_type,
4949 declarator_p);
4950 /* If it worked, we're done. */
4951 if (cp_parser_parse_definitely (parser))
4952 return id;
4953 /* Otherwise, it's an ordinary identifier. */
4954 return cp_parser_identifier (parser);
4957 case CPP_TEMPLATE_ID:
4958 return cp_parser_template_id (parser, template_keyword_p,
4959 check_dependency_p,
4960 none_type,
4961 declarator_p);
4963 case CPP_COMPL:
4965 tree type_decl;
4966 tree qualifying_scope;
4967 tree object_scope;
4968 tree scope;
4969 bool done;
4971 /* Consume the `~' token. */
4972 cp_lexer_consume_token (parser->lexer);
4973 /* Parse the class-name. The standard, as written, seems to
4974 say that:
4976 template <typename T> struct S { ~S (); };
4977 template <typename T> S<T>::~S() {}
4979 is invalid, since `~' must be followed by a class-name, but
4980 `S<T>' is dependent, and so not known to be a class.
4981 That's not right; we need to look in uninstantiated
4982 templates. A further complication arises from:
4984 template <typename T> void f(T t) {
4985 t.T::~T();
4988 Here, it is not possible to look up `T' in the scope of `T'
4989 itself. We must look in both the current scope, and the
4990 scope of the containing complete expression.
4992 Yet another issue is:
4994 struct S {
4995 int S;
4996 ~S();
4999 S::~S() {}
5001 The standard does not seem to say that the `S' in `~S'
5002 should refer to the type `S' and not the data member
5003 `S::S'. */
5005 /* DR 244 says that we look up the name after the "~" in the
5006 same scope as we looked up the qualifying name. That idea
5007 isn't fully worked out; it's more complicated than that. */
5008 scope = parser->scope;
5009 object_scope = parser->object_scope;
5010 qualifying_scope = parser->qualifying_scope;
5012 /* Check for invalid scopes. */
5013 if (scope == error_mark_node)
5015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5016 cp_lexer_consume_token (parser->lexer);
5017 return error_mark_node;
5019 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5021 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5022 error_at (token->location,
5023 "scope %qT before %<~%> is not a class-name",
5024 scope);
5025 cp_parser_simulate_error (parser);
5026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5027 cp_lexer_consume_token (parser->lexer);
5028 return error_mark_node;
5030 gcc_assert (!scope || TYPE_P (scope));
5032 /* If the name is of the form "X::~X" it's OK even if X is a
5033 typedef. */
5034 token = cp_lexer_peek_token (parser->lexer);
5035 if (scope
5036 && token->type == CPP_NAME
5037 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5038 != CPP_LESS)
5039 && (token->u.value == TYPE_IDENTIFIER (scope)
5040 || (CLASS_TYPE_P (scope)
5041 && constructor_name_p (token->u.value, scope))))
5043 cp_lexer_consume_token (parser->lexer);
5044 return build_nt (BIT_NOT_EXPR, scope);
5047 /* ~auto means the destructor of whatever the object is. */
5048 if (cp_parser_is_keyword (token, RID_AUTO))
5050 if (cxx_dialect < cxx1y)
5051 pedwarn (input_location, 0,
5052 "%<~auto%> only available with "
5053 "-std=c++1y or -std=gnu++1y");
5054 cp_lexer_consume_token (parser->lexer);
5055 return build_nt (BIT_NOT_EXPR, make_auto ());
5058 /* If there was an explicit qualification (S::~T), first look
5059 in the scope given by the qualification (i.e., S).
5061 Note: in the calls to cp_parser_class_name below we pass
5062 typename_type so that lookup finds the injected-class-name
5063 rather than the constructor. */
5064 done = false;
5065 type_decl = NULL_TREE;
5066 if (scope)
5068 cp_parser_parse_tentatively (parser);
5069 type_decl = cp_parser_class_name (parser,
5070 /*typename_keyword_p=*/false,
5071 /*template_keyword_p=*/false,
5072 typename_type,
5073 /*check_dependency=*/false,
5074 /*class_head_p=*/false,
5075 declarator_p);
5076 if (cp_parser_parse_definitely (parser))
5077 done = true;
5079 /* In "N::S::~S", look in "N" as well. */
5080 if (!done && scope && qualifying_scope)
5082 cp_parser_parse_tentatively (parser);
5083 parser->scope = qualifying_scope;
5084 parser->object_scope = NULL_TREE;
5085 parser->qualifying_scope = NULL_TREE;
5086 type_decl
5087 = cp_parser_class_name (parser,
5088 /*typename_keyword_p=*/false,
5089 /*template_keyword_p=*/false,
5090 typename_type,
5091 /*check_dependency=*/false,
5092 /*class_head_p=*/false,
5093 declarator_p);
5094 if (cp_parser_parse_definitely (parser))
5095 done = true;
5097 /* In "p->S::~T", look in the scope given by "*p" as well. */
5098 else if (!done && object_scope)
5100 cp_parser_parse_tentatively (parser);
5101 parser->scope = object_scope;
5102 parser->object_scope = NULL_TREE;
5103 parser->qualifying_scope = NULL_TREE;
5104 type_decl
5105 = cp_parser_class_name (parser,
5106 /*typename_keyword_p=*/false,
5107 /*template_keyword_p=*/false,
5108 typename_type,
5109 /*check_dependency=*/false,
5110 /*class_head_p=*/false,
5111 declarator_p);
5112 if (cp_parser_parse_definitely (parser))
5113 done = true;
5115 /* Look in the surrounding context. */
5116 if (!done)
5118 parser->scope = NULL_TREE;
5119 parser->object_scope = NULL_TREE;
5120 parser->qualifying_scope = NULL_TREE;
5121 if (processing_template_decl)
5122 cp_parser_parse_tentatively (parser);
5123 type_decl
5124 = cp_parser_class_name (parser,
5125 /*typename_keyword_p=*/false,
5126 /*template_keyword_p=*/false,
5127 typename_type,
5128 /*check_dependency=*/false,
5129 /*class_head_p=*/false,
5130 declarator_p);
5131 if (processing_template_decl
5132 && ! cp_parser_parse_definitely (parser))
5134 /* We couldn't find a type with this name, so just accept
5135 it and check for a match at instantiation time. */
5136 type_decl = cp_parser_identifier (parser);
5137 if (type_decl != error_mark_node)
5138 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5139 return type_decl;
5142 /* If an error occurred, assume that the name of the
5143 destructor is the same as the name of the qualifying
5144 class. That allows us to keep parsing after running
5145 into ill-formed destructor names. */
5146 if (type_decl == error_mark_node && scope)
5147 return build_nt (BIT_NOT_EXPR, scope);
5148 else if (type_decl == error_mark_node)
5149 return error_mark_node;
5151 /* Check that destructor name and scope match. */
5152 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5154 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5155 error_at (token->location,
5156 "declaration of %<~%T%> as member of %qT",
5157 type_decl, scope);
5158 cp_parser_simulate_error (parser);
5159 return error_mark_node;
5162 /* [class.dtor]
5164 A typedef-name that names a class shall not be used as the
5165 identifier in the declarator for a destructor declaration. */
5166 if (declarator_p
5167 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5168 && !DECL_SELF_REFERENCE_P (type_decl)
5169 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5170 error_at (token->location,
5171 "typedef-name %qD used as destructor declarator",
5172 type_decl);
5174 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5177 case CPP_KEYWORD:
5178 if (token->keyword == RID_OPERATOR)
5180 tree id;
5182 /* This could be a template-id, so we try that first. */
5183 cp_parser_parse_tentatively (parser);
5184 /* Try a template-id. */
5185 id = cp_parser_template_id (parser, template_keyword_p,
5186 /*check_dependency_p=*/true,
5187 none_type,
5188 declarator_p);
5189 /* If that worked, we're done. */
5190 if (cp_parser_parse_definitely (parser))
5191 return id;
5192 /* We still don't know whether we're looking at an
5193 operator-function-id or a conversion-function-id. */
5194 cp_parser_parse_tentatively (parser);
5195 /* Try an operator-function-id. */
5196 id = cp_parser_operator_function_id (parser);
5197 /* If that didn't work, try a conversion-function-id. */
5198 if (!cp_parser_parse_definitely (parser))
5199 id = cp_parser_conversion_function_id (parser);
5200 else if (UDLIT_OPER_P (id))
5202 /* 17.6.3.3.5 */
5203 const char *name = UDLIT_OP_SUFFIX (id);
5204 if (name[0] != '_' && !in_system_header_at (input_location)
5205 && declarator_p)
5206 warning (0, "literal operator suffixes not preceded by %<_%>"
5207 " are reserved for future standardization");
5210 return id;
5212 /* Fall through. */
5214 default:
5215 if (optional_p)
5216 return NULL_TREE;
5217 cp_parser_error (parser, "expected unqualified-id");
5218 return error_mark_node;
5222 /* Parse an (optional) nested-name-specifier.
5224 nested-name-specifier: [C++98]
5225 class-or-namespace-name :: nested-name-specifier [opt]
5226 class-or-namespace-name :: template nested-name-specifier [opt]
5228 nested-name-specifier: [C++0x]
5229 type-name ::
5230 namespace-name ::
5231 nested-name-specifier identifier ::
5232 nested-name-specifier template [opt] simple-template-id ::
5234 PARSER->SCOPE should be set appropriately before this function is
5235 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5236 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5237 in name lookups.
5239 Sets PARSER->SCOPE to the class (TYPE) or namespace
5240 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5241 it unchanged if there is no nested-name-specifier. Returns the new
5242 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5244 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5245 part of a declaration and/or decl-specifier. */
5247 static tree
5248 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5249 bool typename_keyword_p,
5250 bool check_dependency_p,
5251 bool type_p,
5252 bool is_declaration)
5254 bool success = false;
5255 cp_token_position start = 0;
5256 cp_token *token;
5258 /* Remember where the nested-name-specifier starts. */
5259 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5261 start = cp_lexer_token_position (parser->lexer, false);
5262 push_deferring_access_checks (dk_deferred);
5265 while (true)
5267 tree new_scope;
5268 tree old_scope;
5269 tree saved_qualifying_scope;
5270 bool template_keyword_p;
5272 /* Spot cases that cannot be the beginning of a
5273 nested-name-specifier. */
5274 token = cp_lexer_peek_token (parser->lexer);
5276 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5277 the already parsed nested-name-specifier. */
5278 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5280 /* Grab the nested-name-specifier and continue the loop. */
5281 cp_parser_pre_parsed_nested_name_specifier (parser);
5282 /* If we originally encountered this nested-name-specifier
5283 with IS_DECLARATION set to false, we will not have
5284 resolved TYPENAME_TYPEs, so we must do so here. */
5285 if (is_declaration
5286 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5288 new_scope = resolve_typename_type (parser->scope,
5289 /*only_current_p=*/false);
5290 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5291 parser->scope = new_scope;
5293 success = true;
5294 continue;
5297 /* Spot cases that cannot be the beginning of a
5298 nested-name-specifier. On the second and subsequent times
5299 through the loop, we look for the `template' keyword. */
5300 if (success && token->keyword == RID_TEMPLATE)
5302 /* A template-id can start a nested-name-specifier. */
5303 else if (token->type == CPP_TEMPLATE_ID)
5305 /* DR 743: decltype can be used in a nested-name-specifier. */
5306 else if (token_is_decltype (token))
5308 else
5310 /* If the next token is not an identifier, then it is
5311 definitely not a type-name or namespace-name. */
5312 if (token->type != CPP_NAME)
5313 break;
5314 /* If the following token is neither a `<' (to begin a
5315 template-id), nor a `::', then we are not looking at a
5316 nested-name-specifier. */
5317 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5319 if (token->type == CPP_COLON
5320 && parser->colon_corrects_to_scope_p
5321 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5323 error_at (token->location,
5324 "found %<:%> in nested-name-specifier, expected %<::%>");
5325 token->type = CPP_SCOPE;
5328 if (token->type != CPP_SCOPE
5329 && !cp_parser_nth_token_starts_template_argument_list_p
5330 (parser, 2))
5331 break;
5334 /* The nested-name-specifier is optional, so we parse
5335 tentatively. */
5336 cp_parser_parse_tentatively (parser);
5338 /* Look for the optional `template' keyword, if this isn't the
5339 first time through the loop. */
5340 if (success)
5341 template_keyword_p = cp_parser_optional_template_keyword (parser);
5342 else
5343 template_keyword_p = false;
5345 /* Save the old scope since the name lookup we are about to do
5346 might destroy it. */
5347 old_scope = parser->scope;
5348 saved_qualifying_scope = parser->qualifying_scope;
5349 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5350 look up names in "X<T>::I" in order to determine that "Y" is
5351 a template. So, if we have a typename at this point, we make
5352 an effort to look through it. */
5353 if (is_declaration
5354 && !typename_keyword_p
5355 && parser->scope
5356 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5357 parser->scope = resolve_typename_type (parser->scope,
5358 /*only_current_p=*/false);
5359 /* Parse the qualifying entity. */
5360 new_scope
5361 = cp_parser_qualifying_entity (parser,
5362 typename_keyword_p,
5363 template_keyword_p,
5364 check_dependency_p,
5365 type_p,
5366 is_declaration);
5367 /* Look for the `::' token. */
5368 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5370 /* If we found what we wanted, we keep going; otherwise, we're
5371 done. */
5372 if (!cp_parser_parse_definitely (parser))
5374 bool error_p = false;
5376 /* Restore the OLD_SCOPE since it was valid before the
5377 failed attempt at finding the last
5378 class-or-namespace-name. */
5379 parser->scope = old_scope;
5380 parser->qualifying_scope = saved_qualifying_scope;
5382 /* If the next token is a decltype, and the one after that is a
5383 `::', then the decltype has failed to resolve to a class or
5384 enumeration type. Give this error even when parsing
5385 tentatively since it can't possibly be valid--and we're going
5386 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5387 won't get another chance.*/
5388 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5389 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5390 == CPP_SCOPE))
5392 token = cp_lexer_consume_token (parser->lexer);
5393 error_at (token->location, "decltype evaluates to %qT, "
5394 "which is not a class or enumeration type",
5395 token->u.value);
5396 parser->scope = error_mark_node;
5397 error_p = true;
5398 /* As below. */
5399 success = true;
5400 cp_lexer_consume_token (parser->lexer);
5403 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5404 break;
5405 /* If the next token is an identifier, and the one after
5406 that is a `::', then any valid interpretation would have
5407 found a class-or-namespace-name. */
5408 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5409 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5410 == CPP_SCOPE)
5411 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5412 != CPP_COMPL))
5414 token = cp_lexer_consume_token (parser->lexer);
5415 if (!error_p)
5417 if (!token->error_reported)
5419 tree decl;
5420 tree ambiguous_decls;
5422 decl = cp_parser_lookup_name (parser, token->u.value,
5423 none_type,
5424 /*is_template=*/false,
5425 /*is_namespace=*/false,
5426 /*check_dependency=*/true,
5427 &ambiguous_decls,
5428 token->location);
5429 if (TREE_CODE (decl) == TEMPLATE_DECL)
5430 error_at (token->location,
5431 "%qD used without template parameters",
5432 decl);
5433 else if (ambiguous_decls)
5435 // cp_parser_lookup_name has the same diagnostic,
5436 // thus make sure to emit it at most once.
5437 if (cp_parser_uncommitted_to_tentative_parse_p
5438 (parser))
5440 error_at (token->location,
5441 "reference to %qD is ambiguous",
5442 token->u.value);
5443 print_candidates (ambiguous_decls);
5445 decl = error_mark_node;
5447 else
5449 if (cxx_dialect != cxx98)
5450 cp_parser_name_lookup_error
5451 (parser, token->u.value, decl, NLE_NOT_CXX98,
5452 token->location);
5453 else
5454 cp_parser_name_lookup_error
5455 (parser, token->u.value, decl, NLE_CXX98,
5456 token->location);
5459 parser->scope = error_mark_node;
5460 error_p = true;
5461 /* Treat this as a successful nested-name-specifier
5462 due to:
5464 [basic.lookup.qual]
5466 If the name found is not a class-name (clause
5467 _class_) or namespace-name (_namespace.def_), the
5468 program is ill-formed. */
5469 success = true;
5471 cp_lexer_consume_token (parser->lexer);
5473 break;
5475 /* We've found one valid nested-name-specifier. */
5476 success = true;
5477 /* Name lookup always gives us a DECL. */
5478 if (TREE_CODE (new_scope) == TYPE_DECL)
5479 new_scope = TREE_TYPE (new_scope);
5480 /* Uses of "template" must be followed by actual templates. */
5481 if (template_keyword_p
5482 && !(CLASS_TYPE_P (new_scope)
5483 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5484 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5485 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5486 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5487 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5488 == TEMPLATE_ID_EXPR)))
5489 permerror (input_location, TYPE_P (new_scope)
5490 ? G_("%qT is not a template")
5491 : G_("%qD is not a template"),
5492 new_scope);
5493 /* If it is a class scope, try to complete it; we are about to
5494 be looking up names inside the class. */
5495 if (TYPE_P (new_scope)
5496 /* Since checking types for dependency can be expensive,
5497 avoid doing it if the type is already complete. */
5498 && !COMPLETE_TYPE_P (new_scope)
5499 /* Do not try to complete dependent types. */
5500 && !dependent_type_p (new_scope))
5502 new_scope = complete_type (new_scope);
5503 /* If it is a typedef to current class, use the current
5504 class instead, as the typedef won't have any names inside
5505 it yet. */
5506 if (!COMPLETE_TYPE_P (new_scope)
5507 && currently_open_class (new_scope))
5508 new_scope = TYPE_MAIN_VARIANT (new_scope);
5510 /* Make sure we look in the right scope the next time through
5511 the loop. */
5512 parser->scope = new_scope;
5515 /* If parsing tentatively, replace the sequence of tokens that makes
5516 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5517 token. That way, should we re-parse the token stream, we will
5518 not have to repeat the effort required to do the parse, nor will
5519 we issue duplicate error messages. */
5520 if (success && start)
5522 cp_token *token;
5524 token = cp_lexer_token_at (parser->lexer, start);
5525 /* Reset the contents of the START token. */
5526 token->type = CPP_NESTED_NAME_SPECIFIER;
5527 /* Retrieve any deferred checks. Do not pop this access checks yet
5528 so the memory will not be reclaimed during token replacing below. */
5529 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5530 token->u.tree_check_value->value = parser->scope;
5531 token->u.tree_check_value->checks = get_deferred_access_checks ();
5532 token->u.tree_check_value->qualifying_scope =
5533 parser->qualifying_scope;
5534 token->keyword = RID_MAX;
5536 /* Purge all subsequent tokens. */
5537 cp_lexer_purge_tokens_after (parser->lexer, start);
5540 if (start)
5541 pop_to_parent_deferring_access_checks ();
5543 return success ? parser->scope : NULL_TREE;
5546 /* Parse a nested-name-specifier. See
5547 cp_parser_nested_name_specifier_opt for details. This function
5548 behaves identically, except that it will an issue an error if no
5549 nested-name-specifier is present. */
5551 static tree
5552 cp_parser_nested_name_specifier (cp_parser *parser,
5553 bool typename_keyword_p,
5554 bool check_dependency_p,
5555 bool type_p,
5556 bool is_declaration)
5558 tree scope;
5560 /* Look for the nested-name-specifier. */
5561 scope = cp_parser_nested_name_specifier_opt (parser,
5562 typename_keyword_p,
5563 check_dependency_p,
5564 type_p,
5565 is_declaration);
5566 /* If it was not present, issue an error message. */
5567 if (!scope)
5569 cp_parser_error (parser, "expected nested-name-specifier");
5570 parser->scope = NULL_TREE;
5573 return scope;
5576 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5577 this is either a class-name or a namespace-name (which corresponds
5578 to the class-or-namespace-name production in the grammar). For
5579 C++0x, it can also be a type-name that refers to an enumeration
5580 type or a simple-template-id.
5582 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5583 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5584 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5585 TYPE_P is TRUE iff the next name should be taken as a class-name,
5586 even the same name is declared to be another entity in the same
5587 scope.
5589 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5590 specified by the class-or-namespace-name. If neither is found the
5591 ERROR_MARK_NODE is returned. */
5593 static tree
5594 cp_parser_qualifying_entity (cp_parser *parser,
5595 bool typename_keyword_p,
5596 bool template_keyword_p,
5597 bool check_dependency_p,
5598 bool type_p,
5599 bool is_declaration)
5601 tree saved_scope;
5602 tree saved_qualifying_scope;
5603 tree saved_object_scope;
5604 tree scope;
5605 bool only_class_p;
5606 bool successful_parse_p;
5608 /* DR 743: decltype can appear in a nested-name-specifier. */
5609 if (cp_lexer_next_token_is_decltype (parser->lexer))
5611 scope = cp_parser_decltype (parser);
5612 if (TREE_CODE (scope) != ENUMERAL_TYPE
5613 && !MAYBE_CLASS_TYPE_P (scope))
5615 cp_parser_simulate_error (parser);
5616 return error_mark_node;
5618 if (TYPE_NAME (scope))
5619 scope = TYPE_NAME (scope);
5620 return scope;
5623 /* Before we try to parse the class-name, we must save away the
5624 current PARSER->SCOPE since cp_parser_class_name will destroy
5625 it. */
5626 saved_scope = parser->scope;
5627 saved_qualifying_scope = parser->qualifying_scope;
5628 saved_object_scope = parser->object_scope;
5629 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5630 there is no need to look for a namespace-name. */
5631 only_class_p = template_keyword_p
5632 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5633 if (!only_class_p)
5634 cp_parser_parse_tentatively (parser);
5635 scope = cp_parser_class_name (parser,
5636 typename_keyword_p,
5637 template_keyword_p,
5638 type_p ? class_type : none_type,
5639 check_dependency_p,
5640 /*class_head_p=*/false,
5641 is_declaration);
5642 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5643 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5644 if (!only_class_p
5645 && cxx_dialect != cxx98
5646 && !successful_parse_p)
5648 /* Restore the saved scope. */
5649 parser->scope = saved_scope;
5650 parser->qualifying_scope = saved_qualifying_scope;
5651 parser->object_scope = saved_object_scope;
5653 /* Parse tentatively. */
5654 cp_parser_parse_tentatively (parser);
5656 /* Parse a type-name */
5657 scope = cp_parser_type_name (parser);
5659 /* "If the name found does not designate a namespace or a class,
5660 enumeration, or dependent type, the program is ill-formed."
5662 We cover classes and dependent types above and namespaces below,
5663 so this code is only looking for enums. */
5664 if (!scope || TREE_CODE (scope) != TYPE_DECL
5665 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5666 cp_parser_simulate_error (parser);
5668 successful_parse_p = cp_parser_parse_definitely (parser);
5670 /* If that didn't work, try for a namespace-name. */
5671 if (!only_class_p && !successful_parse_p)
5673 /* Restore the saved scope. */
5674 parser->scope = saved_scope;
5675 parser->qualifying_scope = saved_qualifying_scope;
5676 parser->object_scope = saved_object_scope;
5677 /* If we are not looking at an identifier followed by the scope
5678 resolution operator, then this is not part of a
5679 nested-name-specifier. (Note that this function is only used
5680 to parse the components of a nested-name-specifier.) */
5681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5682 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5683 return error_mark_node;
5684 scope = cp_parser_namespace_name (parser);
5687 return scope;
5690 /* Return true if we are looking at a compound-literal, false otherwise. */
5692 static bool
5693 cp_parser_compound_literal_p (cp_parser *parser)
5695 /* Consume the `('. */
5696 cp_lexer_consume_token (parser->lexer);
5698 cp_lexer_save_tokens (parser->lexer);
5700 /* Skip tokens until the next token is a closing parenthesis.
5701 If we find the closing `)', and the next token is a `{', then
5702 we are looking at a compound-literal. */
5703 bool compound_literal_p
5704 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5705 /*consume_paren=*/true)
5706 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5708 /* Roll back the tokens we skipped. */
5709 cp_lexer_rollback_tokens (parser->lexer);
5711 return compound_literal_p;
5714 /* Parse a postfix-expression.
5716 postfix-expression:
5717 primary-expression
5718 postfix-expression [ expression ]
5719 postfix-expression ( expression-list [opt] )
5720 simple-type-specifier ( expression-list [opt] )
5721 typename :: [opt] nested-name-specifier identifier
5722 ( expression-list [opt] )
5723 typename :: [opt] nested-name-specifier template [opt] template-id
5724 ( expression-list [opt] )
5725 postfix-expression . template [opt] id-expression
5726 postfix-expression -> template [opt] id-expression
5727 postfix-expression . pseudo-destructor-name
5728 postfix-expression -> pseudo-destructor-name
5729 postfix-expression ++
5730 postfix-expression --
5731 dynamic_cast < type-id > ( expression )
5732 static_cast < type-id > ( expression )
5733 reinterpret_cast < type-id > ( expression )
5734 const_cast < type-id > ( expression )
5735 typeid ( expression )
5736 typeid ( type-id )
5738 GNU Extension:
5740 postfix-expression:
5741 ( type-id ) { initializer-list , [opt] }
5743 This extension is a GNU version of the C99 compound-literal
5744 construct. (The C99 grammar uses `type-name' instead of `type-id',
5745 but they are essentially the same concept.)
5747 If ADDRESS_P is true, the postfix expression is the operand of the
5748 `&' operator. CAST_P is true if this expression is the target of a
5749 cast.
5751 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5752 class member access expressions [expr.ref].
5754 Returns a representation of the expression. */
5756 static tree
5757 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5758 bool member_access_only_p, bool decltype_p,
5759 cp_id_kind * pidk_return)
5761 cp_token *token;
5762 location_t loc;
5763 enum rid keyword;
5764 cp_id_kind idk = CP_ID_KIND_NONE;
5765 tree postfix_expression = NULL_TREE;
5766 bool is_member_access = false;
5767 int saved_in_statement = -1;
5769 /* Peek at the next token. */
5770 token = cp_lexer_peek_token (parser->lexer);
5771 loc = token->location;
5772 /* Some of the productions are determined by keywords. */
5773 keyword = token->keyword;
5774 switch (keyword)
5776 case RID_DYNCAST:
5777 case RID_STATCAST:
5778 case RID_REINTCAST:
5779 case RID_CONSTCAST:
5781 tree type;
5782 tree expression;
5783 const char *saved_message;
5784 bool saved_in_type_id_in_expr_p;
5786 /* All of these can be handled in the same way from the point
5787 of view of parsing. Begin by consuming the token
5788 identifying the cast. */
5789 cp_lexer_consume_token (parser->lexer);
5791 /* New types cannot be defined in the cast. */
5792 saved_message = parser->type_definition_forbidden_message;
5793 parser->type_definition_forbidden_message
5794 = G_("types may not be defined in casts");
5796 /* Look for the opening `<'. */
5797 cp_parser_require (parser, CPP_LESS, RT_LESS);
5798 /* Parse the type to which we are casting. */
5799 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5800 parser->in_type_id_in_expr_p = true;
5801 type = cp_parser_type_id (parser);
5802 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5803 /* Look for the closing `>'. */
5804 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5805 /* Restore the old message. */
5806 parser->type_definition_forbidden_message = saved_message;
5808 bool saved_greater_than_is_operator_p
5809 = parser->greater_than_is_operator_p;
5810 parser->greater_than_is_operator_p = true;
5812 /* And the expression which is being cast. */
5813 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5814 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5815 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5817 parser->greater_than_is_operator_p
5818 = saved_greater_than_is_operator_p;
5820 /* Only type conversions to integral or enumeration types
5821 can be used in constant-expressions. */
5822 if (!cast_valid_in_integral_constant_expression_p (type)
5823 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5824 return error_mark_node;
5826 switch (keyword)
5828 case RID_DYNCAST:
5829 postfix_expression
5830 = build_dynamic_cast (type, expression, tf_warning_or_error);
5831 break;
5832 case RID_STATCAST:
5833 postfix_expression
5834 = build_static_cast (type, expression, tf_warning_or_error);
5835 break;
5836 case RID_REINTCAST:
5837 postfix_expression
5838 = build_reinterpret_cast (type, expression,
5839 tf_warning_or_error);
5840 break;
5841 case RID_CONSTCAST:
5842 postfix_expression
5843 = build_const_cast (type, expression, tf_warning_or_error);
5844 break;
5845 default:
5846 gcc_unreachable ();
5849 break;
5851 case RID_TYPEID:
5853 tree type;
5854 const char *saved_message;
5855 bool saved_in_type_id_in_expr_p;
5857 /* Consume the `typeid' token. */
5858 cp_lexer_consume_token (parser->lexer);
5859 /* Look for the `(' token. */
5860 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5861 /* Types cannot be defined in a `typeid' expression. */
5862 saved_message = parser->type_definition_forbidden_message;
5863 parser->type_definition_forbidden_message
5864 = G_("types may not be defined in a %<typeid%> expression");
5865 /* We can't be sure yet whether we're looking at a type-id or an
5866 expression. */
5867 cp_parser_parse_tentatively (parser);
5868 /* Try a type-id first. */
5869 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5870 parser->in_type_id_in_expr_p = true;
5871 type = cp_parser_type_id (parser);
5872 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5873 /* Look for the `)' token. Otherwise, we can't be sure that
5874 we're not looking at an expression: consider `typeid (int
5875 (3))', for example. */
5876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5877 /* If all went well, simply lookup the type-id. */
5878 if (cp_parser_parse_definitely (parser))
5879 postfix_expression = get_typeid (type, tf_warning_or_error);
5880 /* Otherwise, fall back to the expression variant. */
5881 else
5883 tree expression;
5885 /* Look for an expression. */
5886 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5887 /* Compute its typeid. */
5888 postfix_expression = build_typeid (expression, tf_warning_or_error);
5889 /* Look for the `)' token. */
5890 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5892 /* Restore the saved message. */
5893 parser->type_definition_forbidden_message = saved_message;
5894 /* `typeid' may not appear in an integral constant expression. */
5895 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5896 return error_mark_node;
5898 break;
5900 case RID_TYPENAME:
5902 tree type;
5903 /* The syntax permitted here is the same permitted for an
5904 elaborated-type-specifier. */
5905 type = cp_parser_elaborated_type_specifier (parser,
5906 /*is_friend=*/false,
5907 /*is_declaration=*/false);
5908 postfix_expression = cp_parser_functional_cast (parser, type);
5910 break;
5912 case RID_CILK_SPAWN:
5914 cp_lexer_consume_token (parser->lexer);
5915 token = cp_lexer_peek_token (parser->lexer);
5916 if (token->type == CPP_SEMICOLON)
5918 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5919 "an expression");
5920 postfix_expression = error_mark_node;
5921 break;
5923 else if (!current_function_decl)
5925 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5926 "inside a function");
5927 postfix_expression = error_mark_node;
5928 break;
5930 else
5932 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5933 saved_in_statement = parser->in_statement;
5934 parser->in_statement |= IN_CILK_SPAWN;
5936 cfun->calls_cilk_spawn = 1;
5937 postfix_expression =
5938 cp_parser_postfix_expression (parser, false, false,
5939 false, false, &idk);
5940 if (!flag_cilkplus)
5942 error_at (token->location, "-fcilkplus must be enabled to use"
5943 " %<_Cilk_spawn%>");
5944 cfun->calls_cilk_spawn = 0;
5946 else if (saved_in_statement & IN_CILK_SPAWN)
5948 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5949 "are not permitted");
5950 postfix_expression = error_mark_node;
5951 cfun->calls_cilk_spawn = 0;
5953 else
5955 postfix_expression = build_cilk_spawn (token->location,
5956 postfix_expression);
5957 if (postfix_expression != error_mark_node)
5958 SET_EXPR_LOCATION (postfix_expression, input_location);
5959 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5961 break;
5964 case RID_BUILTIN_SHUFFLE:
5966 vec<tree, va_gc> *vec;
5967 unsigned int i;
5968 tree p;
5970 cp_lexer_consume_token (parser->lexer);
5971 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5972 /*cast_p=*/false, /*allow_expansion_p=*/true,
5973 /*non_constant_p=*/NULL);
5974 if (vec == NULL)
5975 return error_mark_node;
5977 FOR_EACH_VEC_ELT (*vec, i, p)
5978 mark_exp_read (p);
5980 if (vec->length () == 2)
5981 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5982 tf_warning_or_error);
5983 else if (vec->length () == 3)
5984 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5985 tf_warning_or_error);
5986 else
5988 error_at (loc, "wrong number of arguments to "
5989 "%<__builtin_shuffle%>");
5990 return error_mark_node;
5992 break;
5995 default:
5997 tree type;
5999 /* If the next thing is a simple-type-specifier, we may be
6000 looking at a functional cast. We could also be looking at
6001 an id-expression. So, we try the functional cast, and if
6002 that doesn't work we fall back to the primary-expression. */
6003 cp_parser_parse_tentatively (parser);
6004 /* Look for the simple-type-specifier. */
6005 type = cp_parser_simple_type_specifier (parser,
6006 /*decl_specs=*/NULL,
6007 CP_PARSER_FLAGS_NONE);
6008 /* Parse the cast itself. */
6009 if (!cp_parser_error_occurred (parser))
6010 postfix_expression
6011 = cp_parser_functional_cast (parser, type);
6012 /* If that worked, we're done. */
6013 if (cp_parser_parse_definitely (parser))
6014 break;
6016 /* If the functional-cast didn't work out, try a
6017 compound-literal. */
6018 if (cp_parser_allow_gnu_extensions_p (parser)
6019 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6021 tree initializer = NULL_TREE;
6023 cp_parser_parse_tentatively (parser);
6025 /* Avoid calling cp_parser_type_id pointlessly, see comment
6026 in cp_parser_cast_expression about c++/29234. */
6027 if (!cp_parser_compound_literal_p (parser))
6028 cp_parser_simulate_error (parser);
6029 else
6031 /* Parse the type. */
6032 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6033 parser->in_type_id_in_expr_p = true;
6034 type = cp_parser_type_id (parser);
6035 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6036 /* Look for the `)'. */
6037 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6040 /* If things aren't going well, there's no need to
6041 keep going. */
6042 if (!cp_parser_error_occurred (parser))
6044 bool non_constant_p;
6045 /* Parse the brace-enclosed initializer list. */
6046 initializer = cp_parser_braced_list (parser,
6047 &non_constant_p);
6049 /* If that worked, we're definitely looking at a
6050 compound-literal expression. */
6051 if (cp_parser_parse_definitely (parser))
6053 /* Warn the user that a compound literal is not
6054 allowed in standard C++. */
6055 pedwarn (input_location, OPT_Wpedantic,
6056 "ISO C++ forbids compound-literals");
6057 /* For simplicity, we disallow compound literals in
6058 constant-expressions. We could
6059 allow compound literals of integer type, whose
6060 initializer was a constant, in constant
6061 expressions. Permitting that usage, as a further
6062 extension, would not change the meaning of any
6063 currently accepted programs. (Of course, as
6064 compound literals are not part of ISO C++, the
6065 standard has nothing to say.) */
6066 if (cp_parser_non_integral_constant_expression (parser,
6067 NIC_NCC))
6069 postfix_expression = error_mark_node;
6070 break;
6072 /* Form the representation of the compound-literal. */
6073 postfix_expression
6074 = finish_compound_literal (type, initializer,
6075 tf_warning_or_error);
6076 break;
6080 /* It must be a primary-expression. */
6081 postfix_expression
6082 = cp_parser_primary_expression (parser, address_p, cast_p,
6083 /*template_arg_p=*/false,
6084 decltype_p,
6085 &idk);
6087 break;
6090 /* Note that we don't need to worry about calling build_cplus_new on a
6091 class-valued CALL_EXPR in decltype when it isn't the end of the
6092 postfix-expression; unary_complex_lvalue will take care of that for
6093 all these cases. */
6095 /* Keep looping until the postfix-expression is complete. */
6096 while (true)
6098 if (idk == CP_ID_KIND_UNQUALIFIED
6099 && identifier_p (postfix_expression)
6100 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6101 /* It is not a Koenig lookup function call. */
6102 postfix_expression
6103 = unqualified_name_lookup_error (postfix_expression);
6105 /* Peek at the next token. */
6106 token = cp_lexer_peek_token (parser->lexer);
6108 switch (token->type)
6110 case CPP_OPEN_SQUARE:
6111 if (cp_next_tokens_can_be_std_attribute_p (parser))
6113 cp_parser_error (parser,
6114 "two consecutive %<[%> shall "
6115 "only introduce an attribute");
6116 return error_mark_node;
6118 postfix_expression
6119 = cp_parser_postfix_open_square_expression (parser,
6120 postfix_expression,
6121 false,
6122 decltype_p);
6123 idk = CP_ID_KIND_NONE;
6124 is_member_access = false;
6125 break;
6127 case CPP_OPEN_PAREN:
6128 /* postfix-expression ( expression-list [opt] ) */
6130 bool koenig_p;
6131 bool is_builtin_constant_p;
6132 bool saved_integral_constant_expression_p = false;
6133 bool saved_non_integral_constant_expression_p = false;
6134 tsubst_flags_t complain = complain_flags (decltype_p);
6135 vec<tree, va_gc> *args;
6137 is_member_access = false;
6139 is_builtin_constant_p
6140 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6141 if (is_builtin_constant_p)
6143 /* The whole point of __builtin_constant_p is to allow
6144 non-constant expressions to appear as arguments. */
6145 saved_integral_constant_expression_p
6146 = parser->integral_constant_expression_p;
6147 saved_non_integral_constant_expression_p
6148 = parser->non_integral_constant_expression_p;
6149 parser->integral_constant_expression_p = false;
6151 args = (cp_parser_parenthesized_expression_list
6152 (parser, non_attr,
6153 /*cast_p=*/false, /*allow_expansion_p=*/true,
6154 /*non_constant_p=*/NULL));
6155 if (is_builtin_constant_p)
6157 parser->integral_constant_expression_p
6158 = saved_integral_constant_expression_p;
6159 parser->non_integral_constant_expression_p
6160 = saved_non_integral_constant_expression_p;
6163 if (args == NULL)
6165 postfix_expression = error_mark_node;
6166 break;
6169 /* Function calls are not permitted in
6170 constant-expressions. */
6171 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6172 && cp_parser_non_integral_constant_expression (parser,
6173 NIC_FUNC_CALL))
6175 postfix_expression = error_mark_node;
6176 release_tree_vector (args);
6177 break;
6180 koenig_p = false;
6181 if (idk == CP_ID_KIND_UNQUALIFIED
6182 || idk == CP_ID_KIND_TEMPLATE_ID)
6184 if (identifier_p (postfix_expression))
6186 if (!args->is_empty ())
6188 koenig_p = true;
6189 if (!any_type_dependent_arguments_p (args))
6190 postfix_expression
6191 = perform_koenig_lookup (postfix_expression, args,
6192 complain);
6194 else
6195 postfix_expression
6196 = unqualified_fn_lookup_error (postfix_expression);
6198 /* We do not perform argument-dependent lookup if
6199 normal lookup finds a non-function, in accordance
6200 with the expected resolution of DR 218. */
6201 else if (!args->is_empty ()
6202 && is_overloaded_fn (postfix_expression))
6204 tree fn = get_first_fn (postfix_expression);
6205 fn = STRIP_TEMPLATE (fn);
6207 /* Do not do argument dependent lookup if regular
6208 lookup finds a member function or a block-scope
6209 function declaration. [basic.lookup.argdep]/3 */
6210 if (!DECL_FUNCTION_MEMBER_P (fn)
6211 && !DECL_LOCAL_FUNCTION_P (fn))
6213 koenig_p = true;
6214 if (!any_type_dependent_arguments_p (args))
6215 postfix_expression
6216 = perform_koenig_lookup (postfix_expression, args,
6217 complain);
6222 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6224 tree instance = TREE_OPERAND (postfix_expression, 0);
6225 tree fn = TREE_OPERAND (postfix_expression, 1);
6227 if (processing_template_decl
6228 && (type_dependent_expression_p (instance)
6229 || (!BASELINK_P (fn)
6230 && TREE_CODE (fn) != FIELD_DECL)
6231 || type_dependent_expression_p (fn)
6232 || any_type_dependent_arguments_p (args)))
6234 postfix_expression
6235 = build_nt_call_vec (postfix_expression, args);
6236 release_tree_vector (args);
6237 break;
6240 if (BASELINK_P (fn))
6242 postfix_expression
6243 = (build_new_method_call
6244 (instance, fn, &args, NULL_TREE,
6245 (idk == CP_ID_KIND_QUALIFIED
6246 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6247 : LOOKUP_NORMAL),
6248 /*fn_p=*/NULL,
6249 complain));
6251 else
6252 postfix_expression
6253 = finish_call_expr (postfix_expression, &args,
6254 /*disallow_virtual=*/false,
6255 /*koenig_p=*/false,
6256 complain);
6258 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6259 || TREE_CODE (postfix_expression) == MEMBER_REF
6260 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6261 postfix_expression = (build_offset_ref_call_from_tree
6262 (postfix_expression, &args,
6263 complain));
6264 else if (idk == CP_ID_KIND_QUALIFIED)
6265 /* A call to a static class member, or a namespace-scope
6266 function. */
6267 postfix_expression
6268 = finish_call_expr (postfix_expression, &args,
6269 /*disallow_virtual=*/true,
6270 koenig_p,
6271 complain);
6272 else
6273 /* All other function calls. */
6274 postfix_expression
6275 = finish_call_expr (postfix_expression, &args,
6276 /*disallow_virtual=*/false,
6277 koenig_p,
6278 complain);
6280 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6281 idk = CP_ID_KIND_NONE;
6283 release_tree_vector (args);
6285 break;
6287 case CPP_DOT:
6288 case CPP_DEREF:
6289 /* postfix-expression . template [opt] id-expression
6290 postfix-expression . pseudo-destructor-name
6291 postfix-expression -> template [opt] id-expression
6292 postfix-expression -> pseudo-destructor-name */
6294 /* Consume the `.' or `->' operator. */
6295 cp_lexer_consume_token (parser->lexer);
6297 postfix_expression
6298 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6299 postfix_expression,
6300 false, &idk, loc);
6302 is_member_access = true;
6303 break;
6305 case CPP_PLUS_PLUS:
6306 /* postfix-expression ++ */
6307 /* Consume the `++' token. */
6308 cp_lexer_consume_token (parser->lexer);
6309 /* Generate a representation for the complete expression. */
6310 postfix_expression
6311 = finish_increment_expr (postfix_expression,
6312 POSTINCREMENT_EXPR);
6313 /* Increments may not appear in constant-expressions. */
6314 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6315 postfix_expression = error_mark_node;
6316 idk = CP_ID_KIND_NONE;
6317 is_member_access = false;
6318 break;
6320 case CPP_MINUS_MINUS:
6321 /* postfix-expression -- */
6322 /* Consume the `--' token. */
6323 cp_lexer_consume_token (parser->lexer);
6324 /* Generate a representation for the complete expression. */
6325 postfix_expression
6326 = finish_increment_expr (postfix_expression,
6327 POSTDECREMENT_EXPR);
6328 /* Decrements may not appear in constant-expressions. */
6329 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6330 postfix_expression = error_mark_node;
6331 idk = CP_ID_KIND_NONE;
6332 is_member_access = false;
6333 break;
6335 default:
6336 if (pidk_return != NULL)
6337 * pidk_return = idk;
6338 if (member_access_only_p)
6339 return is_member_access? postfix_expression : error_mark_node;
6340 else
6341 return postfix_expression;
6345 /* We should never get here. */
6346 gcc_unreachable ();
6347 return error_mark_node;
6350 /* This function parses Cilk Plus array notations. If a normal array expr. is
6351 parsed then the array index is passed back to the caller through *INIT_INDEX
6352 and the function returns a NULL_TREE. If array notation expr. is parsed,
6353 then *INIT_INDEX is ignored by the caller and the function returns
6354 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6355 error_mark_node. */
6357 static tree
6358 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6359 tree array_value)
6361 cp_token *token = NULL;
6362 tree length_index, stride = NULL_TREE, value_tree, array_type;
6363 if (!array_value || array_value == error_mark_node)
6365 cp_parser_skip_to_end_of_statement (parser);
6366 return error_mark_node;
6369 array_type = TREE_TYPE (array_value);
6371 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6372 parser->colon_corrects_to_scope_p = false;
6373 token = cp_lexer_peek_token (parser->lexer);
6375 if (!token)
6377 cp_parser_error (parser, "expected %<:%> or numeral");
6378 return error_mark_node;
6380 else if (token->type == CPP_COLON)
6382 /* Consume the ':'. */
6383 cp_lexer_consume_token (parser->lexer);
6385 /* If we are here, then we have a case like this A[:]. */
6386 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6388 cp_parser_error (parser, "expected %<]%>");
6389 cp_parser_skip_to_end_of_statement (parser);
6390 return error_mark_node;
6392 *init_index = NULL_TREE;
6393 stride = NULL_TREE;
6394 length_index = NULL_TREE;
6396 else
6398 /* If we are here, then there are three valid possibilities:
6399 1. ARRAY [ EXP ]
6400 2. ARRAY [ EXP : EXP ]
6401 3. ARRAY [ EXP : EXP : EXP ] */
6403 *init_index = cp_parser_expression (parser, false, NULL);
6404 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6406 /* This indicates that we have a normal array expression. */
6407 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6408 return NULL_TREE;
6411 /* Consume the ':'. */
6412 cp_lexer_consume_token (parser->lexer);
6413 length_index = cp_parser_expression (parser, false, NULL);
6414 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6416 cp_lexer_consume_token (parser->lexer);
6417 stride = cp_parser_expression (parser, false, NULL);
6420 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6422 if (*init_index == error_mark_node || length_index == error_mark_node
6423 || stride == error_mark_node)
6425 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6426 cp_lexer_consume_token (parser->lexer);
6427 return error_mark_node;
6429 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6431 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6432 length_index, stride, array_type);
6433 return value_tree;
6436 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6437 by cp_parser_builtin_offsetof. We're looking for
6439 postfix-expression [ expression ]
6440 postfix-expression [ braced-init-list ] (C++11)
6442 FOR_OFFSETOF is set if we're being called in that context, which
6443 changes how we deal with integer constant expressions. */
6445 static tree
6446 cp_parser_postfix_open_square_expression (cp_parser *parser,
6447 tree postfix_expression,
6448 bool for_offsetof,
6449 bool decltype_p)
6451 tree index = NULL_TREE;
6452 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6453 bool saved_greater_than_is_operator_p;
6455 /* Consume the `[' token. */
6456 cp_lexer_consume_token (parser->lexer);
6458 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6459 parser->greater_than_is_operator_p = true;
6461 /* Parse the index expression. */
6462 /* ??? For offsetof, there is a question of what to allow here. If
6463 offsetof is not being used in an integral constant expression context,
6464 then we *could* get the right answer by computing the value at runtime.
6465 If we are in an integral constant expression context, then we might
6466 could accept any constant expression; hard to say without analysis.
6467 Rather than open the barn door too wide right away, allow only integer
6468 constant expressions here. */
6469 if (for_offsetof)
6470 index = cp_parser_constant_expression (parser, false, NULL);
6471 else
6473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6475 bool expr_nonconst_p;
6476 cp_lexer_set_source_position (parser->lexer);
6477 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6478 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6479 if (flag_cilkplus
6480 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6482 error_at (cp_lexer_peek_token (parser->lexer)->location,
6483 "braced list index is not allowed with array "
6484 "notation");
6485 cp_parser_skip_to_end_of_statement (parser);
6486 return error_mark_node;
6489 else if (flag_cilkplus)
6491 /* Here are have these two options:
6492 ARRAY[EXP : EXP] - Array notation expr with default
6493 stride of 1.
6494 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6495 stride. */
6496 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6497 postfix_expression);
6498 if (an_exp)
6499 return an_exp;
6501 else
6502 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6505 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6507 /* Look for the closing `]'. */
6508 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6510 /* Build the ARRAY_REF. */
6511 postfix_expression = grok_array_decl (loc, postfix_expression,
6512 index, decltype_p);
6514 /* When not doing offsetof, array references are not permitted in
6515 constant-expressions. */
6516 if (!for_offsetof
6517 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6518 postfix_expression = error_mark_node;
6520 return postfix_expression;
6523 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6524 by cp_parser_builtin_offsetof. We're looking for
6526 postfix-expression . template [opt] id-expression
6527 postfix-expression . pseudo-destructor-name
6528 postfix-expression -> template [opt] id-expression
6529 postfix-expression -> pseudo-destructor-name
6531 FOR_OFFSETOF is set if we're being called in that context. That sorta
6532 limits what of the above we'll actually accept, but nevermind.
6533 TOKEN_TYPE is the "." or "->" token, which will already have been
6534 removed from the stream. */
6536 static tree
6537 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6538 enum cpp_ttype token_type,
6539 tree postfix_expression,
6540 bool for_offsetof, cp_id_kind *idk,
6541 location_t location)
6543 tree name;
6544 bool dependent_p;
6545 bool pseudo_destructor_p;
6546 tree scope = NULL_TREE;
6548 /* If this is a `->' operator, dereference the pointer. */
6549 if (token_type == CPP_DEREF)
6550 postfix_expression = build_x_arrow (location, postfix_expression,
6551 tf_warning_or_error);
6552 /* Check to see whether or not the expression is type-dependent. */
6553 dependent_p = type_dependent_expression_p (postfix_expression);
6554 /* The identifier following the `->' or `.' is not qualified. */
6555 parser->scope = NULL_TREE;
6556 parser->qualifying_scope = NULL_TREE;
6557 parser->object_scope = NULL_TREE;
6558 *idk = CP_ID_KIND_NONE;
6560 /* Enter the scope corresponding to the type of the object
6561 given by the POSTFIX_EXPRESSION. */
6562 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6564 scope = TREE_TYPE (postfix_expression);
6565 /* According to the standard, no expression should ever have
6566 reference type. Unfortunately, we do not currently match
6567 the standard in this respect in that our internal representation
6568 of an expression may have reference type even when the standard
6569 says it does not. Therefore, we have to manually obtain the
6570 underlying type here. */
6571 scope = non_reference (scope);
6572 /* The type of the POSTFIX_EXPRESSION must be complete. */
6573 if (scope == unknown_type_node)
6575 error_at (location, "%qE does not have class type",
6576 postfix_expression);
6577 scope = NULL_TREE;
6579 /* Unlike the object expression in other contexts, *this is not
6580 required to be of complete type for purposes of class member
6581 access (5.2.5) outside the member function body. */
6582 else if (postfix_expression != current_class_ref
6583 && !(processing_template_decl && scope == current_class_type))
6584 scope = complete_type_or_else (scope, NULL_TREE);
6585 /* Let the name lookup machinery know that we are processing a
6586 class member access expression. */
6587 parser->context->object_type = scope;
6588 /* If something went wrong, we want to be able to discern that case,
6589 as opposed to the case where there was no SCOPE due to the type
6590 of expression being dependent. */
6591 if (!scope)
6592 scope = error_mark_node;
6593 /* If the SCOPE was erroneous, make the various semantic analysis
6594 functions exit quickly -- and without issuing additional error
6595 messages. */
6596 if (scope == error_mark_node)
6597 postfix_expression = error_mark_node;
6600 /* Assume this expression is not a pseudo-destructor access. */
6601 pseudo_destructor_p = false;
6603 /* If the SCOPE is a scalar type, then, if this is a valid program,
6604 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6605 is type dependent, it can be pseudo-destructor-name or something else.
6606 Try to parse it as pseudo-destructor-name first. */
6607 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6609 tree s;
6610 tree type;
6612 cp_parser_parse_tentatively (parser);
6613 /* Parse the pseudo-destructor-name. */
6614 s = NULL_TREE;
6615 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6616 &s, &type);
6617 if (dependent_p
6618 && (cp_parser_error_occurred (parser)
6619 || !SCALAR_TYPE_P (type)))
6620 cp_parser_abort_tentative_parse (parser);
6621 else if (cp_parser_parse_definitely (parser))
6623 pseudo_destructor_p = true;
6624 postfix_expression
6625 = finish_pseudo_destructor_expr (postfix_expression,
6626 s, type, location);
6630 if (!pseudo_destructor_p)
6632 /* If the SCOPE is not a scalar type, we are looking at an
6633 ordinary class member access expression, rather than a
6634 pseudo-destructor-name. */
6635 bool template_p;
6636 cp_token *token = cp_lexer_peek_token (parser->lexer);
6637 /* Parse the id-expression. */
6638 name = (cp_parser_id_expression
6639 (parser,
6640 cp_parser_optional_template_keyword (parser),
6641 /*check_dependency_p=*/true,
6642 &template_p,
6643 /*declarator_p=*/false,
6644 /*optional_p=*/false));
6645 /* In general, build a SCOPE_REF if the member name is qualified.
6646 However, if the name was not dependent and has already been
6647 resolved; there is no need to build the SCOPE_REF. For example;
6649 struct X { void f(); };
6650 template <typename T> void f(T* t) { t->X::f(); }
6652 Even though "t" is dependent, "X::f" is not and has been resolved
6653 to a BASELINK; there is no need to include scope information. */
6655 /* But we do need to remember that there was an explicit scope for
6656 virtual function calls. */
6657 if (parser->scope)
6658 *idk = CP_ID_KIND_QUALIFIED;
6660 /* If the name is a template-id that names a type, we will get a
6661 TYPE_DECL here. That is invalid code. */
6662 if (TREE_CODE (name) == TYPE_DECL)
6664 error_at (token->location, "invalid use of %qD", name);
6665 postfix_expression = error_mark_node;
6667 else
6669 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6671 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6673 error_at (token->location, "%<%D::%D%> is not a class member",
6674 parser->scope, name);
6675 postfix_expression = error_mark_node;
6677 else
6678 name = build_qualified_name (/*type=*/NULL_TREE,
6679 parser->scope,
6680 name,
6681 template_p);
6682 parser->scope = NULL_TREE;
6683 parser->qualifying_scope = NULL_TREE;
6684 parser->object_scope = NULL_TREE;
6686 if (parser->scope && name && BASELINK_P (name))
6687 adjust_result_of_qualified_name_lookup
6688 (name, parser->scope, scope);
6689 postfix_expression
6690 = finish_class_member_access_expr (postfix_expression, name,
6691 template_p,
6692 tf_warning_or_error);
6696 /* We no longer need to look up names in the scope of the object on
6697 the left-hand side of the `.' or `->' operator. */
6698 parser->context->object_type = NULL_TREE;
6700 /* Outside of offsetof, these operators may not appear in
6701 constant-expressions. */
6702 if (!for_offsetof
6703 && (cp_parser_non_integral_constant_expression
6704 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6705 postfix_expression = error_mark_node;
6707 return postfix_expression;
6710 /* Parse a parenthesized expression-list.
6712 expression-list:
6713 assignment-expression
6714 expression-list, assignment-expression
6716 attribute-list:
6717 expression-list
6718 identifier
6719 identifier, expression-list
6721 CAST_P is true if this expression is the target of a cast.
6723 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6724 argument pack.
6726 Returns a vector of trees. Each element is a representation of an
6727 assignment-expression. NULL is returned if the ( and or ) are
6728 missing. An empty, but allocated, vector is returned on no
6729 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6730 if we are parsing an attribute list for an attribute that wants a
6731 plain identifier argument, normal_attr for an attribute that wants
6732 an expression, or non_attr if we aren't parsing an attribute list. If
6733 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6734 not all of the expressions in the list were constant. */
6736 static vec<tree, va_gc> *
6737 cp_parser_parenthesized_expression_list (cp_parser* parser,
6738 int is_attribute_list,
6739 bool cast_p,
6740 bool allow_expansion_p,
6741 bool *non_constant_p)
6743 vec<tree, va_gc> *expression_list;
6744 bool fold_expr_p = is_attribute_list != non_attr;
6745 tree identifier = NULL_TREE;
6746 bool saved_greater_than_is_operator_p;
6748 /* Assume all the expressions will be constant. */
6749 if (non_constant_p)
6750 *non_constant_p = false;
6752 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6753 return NULL;
6755 expression_list = make_tree_vector ();
6757 /* Within a parenthesized expression, a `>' token is always
6758 the greater-than operator. */
6759 saved_greater_than_is_operator_p
6760 = parser->greater_than_is_operator_p;
6761 parser->greater_than_is_operator_p = true;
6763 /* Consume expressions until there are no more. */
6764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6765 while (true)
6767 tree expr;
6769 /* At the beginning of attribute lists, check to see if the
6770 next token is an identifier. */
6771 if (is_attribute_list == id_attr
6772 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6774 cp_token *token;
6776 /* Consume the identifier. */
6777 token = cp_lexer_consume_token (parser->lexer);
6778 /* Save the identifier. */
6779 identifier = token->u.value;
6781 else
6783 bool expr_non_constant_p;
6785 /* Parse the next assignment-expression. */
6786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6788 /* A braced-init-list. */
6789 cp_lexer_set_source_position (parser->lexer);
6790 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6791 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6792 if (non_constant_p && expr_non_constant_p)
6793 *non_constant_p = true;
6795 else if (non_constant_p)
6797 expr = (cp_parser_constant_expression
6798 (parser, /*allow_non_constant_p=*/true,
6799 &expr_non_constant_p));
6800 if (expr_non_constant_p)
6801 *non_constant_p = true;
6803 else
6804 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6806 if (fold_expr_p)
6807 expr = fold_non_dependent_expr (expr);
6809 /* If we have an ellipsis, then this is an expression
6810 expansion. */
6811 if (allow_expansion_p
6812 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6814 /* Consume the `...'. */
6815 cp_lexer_consume_token (parser->lexer);
6817 /* Build the argument pack. */
6818 expr = make_pack_expansion (expr);
6821 /* Add it to the list. We add error_mark_node
6822 expressions to the list, so that we can still tell if
6823 the correct form for a parenthesized expression-list
6824 is found. That gives better errors. */
6825 vec_safe_push (expression_list, expr);
6827 if (expr == error_mark_node)
6828 goto skip_comma;
6831 /* After the first item, attribute lists look the same as
6832 expression lists. */
6833 is_attribute_list = non_attr;
6835 get_comma:;
6836 /* If the next token isn't a `,', then we are done. */
6837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6838 break;
6840 /* Otherwise, consume the `,' and keep going. */
6841 cp_lexer_consume_token (parser->lexer);
6844 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6846 int ending;
6848 skip_comma:;
6849 /* We try and resync to an unnested comma, as that will give the
6850 user better diagnostics. */
6851 ending = cp_parser_skip_to_closing_parenthesis (parser,
6852 /*recovering=*/true,
6853 /*or_comma=*/true,
6854 /*consume_paren=*/true);
6855 if (ending < 0)
6856 goto get_comma;
6857 if (!ending)
6859 parser->greater_than_is_operator_p
6860 = saved_greater_than_is_operator_p;
6861 return NULL;
6865 parser->greater_than_is_operator_p
6866 = saved_greater_than_is_operator_p;
6868 if (identifier)
6869 vec_safe_insert (expression_list, 0, identifier);
6871 return expression_list;
6874 /* Parse a pseudo-destructor-name.
6876 pseudo-destructor-name:
6877 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6878 :: [opt] nested-name-specifier template template-id :: ~ type-name
6879 :: [opt] nested-name-specifier [opt] ~ type-name
6881 If either of the first two productions is used, sets *SCOPE to the
6882 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6883 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6884 or ERROR_MARK_NODE if the parse fails. */
6886 static void
6887 cp_parser_pseudo_destructor_name (cp_parser* parser,
6888 tree object,
6889 tree* scope,
6890 tree* type)
6892 bool nested_name_specifier_p;
6894 /* Handle ~auto. */
6895 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6896 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6897 && !type_dependent_expression_p (object))
6899 if (cxx_dialect < cxx1y)
6900 pedwarn (input_location, 0,
6901 "%<~auto%> only available with "
6902 "-std=c++1y or -std=gnu++1y");
6903 cp_lexer_consume_token (parser->lexer);
6904 cp_lexer_consume_token (parser->lexer);
6905 *scope = NULL_TREE;
6906 *type = TREE_TYPE (object);
6907 return;
6910 /* Assume that things will not work out. */
6911 *type = error_mark_node;
6913 /* Look for the optional `::' operator. */
6914 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6915 /* Look for the optional nested-name-specifier. */
6916 nested_name_specifier_p
6917 = (cp_parser_nested_name_specifier_opt (parser,
6918 /*typename_keyword_p=*/false,
6919 /*check_dependency_p=*/true,
6920 /*type_p=*/false,
6921 /*is_declaration=*/false)
6922 != NULL_TREE);
6923 /* Now, if we saw a nested-name-specifier, we might be doing the
6924 second production. */
6925 if (nested_name_specifier_p
6926 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6928 /* Consume the `template' keyword. */
6929 cp_lexer_consume_token (parser->lexer);
6930 /* Parse the template-id. */
6931 cp_parser_template_id (parser,
6932 /*template_keyword_p=*/true,
6933 /*check_dependency_p=*/false,
6934 class_type,
6935 /*is_declaration=*/true);
6936 /* Look for the `::' token. */
6937 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6939 /* If the next token is not a `~', then there might be some
6940 additional qualification. */
6941 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6943 /* At this point, we're looking for "type-name :: ~". The type-name
6944 must not be a class-name, since this is a pseudo-destructor. So,
6945 it must be either an enum-name, or a typedef-name -- both of which
6946 are just identifiers. So, we peek ahead to check that the "::"
6947 and "~" tokens are present; if they are not, then we can avoid
6948 calling type_name. */
6949 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6950 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6951 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6953 cp_parser_error (parser, "non-scalar type");
6954 return;
6957 /* Look for the type-name. */
6958 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6959 if (*scope == error_mark_node)
6960 return;
6962 /* Look for the `::' token. */
6963 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6965 else
6966 *scope = NULL_TREE;
6968 /* Look for the `~'. */
6969 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6971 /* Once we see the ~, this has to be a pseudo-destructor. */
6972 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6973 cp_parser_commit_to_topmost_tentative_parse (parser);
6975 /* Look for the type-name again. We are not responsible for
6976 checking that it matches the first type-name. */
6977 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6980 /* Parse a unary-expression.
6982 unary-expression:
6983 postfix-expression
6984 ++ cast-expression
6985 -- cast-expression
6986 unary-operator cast-expression
6987 sizeof unary-expression
6988 sizeof ( type-id )
6989 alignof ( type-id ) [C++0x]
6990 new-expression
6991 delete-expression
6993 GNU Extensions:
6995 unary-expression:
6996 __extension__ cast-expression
6997 __alignof__ unary-expression
6998 __alignof__ ( type-id )
6999 alignof unary-expression [C++0x]
7000 __real__ cast-expression
7001 __imag__ cast-expression
7002 && identifier
7003 sizeof ( type-id ) { initializer-list , [opt] }
7004 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7005 __alignof__ ( type-id ) { initializer-list , [opt] }
7007 ADDRESS_P is true iff the unary-expression is appearing as the
7008 operand of the `&' operator. CAST_P is true if this expression is
7009 the target of a cast.
7011 Returns a representation of the expression. */
7013 static tree
7014 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7015 bool decltype_p, cp_id_kind * pidk)
7017 cp_token *token;
7018 enum tree_code unary_operator;
7020 /* Peek at the next token. */
7021 token = cp_lexer_peek_token (parser->lexer);
7022 /* Some keywords give away the kind of expression. */
7023 if (token->type == CPP_KEYWORD)
7025 enum rid keyword = token->keyword;
7027 switch (keyword)
7029 case RID_ALIGNOF:
7030 case RID_SIZEOF:
7032 tree operand, ret;
7033 enum tree_code op;
7034 location_t first_loc;
7036 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7037 /* Consume the token. */
7038 cp_lexer_consume_token (parser->lexer);
7039 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7040 /* Parse the operand. */
7041 operand = cp_parser_sizeof_operand (parser, keyword);
7043 if (TYPE_P (operand))
7044 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7045 else
7047 /* ISO C++ defines alignof only with types, not with
7048 expressions. So pedwarn if alignof is used with a non-
7049 type expression. However, __alignof__ is ok. */
7050 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7051 pedwarn (token->location, OPT_Wpedantic,
7052 "ISO C++ does not allow %<alignof%> "
7053 "with a non-type");
7055 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7057 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7058 SIZEOF_EXPR with the original operand. */
7059 if (op == SIZEOF_EXPR && ret != error_mark_node)
7061 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7063 if (!processing_template_decl && TYPE_P (operand))
7065 ret = build_min (SIZEOF_EXPR, size_type_node,
7066 build1 (NOP_EXPR, operand,
7067 error_mark_node));
7068 SIZEOF_EXPR_TYPE_P (ret) = 1;
7070 else
7071 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7072 TREE_SIDE_EFFECTS (ret) = 0;
7073 TREE_READONLY (ret) = 1;
7075 SET_EXPR_LOCATION (ret, first_loc);
7077 return ret;
7080 case RID_NEW:
7081 return cp_parser_new_expression (parser);
7083 case RID_DELETE:
7084 return cp_parser_delete_expression (parser);
7086 case RID_EXTENSION:
7088 /* The saved value of the PEDANTIC flag. */
7089 int saved_pedantic;
7090 tree expr;
7092 /* Save away the PEDANTIC flag. */
7093 cp_parser_extension_opt (parser, &saved_pedantic);
7094 /* Parse the cast-expression. */
7095 expr = cp_parser_simple_cast_expression (parser);
7096 /* Restore the PEDANTIC flag. */
7097 pedantic = saved_pedantic;
7099 return expr;
7102 case RID_REALPART:
7103 case RID_IMAGPART:
7105 tree expression;
7107 /* Consume the `__real__' or `__imag__' token. */
7108 cp_lexer_consume_token (parser->lexer);
7109 /* Parse the cast-expression. */
7110 expression = cp_parser_simple_cast_expression (parser);
7111 /* Create the complete representation. */
7112 return build_x_unary_op (token->location,
7113 (keyword == RID_REALPART
7114 ? REALPART_EXPR : IMAGPART_EXPR),
7115 expression,
7116 tf_warning_or_error);
7118 break;
7120 case RID_TRANSACTION_ATOMIC:
7121 case RID_TRANSACTION_RELAXED:
7122 return cp_parser_transaction_expression (parser, keyword);
7124 case RID_NOEXCEPT:
7126 tree expr;
7127 const char *saved_message;
7128 bool saved_integral_constant_expression_p;
7129 bool saved_non_integral_constant_expression_p;
7130 bool saved_greater_than_is_operator_p;
7132 cp_lexer_consume_token (parser->lexer);
7133 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7135 saved_message = parser->type_definition_forbidden_message;
7136 parser->type_definition_forbidden_message
7137 = G_("types may not be defined in %<noexcept%> expressions");
7139 saved_integral_constant_expression_p
7140 = parser->integral_constant_expression_p;
7141 saved_non_integral_constant_expression_p
7142 = parser->non_integral_constant_expression_p;
7143 parser->integral_constant_expression_p = false;
7145 saved_greater_than_is_operator_p
7146 = parser->greater_than_is_operator_p;
7147 parser->greater_than_is_operator_p = true;
7149 ++cp_unevaluated_operand;
7150 ++c_inhibit_evaluation_warnings;
7151 expr = cp_parser_expression (parser, false, NULL);
7152 --c_inhibit_evaluation_warnings;
7153 --cp_unevaluated_operand;
7155 parser->greater_than_is_operator_p
7156 = saved_greater_than_is_operator_p;
7158 parser->integral_constant_expression_p
7159 = saved_integral_constant_expression_p;
7160 parser->non_integral_constant_expression_p
7161 = saved_non_integral_constant_expression_p;
7163 parser->type_definition_forbidden_message = saved_message;
7165 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7166 return finish_noexcept_expr (expr, tf_warning_or_error);
7169 default:
7170 break;
7174 /* Look for the `:: new' and `:: delete', which also signal the
7175 beginning of a new-expression, or delete-expression,
7176 respectively. If the next token is `::', then it might be one of
7177 these. */
7178 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7180 enum rid keyword;
7182 /* See if the token after the `::' is one of the keywords in
7183 which we're interested. */
7184 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7185 /* If it's `new', we have a new-expression. */
7186 if (keyword == RID_NEW)
7187 return cp_parser_new_expression (parser);
7188 /* Similarly, for `delete'. */
7189 else if (keyword == RID_DELETE)
7190 return cp_parser_delete_expression (parser);
7193 /* Look for a unary operator. */
7194 unary_operator = cp_parser_unary_operator (token);
7195 /* The `++' and `--' operators can be handled similarly, even though
7196 they are not technically unary-operators in the grammar. */
7197 if (unary_operator == ERROR_MARK)
7199 if (token->type == CPP_PLUS_PLUS)
7200 unary_operator = PREINCREMENT_EXPR;
7201 else if (token->type == CPP_MINUS_MINUS)
7202 unary_operator = PREDECREMENT_EXPR;
7203 /* Handle the GNU address-of-label extension. */
7204 else if (cp_parser_allow_gnu_extensions_p (parser)
7205 && token->type == CPP_AND_AND)
7207 tree identifier;
7208 tree expression;
7209 location_t loc = token->location;
7211 /* Consume the '&&' token. */
7212 cp_lexer_consume_token (parser->lexer);
7213 /* Look for the identifier. */
7214 identifier = cp_parser_identifier (parser);
7215 /* Create an expression representing the address. */
7216 expression = finish_label_address_expr (identifier, loc);
7217 if (cp_parser_non_integral_constant_expression (parser,
7218 NIC_ADDR_LABEL))
7219 expression = error_mark_node;
7220 return expression;
7223 if (unary_operator != ERROR_MARK)
7225 tree cast_expression;
7226 tree expression = error_mark_node;
7227 non_integral_constant non_constant_p = NIC_NONE;
7228 location_t loc = token->location;
7229 tsubst_flags_t complain = complain_flags (decltype_p);
7231 /* Consume the operator token. */
7232 token = cp_lexer_consume_token (parser->lexer);
7233 /* Parse the cast-expression. */
7234 cast_expression
7235 = cp_parser_cast_expression (parser,
7236 unary_operator == ADDR_EXPR,
7237 /*cast_p=*/false,
7238 /*decltype*/false,
7239 pidk);
7240 /* Now, build an appropriate representation. */
7241 switch (unary_operator)
7243 case INDIRECT_REF:
7244 non_constant_p = NIC_STAR;
7245 expression = build_x_indirect_ref (loc, cast_expression,
7246 RO_UNARY_STAR,
7247 complain);
7248 break;
7250 case ADDR_EXPR:
7251 non_constant_p = NIC_ADDR;
7252 /* Fall through. */
7253 case BIT_NOT_EXPR:
7254 expression = build_x_unary_op (loc, unary_operator,
7255 cast_expression,
7256 complain);
7257 break;
7259 case PREINCREMENT_EXPR:
7260 case PREDECREMENT_EXPR:
7261 non_constant_p = unary_operator == PREINCREMENT_EXPR
7262 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7263 /* Fall through. */
7264 case UNARY_PLUS_EXPR:
7265 case NEGATE_EXPR:
7266 case TRUTH_NOT_EXPR:
7267 expression = finish_unary_op_expr (loc, unary_operator,
7268 cast_expression, complain);
7269 break;
7271 default:
7272 gcc_unreachable ();
7275 if (non_constant_p != NIC_NONE
7276 && cp_parser_non_integral_constant_expression (parser,
7277 non_constant_p))
7278 expression = error_mark_node;
7280 return expression;
7283 return cp_parser_postfix_expression (parser, address_p, cast_p,
7284 /*member_access_only_p=*/false,
7285 decltype_p,
7286 pidk);
7289 static inline tree
7290 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7291 cp_id_kind * pidk)
7293 return cp_parser_unary_expression (parser, address_p, cast_p,
7294 /*decltype*/false, pidk);
7297 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7298 unary-operator, the corresponding tree code is returned. */
7300 static enum tree_code
7301 cp_parser_unary_operator (cp_token* token)
7303 switch (token->type)
7305 case CPP_MULT:
7306 return INDIRECT_REF;
7308 case CPP_AND:
7309 return ADDR_EXPR;
7311 case CPP_PLUS:
7312 return UNARY_PLUS_EXPR;
7314 case CPP_MINUS:
7315 return NEGATE_EXPR;
7317 case CPP_NOT:
7318 return TRUTH_NOT_EXPR;
7320 case CPP_COMPL:
7321 return BIT_NOT_EXPR;
7323 default:
7324 return ERROR_MARK;
7328 /* Parse a new-expression.
7330 new-expression:
7331 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7332 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7334 Returns a representation of the expression. */
7336 static tree
7337 cp_parser_new_expression (cp_parser* parser)
7339 bool global_scope_p;
7340 vec<tree, va_gc> *placement;
7341 tree type;
7342 vec<tree, va_gc> *initializer;
7343 tree nelts = NULL_TREE;
7344 tree ret;
7346 /* Look for the optional `::' operator. */
7347 global_scope_p
7348 = (cp_parser_global_scope_opt (parser,
7349 /*current_scope_valid_p=*/false)
7350 != NULL_TREE);
7351 /* Look for the `new' operator. */
7352 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7353 /* There's no easy way to tell a new-placement from the
7354 `( type-id )' construct. */
7355 cp_parser_parse_tentatively (parser);
7356 /* Look for a new-placement. */
7357 placement = cp_parser_new_placement (parser);
7358 /* If that didn't work out, there's no new-placement. */
7359 if (!cp_parser_parse_definitely (parser))
7361 if (placement != NULL)
7362 release_tree_vector (placement);
7363 placement = NULL;
7366 /* If the next token is a `(', then we have a parenthesized
7367 type-id. */
7368 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7370 cp_token *token;
7371 const char *saved_message = parser->type_definition_forbidden_message;
7373 /* Consume the `('. */
7374 cp_lexer_consume_token (parser->lexer);
7376 /* Parse the type-id. */
7377 parser->type_definition_forbidden_message
7378 = G_("types may not be defined in a new-expression");
7379 type = cp_parser_type_id (parser);
7380 parser->type_definition_forbidden_message = saved_message;
7382 /* Look for the closing `)'. */
7383 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7384 token = cp_lexer_peek_token (parser->lexer);
7385 /* There should not be a direct-new-declarator in this production,
7386 but GCC used to allowed this, so we check and emit a sensible error
7387 message for this case. */
7388 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7390 error_at (token->location,
7391 "array bound forbidden after parenthesized type-id");
7392 inform (token->location,
7393 "try removing the parentheses around the type-id");
7394 cp_parser_direct_new_declarator (parser);
7397 /* Otherwise, there must be a new-type-id. */
7398 else
7399 type = cp_parser_new_type_id (parser, &nelts);
7401 /* If the next token is a `(' or '{', then we have a new-initializer. */
7402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7403 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7404 initializer = cp_parser_new_initializer (parser);
7405 else
7406 initializer = NULL;
7408 /* A new-expression may not appear in an integral constant
7409 expression. */
7410 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7411 ret = error_mark_node;
7412 else
7414 /* Create a representation of the new-expression. */
7415 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7416 tf_warning_or_error);
7419 if (placement != NULL)
7420 release_tree_vector (placement);
7421 if (initializer != NULL)
7422 release_tree_vector (initializer);
7424 return ret;
7427 /* Parse a new-placement.
7429 new-placement:
7430 ( expression-list )
7432 Returns the same representation as for an expression-list. */
7434 static vec<tree, va_gc> *
7435 cp_parser_new_placement (cp_parser* parser)
7437 vec<tree, va_gc> *expression_list;
7439 /* Parse the expression-list. */
7440 expression_list = (cp_parser_parenthesized_expression_list
7441 (parser, non_attr, /*cast_p=*/false,
7442 /*allow_expansion_p=*/true,
7443 /*non_constant_p=*/NULL));
7445 return expression_list;
7448 /* Parse a new-type-id.
7450 new-type-id:
7451 type-specifier-seq new-declarator [opt]
7453 Returns the TYPE allocated. If the new-type-id indicates an array
7454 type, *NELTS is set to the number of elements in the last array
7455 bound; the TYPE will not include the last array bound. */
7457 static tree
7458 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7460 cp_decl_specifier_seq type_specifier_seq;
7461 cp_declarator *new_declarator;
7462 cp_declarator *declarator;
7463 cp_declarator *outer_declarator;
7464 const char *saved_message;
7466 /* The type-specifier sequence must not contain type definitions.
7467 (It cannot contain declarations of new types either, but if they
7468 are not definitions we will catch that because they are not
7469 complete.) */
7470 saved_message = parser->type_definition_forbidden_message;
7471 parser->type_definition_forbidden_message
7472 = G_("types may not be defined in a new-type-id");
7473 /* Parse the type-specifier-seq. */
7474 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7475 /*is_trailing_return=*/false,
7476 &type_specifier_seq);
7477 /* Restore the old message. */
7478 parser->type_definition_forbidden_message = saved_message;
7480 if (type_specifier_seq.type == error_mark_node)
7481 return error_mark_node;
7483 /* Parse the new-declarator. */
7484 new_declarator = cp_parser_new_declarator_opt (parser);
7486 /* Determine the number of elements in the last array dimension, if
7487 any. */
7488 *nelts = NULL_TREE;
7489 /* Skip down to the last array dimension. */
7490 declarator = new_declarator;
7491 outer_declarator = NULL;
7492 while (declarator && (declarator->kind == cdk_pointer
7493 || declarator->kind == cdk_ptrmem))
7495 outer_declarator = declarator;
7496 declarator = declarator->declarator;
7498 while (declarator
7499 && declarator->kind == cdk_array
7500 && declarator->declarator
7501 && declarator->declarator->kind == cdk_array)
7503 outer_declarator = declarator;
7504 declarator = declarator->declarator;
7507 if (declarator && declarator->kind == cdk_array)
7509 *nelts = declarator->u.array.bounds;
7510 if (*nelts == error_mark_node)
7511 *nelts = integer_one_node;
7513 if (outer_declarator)
7514 outer_declarator->declarator = declarator->declarator;
7515 else
7516 new_declarator = NULL;
7519 return groktypename (&type_specifier_seq, new_declarator, false);
7522 /* Parse an (optional) new-declarator.
7524 new-declarator:
7525 ptr-operator new-declarator [opt]
7526 direct-new-declarator
7528 Returns the declarator. */
7530 static cp_declarator *
7531 cp_parser_new_declarator_opt (cp_parser* parser)
7533 enum tree_code code;
7534 tree type, std_attributes = NULL_TREE;
7535 cp_cv_quals cv_quals;
7537 /* We don't know if there's a ptr-operator next, or not. */
7538 cp_parser_parse_tentatively (parser);
7539 /* Look for a ptr-operator. */
7540 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7541 /* If that worked, look for more new-declarators. */
7542 if (cp_parser_parse_definitely (parser))
7544 cp_declarator *declarator;
7546 /* Parse another optional declarator. */
7547 declarator = cp_parser_new_declarator_opt (parser);
7549 declarator = cp_parser_make_indirect_declarator
7550 (code, type, cv_quals, declarator, std_attributes);
7552 return declarator;
7555 /* If the next token is a `[', there is a direct-new-declarator. */
7556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7557 return cp_parser_direct_new_declarator (parser);
7559 return NULL;
7562 /* Parse a direct-new-declarator.
7564 direct-new-declarator:
7565 [ expression ]
7566 direct-new-declarator [constant-expression]
7570 static cp_declarator *
7571 cp_parser_direct_new_declarator (cp_parser* parser)
7573 cp_declarator *declarator = NULL;
7575 while (true)
7577 tree expression;
7578 cp_token *token;
7580 /* Look for the opening `['. */
7581 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7583 token = cp_lexer_peek_token (parser->lexer);
7584 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7585 /* The standard requires that the expression have integral
7586 type. DR 74 adds enumeration types. We believe that the
7587 real intent is that these expressions be handled like the
7588 expression in a `switch' condition, which also allows
7589 classes with a single conversion to integral or
7590 enumeration type. */
7591 if (!processing_template_decl)
7593 expression
7594 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7595 expression,
7596 /*complain=*/true);
7597 if (!expression)
7599 error_at (token->location,
7600 "expression in new-declarator must have integral "
7601 "or enumeration type");
7602 expression = error_mark_node;
7606 /* Look for the closing `]'. */
7607 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7609 /* Add this bound to the declarator. */
7610 declarator = make_array_declarator (declarator, expression);
7612 /* If the next token is not a `[', then there are no more
7613 bounds. */
7614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7615 break;
7618 return declarator;
7621 /* Parse a new-initializer.
7623 new-initializer:
7624 ( expression-list [opt] )
7625 braced-init-list
7627 Returns a representation of the expression-list. */
7629 static vec<tree, va_gc> *
7630 cp_parser_new_initializer (cp_parser* parser)
7632 vec<tree, va_gc> *expression_list;
7634 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7636 tree t;
7637 bool expr_non_constant_p;
7638 cp_lexer_set_source_position (parser->lexer);
7639 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7640 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7641 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7642 expression_list = make_tree_vector_single (t);
7644 else
7645 expression_list = (cp_parser_parenthesized_expression_list
7646 (parser, non_attr, /*cast_p=*/false,
7647 /*allow_expansion_p=*/true,
7648 /*non_constant_p=*/NULL));
7650 return expression_list;
7653 /* Parse a delete-expression.
7655 delete-expression:
7656 :: [opt] delete cast-expression
7657 :: [opt] delete [ ] cast-expression
7659 Returns a representation of the expression. */
7661 static tree
7662 cp_parser_delete_expression (cp_parser* parser)
7664 bool global_scope_p;
7665 bool array_p;
7666 tree expression;
7668 /* Look for the optional `::' operator. */
7669 global_scope_p
7670 = (cp_parser_global_scope_opt (parser,
7671 /*current_scope_valid_p=*/false)
7672 != NULL_TREE);
7673 /* Look for the `delete' keyword. */
7674 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7675 /* See if the array syntax is in use. */
7676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7678 /* Consume the `[' token. */
7679 cp_lexer_consume_token (parser->lexer);
7680 /* Look for the `]' token. */
7681 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7682 /* Remember that this is the `[]' construct. */
7683 array_p = true;
7685 else
7686 array_p = false;
7688 /* Parse the cast-expression. */
7689 expression = cp_parser_simple_cast_expression (parser);
7691 /* A delete-expression may not appear in an integral constant
7692 expression. */
7693 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7694 return error_mark_node;
7696 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7697 tf_warning_or_error);
7700 /* Returns true if TOKEN may start a cast-expression and false
7701 otherwise. */
7703 static bool
7704 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7706 cp_token *token = cp_lexer_peek_token (parser->lexer);
7707 switch (token->type)
7709 case CPP_COMMA:
7710 case CPP_SEMICOLON:
7711 case CPP_QUERY:
7712 case CPP_COLON:
7713 case CPP_CLOSE_SQUARE:
7714 case CPP_CLOSE_PAREN:
7715 case CPP_CLOSE_BRACE:
7716 case CPP_OPEN_BRACE:
7717 case CPP_DOT:
7718 case CPP_DOT_STAR:
7719 case CPP_DEREF:
7720 case CPP_DEREF_STAR:
7721 case CPP_DIV:
7722 case CPP_MOD:
7723 case CPP_LSHIFT:
7724 case CPP_RSHIFT:
7725 case CPP_LESS:
7726 case CPP_GREATER:
7727 case CPP_LESS_EQ:
7728 case CPP_GREATER_EQ:
7729 case CPP_EQ_EQ:
7730 case CPP_NOT_EQ:
7731 case CPP_EQ:
7732 case CPP_MULT_EQ:
7733 case CPP_DIV_EQ:
7734 case CPP_MOD_EQ:
7735 case CPP_PLUS_EQ:
7736 case CPP_MINUS_EQ:
7737 case CPP_RSHIFT_EQ:
7738 case CPP_LSHIFT_EQ:
7739 case CPP_AND_EQ:
7740 case CPP_XOR_EQ:
7741 case CPP_OR_EQ:
7742 case CPP_XOR:
7743 case CPP_OR:
7744 case CPP_OR_OR:
7745 case CPP_EOF:
7746 return false;
7748 case CPP_OPEN_PAREN:
7749 /* In ((type ()) () the last () isn't a valid cast-expression,
7750 so the whole must be parsed as postfix-expression. */
7751 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7752 != CPP_CLOSE_PAREN;
7754 /* '[' may start a primary-expression in obj-c++. */
7755 case CPP_OPEN_SQUARE:
7756 return c_dialect_objc ();
7758 default:
7759 return true;
7763 /* Parse a cast-expression.
7765 cast-expression:
7766 unary-expression
7767 ( type-id ) cast-expression
7769 ADDRESS_P is true iff the unary-expression is appearing as the
7770 operand of the `&' operator. CAST_P is true if this expression is
7771 the target of a cast.
7773 Returns a representation of the expression. */
7775 static tree
7776 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7777 bool decltype_p, cp_id_kind * pidk)
7779 /* If it's a `(', then we might be looking at a cast. */
7780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7782 tree type = NULL_TREE;
7783 tree expr = NULL_TREE;
7784 bool cast_expression_p;
7785 const char *saved_message;
7787 /* There's no way to know yet whether or not this is a cast.
7788 For example, `(int (3))' is a unary-expression, while `(int)
7789 3' is a cast. So, we resort to parsing tentatively. */
7790 cp_parser_parse_tentatively (parser);
7791 /* Types may not be defined in a cast. */
7792 saved_message = parser->type_definition_forbidden_message;
7793 parser->type_definition_forbidden_message
7794 = G_("types may not be defined in casts");
7795 /* Consume the `('. */
7796 cp_lexer_consume_token (parser->lexer);
7797 /* A very tricky bit is that `(struct S) { 3 }' is a
7798 compound-literal (which we permit in C++ as an extension).
7799 But, that construct is not a cast-expression -- it is a
7800 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7801 is legal; if the compound-literal were a cast-expression,
7802 you'd need an extra set of parentheses.) But, if we parse
7803 the type-id, and it happens to be a class-specifier, then we
7804 will commit to the parse at that point, because we cannot
7805 undo the action that is done when creating a new class. So,
7806 then we cannot back up and do a postfix-expression.
7807 Another tricky case is the following (c++/29234):
7809 struct S { void operator () (); };
7811 void foo ()
7813 ( S()() );
7816 As a type-id we parse the parenthesized S()() as a function
7817 returning a function, groktypename complains and we cannot
7818 back up in this case either.
7820 Therefore, we scan ahead to the closing `)', and check to see
7821 if the tokens after the `)' can start a cast-expression. Otherwise
7822 we are dealing with an unary-expression, a postfix-expression
7823 or something else.
7825 Save tokens so that we can put them back. */
7826 cp_lexer_save_tokens (parser->lexer);
7828 /* We may be looking at a cast-expression. */
7829 cast_expression_p
7830 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7831 /*consume_paren=*/true)
7832 && cp_parser_tokens_start_cast_expression (parser));
7834 /* Roll back the tokens we skipped. */
7835 cp_lexer_rollback_tokens (parser->lexer);
7836 /* If we aren't looking at a cast-expression, simulate an error so
7837 that the call to cp_parser_parse_definitely below will fail. */
7838 if (!cast_expression_p)
7839 cp_parser_simulate_error (parser);
7840 else
7842 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7843 parser->in_type_id_in_expr_p = true;
7844 /* Look for the type-id. */
7845 type = cp_parser_type_id (parser);
7846 /* Look for the closing `)'. */
7847 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7848 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7851 /* Restore the saved message. */
7852 parser->type_definition_forbidden_message = saved_message;
7854 /* At this point this can only be either a cast or a
7855 parenthesized ctor such as `(T ())' that looks like a cast to
7856 function returning T. */
7857 if (!cp_parser_error_occurred (parser))
7859 cp_parser_parse_definitely (parser);
7860 expr = cp_parser_cast_expression (parser,
7861 /*address_p=*/false,
7862 /*cast_p=*/true,
7863 /*decltype_p=*/false,
7864 pidk);
7866 /* Warn about old-style casts, if so requested. */
7867 if (warn_old_style_cast
7868 && !in_system_header_at (input_location)
7869 && !VOID_TYPE_P (type)
7870 && current_lang_name != lang_name_c)
7871 warning (OPT_Wold_style_cast, "use of old-style cast");
7873 /* Only type conversions to integral or enumeration types
7874 can be used in constant-expressions. */
7875 if (!cast_valid_in_integral_constant_expression_p (type)
7876 && cp_parser_non_integral_constant_expression (parser,
7877 NIC_CAST))
7878 return error_mark_node;
7880 /* Perform the cast. */
7881 expr = build_c_cast (input_location, type, expr);
7882 return expr;
7884 else
7885 cp_parser_abort_tentative_parse (parser);
7888 /* If we get here, then it's not a cast, so it must be a
7889 unary-expression. */
7890 return cp_parser_unary_expression (parser, address_p, cast_p,
7891 decltype_p, pidk);
7894 /* Parse a binary expression of the general form:
7896 pm-expression:
7897 cast-expression
7898 pm-expression .* cast-expression
7899 pm-expression ->* cast-expression
7901 multiplicative-expression:
7902 pm-expression
7903 multiplicative-expression * pm-expression
7904 multiplicative-expression / pm-expression
7905 multiplicative-expression % pm-expression
7907 additive-expression:
7908 multiplicative-expression
7909 additive-expression + multiplicative-expression
7910 additive-expression - multiplicative-expression
7912 shift-expression:
7913 additive-expression
7914 shift-expression << additive-expression
7915 shift-expression >> additive-expression
7917 relational-expression:
7918 shift-expression
7919 relational-expression < shift-expression
7920 relational-expression > shift-expression
7921 relational-expression <= shift-expression
7922 relational-expression >= shift-expression
7924 GNU Extension:
7926 relational-expression:
7927 relational-expression <? shift-expression
7928 relational-expression >? shift-expression
7930 equality-expression:
7931 relational-expression
7932 equality-expression == relational-expression
7933 equality-expression != relational-expression
7935 and-expression:
7936 equality-expression
7937 and-expression & equality-expression
7939 exclusive-or-expression:
7940 and-expression
7941 exclusive-or-expression ^ and-expression
7943 inclusive-or-expression:
7944 exclusive-or-expression
7945 inclusive-or-expression | exclusive-or-expression
7947 logical-and-expression:
7948 inclusive-or-expression
7949 logical-and-expression && inclusive-or-expression
7951 logical-or-expression:
7952 logical-and-expression
7953 logical-or-expression || logical-and-expression
7955 All these are implemented with a single function like:
7957 binary-expression:
7958 simple-cast-expression
7959 binary-expression <token> binary-expression
7961 CAST_P is true if this expression is the target of a cast.
7963 The binops_by_token map is used to get the tree codes for each <token> type.
7964 binary-expressions are associated according to a precedence table. */
7966 #define TOKEN_PRECEDENCE(token) \
7967 (((token->type == CPP_GREATER \
7968 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7969 && !parser->greater_than_is_operator_p) \
7970 ? PREC_NOT_OPERATOR \
7971 : binops_by_token[token->type].prec)
7973 static tree
7974 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7975 bool no_toplevel_fold_p,
7976 bool decltype_p,
7977 enum cp_parser_prec prec,
7978 cp_id_kind * pidk)
7980 cp_parser_expression_stack stack;
7981 cp_parser_expression_stack_entry *sp = &stack[0];
7982 cp_parser_expression_stack_entry current;
7983 tree rhs;
7984 cp_token *token;
7985 enum tree_code rhs_type;
7986 enum cp_parser_prec new_prec, lookahead_prec;
7987 tree overload;
7988 bool parenthesized_not_lhs_warn
7989 = cp_lexer_next_token_is (parser->lexer, CPP_NOT);
7991 /* Parse the first expression. */
7992 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7993 cast_p, decltype_p, pidk);
7994 current.lhs_type = ERROR_MARK;
7995 current.prec = prec;
7997 if (cp_parser_error_occurred (parser))
7998 return error_mark_node;
8000 for (;;)
8002 /* Get an operator token. */
8003 token = cp_lexer_peek_token (parser->lexer);
8005 if (warn_cxx0x_compat
8006 && token->type == CPP_RSHIFT
8007 && !parser->greater_than_is_operator_p)
8009 if (warning_at (token->location, OPT_Wc__0x_compat,
8010 "%<>>%> operator is treated"
8011 " as two right angle brackets in C++11"))
8012 inform (token->location,
8013 "suggest parentheses around %<>>%> expression");
8016 new_prec = TOKEN_PRECEDENCE (token);
8018 /* Popping an entry off the stack means we completed a subexpression:
8019 - either we found a token which is not an operator (`>' where it is not
8020 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8021 will happen repeatedly;
8022 - or, we found an operator which has lower priority. This is the case
8023 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8024 parsing `3 * 4'. */
8025 if (new_prec <= current.prec)
8027 if (sp == stack)
8028 break;
8029 else
8030 goto pop;
8033 get_rhs:
8034 current.tree_type = binops_by_token[token->type].tree_type;
8035 current.loc = token->location;
8037 /* We used the operator token. */
8038 cp_lexer_consume_token (parser->lexer);
8040 /* For "false && x" or "true || x", x will never be executed;
8041 disable warnings while evaluating it. */
8042 if (current.tree_type == TRUTH_ANDIF_EXPR)
8043 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8044 else if (current.tree_type == TRUTH_ORIF_EXPR)
8045 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8047 /* Extract another operand. It may be the RHS of this expression
8048 or the LHS of a new, higher priority expression. */
8049 rhs = cp_parser_simple_cast_expression (parser);
8050 rhs_type = ERROR_MARK;
8052 /* Get another operator token. Look up its precedence to avoid
8053 building a useless (immediately popped) stack entry for common
8054 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8055 token = cp_lexer_peek_token (parser->lexer);
8056 lookahead_prec = TOKEN_PRECEDENCE (token);
8057 if (lookahead_prec > new_prec)
8059 /* ... and prepare to parse the RHS of the new, higher priority
8060 expression. Since precedence levels on the stack are
8061 monotonically increasing, we do not have to care about
8062 stack overflows. */
8063 *sp = current;
8064 ++sp;
8065 current.lhs = rhs;
8066 current.lhs_type = rhs_type;
8067 current.prec = new_prec;
8068 new_prec = lookahead_prec;
8069 goto get_rhs;
8071 pop:
8072 lookahead_prec = new_prec;
8073 /* If the stack is not empty, we have parsed into LHS the right side
8074 (`4' in the example above) of an expression we had suspended.
8075 We can use the information on the stack to recover the LHS (`3')
8076 from the stack together with the tree code (`MULT_EXPR'), and
8077 the precedence of the higher level subexpression
8078 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8079 which will be used to actually build the additive expression. */
8080 rhs = current.lhs;
8081 rhs_type = current.lhs_type;
8082 --sp;
8083 current = *sp;
8086 /* Undo the disabling of warnings done above. */
8087 if (current.tree_type == TRUTH_ANDIF_EXPR)
8088 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8089 else if (current.tree_type == TRUTH_ORIF_EXPR)
8090 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8092 if (warn_logical_not_paren
8093 && parenthesized_not_lhs_warn)
8094 warn_logical_not_parentheses (current.loc, current.tree_type,
8095 TREE_OPERAND (current.lhs, 0), rhs);
8097 overload = NULL;
8098 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8099 ERROR_MARK for everything that is not a binary expression.
8100 This makes warn_about_parentheses miss some warnings that
8101 involve unary operators. For unary expressions we should
8102 pass the correct tree_code unless the unary expression was
8103 surrounded by parentheses.
8105 if (no_toplevel_fold_p
8106 && lookahead_prec <= current.prec
8107 && sp == stack)
8108 current.lhs = build2 (current.tree_type,
8109 TREE_CODE_CLASS (current.tree_type)
8110 == tcc_comparison
8111 ? boolean_type_node : TREE_TYPE (current.lhs),
8112 current.lhs, rhs);
8113 else
8114 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8115 current.lhs, current.lhs_type,
8116 rhs, rhs_type, &overload,
8117 complain_flags (decltype_p));
8118 current.lhs_type = current.tree_type;
8119 if (EXPR_P (current.lhs))
8120 SET_EXPR_LOCATION (current.lhs, current.loc);
8122 /* If the binary operator required the use of an overloaded operator,
8123 then this expression cannot be an integral constant-expression.
8124 An overloaded operator can be used even if both operands are
8125 otherwise permissible in an integral constant-expression if at
8126 least one of the operands is of enumeration type. */
8128 if (overload
8129 && cp_parser_non_integral_constant_expression (parser,
8130 NIC_OVERLOADED))
8131 return error_mark_node;
8134 return current.lhs;
8137 static tree
8138 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8139 bool no_toplevel_fold_p,
8140 enum cp_parser_prec prec,
8141 cp_id_kind * pidk)
8143 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8144 /*decltype*/false, prec, pidk);
8147 /* Parse the `? expression : assignment-expression' part of a
8148 conditional-expression. The LOGICAL_OR_EXPR is the
8149 logical-or-expression that started the conditional-expression.
8150 Returns a representation of the entire conditional-expression.
8152 This routine is used by cp_parser_assignment_expression.
8154 ? expression : assignment-expression
8156 GNU Extensions:
8158 ? : assignment-expression */
8160 static tree
8161 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8163 tree expr;
8164 tree assignment_expr;
8165 struct cp_token *token;
8166 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8168 /* Consume the `?' token. */
8169 cp_lexer_consume_token (parser->lexer);
8170 token = cp_lexer_peek_token (parser->lexer);
8171 if (cp_parser_allow_gnu_extensions_p (parser)
8172 && token->type == CPP_COLON)
8174 pedwarn (token->location, OPT_Wpedantic,
8175 "ISO C++ does not allow ?: with omitted middle operand");
8176 /* Implicit true clause. */
8177 expr = NULL_TREE;
8178 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8179 warn_for_omitted_condop (token->location, logical_or_expr);
8181 else
8183 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8184 parser->colon_corrects_to_scope_p = false;
8185 /* Parse the expression. */
8186 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8187 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8188 c_inhibit_evaluation_warnings +=
8189 ((logical_or_expr == truthvalue_true_node)
8190 - (logical_or_expr == truthvalue_false_node));
8191 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8194 /* The next token should be a `:'. */
8195 cp_parser_require (parser, CPP_COLON, RT_COLON);
8196 /* Parse the assignment-expression. */
8197 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8198 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8200 /* Build the conditional-expression. */
8201 return build_x_conditional_expr (loc, logical_or_expr,
8202 expr,
8203 assignment_expr,
8204 tf_warning_or_error);
8207 /* Parse an assignment-expression.
8209 assignment-expression:
8210 conditional-expression
8211 logical-or-expression assignment-operator assignment_expression
8212 throw-expression
8214 CAST_P is true if this expression is the target of a cast.
8215 DECLTYPE_P is true if this expression is the operand of decltype.
8217 Returns a representation for the expression. */
8219 static tree
8220 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8221 bool decltype_p, cp_id_kind * pidk)
8223 tree expr;
8225 /* If the next token is the `throw' keyword, then we're looking at
8226 a throw-expression. */
8227 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8228 expr = cp_parser_throw_expression (parser);
8229 /* Otherwise, it must be that we are looking at a
8230 logical-or-expression. */
8231 else
8233 /* Parse the binary expressions (logical-or-expression). */
8234 expr = cp_parser_binary_expression (parser, cast_p, false,
8235 decltype_p,
8236 PREC_NOT_OPERATOR, pidk);
8237 /* If the next token is a `?' then we're actually looking at a
8238 conditional-expression. */
8239 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8240 return cp_parser_question_colon_clause (parser, expr);
8241 else
8243 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8245 /* If it's an assignment-operator, we're using the second
8246 production. */
8247 enum tree_code assignment_operator
8248 = cp_parser_assignment_operator_opt (parser);
8249 if (assignment_operator != ERROR_MARK)
8251 bool non_constant_p;
8252 location_t saved_input_location;
8254 /* Parse the right-hand side of the assignment. */
8255 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8257 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8258 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8260 /* An assignment may not appear in a
8261 constant-expression. */
8262 if (cp_parser_non_integral_constant_expression (parser,
8263 NIC_ASSIGNMENT))
8264 return error_mark_node;
8265 /* Build the assignment expression. Its default
8266 location is the location of the '=' token. */
8267 saved_input_location = input_location;
8268 input_location = loc;
8269 expr = build_x_modify_expr (loc, expr,
8270 assignment_operator,
8271 rhs,
8272 complain_flags (decltype_p));
8273 input_location = saved_input_location;
8278 return expr;
8281 static tree
8282 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8283 cp_id_kind * pidk)
8285 return cp_parser_assignment_expression (parser, cast_p,
8286 /*decltype*/false, pidk);
8289 /* Parse an (optional) assignment-operator.
8291 assignment-operator: one of
8292 = *= /= %= += -= >>= <<= &= ^= |=
8294 GNU Extension:
8296 assignment-operator: one of
8297 <?= >?=
8299 If the next token is an assignment operator, the corresponding tree
8300 code is returned, and the token is consumed. For example, for
8301 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8302 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8303 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8304 operator, ERROR_MARK is returned. */
8306 static enum tree_code
8307 cp_parser_assignment_operator_opt (cp_parser* parser)
8309 enum tree_code op;
8310 cp_token *token;
8312 /* Peek at the next token. */
8313 token = cp_lexer_peek_token (parser->lexer);
8315 switch (token->type)
8317 case CPP_EQ:
8318 op = NOP_EXPR;
8319 break;
8321 case CPP_MULT_EQ:
8322 op = MULT_EXPR;
8323 break;
8325 case CPP_DIV_EQ:
8326 op = TRUNC_DIV_EXPR;
8327 break;
8329 case CPP_MOD_EQ:
8330 op = TRUNC_MOD_EXPR;
8331 break;
8333 case CPP_PLUS_EQ:
8334 op = PLUS_EXPR;
8335 break;
8337 case CPP_MINUS_EQ:
8338 op = MINUS_EXPR;
8339 break;
8341 case CPP_RSHIFT_EQ:
8342 op = RSHIFT_EXPR;
8343 break;
8345 case CPP_LSHIFT_EQ:
8346 op = LSHIFT_EXPR;
8347 break;
8349 case CPP_AND_EQ:
8350 op = BIT_AND_EXPR;
8351 break;
8353 case CPP_XOR_EQ:
8354 op = BIT_XOR_EXPR;
8355 break;
8357 case CPP_OR_EQ:
8358 op = BIT_IOR_EXPR;
8359 break;
8361 default:
8362 /* Nothing else is an assignment operator. */
8363 op = ERROR_MARK;
8366 /* If it was an assignment operator, consume it. */
8367 if (op != ERROR_MARK)
8368 cp_lexer_consume_token (parser->lexer);
8370 return op;
8373 /* Parse an expression.
8375 expression:
8376 assignment-expression
8377 expression , assignment-expression
8379 CAST_P is true if this expression is the target of a cast.
8380 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8381 except possibly parenthesized or on the RHS of a comma (N3276).
8383 Returns a representation of the expression. */
8385 static tree
8386 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8387 cp_id_kind * pidk)
8389 tree expression = NULL_TREE;
8390 location_t loc = UNKNOWN_LOCATION;
8392 while (true)
8394 tree assignment_expression;
8396 /* Parse the next assignment-expression. */
8397 assignment_expression
8398 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8400 /* We don't create a temporary for a call that is the immediate operand
8401 of decltype or on the RHS of a comma. But when we see a comma, we
8402 need to create a temporary for a call on the LHS. */
8403 if (decltype_p && !processing_template_decl
8404 && TREE_CODE (assignment_expression) == CALL_EXPR
8405 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8406 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8407 assignment_expression
8408 = build_cplus_new (TREE_TYPE (assignment_expression),
8409 assignment_expression, tf_warning_or_error);
8411 /* If this is the first assignment-expression, we can just
8412 save it away. */
8413 if (!expression)
8414 expression = assignment_expression;
8415 else
8416 expression = build_x_compound_expr (loc, expression,
8417 assignment_expression,
8418 complain_flags (decltype_p));
8419 /* If the next token is not a comma, then we are done with the
8420 expression. */
8421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8422 break;
8423 /* Consume the `,'. */
8424 loc = cp_lexer_peek_token (parser->lexer)->location;
8425 cp_lexer_consume_token (parser->lexer);
8426 /* A comma operator cannot appear in a constant-expression. */
8427 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8428 expression = error_mark_node;
8431 return expression;
8434 static inline tree
8435 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8437 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8440 /* Parse a constant-expression.
8442 constant-expression:
8443 conditional-expression
8445 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8446 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8447 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8448 is false, NON_CONSTANT_P should be NULL. */
8450 static tree
8451 cp_parser_constant_expression (cp_parser* parser,
8452 bool allow_non_constant_p,
8453 bool *non_constant_p)
8455 bool saved_integral_constant_expression_p;
8456 bool saved_allow_non_integral_constant_expression_p;
8457 bool saved_non_integral_constant_expression_p;
8458 tree expression;
8460 /* It might seem that we could simply parse the
8461 conditional-expression, and then check to see if it were
8462 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8463 one that the compiler can figure out is constant, possibly after
8464 doing some simplifications or optimizations. The standard has a
8465 precise definition of constant-expression, and we must honor
8466 that, even though it is somewhat more restrictive.
8468 For example:
8470 int i[(2, 3)];
8472 is not a legal declaration, because `(2, 3)' is not a
8473 constant-expression. The `,' operator is forbidden in a
8474 constant-expression. However, GCC's constant-folding machinery
8475 will fold this operation to an INTEGER_CST for `3'. */
8477 /* Save the old settings. */
8478 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8479 saved_allow_non_integral_constant_expression_p
8480 = parser->allow_non_integral_constant_expression_p;
8481 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8482 /* We are now parsing a constant-expression. */
8483 parser->integral_constant_expression_p = true;
8484 parser->allow_non_integral_constant_expression_p
8485 = (allow_non_constant_p || cxx_dialect >= cxx11);
8486 parser->non_integral_constant_expression_p = false;
8487 /* Although the grammar says "conditional-expression", we parse an
8488 "assignment-expression", which also permits "throw-expression"
8489 and the use of assignment operators. In the case that
8490 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8491 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8492 actually essential that we look for an assignment-expression.
8493 For example, cp_parser_initializer_clauses uses this function to
8494 determine whether a particular assignment-expression is in fact
8495 constant. */
8496 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8497 /* Restore the old settings. */
8498 parser->integral_constant_expression_p
8499 = saved_integral_constant_expression_p;
8500 parser->allow_non_integral_constant_expression_p
8501 = saved_allow_non_integral_constant_expression_p;
8502 if (cxx_dialect >= cxx11)
8504 /* Require an rvalue constant expression here; that's what our
8505 callers expect. Reference constant expressions are handled
8506 separately in e.g. cp_parser_template_argument. */
8507 bool is_const = potential_rvalue_constant_expression (expression);
8508 parser->non_integral_constant_expression_p = !is_const;
8509 if (!is_const && !allow_non_constant_p)
8510 require_potential_rvalue_constant_expression (expression);
8512 if (allow_non_constant_p)
8513 *non_constant_p = parser->non_integral_constant_expression_p;
8514 parser->non_integral_constant_expression_p
8515 = saved_non_integral_constant_expression_p;
8517 return expression;
8520 /* Parse __builtin_offsetof.
8522 offsetof-expression:
8523 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8525 offsetof-member-designator:
8526 id-expression
8527 | offsetof-member-designator "." id-expression
8528 | offsetof-member-designator "[" expression "]"
8529 | offsetof-member-designator "->" id-expression */
8531 static tree
8532 cp_parser_builtin_offsetof (cp_parser *parser)
8534 int save_ice_p, save_non_ice_p;
8535 tree type, expr;
8536 cp_id_kind dummy;
8537 cp_token *token;
8539 /* We're about to accept non-integral-constant things, but will
8540 definitely yield an integral constant expression. Save and
8541 restore these values around our local parsing. */
8542 save_ice_p = parser->integral_constant_expression_p;
8543 save_non_ice_p = parser->non_integral_constant_expression_p;
8545 /* Consume the "__builtin_offsetof" token. */
8546 cp_lexer_consume_token (parser->lexer);
8547 /* Consume the opening `('. */
8548 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8549 /* Parse the type-id. */
8550 type = cp_parser_type_id (parser);
8551 /* Look for the `,'. */
8552 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8553 token = cp_lexer_peek_token (parser->lexer);
8555 /* Build the (type *)null that begins the traditional offsetof macro. */
8556 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8557 tf_warning_or_error);
8559 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8560 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8561 true, &dummy, token->location);
8562 while (true)
8564 token = cp_lexer_peek_token (parser->lexer);
8565 switch (token->type)
8567 case CPP_OPEN_SQUARE:
8568 /* offsetof-member-designator "[" expression "]" */
8569 expr = cp_parser_postfix_open_square_expression (parser, expr,
8570 true, false);
8571 break;
8573 case CPP_DEREF:
8574 /* offsetof-member-designator "->" identifier */
8575 expr = grok_array_decl (token->location, expr,
8576 integer_zero_node, false);
8577 /* FALLTHRU */
8579 case CPP_DOT:
8580 /* offsetof-member-designator "." identifier */
8581 cp_lexer_consume_token (parser->lexer);
8582 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8583 expr, true, &dummy,
8584 token->location);
8585 break;
8587 case CPP_CLOSE_PAREN:
8588 /* Consume the ")" token. */
8589 cp_lexer_consume_token (parser->lexer);
8590 goto success;
8592 default:
8593 /* Error. We know the following require will fail, but
8594 that gives the proper error message. */
8595 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8596 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8597 expr = error_mark_node;
8598 goto failure;
8602 success:
8603 /* If we're processing a template, we can't finish the semantics yet.
8604 Otherwise we can fold the entire expression now. */
8605 if (processing_template_decl)
8606 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8607 else
8608 expr = finish_offsetof (expr);
8610 failure:
8611 parser->integral_constant_expression_p = save_ice_p;
8612 parser->non_integral_constant_expression_p = save_non_ice_p;
8614 return expr;
8617 /* Parse a trait expression.
8619 Returns a representation of the expression, the underlying type
8620 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8622 static tree
8623 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8625 cp_trait_kind kind;
8626 tree type1, type2 = NULL_TREE;
8627 bool binary = false;
8628 cp_decl_specifier_seq decl_specs;
8630 switch (keyword)
8632 case RID_HAS_NOTHROW_ASSIGN:
8633 kind = CPTK_HAS_NOTHROW_ASSIGN;
8634 break;
8635 case RID_HAS_NOTHROW_CONSTRUCTOR:
8636 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8637 break;
8638 case RID_HAS_NOTHROW_COPY:
8639 kind = CPTK_HAS_NOTHROW_COPY;
8640 break;
8641 case RID_HAS_TRIVIAL_ASSIGN:
8642 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8643 break;
8644 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8645 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8646 break;
8647 case RID_HAS_TRIVIAL_COPY:
8648 kind = CPTK_HAS_TRIVIAL_COPY;
8649 break;
8650 case RID_HAS_TRIVIAL_DESTRUCTOR:
8651 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8652 break;
8653 case RID_HAS_VIRTUAL_DESTRUCTOR:
8654 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8655 break;
8656 case RID_IS_ABSTRACT:
8657 kind = CPTK_IS_ABSTRACT;
8658 break;
8659 case RID_IS_BASE_OF:
8660 kind = CPTK_IS_BASE_OF;
8661 binary = true;
8662 break;
8663 case RID_IS_CLASS:
8664 kind = CPTK_IS_CLASS;
8665 break;
8666 case RID_IS_CONVERTIBLE_TO:
8667 kind = CPTK_IS_CONVERTIBLE_TO;
8668 binary = true;
8669 break;
8670 case RID_IS_EMPTY:
8671 kind = CPTK_IS_EMPTY;
8672 break;
8673 case RID_IS_ENUM:
8674 kind = CPTK_IS_ENUM;
8675 break;
8676 case RID_IS_FINAL:
8677 kind = CPTK_IS_FINAL;
8678 break;
8679 case RID_IS_LITERAL_TYPE:
8680 kind = CPTK_IS_LITERAL_TYPE;
8681 break;
8682 case RID_IS_POD:
8683 kind = CPTK_IS_POD;
8684 break;
8685 case RID_IS_POLYMORPHIC:
8686 kind = CPTK_IS_POLYMORPHIC;
8687 break;
8688 case RID_IS_SAME_AS:
8689 kind = CPTK_IS_SAME_AS;
8690 binary = true;
8691 break;
8692 case RID_IS_STD_LAYOUT:
8693 kind = CPTK_IS_STD_LAYOUT;
8694 break;
8695 case RID_IS_TRIVIAL:
8696 kind = CPTK_IS_TRIVIAL;
8697 break;
8698 case RID_IS_UNION:
8699 kind = CPTK_IS_UNION;
8700 break;
8701 case RID_UNDERLYING_TYPE:
8702 kind = CPTK_UNDERLYING_TYPE;
8703 break;
8704 case RID_BASES:
8705 kind = CPTK_BASES;
8706 break;
8707 case RID_DIRECT_BASES:
8708 kind = CPTK_DIRECT_BASES;
8709 break;
8710 default:
8711 gcc_unreachable ();
8714 /* Consume the token. */
8715 cp_lexer_consume_token (parser->lexer);
8717 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8719 type1 = cp_parser_type_id (parser);
8721 if (type1 == error_mark_node)
8722 return error_mark_node;
8724 /* Build a trivial decl-specifier-seq. */
8725 clear_decl_specs (&decl_specs);
8726 decl_specs.type = type1;
8728 /* Call grokdeclarator to figure out what type this is. */
8729 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8730 /*initialized=*/0, /*attrlist=*/NULL);
8732 if (binary)
8734 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8736 type2 = cp_parser_type_id (parser);
8738 if (type2 == error_mark_node)
8739 return error_mark_node;
8741 /* Build a trivial decl-specifier-seq. */
8742 clear_decl_specs (&decl_specs);
8743 decl_specs.type = type2;
8745 /* Call grokdeclarator to figure out what type this is. */
8746 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8747 /*initialized=*/0, /*attrlist=*/NULL);
8750 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8752 /* Complete the trait expression, which may mean either processing
8753 the trait expr now or saving it for template instantiation. */
8754 switch(kind)
8756 case CPTK_UNDERLYING_TYPE:
8757 return finish_underlying_type (type1);
8758 case CPTK_BASES:
8759 return finish_bases (type1, false);
8760 case CPTK_DIRECT_BASES:
8761 return finish_bases (type1, true);
8762 default:
8763 return finish_trait_expr (kind, type1, type2);
8767 /* Lambdas that appear in variable initializer or default argument scope
8768 get that in their mangling, so we need to record it. We might as well
8769 use the count for function and namespace scopes as well. */
8770 static GTY(()) tree lambda_scope;
8771 static GTY(()) int lambda_count;
8772 typedef struct GTY(()) tree_int
8774 tree t;
8775 int i;
8776 } tree_int;
8777 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8779 static void
8780 start_lambda_scope (tree decl)
8782 tree_int ti;
8783 gcc_assert (decl);
8784 /* Once we're inside a function, we ignore other scopes and just push
8785 the function again so that popping works properly. */
8786 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8787 decl = current_function_decl;
8788 ti.t = lambda_scope;
8789 ti.i = lambda_count;
8790 vec_safe_push (lambda_scope_stack, ti);
8791 if (lambda_scope != decl)
8793 /* Don't reset the count if we're still in the same function. */
8794 lambda_scope = decl;
8795 lambda_count = 0;
8799 static void
8800 record_lambda_scope (tree lambda)
8802 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8803 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8806 static void
8807 finish_lambda_scope (void)
8809 tree_int *p = &lambda_scope_stack->last ();
8810 if (lambda_scope != p->t)
8812 lambda_scope = p->t;
8813 lambda_count = p->i;
8815 lambda_scope_stack->pop ();
8818 /* Parse a lambda expression.
8820 lambda-expression:
8821 lambda-introducer lambda-declarator [opt] compound-statement
8823 Returns a representation of the expression. */
8825 static tree
8826 cp_parser_lambda_expression (cp_parser* parser)
8828 tree lambda_expr = build_lambda_expr ();
8829 tree type;
8830 bool ok = true;
8831 cp_token *token = cp_lexer_peek_token (parser->lexer);
8833 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8835 if (cp_unevaluated_operand)
8837 if (!token->error_reported)
8839 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8840 "lambda-expression in unevaluated context");
8841 token->error_reported = true;
8843 ok = false;
8846 /* We may be in the middle of deferred access check. Disable
8847 it now. */
8848 push_deferring_access_checks (dk_no_deferred);
8850 cp_parser_lambda_introducer (parser, lambda_expr);
8852 type = begin_lambda_type (lambda_expr);
8853 if (type == error_mark_node)
8854 return error_mark_node;
8856 record_lambda_scope (lambda_expr);
8858 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8859 determine_visibility (TYPE_NAME (type));
8861 /* Now that we've started the type, add the capture fields for any
8862 explicit captures. */
8863 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8866 /* Inside the class, surrounding template-parameter-lists do not apply. */
8867 unsigned int saved_num_template_parameter_lists
8868 = parser->num_template_parameter_lists;
8869 unsigned char in_statement = parser->in_statement;
8870 bool in_switch_statement_p = parser->in_switch_statement_p;
8871 bool fully_implicit_function_template_p
8872 = parser->fully_implicit_function_template_p;
8873 tree implicit_template_parms = parser->implicit_template_parms;
8874 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8875 bool auto_is_implicit_function_template_parm_p
8876 = parser->auto_is_implicit_function_template_parm_p;
8878 parser->num_template_parameter_lists = 0;
8879 parser->in_statement = 0;
8880 parser->in_switch_statement_p = false;
8881 parser->fully_implicit_function_template_p = false;
8882 parser->implicit_template_parms = 0;
8883 parser->implicit_template_scope = 0;
8884 parser->auto_is_implicit_function_template_parm_p = false;
8886 /* By virtue of defining a local class, a lambda expression has access to
8887 the private variables of enclosing classes. */
8889 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8891 if (ok)
8892 cp_parser_lambda_body (parser, lambda_expr);
8893 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8895 if (cp_parser_skip_to_closing_brace (parser))
8896 cp_lexer_consume_token (parser->lexer);
8899 /* The capture list was built up in reverse order; fix that now. */
8900 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8901 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8903 if (ok)
8904 maybe_add_lambda_conv_op (type);
8906 type = finish_struct (type, /*attributes=*/NULL_TREE);
8908 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8909 parser->in_statement = in_statement;
8910 parser->in_switch_statement_p = in_switch_statement_p;
8911 parser->fully_implicit_function_template_p
8912 = fully_implicit_function_template_p;
8913 parser->implicit_template_parms = implicit_template_parms;
8914 parser->implicit_template_scope = implicit_template_scope;
8915 parser->auto_is_implicit_function_template_parm_p
8916 = auto_is_implicit_function_template_parm_p;
8919 pop_deferring_access_checks ();
8921 /* This field is only used during parsing of the lambda. */
8922 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8924 /* This lambda shouldn't have any proxies left at this point. */
8925 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8926 /* And now that we're done, push proxies for an enclosing lambda. */
8927 insert_pending_capture_proxies ();
8929 if (ok)
8930 return build_lambda_object (lambda_expr);
8931 else
8932 return error_mark_node;
8935 /* Parse the beginning of a lambda expression.
8937 lambda-introducer:
8938 [ lambda-capture [opt] ]
8940 LAMBDA_EXPR is the current representation of the lambda expression. */
8942 static void
8943 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8945 /* Need commas after the first capture. */
8946 bool first = true;
8948 /* Eat the leading `['. */
8949 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8951 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8952 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8953 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8954 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8955 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8956 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8958 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8960 cp_lexer_consume_token (parser->lexer);
8961 first = false;
8964 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8966 cp_token* capture_token;
8967 tree capture_id;
8968 tree capture_init_expr;
8969 cp_id_kind idk = CP_ID_KIND_NONE;
8970 bool explicit_init_p = false;
8972 enum capture_kind_type
8974 BY_COPY,
8975 BY_REFERENCE
8977 enum capture_kind_type capture_kind = BY_COPY;
8979 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8981 error ("expected end of capture-list");
8982 return;
8985 if (first)
8986 first = false;
8987 else
8988 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8990 /* Possibly capture `this'. */
8991 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8993 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8994 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8995 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8996 "with by-copy capture default");
8997 cp_lexer_consume_token (parser->lexer);
8998 add_capture (lambda_expr,
8999 /*id=*/this_identifier,
9000 /*initializer=*/finish_this_expr(),
9001 /*by_reference_p=*/false,
9002 explicit_init_p);
9003 continue;
9006 /* Remember whether we want to capture as a reference or not. */
9007 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9009 capture_kind = BY_REFERENCE;
9010 cp_lexer_consume_token (parser->lexer);
9013 /* Get the identifier. */
9014 capture_token = cp_lexer_peek_token (parser->lexer);
9015 capture_id = cp_parser_identifier (parser);
9017 if (capture_id == error_mark_node)
9018 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9019 delimiters, but I modified this to stop on unnested ']' as well. It
9020 was already changed to stop on unnested '}', so the
9021 "closing_parenthesis" name is no more misleading with my change. */
9023 cp_parser_skip_to_closing_parenthesis (parser,
9024 /*recovering=*/true,
9025 /*or_comma=*/true,
9026 /*consume_paren=*/true);
9027 break;
9030 /* Find the initializer for this capture. */
9031 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9032 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9033 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9035 bool direct, non_constant;
9036 /* An explicit initializer exists. */
9037 if (cxx_dialect < cxx1y)
9038 pedwarn (input_location, 0,
9039 "lambda capture initializers "
9040 "only available with -std=c++1y or -std=gnu++1y");
9041 capture_init_expr = cp_parser_initializer (parser, &direct,
9042 &non_constant);
9043 explicit_init_p = true;
9044 if (capture_init_expr == NULL_TREE)
9046 error ("empty initializer for lambda init-capture");
9047 capture_init_expr = error_mark_node;
9050 else
9052 const char* error_msg;
9054 /* Turn the identifier into an id-expression. */
9055 capture_init_expr
9056 = cp_parser_lookup_name_simple (parser, capture_id,
9057 capture_token->location);
9059 if (capture_init_expr == error_mark_node)
9061 unqualified_name_lookup_error (capture_id);
9062 continue;
9064 else if (DECL_P (capture_init_expr)
9065 && (!VAR_P (capture_init_expr)
9066 && TREE_CODE (capture_init_expr) != PARM_DECL))
9068 error_at (capture_token->location,
9069 "capture of non-variable %qD ",
9070 capture_init_expr);
9071 inform (0, "%q+#D declared here", capture_init_expr);
9072 continue;
9074 if (VAR_P (capture_init_expr)
9075 && decl_storage_duration (capture_init_expr) != dk_auto)
9077 if (pedwarn (capture_token->location, 0, "capture of variable "
9078 "%qD with non-automatic storage duration",
9079 capture_init_expr))
9080 inform (0, "%q+#D declared here", capture_init_expr);
9081 continue;
9084 capture_init_expr
9085 = finish_id_expression
9086 (capture_id,
9087 capture_init_expr,
9088 parser->scope,
9089 &idk,
9090 /*integral_constant_expression_p=*/false,
9091 /*allow_non_integral_constant_expression_p=*/false,
9092 /*non_integral_constant_expression_p=*/NULL,
9093 /*template_p=*/false,
9094 /*done=*/true,
9095 /*address_p=*/false,
9096 /*template_arg_p=*/false,
9097 &error_msg,
9098 capture_token->location);
9100 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9102 cp_lexer_consume_token (parser->lexer);
9103 capture_init_expr = make_pack_expansion (capture_init_expr);
9105 else
9106 check_for_bare_parameter_packs (capture_init_expr);
9109 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9110 && !explicit_init_p)
9112 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9113 && capture_kind == BY_COPY)
9114 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9115 "of %qD redundant with by-copy capture default",
9116 capture_id);
9117 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9118 && capture_kind == BY_REFERENCE)
9119 pedwarn (capture_token->location, 0, "explicit by-reference "
9120 "capture of %qD redundant with by-reference capture "
9121 "default", capture_id);
9124 add_capture (lambda_expr,
9125 capture_id,
9126 capture_init_expr,
9127 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9128 explicit_init_p);
9131 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9134 /* Parse the (optional) middle of a lambda expression.
9136 lambda-declarator:
9137 < template-parameter-list [opt] >
9138 ( parameter-declaration-clause [opt] )
9139 attribute-specifier [opt]
9140 mutable [opt]
9141 exception-specification [opt]
9142 lambda-return-type-clause [opt]
9144 LAMBDA_EXPR is the current representation of the lambda expression. */
9146 static bool
9147 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9149 /* 5.1.1.4 of the standard says:
9150 If a lambda-expression does not include a lambda-declarator, it is as if
9151 the lambda-declarator were ().
9152 This means an empty parameter list, no attributes, and no exception
9153 specification. */
9154 tree param_list = void_list_node;
9155 tree attributes = NULL_TREE;
9156 tree exception_spec = NULL_TREE;
9157 tree template_param_list = NULL_TREE;
9159 /* The template-parameter-list is optional, but must begin with
9160 an opening angle if present. */
9161 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9163 if (cxx_dialect < cxx1y)
9164 pedwarn (parser->lexer->next_token->location, 0,
9165 "lambda templates are only available with "
9166 "-std=c++1y or -std=gnu++1y");
9168 cp_lexer_consume_token (parser->lexer);
9170 template_param_list = cp_parser_template_parameter_list (parser);
9172 cp_parser_skip_to_end_of_template_parameter_list (parser);
9174 /* We just processed one more parameter list. */
9175 ++parser->num_template_parameter_lists;
9178 /* The parameter-declaration-clause is optional (unless
9179 template-parameter-list was given), but must begin with an
9180 opening parenthesis if present. */
9181 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9183 cp_lexer_consume_token (parser->lexer);
9185 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9187 /* Parse parameters. */
9188 param_list = cp_parser_parameter_declaration_clause (parser);
9190 /* Default arguments shall not be specified in the
9191 parameter-declaration-clause of a lambda-declarator. */
9192 for (tree t = param_list; t; t = TREE_CHAIN (t))
9193 if (TREE_PURPOSE (t))
9194 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9195 "default argument specified for lambda parameter");
9197 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9199 attributes = cp_parser_attributes_opt (parser);
9201 /* Parse optional `mutable' keyword. */
9202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9204 cp_lexer_consume_token (parser->lexer);
9205 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9208 /* Parse optional exception specification. */
9209 exception_spec = cp_parser_exception_specification_opt (parser);
9211 /* Parse optional trailing return type. */
9212 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9214 cp_lexer_consume_token (parser->lexer);
9215 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9216 = cp_parser_trailing_type_id (parser);
9219 /* The function parameters must be in scope all the way until after the
9220 trailing-return-type in case of decltype. */
9221 pop_bindings_and_leave_scope ();
9223 else if (template_param_list != NULL_TREE) // generate diagnostic
9224 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9226 /* Create the function call operator.
9228 Messing with declarators like this is no uglier than building up the
9229 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9230 other code. */
9232 cp_decl_specifier_seq return_type_specs;
9233 cp_declarator* declarator;
9234 tree fco;
9235 int quals;
9236 void *p;
9238 clear_decl_specs (&return_type_specs);
9239 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9240 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9241 else
9242 /* Maybe we will deduce the return type later. */
9243 return_type_specs.type = make_auto ();
9245 p = obstack_alloc (&declarator_obstack, 0);
9247 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9248 sfk_none);
9250 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9251 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9252 declarator = make_call_declarator (declarator, param_list, quals,
9253 VIRT_SPEC_UNSPECIFIED,
9254 REF_QUAL_NONE,
9255 exception_spec,
9256 /*late_return_type=*/NULL_TREE);
9257 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9259 fco = grokmethod (&return_type_specs,
9260 declarator,
9261 attributes);
9262 if (fco != error_mark_node)
9264 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9265 DECL_ARTIFICIAL (fco) = 1;
9266 /* Give the object parameter a different name. */
9267 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9268 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9269 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9271 if (template_param_list)
9273 fco = finish_member_template_decl (fco);
9274 finish_template_decl (template_param_list);
9275 --parser->num_template_parameter_lists;
9277 else if (parser->fully_implicit_function_template_p)
9278 fco = finish_fully_implicit_template (parser, fco);
9280 finish_member_declaration (fco);
9282 obstack_free (&declarator_obstack, p);
9284 return (fco != error_mark_node);
9288 /* Parse the body of a lambda expression, which is simply
9290 compound-statement
9292 but which requires special handling.
9293 LAMBDA_EXPR is the current representation of the lambda expression. */
9295 static void
9296 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9298 bool nested = (current_function_decl != NULL_TREE);
9299 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9300 if (nested)
9301 push_function_context ();
9302 else
9303 /* Still increment function_depth so that we don't GC in the
9304 middle of an expression. */
9305 ++function_depth;
9306 /* Clear this in case we're in the middle of a default argument. */
9307 parser->local_variables_forbidden_p = false;
9309 /* Finish the function call operator
9310 - class_specifier
9311 + late_parsing_for_member
9312 + function_definition_after_declarator
9313 + ctor_initializer_opt_and_function_body */
9315 tree fco = lambda_function (lambda_expr);
9316 tree body;
9317 bool done = false;
9318 tree compound_stmt;
9319 tree cap;
9321 /* Let the front end know that we are going to be defining this
9322 function. */
9323 start_preparsed_function (fco,
9324 NULL_TREE,
9325 SF_PRE_PARSED | SF_INCLASS_INLINE);
9327 start_lambda_scope (fco);
9328 body = begin_function_body ();
9330 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9331 goto out;
9333 /* Push the proxies for any explicit captures. */
9334 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9335 cap = TREE_CHAIN (cap))
9336 build_capture_proxy (TREE_PURPOSE (cap));
9338 compound_stmt = begin_compound_stmt (0);
9340 /* 5.1.1.4 of the standard says:
9341 If a lambda-expression does not include a trailing-return-type, it
9342 is as if the trailing-return-type denotes the following type:
9343 * if the compound-statement is of the form
9344 { return attribute-specifier [opt] expression ; }
9345 the type of the returned expression after lvalue-to-rvalue
9346 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9347 (_conv.array_ 4.2), and function-to-pointer conversion
9348 (_conv.func_ 4.3);
9349 * otherwise, void. */
9351 /* In a lambda that has neither a lambda-return-type-clause
9352 nor a deducible form, errors should be reported for return statements
9353 in the body. Since we used void as the placeholder return type, parsing
9354 the body as usual will give such desired behavior. */
9355 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9356 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9357 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9359 tree expr = NULL_TREE;
9360 cp_id_kind idk = CP_ID_KIND_NONE;
9362 /* Parse tentatively in case there's more after the initial return
9363 statement. */
9364 cp_parser_parse_tentatively (parser);
9366 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9368 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9371 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9373 if (cp_parser_parse_definitely (parser))
9375 if (!processing_template_decl)
9376 apply_deduced_return_type (fco, lambda_return_type (expr));
9378 /* Will get error here if type not deduced yet. */
9379 finish_return_stmt (expr);
9381 done = true;
9385 if (!done)
9387 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9388 cp_parser_label_declaration (parser);
9389 cp_parser_statement_seq_opt (parser, NULL_TREE);
9390 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9393 finish_compound_stmt (compound_stmt);
9395 out:
9396 finish_function_body (body);
9397 finish_lambda_scope ();
9399 /* Finish the function and generate code for it if necessary. */
9400 tree fn = finish_function (/*inline*/2);
9402 /* Only expand if the call op is not a template. */
9403 if (!DECL_TEMPLATE_INFO (fco))
9404 expand_or_defer_fn (fn);
9407 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9408 if (nested)
9409 pop_function_context();
9410 else
9411 --function_depth;
9414 /* Statements [gram.stmt.stmt] */
9416 /* Parse a statement.
9418 statement:
9419 labeled-statement
9420 expression-statement
9421 compound-statement
9422 selection-statement
9423 iteration-statement
9424 jump-statement
9425 declaration-statement
9426 try-block
9428 C++11:
9430 statement:
9431 labeled-statement
9432 attribute-specifier-seq (opt) expression-statement
9433 attribute-specifier-seq (opt) compound-statement
9434 attribute-specifier-seq (opt) selection-statement
9435 attribute-specifier-seq (opt) iteration-statement
9436 attribute-specifier-seq (opt) jump-statement
9437 declaration-statement
9438 attribute-specifier-seq (opt) try-block
9440 TM Extension:
9442 statement:
9443 atomic-statement
9445 IN_COMPOUND is true when the statement is nested inside a
9446 cp_parser_compound_statement; this matters for certain pragmas.
9448 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9449 is a (possibly labeled) if statement which is not enclosed in braces
9450 and has an else clause. This is used to implement -Wparentheses. */
9452 static void
9453 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9454 bool in_compound, bool *if_p)
9456 tree statement, std_attrs = NULL_TREE;
9457 cp_token *token;
9458 location_t statement_location, attrs_location;
9460 restart:
9461 if (if_p != NULL)
9462 *if_p = false;
9463 /* There is no statement yet. */
9464 statement = NULL_TREE;
9466 cp_lexer_save_tokens (parser->lexer);
9467 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9468 if (c_dialect_objc ())
9469 /* In obj-c++, seeing '[[' might be the either the beginning of
9470 c++11 attributes, or a nested objc-message-expression. So
9471 let's parse the c++11 attributes tentatively. */
9472 cp_parser_parse_tentatively (parser);
9473 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9474 if (c_dialect_objc ())
9476 if (!cp_parser_parse_definitely (parser))
9477 std_attrs = NULL_TREE;
9480 /* Peek at the next token. */
9481 token = cp_lexer_peek_token (parser->lexer);
9482 /* Remember the location of the first token in the statement. */
9483 statement_location = token->location;
9484 /* If this is a keyword, then that will often determine what kind of
9485 statement we have. */
9486 if (token->type == CPP_KEYWORD)
9488 enum rid keyword = token->keyword;
9490 switch (keyword)
9492 case RID_CASE:
9493 case RID_DEFAULT:
9494 /* Looks like a labeled-statement with a case label.
9495 Parse the label, and then use tail recursion to parse
9496 the statement. */
9497 cp_parser_label_for_labeled_statement (parser, std_attrs);
9498 goto restart;
9500 case RID_IF:
9501 case RID_SWITCH:
9502 statement = cp_parser_selection_statement (parser, if_p);
9503 break;
9505 case RID_WHILE:
9506 case RID_DO:
9507 case RID_FOR:
9508 statement = cp_parser_iteration_statement (parser, false);
9509 break;
9511 case RID_BREAK:
9512 case RID_CONTINUE:
9513 case RID_RETURN:
9514 case RID_GOTO:
9515 statement = cp_parser_jump_statement (parser);
9516 break;
9518 case RID_CILK_SYNC:
9519 cp_lexer_consume_token (parser->lexer);
9520 if (flag_cilkplus)
9522 tree sync_expr = build_cilk_sync ();
9523 SET_EXPR_LOCATION (sync_expr,
9524 token->location);
9525 statement = finish_expr_stmt (sync_expr);
9527 else
9529 error_at (token->location, "-fcilkplus must be enabled to use"
9530 " %<_Cilk_sync%>");
9531 statement = error_mark_node;
9533 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9534 break;
9536 /* Objective-C++ exception-handling constructs. */
9537 case RID_AT_TRY:
9538 case RID_AT_CATCH:
9539 case RID_AT_FINALLY:
9540 case RID_AT_SYNCHRONIZED:
9541 case RID_AT_THROW:
9542 statement = cp_parser_objc_statement (parser);
9543 break;
9545 case RID_TRY:
9546 statement = cp_parser_try_block (parser);
9547 break;
9549 case RID_NAMESPACE:
9550 /* This must be a namespace alias definition. */
9551 cp_parser_declaration_statement (parser);
9552 return;
9554 case RID_TRANSACTION_ATOMIC:
9555 case RID_TRANSACTION_RELAXED:
9556 statement = cp_parser_transaction (parser, keyword);
9557 break;
9558 case RID_TRANSACTION_CANCEL:
9559 statement = cp_parser_transaction_cancel (parser);
9560 break;
9562 default:
9563 /* It might be a keyword like `int' that can start a
9564 declaration-statement. */
9565 break;
9568 else if (token->type == CPP_NAME)
9570 /* If the next token is a `:', then we are looking at a
9571 labeled-statement. */
9572 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9573 if (token->type == CPP_COLON)
9575 /* Looks like a labeled-statement with an ordinary label.
9576 Parse the label, and then use tail recursion to parse
9577 the statement. */
9579 cp_parser_label_for_labeled_statement (parser, std_attrs);
9580 goto restart;
9583 /* Anything that starts with a `{' must be a compound-statement. */
9584 else if (token->type == CPP_OPEN_BRACE)
9585 statement = cp_parser_compound_statement (parser, NULL, false, false);
9586 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9587 a statement all its own. */
9588 else if (token->type == CPP_PRAGMA)
9590 /* Only certain OpenMP pragmas are attached to statements, and thus
9591 are considered statements themselves. All others are not. In
9592 the context of a compound, accept the pragma as a "statement" and
9593 return so that we can check for a close brace. Otherwise we
9594 require a real statement and must go back and read one. */
9595 if (in_compound)
9596 cp_parser_pragma (parser, pragma_compound);
9597 else if (!cp_parser_pragma (parser, pragma_stmt))
9598 goto restart;
9599 return;
9601 else if (token->type == CPP_EOF)
9603 cp_parser_error (parser, "expected statement");
9604 return;
9607 /* Everything else must be a declaration-statement or an
9608 expression-statement. Try for the declaration-statement
9609 first, unless we are looking at a `;', in which case we know that
9610 we have an expression-statement. */
9611 if (!statement)
9613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9615 if (std_attrs != NULL_TREE)
9617 /* Attributes should be parsed as part of the the
9618 declaration, so let's un-parse them. */
9619 cp_lexer_rollback_tokens (parser->lexer);
9620 std_attrs = NULL_TREE;
9623 cp_parser_parse_tentatively (parser);
9624 /* Try to parse the declaration-statement. */
9625 cp_parser_declaration_statement (parser);
9626 /* If that worked, we're done. */
9627 if (cp_parser_parse_definitely (parser))
9628 return;
9630 /* Look for an expression-statement instead. */
9631 statement = cp_parser_expression_statement (parser, in_statement_expr);
9634 /* Set the line number for the statement. */
9635 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9636 SET_EXPR_LOCATION (statement, statement_location);
9638 /* Note that for now, we don't do anything with c++11 statements
9639 parsed at this level. */
9640 if (std_attrs != NULL_TREE)
9641 warning_at (attrs_location,
9642 OPT_Wattributes,
9643 "attributes at the beginning of statement are ignored");
9646 /* Parse the label for a labeled-statement, i.e.
9648 identifier :
9649 case constant-expression :
9650 default :
9652 GNU Extension:
9653 case constant-expression ... constant-expression : statement
9655 When a label is parsed without errors, the label is added to the
9656 parse tree by the finish_* functions, so this function doesn't
9657 have to return the label. */
9659 static void
9660 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9662 cp_token *token;
9663 tree label = NULL_TREE;
9664 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9666 /* The next token should be an identifier. */
9667 token = cp_lexer_peek_token (parser->lexer);
9668 if (token->type != CPP_NAME
9669 && token->type != CPP_KEYWORD)
9671 cp_parser_error (parser, "expected labeled-statement");
9672 return;
9675 parser->colon_corrects_to_scope_p = false;
9676 switch (token->keyword)
9678 case RID_CASE:
9680 tree expr, expr_hi;
9681 cp_token *ellipsis;
9683 /* Consume the `case' token. */
9684 cp_lexer_consume_token (parser->lexer);
9685 /* Parse the constant-expression. */
9686 expr = cp_parser_constant_expression (parser,
9687 /*allow_non_constant_p=*/false,
9688 NULL);
9690 ellipsis = cp_lexer_peek_token (parser->lexer);
9691 if (ellipsis->type == CPP_ELLIPSIS)
9693 /* Consume the `...' token. */
9694 cp_lexer_consume_token (parser->lexer);
9695 expr_hi =
9696 cp_parser_constant_expression (parser,
9697 /*allow_non_constant_p=*/false,
9698 NULL);
9699 /* We don't need to emit warnings here, as the common code
9700 will do this for us. */
9702 else
9703 expr_hi = NULL_TREE;
9705 if (parser->in_switch_statement_p)
9706 finish_case_label (token->location, expr, expr_hi);
9707 else
9708 error_at (token->location,
9709 "case label %qE not within a switch statement",
9710 expr);
9712 break;
9714 case RID_DEFAULT:
9715 /* Consume the `default' token. */
9716 cp_lexer_consume_token (parser->lexer);
9718 if (parser->in_switch_statement_p)
9719 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9720 else
9721 error_at (token->location, "case label not within a switch statement");
9722 break;
9724 default:
9725 /* Anything else must be an ordinary label. */
9726 label = finish_label_stmt (cp_parser_identifier (parser));
9727 break;
9730 /* Require the `:' token. */
9731 cp_parser_require (parser, CPP_COLON, RT_COLON);
9733 /* An ordinary label may optionally be followed by attributes.
9734 However, this is only permitted if the attributes are then
9735 followed by a semicolon. This is because, for backward
9736 compatibility, when parsing
9737 lab: __attribute__ ((unused)) int i;
9738 we want the attribute to attach to "i", not "lab". */
9739 if (label != NULL_TREE
9740 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9742 tree attrs;
9743 cp_parser_parse_tentatively (parser);
9744 attrs = cp_parser_gnu_attributes_opt (parser);
9745 if (attrs == NULL_TREE
9746 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9747 cp_parser_abort_tentative_parse (parser);
9748 else if (!cp_parser_parse_definitely (parser))
9750 else
9751 attributes = chainon (attributes, attrs);
9754 if (attributes != NULL_TREE)
9755 cplus_decl_attributes (&label, attributes, 0);
9757 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9760 /* Parse an expression-statement.
9762 expression-statement:
9763 expression [opt] ;
9765 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9766 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9767 indicates whether this expression-statement is part of an
9768 expression statement. */
9770 static tree
9771 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9773 tree statement = NULL_TREE;
9774 cp_token *token = cp_lexer_peek_token (parser->lexer);
9776 /* If the next token is a ';', then there is no expression
9777 statement. */
9778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9780 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9781 if (statement == error_mark_node
9782 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9784 cp_parser_skip_to_end_of_block_or_statement (parser);
9785 return error_mark_node;
9789 /* Give a helpful message for "A<T>::type t;" and the like. */
9790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9791 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9793 if (TREE_CODE (statement) == SCOPE_REF)
9794 error_at (token->location, "need %<typename%> before %qE because "
9795 "%qT is a dependent scope",
9796 statement, TREE_OPERAND (statement, 0));
9797 else if (is_overloaded_fn (statement)
9798 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9800 /* A::A a; */
9801 tree fn = get_first_fn (statement);
9802 error_at (token->location,
9803 "%<%T::%D%> names the constructor, not the type",
9804 DECL_CONTEXT (fn), DECL_NAME (fn));
9808 /* Consume the final `;'. */
9809 cp_parser_consume_semicolon_at_end_of_statement (parser);
9811 if (in_statement_expr
9812 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9813 /* This is the final expression statement of a statement
9814 expression. */
9815 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9816 else if (statement)
9817 statement = finish_expr_stmt (statement);
9819 return statement;
9822 /* Parse a compound-statement.
9824 compound-statement:
9825 { statement-seq [opt] }
9827 GNU extension:
9829 compound-statement:
9830 { label-declaration-seq [opt] statement-seq [opt] }
9832 label-declaration-seq:
9833 label-declaration
9834 label-declaration-seq label-declaration
9836 Returns a tree representing the statement. */
9838 static tree
9839 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9840 bool in_try, bool function_body)
9842 tree compound_stmt;
9844 /* Consume the `{'. */
9845 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9846 return error_mark_node;
9847 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9848 && !function_body)
9849 pedwarn (input_location, OPT_Wpedantic,
9850 "compound-statement in constexpr function");
9851 /* Begin the compound-statement. */
9852 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9853 /* If the next keyword is `__label__' we have a label declaration. */
9854 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9855 cp_parser_label_declaration (parser);
9856 /* Parse an (optional) statement-seq. */
9857 cp_parser_statement_seq_opt (parser, in_statement_expr);
9858 /* Finish the compound-statement. */
9859 finish_compound_stmt (compound_stmt);
9860 /* Consume the `}'. */
9861 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9863 return compound_stmt;
9866 /* Parse an (optional) statement-seq.
9868 statement-seq:
9869 statement
9870 statement-seq [opt] statement */
9872 static void
9873 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9875 /* Scan statements until there aren't any more. */
9876 while (true)
9878 cp_token *token = cp_lexer_peek_token (parser->lexer);
9880 /* If we are looking at a `}', then we have run out of
9881 statements; the same is true if we have reached the end
9882 of file, or have stumbled upon a stray '@end'. */
9883 if (token->type == CPP_CLOSE_BRACE
9884 || token->type == CPP_EOF
9885 || token->type == CPP_PRAGMA_EOL
9886 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9887 break;
9889 /* If we are in a compound statement and find 'else' then
9890 something went wrong. */
9891 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9893 if (parser->in_statement & IN_IF_STMT)
9894 break;
9895 else
9897 token = cp_lexer_consume_token (parser->lexer);
9898 error_at (token->location, "%<else%> without a previous %<if%>");
9902 /* Parse the statement. */
9903 cp_parser_statement (parser, in_statement_expr, true, NULL);
9907 /* Parse a selection-statement.
9909 selection-statement:
9910 if ( condition ) statement
9911 if ( condition ) statement else statement
9912 switch ( condition ) statement
9914 Returns the new IF_STMT or SWITCH_STMT.
9916 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9917 is a (possibly labeled) if statement which is not enclosed in
9918 braces and has an else clause. This is used to implement
9919 -Wparentheses. */
9921 static tree
9922 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9924 cp_token *token;
9925 enum rid keyword;
9927 if (if_p != NULL)
9928 *if_p = false;
9930 /* Peek at the next token. */
9931 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9933 /* See what kind of keyword it is. */
9934 keyword = token->keyword;
9935 switch (keyword)
9937 case RID_IF:
9938 case RID_SWITCH:
9940 tree statement;
9941 tree condition;
9943 /* Look for the `('. */
9944 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9946 cp_parser_skip_to_end_of_statement (parser);
9947 return error_mark_node;
9950 /* Begin the selection-statement. */
9951 if (keyword == RID_IF)
9952 statement = begin_if_stmt ();
9953 else
9954 statement = begin_switch_stmt ();
9956 /* Parse the condition. */
9957 condition = cp_parser_condition (parser);
9958 /* Look for the `)'. */
9959 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9960 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9961 /*consume_paren=*/true);
9963 if (keyword == RID_IF)
9965 bool nested_if;
9966 unsigned char in_statement;
9968 /* Add the condition. */
9969 finish_if_stmt_cond (condition, statement);
9971 /* Parse the then-clause. */
9972 in_statement = parser->in_statement;
9973 parser->in_statement |= IN_IF_STMT;
9974 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9977 add_stmt (build_empty_stmt (loc));
9978 cp_lexer_consume_token (parser->lexer);
9979 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9980 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9981 "empty body in an %<if%> statement");
9982 nested_if = false;
9984 else
9985 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9986 parser->in_statement = in_statement;
9988 finish_then_clause (statement);
9990 /* If the next token is `else', parse the else-clause. */
9991 if (cp_lexer_next_token_is_keyword (parser->lexer,
9992 RID_ELSE))
9994 /* Consume the `else' keyword. */
9995 cp_lexer_consume_token (parser->lexer);
9996 begin_else_clause (statement);
9997 /* Parse the else-clause. */
9998 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10000 location_t loc;
10001 loc = cp_lexer_peek_token (parser->lexer)->location;
10002 warning_at (loc,
10003 OPT_Wempty_body, "suggest braces around "
10004 "empty body in an %<else%> statement");
10005 add_stmt (build_empty_stmt (loc));
10006 cp_lexer_consume_token (parser->lexer);
10008 else
10009 cp_parser_implicitly_scoped_statement (parser, NULL);
10011 finish_else_clause (statement);
10013 /* If we are currently parsing a then-clause, then
10014 IF_P will not be NULL. We set it to true to
10015 indicate that this if statement has an else clause.
10016 This may trigger the Wparentheses warning below
10017 when we get back up to the parent if statement. */
10018 if (if_p != NULL)
10019 *if_p = true;
10021 else
10023 /* This if statement does not have an else clause. If
10024 NESTED_IF is true, then the then-clause is an if
10025 statement which does have an else clause. We warn
10026 about the potential ambiguity. */
10027 if (nested_if)
10028 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10029 "suggest explicit braces to avoid ambiguous"
10030 " %<else%>");
10033 /* Now we're all done with the if-statement. */
10034 finish_if_stmt (statement);
10036 else
10038 bool in_switch_statement_p;
10039 unsigned char in_statement;
10041 /* Add the condition. */
10042 finish_switch_cond (condition, statement);
10044 /* Parse the body of the switch-statement. */
10045 in_switch_statement_p = parser->in_switch_statement_p;
10046 in_statement = parser->in_statement;
10047 parser->in_switch_statement_p = true;
10048 parser->in_statement |= IN_SWITCH_STMT;
10049 cp_parser_implicitly_scoped_statement (parser, NULL);
10050 parser->in_switch_statement_p = in_switch_statement_p;
10051 parser->in_statement = in_statement;
10053 /* Now we're all done with the switch-statement. */
10054 finish_switch_stmt (statement);
10057 return statement;
10059 break;
10061 default:
10062 cp_parser_error (parser, "expected selection-statement");
10063 return error_mark_node;
10067 /* Parse a condition.
10069 condition:
10070 expression
10071 type-specifier-seq declarator = initializer-clause
10072 type-specifier-seq declarator braced-init-list
10074 GNU Extension:
10076 condition:
10077 type-specifier-seq declarator asm-specification [opt]
10078 attributes [opt] = assignment-expression
10080 Returns the expression that should be tested. */
10082 static tree
10083 cp_parser_condition (cp_parser* parser)
10085 cp_decl_specifier_seq type_specifiers;
10086 const char *saved_message;
10087 int declares_class_or_enum;
10089 /* Try the declaration first. */
10090 cp_parser_parse_tentatively (parser);
10091 /* New types are not allowed in the type-specifier-seq for a
10092 condition. */
10093 saved_message = parser->type_definition_forbidden_message;
10094 parser->type_definition_forbidden_message
10095 = G_("types may not be defined in conditions");
10096 /* Parse the type-specifier-seq. */
10097 cp_parser_decl_specifier_seq (parser,
10098 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10099 &type_specifiers,
10100 &declares_class_or_enum);
10101 /* Restore the saved message. */
10102 parser->type_definition_forbidden_message = saved_message;
10103 /* If all is well, we might be looking at a declaration. */
10104 if (!cp_parser_error_occurred (parser))
10106 tree decl;
10107 tree asm_specification;
10108 tree attributes;
10109 cp_declarator *declarator;
10110 tree initializer = NULL_TREE;
10112 /* Parse the declarator. */
10113 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10114 /*ctor_dtor_or_conv_p=*/NULL,
10115 /*parenthesized_p=*/NULL,
10116 /*member_p=*/false,
10117 /*friend_p=*/false);
10118 /* Parse the attributes. */
10119 attributes = cp_parser_attributes_opt (parser);
10120 /* Parse the asm-specification. */
10121 asm_specification = cp_parser_asm_specification_opt (parser);
10122 /* If the next token is not an `=' or '{', then we might still be
10123 looking at an expression. For example:
10125 if (A(a).x)
10127 looks like a decl-specifier-seq and a declarator -- but then
10128 there is no `=', so this is an expression. */
10129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10130 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10131 cp_parser_simulate_error (parser);
10133 /* If we did see an `=' or '{', then we are looking at a declaration
10134 for sure. */
10135 if (cp_parser_parse_definitely (parser))
10137 tree pushed_scope;
10138 bool non_constant_p;
10139 bool flags = LOOKUP_ONLYCONVERTING;
10141 /* Create the declaration. */
10142 decl = start_decl (declarator, &type_specifiers,
10143 /*initialized_p=*/true,
10144 attributes, /*prefix_attributes=*/NULL_TREE,
10145 &pushed_scope);
10147 /* Parse the initializer. */
10148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10150 initializer = cp_parser_braced_list (parser, &non_constant_p);
10151 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10152 flags = 0;
10154 else
10156 /* Consume the `='. */
10157 cp_parser_require (parser, CPP_EQ, RT_EQ);
10158 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10160 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10161 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10163 /* Process the initializer. */
10164 cp_finish_decl (decl,
10165 initializer, !non_constant_p,
10166 asm_specification,
10167 flags);
10169 if (pushed_scope)
10170 pop_scope (pushed_scope);
10172 return convert_from_reference (decl);
10175 /* If we didn't even get past the declarator successfully, we are
10176 definitely not looking at a declaration. */
10177 else
10178 cp_parser_abort_tentative_parse (parser);
10180 /* Otherwise, we are looking at an expression. */
10181 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10184 /* Parses a for-statement or range-for-statement until the closing ')',
10185 not included. */
10187 static tree
10188 cp_parser_for (cp_parser *parser, bool ivdep)
10190 tree init, scope, decl;
10191 bool is_range_for;
10193 /* Begin the for-statement. */
10194 scope = begin_for_scope (&init);
10196 /* Parse the initialization. */
10197 is_range_for = cp_parser_for_init_statement (parser, &decl);
10199 if (is_range_for)
10200 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10201 else
10202 return cp_parser_c_for (parser, scope, init, ivdep);
10205 static tree
10206 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10208 /* Normal for loop */
10209 tree condition = NULL_TREE;
10210 tree expression = NULL_TREE;
10211 tree stmt;
10213 stmt = begin_for_stmt (scope, init);
10214 /* The for-init-statement has already been parsed in
10215 cp_parser_for_init_statement, so no work is needed here. */
10216 finish_for_init_stmt (stmt);
10218 /* If there's a condition, process it. */
10219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10220 condition = cp_parser_condition (parser);
10221 else if (ivdep)
10223 cp_parser_error (parser, "missing loop condition in loop with "
10224 "%<GCC ivdep%> pragma");
10225 condition = error_mark_node;
10227 finish_for_cond (condition, stmt, ivdep);
10228 /* Look for the `;'. */
10229 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10231 /* If there's an expression, process it. */
10232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10233 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10234 finish_for_expr (expression, stmt);
10236 return stmt;
10239 /* Tries to parse a range-based for-statement:
10241 range-based-for:
10242 decl-specifier-seq declarator : expression
10244 The decl-specifier-seq declarator and the `:' are already parsed by
10245 cp_parser_for_init_statement. If processing_template_decl it returns a
10246 newly created RANGE_FOR_STMT; if not, it is converted to a
10247 regular FOR_STMT. */
10249 static tree
10250 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10251 bool ivdep)
10253 tree stmt, range_expr;
10255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10257 bool expr_non_constant_p;
10258 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10260 else
10261 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10263 /* If in template, STMT is converted to a normal for-statement
10264 at instantiation. If not, it is done just ahead. */
10265 if (processing_template_decl)
10267 if (check_for_bare_parameter_packs (range_expr))
10268 range_expr = error_mark_node;
10269 stmt = begin_range_for_stmt (scope, init);
10270 if (ivdep)
10271 RANGE_FOR_IVDEP (stmt) = 1;
10272 finish_range_for_decl (stmt, range_decl, range_expr);
10273 if (!type_dependent_expression_p (range_expr)
10274 /* do_auto_deduction doesn't mess with template init-lists. */
10275 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10276 do_range_for_auto_deduction (range_decl, range_expr);
10278 else
10280 stmt = begin_for_stmt (scope, init);
10281 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10283 return stmt;
10286 /* Subroutine of cp_convert_range_for: given the initializer expression,
10287 builds up the range temporary. */
10289 static tree
10290 build_range_temp (tree range_expr)
10292 tree range_type, range_temp;
10294 /* Find out the type deduced by the declaration
10295 `auto &&__range = range_expr'. */
10296 range_type = cp_build_reference_type (make_auto (), true);
10297 range_type = do_auto_deduction (range_type, range_expr,
10298 type_uses_auto (range_type));
10300 /* Create the __range variable. */
10301 range_temp = build_decl (input_location, VAR_DECL,
10302 get_identifier ("__for_range"), range_type);
10303 TREE_USED (range_temp) = 1;
10304 DECL_ARTIFICIAL (range_temp) = 1;
10306 return range_temp;
10309 /* Used by cp_parser_range_for in template context: we aren't going to
10310 do a full conversion yet, but we still need to resolve auto in the
10311 type of the for-range-declaration if present. This is basically
10312 a shortcut version of cp_convert_range_for. */
10314 static void
10315 do_range_for_auto_deduction (tree decl, tree range_expr)
10317 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10318 if (auto_node)
10320 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10321 range_temp = convert_from_reference (build_range_temp (range_expr));
10322 iter_type = (cp_parser_perform_range_for_lookup
10323 (range_temp, &begin_dummy, &end_dummy));
10324 if (iter_type)
10326 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10327 iter_type);
10328 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10329 tf_warning_or_error);
10330 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10331 iter_decl, auto_node);
10336 /* Converts a range-based for-statement into a normal
10337 for-statement, as per the definition.
10339 for (RANGE_DECL : RANGE_EXPR)
10340 BLOCK
10342 should be equivalent to:
10345 auto &&__range = RANGE_EXPR;
10346 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10347 __begin != __end;
10348 ++__begin)
10350 RANGE_DECL = *__begin;
10351 BLOCK
10355 If RANGE_EXPR is an array:
10356 BEGIN_EXPR = __range
10357 END_EXPR = __range + ARRAY_SIZE(__range)
10358 Else if RANGE_EXPR has a member 'begin' or 'end':
10359 BEGIN_EXPR = __range.begin()
10360 END_EXPR = __range.end()
10361 Else:
10362 BEGIN_EXPR = begin(__range)
10363 END_EXPR = end(__range);
10365 If __range has a member 'begin' but not 'end', or vice versa, we must
10366 still use the second alternative (it will surely fail, however).
10367 When calling begin()/end() in the third alternative we must use
10368 argument dependent lookup, but always considering 'std' as an associated
10369 namespace. */
10371 tree
10372 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10373 bool ivdep)
10375 tree begin, end;
10376 tree iter_type, begin_expr, end_expr;
10377 tree condition, expression;
10379 if (range_decl == error_mark_node || range_expr == error_mark_node)
10380 /* If an error happened previously do nothing or else a lot of
10381 unhelpful errors would be issued. */
10382 begin_expr = end_expr = iter_type = error_mark_node;
10383 else
10385 tree range_temp;
10387 if (TREE_CODE (range_expr) == VAR_DECL
10388 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10389 /* Can't bind a reference to an array of runtime bound. */
10390 range_temp = range_expr;
10391 else
10393 range_temp = build_range_temp (range_expr);
10394 pushdecl (range_temp);
10395 cp_finish_decl (range_temp, range_expr,
10396 /*is_constant_init*/false, NULL_TREE,
10397 LOOKUP_ONLYCONVERTING);
10398 range_temp = convert_from_reference (range_temp);
10400 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10401 &begin_expr, &end_expr);
10404 /* The new for initialization statement. */
10405 begin = build_decl (input_location, VAR_DECL,
10406 get_identifier ("__for_begin"), iter_type);
10407 TREE_USED (begin) = 1;
10408 DECL_ARTIFICIAL (begin) = 1;
10409 pushdecl (begin);
10410 cp_finish_decl (begin, begin_expr,
10411 /*is_constant_init*/false, NULL_TREE,
10412 LOOKUP_ONLYCONVERTING);
10414 end = build_decl (input_location, VAR_DECL,
10415 get_identifier ("__for_end"), iter_type);
10416 TREE_USED (end) = 1;
10417 DECL_ARTIFICIAL (end) = 1;
10418 pushdecl (end);
10419 cp_finish_decl (end, end_expr,
10420 /*is_constant_init*/false, NULL_TREE,
10421 LOOKUP_ONLYCONVERTING);
10423 finish_for_init_stmt (statement);
10425 /* The new for condition. */
10426 condition = build_x_binary_op (input_location, NE_EXPR,
10427 begin, ERROR_MARK,
10428 end, ERROR_MARK,
10429 NULL, tf_warning_or_error);
10430 finish_for_cond (condition, statement, ivdep);
10432 /* The new increment expression. */
10433 expression = finish_unary_op_expr (input_location,
10434 PREINCREMENT_EXPR, begin,
10435 tf_warning_or_error);
10436 finish_for_expr (expression, statement);
10438 /* The declaration is initialized with *__begin inside the loop body. */
10439 cp_finish_decl (range_decl,
10440 build_x_indirect_ref (input_location, begin, RO_NULL,
10441 tf_warning_or_error),
10442 /*is_constant_init*/false, NULL_TREE,
10443 LOOKUP_ONLYCONVERTING);
10445 return statement;
10448 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10449 We need to solve both at the same time because the method used
10450 depends on the existence of members begin or end.
10451 Returns the type deduced for the iterator expression. */
10453 static tree
10454 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10456 if (error_operand_p (range))
10458 *begin = *end = error_mark_node;
10459 return error_mark_node;
10462 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10464 error ("range-based %<for%> expression of type %qT "
10465 "has incomplete type", TREE_TYPE (range));
10466 *begin = *end = error_mark_node;
10467 return error_mark_node;
10469 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10471 /* If RANGE is an array, we will use pointer arithmetic. */
10472 *begin = range;
10473 *end = build_binary_op (input_location, PLUS_EXPR,
10474 range,
10475 array_type_nelts_top (TREE_TYPE (range)),
10477 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10479 else
10481 /* If it is not an array, we must do a bit of magic. */
10482 tree id_begin, id_end;
10483 tree member_begin, member_end;
10485 *begin = *end = error_mark_node;
10487 id_begin = get_identifier ("begin");
10488 id_end = get_identifier ("end");
10489 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10490 /*protect=*/2, /*want_type=*/false,
10491 tf_warning_or_error);
10492 member_end = lookup_member (TREE_TYPE (range), id_end,
10493 /*protect=*/2, /*want_type=*/false,
10494 tf_warning_or_error);
10496 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10498 /* Use the member functions. */
10499 if (member_begin != NULL_TREE)
10500 *begin = cp_parser_range_for_member_function (range, id_begin);
10501 else
10502 error ("range-based %<for%> expression of type %qT has an "
10503 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10505 if (member_end != NULL_TREE)
10506 *end = cp_parser_range_for_member_function (range, id_end);
10507 else
10508 error ("range-based %<for%> expression of type %qT has a "
10509 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10511 else
10513 /* Use global functions with ADL. */
10514 vec<tree, va_gc> *vec;
10515 vec = make_tree_vector ();
10517 vec_safe_push (vec, range);
10519 member_begin = perform_koenig_lookup (id_begin, vec,
10520 tf_warning_or_error);
10521 *begin = finish_call_expr (member_begin, &vec, false, true,
10522 tf_warning_or_error);
10523 member_end = perform_koenig_lookup (id_end, vec,
10524 tf_warning_or_error);
10525 *end = finish_call_expr (member_end, &vec, false, true,
10526 tf_warning_or_error);
10528 release_tree_vector (vec);
10531 /* Last common checks. */
10532 if (*begin == error_mark_node || *end == error_mark_node)
10534 /* If one of the expressions is an error do no more checks. */
10535 *begin = *end = error_mark_node;
10536 return error_mark_node;
10538 else if (type_dependent_expression_p (*begin)
10539 || type_dependent_expression_p (*end))
10540 /* Can happen, when, eg, in a template context, Koenig lookup
10541 can't resolve begin/end (c++/58503). */
10542 return NULL_TREE;
10543 else
10545 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10546 /* The unqualified type of the __begin and __end temporaries should
10547 be the same, as required by the multiple auto declaration. */
10548 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10549 error ("inconsistent begin/end types in range-based %<for%> "
10550 "statement: %qT and %qT",
10551 TREE_TYPE (*begin), TREE_TYPE (*end));
10552 return iter_type;
10557 /* Helper function for cp_parser_perform_range_for_lookup.
10558 Builds a tree for RANGE.IDENTIFIER(). */
10560 static tree
10561 cp_parser_range_for_member_function (tree range, tree identifier)
10563 tree member, res;
10564 vec<tree, va_gc> *vec;
10566 member = finish_class_member_access_expr (range, identifier,
10567 false, tf_warning_or_error);
10568 if (member == error_mark_node)
10569 return error_mark_node;
10571 vec = make_tree_vector ();
10572 res = finish_call_expr (member, &vec,
10573 /*disallow_virtual=*/false,
10574 /*koenig_p=*/false,
10575 tf_warning_or_error);
10576 release_tree_vector (vec);
10577 return res;
10580 /* Parse an iteration-statement.
10582 iteration-statement:
10583 while ( condition ) statement
10584 do statement while ( expression ) ;
10585 for ( for-init-statement condition [opt] ; expression [opt] )
10586 statement
10588 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10590 static tree
10591 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10593 cp_token *token;
10594 enum rid keyword;
10595 tree statement;
10596 unsigned char in_statement;
10598 /* Peek at the next token. */
10599 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10600 if (!token)
10601 return error_mark_node;
10603 /* Remember whether or not we are already within an iteration
10604 statement. */
10605 in_statement = parser->in_statement;
10607 /* See what kind of keyword it is. */
10608 keyword = token->keyword;
10609 switch (keyword)
10611 case RID_WHILE:
10613 tree condition;
10615 /* Begin the while-statement. */
10616 statement = begin_while_stmt ();
10617 /* Look for the `('. */
10618 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10619 /* Parse the condition. */
10620 condition = cp_parser_condition (parser);
10621 finish_while_stmt_cond (condition, statement, ivdep);
10622 /* Look for the `)'. */
10623 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10624 /* Parse the dependent statement. */
10625 parser->in_statement = IN_ITERATION_STMT;
10626 cp_parser_already_scoped_statement (parser);
10627 parser->in_statement = in_statement;
10628 /* We're done with the while-statement. */
10629 finish_while_stmt (statement);
10631 break;
10633 case RID_DO:
10635 tree expression;
10637 /* Begin the do-statement. */
10638 statement = begin_do_stmt ();
10639 /* Parse the body of the do-statement. */
10640 parser->in_statement = IN_ITERATION_STMT;
10641 cp_parser_implicitly_scoped_statement (parser, NULL);
10642 parser->in_statement = in_statement;
10643 finish_do_body (statement);
10644 /* Look for the `while' keyword. */
10645 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10646 /* Look for the `('. */
10647 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10648 /* Parse the expression. */
10649 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10650 /* We're done with the do-statement. */
10651 finish_do_stmt (expression, statement, ivdep);
10652 /* Look for the `)'. */
10653 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10654 /* Look for the `;'. */
10655 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10657 break;
10659 case RID_FOR:
10661 /* Look for the `('. */
10662 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10664 statement = cp_parser_for (parser, ivdep);
10666 /* Look for the `)'. */
10667 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10669 /* Parse the body of the for-statement. */
10670 parser->in_statement = IN_ITERATION_STMT;
10671 cp_parser_already_scoped_statement (parser);
10672 parser->in_statement = in_statement;
10674 /* We're done with the for-statement. */
10675 finish_for_stmt (statement);
10677 break;
10679 default:
10680 cp_parser_error (parser, "expected iteration-statement");
10681 statement = error_mark_node;
10682 break;
10685 return statement;
10688 /* Parse a for-init-statement or the declarator of a range-based-for.
10689 Returns true if a range-based-for declaration is seen.
10691 for-init-statement:
10692 expression-statement
10693 simple-declaration */
10695 static bool
10696 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10698 /* If the next token is a `;', then we have an empty
10699 expression-statement. Grammatically, this is also a
10700 simple-declaration, but an invalid one, because it does not
10701 declare anything. Therefore, if we did not handle this case
10702 specially, we would issue an error message about an invalid
10703 declaration. */
10704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10706 bool is_range_for = false;
10707 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10710 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10712 /* N3994 -- for (id : init) ... */
10713 if (cxx_dialect < cxx1z)
10714 pedwarn (input_location, 0, "range-based for loop without a "
10715 "type-specifier only available with "
10716 "-std=c++1z or -std=gnu++1z");
10717 tree name = cp_parser_identifier (parser);
10718 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10719 *decl = build_decl (input_location, VAR_DECL, name, type);
10720 pushdecl (*decl);
10721 cp_lexer_consume_token (parser->lexer);
10722 return true;
10725 /* A colon is used in range-based for. */
10726 parser->colon_corrects_to_scope_p = false;
10728 /* We're going to speculatively look for a declaration, falling back
10729 to an expression, if necessary. */
10730 cp_parser_parse_tentatively (parser);
10731 /* Parse the declaration. */
10732 cp_parser_simple_declaration (parser,
10733 /*function_definition_allowed_p=*/false,
10734 decl);
10735 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10736 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10738 /* It is a range-for, consume the ':' */
10739 cp_lexer_consume_token (parser->lexer);
10740 is_range_for = true;
10741 if (cxx_dialect < cxx11)
10743 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10744 "range-based %<for%> loops only available with "
10745 "-std=c++11 or -std=gnu++11");
10746 *decl = error_mark_node;
10749 else
10750 /* The ';' is not consumed yet because we told
10751 cp_parser_simple_declaration not to. */
10752 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10754 if (cp_parser_parse_definitely (parser))
10755 return is_range_for;
10756 /* If the tentative parse failed, then we shall need to look for an
10757 expression-statement. */
10759 /* If we are here, it is an expression-statement. */
10760 cp_parser_expression_statement (parser, NULL_TREE);
10761 return false;
10764 /* Parse a jump-statement.
10766 jump-statement:
10767 break ;
10768 continue ;
10769 return expression [opt] ;
10770 return braced-init-list ;
10771 goto identifier ;
10773 GNU extension:
10775 jump-statement:
10776 goto * expression ;
10778 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10780 static tree
10781 cp_parser_jump_statement (cp_parser* parser)
10783 tree statement = error_mark_node;
10784 cp_token *token;
10785 enum rid keyword;
10786 unsigned char in_statement;
10788 /* Peek at the next token. */
10789 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10790 if (!token)
10791 return error_mark_node;
10793 /* See what kind of keyword it is. */
10794 keyword = token->keyword;
10795 switch (keyword)
10797 case RID_BREAK:
10798 in_statement = parser->in_statement & ~IN_IF_STMT;
10799 switch (in_statement)
10801 case 0:
10802 error_at (token->location, "break statement not within loop or switch");
10803 break;
10804 default:
10805 gcc_assert ((in_statement & IN_SWITCH_STMT)
10806 || in_statement == IN_ITERATION_STMT);
10807 statement = finish_break_stmt ();
10808 if (in_statement == IN_ITERATION_STMT)
10809 break_maybe_infinite_loop ();
10810 break;
10811 case IN_OMP_BLOCK:
10812 error_at (token->location, "invalid exit from OpenMP structured block");
10813 break;
10814 case IN_OMP_FOR:
10815 error_at (token->location, "break statement used with OpenMP for loop");
10816 break;
10817 case IN_CILK_SIMD_FOR:
10818 error_at (token->location, "break statement used with Cilk Plus for loop");
10819 break;
10821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10822 break;
10824 case RID_CONTINUE:
10825 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10827 case 0:
10828 error_at (token->location, "continue statement not within a loop");
10829 break;
10830 case IN_CILK_SIMD_FOR:
10831 error_at (token->location,
10832 "continue statement within %<#pragma simd%> loop body");
10833 /* Fall through. */
10834 case IN_ITERATION_STMT:
10835 case IN_OMP_FOR:
10836 statement = finish_continue_stmt ();
10837 break;
10838 case IN_OMP_BLOCK:
10839 error_at (token->location, "invalid exit from OpenMP structured block");
10840 break;
10841 default:
10842 gcc_unreachable ();
10844 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10845 break;
10847 case RID_RETURN:
10849 tree expr;
10850 bool expr_non_constant_p;
10852 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10854 cp_lexer_set_source_position (parser->lexer);
10855 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10856 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10858 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10859 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10860 else
10861 /* If the next token is a `;', then there is no
10862 expression. */
10863 expr = NULL_TREE;
10864 /* Build the return-statement. */
10865 statement = finish_return_stmt (expr);
10866 /* Look for the final `;'. */
10867 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10869 break;
10871 case RID_GOTO:
10872 /* Create the goto-statement. */
10873 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10875 /* Issue a warning about this use of a GNU extension. */
10876 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10877 /* Consume the '*' token. */
10878 cp_lexer_consume_token (parser->lexer);
10879 /* Parse the dependent expression. */
10880 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10882 else
10883 finish_goto_stmt (cp_parser_identifier (parser));
10884 /* Look for the final `;'. */
10885 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10886 break;
10888 default:
10889 cp_parser_error (parser, "expected jump-statement");
10890 break;
10893 return statement;
10896 /* Parse a declaration-statement.
10898 declaration-statement:
10899 block-declaration */
10901 static void
10902 cp_parser_declaration_statement (cp_parser* parser)
10904 void *p;
10906 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10907 p = obstack_alloc (&declarator_obstack, 0);
10909 /* Parse the block-declaration. */
10910 cp_parser_block_declaration (parser, /*statement_p=*/true);
10912 /* Free any declarators allocated. */
10913 obstack_free (&declarator_obstack, p);
10916 /* Some dependent statements (like `if (cond) statement'), are
10917 implicitly in their own scope. In other words, if the statement is
10918 a single statement (as opposed to a compound-statement), it is
10919 none-the-less treated as if it were enclosed in braces. Any
10920 declarations appearing in the dependent statement are out of scope
10921 after control passes that point. This function parses a statement,
10922 but ensures that is in its own scope, even if it is not a
10923 compound-statement.
10925 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10926 is a (possibly labeled) if statement which is not enclosed in
10927 braces and has an else clause. This is used to implement
10928 -Wparentheses.
10930 Returns the new statement. */
10932 static tree
10933 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10935 tree statement;
10937 if (if_p != NULL)
10938 *if_p = false;
10940 /* Mark if () ; with a special NOP_EXPR. */
10941 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10943 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10944 cp_lexer_consume_token (parser->lexer);
10945 statement = add_stmt (build_empty_stmt (loc));
10947 /* if a compound is opened, we simply parse the statement directly. */
10948 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10949 statement = cp_parser_compound_statement (parser, NULL, false, false);
10950 /* If the token is not a `{', then we must take special action. */
10951 else
10953 /* Create a compound-statement. */
10954 statement = begin_compound_stmt (0);
10955 /* Parse the dependent-statement. */
10956 cp_parser_statement (parser, NULL_TREE, false, if_p);
10957 /* Finish the dummy compound-statement. */
10958 finish_compound_stmt (statement);
10961 /* Return the statement. */
10962 return statement;
10965 /* For some dependent statements (like `while (cond) statement'), we
10966 have already created a scope. Therefore, even if the dependent
10967 statement is a compound-statement, we do not want to create another
10968 scope. */
10970 static void
10971 cp_parser_already_scoped_statement (cp_parser* parser)
10973 /* If the token is a `{', then we must take special action. */
10974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10975 cp_parser_statement (parser, NULL_TREE, false, NULL);
10976 else
10978 /* Avoid calling cp_parser_compound_statement, so that we
10979 don't create a new scope. Do everything else by hand. */
10980 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10981 /* If the next keyword is `__label__' we have a label declaration. */
10982 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10983 cp_parser_label_declaration (parser);
10984 /* Parse an (optional) statement-seq. */
10985 cp_parser_statement_seq_opt (parser, NULL_TREE);
10986 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10990 /* Declarations [gram.dcl.dcl] */
10992 /* Parse an optional declaration-sequence.
10994 declaration-seq:
10995 declaration
10996 declaration-seq declaration */
10998 static void
10999 cp_parser_declaration_seq_opt (cp_parser* parser)
11001 while (true)
11003 cp_token *token;
11005 token = cp_lexer_peek_token (parser->lexer);
11007 if (token->type == CPP_CLOSE_BRACE
11008 || token->type == CPP_EOF
11009 || token->type == CPP_PRAGMA_EOL)
11010 break;
11012 if (token->type == CPP_SEMICOLON)
11014 /* A declaration consisting of a single semicolon is
11015 invalid. Allow it unless we're being pedantic. */
11016 cp_lexer_consume_token (parser->lexer);
11017 if (!in_system_header_at (input_location))
11018 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11019 continue;
11022 /* If we're entering or exiting a region that's implicitly
11023 extern "C", modify the lang context appropriately. */
11024 if (!parser->implicit_extern_c && token->implicit_extern_c)
11026 push_lang_context (lang_name_c);
11027 parser->implicit_extern_c = true;
11029 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11031 pop_lang_context ();
11032 parser->implicit_extern_c = false;
11035 if (token->type == CPP_PRAGMA)
11037 /* A top-level declaration can consist solely of a #pragma.
11038 A nested declaration cannot, so this is done here and not
11039 in cp_parser_declaration. (A #pragma at block scope is
11040 handled in cp_parser_statement.) */
11041 cp_parser_pragma (parser, pragma_external);
11042 continue;
11045 /* Parse the declaration itself. */
11046 cp_parser_declaration (parser);
11050 /* Parse a declaration.
11052 declaration:
11053 block-declaration
11054 function-definition
11055 template-declaration
11056 explicit-instantiation
11057 explicit-specialization
11058 linkage-specification
11059 namespace-definition
11061 GNU extension:
11063 declaration:
11064 __extension__ declaration */
11066 static void
11067 cp_parser_declaration (cp_parser* parser)
11069 cp_token token1;
11070 cp_token token2;
11071 int saved_pedantic;
11072 void *p;
11073 tree attributes = NULL_TREE;
11075 /* Check for the `__extension__' keyword. */
11076 if (cp_parser_extension_opt (parser, &saved_pedantic))
11078 /* Parse the qualified declaration. */
11079 cp_parser_declaration (parser);
11080 /* Restore the PEDANTIC flag. */
11081 pedantic = saved_pedantic;
11083 return;
11086 /* Try to figure out what kind of declaration is present. */
11087 token1 = *cp_lexer_peek_token (parser->lexer);
11089 if (token1.type != CPP_EOF)
11090 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11091 else
11093 token2.type = CPP_EOF;
11094 token2.keyword = RID_MAX;
11097 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11098 p = obstack_alloc (&declarator_obstack, 0);
11100 /* If the next token is `extern' and the following token is a string
11101 literal, then we have a linkage specification. */
11102 if (token1.keyword == RID_EXTERN
11103 && cp_parser_is_pure_string_literal (&token2))
11104 cp_parser_linkage_specification (parser);
11105 /* If the next token is `template', then we have either a template
11106 declaration, an explicit instantiation, or an explicit
11107 specialization. */
11108 else if (token1.keyword == RID_TEMPLATE)
11110 /* `template <>' indicates a template specialization. */
11111 if (token2.type == CPP_LESS
11112 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11113 cp_parser_explicit_specialization (parser);
11114 /* `template <' indicates a template declaration. */
11115 else if (token2.type == CPP_LESS)
11116 cp_parser_template_declaration (parser, /*member_p=*/false);
11117 /* Anything else must be an explicit instantiation. */
11118 else
11119 cp_parser_explicit_instantiation (parser);
11121 /* If the next token is `export', then we have a template
11122 declaration. */
11123 else if (token1.keyword == RID_EXPORT)
11124 cp_parser_template_declaration (parser, /*member_p=*/false);
11125 /* If the next token is `extern', 'static' or 'inline' and the one
11126 after that is `template', we have a GNU extended explicit
11127 instantiation directive. */
11128 else if (cp_parser_allow_gnu_extensions_p (parser)
11129 && (token1.keyword == RID_EXTERN
11130 || token1.keyword == RID_STATIC
11131 || token1.keyword == RID_INLINE)
11132 && token2.keyword == RID_TEMPLATE)
11133 cp_parser_explicit_instantiation (parser);
11134 /* If the next token is `namespace', check for a named or unnamed
11135 namespace definition. */
11136 else if (token1.keyword == RID_NAMESPACE
11137 && (/* A named namespace definition. */
11138 (token2.type == CPP_NAME
11139 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11140 != CPP_EQ))
11141 /* An unnamed namespace definition. */
11142 || token2.type == CPP_OPEN_BRACE
11143 || token2.keyword == RID_ATTRIBUTE))
11144 cp_parser_namespace_definition (parser);
11145 /* An inline (associated) namespace definition. */
11146 else if (token1.keyword == RID_INLINE
11147 && token2.keyword == RID_NAMESPACE)
11148 cp_parser_namespace_definition (parser);
11149 /* Objective-C++ declaration/definition. */
11150 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11151 cp_parser_objc_declaration (parser, NULL_TREE);
11152 else if (c_dialect_objc ()
11153 && token1.keyword == RID_ATTRIBUTE
11154 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11155 cp_parser_objc_declaration (parser, attributes);
11156 /* We must have either a block declaration or a function
11157 definition. */
11158 else
11159 /* Try to parse a block-declaration, or a function-definition. */
11160 cp_parser_block_declaration (parser, /*statement_p=*/false);
11162 /* Free any declarators allocated. */
11163 obstack_free (&declarator_obstack, p);
11166 /* Parse a block-declaration.
11168 block-declaration:
11169 simple-declaration
11170 asm-definition
11171 namespace-alias-definition
11172 using-declaration
11173 using-directive
11175 GNU Extension:
11177 block-declaration:
11178 __extension__ block-declaration
11180 C++0x Extension:
11182 block-declaration:
11183 static_assert-declaration
11185 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11186 part of a declaration-statement. */
11188 static void
11189 cp_parser_block_declaration (cp_parser *parser,
11190 bool statement_p)
11192 cp_token *token1;
11193 int saved_pedantic;
11195 /* Check for the `__extension__' keyword. */
11196 if (cp_parser_extension_opt (parser, &saved_pedantic))
11198 /* Parse the qualified declaration. */
11199 cp_parser_block_declaration (parser, statement_p);
11200 /* Restore the PEDANTIC flag. */
11201 pedantic = saved_pedantic;
11203 return;
11206 /* Peek at the next token to figure out which kind of declaration is
11207 present. */
11208 token1 = cp_lexer_peek_token (parser->lexer);
11210 /* If the next keyword is `asm', we have an asm-definition. */
11211 if (token1->keyword == RID_ASM)
11213 if (statement_p)
11214 cp_parser_commit_to_tentative_parse (parser);
11215 cp_parser_asm_definition (parser);
11217 /* If the next keyword is `namespace', we have a
11218 namespace-alias-definition. */
11219 else if (token1->keyword == RID_NAMESPACE)
11220 cp_parser_namespace_alias_definition (parser);
11221 /* If the next keyword is `using', we have a
11222 using-declaration, a using-directive, or an alias-declaration. */
11223 else if (token1->keyword == RID_USING)
11225 cp_token *token2;
11227 if (statement_p)
11228 cp_parser_commit_to_tentative_parse (parser);
11229 /* If the token after `using' is `namespace', then we have a
11230 using-directive. */
11231 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11232 if (token2->keyword == RID_NAMESPACE)
11233 cp_parser_using_directive (parser);
11234 /* If the second token after 'using' is '=', then we have an
11235 alias-declaration. */
11236 else if (cxx_dialect >= cxx11
11237 && token2->type == CPP_NAME
11238 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11239 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11240 cp_parser_alias_declaration (parser);
11241 /* Otherwise, it's a using-declaration. */
11242 else
11243 cp_parser_using_declaration (parser,
11244 /*access_declaration_p=*/false);
11246 /* If the next keyword is `__label__' we have a misplaced label
11247 declaration. */
11248 else if (token1->keyword == RID_LABEL)
11250 cp_lexer_consume_token (parser->lexer);
11251 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11252 cp_parser_skip_to_end_of_statement (parser);
11253 /* If the next token is now a `;', consume it. */
11254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11255 cp_lexer_consume_token (parser->lexer);
11257 /* If the next token is `static_assert' we have a static assertion. */
11258 else if (token1->keyword == RID_STATIC_ASSERT)
11259 cp_parser_static_assert (parser, /*member_p=*/false);
11260 /* Anything else must be a simple-declaration. */
11261 else
11262 cp_parser_simple_declaration (parser, !statement_p,
11263 /*maybe_range_for_decl*/NULL);
11266 /* Parse a simple-declaration.
11268 simple-declaration:
11269 decl-specifier-seq [opt] init-declarator-list [opt] ;
11271 init-declarator-list:
11272 init-declarator
11273 init-declarator-list , init-declarator
11275 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11276 function-definition as a simple-declaration.
11278 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11279 parsed declaration if it is an uninitialized single declarator not followed
11280 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11281 if present, will not be consumed. */
11283 static void
11284 cp_parser_simple_declaration (cp_parser* parser,
11285 bool function_definition_allowed_p,
11286 tree *maybe_range_for_decl)
11288 cp_decl_specifier_seq decl_specifiers;
11289 int declares_class_or_enum;
11290 bool saw_declarator;
11292 if (maybe_range_for_decl)
11293 *maybe_range_for_decl = NULL_TREE;
11295 /* Defer access checks until we know what is being declared; the
11296 checks for names appearing in the decl-specifier-seq should be
11297 done as if we were in the scope of the thing being declared. */
11298 push_deferring_access_checks (dk_deferred);
11300 /* Parse the decl-specifier-seq. We have to keep track of whether
11301 or not the decl-specifier-seq declares a named class or
11302 enumeration type, since that is the only case in which the
11303 init-declarator-list is allowed to be empty.
11305 [dcl.dcl]
11307 In a simple-declaration, the optional init-declarator-list can be
11308 omitted only when declaring a class or enumeration, that is when
11309 the decl-specifier-seq contains either a class-specifier, an
11310 elaborated-type-specifier, or an enum-specifier. */
11311 cp_parser_decl_specifier_seq (parser,
11312 CP_PARSER_FLAGS_OPTIONAL,
11313 &decl_specifiers,
11314 &declares_class_or_enum);
11315 /* We no longer need to defer access checks. */
11316 stop_deferring_access_checks ();
11318 /* In a block scope, a valid declaration must always have a
11319 decl-specifier-seq. By not trying to parse declarators, we can
11320 resolve the declaration/expression ambiguity more quickly. */
11321 if (!function_definition_allowed_p
11322 && !decl_specifiers.any_specifiers_p)
11324 cp_parser_error (parser, "expected declaration");
11325 goto done;
11328 /* If the next two tokens are both identifiers, the code is
11329 erroneous. The usual cause of this situation is code like:
11331 T t;
11333 where "T" should name a type -- but does not. */
11334 if (!decl_specifiers.any_type_specifiers_p
11335 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11337 /* If parsing tentatively, we should commit; we really are
11338 looking at a declaration. */
11339 cp_parser_commit_to_tentative_parse (parser);
11340 /* Give up. */
11341 goto done;
11344 /* If we have seen at least one decl-specifier, and the next token
11345 is not a parenthesis, then we must be looking at a declaration.
11346 (After "int (" we might be looking at a functional cast.) */
11347 if (decl_specifiers.any_specifiers_p
11348 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11349 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11350 && !cp_parser_error_occurred (parser))
11351 cp_parser_commit_to_tentative_parse (parser);
11353 /* Keep going until we hit the `;' at the end of the simple
11354 declaration. */
11355 saw_declarator = false;
11356 while (cp_lexer_next_token_is_not (parser->lexer,
11357 CPP_SEMICOLON))
11359 cp_token *token;
11360 bool function_definition_p;
11361 tree decl;
11363 if (saw_declarator)
11365 /* If we are processing next declarator, coma is expected */
11366 token = cp_lexer_peek_token (parser->lexer);
11367 gcc_assert (token->type == CPP_COMMA);
11368 cp_lexer_consume_token (parser->lexer);
11369 if (maybe_range_for_decl)
11370 *maybe_range_for_decl = error_mark_node;
11372 else
11373 saw_declarator = true;
11375 /* Parse the init-declarator. */
11376 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11377 /*checks=*/NULL,
11378 function_definition_allowed_p,
11379 /*member_p=*/false,
11380 declares_class_or_enum,
11381 &function_definition_p,
11382 maybe_range_for_decl);
11383 /* If an error occurred while parsing tentatively, exit quickly.
11384 (That usually happens when in the body of a function; each
11385 statement is treated as a declaration-statement until proven
11386 otherwise.) */
11387 if (cp_parser_error_occurred (parser))
11388 goto done;
11389 /* Handle function definitions specially. */
11390 if (function_definition_p)
11392 /* If the next token is a `,', then we are probably
11393 processing something like:
11395 void f() {}, *p;
11397 which is erroneous. */
11398 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11400 cp_token *token = cp_lexer_peek_token (parser->lexer);
11401 error_at (token->location,
11402 "mixing"
11403 " declarations and function-definitions is forbidden");
11405 /* Otherwise, we're done with the list of declarators. */
11406 else
11408 pop_deferring_access_checks ();
11409 return;
11412 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11413 *maybe_range_for_decl = decl;
11414 /* The next token should be either a `,' or a `;'. */
11415 token = cp_lexer_peek_token (parser->lexer);
11416 /* If it's a `,', there are more declarators to come. */
11417 if (token->type == CPP_COMMA)
11418 /* will be consumed next time around */;
11419 /* If it's a `;', we are done. */
11420 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11421 break;
11422 /* Anything else is an error. */
11423 else
11425 /* If we have already issued an error message we don't need
11426 to issue another one. */
11427 if (decl != error_mark_node
11428 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11429 cp_parser_error (parser, "expected %<,%> or %<;%>");
11430 /* Skip tokens until we reach the end of the statement. */
11431 cp_parser_skip_to_end_of_statement (parser);
11432 /* If the next token is now a `;', consume it. */
11433 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11434 cp_lexer_consume_token (parser->lexer);
11435 goto done;
11437 /* After the first time around, a function-definition is not
11438 allowed -- even if it was OK at first. For example:
11440 int i, f() {}
11442 is not valid. */
11443 function_definition_allowed_p = false;
11446 /* Issue an error message if no declarators are present, and the
11447 decl-specifier-seq does not itself declare a class or
11448 enumeration: [dcl.dcl]/3. */
11449 if (!saw_declarator)
11451 if (cp_parser_declares_only_class_p (parser))
11453 if (!declares_class_or_enum
11454 && decl_specifiers.type
11455 && OVERLOAD_TYPE_P (decl_specifiers.type))
11456 /* Ensure an error is issued anyway when finish_decltype_type,
11457 called via cp_parser_decl_specifier_seq, returns a class or
11458 an enumeration (c++/51786). */
11459 decl_specifiers.type = NULL_TREE;
11460 shadow_tag (&decl_specifiers);
11462 /* Perform any deferred access checks. */
11463 perform_deferred_access_checks (tf_warning_or_error);
11466 /* Consume the `;'. */
11467 if (!maybe_range_for_decl)
11468 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11470 done:
11471 pop_deferring_access_checks ();
11474 /* Parse a decl-specifier-seq.
11476 decl-specifier-seq:
11477 decl-specifier-seq [opt] decl-specifier
11478 decl-specifier attribute-specifier-seq [opt] (C++11)
11480 decl-specifier:
11481 storage-class-specifier
11482 type-specifier
11483 function-specifier
11484 friend
11485 typedef
11487 GNU Extension:
11489 decl-specifier:
11490 attributes
11492 Concepts Extension:
11494 decl-specifier:
11495 concept
11498 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11500 The parser flags FLAGS is used to control type-specifier parsing.
11502 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11503 flags:
11505 1: one of the decl-specifiers is an elaborated-type-specifier
11506 (i.e., a type declaration)
11507 2: one of the decl-specifiers is an enum-specifier or a
11508 class-specifier (i.e., a type definition)
11512 static void
11513 cp_parser_decl_specifier_seq (cp_parser* parser,
11514 cp_parser_flags flags,
11515 cp_decl_specifier_seq *decl_specs,
11516 int* declares_class_or_enum)
11518 bool constructor_possible_p = !parser->in_declarator_p;
11519 bool found_decl_spec = false;
11520 cp_token *start_token = NULL;
11521 cp_decl_spec ds;
11523 /* Clear DECL_SPECS. */
11524 clear_decl_specs (decl_specs);
11526 /* Assume no class or enumeration type is declared. */
11527 *declares_class_or_enum = 0;
11529 /* Keep reading specifiers until there are no more to read. */
11530 while (true)
11532 bool constructor_p;
11533 cp_token *token;
11534 ds = ds_last;
11536 /* Peek at the next token. */
11537 token = cp_lexer_peek_token (parser->lexer);
11539 /* Save the first token of the decl spec list for error
11540 reporting. */
11541 if (!start_token)
11542 start_token = token;
11543 /* Handle attributes. */
11544 if (cp_next_tokens_can_be_attribute_p (parser))
11546 /* Parse the attributes. */
11547 tree attrs = cp_parser_attributes_opt (parser);
11549 /* In a sequence of declaration specifiers, c++11 attributes
11550 appertain to the type that precede them. In that case
11551 [dcl.spec]/1 says:
11553 The attribute-specifier-seq affects the type only for
11554 the declaration it appears in, not other declarations
11555 involving the same type.
11557 But for now let's force the user to position the
11558 attribute either at the beginning of the declaration or
11559 after the declarator-id, which would clearly mean that it
11560 applies to the declarator. */
11561 if (cxx11_attribute_p (attrs))
11563 if (!found_decl_spec)
11564 /* The c++11 attribute is at the beginning of the
11565 declaration. It appertains to the entity being
11566 declared. */;
11567 else
11569 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11571 /* This is an attribute following a
11572 class-specifier. */
11573 if (decl_specs->type_definition_p)
11574 warn_misplaced_attr_for_class_type (token->location,
11575 decl_specs->type);
11576 attrs = NULL_TREE;
11578 else
11580 decl_specs->std_attributes
11581 = chainon (decl_specs->std_attributes,
11582 attrs);
11583 if (decl_specs->locations[ds_std_attribute] == 0)
11584 decl_specs->locations[ds_std_attribute] = token->location;
11586 continue;
11590 decl_specs->attributes
11591 = chainon (decl_specs->attributes,
11592 attrs);
11593 if (decl_specs->locations[ds_attribute] == 0)
11594 decl_specs->locations[ds_attribute] = token->location;
11595 continue;
11597 /* Assume we will find a decl-specifier keyword. */
11598 found_decl_spec = true;
11599 /* If the next token is an appropriate keyword, we can simply
11600 add it to the list. */
11601 switch (token->keyword)
11603 /* decl-specifier:
11604 friend
11605 constexpr */
11606 case RID_FRIEND:
11607 if (!at_class_scope_p ())
11609 error_at (token->location, "%<friend%> used outside of class");
11610 cp_lexer_purge_token (parser->lexer);
11612 else
11614 ds = ds_friend;
11615 /* Consume the token. */
11616 cp_lexer_consume_token (parser->lexer);
11618 break;
11620 case RID_CONSTEXPR:
11621 ds = ds_constexpr;
11622 cp_lexer_consume_token (parser->lexer);
11623 break;
11625 case RID_CONCEPT:
11626 ds = ds_concept;
11627 cp_lexer_consume_token (parser->lexer);
11628 break;
11630 /* function-specifier:
11631 inline
11632 virtual
11633 explicit */
11634 case RID_INLINE:
11635 case RID_VIRTUAL:
11636 case RID_EXPLICIT:
11637 cp_parser_function_specifier_opt (parser, decl_specs);
11638 break;
11640 /* decl-specifier:
11641 typedef */
11642 case RID_TYPEDEF:
11643 ds = ds_typedef;
11644 /* Consume the token. */
11645 cp_lexer_consume_token (parser->lexer);
11646 /* A constructor declarator cannot appear in a typedef. */
11647 constructor_possible_p = false;
11648 /* The "typedef" keyword can only occur in a declaration; we
11649 may as well commit at this point. */
11650 cp_parser_commit_to_tentative_parse (parser);
11652 if (decl_specs->storage_class != sc_none)
11653 decl_specs->conflicting_specifiers_p = true;
11654 break;
11656 /* storage-class-specifier:
11657 auto
11658 register
11659 static
11660 extern
11661 mutable
11663 GNU Extension:
11664 thread */
11665 case RID_AUTO:
11666 if (cxx_dialect == cxx98)
11668 /* Consume the token. */
11669 cp_lexer_consume_token (parser->lexer);
11671 /* Complain about `auto' as a storage specifier, if
11672 we're complaining about C++0x compatibility. */
11673 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11674 " changes meaning in C++11; please remove it");
11676 /* Set the storage class anyway. */
11677 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11678 token);
11680 else
11681 /* C++0x auto type-specifier. */
11682 found_decl_spec = false;
11683 break;
11685 case RID_REGISTER:
11686 case RID_STATIC:
11687 case RID_EXTERN:
11688 case RID_MUTABLE:
11689 /* Consume the token. */
11690 cp_lexer_consume_token (parser->lexer);
11691 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11692 token);
11693 break;
11694 case RID_THREAD:
11695 /* Consume the token. */
11696 ds = ds_thread;
11697 cp_lexer_consume_token (parser->lexer);
11698 break;
11700 default:
11701 /* We did not yet find a decl-specifier yet. */
11702 found_decl_spec = false;
11703 break;
11706 if (found_decl_spec
11707 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11708 && token->keyword != RID_CONSTEXPR)
11709 error ("decl-specifier invalid in condition");
11711 if (ds != ds_last)
11712 set_and_check_decl_spec_loc (decl_specs, ds, token);
11714 /* Constructors are a special case. The `S' in `S()' is not a
11715 decl-specifier; it is the beginning of the declarator. */
11716 constructor_p
11717 = (!found_decl_spec
11718 && constructor_possible_p
11719 && (cp_parser_constructor_declarator_p
11720 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11722 /* If we don't have a DECL_SPEC yet, then we must be looking at
11723 a type-specifier. */
11724 if (!found_decl_spec && !constructor_p)
11726 int decl_spec_declares_class_or_enum;
11727 bool is_cv_qualifier;
11728 tree type_spec;
11730 type_spec
11731 = cp_parser_type_specifier (parser, flags,
11732 decl_specs,
11733 /*is_declaration=*/true,
11734 &decl_spec_declares_class_or_enum,
11735 &is_cv_qualifier);
11736 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11738 /* If this type-specifier referenced a user-defined type
11739 (a typedef, class-name, etc.), then we can't allow any
11740 more such type-specifiers henceforth.
11742 [dcl.spec]
11744 The longest sequence of decl-specifiers that could
11745 possibly be a type name is taken as the
11746 decl-specifier-seq of a declaration. The sequence shall
11747 be self-consistent as described below.
11749 [dcl.type]
11751 As a general rule, at most one type-specifier is allowed
11752 in the complete decl-specifier-seq of a declaration. The
11753 only exceptions are the following:
11755 -- const or volatile can be combined with any other
11756 type-specifier.
11758 -- signed or unsigned can be combined with char, long,
11759 short, or int.
11761 -- ..
11763 Example:
11765 typedef char* Pc;
11766 void g (const int Pc);
11768 Here, Pc is *not* part of the decl-specifier seq; it's
11769 the declarator. Therefore, once we see a type-specifier
11770 (other than a cv-qualifier), we forbid any additional
11771 user-defined types. We *do* still allow things like `int
11772 int' to be considered a decl-specifier-seq, and issue the
11773 error message later. */
11774 if (type_spec && !is_cv_qualifier)
11775 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11776 /* A constructor declarator cannot follow a type-specifier. */
11777 if (type_spec)
11779 constructor_possible_p = false;
11780 found_decl_spec = true;
11781 if (!is_cv_qualifier)
11782 decl_specs->any_type_specifiers_p = true;
11786 /* If we still do not have a DECL_SPEC, then there are no more
11787 decl-specifiers. */
11788 if (!found_decl_spec)
11789 break;
11791 decl_specs->any_specifiers_p = true;
11792 /* After we see one decl-specifier, further decl-specifiers are
11793 always optional. */
11794 flags |= CP_PARSER_FLAGS_OPTIONAL;
11797 /* Don't allow a friend specifier with a class definition. */
11798 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11799 && (*declares_class_or_enum & 2))
11800 error_at (decl_specs->locations[ds_friend],
11801 "class definition may not be declared a friend");
11804 /* Parse an (optional) storage-class-specifier.
11806 storage-class-specifier:
11807 auto
11808 register
11809 static
11810 extern
11811 mutable
11813 GNU Extension:
11815 storage-class-specifier:
11816 thread
11818 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11820 static tree
11821 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11823 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11825 case RID_AUTO:
11826 if (cxx_dialect != cxx98)
11827 return NULL_TREE;
11828 /* Fall through for C++98. */
11830 case RID_REGISTER:
11831 case RID_STATIC:
11832 case RID_EXTERN:
11833 case RID_MUTABLE:
11834 case RID_THREAD:
11835 /* Consume the token. */
11836 return cp_lexer_consume_token (parser->lexer)->u.value;
11838 default:
11839 return NULL_TREE;
11843 /* Parse an (optional) function-specifier.
11845 function-specifier:
11846 inline
11847 virtual
11848 explicit
11850 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11851 Updates DECL_SPECS, if it is non-NULL. */
11853 static tree
11854 cp_parser_function_specifier_opt (cp_parser* parser,
11855 cp_decl_specifier_seq *decl_specs)
11857 cp_token *token = cp_lexer_peek_token (parser->lexer);
11858 switch (token->keyword)
11860 case RID_INLINE:
11861 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11862 break;
11864 case RID_VIRTUAL:
11865 /* 14.5.2.3 [temp.mem]
11867 A member function template shall not be virtual. */
11868 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11869 error_at (token->location, "templates may not be %<virtual%>");
11870 else
11871 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11872 break;
11874 case RID_EXPLICIT:
11875 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11876 break;
11878 default:
11879 return NULL_TREE;
11882 /* Consume the token. */
11883 return cp_lexer_consume_token (parser->lexer)->u.value;
11886 /* Parse a linkage-specification.
11888 linkage-specification:
11889 extern string-literal { declaration-seq [opt] }
11890 extern string-literal declaration */
11892 static void
11893 cp_parser_linkage_specification (cp_parser* parser)
11895 tree linkage;
11897 /* Look for the `extern' keyword. */
11898 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11900 /* Look for the string-literal. */
11901 linkage = cp_parser_string_literal (parser, false, false);
11903 /* Transform the literal into an identifier. If the literal is a
11904 wide-character string, or contains embedded NULs, then we can't
11905 handle it as the user wants. */
11906 if (strlen (TREE_STRING_POINTER (linkage))
11907 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11909 cp_parser_error (parser, "invalid linkage-specification");
11910 /* Assume C++ linkage. */
11911 linkage = lang_name_cplusplus;
11913 else
11914 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11916 /* We're now using the new linkage. */
11917 push_lang_context (linkage);
11919 /* If the next token is a `{', then we're using the first
11920 production. */
11921 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11923 cp_ensure_no_omp_declare_simd (parser);
11925 /* Consume the `{' token. */
11926 cp_lexer_consume_token (parser->lexer);
11927 /* Parse the declarations. */
11928 cp_parser_declaration_seq_opt (parser);
11929 /* Look for the closing `}'. */
11930 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11932 /* Otherwise, there's just one declaration. */
11933 else
11935 bool saved_in_unbraced_linkage_specification_p;
11937 saved_in_unbraced_linkage_specification_p
11938 = parser->in_unbraced_linkage_specification_p;
11939 parser->in_unbraced_linkage_specification_p = true;
11940 cp_parser_declaration (parser);
11941 parser->in_unbraced_linkage_specification_p
11942 = saved_in_unbraced_linkage_specification_p;
11945 /* We're done with the linkage-specification. */
11946 pop_lang_context ();
11949 /* Parse a static_assert-declaration.
11951 static_assert-declaration:
11952 static_assert ( constant-expression , string-literal ) ;
11954 If MEMBER_P, this static_assert is a class member. */
11956 static void
11957 cp_parser_static_assert(cp_parser *parser, bool member_p)
11959 tree condition;
11960 tree message;
11961 cp_token *token;
11962 location_t saved_loc;
11963 bool dummy;
11965 /* Peek at the `static_assert' token so we can keep track of exactly
11966 where the static assertion started. */
11967 token = cp_lexer_peek_token (parser->lexer);
11968 saved_loc = token->location;
11970 /* Look for the `static_assert' keyword. */
11971 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11972 RT_STATIC_ASSERT))
11973 return;
11975 /* We know we are in a static assertion; commit to any tentative
11976 parse. */
11977 if (cp_parser_parsing_tentatively (parser))
11978 cp_parser_commit_to_tentative_parse (parser);
11980 /* Parse the `(' starting the static assertion condition. */
11981 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11983 /* Parse the constant-expression. Allow a non-constant expression
11984 here in order to give better diagnostics in finish_static_assert. */
11985 condition =
11986 cp_parser_constant_expression (parser,
11987 /*allow_non_constant_p=*/true,
11988 /*non_constant_p=*/&dummy);
11990 /* Parse the separating `,'. */
11991 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11993 /* Parse the string-literal message. */
11994 message = cp_parser_string_literal (parser,
11995 /*translate=*/false,
11996 /*wide_ok=*/true);
11998 /* A `)' completes the static assertion. */
11999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12000 cp_parser_skip_to_closing_parenthesis (parser,
12001 /*recovering=*/true,
12002 /*or_comma=*/false,
12003 /*consume_paren=*/true);
12005 /* A semicolon terminates the declaration. */
12006 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12008 /* Complete the static assertion, which may mean either processing
12009 the static assert now or saving it for template instantiation. */
12010 finish_static_assert (condition, message, saved_loc, member_p);
12013 /* Parse the expression in decltype ( expression ). */
12015 static tree
12016 cp_parser_decltype_expr (cp_parser *parser,
12017 bool &id_expression_or_member_access_p)
12019 cp_token *id_expr_start_token;
12020 tree expr;
12022 /* First, try parsing an id-expression. */
12023 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12024 cp_parser_parse_tentatively (parser);
12025 expr = cp_parser_id_expression (parser,
12026 /*template_keyword_p=*/false,
12027 /*check_dependency_p=*/true,
12028 /*template_p=*/NULL,
12029 /*declarator_p=*/false,
12030 /*optional_p=*/false);
12032 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12034 bool non_integral_constant_expression_p = false;
12035 tree id_expression = expr;
12036 cp_id_kind idk;
12037 const char *error_msg;
12039 if (identifier_p (expr))
12040 /* Lookup the name we got back from the id-expression. */
12041 expr = cp_parser_lookup_name_simple (parser, expr,
12042 id_expr_start_token->location);
12044 if (expr
12045 && expr != error_mark_node
12046 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12047 && TREE_CODE (expr) != TYPE_DECL
12048 && (TREE_CODE (expr) != BIT_NOT_EXPR
12049 || !TYPE_P (TREE_OPERAND (expr, 0)))
12050 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12052 /* Complete lookup of the id-expression. */
12053 expr = (finish_id_expression
12054 (id_expression, expr, parser->scope, &idk,
12055 /*integral_constant_expression_p=*/false,
12056 /*allow_non_integral_constant_expression_p=*/true,
12057 &non_integral_constant_expression_p,
12058 /*template_p=*/false,
12059 /*done=*/true,
12060 /*address_p=*/false,
12061 /*template_arg_p=*/false,
12062 &error_msg,
12063 id_expr_start_token->location));
12065 if (expr == error_mark_node)
12066 /* We found an id-expression, but it was something that we
12067 should not have found. This is an error, not something
12068 we can recover from, so note that we found an
12069 id-expression and we'll recover as gracefully as
12070 possible. */
12071 id_expression_or_member_access_p = true;
12074 if (expr
12075 && expr != error_mark_node
12076 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12077 /* We have an id-expression. */
12078 id_expression_or_member_access_p = true;
12081 if (!id_expression_or_member_access_p)
12083 /* Abort the id-expression parse. */
12084 cp_parser_abort_tentative_parse (parser);
12086 /* Parsing tentatively, again. */
12087 cp_parser_parse_tentatively (parser);
12089 /* Parse a class member access. */
12090 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12091 /*cast_p=*/false, /*decltype*/true,
12092 /*member_access_only_p=*/true, NULL);
12094 if (expr
12095 && expr != error_mark_node
12096 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12097 /* We have an id-expression. */
12098 id_expression_or_member_access_p = true;
12101 if (id_expression_or_member_access_p)
12102 /* We have parsed the complete id-expression or member access. */
12103 cp_parser_parse_definitely (parser);
12104 else
12106 /* Abort our attempt to parse an id-expression or member access
12107 expression. */
12108 cp_parser_abort_tentative_parse (parser);
12110 /* Parse a full expression. */
12111 expr = cp_parser_expression (parser, /*cast_p=*/false,
12112 /*decltype*/true, NULL);
12115 return expr;
12118 /* Parse a `decltype' type. Returns the type.
12120 simple-type-specifier:
12121 decltype ( expression )
12122 C++14 proposal:
12123 decltype ( auto ) */
12125 static tree
12126 cp_parser_decltype (cp_parser *parser)
12128 tree expr;
12129 bool id_expression_or_member_access_p = false;
12130 const char *saved_message;
12131 bool saved_integral_constant_expression_p;
12132 bool saved_non_integral_constant_expression_p;
12133 bool saved_greater_than_is_operator_p;
12134 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12136 if (start_token->type == CPP_DECLTYPE)
12138 /* Already parsed. */
12139 cp_lexer_consume_token (parser->lexer);
12140 return start_token->u.value;
12143 /* Look for the `decltype' token. */
12144 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12145 return error_mark_node;
12147 /* Parse the opening `('. */
12148 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12149 return error_mark_node;
12151 /* decltype (auto) */
12152 if (cxx_dialect >= cxx1y
12153 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12155 cp_lexer_consume_token (parser->lexer);
12156 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12157 return error_mark_node;
12158 expr = make_decltype_auto ();
12159 AUTO_IS_DECLTYPE (expr) = true;
12160 goto rewrite;
12163 /* Types cannot be defined in a `decltype' expression. Save away the
12164 old message. */
12165 saved_message = parser->type_definition_forbidden_message;
12167 /* And create the new one. */
12168 parser->type_definition_forbidden_message
12169 = G_("types may not be defined in %<decltype%> expressions");
12171 /* The restrictions on constant-expressions do not apply inside
12172 decltype expressions. */
12173 saved_integral_constant_expression_p
12174 = parser->integral_constant_expression_p;
12175 saved_non_integral_constant_expression_p
12176 = parser->non_integral_constant_expression_p;
12177 parser->integral_constant_expression_p = false;
12179 /* Within a parenthesized expression, a `>' token is always
12180 the greater-than operator. */
12181 saved_greater_than_is_operator_p
12182 = parser->greater_than_is_operator_p;
12183 parser->greater_than_is_operator_p = true;
12185 /* Do not actually evaluate the expression. */
12186 ++cp_unevaluated_operand;
12188 /* Do not warn about problems with the expression. */
12189 ++c_inhibit_evaluation_warnings;
12191 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12193 /* Go back to evaluating expressions. */
12194 --cp_unevaluated_operand;
12195 --c_inhibit_evaluation_warnings;
12197 /* The `>' token might be the end of a template-id or
12198 template-parameter-list now. */
12199 parser->greater_than_is_operator_p
12200 = saved_greater_than_is_operator_p;
12202 /* Restore the old message and the integral constant expression
12203 flags. */
12204 parser->type_definition_forbidden_message = saved_message;
12205 parser->integral_constant_expression_p
12206 = saved_integral_constant_expression_p;
12207 parser->non_integral_constant_expression_p
12208 = saved_non_integral_constant_expression_p;
12210 /* Parse to the closing `)'. */
12211 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12213 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12214 /*consume_paren=*/true);
12215 return error_mark_node;
12218 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12219 tf_warning_or_error);
12221 rewrite:
12222 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12223 it again. */
12224 start_token->type = CPP_DECLTYPE;
12225 start_token->u.value = expr;
12226 start_token->keyword = RID_MAX;
12227 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12229 return expr;
12232 /* Special member functions [gram.special] */
12234 /* Parse a conversion-function-id.
12236 conversion-function-id:
12237 operator conversion-type-id
12239 Returns an IDENTIFIER_NODE representing the operator. */
12241 static tree
12242 cp_parser_conversion_function_id (cp_parser* parser)
12244 tree type;
12245 tree saved_scope;
12246 tree saved_qualifying_scope;
12247 tree saved_object_scope;
12248 tree pushed_scope = NULL_TREE;
12250 /* Look for the `operator' token. */
12251 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12252 return error_mark_node;
12253 /* When we parse the conversion-type-id, the current scope will be
12254 reset. However, we need that information in able to look up the
12255 conversion function later, so we save it here. */
12256 saved_scope = parser->scope;
12257 saved_qualifying_scope = parser->qualifying_scope;
12258 saved_object_scope = parser->object_scope;
12259 /* We must enter the scope of the class so that the names of
12260 entities declared within the class are available in the
12261 conversion-type-id. For example, consider:
12263 struct S {
12264 typedef int I;
12265 operator I();
12268 S::operator I() { ... }
12270 In order to see that `I' is a type-name in the definition, we
12271 must be in the scope of `S'. */
12272 if (saved_scope)
12273 pushed_scope = push_scope (saved_scope);
12274 /* Parse the conversion-type-id. */
12275 type = cp_parser_conversion_type_id (parser);
12276 /* Leave the scope of the class, if any. */
12277 if (pushed_scope)
12278 pop_scope (pushed_scope);
12279 /* Restore the saved scope. */
12280 parser->scope = saved_scope;
12281 parser->qualifying_scope = saved_qualifying_scope;
12282 parser->object_scope = saved_object_scope;
12283 /* If the TYPE is invalid, indicate failure. */
12284 if (type == error_mark_node)
12285 return error_mark_node;
12286 return mangle_conv_op_name_for_type (type);
12289 /* Parse a conversion-type-id:
12291 conversion-type-id:
12292 type-specifier-seq conversion-declarator [opt]
12294 Returns the TYPE specified. */
12296 static tree
12297 cp_parser_conversion_type_id (cp_parser* parser)
12299 tree attributes;
12300 cp_decl_specifier_seq type_specifiers;
12301 cp_declarator *declarator;
12302 tree type_specified;
12303 const char *saved_message;
12305 /* Parse the attributes. */
12306 attributes = cp_parser_attributes_opt (parser);
12308 saved_message = parser->type_definition_forbidden_message;
12309 parser->type_definition_forbidden_message
12310 = G_("types may not be defined in a conversion-type-id");
12312 /* Parse the type-specifiers. */
12313 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12314 /*is_trailing_return=*/false,
12315 &type_specifiers);
12317 parser->type_definition_forbidden_message = saved_message;
12319 /* If that didn't work, stop. */
12320 if (type_specifiers.type == error_mark_node)
12321 return error_mark_node;
12322 /* Parse the conversion-declarator. */
12323 declarator = cp_parser_conversion_declarator_opt (parser);
12325 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12326 /*initialized=*/0, &attributes);
12327 if (attributes)
12328 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12330 /* Don't give this error when parsing tentatively. This happens to
12331 work because we always parse this definitively once. */
12332 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12333 && type_uses_auto (type_specified))
12335 if (cxx_dialect < cxx1y)
12337 error ("invalid use of %<auto%> in conversion operator");
12338 return error_mark_node;
12340 else if (template_parm_scope_p ())
12341 warning (0, "use of %<auto%> in member template "
12342 "conversion operator can never be deduced");
12345 return type_specified;
12348 /* Parse an (optional) conversion-declarator.
12350 conversion-declarator:
12351 ptr-operator conversion-declarator [opt]
12355 static cp_declarator *
12356 cp_parser_conversion_declarator_opt (cp_parser* parser)
12358 enum tree_code code;
12359 tree class_type, std_attributes = NULL_TREE;
12360 cp_cv_quals cv_quals;
12362 /* We don't know if there's a ptr-operator next, or not. */
12363 cp_parser_parse_tentatively (parser);
12364 /* Try the ptr-operator. */
12365 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12366 &std_attributes);
12367 /* If it worked, look for more conversion-declarators. */
12368 if (cp_parser_parse_definitely (parser))
12370 cp_declarator *declarator;
12372 /* Parse another optional declarator. */
12373 declarator = cp_parser_conversion_declarator_opt (parser);
12375 declarator = cp_parser_make_indirect_declarator
12376 (code, class_type, cv_quals, declarator, std_attributes);
12378 return declarator;
12381 return NULL;
12384 /* Parse an (optional) ctor-initializer.
12386 ctor-initializer:
12387 : mem-initializer-list
12389 Returns TRUE iff the ctor-initializer was actually present. */
12391 static bool
12392 cp_parser_ctor_initializer_opt (cp_parser* parser)
12394 /* If the next token is not a `:', then there is no
12395 ctor-initializer. */
12396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12398 /* Do default initialization of any bases and members. */
12399 if (DECL_CONSTRUCTOR_P (current_function_decl))
12400 finish_mem_initializers (NULL_TREE);
12402 return false;
12405 /* Consume the `:' token. */
12406 cp_lexer_consume_token (parser->lexer);
12407 /* And the mem-initializer-list. */
12408 cp_parser_mem_initializer_list (parser);
12410 return true;
12413 /* Parse a mem-initializer-list.
12415 mem-initializer-list:
12416 mem-initializer ... [opt]
12417 mem-initializer ... [opt] , mem-initializer-list */
12419 static void
12420 cp_parser_mem_initializer_list (cp_parser* parser)
12422 tree mem_initializer_list = NULL_TREE;
12423 tree target_ctor = error_mark_node;
12424 cp_token *token = cp_lexer_peek_token (parser->lexer);
12426 /* Let the semantic analysis code know that we are starting the
12427 mem-initializer-list. */
12428 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12429 error_at (token->location,
12430 "only constructors take member initializers");
12432 /* Loop through the list. */
12433 while (true)
12435 tree mem_initializer;
12437 token = cp_lexer_peek_token (parser->lexer);
12438 /* Parse the mem-initializer. */
12439 mem_initializer = cp_parser_mem_initializer (parser);
12440 /* If the next token is a `...', we're expanding member initializers. */
12441 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12443 /* Consume the `...'. */
12444 cp_lexer_consume_token (parser->lexer);
12446 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12447 can be expanded but members cannot. */
12448 if (mem_initializer != error_mark_node
12449 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12451 error_at (token->location,
12452 "cannot expand initializer for member %<%D%>",
12453 TREE_PURPOSE (mem_initializer));
12454 mem_initializer = error_mark_node;
12457 /* Construct the pack expansion type. */
12458 if (mem_initializer != error_mark_node)
12459 mem_initializer = make_pack_expansion (mem_initializer);
12461 if (target_ctor != error_mark_node
12462 && mem_initializer != error_mark_node)
12464 error ("mem-initializer for %qD follows constructor delegation",
12465 TREE_PURPOSE (mem_initializer));
12466 mem_initializer = error_mark_node;
12468 /* Look for a target constructor. */
12469 if (mem_initializer != error_mark_node
12470 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12471 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12473 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12474 if (mem_initializer_list)
12476 error ("constructor delegation follows mem-initializer for %qD",
12477 TREE_PURPOSE (mem_initializer_list));
12478 mem_initializer = error_mark_node;
12480 target_ctor = mem_initializer;
12482 /* Add it to the list, unless it was erroneous. */
12483 if (mem_initializer != error_mark_node)
12485 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12486 mem_initializer_list = mem_initializer;
12488 /* If the next token is not a `,', we're done. */
12489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12490 break;
12491 /* Consume the `,' token. */
12492 cp_lexer_consume_token (parser->lexer);
12495 /* Perform semantic analysis. */
12496 if (DECL_CONSTRUCTOR_P (current_function_decl))
12497 finish_mem_initializers (mem_initializer_list);
12500 /* Parse a mem-initializer.
12502 mem-initializer:
12503 mem-initializer-id ( expression-list [opt] )
12504 mem-initializer-id braced-init-list
12506 GNU extension:
12508 mem-initializer:
12509 ( expression-list [opt] )
12511 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12512 class) or FIELD_DECL (for a non-static data member) to initialize;
12513 the TREE_VALUE is the expression-list. An empty initialization
12514 list is represented by void_list_node. */
12516 static tree
12517 cp_parser_mem_initializer (cp_parser* parser)
12519 tree mem_initializer_id;
12520 tree expression_list;
12521 tree member;
12522 cp_token *token = cp_lexer_peek_token (parser->lexer);
12524 /* Find out what is being initialized. */
12525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12527 permerror (token->location,
12528 "anachronistic old-style base class initializer");
12529 mem_initializer_id = NULL_TREE;
12531 else
12533 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12534 if (mem_initializer_id == error_mark_node)
12535 return mem_initializer_id;
12537 member = expand_member_init (mem_initializer_id);
12538 if (member && !DECL_P (member))
12539 in_base_initializer = 1;
12541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12543 bool expr_non_constant_p;
12544 cp_lexer_set_source_position (parser->lexer);
12545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12546 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12547 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12548 expression_list = build_tree_list (NULL_TREE, expression_list);
12550 else
12552 vec<tree, va_gc> *vec;
12553 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12554 /*cast_p=*/false,
12555 /*allow_expansion_p=*/true,
12556 /*non_constant_p=*/NULL);
12557 if (vec == NULL)
12558 return error_mark_node;
12559 expression_list = build_tree_list_vec (vec);
12560 release_tree_vector (vec);
12563 if (expression_list == error_mark_node)
12564 return error_mark_node;
12565 if (!expression_list)
12566 expression_list = void_type_node;
12568 in_base_initializer = 0;
12570 return member ? build_tree_list (member, expression_list) : error_mark_node;
12573 /* Parse a mem-initializer-id.
12575 mem-initializer-id:
12576 :: [opt] nested-name-specifier [opt] class-name
12577 identifier
12579 Returns a TYPE indicating the class to be initializer for the first
12580 production. Returns an IDENTIFIER_NODE indicating the data member
12581 to be initialized for the second production. */
12583 static tree
12584 cp_parser_mem_initializer_id (cp_parser* parser)
12586 bool global_scope_p;
12587 bool nested_name_specifier_p;
12588 bool template_p = false;
12589 tree id;
12591 cp_token *token = cp_lexer_peek_token (parser->lexer);
12593 /* `typename' is not allowed in this context ([temp.res]). */
12594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12596 error_at (token->location,
12597 "keyword %<typename%> not allowed in this context (a qualified "
12598 "member initializer is implicitly a type)");
12599 cp_lexer_consume_token (parser->lexer);
12601 /* Look for the optional `::' operator. */
12602 global_scope_p
12603 = (cp_parser_global_scope_opt (parser,
12604 /*current_scope_valid_p=*/false)
12605 != NULL_TREE);
12606 /* Look for the optional nested-name-specifier. The simplest way to
12607 implement:
12609 [temp.res]
12611 The keyword `typename' is not permitted in a base-specifier or
12612 mem-initializer; in these contexts a qualified name that
12613 depends on a template-parameter is implicitly assumed to be a
12614 type name.
12616 is to assume that we have seen the `typename' keyword at this
12617 point. */
12618 nested_name_specifier_p
12619 = (cp_parser_nested_name_specifier_opt (parser,
12620 /*typename_keyword_p=*/true,
12621 /*check_dependency_p=*/true,
12622 /*type_p=*/true,
12623 /*is_declaration=*/true)
12624 != NULL_TREE);
12625 if (nested_name_specifier_p)
12626 template_p = cp_parser_optional_template_keyword (parser);
12627 /* If there is a `::' operator or a nested-name-specifier, then we
12628 are definitely looking for a class-name. */
12629 if (global_scope_p || nested_name_specifier_p)
12630 return cp_parser_class_name (parser,
12631 /*typename_keyword_p=*/true,
12632 /*template_keyword_p=*/template_p,
12633 typename_type,
12634 /*check_dependency_p=*/true,
12635 /*class_head_p=*/false,
12636 /*is_declaration=*/true);
12637 /* Otherwise, we could also be looking for an ordinary identifier. */
12638 cp_parser_parse_tentatively (parser);
12639 /* Try a class-name. */
12640 id = cp_parser_class_name (parser,
12641 /*typename_keyword_p=*/true,
12642 /*template_keyword_p=*/false,
12643 none_type,
12644 /*check_dependency_p=*/true,
12645 /*class_head_p=*/false,
12646 /*is_declaration=*/true);
12647 /* If we found one, we're done. */
12648 if (cp_parser_parse_definitely (parser))
12649 return id;
12650 /* Otherwise, look for an ordinary identifier. */
12651 return cp_parser_identifier (parser);
12654 /* Overloading [gram.over] */
12656 /* Parse an operator-function-id.
12658 operator-function-id:
12659 operator operator
12661 Returns an IDENTIFIER_NODE for the operator which is a
12662 human-readable spelling of the identifier, e.g., `operator +'. */
12664 static tree
12665 cp_parser_operator_function_id (cp_parser* parser)
12667 /* Look for the `operator' keyword. */
12668 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12669 return error_mark_node;
12670 /* And then the name of the operator itself. */
12671 return cp_parser_operator (parser);
12674 /* Return an identifier node for a user-defined literal operator.
12675 The suffix identifier is chained to the operator name identifier. */
12677 static tree
12678 cp_literal_operator_id (const char* name)
12680 tree identifier;
12681 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12682 + strlen (name) + 10);
12683 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12684 identifier = get_identifier (buffer);
12686 return identifier;
12689 /* Parse an operator.
12691 operator:
12692 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12693 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12694 || ++ -- , ->* -> () []
12696 GNU Extensions:
12698 operator:
12699 <? >? <?= >?=
12701 Returns an IDENTIFIER_NODE for the operator which is a
12702 human-readable spelling of the identifier, e.g., `operator +'. */
12704 static tree
12705 cp_parser_operator (cp_parser* parser)
12707 tree id = NULL_TREE;
12708 cp_token *token;
12709 bool bad_encoding_prefix = false;
12711 /* Peek at the next token. */
12712 token = cp_lexer_peek_token (parser->lexer);
12713 /* Figure out which operator we have. */
12714 switch (token->type)
12716 case CPP_KEYWORD:
12718 enum tree_code op;
12720 /* The keyword should be either `new' or `delete'. */
12721 if (token->keyword == RID_NEW)
12722 op = NEW_EXPR;
12723 else if (token->keyword == RID_DELETE)
12724 op = DELETE_EXPR;
12725 else
12726 break;
12728 /* Consume the `new' or `delete' token. */
12729 cp_lexer_consume_token (parser->lexer);
12731 /* Peek at the next token. */
12732 token = cp_lexer_peek_token (parser->lexer);
12733 /* If it's a `[' token then this is the array variant of the
12734 operator. */
12735 if (token->type == CPP_OPEN_SQUARE)
12737 /* Consume the `[' token. */
12738 cp_lexer_consume_token (parser->lexer);
12739 /* Look for the `]' token. */
12740 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12741 id = ansi_opname (op == NEW_EXPR
12742 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12744 /* Otherwise, we have the non-array variant. */
12745 else
12746 id = ansi_opname (op);
12748 return id;
12751 case CPP_PLUS:
12752 id = ansi_opname (PLUS_EXPR);
12753 break;
12755 case CPP_MINUS:
12756 id = ansi_opname (MINUS_EXPR);
12757 break;
12759 case CPP_MULT:
12760 id = ansi_opname (MULT_EXPR);
12761 break;
12763 case CPP_DIV:
12764 id = ansi_opname (TRUNC_DIV_EXPR);
12765 break;
12767 case CPP_MOD:
12768 id = ansi_opname (TRUNC_MOD_EXPR);
12769 break;
12771 case CPP_XOR:
12772 id = ansi_opname (BIT_XOR_EXPR);
12773 break;
12775 case CPP_AND:
12776 id = ansi_opname (BIT_AND_EXPR);
12777 break;
12779 case CPP_OR:
12780 id = ansi_opname (BIT_IOR_EXPR);
12781 break;
12783 case CPP_COMPL:
12784 id = ansi_opname (BIT_NOT_EXPR);
12785 break;
12787 case CPP_NOT:
12788 id = ansi_opname (TRUTH_NOT_EXPR);
12789 break;
12791 case CPP_EQ:
12792 id = ansi_assopname (NOP_EXPR);
12793 break;
12795 case CPP_LESS:
12796 id = ansi_opname (LT_EXPR);
12797 break;
12799 case CPP_GREATER:
12800 id = ansi_opname (GT_EXPR);
12801 break;
12803 case CPP_PLUS_EQ:
12804 id = ansi_assopname (PLUS_EXPR);
12805 break;
12807 case CPP_MINUS_EQ:
12808 id = ansi_assopname (MINUS_EXPR);
12809 break;
12811 case CPP_MULT_EQ:
12812 id = ansi_assopname (MULT_EXPR);
12813 break;
12815 case CPP_DIV_EQ:
12816 id = ansi_assopname (TRUNC_DIV_EXPR);
12817 break;
12819 case CPP_MOD_EQ:
12820 id = ansi_assopname (TRUNC_MOD_EXPR);
12821 break;
12823 case CPP_XOR_EQ:
12824 id = ansi_assopname (BIT_XOR_EXPR);
12825 break;
12827 case CPP_AND_EQ:
12828 id = ansi_assopname (BIT_AND_EXPR);
12829 break;
12831 case CPP_OR_EQ:
12832 id = ansi_assopname (BIT_IOR_EXPR);
12833 break;
12835 case CPP_LSHIFT:
12836 id = ansi_opname (LSHIFT_EXPR);
12837 break;
12839 case CPP_RSHIFT:
12840 id = ansi_opname (RSHIFT_EXPR);
12841 break;
12843 case CPP_LSHIFT_EQ:
12844 id = ansi_assopname (LSHIFT_EXPR);
12845 break;
12847 case CPP_RSHIFT_EQ:
12848 id = ansi_assopname (RSHIFT_EXPR);
12849 break;
12851 case CPP_EQ_EQ:
12852 id = ansi_opname (EQ_EXPR);
12853 break;
12855 case CPP_NOT_EQ:
12856 id = ansi_opname (NE_EXPR);
12857 break;
12859 case CPP_LESS_EQ:
12860 id = ansi_opname (LE_EXPR);
12861 break;
12863 case CPP_GREATER_EQ:
12864 id = ansi_opname (GE_EXPR);
12865 break;
12867 case CPP_AND_AND:
12868 id = ansi_opname (TRUTH_ANDIF_EXPR);
12869 break;
12871 case CPP_OR_OR:
12872 id = ansi_opname (TRUTH_ORIF_EXPR);
12873 break;
12875 case CPP_PLUS_PLUS:
12876 id = ansi_opname (POSTINCREMENT_EXPR);
12877 break;
12879 case CPP_MINUS_MINUS:
12880 id = ansi_opname (PREDECREMENT_EXPR);
12881 break;
12883 case CPP_COMMA:
12884 id = ansi_opname (COMPOUND_EXPR);
12885 break;
12887 case CPP_DEREF_STAR:
12888 id = ansi_opname (MEMBER_REF);
12889 break;
12891 case CPP_DEREF:
12892 id = ansi_opname (COMPONENT_REF);
12893 break;
12895 case CPP_OPEN_PAREN:
12896 /* Consume the `('. */
12897 cp_lexer_consume_token (parser->lexer);
12898 /* Look for the matching `)'. */
12899 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12900 return ansi_opname (CALL_EXPR);
12902 case CPP_OPEN_SQUARE:
12903 /* Consume the `['. */
12904 cp_lexer_consume_token (parser->lexer);
12905 /* Look for the matching `]'. */
12906 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12907 return ansi_opname (ARRAY_REF);
12909 case CPP_WSTRING:
12910 case CPP_STRING16:
12911 case CPP_STRING32:
12912 case CPP_UTF8STRING:
12913 bad_encoding_prefix = true;
12914 /* Fall through. */
12916 case CPP_STRING:
12917 if (cxx_dialect == cxx98)
12918 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12919 if (bad_encoding_prefix)
12921 error ("invalid encoding prefix in literal operator");
12922 return error_mark_node;
12924 if (TREE_STRING_LENGTH (token->u.value) > 2)
12926 error ("expected empty string after %<operator%> keyword");
12927 return error_mark_node;
12929 /* Consume the string. */
12930 cp_lexer_consume_token (parser->lexer);
12931 /* Look for the suffix identifier. */
12932 token = cp_lexer_peek_token (parser->lexer);
12933 if (token->type == CPP_NAME)
12935 id = cp_parser_identifier (parser);
12936 if (id != error_mark_node)
12938 const char *name = IDENTIFIER_POINTER (id);
12939 return cp_literal_operator_id (name);
12942 else if (token->type == CPP_KEYWORD)
12944 error ("unexpected keyword;"
12945 " remove space between quotes and suffix identifier");
12946 return error_mark_node;
12948 else
12950 error ("expected suffix identifier");
12951 return error_mark_node;
12954 case CPP_WSTRING_USERDEF:
12955 case CPP_STRING16_USERDEF:
12956 case CPP_STRING32_USERDEF:
12957 case CPP_UTF8STRING_USERDEF:
12958 bad_encoding_prefix = true;
12959 /* Fall through. */
12961 case CPP_STRING_USERDEF:
12962 if (cxx_dialect == cxx98)
12963 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12964 if (bad_encoding_prefix)
12966 error ("invalid encoding prefix in literal operator");
12967 return error_mark_node;
12970 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12971 if (TREE_STRING_LENGTH (string_tree) > 2)
12973 error ("expected empty string after %<operator%> keyword");
12974 return error_mark_node;
12976 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12977 /* Consume the user-defined string literal. */
12978 cp_lexer_consume_token (parser->lexer);
12979 if (id != error_mark_node)
12981 const char *name = IDENTIFIER_POINTER (id);
12982 return cp_literal_operator_id (name);
12984 else
12985 return error_mark_node;
12988 default:
12989 /* Anything else is an error. */
12990 break;
12993 /* If we have selected an identifier, we need to consume the
12994 operator token. */
12995 if (id)
12996 cp_lexer_consume_token (parser->lexer);
12997 /* Otherwise, no valid operator name was present. */
12998 else
13000 cp_parser_error (parser, "expected operator");
13001 id = error_mark_node;
13004 return id;
13007 /* Parse a template-declaration.
13009 template-declaration:
13010 export [opt] template < template-parameter-list > declaration
13012 If MEMBER_P is TRUE, this template-declaration occurs within a
13013 class-specifier.
13015 The grammar rule given by the standard isn't correct. What
13016 is really meant is:
13018 template-declaration:
13019 export [opt] template-parameter-list-seq
13020 decl-specifier-seq [opt] init-declarator [opt] ;
13021 export [opt] template-parameter-list-seq
13022 function-definition
13024 template-parameter-list-seq:
13025 template-parameter-list-seq [opt]
13026 template < template-parameter-list >
13028 Concept Extensions:
13030 template-parameter-list-seq:
13031 template < template-parameter-list > template-requirement [opt]
13033 template-requirement:
13034 requires logical-or-expression
13037 static void
13038 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13040 /* Check for `export'. */
13041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13043 /* Consume the `export' token. */
13044 cp_lexer_consume_token (parser->lexer);
13045 /* Warn that we do not support `export'. */
13046 warning (0, "keyword %<export%> not implemented, and will be ignored");
13049 cp_parser_template_declaration_after_export (parser, member_p);
13052 /* Parse a template-parameter-list.
13054 template-parameter-list:
13055 template-parameter
13056 template-parameter-list , template-parameter
13058 Returns a TREE_LIST. Each node represents a template parameter.
13059 The nodes are connected via their TREE_CHAINs. */
13061 static tree
13062 cp_parser_template_parameter_list (cp_parser* parser)
13064 tree parameter_list = NULL_TREE;
13066 begin_template_parm_list ();
13068 /* The loop below parses the template parms. We first need to know
13069 the total number of template parms to be able to compute proper
13070 canonical types of each dependent type. So after the loop, when
13071 we know the total number of template parms,
13072 end_template_parm_list computes the proper canonical types and
13073 fixes up the dependent types accordingly. */
13074 while (true)
13076 tree parameter;
13077 bool is_non_type;
13078 bool is_parameter_pack;
13079 location_t parm_loc;
13081 /* Parse the template-parameter. */
13082 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13083 parameter = cp_parser_template_parameter (parser,
13084 &is_non_type,
13085 &is_parameter_pack);
13086 /* Add it to the list. */
13087 if (parameter != error_mark_node)
13088 parameter_list = process_template_parm (parameter_list,
13089 parm_loc,
13090 parameter,
13091 is_non_type,
13092 is_parameter_pack);
13093 else
13095 tree err_parm = build_tree_list (parameter, parameter);
13096 parameter_list = chainon (parameter_list, err_parm);
13099 /* If the next token is not a `,', we're done. */
13100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13101 break;
13102 /* Otherwise, consume the `,' token. */
13103 cp_lexer_consume_token (parser->lexer);
13106 return end_template_parm_list (parameter_list);
13109 // Returns true if PARM declares a constrained-parameter.
13110 static inline bool
13111 cp_is_constrained_parameter (cp_parameter_declarator *parm)
13113 gcc_assert (parm);
13114 tree decl = parm->decl_specifiers.type;
13115 return (decl
13116 && TREE_CODE (decl) == TYPE_DECL
13117 && DECL_INITIAL (decl)
13118 && DECL_SIZE_UNIT (decl)
13119 && TREE_CODE (DECL_SIZE_UNIT (decl)) == FUNCTION_DECL);
13123 // Check that the type parameter is only a declarator-id, and that its
13124 // type is not cv-qualified.
13125 bool
13126 cp_check_constrained_type_parm (cp_parser *parser,
13127 cp_parameter_declarator *parm)
13129 // Don't ptr, ref, function, or array declarators for a constrained type
13130 // or templtae template parameter.
13131 if (parm->declarator->kind != cdk_id)
13133 cp_parser_error (parser, "invalid constrained type parameter");
13134 return false;
13137 // Don't allow cv-qualified type parameters.
13138 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const) ||
13139 decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13141 cp_parser_error (parser, "cv-qualified type parameter");
13142 return false;
13145 return true;
13148 // Finish parsing/processing a template type parameter and chekcing
13149 // various restrictions.
13150 static inline tree
13151 cp_constrained_type_template_parm (cp_parser *parser,
13152 tree id,
13153 cp_parameter_declarator* parmdecl)
13155 if (cp_check_constrained_type_parm (parser, parmdecl))
13156 return finish_template_type_parm (class_type_node, id);
13157 else
13158 return error_mark_node;
13161 // Finish parsing/processing a template template parameter by borrowing
13162 // the template parameter list from the prototype parameter.
13163 static tree
13164 cp_constrained_template_template_parm (cp_parser *parser,
13165 tree proto,
13166 tree id,
13167 cp_parameter_declarator *parmdecl)
13169 if (!cp_check_constrained_type_parm (parser, parmdecl))
13170 return error_mark_node;
13172 // FIXME: This should probably be copied, and we may need to adjust
13173 // the template parameter depths.
13174 tree saved_parms = current_template_parms;
13175 begin_template_parm_list ();
13176 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13177 end_template_parm_list ();
13179 tree parm = finish_template_template_parm (class_type_node, id);
13180 current_template_parms = saved_parms;
13181 return parm;
13184 // Create a new non-type template parameter from the given PARM declarator.
13185 static tree
13186 cp_constrained_non_type_template_parm (bool *is_non_type,
13187 cp_parameter_declarator *parm)
13189 *is_non_type = true;
13190 cp_declarator *decl = parm->declarator;
13191 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13192 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13193 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13196 // Build a constrained template parameter based on the PARMDECL
13197 // declarator. The type of PARMDECL is the constrained type, which
13198 // refers to the prototype template parameter that ultimately
13199 // specifies the type of the declared parameter.
13200 static tree
13201 cp_finish_constrained_parameter (cp_parser *parser,
13202 cp_parameter_declarator *parmdecl,
13203 bool *is_non_type,
13204 bool *is_parameter_pack)
13206 tree decl = parmdecl->decl_specifiers.type;
13207 tree id = parmdecl->declarator->u.id.unqualified_name;
13208 tree def = parmdecl->default_argument;
13209 tree proto = DECL_INITIAL (decl);
13211 // Remember if the user declared this as a parameter pack and
13212 // erase that flag on the annotation. Template packs are dealt
13213 // with separately.
13214 bool is_pack = parmdecl->declarator->parameter_pack_p;
13215 if (is_pack)
13216 parmdecl->declarator->parameter_pack_p = false;
13218 // Is the prototype a parameter pack? If so, but the declaration
13219 // does not include "...", then emit an error.
13220 bool is_variadic = template_parameter_pack_p (proto);
13221 if (is_variadic && !is_pack)
13222 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13224 // The prototype is a template parameter pack, then the resulting
13225 // parameter also needs to be a pack.
13226 if (is_pack || is_variadic)
13227 *is_parameter_pack = true;
13229 // Build the parameter. Return an error if the declarator
13230 // was invalid.
13231 tree parm;
13232 if (TREE_CODE (proto) == TYPE_DECL)
13233 parm = cp_constrained_type_template_parm (parser, id, parmdecl);
13234 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13235 parm = cp_constrained_template_template_parm (parser, proto, id, parmdecl);
13236 else
13237 parm = cp_constrained_non_type_template_parm (is_non_type, parmdecl);
13238 if (parm == error_mark_node)
13239 return error_mark_node;
13241 // Finish the parameter decl and create a node attaching the
13242 // default argument and constraint.
13243 parm = build_tree_list (def, parm);
13244 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13245 return parm;
13249 /* Parse a template-parameter.
13251 template-parameter:
13252 type-parameter
13253 parameter-declaration
13255 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13256 the parameter. The TREE_PURPOSE is the default value, if any.
13257 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13258 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13259 set to true iff this parameter is a parameter pack. */
13261 static tree
13262 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13263 bool *is_parameter_pack)
13265 cp_token *token;
13266 cp_parameter_declarator *parameter_declarator;
13267 cp_declarator *id_declarator;
13268 tree parm;
13270 /* Assume it is a type parameter or a template parameter. */
13271 *is_non_type = false;
13272 /* Assume it not a parameter pack. */
13273 *is_parameter_pack = false;
13274 /* Peek at the next token. */
13275 token = cp_lexer_peek_token (parser->lexer);
13276 /* If it is `class' or `template', we have a type-parameter. */
13277 if (token->keyword == RID_TEMPLATE)
13278 return cp_parser_type_parameter (parser, is_parameter_pack);
13279 /* If it is `class' or `typename' we do not know yet whether it is a
13280 type parameter or a non-type parameter. Consider:
13282 template <typename T, typename T::X X> ...
13286 template <class C, class D*> ...
13288 Here, the first parameter is a type parameter, and the second is
13289 a non-type parameter. We can tell by looking at the token after
13290 the identifier -- if it is a `,', `=', or `>' then we have a type
13291 parameter. */
13292 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13294 /* Peek at the token after `class' or `typename'. */
13295 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13296 /* If it's an ellipsis, we have a template type parameter
13297 pack. */
13298 if (token->type == CPP_ELLIPSIS)
13299 return cp_parser_type_parameter (parser, is_parameter_pack);
13300 /* If it's an identifier, skip it. */
13301 if (token->type == CPP_NAME)
13302 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13303 /* Now, see if the token looks like the end of a template
13304 parameter. */
13305 if (token->type == CPP_COMMA
13306 || token->type == CPP_EQ
13307 || token->type == CPP_GREATER)
13308 return cp_parser_type_parameter (parser, is_parameter_pack);
13311 /* Otherwise, it is a non-type parameter.
13313 [temp.param]
13315 When parsing a default template-argument for a non-type
13316 template-parameter, the first non-nested `>' is taken as the end
13317 of the template parameter-list rather than a greater-than
13318 operator. */
13319 parameter_declarator
13320 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13321 /*parenthesized_p=*/NULL);
13323 if (!parameter_declarator)
13324 return error_mark_node;
13326 // The parameter may have been constrained.
13327 if (cp_is_constrained_parameter (parameter_declarator))
13328 return cp_finish_constrained_parameter (parser,
13329 parameter_declarator,
13330 is_non_type,
13331 is_parameter_pack);
13333 // Now we're sure that the parameter is a non-type parameter.
13334 *is_non_type = true;
13336 /* If the parameter declaration is marked as a parameter pack, set
13337 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13338 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13339 grokdeclarator. */
13340 if (parameter_declarator->declarator
13341 && parameter_declarator->declarator->parameter_pack_p)
13343 *is_parameter_pack = true;
13344 parameter_declarator->declarator->parameter_pack_p = false;
13347 if (parameter_declarator->default_argument)
13349 /* Can happen in some cases of erroneous input (c++/34892). */
13350 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13351 /* Consume the `...' for better error recovery. */
13352 cp_lexer_consume_token (parser->lexer);
13354 /* If the next token is an ellipsis, and we don't already have it
13355 marked as a parameter pack, then we have a parameter pack (that
13356 has no declarator). */
13357 else if (!*is_parameter_pack
13358 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13359 && (declarator_can_be_parameter_pack
13360 (parameter_declarator->declarator)))
13362 /* Consume the `...'. */
13363 cp_lexer_consume_token (parser->lexer);
13364 maybe_warn_variadic_templates ();
13366 *is_parameter_pack = true;
13368 /* We might end up with a pack expansion as the type of the non-type
13369 template parameter, in which case this is a non-type template
13370 parameter pack. */
13371 else if (parameter_declarator->decl_specifiers.type
13372 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13374 *is_parameter_pack = true;
13375 parameter_declarator->decl_specifiers.type =
13376 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13379 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13381 /* Parameter packs cannot have default arguments. However, a
13382 user may try to do so, so we'll parse them and give an
13383 appropriate diagnostic here. */
13385 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13387 /* Find the name of the parameter pack. */
13388 id_declarator = parameter_declarator->declarator;
13389 while (id_declarator && id_declarator->kind != cdk_id)
13390 id_declarator = id_declarator->declarator;
13392 if (id_declarator && id_declarator->kind == cdk_id)
13393 error_at (start_token->location,
13394 "template parameter pack %qD cannot have a default argument",
13395 id_declarator->u.id.unqualified_name);
13396 else
13397 error_at (start_token->location,
13398 "template parameter pack cannot have a default argument");
13400 /* Parse the default argument, but throw away the result. */
13401 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13404 parm = grokdeclarator (parameter_declarator->declarator,
13405 &parameter_declarator->decl_specifiers,
13406 TPARM, /*initialized=*/0,
13407 /*attrlist=*/NULL);
13408 if (parm == error_mark_node)
13409 return error_mark_node;
13411 return build_tree_list (parameter_declarator->default_argument, parm);
13414 /* Parse a type-parameter.
13416 type-parameter:
13417 class identifier [opt]
13418 class identifier [opt] = type-id
13419 typename identifier [opt]
13420 typename identifier [opt] = type-id
13421 template < template-parameter-list > class identifier [opt]
13422 template < template-parameter-list > class identifier [opt]
13423 = id-expression
13425 GNU Extension (variadic templates):
13427 type-parameter:
13428 class ... identifier [opt]
13429 typename ... identifier [opt]
13431 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13432 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13433 the declaration of the parameter.
13435 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13437 static tree
13438 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13440 cp_token *token;
13441 tree parameter;
13443 /* Look for a keyword to tell us what kind of parameter this is. */
13444 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13445 if (!token)
13446 return error_mark_node;
13448 switch (token->keyword)
13450 case RID_CLASS:
13451 case RID_TYPENAME:
13453 tree identifier;
13454 tree default_argument;
13456 /* If the next token is an ellipsis, we have a template
13457 argument pack. */
13458 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13460 /* Consume the `...' token. */
13461 cp_lexer_consume_token (parser->lexer);
13462 maybe_warn_variadic_templates ();
13464 *is_parameter_pack = true;
13467 /* If the next token is an identifier, then it names the
13468 parameter. */
13469 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13470 identifier = cp_parser_identifier (parser);
13471 else
13472 identifier = NULL_TREE;
13474 /* Create the parameter. */
13475 parameter = finish_template_type_parm (class_type_node, identifier);
13477 /* If the next token is an `=', we have a default argument. */
13478 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13480 /* Consume the `=' token. */
13481 cp_lexer_consume_token (parser->lexer);
13482 /* Parse the default-argument. */
13483 push_deferring_access_checks (dk_no_deferred);
13484 default_argument = cp_parser_type_id (parser);
13486 /* Template parameter packs cannot have default
13487 arguments. */
13488 if (*is_parameter_pack)
13490 if (identifier)
13491 error_at (token->location,
13492 "template parameter pack %qD cannot have a "
13493 "default argument", identifier);
13494 else
13495 error_at (token->location,
13496 "template parameter packs cannot have "
13497 "default arguments");
13498 default_argument = NULL_TREE;
13500 pop_deferring_access_checks ();
13502 else
13503 default_argument = NULL_TREE;
13505 /* Create the combined representation of the parameter and the
13506 default argument. */
13507 parameter = build_tree_list (default_argument, parameter);
13509 break;
13511 case RID_TEMPLATE:
13513 tree identifier;
13514 tree default_argument;
13516 // Save the current requirements before parsing the
13517 // template parameter list.
13518 tree saved_template_reqs = release (current_template_reqs);
13520 /* Look for the `<'. */
13521 cp_parser_require (parser, CPP_LESS, RT_LESS);
13522 /* Parse the template-parameter-list. */
13523 cp_parser_template_parameter_list (parser);
13524 /* Look for the `>'. */
13525 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13527 // If template requirements are present, parse them.
13528 if (flag_concepts)
13530 tree reqs = get_shorthand_requirements (current_template_parms);
13531 if (tree r = cp_parser_requires_clause_opt (parser))
13532 reqs = conjoin_requirements (reqs, r);
13533 current_template_reqs = finish_template_requirements (reqs);
13535 // Attach the constraints to the parameter list.
13536 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
13537 = current_template_reqs;
13540 /* Look for the `class' keyword. */
13541 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13542 /* If the next token is an ellipsis, we have a template
13543 argument pack. */
13544 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13546 /* Consume the `...' token. */
13547 cp_lexer_consume_token (parser->lexer);
13548 maybe_warn_variadic_templates ();
13550 *is_parameter_pack = true;
13552 /* If the next token is an `=', then there is a
13553 default-argument. If the next token is a `>', we are at
13554 the end of the parameter-list. If the next token is a `,',
13555 then we are at the end of this parameter. */
13556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13557 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13558 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13560 identifier = cp_parser_identifier (parser);
13561 /* Treat invalid names as if the parameter were nameless. */
13562 if (identifier == error_mark_node)
13563 identifier = NULL_TREE;
13565 else
13566 identifier = NULL_TREE;
13568 /* Create the template parameter. */
13569 parameter = finish_template_template_parm (class_type_node,
13570 identifier);
13572 // Restore the saved constraints.
13573 current_template_reqs = saved_template_reqs;
13575 /* If the next token is an `=', then there is a
13576 default-argument. */
13577 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13579 bool is_template;
13581 /* Consume the `='. */
13582 cp_lexer_consume_token (parser->lexer);
13583 /* Parse the id-expression. */
13584 push_deferring_access_checks (dk_no_deferred);
13585 /* save token before parsing the id-expression, for error
13586 reporting */
13587 token = cp_lexer_peek_token (parser->lexer);
13588 default_argument
13589 = cp_parser_id_expression (parser,
13590 /*template_keyword_p=*/false,
13591 /*check_dependency_p=*/true,
13592 /*template_p=*/&is_template,
13593 /*declarator_p=*/false,
13594 /*optional_p=*/false);
13595 if (TREE_CODE (default_argument) == TYPE_DECL)
13596 /* If the id-expression was a template-id that refers to
13597 a template-class, we already have the declaration here,
13598 so no further lookup is needed. */
13600 else
13601 /* Look up the name. */
13602 default_argument
13603 = cp_parser_lookup_name (parser, default_argument,
13604 none_type,
13605 /*is_template=*/is_template,
13606 /*is_namespace=*/false,
13607 /*check_dependency=*/true,
13608 /*ambiguous_decls=*/NULL,
13609 token->location);
13610 /* See if the default argument is valid. */
13611 default_argument
13612 = check_template_template_default_arg (default_argument);
13614 /* Template parameter packs cannot have default
13615 arguments. */
13616 if (*is_parameter_pack)
13618 if (identifier)
13619 error_at (token->location,
13620 "template parameter pack %qD cannot "
13621 "have a default argument",
13622 identifier);
13623 else
13624 error_at (token->location, "template parameter packs cannot "
13625 "have default arguments");
13626 default_argument = NULL_TREE;
13628 pop_deferring_access_checks ();
13630 else
13631 default_argument = NULL_TREE;
13633 /* Create the combined representation of the parameter and the
13634 default argument. */
13635 parameter = build_tree_list (default_argument, parameter);
13637 break;
13639 default:
13640 gcc_unreachable ();
13641 break;
13644 return parameter;
13647 /* Parse a template-id.
13649 template-id:
13650 template-name < template-argument-list [opt] >
13652 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13653 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13654 returned. Otherwise, if the template-name names a function, or set
13655 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13656 names a class, returns a TYPE_DECL for the specialization.
13658 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13659 uninstantiated templates. */
13661 static tree
13662 cp_parser_template_id (cp_parser *parser,
13663 bool template_keyword_p,
13664 bool check_dependency_p,
13665 enum tag_types tag_type,
13666 bool is_declaration)
13668 int i;
13669 tree templ;
13670 tree arguments;
13671 tree template_id;
13672 cp_token_position start_of_id = 0;
13673 deferred_access_check *chk;
13674 vec<deferred_access_check, va_gc> *access_check;
13675 cp_token *next_token = NULL, *next_token_2 = NULL;
13676 bool is_identifier;
13678 /* If the next token corresponds to a template-id, there is no need
13679 to reparse it. */
13680 next_token = cp_lexer_peek_token (parser->lexer);
13681 if (next_token->type == CPP_TEMPLATE_ID)
13683 struct tree_check *check_value;
13685 /* Get the stored value. */
13686 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13687 /* Perform any access checks that were deferred. */
13688 access_check = check_value->checks;
13689 if (access_check)
13691 FOR_EACH_VEC_ELT (*access_check, i, chk)
13692 perform_or_defer_access_check (chk->binfo,
13693 chk->decl,
13694 chk->diag_decl,
13695 tf_warning_or_error);
13697 /* Return the stored value. */
13698 return check_value->value;
13701 /* Avoid performing name lookup if there is no possibility of
13702 finding a template-id. */
13703 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13704 || (next_token->type == CPP_NAME
13705 && !cp_parser_nth_token_starts_template_argument_list_p
13706 (parser, 2)))
13708 cp_parser_error (parser, "expected template-id");
13709 return error_mark_node;
13712 /* Remember where the template-id starts. */
13713 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13714 start_of_id = cp_lexer_token_position (parser->lexer, false);
13716 push_deferring_access_checks (dk_deferred);
13718 /* Parse the template-name. */
13719 is_identifier = false;
13720 templ = cp_parser_template_name (parser, template_keyword_p,
13721 check_dependency_p,
13722 is_declaration,
13723 tag_type,
13724 &is_identifier);
13725 if (templ == error_mark_node || is_identifier)
13727 pop_deferring_access_checks ();
13728 return templ;
13731 /* If we find the sequence `[:' after a template-name, it's probably
13732 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13733 parse correctly the argument list. */
13734 next_token = cp_lexer_peek_token (parser->lexer);
13735 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13736 if (next_token->type == CPP_OPEN_SQUARE
13737 && next_token->flags & DIGRAPH
13738 && next_token_2->type == CPP_COLON
13739 && !(next_token_2->flags & PREV_WHITE))
13741 cp_parser_parse_tentatively (parser);
13742 /* Change `:' into `::'. */
13743 next_token_2->type = CPP_SCOPE;
13744 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13745 CPP_LESS. */
13746 cp_lexer_consume_token (parser->lexer);
13748 /* Parse the arguments. */
13749 arguments = cp_parser_enclosed_template_argument_list (parser);
13750 if (!cp_parser_parse_definitely (parser))
13752 /* If we couldn't parse an argument list, then we revert our changes
13753 and return simply an error. Maybe this is not a template-id
13754 after all. */
13755 next_token_2->type = CPP_COLON;
13756 cp_parser_error (parser, "expected %<<%>");
13757 pop_deferring_access_checks ();
13758 return error_mark_node;
13760 /* Otherwise, emit an error about the invalid digraph, but continue
13761 parsing because we got our argument list. */
13762 if (permerror (next_token->location,
13763 "%<<::%> cannot begin a template-argument list"))
13765 static bool hint = false;
13766 inform (next_token->location,
13767 "%<<:%> is an alternate spelling for %<[%>."
13768 " Insert whitespace between %<<%> and %<::%>");
13769 if (!hint && !flag_permissive)
13771 inform (next_token->location, "(if you use %<-fpermissive%> "
13772 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13773 "accept your code)");
13774 hint = true;
13778 else
13780 /* Look for the `<' that starts the template-argument-list. */
13781 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13783 pop_deferring_access_checks ();
13784 return error_mark_node;
13786 /* Parse the arguments. */
13787 arguments = cp_parser_enclosed_template_argument_list (parser);
13790 /* Build a representation of the specialization. */
13791 if (identifier_p (templ))
13792 template_id = build_min_nt_loc (next_token->location,
13793 TEMPLATE_ID_EXPR,
13794 templ, arguments);
13795 else if (DECL_TYPE_TEMPLATE_P (templ)
13796 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13798 bool entering_scope;
13799 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13800 template (rather than some instantiation thereof) only if
13801 is not nested within some other construct. For example, in
13802 "template <typename T> void f(T) { A<T>::", A<T> is just an
13803 instantiation of A. */
13804 entering_scope = (template_parm_scope_p ()
13805 && cp_lexer_next_token_is (parser->lexer,
13806 CPP_SCOPE));
13807 template_id
13808 = finish_template_type (templ, arguments, entering_scope);
13810 else
13812 /* If it's not a class-template or a template-template, it should be
13813 a function-template. */
13814 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13815 || TREE_CODE (templ) == OVERLOAD
13816 || BASELINK_P (templ)));
13818 // If the template + args designate a concept, then return
13819 // something else.
13820 if (tree id = cp_maybe_partial_concept_id (parser, templ, arguments))
13821 return id;
13823 template_id = lookup_template_function (templ, arguments);
13826 /* If parsing tentatively, replace the sequence of tokens that makes
13827 up the template-id with a CPP_TEMPLATE_ID token. That way,
13828 should we re-parse the token stream, we will not have to repeat
13829 the effort required to do the parse, nor will we issue duplicate
13830 error messages about problems during instantiation of the
13831 template. */
13832 if (start_of_id
13833 /* Don't do this if we had a parse error in a declarator; re-parsing
13834 might succeed if a name changes meaning (60361). */
13835 && !(cp_parser_error_occurred (parser)
13836 && cp_parser_parsing_tentatively (parser)
13837 && parser->in_declarator_p))
13839 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13841 /* Reset the contents of the START_OF_ID token. */
13842 token->type = CPP_TEMPLATE_ID;
13843 /* Retrieve any deferred checks. Do not pop this access checks yet
13844 so the memory will not be reclaimed during token replacing below. */
13845 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13846 token->u.tree_check_value->value = template_id;
13847 token->u.tree_check_value->checks = get_deferred_access_checks ();
13848 token->keyword = RID_MAX;
13850 /* Purge all subsequent tokens. */
13851 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13853 /* ??? Can we actually assume that, if template_id ==
13854 error_mark_node, we will have issued a diagnostic to the
13855 user, as opposed to simply marking the tentative parse as
13856 failed? */
13857 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13858 error_at (token->location, "parse error in template argument list");
13861 pop_to_parent_deferring_access_checks ();
13862 return template_id;
13865 /* Parse a template-name.
13867 template-name:
13868 identifier
13870 The standard should actually say:
13872 template-name:
13873 identifier
13874 operator-function-id
13876 A defect report has been filed about this issue.
13878 A conversion-function-id cannot be a template name because they cannot
13879 be part of a template-id. In fact, looking at this code:
13881 a.operator K<int>()
13883 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13884 It is impossible to call a templated conversion-function-id with an
13885 explicit argument list, since the only allowed template parameter is
13886 the type to which it is converting.
13888 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13889 `template' keyword, in a construction like:
13891 T::template f<3>()
13893 In that case `f' is taken to be a template-name, even though there
13894 is no way of knowing for sure.
13896 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13897 name refers to a set of overloaded functions, at least one of which
13898 is a template, or an IDENTIFIER_NODE with the name of the template,
13899 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13900 names are looked up inside uninstantiated templates. */
13902 static tree
13903 cp_parser_template_name (cp_parser* parser,
13904 bool template_keyword_p,
13905 bool check_dependency_p,
13906 bool is_declaration,
13907 enum tag_types tag_type,
13908 bool *is_identifier)
13910 tree identifier;
13911 tree decl;
13912 tree fns;
13913 cp_token *token = cp_lexer_peek_token (parser->lexer);
13915 /* If the next token is `operator', then we have either an
13916 operator-function-id or a conversion-function-id. */
13917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13919 /* We don't know whether we're looking at an
13920 operator-function-id or a conversion-function-id. */
13921 cp_parser_parse_tentatively (parser);
13922 /* Try an operator-function-id. */
13923 identifier = cp_parser_operator_function_id (parser);
13924 /* If that didn't work, try a conversion-function-id. */
13925 if (!cp_parser_parse_definitely (parser))
13927 cp_parser_error (parser, "expected template-name");
13928 return error_mark_node;
13931 /* Look for the identifier. */
13932 else
13933 identifier = cp_parser_identifier (parser);
13935 /* If we didn't find an identifier, we don't have a template-id. */
13936 if (identifier == error_mark_node)
13937 return error_mark_node;
13939 /* If the name immediately followed the `template' keyword, then it
13940 is a template-name. However, if the next token is not `<', then
13941 we do not treat it as a template-name, since it is not being used
13942 as part of a template-id. This enables us to handle constructs
13943 like:
13945 template <typename T> struct S { S(); };
13946 template <typename T> S<T>::S();
13948 correctly. We would treat `S' as a template -- if it were `S<T>'
13949 -- but we do not if there is no `<'. */
13951 if (processing_template_decl
13952 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13954 /* In a declaration, in a dependent context, we pretend that the
13955 "template" keyword was present in order to improve error
13956 recovery. For example, given:
13958 template <typename T> void f(T::X<int>);
13960 we want to treat "X<int>" as a template-id. */
13961 if (is_declaration
13962 && !template_keyword_p
13963 && parser->scope && TYPE_P (parser->scope)
13964 && check_dependency_p
13965 && dependent_scope_p (parser->scope)
13966 /* Do not do this for dtors (or ctors), since they never
13967 need the template keyword before their name. */
13968 && !constructor_name_p (identifier, parser->scope))
13970 cp_token_position start = 0;
13972 /* Explain what went wrong. */
13973 error_at (token->location, "non-template %qD used as template",
13974 identifier);
13975 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13976 parser->scope, identifier);
13977 /* If parsing tentatively, find the location of the "<" token. */
13978 if (cp_parser_simulate_error (parser))
13979 start = cp_lexer_token_position (parser->lexer, true);
13980 /* Parse the template arguments so that we can issue error
13981 messages about them. */
13982 cp_lexer_consume_token (parser->lexer);
13983 cp_parser_enclosed_template_argument_list (parser);
13984 /* Skip tokens until we find a good place from which to
13985 continue parsing. */
13986 cp_parser_skip_to_closing_parenthesis (parser,
13987 /*recovering=*/true,
13988 /*or_comma=*/true,
13989 /*consume_paren=*/false);
13990 /* If parsing tentatively, permanently remove the
13991 template argument list. That will prevent duplicate
13992 error messages from being issued about the missing
13993 "template" keyword. */
13994 if (start)
13995 cp_lexer_purge_tokens_after (parser->lexer, start);
13996 if (is_identifier)
13997 *is_identifier = true;
13998 return identifier;
14001 /* If the "template" keyword is present, then there is generally
14002 no point in doing name-lookup, so we just return IDENTIFIER.
14003 But, if the qualifying scope is non-dependent then we can
14004 (and must) do name-lookup normally. */
14005 if (template_keyword_p
14006 && (!parser->scope
14007 || (TYPE_P (parser->scope)
14008 && dependent_type_p (parser->scope))))
14009 return identifier;
14012 /* Look up the name. */
14013 decl = cp_parser_lookup_name (parser, identifier,
14014 tag_type,
14015 /*is_template=*/true,
14016 /*is_namespace=*/false,
14017 check_dependency_p,
14018 /*ambiguous_decls=*/NULL,
14019 token->location);
14021 /* If DECL is a template, then the name was a template-name. */
14022 if (TREE_CODE (decl) == TEMPLATE_DECL)
14024 else
14026 tree fn = NULL_TREE;
14028 /* The standard does not explicitly indicate whether a name that
14029 names a set of overloaded declarations, some of which are
14030 templates, is a template-name. However, such a name should
14031 be a template-name; otherwise, there is no way to form a
14032 template-id for the overloaded templates. */
14033 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14034 if (TREE_CODE (fns) == OVERLOAD)
14035 for (fn = fns; fn; fn = OVL_NEXT (fn))
14036 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14037 break;
14039 if (!fn)
14041 /* The name does not name a template. */
14042 cp_parser_error (parser, "expected template-name");
14043 return error_mark_node;
14047 /* If DECL is dependent, and refers to a function, then just return
14048 its name; we will look it up again during template instantiation. */
14049 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14051 tree scope = ovl_scope (decl);
14052 if (TYPE_P (scope) && dependent_type_p (scope))
14053 return identifier;
14056 return decl;
14059 /* Parse a template-argument-list.
14061 template-argument-list:
14062 template-argument ... [opt]
14063 template-argument-list , template-argument ... [opt]
14065 Returns a TREE_VEC containing the arguments. */
14067 static tree
14068 cp_parser_template_argument_list (cp_parser* parser)
14070 tree fixed_args[10];
14071 unsigned n_args = 0;
14072 unsigned alloced = 10;
14073 tree *arg_ary = fixed_args;
14074 tree vec;
14075 bool saved_in_template_argument_list_p;
14076 bool saved_ice_p;
14077 bool saved_non_ice_p;
14079 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14080 parser->in_template_argument_list_p = true;
14081 /* Even if the template-id appears in an integral
14082 constant-expression, the contents of the argument list do
14083 not. */
14084 saved_ice_p = parser->integral_constant_expression_p;
14085 parser->integral_constant_expression_p = false;
14086 saved_non_ice_p = parser->non_integral_constant_expression_p;
14087 parser->non_integral_constant_expression_p = false;
14089 /* Parse the arguments. */
14092 tree argument;
14094 if (n_args)
14095 /* Consume the comma. */
14096 cp_lexer_consume_token (parser->lexer);
14098 /* Parse the template-argument. */
14099 argument = cp_parser_template_argument (parser);
14101 /* If the next token is an ellipsis, we're expanding a template
14102 argument pack. */
14103 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14105 if (argument == error_mark_node)
14107 cp_token *token = cp_lexer_peek_token (parser->lexer);
14108 error_at (token->location,
14109 "expected parameter pack before %<...%>");
14111 /* Consume the `...' token. */
14112 cp_lexer_consume_token (parser->lexer);
14114 /* Make the argument into a TYPE_PACK_EXPANSION or
14115 EXPR_PACK_EXPANSION. */
14116 argument = make_pack_expansion (argument);
14119 if (n_args == alloced)
14121 alloced *= 2;
14123 if (arg_ary == fixed_args)
14125 arg_ary = XNEWVEC (tree, alloced);
14126 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14128 else
14129 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14131 arg_ary[n_args++] = argument;
14133 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14135 vec = make_tree_vec (n_args);
14137 while (n_args--)
14138 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14140 if (arg_ary != fixed_args)
14141 free (arg_ary);
14142 parser->non_integral_constant_expression_p = saved_non_ice_p;
14143 parser->integral_constant_expression_p = saved_ice_p;
14144 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14145 #ifdef ENABLE_CHECKING
14146 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14147 #endif
14148 return vec;
14151 /* Parse a template-argument.
14153 template-argument:
14154 assignment-expression
14155 type-id
14156 id-expression
14158 The representation is that of an assignment-expression, type-id, or
14159 id-expression -- except that the qualified id-expression is
14160 evaluated, so that the value returned is either a DECL or an
14161 OVERLOAD.
14163 Although the standard says "assignment-expression", it forbids
14164 throw-expressions or assignments in the template argument.
14165 Therefore, we use "conditional-expression" instead. */
14167 static tree
14168 cp_parser_template_argument (cp_parser* parser)
14170 tree argument;
14171 bool template_p;
14172 bool address_p;
14173 bool maybe_type_id = false;
14174 cp_token *token = NULL, *argument_start_token = NULL;
14175 location_t loc = 0;
14176 cp_id_kind idk;
14178 /* There's really no way to know what we're looking at, so we just
14179 try each alternative in order.
14181 [temp.arg]
14183 In a template-argument, an ambiguity between a type-id and an
14184 expression is resolved to a type-id, regardless of the form of
14185 the corresponding template-parameter.
14187 Therefore, we try a type-id first. */
14188 cp_parser_parse_tentatively (parser);
14189 argument = cp_parser_template_type_arg (parser);
14190 /* If there was no error parsing the type-id but the next token is a
14191 '>>', our behavior depends on which dialect of C++ we're
14192 parsing. In C++98, we probably found a typo for '> >'. But there
14193 are type-id which are also valid expressions. For instance:
14195 struct X { int operator >> (int); };
14196 template <int V> struct Foo {};
14197 Foo<X () >> 5> r;
14199 Here 'X()' is a valid type-id of a function type, but the user just
14200 wanted to write the expression "X() >> 5". Thus, we remember that we
14201 found a valid type-id, but we still try to parse the argument as an
14202 expression to see what happens.
14204 In C++0x, the '>>' will be considered two separate '>'
14205 tokens. */
14206 if (!cp_parser_error_occurred (parser)
14207 && cxx_dialect == cxx98
14208 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14210 maybe_type_id = true;
14211 cp_parser_abort_tentative_parse (parser);
14213 else
14215 /* If the next token isn't a `,' or a `>', then this argument wasn't
14216 really finished. This means that the argument is not a valid
14217 type-id. */
14218 if (!cp_parser_next_token_ends_template_argument_p (parser))
14219 cp_parser_error (parser, "expected template-argument");
14220 /* If that worked, we're done. */
14221 if (cp_parser_parse_definitely (parser))
14222 return argument;
14224 /* We're still not sure what the argument will be. */
14225 cp_parser_parse_tentatively (parser);
14226 /* Try a template. */
14227 argument_start_token = cp_lexer_peek_token (parser->lexer);
14228 argument = cp_parser_id_expression (parser,
14229 /*template_keyword_p=*/false,
14230 /*check_dependency_p=*/true,
14231 &template_p,
14232 /*declarator_p=*/false,
14233 /*optional_p=*/false);
14234 /* If the next token isn't a `,' or a `>', then this argument wasn't
14235 really finished. */
14236 if (!cp_parser_next_token_ends_template_argument_p (parser))
14237 cp_parser_error (parser, "expected template-argument");
14238 if (!cp_parser_error_occurred (parser))
14240 /* Figure out what is being referred to. If the id-expression
14241 was for a class template specialization, then we will have a
14242 TYPE_DECL at this point. There is no need to do name lookup
14243 at this point in that case. */
14244 if (TREE_CODE (argument) != TYPE_DECL)
14245 argument = cp_parser_lookup_name (parser, argument,
14246 none_type,
14247 /*is_template=*/template_p,
14248 /*is_namespace=*/false,
14249 /*check_dependency=*/true,
14250 /*ambiguous_decls=*/NULL,
14251 argument_start_token->location);
14252 if (TREE_CODE (argument) != TEMPLATE_DECL
14253 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14254 cp_parser_error (parser, "expected template-name");
14256 if (cp_parser_parse_definitely (parser))
14257 return argument;
14258 /* It must be a non-type argument. There permitted cases are given
14259 in [temp.arg.nontype]:
14261 -- an integral constant-expression of integral or enumeration
14262 type; or
14264 -- the name of a non-type template-parameter; or
14266 -- the name of an object or function with external linkage...
14268 -- the address of an object or function with external linkage...
14270 -- a pointer to member... */
14271 /* Look for a non-type template parameter. */
14272 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14274 cp_parser_parse_tentatively (parser);
14275 argument = cp_parser_primary_expression (parser,
14276 /*address_p=*/false,
14277 /*cast_p=*/false,
14278 /*template_arg_p=*/true,
14279 &idk);
14280 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14281 || !cp_parser_next_token_ends_template_argument_p (parser))
14282 cp_parser_simulate_error (parser);
14283 if (cp_parser_parse_definitely (parser))
14284 return argument;
14287 /* If the next token is "&", the argument must be the address of an
14288 object or function with external linkage. */
14289 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14290 if (address_p)
14292 loc = cp_lexer_peek_token (parser->lexer)->location;
14293 cp_lexer_consume_token (parser->lexer);
14295 /* See if we might have an id-expression. */
14296 token = cp_lexer_peek_token (parser->lexer);
14297 if (token->type == CPP_NAME
14298 || token->keyword == RID_OPERATOR
14299 || token->type == CPP_SCOPE
14300 || token->type == CPP_TEMPLATE_ID
14301 || token->type == CPP_NESTED_NAME_SPECIFIER)
14303 cp_parser_parse_tentatively (parser);
14304 argument = cp_parser_primary_expression (parser,
14305 address_p,
14306 /*cast_p=*/false,
14307 /*template_arg_p=*/true,
14308 &idk);
14309 if (cp_parser_error_occurred (parser)
14310 || !cp_parser_next_token_ends_template_argument_p (parser))
14311 cp_parser_abort_tentative_parse (parser);
14312 else
14314 tree probe;
14316 if (INDIRECT_REF_P (argument))
14318 /* Strip the dereference temporarily. */
14319 gcc_assert (REFERENCE_REF_P (argument));
14320 argument = TREE_OPERAND (argument, 0);
14323 /* If we're in a template, we represent a qualified-id referring
14324 to a static data member as a SCOPE_REF even if the scope isn't
14325 dependent so that we can check access control later. */
14326 probe = argument;
14327 if (TREE_CODE (probe) == SCOPE_REF)
14328 probe = TREE_OPERAND (probe, 1);
14329 if (VAR_P (probe))
14331 /* A variable without external linkage might still be a
14332 valid constant-expression, so no error is issued here
14333 if the external-linkage check fails. */
14334 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14335 cp_parser_simulate_error (parser);
14337 else if (is_overloaded_fn (argument))
14338 /* All overloaded functions are allowed; if the external
14339 linkage test does not pass, an error will be issued
14340 later. */
14342 else if (address_p
14343 && (TREE_CODE (argument) == OFFSET_REF
14344 || TREE_CODE (argument) == SCOPE_REF))
14345 /* A pointer-to-member. */
14347 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14349 else
14350 cp_parser_simulate_error (parser);
14352 if (cp_parser_parse_definitely (parser))
14354 if (address_p)
14355 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14356 tf_warning_or_error);
14357 else
14358 argument = convert_from_reference (argument);
14359 return argument;
14363 /* If the argument started with "&", there are no other valid
14364 alternatives at this point. */
14365 if (address_p)
14367 cp_parser_error (parser, "invalid non-type template argument");
14368 return error_mark_node;
14371 /* If the argument wasn't successfully parsed as a type-id followed
14372 by '>>', the argument can only be a constant expression now.
14373 Otherwise, we try parsing the constant-expression tentatively,
14374 because the argument could really be a type-id. */
14375 if (maybe_type_id)
14376 cp_parser_parse_tentatively (parser);
14377 argument = cp_parser_constant_expression (parser,
14378 /*allow_non_constant_p=*/false,
14379 /*non_constant_p=*/NULL);
14380 if (!maybe_type_id)
14381 return argument;
14382 if (!cp_parser_next_token_ends_template_argument_p (parser))
14383 cp_parser_error (parser, "expected template-argument");
14384 if (cp_parser_parse_definitely (parser))
14385 return argument;
14386 /* We did our best to parse the argument as a non type-id, but that
14387 was the only alternative that matched (albeit with a '>' after
14388 it). We can assume it's just a typo from the user, and a
14389 diagnostic will then be issued. */
14390 return cp_parser_template_type_arg (parser);
14393 /* Parse an explicit-instantiation.
14395 explicit-instantiation:
14396 template declaration
14398 Although the standard says `declaration', what it really means is:
14400 explicit-instantiation:
14401 template decl-specifier-seq [opt] declarator [opt] ;
14403 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14404 supposed to be allowed. A defect report has been filed about this
14405 issue.
14407 GNU Extension:
14409 explicit-instantiation:
14410 storage-class-specifier template
14411 decl-specifier-seq [opt] declarator [opt] ;
14412 function-specifier template
14413 decl-specifier-seq [opt] declarator [opt] ; */
14415 static void
14416 cp_parser_explicit_instantiation (cp_parser* parser)
14418 int declares_class_or_enum;
14419 cp_decl_specifier_seq decl_specifiers;
14420 tree extension_specifier = NULL_TREE;
14422 timevar_push (TV_TEMPLATE_INST);
14424 /* Look for an (optional) storage-class-specifier or
14425 function-specifier. */
14426 if (cp_parser_allow_gnu_extensions_p (parser))
14428 extension_specifier
14429 = cp_parser_storage_class_specifier_opt (parser);
14430 if (!extension_specifier)
14431 extension_specifier
14432 = cp_parser_function_specifier_opt (parser,
14433 /*decl_specs=*/NULL);
14436 /* Look for the `template' keyword. */
14437 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14438 /* Let the front end know that we are processing an explicit
14439 instantiation. */
14440 begin_explicit_instantiation ();
14441 /* [temp.explicit] says that we are supposed to ignore access
14442 control while processing explicit instantiation directives. */
14443 push_deferring_access_checks (dk_no_check);
14444 /* Parse a decl-specifier-seq. */
14445 cp_parser_decl_specifier_seq (parser,
14446 CP_PARSER_FLAGS_OPTIONAL,
14447 &decl_specifiers,
14448 &declares_class_or_enum);
14449 /* If there was exactly one decl-specifier, and it declared a class,
14450 and there's no declarator, then we have an explicit type
14451 instantiation. */
14452 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14454 tree type;
14456 type = check_tag_decl (&decl_specifiers,
14457 /*explicit_type_instantiation_p=*/true);
14458 /* Turn access control back on for names used during
14459 template instantiation. */
14460 pop_deferring_access_checks ();
14461 if (type)
14462 do_type_instantiation (type, extension_specifier,
14463 /*complain=*/tf_error);
14465 else
14467 cp_declarator *declarator;
14468 tree decl;
14470 /* Parse the declarator. */
14471 declarator
14472 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14473 /*ctor_dtor_or_conv_p=*/NULL,
14474 /*parenthesized_p=*/NULL,
14475 /*member_p=*/false,
14476 /*friend_p=*/false);
14477 if (declares_class_or_enum & 2)
14478 cp_parser_check_for_definition_in_return_type (declarator,
14479 decl_specifiers.type,
14480 decl_specifiers.locations[ds_type_spec]);
14481 if (declarator != cp_error_declarator)
14483 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14484 permerror (decl_specifiers.locations[ds_inline],
14485 "explicit instantiation shall not use"
14486 " %<inline%> specifier");
14487 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14488 permerror (decl_specifiers.locations[ds_constexpr],
14489 "explicit instantiation shall not use"
14490 " %<constexpr%> specifier");
14492 decl = grokdeclarator (declarator, &decl_specifiers,
14493 NORMAL, 0, &decl_specifiers.attributes);
14494 /* Turn access control back on for names used during
14495 template instantiation. */
14496 pop_deferring_access_checks ();
14497 /* Do the explicit instantiation. */
14498 do_decl_instantiation (decl, extension_specifier);
14500 else
14502 pop_deferring_access_checks ();
14503 /* Skip the body of the explicit instantiation. */
14504 cp_parser_skip_to_end_of_statement (parser);
14507 /* We're done with the instantiation. */
14508 end_explicit_instantiation ();
14510 cp_parser_consume_semicolon_at_end_of_statement (parser);
14512 timevar_pop (TV_TEMPLATE_INST);
14515 /* Parse an explicit-specialization.
14517 explicit-specialization:
14518 template < > declaration
14520 Although the standard says `declaration', what it really means is:
14522 explicit-specialization:
14523 template <> decl-specifier [opt] init-declarator [opt] ;
14524 template <> function-definition
14525 template <> explicit-specialization
14526 template <> template-declaration */
14528 static void
14529 cp_parser_explicit_specialization (cp_parser* parser)
14531 bool need_lang_pop;
14532 cp_token *token = cp_lexer_peek_token (parser->lexer);
14534 /* Look for the `template' keyword. */
14535 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14536 /* Look for the `<'. */
14537 cp_parser_require (parser, CPP_LESS, RT_LESS);
14538 /* Look for the `>'. */
14539 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14540 /* We have processed another parameter list. */
14541 ++parser->num_template_parameter_lists;
14542 /* [temp]
14544 A template ... explicit specialization ... shall not have C
14545 linkage. */
14546 if (current_lang_name == lang_name_c)
14548 error_at (token->location, "template specialization with C linkage");
14549 /* Give it C++ linkage to avoid confusing other parts of the
14550 front end. */
14551 push_lang_context (lang_name_cplusplus);
14552 need_lang_pop = true;
14554 else
14555 need_lang_pop = false;
14556 /* Let the front end know that we are beginning a specialization. */
14557 if (!begin_specialization ())
14559 end_specialization ();
14560 return;
14563 /* If the next keyword is `template', we need to figure out whether
14564 or not we're looking a template-declaration. */
14565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14567 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14568 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14569 cp_parser_template_declaration_after_export (parser,
14570 /*member_p=*/false);
14571 else
14572 cp_parser_explicit_specialization (parser);
14574 else
14575 /* Parse the dependent declaration. */
14576 cp_parser_single_declaration (parser,
14577 /*checks=*/NULL,
14578 /*member_p=*/false,
14579 /*explicit_specialization_p=*/true,
14580 /*friend_p=*/NULL);
14581 /* We're done with the specialization. */
14582 end_specialization ();
14583 /* For the erroneous case of a template with C linkage, we pushed an
14584 implicit C++ linkage scope; exit that scope now. */
14585 if (need_lang_pop)
14586 pop_lang_context ();
14587 /* We're done with this parameter list. */
14588 --parser->num_template_parameter_lists;
14591 /* Parse a type-specifier.
14593 type-specifier:
14594 simple-type-specifier
14595 class-specifier
14596 enum-specifier
14597 elaborated-type-specifier
14598 cv-qualifier
14600 GNU Extension:
14602 type-specifier:
14603 __complex__
14605 Returns a representation of the type-specifier. For a
14606 class-specifier, enum-specifier, or elaborated-type-specifier, a
14607 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14609 The parser flags FLAGS is used to control type-specifier parsing.
14611 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14612 in a decl-specifier-seq.
14614 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14615 class-specifier, enum-specifier, or elaborated-type-specifier, then
14616 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14617 if a type is declared; 2 if it is defined. Otherwise, it is set to
14618 zero.
14620 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14621 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14622 is set to FALSE. */
14624 static tree
14625 cp_parser_type_specifier (cp_parser* parser,
14626 cp_parser_flags flags,
14627 cp_decl_specifier_seq *decl_specs,
14628 bool is_declaration,
14629 int* declares_class_or_enum,
14630 bool* is_cv_qualifier)
14632 tree type_spec = NULL_TREE;
14633 cp_token *token;
14634 enum rid keyword;
14635 cp_decl_spec ds = ds_last;
14637 /* Assume this type-specifier does not declare a new type. */
14638 if (declares_class_or_enum)
14639 *declares_class_or_enum = 0;
14640 /* And that it does not specify a cv-qualifier. */
14641 if (is_cv_qualifier)
14642 *is_cv_qualifier = false;
14643 /* Peek at the next token. */
14644 token = cp_lexer_peek_token (parser->lexer);
14646 /* If we're looking at a keyword, we can use that to guide the
14647 production we choose. */
14648 keyword = token->keyword;
14649 switch (keyword)
14651 case RID_ENUM:
14652 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14653 goto elaborated_type_specifier;
14655 /* Look for the enum-specifier. */
14656 type_spec = cp_parser_enum_specifier (parser);
14657 /* If that worked, we're done. */
14658 if (type_spec)
14660 if (declares_class_or_enum)
14661 *declares_class_or_enum = 2;
14662 if (decl_specs)
14663 cp_parser_set_decl_spec_type (decl_specs,
14664 type_spec,
14665 token,
14666 /*type_definition_p=*/true);
14667 return type_spec;
14669 else
14670 goto elaborated_type_specifier;
14672 /* Any of these indicate either a class-specifier, or an
14673 elaborated-type-specifier. */
14674 case RID_CLASS:
14675 case RID_STRUCT:
14676 case RID_UNION:
14677 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14678 goto elaborated_type_specifier;
14680 /* Parse tentatively so that we can back up if we don't find a
14681 class-specifier. */
14682 cp_parser_parse_tentatively (parser);
14683 /* Look for the class-specifier. */
14684 type_spec = cp_parser_class_specifier (parser);
14685 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14686 /* If that worked, we're done. */
14687 if (cp_parser_parse_definitely (parser))
14689 if (declares_class_or_enum)
14690 *declares_class_or_enum = 2;
14691 if (decl_specs)
14692 cp_parser_set_decl_spec_type (decl_specs,
14693 type_spec,
14694 token,
14695 /*type_definition_p=*/true);
14696 return type_spec;
14699 /* Fall through. */
14700 elaborated_type_specifier:
14701 /* We're declaring (not defining) a class or enum. */
14702 if (declares_class_or_enum)
14703 *declares_class_or_enum = 1;
14705 /* Fall through. */
14706 case RID_TYPENAME:
14707 /* Look for an elaborated-type-specifier. */
14708 type_spec
14709 = (cp_parser_elaborated_type_specifier
14710 (parser,
14711 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14712 is_declaration));
14713 if (decl_specs)
14714 cp_parser_set_decl_spec_type (decl_specs,
14715 type_spec,
14716 token,
14717 /*type_definition_p=*/false);
14718 return type_spec;
14720 case RID_CONST:
14721 ds = ds_const;
14722 if (is_cv_qualifier)
14723 *is_cv_qualifier = true;
14724 break;
14726 case RID_VOLATILE:
14727 ds = ds_volatile;
14728 if (is_cv_qualifier)
14729 *is_cv_qualifier = true;
14730 break;
14732 case RID_RESTRICT:
14733 ds = ds_restrict;
14734 if (is_cv_qualifier)
14735 *is_cv_qualifier = true;
14736 break;
14738 case RID_COMPLEX:
14739 /* The `__complex__' keyword is a GNU extension. */
14740 ds = ds_complex;
14741 break;
14743 default:
14744 break;
14747 /* Handle simple keywords. */
14748 if (ds != ds_last)
14750 if (decl_specs)
14752 set_and_check_decl_spec_loc (decl_specs, ds, token);
14753 decl_specs->any_specifiers_p = true;
14755 return cp_lexer_consume_token (parser->lexer)->u.value;
14758 /* If we do not already have a type-specifier, assume we are looking
14759 at a simple-type-specifier. */
14760 type_spec = cp_parser_simple_type_specifier (parser,
14761 decl_specs,
14762 flags);
14764 /* If we didn't find a type-specifier, and a type-specifier was not
14765 optional in this context, issue an error message. */
14766 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14768 cp_parser_error (parser, "expected type specifier");
14769 return error_mark_node;
14772 return type_spec;
14775 /* Parse a simple-type-specifier.
14777 simple-type-specifier:
14778 :: [opt] nested-name-specifier [opt] type-name
14779 :: [opt] nested-name-specifier template template-id
14780 char
14781 wchar_t
14782 bool
14783 short
14785 long
14786 signed
14787 unsigned
14788 float
14789 double
14790 void
14792 C++0x Extension:
14794 simple-type-specifier:
14795 auto
14796 decltype ( expression )
14797 char16_t
14798 char32_t
14799 __underlying_type ( type-id )
14801 GNU Extension:
14803 simple-type-specifier:
14804 __int128
14805 __typeof__ unary-expression
14806 __typeof__ ( type-id )
14807 __typeof__ ( type-id ) { initializer-list , [opt] }
14809 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14810 appropriately updated. */
14812 static tree
14813 cp_parser_simple_type_specifier (cp_parser* parser,
14814 cp_decl_specifier_seq *decl_specs,
14815 cp_parser_flags flags)
14817 tree type = NULL_TREE;
14818 cp_token *token;
14820 /* Peek at the next token. */
14821 token = cp_lexer_peek_token (parser->lexer);
14823 /* If we're looking at a keyword, things are easy. */
14824 switch (token->keyword)
14826 case RID_CHAR:
14827 if (decl_specs)
14828 decl_specs->explicit_char_p = true;
14829 type = char_type_node;
14830 break;
14831 case RID_CHAR16:
14832 type = char16_type_node;
14833 break;
14834 case RID_CHAR32:
14835 type = char32_type_node;
14836 break;
14837 case RID_WCHAR:
14838 type = wchar_type_node;
14839 break;
14840 case RID_BOOL:
14841 type = boolean_type_node;
14842 break;
14843 case RID_SHORT:
14844 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14845 type = short_integer_type_node;
14846 break;
14847 case RID_INT:
14848 if (decl_specs)
14849 decl_specs->explicit_int_p = true;
14850 type = integer_type_node;
14851 break;
14852 case RID_INT128:
14853 if (!int128_integer_type_node)
14854 break;
14855 if (decl_specs)
14856 decl_specs->explicit_int128_p = true;
14857 type = int128_integer_type_node;
14858 break;
14859 case RID_LONG:
14860 if (decl_specs)
14861 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14862 type = long_integer_type_node;
14863 break;
14864 case RID_SIGNED:
14865 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14866 type = integer_type_node;
14867 break;
14868 case RID_UNSIGNED:
14869 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14870 type = unsigned_type_node;
14871 break;
14872 case RID_FLOAT:
14873 type = float_type_node;
14874 break;
14875 case RID_DOUBLE:
14876 type = double_type_node;
14877 break;
14878 case RID_VOID:
14879 type = void_type_node;
14880 break;
14882 case RID_AUTO:
14883 maybe_warn_cpp0x (CPP0X_AUTO);
14884 if (parser->auto_is_implicit_function_template_parm_p)
14886 type = synthesize_implicit_template_parm (parser, NULL_TREE);
14888 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14890 if (cxx_dialect < cxx1y)
14891 pedwarn (location_of (type), 0,
14892 "use of %<auto%> in lambda parameter declaration "
14893 "only available with "
14894 "-std=c++1y or -std=gnu++1y");
14896 else if (cxx_dialect < cxx1y)
14897 pedwarn (location_of (type), 0,
14898 "use of %<auto%> in parameter declaration "
14899 "only available with "
14900 "-std=c++1y or -std=gnu++1y");
14901 else
14902 pedwarn (location_of (type), OPT_Wpedantic,
14903 "ISO C++ forbids use of %<auto%> in parameter "
14904 "declaration");
14906 else
14907 type = make_auto ();
14908 break;
14910 case RID_DECLTYPE:
14911 /* Since DR 743, decltype can either be a simple-type-specifier by
14912 itself or begin a nested-name-specifier. Parsing it will replace
14913 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14914 handling below decide what to do. */
14915 cp_parser_decltype (parser);
14916 cp_lexer_set_token_position (parser->lexer, token);
14917 break;
14919 case RID_TYPEOF:
14920 /* Consume the `typeof' token. */
14921 cp_lexer_consume_token (parser->lexer);
14922 /* Parse the operand to `typeof'. */
14923 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14924 /* If it is not already a TYPE, take its type. */
14925 if (!TYPE_P (type))
14926 type = finish_typeof (type);
14928 if (decl_specs)
14929 cp_parser_set_decl_spec_type (decl_specs, type,
14930 token,
14931 /*type_definition_p=*/false);
14933 return type;
14935 case RID_UNDERLYING_TYPE:
14936 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14937 if (decl_specs)
14938 cp_parser_set_decl_spec_type (decl_specs, type,
14939 token,
14940 /*type_definition_p=*/false);
14942 return type;
14944 case RID_BASES:
14945 case RID_DIRECT_BASES:
14946 type = cp_parser_trait_expr (parser, token->keyword);
14947 if (decl_specs)
14948 cp_parser_set_decl_spec_type (decl_specs, type,
14949 token,
14950 /*type_definition_p=*/false);
14951 return type;
14952 default:
14953 break;
14956 /* If token is an already-parsed decltype not followed by ::,
14957 it's a simple-type-specifier. */
14958 if (token->type == CPP_DECLTYPE
14959 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14961 type = token->u.value;
14962 if (decl_specs)
14963 cp_parser_set_decl_spec_type (decl_specs, type,
14964 token,
14965 /*type_definition_p=*/false);
14966 cp_lexer_consume_token (parser->lexer);
14967 return type;
14970 /* If the type-specifier was for a built-in type, we're done. */
14971 if (type)
14973 /* Record the type. */
14974 if (decl_specs
14975 && (token->keyword != RID_SIGNED
14976 && token->keyword != RID_UNSIGNED
14977 && token->keyword != RID_SHORT
14978 && token->keyword != RID_LONG))
14979 cp_parser_set_decl_spec_type (decl_specs,
14980 type,
14981 token,
14982 /*type_definition_p=*/false);
14983 if (decl_specs)
14984 decl_specs->any_specifiers_p = true;
14986 /* Consume the token. */
14987 cp_lexer_consume_token (parser->lexer);
14989 /* There is no valid C++ program where a non-template type is
14990 followed by a "<". That usually indicates that the user thought
14991 that the type was a template. */
14992 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14993 token->location);
14995 return TYPE_NAME (type);
14998 /* The type-specifier must be a user-defined type. */
14999 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15001 bool qualified_p;
15002 bool global_p;
15004 /* Don't gobble tokens or issue error messages if this is an
15005 optional type-specifier. */
15006 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15007 cp_parser_parse_tentatively (parser);
15009 /* Look for the optional `::' operator. */
15010 global_p
15011 = (cp_parser_global_scope_opt (parser,
15012 /*current_scope_valid_p=*/false)
15013 != NULL_TREE);
15014 /* Look for the nested-name specifier. */
15015 qualified_p
15016 = (cp_parser_nested_name_specifier_opt (parser,
15017 /*typename_keyword_p=*/false,
15018 /*check_dependency_p=*/true,
15019 /*type_p=*/false,
15020 /*is_declaration=*/false)
15021 != NULL_TREE);
15022 token = cp_lexer_peek_token (parser->lexer);
15023 /* If we have seen a nested-name-specifier, and the next token
15024 is `template', then we are using the template-id production. */
15025 if (parser->scope
15026 && cp_parser_optional_template_keyword (parser))
15028 /* Look for the template-id. */
15029 type = cp_parser_template_id (parser,
15030 /*template_keyword_p=*/true,
15031 /*check_dependency_p=*/true,
15032 none_type,
15033 /*is_declaration=*/false);
15034 /* If the template-id did not name a type, we are out of
15035 luck. */
15036 if (TREE_CODE (type) != TYPE_DECL)
15038 cp_parser_error (parser, "expected template-id for type");
15039 type = NULL_TREE;
15042 /* Otherwise, look for a type-name. */
15043 else
15044 type = cp_parser_type_name (parser);
15046 /* Keep track of all name-lookups performed in class scopes. */
15047 if (type
15048 && !global_p
15049 && !qualified_p
15050 && TREE_CODE (type) == TYPE_DECL
15051 && identifier_p (DECL_NAME (type)))
15052 maybe_note_name_used_in_class (DECL_NAME (type), type);
15053 /* If it didn't work out, we don't have a TYPE. */
15054 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15055 && !cp_parser_parse_definitely (parser))
15056 type = NULL_TREE;
15057 if (type && decl_specs)
15058 cp_parser_set_decl_spec_type (decl_specs, type,
15059 token,
15060 /*type_definition_p=*/false);
15063 /* If we didn't get a type-name, issue an error message. */
15064 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15066 cp_parser_error (parser, "expected type-name");
15067 return error_mark_node;
15070 if (type && type != error_mark_node)
15072 /* See if TYPE is an Objective-C type, and if so, parse and
15073 accept any protocol references following it. Do this before
15074 the cp_parser_check_for_invalid_template_id() call, because
15075 Objective-C types can be followed by '<...>' which would
15076 enclose protocol names rather than template arguments, and so
15077 everything is fine. */
15078 if (c_dialect_objc () && !parser->scope
15079 && (objc_is_id (type) || objc_is_class_name (type)))
15081 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15082 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15084 /* Clobber the "unqualified" type previously entered into
15085 DECL_SPECS with the new, improved protocol-qualified version. */
15086 if (decl_specs)
15087 decl_specs->type = qual_type;
15089 return qual_type;
15092 /* There is no valid C++ program where a non-template type is
15093 followed by a "<". That usually indicates that the user
15094 thought that the type was a template. */
15095 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15096 none_type,
15097 token->location);
15100 return type;
15103 /* Parse a type-name.
15105 type-name:
15106 class-name
15107 enum-name
15108 typedef-name
15109 simple-template-id [in c++0x]
15111 enum-name:
15112 identifier
15114 typedef-name:
15115 identifier
15117 Concepts:
15119 type-name:
15120 concept-name
15121 partial-concept-id
15123 concept-name:
15124 identifier
15126 Returns a TYPE_DECL for the type. */
15128 static tree
15129 cp_parser_type_name (cp_parser* parser)
15131 tree type_decl;
15133 /* We can't know yet whether it is a class-name or not. */
15134 cp_parser_parse_tentatively (parser);
15135 /* Try a class-name. */
15136 type_decl = cp_parser_class_name (parser,
15137 /*typename_keyword_p=*/false,
15138 /*template_keyword_p=*/false,
15139 none_type,
15140 /*check_dependency_p=*/true,
15141 /*class_head_p=*/false,
15142 /*is_declaration=*/false);
15144 /* If it's not a class-name, keep looking. */
15145 if (!cp_parser_parse_definitely (parser))
15147 if (cxx_dialect < cxx11)
15148 /* It must be a typedef-name or an enum-name. */
15149 return cp_parser_nonclass_name (parser);
15151 cp_parser_parse_tentatively (parser);
15152 /* It is either a simple-template-id representing an
15153 instantiation of an alias template... */
15154 type_decl = cp_parser_template_id (parser,
15155 /*template_keyword_p=*/false,
15156 /*check_dependency_p=*/true,
15157 none_type,
15158 /*is_declaration=*/false);
15160 /* Note that this must be an instantiation of an alias template
15161 because [temp.names]/6 says:
15163 A template-id that names an alias template specialization
15164 is a type-name.
15166 Whereas [temp.names]/7 says:
15168 A simple-template-id that names a class template
15169 specialization is a class-name. */
15170 if (type_decl != NULL_TREE
15171 && TREE_CODE (type_decl) == TYPE_DECL
15172 && TYPE_DECL_ALIAS_P (type_decl))
15173 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15174 else
15175 cp_parser_simulate_error (parser);
15177 if (!cp_parser_parse_definitely (parser))
15178 /* ... Or a typedef-name or an enum-name. */
15179 return cp_parser_nonclass_name (parser);
15182 return type_decl;
15185 /// Returns true if proto is a type parameter, but not a template template
15186 /// parameter.
15187 static bool
15188 cp_check_type_concept (tree fn, tree proto)
15190 if (TREE_CODE (proto) != TYPE_DECL)
15192 error ("invalid use of non-type concept %qD", fn);
15193 return false;
15195 return true;
15198 /// Returns true if the parser is in a context that allows the
15199 /// use of a constrained type specifier.
15200 static inline bool
15201 cp_parser_allows_constrained_type_specifier (cp_parser *parser)
15203 return flag_concepts
15204 && (processing_template_parmlist
15205 || parser->auto_is_implicit_function_template_parm_p
15206 || parser->in_result_type_constraint_p);
15210 // Check if DECL and ARGS can form a constrained-type-specifier. If ARGS
15211 // is non-null, we try to form a concept check of the form DECL<?, ARGS>
15212 // where ? is a placeholder for any kind of template argument. If ARGS
15213 // is NULL, then we try to form a concept check of the form DEC<?>.
15214 static tree
15215 cp_maybe_constrained_type_specifier (cp_parser *parser, tree decl, tree args)
15217 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
15219 // Don't do any heavy lifting if we know we're not in a context
15220 // where it could succeed.
15221 if (!cp_parser_allows_constrained_type_specifier (parser))
15222 return NULL_TREE;
15224 // Try to build a call expression that evaluates the concept. This
15225 // can fail if the overload set refers only to non-templates.
15226 tree placeholder = build_nt(PLACEHOLDER_EXPR);
15227 tree call = build_concept_check (decl, placeholder, args);
15228 if (call == error_mark_node)
15229 return NULL_TREE;
15231 // Deduce the checked constraint and the prototype parameter.
15232 tree fn;
15233 tree proto;
15234 if (!deduce_constrained_parameter (call, fn, proto))
15235 return NULL_TREE;
15237 // In template paramteer scope, this results in a constrained parameter.
15238 // Return a descriptor of that parm.
15239 if (processing_template_parmlist)
15240 return build_constrained_parameter (fn, proto, args);
15242 // In any other context, a concept must be a type concept.
15243 if (!cp_check_type_concept (fn, proto))
15244 return error_mark_node;
15246 // In a parameter-declaration-clause, constrained-type specifiers
15247 // result in invented template parameters.
15248 if (parser->auto_is_implicit_function_template_parm_p)
15250 tree x = build_constrained_parameter (fn, proto, args);
15251 tree r = synthesize_implicit_template_parm (parser, x);
15252 return r;
15255 // A concept-name appearing in a result-type constraint is a
15256 // constrained auto. Meaning that type deduction will be applied
15257 // and the constraint checked.
15259 // TODO: Actually bind the constraint to the auto.
15260 if (parser->in_result_type_constraint_p)
15261 return make_auto();
15263 return NULL_TREE;
15267 // If DECL refers to a concept, return a TYPE_DECL representing the result
15268 // of using the constrained type specifier in the current context.
15270 // DECL refers to a concept if
15271 // - it is an overload set containing a function concept taking a single
15272 // type argument, or
15273 // - it is a variable concept taking a single type argument
15275 // TODO: DECL could be a variable concept.
15276 static tree
15277 cp_maybe_concept_name (cp_parser* parser, tree decl)
15279 return cp_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
15282 // Check if DECL and ARGS forms a partial concept id. Let C be the name
15283 // of the overload set denoted by TMPL, Args the sequence of ARGS, and
15284 // ? denote an unspecified template argument. If the template-id C<?, Args>
15285 // is valid, this denotes a partial-concept-id to be acted on.
15287 // TODO: TMPL could be a variable concept.
15288 tree
15289 cp_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
15291 return cp_maybe_constrained_type_specifier (parser, decl, args);
15294 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
15295 or a concept-name.
15297 enum-name:
15298 identifier
15300 typedef-name:
15301 identifier
15303 concept-name:
15304 identifier
15306 Returns a TYPE_DECL for the type. */
15308 static tree
15309 cp_parser_nonclass_name (cp_parser* parser)
15311 tree type_decl;
15312 tree identifier;
15314 cp_token *token = cp_lexer_peek_token (parser->lexer);
15315 identifier = cp_parser_identifier (parser);
15316 if (identifier == error_mark_node)
15317 return error_mark_node;
15319 /* Look up the type-name. */
15320 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15321 type_decl = strip_using_decl (type_decl);
15323 // If we found an overload set, then it may refer to a concept-name.
15325 // TODO: The name could also refer to a variable template or an
15326 // introduction (if followed by '{').
15327 if (flag_concepts &&
15328 (TREE_CODE (type_decl) == OVERLOAD || BASELINK_P (type_decl)))
15330 // Determine whether the overload refers to a concept.
15331 if (tree decl = cp_maybe_concept_name (parser, type_decl))
15332 return decl;
15335 if (TREE_CODE (type_decl) == USING_DECL)
15337 if (!DECL_DEPENDENT_P (type_decl))
15338 type_decl = strip_using_decl (type_decl);
15339 else if (USING_DECL_TYPENAME_P (type_decl))
15341 /* We have found a type introduced by a using
15342 declaration at class scope that refers to a dependent
15343 type.
15345 using typename :: [opt] nested-name-specifier unqualified-id ;
15347 type_decl = make_typename_type (TREE_TYPE (type_decl),
15348 DECL_NAME (type_decl),
15349 typename_type, tf_error);
15350 if (type_decl != error_mark_node)
15351 type_decl = TYPE_NAME (type_decl);
15355 if (TREE_CODE (type_decl) != TYPE_DECL
15356 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15358 /* See if this is an Objective-C type. */
15359 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15360 tree type = objc_get_protocol_qualified_type (identifier, protos);
15361 if (type)
15362 type_decl = TYPE_NAME (type);
15365 /* Issue an error if we did not find a type-name. */
15366 if (TREE_CODE (type_decl) != TYPE_DECL
15367 /* In Objective-C, we have the complication that class names are
15368 normally type names and start declarations (eg, the
15369 "NSObject" in "NSObject *object;"), but can be used in an
15370 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15371 is an expression. So, a classname followed by a dot is not a
15372 valid type-name. */
15373 || (objc_is_class_name (TREE_TYPE (type_decl))
15374 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15376 if (!cp_parser_simulate_error (parser))
15377 cp_parser_name_lookup_error (parser, identifier, type_decl,
15378 NLE_TYPE, token->location);
15379 return error_mark_node;
15381 /* Remember that the name was used in the definition of the
15382 current class so that we can check later to see if the
15383 meaning would have been different after the class was
15384 entirely defined. */
15385 else if (type_decl != error_mark_node
15386 && !parser->scope)
15387 maybe_note_name_used_in_class (identifier, type_decl);
15389 return type_decl;
15392 /* Parse an elaborated-type-specifier. Note that the grammar given
15393 here incorporates the resolution to DR68.
15395 elaborated-type-specifier:
15396 class-key :: [opt] nested-name-specifier [opt] identifier
15397 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15398 enum-key :: [opt] nested-name-specifier [opt] identifier
15399 typename :: [opt] nested-name-specifier identifier
15400 typename :: [opt] nested-name-specifier template [opt]
15401 template-id
15403 GNU extension:
15405 elaborated-type-specifier:
15406 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15407 class-key attributes :: [opt] nested-name-specifier [opt]
15408 template [opt] template-id
15409 enum attributes :: [opt] nested-name-specifier [opt] identifier
15411 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15412 declared `friend'. If IS_DECLARATION is TRUE, then this
15413 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15414 something is being declared.
15416 Returns the TYPE specified. */
15418 static tree
15419 cp_parser_elaborated_type_specifier (cp_parser* parser,
15420 bool is_friend,
15421 bool is_declaration)
15423 enum tag_types tag_type;
15424 tree identifier;
15425 tree type = NULL_TREE;
15426 tree attributes = NULL_TREE;
15427 tree globalscope;
15428 cp_token *token = NULL;
15430 /* See if we're looking at the `enum' keyword. */
15431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15433 /* Consume the `enum' token. */
15434 cp_lexer_consume_token (parser->lexer);
15435 /* Remember that it's an enumeration type. */
15436 tag_type = enum_type;
15437 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15438 enums) is used here. */
15439 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15440 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15442 pedwarn (input_location, 0, "elaborated-type-specifier "
15443 "for a scoped enum must not use the %<%D%> keyword",
15444 cp_lexer_peek_token (parser->lexer)->u.value);
15445 /* Consume the `struct' or `class' and parse it anyway. */
15446 cp_lexer_consume_token (parser->lexer);
15448 /* Parse the attributes. */
15449 attributes = cp_parser_attributes_opt (parser);
15451 /* Or, it might be `typename'. */
15452 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15453 RID_TYPENAME))
15455 /* Consume the `typename' token. */
15456 cp_lexer_consume_token (parser->lexer);
15457 /* Remember that it's a `typename' type. */
15458 tag_type = typename_type;
15460 /* Otherwise it must be a class-key. */
15461 else
15463 tag_type = cp_parser_class_key (parser);
15464 if (tag_type == none_type)
15465 return error_mark_node;
15466 /* Parse the attributes. */
15467 attributes = cp_parser_attributes_opt (parser);
15470 /* Look for the `::' operator. */
15471 globalscope = cp_parser_global_scope_opt (parser,
15472 /*current_scope_valid_p=*/false);
15473 /* Look for the nested-name-specifier. */
15474 if (tag_type == typename_type && !globalscope)
15476 if (!cp_parser_nested_name_specifier (parser,
15477 /*typename_keyword_p=*/true,
15478 /*check_dependency_p=*/true,
15479 /*type_p=*/true,
15480 is_declaration))
15481 return error_mark_node;
15483 else
15484 /* Even though `typename' is not present, the proposed resolution
15485 to Core Issue 180 says that in `class A<T>::B', `B' should be
15486 considered a type-name, even if `A<T>' is dependent. */
15487 cp_parser_nested_name_specifier_opt (parser,
15488 /*typename_keyword_p=*/true,
15489 /*check_dependency_p=*/true,
15490 /*type_p=*/true,
15491 is_declaration);
15492 /* For everything but enumeration types, consider a template-id.
15493 For an enumeration type, consider only a plain identifier. */
15494 if (tag_type != enum_type)
15496 bool template_p = false;
15497 tree decl;
15499 /* Allow the `template' keyword. */
15500 template_p = cp_parser_optional_template_keyword (parser);
15501 /* If we didn't see `template', we don't know if there's a
15502 template-id or not. */
15503 if (!template_p)
15504 cp_parser_parse_tentatively (parser);
15505 /* Parse the template-id. */
15506 token = cp_lexer_peek_token (parser->lexer);
15507 decl = cp_parser_template_id (parser, template_p,
15508 /*check_dependency_p=*/true,
15509 tag_type,
15510 is_declaration);
15511 /* If we didn't find a template-id, look for an ordinary
15512 identifier. */
15513 if (!template_p && !cp_parser_parse_definitely (parser))
15515 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15516 in effect, then we must assume that, upon instantiation, the
15517 template will correspond to a class. */
15518 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15519 && tag_type == typename_type)
15520 type = make_typename_type (parser->scope, decl,
15521 typename_type,
15522 /*complain=*/tf_error);
15523 /* If the `typename' keyword is in effect and DECL is not a type
15524 decl, then type is non existent. */
15525 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15527 else if (TREE_CODE (decl) == TYPE_DECL)
15528 type = check_elaborated_type_specifier (tag_type, decl,
15529 /*allow_template_p=*/true);
15530 else if (decl == error_mark_node)
15531 type = error_mark_node;
15534 if (!type)
15536 token = cp_lexer_peek_token (parser->lexer);
15537 identifier = cp_parser_identifier (parser);
15539 if (identifier == error_mark_node)
15541 parser->scope = NULL_TREE;
15542 return error_mark_node;
15545 /* For a `typename', we needn't call xref_tag. */
15546 if (tag_type == typename_type
15547 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15548 return cp_parser_make_typename_type (parser, parser->scope,
15549 identifier,
15550 token->location);
15552 /* Template parameter lists apply only if we are not within a
15553 function parameter list. */
15554 bool template_parm_lists_apply
15555 = parser->num_template_parameter_lists;
15556 if (template_parm_lists_apply)
15557 for (cp_binding_level *s = current_binding_level;
15558 s && s->kind != sk_template_parms;
15559 s = s->level_chain)
15560 if (s->kind == sk_function_parms)
15561 template_parm_lists_apply = false;
15563 /* Look up a qualified name in the usual way. */
15564 if (parser->scope)
15566 tree decl;
15567 tree ambiguous_decls;
15569 decl = cp_parser_lookup_name (parser, identifier,
15570 tag_type,
15571 /*is_template=*/false,
15572 /*is_namespace=*/false,
15573 /*check_dependency=*/true,
15574 &ambiguous_decls,
15575 token->location);
15577 /* If the lookup was ambiguous, an error will already have been
15578 issued. */
15579 if (ambiguous_decls)
15580 return error_mark_node;
15582 /* If we are parsing friend declaration, DECL may be a
15583 TEMPLATE_DECL tree node here. However, we need to check
15584 whether this TEMPLATE_DECL results in valid code. Consider
15585 the following example:
15587 namespace N {
15588 template <class T> class C {};
15590 class X {
15591 template <class T> friend class N::C; // #1, valid code
15593 template <class T> class Y {
15594 friend class N::C; // #2, invalid code
15597 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15598 name lookup of `N::C'. We see that friend declaration must
15599 be template for the code to be valid. Note that
15600 processing_template_decl does not work here since it is
15601 always 1 for the above two cases. */
15603 decl = (cp_parser_maybe_treat_template_as_class
15604 (decl, /*tag_name_p=*/is_friend
15605 && template_parm_lists_apply));
15607 if (TREE_CODE (decl) != TYPE_DECL)
15609 cp_parser_diagnose_invalid_type_name (parser,
15610 parser->scope,
15611 identifier,
15612 token->location);
15613 return error_mark_node;
15616 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15618 bool allow_template = (template_parm_lists_apply
15619 || DECL_SELF_REFERENCE_P (decl));
15620 type = check_elaborated_type_specifier (tag_type, decl,
15621 allow_template);
15623 if (type == error_mark_node)
15624 return error_mark_node;
15627 /* Forward declarations of nested types, such as
15629 class C1::C2;
15630 class C1::C2::C3;
15632 are invalid unless all components preceding the final '::'
15633 are complete. If all enclosing types are complete, these
15634 declarations become merely pointless.
15636 Invalid forward declarations of nested types are errors
15637 caught elsewhere in parsing. Those that are pointless arrive
15638 here. */
15640 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15641 && !is_friend && !processing_explicit_instantiation)
15642 warning (0, "declaration %qD does not declare anything", decl);
15644 type = TREE_TYPE (decl);
15646 else
15648 /* An elaborated-type-specifier sometimes introduces a new type and
15649 sometimes names an existing type. Normally, the rule is that it
15650 introduces a new type only if there is not an existing type of
15651 the same name already in scope. For example, given:
15653 struct S {};
15654 void f() { struct S s; }
15656 the `struct S' in the body of `f' is the same `struct S' as in
15657 the global scope; the existing definition is used. However, if
15658 there were no global declaration, this would introduce a new
15659 local class named `S'.
15661 An exception to this rule applies to the following code:
15663 namespace N { struct S; }
15665 Here, the elaborated-type-specifier names a new type
15666 unconditionally; even if there is already an `S' in the
15667 containing scope this declaration names a new type.
15668 This exception only applies if the elaborated-type-specifier
15669 forms the complete declaration:
15671 [class.name]
15673 A declaration consisting solely of `class-key identifier ;' is
15674 either a redeclaration of the name in the current scope or a
15675 forward declaration of the identifier as a class name. It
15676 introduces the name into the current scope.
15678 We are in this situation precisely when the next token is a `;'.
15680 An exception to the exception is that a `friend' declaration does
15681 *not* name a new type; i.e., given:
15683 struct S { friend struct T; };
15685 `T' is not a new type in the scope of `S'.
15687 Also, `new struct S' or `sizeof (struct S)' never results in the
15688 definition of a new type; a new type can only be declared in a
15689 declaration context. */
15691 tag_scope ts;
15692 bool template_p;
15694 if (is_friend)
15695 /* Friends have special name lookup rules. */
15696 ts = ts_within_enclosing_non_class;
15697 else if (is_declaration
15698 && cp_lexer_next_token_is (parser->lexer,
15699 CPP_SEMICOLON))
15700 /* This is a `class-key identifier ;' */
15701 ts = ts_current;
15702 else
15703 ts = ts_global;
15705 template_p =
15706 (template_parm_lists_apply
15707 && (cp_parser_next_token_starts_class_definition_p (parser)
15708 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15709 /* An unqualified name was used to reference this type, so
15710 there were no qualifying templates. */
15711 if (template_parm_lists_apply
15712 && !cp_parser_check_template_parameters (parser,
15713 /*num_templates=*/0,
15714 token->location,
15715 /*declarator=*/NULL))
15716 return error_mark_node;
15717 type = xref_tag (tag_type, identifier, ts, template_p);
15721 if (type == error_mark_node)
15722 return error_mark_node;
15724 /* Allow attributes on forward declarations of classes. */
15725 if (attributes)
15727 if (TREE_CODE (type) == TYPENAME_TYPE)
15728 warning (OPT_Wattributes,
15729 "attributes ignored on uninstantiated type");
15730 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15731 && ! processing_explicit_instantiation)
15732 warning (OPT_Wattributes,
15733 "attributes ignored on template instantiation");
15734 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15735 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15736 else
15737 warning (OPT_Wattributes,
15738 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15741 if (tag_type != enum_type)
15743 /* Indicate whether this class was declared as a `class' or as a
15744 `struct'. */
15745 if (TREE_CODE (type) == RECORD_TYPE)
15746 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15747 cp_parser_check_class_key (tag_type, type);
15750 /* A "<" cannot follow an elaborated type specifier. If that
15751 happens, the user was probably trying to form a template-id. */
15752 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15753 token->location);
15755 return type;
15758 /* Parse an enum-specifier.
15760 enum-specifier:
15761 enum-head { enumerator-list [opt] }
15762 enum-head { enumerator-list , } [C++0x]
15764 enum-head:
15765 enum-key identifier [opt] enum-base [opt]
15766 enum-key nested-name-specifier identifier enum-base [opt]
15768 enum-key:
15769 enum
15770 enum class [C++0x]
15771 enum struct [C++0x]
15773 enum-base: [C++0x]
15774 : type-specifier-seq
15776 opaque-enum-specifier:
15777 enum-key identifier enum-base [opt] ;
15779 GNU Extensions:
15780 enum-key attributes[opt] identifier [opt] enum-base [opt]
15781 { enumerator-list [opt] }attributes[opt]
15782 enum-key attributes[opt] identifier [opt] enum-base [opt]
15783 { enumerator-list, }attributes[opt] [C++0x]
15785 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15786 if the token stream isn't an enum-specifier after all. */
15788 static tree
15789 cp_parser_enum_specifier (cp_parser* parser)
15791 tree identifier;
15792 tree type = NULL_TREE;
15793 tree prev_scope;
15794 tree nested_name_specifier = NULL_TREE;
15795 tree attributes;
15796 bool scoped_enum_p = false;
15797 bool has_underlying_type = false;
15798 bool nested_being_defined = false;
15799 bool new_value_list = false;
15800 bool is_new_type = false;
15801 bool is_anonymous = false;
15802 tree underlying_type = NULL_TREE;
15803 cp_token *type_start_token = NULL;
15804 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15806 parser->colon_corrects_to_scope_p = false;
15808 /* Parse tentatively so that we can back up if we don't find a
15809 enum-specifier. */
15810 cp_parser_parse_tentatively (parser);
15812 /* Caller guarantees that the current token is 'enum', an identifier
15813 possibly follows, and the token after that is an opening brace.
15814 If we don't have an identifier, fabricate an anonymous name for
15815 the enumeration being defined. */
15816 cp_lexer_consume_token (parser->lexer);
15818 /* Parse the "class" or "struct", which indicates a scoped
15819 enumeration type in C++0x. */
15820 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15821 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15823 if (cxx_dialect < cxx11)
15824 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15826 /* Consume the `struct' or `class' token. */
15827 cp_lexer_consume_token (parser->lexer);
15829 scoped_enum_p = true;
15832 attributes = cp_parser_attributes_opt (parser);
15834 /* Clear the qualification. */
15835 parser->scope = NULL_TREE;
15836 parser->qualifying_scope = NULL_TREE;
15837 parser->object_scope = NULL_TREE;
15839 /* Figure out in what scope the declaration is being placed. */
15840 prev_scope = current_scope ();
15842 type_start_token = cp_lexer_peek_token (parser->lexer);
15844 push_deferring_access_checks (dk_no_check);
15845 nested_name_specifier
15846 = cp_parser_nested_name_specifier_opt (parser,
15847 /*typename_keyword_p=*/true,
15848 /*check_dependency_p=*/false,
15849 /*type_p=*/false,
15850 /*is_declaration=*/false);
15852 if (nested_name_specifier)
15854 tree name;
15856 identifier = cp_parser_identifier (parser);
15857 name = cp_parser_lookup_name (parser, identifier,
15858 enum_type,
15859 /*is_template=*/false,
15860 /*is_namespace=*/false,
15861 /*check_dependency=*/true,
15862 /*ambiguous_decls=*/NULL,
15863 input_location);
15864 if (name && name != error_mark_node)
15866 type = TREE_TYPE (name);
15867 if (TREE_CODE (type) == TYPENAME_TYPE)
15869 /* Are template enums allowed in ISO? */
15870 if (template_parm_scope_p ())
15871 pedwarn (type_start_token->location, OPT_Wpedantic,
15872 "%qD is an enumeration template", name);
15873 /* ignore a typename reference, for it will be solved by name
15874 in start_enum. */
15875 type = NULL_TREE;
15878 else if (nested_name_specifier == error_mark_node)
15879 /* We already issued an error. */;
15880 else
15881 error_at (type_start_token->location,
15882 "%qD is not an enumerator-name", identifier);
15884 else
15886 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15887 identifier = cp_parser_identifier (parser);
15888 else
15890 identifier = make_anon_name ();
15891 is_anonymous = true;
15892 if (scoped_enum_p)
15893 error_at (type_start_token->location,
15894 "anonymous scoped enum is not allowed");
15897 pop_deferring_access_checks ();
15899 /* Check for the `:' that denotes a specified underlying type in C++0x.
15900 Note that a ':' could also indicate a bitfield width, however. */
15901 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15903 cp_decl_specifier_seq type_specifiers;
15905 /* Consume the `:'. */
15906 cp_lexer_consume_token (parser->lexer);
15908 /* Parse the type-specifier-seq. */
15909 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15910 /*is_trailing_return=*/false,
15911 &type_specifiers);
15913 /* At this point this is surely not elaborated type specifier. */
15914 if (!cp_parser_parse_definitely (parser))
15915 return NULL_TREE;
15917 if (cxx_dialect < cxx11)
15918 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15920 has_underlying_type = true;
15922 /* If that didn't work, stop. */
15923 if (type_specifiers.type != error_mark_node)
15925 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15926 /*initialized=*/0, NULL);
15927 if (underlying_type == error_mark_node
15928 || check_for_bare_parameter_packs (underlying_type))
15929 underlying_type = NULL_TREE;
15933 /* Look for the `{' but don't consume it yet. */
15934 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15936 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15938 cp_parser_error (parser, "expected %<{%>");
15939 if (has_underlying_type)
15941 type = NULL_TREE;
15942 goto out;
15945 /* An opaque-enum-specifier must have a ';' here. */
15946 if ((scoped_enum_p || underlying_type)
15947 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15949 cp_parser_error (parser, "expected %<;%> or %<{%>");
15950 if (has_underlying_type)
15952 type = NULL_TREE;
15953 goto out;
15958 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15959 return NULL_TREE;
15961 if (nested_name_specifier)
15963 if (CLASS_TYPE_P (nested_name_specifier))
15965 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15966 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15967 push_scope (nested_name_specifier);
15969 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15971 push_nested_namespace (nested_name_specifier);
15975 /* Issue an error message if type-definitions are forbidden here. */
15976 if (!cp_parser_check_type_definition (parser))
15977 type = error_mark_node;
15978 else
15979 /* Create the new type. We do this before consuming the opening
15980 brace so the enum will be recorded as being on the line of its
15981 tag (or the 'enum' keyword, if there is no tag). */
15982 type = start_enum (identifier, type, underlying_type,
15983 scoped_enum_p, &is_new_type);
15985 /* If the next token is not '{' it is an opaque-enum-specifier or an
15986 elaborated-type-specifier. */
15987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15989 timevar_push (TV_PARSE_ENUM);
15990 if (nested_name_specifier
15991 && nested_name_specifier != error_mark_node)
15993 /* The following catches invalid code such as:
15994 enum class S<int>::E { A, B, C }; */
15995 if (!processing_specialization
15996 && CLASS_TYPE_P (nested_name_specifier)
15997 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15998 error_at (type_start_token->location, "cannot add an enumerator "
15999 "list to a template instantiation");
16001 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
16003 error_at (type_start_token->location,
16004 "%<%T::%E%> has not been declared",
16005 TYPE_CONTEXT (nested_name_specifier),
16006 nested_name_specifier);
16007 type = error_mark_node;
16009 /* If that scope does not contain the scope in which the
16010 class was originally declared, the program is invalid. */
16011 else if (prev_scope && !is_ancestor (prev_scope,
16012 nested_name_specifier))
16014 if (at_namespace_scope_p ())
16015 error_at (type_start_token->location,
16016 "declaration of %qD in namespace %qD which does not "
16017 "enclose %qD",
16018 type, prev_scope, nested_name_specifier);
16019 else
16020 error_at (type_start_token->location,
16021 "declaration of %qD in %qD which does not "
16022 "enclose %qD",
16023 type, prev_scope, nested_name_specifier);
16024 type = error_mark_node;
16028 if (scoped_enum_p)
16029 begin_scope (sk_scoped_enum, type);
16031 /* Consume the opening brace. */
16032 cp_lexer_consume_token (parser->lexer);
16034 if (type == error_mark_node)
16035 ; /* Nothing to add */
16036 else if (OPAQUE_ENUM_P (type)
16037 || (cxx_dialect > cxx98 && processing_specialization))
16039 new_value_list = true;
16040 SET_OPAQUE_ENUM_P (type, false);
16041 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16043 else
16045 error_at (type_start_token->location,
16046 "multiple definition of %q#T", type);
16047 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16048 "previous definition here");
16049 type = error_mark_node;
16052 if (type == error_mark_node)
16053 cp_parser_skip_to_end_of_block_or_statement (parser);
16054 /* If the next token is not '}', then there are some enumerators. */
16055 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16057 if (is_anonymous && !scoped_enum_p)
16058 pedwarn (type_start_token->location, OPT_Wpedantic,
16059 "ISO C++ forbids empty anonymous enum");
16061 else
16062 cp_parser_enumerator_list (parser, type);
16064 /* Consume the final '}'. */
16065 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16067 if (scoped_enum_p)
16068 finish_scope ();
16069 timevar_pop (TV_PARSE_ENUM);
16071 else
16073 /* If a ';' follows, then it is an opaque-enum-specifier
16074 and additional restrictions apply. */
16075 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16077 if (is_anonymous)
16078 error_at (type_start_token->location,
16079 "opaque-enum-specifier without name");
16080 else if (nested_name_specifier)
16081 error_at (type_start_token->location,
16082 "opaque-enum-specifier must use a simple identifier");
16086 /* Look for trailing attributes to apply to this enumeration, and
16087 apply them if appropriate. */
16088 if (cp_parser_allow_gnu_extensions_p (parser))
16090 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16091 trailing_attr = chainon (trailing_attr, attributes);
16092 cplus_decl_attributes (&type,
16093 trailing_attr,
16094 (int) ATTR_FLAG_TYPE_IN_PLACE);
16097 /* Finish up the enumeration. */
16098 if (type != error_mark_node)
16100 if (new_value_list)
16101 finish_enum_value_list (type);
16102 if (is_new_type)
16103 finish_enum (type);
16106 if (nested_name_specifier)
16108 if (CLASS_TYPE_P (nested_name_specifier))
16110 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16111 pop_scope (nested_name_specifier);
16113 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16115 pop_nested_namespace (nested_name_specifier);
16118 out:
16119 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16120 return type;
16123 /* Parse an enumerator-list. The enumerators all have the indicated
16124 TYPE.
16126 enumerator-list:
16127 enumerator-definition
16128 enumerator-list , enumerator-definition */
16130 static void
16131 cp_parser_enumerator_list (cp_parser* parser, tree type)
16133 while (true)
16135 /* Parse an enumerator-definition. */
16136 cp_parser_enumerator_definition (parser, type);
16138 /* If the next token is not a ',', we've reached the end of
16139 the list. */
16140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16141 break;
16142 /* Otherwise, consume the `,' and keep going. */
16143 cp_lexer_consume_token (parser->lexer);
16144 /* If the next token is a `}', there is a trailing comma. */
16145 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16147 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16148 pedwarn (input_location, OPT_Wpedantic,
16149 "comma at end of enumerator list");
16150 break;
16155 /* Parse an enumerator-definition. The enumerator has the indicated
16156 TYPE.
16158 enumerator-definition:
16159 enumerator
16160 enumerator = constant-expression
16162 enumerator:
16163 identifier */
16165 static void
16166 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16168 tree identifier;
16169 tree value;
16170 location_t loc;
16172 /* Save the input location because we are interested in the location
16173 of the identifier and not the location of the explicit value. */
16174 loc = cp_lexer_peek_token (parser->lexer)->location;
16176 /* Look for the identifier. */
16177 identifier = cp_parser_identifier (parser);
16178 if (identifier == error_mark_node)
16179 return;
16181 /* If the next token is an '=', then there is an explicit value. */
16182 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16184 /* Consume the `=' token. */
16185 cp_lexer_consume_token (parser->lexer);
16186 /* Parse the value. */
16187 value = cp_parser_constant_expression (parser,
16188 /*allow_non_constant_p=*/false,
16189 NULL);
16191 else
16192 value = NULL_TREE;
16194 /* If we are processing a template, make sure the initializer of the
16195 enumerator doesn't contain any bare template parameter pack. */
16196 if (check_for_bare_parameter_packs (value))
16197 value = error_mark_node;
16199 /* integral_constant_value will pull out this expression, so make sure
16200 it's folded as appropriate. */
16201 value = fold_non_dependent_expr (value);
16203 /* Create the enumerator. */
16204 build_enumerator (identifier, value, type, loc);
16207 /* Parse a namespace-name.
16209 namespace-name:
16210 original-namespace-name
16211 namespace-alias
16213 Returns the NAMESPACE_DECL for the namespace. */
16215 static tree
16216 cp_parser_namespace_name (cp_parser* parser)
16218 tree identifier;
16219 tree namespace_decl;
16221 cp_token *token = cp_lexer_peek_token (parser->lexer);
16223 /* Get the name of the namespace. */
16224 identifier = cp_parser_identifier (parser);
16225 if (identifier == error_mark_node)
16226 return error_mark_node;
16228 /* Look up the identifier in the currently active scope. Look only
16229 for namespaces, due to:
16231 [basic.lookup.udir]
16233 When looking up a namespace-name in a using-directive or alias
16234 definition, only namespace names are considered.
16236 And:
16238 [basic.lookup.qual]
16240 During the lookup of a name preceding the :: scope resolution
16241 operator, object, function, and enumerator names are ignored.
16243 (Note that cp_parser_qualifying_entity only calls this
16244 function if the token after the name is the scope resolution
16245 operator.) */
16246 namespace_decl = cp_parser_lookup_name (parser, identifier,
16247 none_type,
16248 /*is_template=*/false,
16249 /*is_namespace=*/true,
16250 /*check_dependency=*/true,
16251 /*ambiguous_decls=*/NULL,
16252 token->location);
16253 /* If it's not a namespace, issue an error. */
16254 if (namespace_decl == error_mark_node
16255 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16257 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16258 error_at (token->location, "%qD is not a namespace-name", identifier);
16259 cp_parser_error (parser, "expected namespace-name");
16260 namespace_decl = error_mark_node;
16263 return namespace_decl;
16266 /* Parse a namespace-definition.
16268 namespace-definition:
16269 named-namespace-definition
16270 unnamed-namespace-definition
16272 named-namespace-definition:
16273 original-namespace-definition
16274 extension-namespace-definition
16276 original-namespace-definition:
16277 namespace identifier { namespace-body }
16279 extension-namespace-definition:
16280 namespace original-namespace-name { namespace-body }
16282 unnamed-namespace-definition:
16283 namespace { namespace-body } */
16285 static void
16286 cp_parser_namespace_definition (cp_parser* parser)
16288 tree identifier, attribs;
16289 bool has_visibility;
16290 bool is_inline;
16292 cp_ensure_no_omp_declare_simd (parser);
16293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16295 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16296 is_inline = true;
16297 cp_lexer_consume_token (parser->lexer);
16299 else
16300 is_inline = false;
16302 /* Look for the `namespace' keyword. */
16303 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16305 /* Get the name of the namespace. We do not attempt to distinguish
16306 between an original-namespace-definition and an
16307 extension-namespace-definition at this point. The semantic
16308 analysis routines are responsible for that. */
16309 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16310 identifier = cp_parser_identifier (parser);
16311 else
16312 identifier = NULL_TREE;
16314 /* Parse any specified attributes. */
16315 attribs = cp_parser_attributes_opt (parser);
16317 /* Look for the `{' to start the namespace. */
16318 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16319 /* Start the namespace. */
16320 push_namespace (identifier);
16322 /* "inline namespace" is equivalent to a stub namespace definition
16323 followed by a strong using directive. */
16324 if (is_inline)
16326 tree name_space = current_namespace;
16327 /* Set up namespace association. */
16328 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16329 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16330 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16331 /* Import the contents of the inline namespace. */
16332 pop_namespace ();
16333 do_using_directive (name_space);
16334 push_namespace (identifier);
16337 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16339 /* Parse the body of the namespace. */
16340 cp_parser_namespace_body (parser);
16342 if (has_visibility)
16343 pop_visibility (1);
16345 /* Finish the namespace. */
16346 pop_namespace ();
16347 /* Look for the final `}'. */
16348 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16351 /* Parse a namespace-body.
16353 namespace-body:
16354 declaration-seq [opt] */
16356 static void
16357 cp_parser_namespace_body (cp_parser* parser)
16359 cp_parser_declaration_seq_opt (parser);
16362 /* Parse a namespace-alias-definition.
16364 namespace-alias-definition:
16365 namespace identifier = qualified-namespace-specifier ; */
16367 static void
16368 cp_parser_namespace_alias_definition (cp_parser* parser)
16370 tree identifier;
16371 tree namespace_specifier;
16373 cp_token *token = cp_lexer_peek_token (parser->lexer);
16375 /* Look for the `namespace' keyword. */
16376 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16377 /* Look for the identifier. */
16378 identifier = cp_parser_identifier (parser);
16379 if (identifier == error_mark_node)
16380 return;
16381 /* Look for the `=' token. */
16382 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16383 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16385 error_at (token->location, "%<namespace%> definition is not allowed here");
16386 /* Skip the definition. */
16387 cp_lexer_consume_token (parser->lexer);
16388 if (cp_parser_skip_to_closing_brace (parser))
16389 cp_lexer_consume_token (parser->lexer);
16390 return;
16392 cp_parser_require (parser, CPP_EQ, RT_EQ);
16393 /* Look for the qualified-namespace-specifier. */
16394 namespace_specifier
16395 = cp_parser_qualified_namespace_specifier (parser);
16396 /* Look for the `;' token. */
16397 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16399 /* Register the alias in the symbol table. */
16400 do_namespace_alias (identifier, namespace_specifier);
16403 /* Parse a qualified-namespace-specifier.
16405 qualified-namespace-specifier:
16406 :: [opt] nested-name-specifier [opt] namespace-name
16408 Returns a NAMESPACE_DECL corresponding to the specified
16409 namespace. */
16411 static tree
16412 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16414 /* Look for the optional `::'. */
16415 cp_parser_global_scope_opt (parser,
16416 /*current_scope_valid_p=*/false);
16418 /* Look for the optional nested-name-specifier. */
16419 cp_parser_nested_name_specifier_opt (parser,
16420 /*typename_keyword_p=*/false,
16421 /*check_dependency_p=*/true,
16422 /*type_p=*/false,
16423 /*is_declaration=*/true);
16425 return cp_parser_namespace_name (parser);
16428 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16429 access declaration.
16431 using-declaration:
16432 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16433 using :: unqualified-id ;
16435 access-declaration:
16436 qualified-id ;
16440 static bool
16441 cp_parser_using_declaration (cp_parser* parser,
16442 bool access_declaration_p)
16444 cp_token *token;
16445 bool typename_p = false;
16446 bool global_scope_p;
16447 tree decl;
16448 tree identifier;
16449 tree qscope;
16450 int oldcount = errorcount;
16451 cp_token *diag_token = NULL;
16453 if (access_declaration_p)
16455 diag_token = cp_lexer_peek_token (parser->lexer);
16456 cp_parser_parse_tentatively (parser);
16458 else
16460 /* Look for the `using' keyword. */
16461 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16463 /* Peek at the next token. */
16464 token = cp_lexer_peek_token (parser->lexer);
16465 /* See if it's `typename'. */
16466 if (token->keyword == RID_TYPENAME)
16468 /* Remember that we've seen it. */
16469 typename_p = true;
16470 /* Consume the `typename' token. */
16471 cp_lexer_consume_token (parser->lexer);
16475 /* Look for the optional global scope qualification. */
16476 global_scope_p
16477 = (cp_parser_global_scope_opt (parser,
16478 /*current_scope_valid_p=*/false)
16479 != NULL_TREE);
16481 /* If we saw `typename', or didn't see `::', then there must be a
16482 nested-name-specifier present. */
16483 if (typename_p || !global_scope_p)
16485 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16486 /*check_dependency_p=*/true,
16487 /*type_p=*/false,
16488 /*is_declaration=*/true);
16489 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16491 cp_parser_skip_to_end_of_block_or_statement (parser);
16492 return false;
16495 /* Otherwise, we could be in either of the two productions. In that
16496 case, treat the nested-name-specifier as optional. */
16497 else
16498 qscope = cp_parser_nested_name_specifier_opt (parser,
16499 /*typename_keyword_p=*/false,
16500 /*check_dependency_p=*/true,
16501 /*type_p=*/false,
16502 /*is_declaration=*/true);
16503 if (!qscope)
16504 qscope = global_namespace;
16505 else if (UNSCOPED_ENUM_P (qscope))
16506 qscope = CP_TYPE_CONTEXT (qscope);
16508 if (access_declaration_p && cp_parser_error_occurred (parser))
16509 /* Something has already gone wrong; there's no need to parse
16510 further. Since an error has occurred, the return value of
16511 cp_parser_parse_definitely will be false, as required. */
16512 return cp_parser_parse_definitely (parser);
16514 token = cp_lexer_peek_token (parser->lexer);
16515 /* Parse the unqualified-id. */
16516 identifier = cp_parser_unqualified_id (parser,
16517 /*template_keyword_p=*/false,
16518 /*check_dependency_p=*/true,
16519 /*declarator_p=*/true,
16520 /*optional_p=*/false);
16522 if (access_declaration_p)
16524 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16525 cp_parser_simulate_error (parser);
16526 if (!cp_parser_parse_definitely (parser))
16527 return false;
16530 /* The function we call to handle a using-declaration is different
16531 depending on what scope we are in. */
16532 if (qscope == error_mark_node || identifier == error_mark_node)
16534 else if (!identifier_p (identifier)
16535 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16536 /* [namespace.udecl]
16538 A using declaration shall not name a template-id. */
16539 error_at (token->location,
16540 "a template-id may not appear in a using-declaration");
16541 else
16543 if (at_class_scope_p ())
16545 /* Create the USING_DECL. */
16546 decl = do_class_using_decl (parser->scope, identifier);
16548 if (decl && typename_p)
16549 USING_DECL_TYPENAME_P (decl) = 1;
16551 if (check_for_bare_parameter_packs (decl))
16553 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16554 return false;
16556 else
16557 /* Add it to the list of members in this class. */
16558 finish_member_declaration (decl);
16560 else
16562 decl = cp_parser_lookup_name_simple (parser,
16563 identifier,
16564 token->location);
16565 if (decl == error_mark_node)
16566 cp_parser_name_lookup_error (parser, identifier,
16567 decl, NLE_NULL,
16568 token->location);
16569 else if (check_for_bare_parameter_packs (decl))
16571 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16572 return false;
16574 else if (!at_namespace_scope_p ())
16575 do_local_using_decl (decl, qscope, identifier);
16576 else
16577 do_toplevel_using_decl (decl, qscope, identifier);
16581 /* Look for the final `;'. */
16582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16584 if (access_declaration_p && errorcount == oldcount)
16585 warning_at (diag_token->location, OPT_Wdeprecated,
16586 "access declarations are deprecated "
16587 "in favour of using-declarations; "
16588 "suggestion: add the %<using%> keyword");
16590 return true;
16593 /* Parse an alias-declaration.
16595 alias-declaration:
16596 using identifier attribute-specifier-seq [opt] = type-id */
16598 static tree
16599 cp_parser_alias_declaration (cp_parser* parser)
16601 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16602 location_t id_location;
16603 cp_declarator *declarator;
16604 cp_decl_specifier_seq decl_specs;
16605 bool member_p;
16606 const char *saved_message = NULL;
16608 /* Look for the `using' keyword. */
16609 cp_token *using_token
16610 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16611 if (using_token == NULL)
16612 return error_mark_node;
16614 id_location = cp_lexer_peek_token (parser->lexer)->location;
16615 id = cp_parser_identifier (parser);
16616 if (id == error_mark_node)
16617 return error_mark_node;
16619 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16620 attributes = cp_parser_attributes_opt (parser);
16621 if (attributes == error_mark_node)
16622 return error_mark_node;
16624 cp_parser_require (parser, CPP_EQ, RT_EQ);
16626 if (cp_parser_error_occurred (parser))
16627 return error_mark_node;
16629 cp_parser_commit_to_tentative_parse (parser);
16631 /* Now we are going to parse the type-id of the declaration. */
16634 [dcl.type]/3 says:
16636 "A type-specifier-seq shall not define a class or enumeration
16637 unless it appears in the type-id of an alias-declaration (7.1.3) that
16638 is not the declaration of a template-declaration."
16640 In other words, if we currently are in an alias template, the
16641 type-id should not define a type.
16643 So let's set parser->type_definition_forbidden_message in that
16644 case; cp_parser_check_type_definition (called by
16645 cp_parser_class_specifier) will then emit an error if a type is
16646 defined in the type-id. */
16647 if (parser->num_template_parameter_lists)
16649 saved_message = parser->type_definition_forbidden_message;
16650 parser->type_definition_forbidden_message =
16651 G_("types may not be defined in alias template declarations");
16654 type = cp_parser_type_id (parser);
16656 /* Restore the error message if need be. */
16657 if (parser->num_template_parameter_lists)
16658 parser->type_definition_forbidden_message = saved_message;
16660 if (type == error_mark_node
16661 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16663 cp_parser_skip_to_end_of_block_or_statement (parser);
16664 return error_mark_node;
16667 /* A typedef-name can also be introduced by an alias-declaration. The
16668 identifier following the using keyword becomes a typedef-name. It has
16669 the same semantics as if it were introduced by the typedef
16670 specifier. In particular, it does not define a new type and it shall
16671 not appear in the type-id. */
16673 clear_decl_specs (&decl_specs);
16674 decl_specs.type = type;
16675 if (attributes != NULL_TREE)
16677 decl_specs.attributes = attributes;
16678 set_and_check_decl_spec_loc (&decl_specs,
16679 ds_attribute,
16680 attrs_token);
16682 set_and_check_decl_spec_loc (&decl_specs,
16683 ds_typedef,
16684 using_token);
16685 set_and_check_decl_spec_loc (&decl_specs,
16686 ds_alias,
16687 using_token);
16689 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16690 declarator->id_loc = id_location;
16692 member_p = at_class_scope_p ();
16693 if (member_p)
16694 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16695 NULL_TREE, attributes);
16696 else
16697 decl = start_decl (declarator, &decl_specs, 0,
16698 attributes, NULL_TREE, &pushed_scope);
16699 if (decl == error_mark_node)
16700 return decl;
16702 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16704 if (pushed_scope)
16705 pop_scope (pushed_scope);
16707 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16708 added into the symbol table; otherwise, return the TYPE_DECL. */
16709 if (DECL_LANG_SPECIFIC (decl)
16710 && DECL_TEMPLATE_INFO (decl)
16711 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16713 decl = DECL_TI_TEMPLATE (decl);
16714 if (member_p)
16715 check_member_template (decl);
16718 return decl;
16721 /* Parse a using-directive.
16723 using-directive:
16724 using namespace :: [opt] nested-name-specifier [opt]
16725 namespace-name ; */
16727 static void
16728 cp_parser_using_directive (cp_parser* parser)
16730 tree namespace_decl;
16731 tree attribs;
16733 /* Look for the `using' keyword. */
16734 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16735 /* And the `namespace' keyword. */
16736 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16737 /* Look for the optional `::' operator. */
16738 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16739 /* And the optional nested-name-specifier. */
16740 cp_parser_nested_name_specifier_opt (parser,
16741 /*typename_keyword_p=*/false,
16742 /*check_dependency_p=*/true,
16743 /*type_p=*/false,
16744 /*is_declaration=*/true);
16745 /* Get the namespace being used. */
16746 namespace_decl = cp_parser_namespace_name (parser);
16747 /* And any specified attributes. */
16748 attribs = cp_parser_attributes_opt (parser);
16749 /* Update the symbol table. */
16750 parse_using_directive (namespace_decl, attribs);
16751 /* Look for the final `;'. */
16752 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16755 /* Parse an asm-definition.
16757 asm-definition:
16758 asm ( string-literal ) ;
16760 GNU Extension:
16762 asm-definition:
16763 asm volatile [opt] ( string-literal ) ;
16764 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16765 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16766 : asm-operand-list [opt] ) ;
16767 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16768 : asm-operand-list [opt]
16769 : asm-clobber-list [opt] ) ;
16770 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16771 : asm-clobber-list [opt]
16772 : asm-goto-list ) ; */
16774 static void
16775 cp_parser_asm_definition (cp_parser* parser)
16777 tree string;
16778 tree outputs = NULL_TREE;
16779 tree inputs = NULL_TREE;
16780 tree clobbers = NULL_TREE;
16781 tree labels = NULL_TREE;
16782 tree asm_stmt;
16783 bool volatile_p = false;
16784 bool extended_p = false;
16785 bool invalid_inputs_p = false;
16786 bool invalid_outputs_p = false;
16787 bool goto_p = false;
16788 required_token missing = RT_NONE;
16790 /* Look for the `asm' keyword. */
16791 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16792 /* See if the next token is `volatile'. */
16793 if (cp_parser_allow_gnu_extensions_p (parser)
16794 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16796 /* Remember that we saw the `volatile' keyword. */
16797 volatile_p = true;
16798 /* Consume the token. */
16799 cp_lexer_consume_token (parser->lexer);
16801 if (cp_parser_allow_gnu_extensions_p (parser)
16802 && parser->in_function_body
16803 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16805 /* Remember that we saw the `goto' keyword. */
16806 goto_p = true;
16807 /* Consume the token. */
16808 cp_lexer_consume_token (parser->lexer);
16810 /* Look for the opening `('. */
16811 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16812 return;
16813 /* Look for the string. */
16814 string = cp_parser_string_literal (parser, false, false);
16815 if (string == error_mark_node)
16817 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16818 /*consume_paren=*/true);
16819 return;
16822 /* If we're allowing GNU extensions, check for the extended assembly
16823 syntax. Unfortunately, the `:' tokens need not be separated by
16824 a space in C, and so, for compatibility, we tolerate that here
16825 too. Doing that means that we have to treat the `::' operator as
16826 two `:' tokens. */
16827 if (cp_parser_allow_gnu_extensions_p (parser)
16828 && parser->in_function_body
16829 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16830 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16832 bool inputs_p = false;
16833 bool clobbers_p = false;
16834 bool labels_p = false;
16836 /* The extended syntax was used. */
16837 extended_p = true;
16839 /* Look for outputs. */
16840 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16842 /* Consume the `:'. */
16843 cp_lexer_consume_token (parser->lexer);
16844 /* Parse the output-operands. */
16845 if (cp_lexer_next_token_is_not (parser->lexer,
16846 CPP_COLON)
16847 && cp_lexer_next_token_is_not (parser->lexer,
16848 CPP_SCOPE)
16849 && cp_lexer_next_token_is_not (parser->lexer,
16850 CPP_CLOSE_PAREN)
16851 && !goto_p)
16852 outputs = cp_parser_asm_operand_list (parser);
16854 if (outputs == error_mark_node)
16855 invalid_outputs_p = true;
16857 /* If the next token is `::', there are no outputs, and the
16858 next token is the beginning of the inputs. */
16859 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16860 /* The inputs are coming next. */
16861 inputs_p = true;
16863 /* Look for inputs. */
16864 if (inputs_p
16865 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16867 /* Consume the `:' or `::'. */
16868 cp_lexer_consume_token (parser->lexer);
16869 /* Parse the output-operands. */
16870 if (cp_lexer_next_token_is_not (parser->lexer,
16871 CPP_COLON)
16872 && cp_lexer_next_token_is_not (parser->lexer,
16873 CPP_SCOPE)
16874 && cp_lexer_next_token_is_not (parser->lexer,
16875 CPP_CLOSE_PAREN))
16876 inputs = cp_parser_asm_operand_list (parser);
16878 if (inputs == error_mark_node)
16879 invalid_inputs_p = true;
16881 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16882 /* The clobbers are coming next. */
16883 clobbers_p = true;
16885 /* Look for clobbers. */
16886 if (clobbers_p
16887 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16889 clobbers_p = true;
16890 /* Consume the `:' or `::'. */
16891 cp_lexer_consume_token (parser->lexer);
16892 /* Parse the clobbers. */
16893 if (cp_lexer_next_token_is_not (parser->lexer,
16894 CPP_COLON)
16895 && cp_lexer_next_token_is_not (parser->lexer,
16896 CPP_CLOSE_PAREN))
16897 clobbers = cp_parser_asm_clobber_list (parser);
16899 else if (goto_p
16900 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16901 /* The labels are coming next. */
16902 labels_p = true;
16904 /* Look for labels. */
16905 if (labels_p
16906 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16908 labels_p = true;
16909 /* Consume the `:' or `::'. */
16910 cp_lexer_consume_token (parser->lexer);
16911 /* Parse the labels. */
16912 labels = cp_parser_asm_label_list (parser);
16915 if (goto_p && !labels_p)
16916 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16918 else if (goto_p)
16919 missing = RT_COLON_SCOPE;
16921 /* Look for the closing `)'. */
16922 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16923 missing ? missing : RT_CLOSE_PAREN))
16924 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16925 /*consume_paren=*/true);
16926 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16928 if (!invalid_inputs_p && !invalid_outputs_p)
16930 /* Create the ASM_EXPR. */
16931 if (parser->in_function_body)
16933 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16934 inputs, clobbers, labels);
16935 /* If the extended syntax was not used, mark the ASM_EXPR. */
16936 if (!extended_p)
16938 tree temp = asm_stmt;
16939 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16940 temp = TREE_OPERAND (temp, 0);
16942 ASM_INPUT_P (temp) = 1;
16945 else
16946 add_asm_node (string);
16950 // An RAII guard that manages the current template reuqirememts. The
16951 // constructor takes a boolean value, SAVE that, when true, resets
16952 // the current template requirements on construction. Otherwise, no change
16953 // is made. When the destructor is invoked, the current template requirements
16954 // are reset (made null) preventing subsequent parsing routines from using
16955 // them.
16956 struct cp_manage_requirements {
16957 bool save;
16958 tree reqs;
16960 // Construct the guard. If SAVE is true, the current requirements
16961 // saved and reset. Otherwise, no action is taken.
16962 cp_manage_requirements (bool save)
16963 : save (save), reqs (save ? release (current_template_reqs) : NULL_TREE)
16966 ~cp_manage_requirements ()
16968 current_template_reqs = reqs;
16972 // A DECLARATOR may have a trailing requires clause; it may also have
16973 // requirements introduced through the use of terse notation in the
16974 // declaration of function parameters. Returns the parsed and analyzed
16975 // template requirements.
16976 static tree
16977 cp_parser_trailing_requirements (cp_parser *parser, cp_declarator *decl)
16979 if (function_declarator_p (decl))
16981 // Get any constraints induced by the terse notaiton.
16982 tree terse_reqs = NULL_TREE;
16983 if (parser->fully_implicit_function_template_p)
16984 terse_reqs = get_shorthand_requirements (current_template_parms);
16986 // An optional requires clause can yield an additional constraint.
16987 tree explicit_reqs = cp_parser_requires_clause_opt (parser);
16989 // If requirements were specified in either the implicit
16990 // template parameter list or an explicit requires clause,
16991 // prepare to associate those with the declaration.
16992 if (tree r = conjoin_requirements (terse_reqs, explicit_reqs))
16993 current_template_reqs = finish_template_requirements (r);
16996 return current_template_reqs;
17000 /* Declarators [gram.dcl.decl] */
17002 /* Parse an init-declarator.
17004 init-declarator:
17005 declarator initializer [opt]
17007 GNU Extension:
17009 init-declarator:
17010 declarator asm-specification [opt] attributes [opt] initializer [opt]
17012 function-definition:
17013 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17014 function-body
17015 decl-specifier-seq [opt] declarator function-try-block
17017 GNU Extension:
17019 function-definition:
17020 __extension__ function-definition
17022 TM Extension:
17024 function-definition:
17025 decl-specifier-seq [opt] declarator function-transaction-block
17027 The DECL_SPECIFIERS apply to this declarator. Returns a
17028 representation of the entity declared. If MEMBER_P is TRUE, then
17029 this declarator appears in a class scope. The new DECL created by
17030 this declarator is returned.
17032 The CHECKS are access checks that should be performed once we know
17033 what entity is being declared (and, therefore, what classes have
17034 befriended it).
17036 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17037 for a function-definition here as well. If the declarator is a
17038 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17039 be TRUE upon return. By that point, the function-definition will
17040 have been completely parsed.
17042 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17043 is FALSE.
17045 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17046 parsed declaration if it is an uninitialized single declarator not followed
17047 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17048 if present, will not be consumed. If returned, this declarator will be
17049 created with SD_INITIALIZED but will not call cp_finish_decl. */
17051 static tree
17052 cp_parser_init_declarator (cp_parser* parser,
17053 cp_decl_specifier_seq *decl_specifiers,
17054 vec<deferred_access_check, va_gc> *checks,
17055 bool function_definition_allowed_p,
17056 bool member_p,
17057 int declares_class_or_enum,
17058 bool* function_definition_p,
17059 tree* maybe_range_for_decl)
17061 cp_token *token = NULL, *asm_spec_start_token = NULL,
17062 *attributes_start_token = NULL;
17063 cp_declarator *declarator;
17064 tree prefix_attributes;
17065 tree attributes = NULL;
17066 tree asm_specification;
17067 tree initializer;
17068 tree decl = NULL_TREE;
17069 tree scope;
17070 int is_initialized;
17071 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17072 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17073 "(...)". */
17074 enum cpp_ttype initialization_kind;
17075 bool is_direct_init = false;
17076 bool is_non_constant_init;
17077 int ctor_dtor_or_conv_p;
17078 bool friend_p = cp_parser_friend_p (decl_specifiers);
17079 tree pushed_scope = NULL_TREE;
17080 bool range_for_decl_p = false;
17081 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17083 /* Gather the attributes that were provided with the
17084 decl-specifiers. */
17085 prefix_attributes = decl_specifiers->attributes;
17087 /* Assume that this is not the declarator for a function
17088 definition. */
17089 if (function_definition_p)
17090 *function_definition_p = false;
17092 /* Default arguments are only permitted for function parameters. */
17093 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17094 parser->default_arg_ok_p = false;
17096 /* Defer access checks while parsing the declarator; we cannot know
17097 what names are accessible until we know what is being
17098 declared. */
17099 resume_deferring_access_checks ();
17101 /* Parse the declarator. */
17102 token = cp_lexer_peek_token (parser->lexer);
17103 declarator
17104 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17105 &ctor_dtor_or_conv_p,
17106 /*parenthesized_p=*/NULL,
17107 member_p, friend_p);
17108 /* Gather up the deferred checks. */
17109 stop_deferring_access_checks ();
17111 parser->default_arg_ok_p = saved_default_arg_ok_p;
17113 /* If the DECLARATOR was erroneous, there's no need to go
17114 further. */
17115 if (declarator == cp_error_declarator)
17116 return error_mark_node;
17118 /* Check that the number of template-parameter-lists is OK. */
17119 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17120 token->location))
17121 return error_mark_node;
17123 if (declares_class_or_enum & 2)
17124 cp_parser_check_for_definition_in_return_type (declarator,
17125 decl_specifiers->type,
17126 decl_specifiers->locations[ds_type_spec]);
17128 /* Figure out what scope the entity declared by the DECLARATOR is
17129 located in. `grokdeclarator' sometimes changes the scope, so
17130 we compute it now. */
17131 scope = get_scope_of_declarator (declarator);
17133 /* Perform any lookups in the declared type which were thought to be
17134 dependent, but are not in the scope of the declarator. */
17135 decl_specifiers->type
17136 = maybe_update_decl_type (decl_specifiers->type, scope);
17138 /* If we're allowing GNU extensions, look for an
17139 asm-specification. */
17140 if (cp_parser_allow_gnu_extensions_p (parser))
17142 /* Look for an asm-specification. */
17143 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17144 asm_specification = cp_parser_asm_specification_opt (parser);
17146 else
17147 asm_specification = NULL_TREE;
17149 /* Look for attributes. */
17150 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17151 attributes = cp_parser_attributes_opt (parser);
17154 // If scope is non-null, then we're parsing a declarator with
17155 // a nested-name specifier. It's possible that some component of
17156 // that specifier is bringing template contsraints with it, which
17157 // we need to suppress for for the time being. For example:
17159 // template<typename T>
17160 // requires C<T>()
17161 // void S<T>::f() requires D<T>() { ... }
17163 // At the point we parse the 2nd requires clause, the previous the
17164 // current constraints will have been used to resolve the enclosing
17165 // class S<T>. The D<T>() requirement applies only to the definition
17166 // of f and do not include C<T>().
17168 // Save requirements, resetting them if the scope was established.
17169 cp_manage_requirements saved_requirements (scope != NULL_TREE);
17171 // TODO: Check that a declaration does not have 2 requires clauses.
17173 // Parse an optional trailing requires clause for the parsed declarator.
17174 if (flag_concepts)
17175 cp_parser_trailing_requirements (parser, declarator);
17177 /* Peek at the next token. */
17178 token = cp_lexer_peek_token (parser->lexer);
17180 if (function_declarator_p (declarator))
17182 /* Check to see if the token indicates the start of a
17183 function-definition. */
17184 if (cp_parser_token_starts_function_definition_p (token))
17186 if (!function_definition_allowed_p)
17188 /* If a function-definition should not appear here, issue an
17189 error message. */
17190 cp_parser_error (parser,
17191 "a function-definition is not allowed here");
17192 return error_mark_node;
17195 location_t func_brace_location
17196 = cp_lexer_peek_token (parser->lexer)->location;
17198 /* Neither attributes nor an asm-specification are allowed
17199 on a function-definition. */
17200 if (asm_specification)
17201 error_at (asm_spec_start_token->location,
17202 "an asm-specification is not allowed "
17203 "on a function-definition");
17204 if (attributes)
17205 error_at (attributes_start_token->location,
17206 "attributes are not allowed "
17207 "on a function-definition");
17208 /* This is a function-definition. */
17209 *function_definition_p = true;
17211 /* Parse the function definition. */
17212 if (member_p)
17213 decl = cp_parser_save_member_function_body (parser,
17214 decl_specifiers,
17215 declarator,
17216 prefix_attributes);
17217 else
17218 decl =
17219 (cp_parser_function_definition_from_specifiers_and_declarator
17220 (parser, decl_specifiers, prefix_attributes, declarator));
17222 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17224 /* This is where the prologue starts... */
17225 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17226 = func_brace_location;
17229 return decl;
17233 /* [dcl.dcl]
17235 Only in function declarations for constructors, destructors, and
17236 type conversions can the decl-specifier-seq be omitted.
17238 We explicitly postpone this check past the point where we handle
17239 function-definitions because we tolerate function-definitions
17240 that are missing their return types in some modes. */
17241 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17243 cp_parser_error (parser,
17244 "expected constructor, destructor, or type conversion");
17245 return error_mark_node;
17248 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17249 if (token->type == CPP_EQ
17250 || token->type == CPP_OPEN_PAREN
17251 || token->type == CPP_OPEN_BRACE)
17253 is_initialized = SD_INITIALIZED;
17254 initialization_kind = token->type;
17255 if (maybe_range_for_decl)
17256 *maybe_range_for_decl = error_mark_node;
17258 if (token->type == CPP_EQ
17259 && function_declarator_p (declarator))
17261 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17262 if (t2->keyword == RID_DEFAULT)
17263 is_initialized = SD_DEFAULTED;
17264 else if (t2->keyword == RID_DELETE)
17265 is_initialized = SD_DELETED;
17268 else
17270 /* If the init-declarator isn't initialized and isn't followed by a
17271 `,' or `;', it's not a valid init-declarator. */
17272 if (token->type != CPP_COMMA
17273 && token->type != CPP_SEMICOLON)
17275 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17276 range_for_decl_p = true;
17277 else
17279 cp_parser_error (parser, "expected initializer");
17280 return error_mark_node;
17283 is_initialized = SD_UNINITIALIZED;
17284 initialization_kind = CPP_EOF;
17287 /* Because start_decl has side-effects, we should only call it if we
17288 know we're going ahead. By this point, we know that we cannot
17289 possibly be looking at any other construct. */
17290 cp_parser_commit_to_tentative_parse (parser);
17292 /* If the decl specifiers were bad, issue an error now that we're
17293 sure this was intended to be a declarator. Then continue
17294 declaring the variable(s), as int, to try to cut down on further
17295 errors. */
17296 if (decl_specifiers->any_specifiers_p
17297 && decl_specifiers->type == error_mark_node)
17299 cp_parser_error (parser, "invalid type in declaration");
17300 decl_specifiers->type = integer_type_node;
17303 /* Enter the newly declared entry in the symbol table. If we're
17304 processing a declaration in a class-specifier, we wait until
17305 after processing the initializer. */
17306 if (!member_p)
17308 if (parser->in_unbraced_linkage_specification_p)
17309 decl_specifiers->storage_class = sc_extern;
17310 decl = start_decl (declarator, decl_specifiers,
17311 range_for_decl_p? SD_INITIALIZED : is_initialized,
17312 attributes, prefix_attributes, &pushed_scope);
17313 cp_finalize_omp_declare_simd (parser, decl);
17314 /* Adjust location of decl if declarator->id_loc is more appropriate:
17315 set, and decl wasn't merged with another decl, in which case its
17316 location would be different from input_location, and more accurate. */
17317 if (DECL_P (decl)
17318 && declarator->id_loc != UNKNOWN_LOCATION
17319 && DECL_SOURCE_LOCATION (decl) == input_location)
17320 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17322 else if (scope)
17323 /* Enter the SCOPE. That way unqualified names appearing in the
17324 initializer will be looked up in SCOPE. */
17325 pushed_scope = push_scope (scope);
17327 /* Perform deferred access control checks, now that we know in which
17328 SCOPE the declared entity resides. */
17329 if (!member_p && decl)
17331 tree saved_current_function_decl = NULL_TREE;
17333 /* If the entity being declared is a function, pretend that we
17334 are in its scope. If it is a `friend', it may have access to
17335 things that would not otherwise be accessible. */
17336 if (TREE_CODE (decl) == FUNCTION_DECL)
17338 saved_current_function_decl = current_function_decl;
17339 current_function_decl = decl;
17342 /* Perform access checks for template parameters. */
17343 cp_parser_perform_template_parameter_access_checks (checks);
17345 /* Perform the access control checks for the declarator and the
17346 decl-specifiers. */
17347 perform_deferred_access_checks (tf_warning_or_error);
17349 /* Restore the saved value. */
17350 if (TREE_CODE (decl) == FUNCTION_DECL)
17351 current_function_decl = saved_current_function_decl;
17354 /* Parse the initializer. */
17355 initializer = NULL_TREE;
17356 is_direct_init = false;
17357 is_non_constant_init = true;
17358 if (is_initialized)
17360 if (function_declarator_p (declarator))
17362 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
17363 if (initialization_kind == CPP_EQ)
17364 initializer = cp_parser_pure_specifier (parser);
17365 else
17367 /* If the declaration was erroneous, we don't really
17368 know what the user intended, so just silently
17369 consume the initializer. */
17370 if (decl != error_mark_node)
17371 error_at (initializer_start_token->location,
17372 "initializer provided for function");
17373 cp_parser_skip_to_closing_parenthesis (parser,
17374 /*recovering=*/true,
17375 /*or_comma=*/false,
17376 /*consume_paren=*/true);
17379 else
17381 /* We want to record the extra mangling scope for in-class
17382 initializers of class members and initializers of static data
17383 member templates. The former involves deferring
17384 parsing of the initializer until end of class as with default
17385 arguments. So right here we only handle the latter. */
17386 if (!member_p && processing_template_decl)
17387 start_lambda_scope (decl);
17388 initializer = cp_parser_initializer (parser,
17389 &is_direct_init,
17390 &is_non_constant_init);
17391 if (!member_p && processing_template_decl)
17392 finish_lambda_scope ();
17393 if (initializer == error_mark_node)
17394 cp_parser_skip_to_end_of_statement (parser);
17398 /* The old parser allows attributes to appear after a parenthesized
17399 initializer. Mark Mitchell proposed removing this functionality
17400 on the GCC mailing lists on 2002-08-13. This parser accepts the
17401 attributes -- but ignores them. */
17402 if (cp_parser_allow_gnu_extensions_p (parser)
17403 && initialization_kind == CPP_OPEN_PAREN)
17404 if (cp_parser_attributes_opt (parser))
17405 warning (OPT_Wattributes,
17406 "attributes after parenthesized initializer ignored");
17408 /* A non-template declaration involving a function parameter list containing
17409 an implicit template parameter will have been made into a template. If it
17410 turns out that the resulting declaration is not an actual function then
17411 finish the template declaration here. An error message will already have
17412 been issued. */
17413 if (parser->fully_implicit_function_template_p)
17414 if (!function_declarator_p (declarator))
17416 if (pushed_scope)
17418 pop_scope (pushed_scope);
17419 pushed_scope = 0;
17421 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17424 /* For an in-class declaration, use `grokfield' to create the
17425 declaration. */
17426 if (member_p)
17428 if (pushed_scope)
17430 pop_scope (pushed_scope);
17431 pushed_scope = NULL_TREE;
17433 decl = grokfield (declarator, decl_specifiers,
17434 initializer, !is_non_constant_init,
17435 /*asmspec=*/NULL_TREE,
17436 chainon (attributes, prefix_attributes));
17437 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17438 cp_parser_save_default_args (parser, decl);
17439 cp_finalize_omp_declare_simd (parser, decl);
17442 /* Finish processing the declaration. But, skip member
17443 declarations. */
17444 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17446 cp_finish_decl (decl,
17447 initializer, !is_non_constant_init,
17448 asm_specification,
17449 /* If the initializer is in parentheses, then this is
17450 a direct-initialization, which means that an
17451 `explicit' constructor is OK. Otherwise, an
17452 `explicit' constructor cannot be used. */
17453 ((is_direct_init || !is_initialized)
17454 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17456 else if ((cxx_dialect != cxx98) && friend_p
17457 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17458 /* Core issue #226 (C++0x only): A default template-argument
17459 shall not be specified in a friend class template
17460 declaration. */
17461 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17462 /*is_partial=*/false, /*is_friend_decl=*/1);
17464 if (!friend_p && pushed_scope)
17465 pop_scope (pushed_scope);
17467 if (function_declarator_p (declarator)
17468 && parser->fully_implicit_function_template_p)
17470 if (member_p)
17471 decl = finish_fully_implicit_template (parser, decl);
17472 else
17473 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17476 return decl;
17479 /* Parse a declarator.
17481 declarator:
17482 direct-declarator
17483 ptr-operator declarator
17485 abstract-declarator:
17486 ptr-operator abstract-declarator [opt]
17487 direct-abstract-declarator
17489 GNU Extensions:
17491 declarator:
17492 attributes [opt] direct-declarator
17493 attributes [opt] ptr-operator declarator
17495 abstract-declarator:
17496 attributes [opt] ptr-operator abstract-declarator [opt]
17497 attributes [opt] direct-abstract-declarator
17499 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17500 detect constructor, destructor or conversion operators. It is set
17501 to -1 if the declarator is a name, and +1 if it is a
17502 function. Otherwise it is set to zero. Usually you just want to
17503 test for >0, but internally the negative value is used.
17505 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17506 a decl-specifier-seq unless it declares a constructor, destructor,
17507 or conversion. It might seem that we could check this condition in
17508 semantic analysis, rather than parsing, but that makes it difficult
17509 to handle something like `f()'. We want to notice that there are
17510 no decl-specifiers, and therefore realize that this is an
17511 expression, not a declaration.)
17513 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17514 the declarator is a direct-declarator of the form "(...)".
17516 MEMBER_P is true iff this declarator is a member-declarator.
17518 FRIEND_P is true iff this declarator is a friend. */
17520 static cp_declarator *
17521 cp_parser_declarator (cp_parser* parser,
17522 cp_parser_declarator_kind dcl_kind,
17523 int* ctor_dtor_or_conv_p,
17524 bool* parenthesized_p,
17525 bool member_p, bool friend_p)
17527 cp_declarator *declarator;
17528 enum tree_code code;
17529 cp_cv_quals cv_quals;
17530 tree class_type;
17531 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17533 /* Assume this is not a constructor, destructor, or type-conversion
17534 operator. */
17535 if (ctor_dtor_or_conv_p)
17536 *ctor_dtor_or_conv_p = 0;
17538 if (cp_parser_allow_gnu_extensions_p (parser))
17539 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17541 /* Check for the ptr-operator production. */
17542 cp_parser_parse_tentatively (parser);
17543 /* Parse the ptr-operator. */
17544 code = cp_parser_ptr_operator (parser,
17545 &class_type,
17546 &cv_quals,
17547 &std_attributes);
17549 /* If that worked, then we have a ptr-operator. */
17550 if (cp_parser_parse_definitely (parser))
17552 /* If a ptr-operator was found, then this declarator was not
17553 parenthesized. */
17554 if (parenthesized_p)
17555 *parenthesized_p = true;
17556 /* The dependent declarator is optional if we are parsing an
17557 abstract-declarator. */
17558 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17559 cp_parser_parse_tentatively (parser);
17561 /* Parse the dependent declarator. */
17562 declarator = cp_parser_declarator (parser, dcl_kind,
17563 /*ctor_dtor_or_conv_p=*/NULL,
17564 /*parenthesized_p=*/NULL,
17565 /*member_p=*/false,
17566 friend_p);
17568 /* If we are parsing an abstract-declarator, we must handle the
17569 case where the dependent declarator is absent. */
17570 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17571 && !cp_parser_parse_definitely (parser))
17572 declarator = NULL;
17574 declarator = cp_parser_make_indirect_declarator
17575 (code, class_type, cv_quals, declarator, std_attributes);
17577 /* Everything else is a direct-declarator. */
17578 else
17580 if (parenthesized_p)
17581 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17582 CPP_OPEN_PAREN);
17583 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17584 ctor_dtor_or_conv_p,
17585 member_p, friend_p);
17588 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17589 declarator->attributes = gnu_attributes;
17591 return declarator;
17594 /* Parse a direct-declarator or direct-abstract-declarator.
17596 direct-declarator:
17597 declarator-id
17598 direct-declarator ( parameter-declaration-clause )
17599 cv-qualifier-seq [opt]
17600 ref-qualifier [opt]
17601 exception-specification [opt]
17602 direct-declarator [ constant-expression [opt] ]
17603 ( declarator )
17605 direct-abstract-declarator:
17606 direct-abstract-declarator [opt]
17607 ( parameter-declaration-clause )
17608 cv-qualifier-seq [opt]
17609 ref-qualifier [opt]
17610 exception-specification [opt]
17611 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17612 ( abstract-declarator )
17614 Returns a representation of the declarator. DCL_KIND is
17615 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17616 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17617 we are parsing a direct-declarator. It is
17618 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17619 of ambiguity we prefer an abstract declarator, as per
17620 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17621 as for cp_parser_declarator. */
17623 static cp_declarator *
17624 cp_parser_direct_declarator (cp_parser* parser,
17625 cp_parser_declarator_kind dcl_kind,
17626 int* ctor_dtor_or_conv_p,
17627 bool member_p, bool friend_p)
17629 cp_token *token;
17630 cp_declarator *declarator = NULL;
17631 tree scope = NULL_TREE;
17632 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17633 bool saved_in_declarator_p = parser->in_declarator_p;
17634 bool first = true;
17635 tree pushed_scope = NULL_TREE;
17637 while (true)
17639 /* Peek at the next token. */
17640 token = cp_lexer_peek_token (parser->lexer);
17641 if (token->type == CPP_OPEN_PAREN)
17643 /* This is either a parameter-declaration-clause, or a
17644 parenthesized declarator. When we know we are parsing a
17645 named declarator, it must be a parenthesized declarator
17646 if FIRST is true. For instance, `(int)' is a
17647 parameter-declaration-clause, with an omitted
17648 direct-abstract-declarator. But `((*))', is a
17649 parenthesized abstract declarator. Finally, when T is a
17650 template parameter `(T)' is a
17651 parameter-declaration-clause, and not a parenthesized
17652 named declarator.
17654 We first try and parse a parameter-declaration-clause,
17655 and then try a nested declarator (if FIRST is true).
17657 It is not an error for it not to be a
17658 parameter-declaration-clause, even when FIRST is
17659 false. Consider,
17661 int i (int);
17662 int i (3);
17664 The first is the declaration of a function while the
17665 second is the definition of a variable, including its
17666 initializer.
17668 Having seen only the parenthesis, we cannot know which of
17669 these two alternatives should be selected. Even more
17670 complex are examples like:
17672 int i (int (a));
17673 int i (int (3));
17675 The former is a function-declaration; the latter is a
17676 variable initialization.
17678 Thus again, we try a parameter-declaration-clause, and if
17679 that fails, we back out and return. */
17681 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17683 tree params;
17684 bool is_declarator = false;
17686 /* In a member-declarator, the only valid interpretation
17687 of a parenthesis is the start of a
17688 parameter-declaration-clause. (It is invalid to
17689 initialize a static data member with a parenthesized
17690 initializer; only the "=" form of initialization is
17691 permitted.) */
17692 if (!member_p)
17693 cp_parser_parse_tentatively (parser);
17695 /* Consume the `('. */
17696 cp_lexer_consume_token (parser->lexer);
17697 if (first)
17699 /* If this is going to be an abstract declarator, we're
17700 in a declarator and we can't have default args. */
17701 parser->default_arg_ok_p = false;
17702 parser->in_declarator_p = true;
17705 begin_scope (sk_function_parms, NULL_TREE);
17707 /* Parse the parameter-declaration-clause. */
17708 params = cp_parser_parameter_declaration_clause (parser);
17710 /* Consume the `)'. */
17711 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17713 /* If all went well, parse the cv-qualifier-seq,
17714 ref-qualifier and the exception-specification. */
17715 if (member_p || cp_parser_parse_definitely (parser))
17717 cp_cv_quals cv_quals;
17718 cp_virt_specifiers virt_specifiers;
17719 cp_ref_qualifier ref_qual;
17720 tree exception_specification;
17721 tree late_return;
17722 tree attrs;
17723 bool memfn = (member_p || (pushed_scope
17724 && CLASS_TYPE_P (pushed_scope)));
17726 is_declarator = true;
17728 if (ctor_dtor_or_conv_p)
17729 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17730 first = false;
17732 /* Parse the cv-qualifier-seq. */
17733 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17734 /* Parse the ref-qualifier. */
17735 ref_qual = cp_parser_ref_qualifier_opt (parser);
17736 /* And the exception-specification. */
17737 exception_specification
17738 = cp_parser_exception_specification_opt (parser);
17740 attrs = cp_parser_std_attribute_spec_seq (parser);
17742 /* In here, we handle cases where attribute is used after
17743 the function declaration. For example:
17744 void func (int x) __attribute__((vector(..))); */
17745 if (flag_cilkplus
17746 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17748 cp_parser_parse_tentatively (parser);
17749 tree attr = cp_parser_gnu_attributes_opt (parser);
17750 if (cp_lexer_next_token_is_not (parser->lexer,
17751 CPP_SEMICOLON)
17752 && cp_lexer_next_token_is_not (parser->lexer,
17753 CPP_OPEN_BRACE))
17754 cp_parser_abort_tentative_parse (parser);
17755 else if (!cp_parser_parse_definitely (parser))
17757 else
17758 attrs = chainon (attr, attrs);
17760 late_return = (cp_parser_late_return_type_opt
17761 (parser, declarator,
17762 memfn ? cv_quals : -1));
17765 /* Parse the virt-specifier-seq. */
17766 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17768 /* Create the function-declarator. */
17769 declarator = make_call_declarator (declarator,
17770 params,
17771 cv_quals,
17772 virt_specifiers,
17773 ref_qual,
17774 exception_specification,
17775 late_return);
17776 declarator->std_attributes = attrs;
17777 /* Any subsequent parameter lists are to do with
17778 return type, so are not those of the declared
17779 function. */
17780 parser->default_arg_ok_p = false;
17783 /* Remove the function parms from scope. */
17784 pop_bindings_and_leave_scope ();
17786 if (is_declarator)
17787 /* Repeat the main loop. */
17788 continue;
17791 /* If this is the first, we can try a parenthesized
17792 declarator. */
17793 if (first)
17795 bool saved_in_type_id_in_expr_p;
17797 parser->default_arg_ok_p = saved_default_arg_ok_p;
17798 parser->in_declarator_p = saved_in_declarator_p;
17800 /* Consume the `('. */
17801 cp_lexer_consume_token (parser->lexer);
17802 /* Parse the nested declarator. */
17803 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17804 parser->in_type_id_in_expr_p = true;
17805 declarator
17806 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17807 /*parenthesized_p=*/NULL,
17808 member_p, friend_p);
17809 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17810 first = false;
17811 /* Expect a `)'. */
17812 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17813 declarator = cp_error_declarator;
17814 if (declarator == cp_error_declarator)
17815 break;
17817 goto handle_declarator;
17819 /* Otherwise, we must be done. */
17820 else
17821 break;
17823 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17824 && token->type == CPP_OPEN_SQUARE
17825 && !cp_next_tokens_can_be_attribute_p (parser))
17827 /* Parse an array-declarator. */
17828 tree bounds, attrs;
17830 if (ctor_dtor_or_conv_p)
17831 *ctor_dtor_or_conv_p = 0;
17833 first = false;
17834 parser->default_arg_ok_p = false;
17835 parser->in_declarator_p = true;
17836 /* Consume the `['. */
17837 cp_lexer_consume_token (parser->lexer);
17838 /* Peek at the next token. */
17839 token = cp_lexer_peek_token (parser->lexer);
17840 /* If the next token is `]', then there is no
17841 constant-expression. */
17842 if (token->type != CPP_CLOSE_SQUARE)
17844 bool non_constant_p;
17845 bounds
17846 = cp_parser_constant_expression (parser,
17847 /*allow_non_constant=*/true,
17848 &non_constant_p);
17849 if (!non_constant_p)
17850 /* OK */;
17851 else if (error_operand_p (bounds))
17852 /* Already gave an error. */;
17853 else if (!parser->in_function_body
17854 || current_binding_level->kind == sk_function_parms)
17856 /* Normally, the array bound must be an integral constant
17857 expression. However, as an extension, we allow VLAs
17858 in function scopes as long as they aren't part of a
17859 parameter declaration. */
17860 cp_parser_error (parser,
17861 "array bound is not an integer constant");
17862 bounds = error_mark_node;
17864 else if (processing_template_decl
17865 && !type_dependent_expression_p (bounds))
17867 /* Remember this wasn't a constant-expression. */
17868 bounds = build_nop (TREE_TYPE (bounds), bounds);
17869 TREE_SIDE_EFFECTS (bounds) = 1;
17872 else
17873 bounds = NULL_TREE;
17874 /* Look for the closing `]'. */
17875 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17877 declarator = cp_error_declarator;
17878 break;
17881 attrs = cp_parser_std_attribute_spec_seq (parser);
17882 declarator = make_array_declarator (declarator, bounds);
17883 declarator->std_attributes = attrs;
17885 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17888 tree qualifying_scope;
17889 tree unqualified_name;
17890 tree attrs;
17891 special_function_kind sfk;
17892 bool abstract_ok;
17893 bool pack_expansion_p = false;
17894 cp_token *declarator_id_start_token;
17896 /* Parse a declarator-id */
17897 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17898 if (abstract_ok)
17900 cp_parser_parse_tentatively (parser);
17902 /* If we see an ellipsis, we should be looking at a
17903 parameter pack. */
17904 if (token->type == CPP_ELLIPSIS)
17906 /* Consume the `...' */
17907 cp_lexer_consume_token (parser->lexer);
17909 pack_expansion_p = true;
17913 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17914 unqualified_name
17915 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17916 qualifying_scope = parser->scope;
17917 if (abstract_ok)
17919 bool okay = false;
17921 if (!unqualified_name && pack_expansion_p)
17923 /* Check whether an error occurred. */
17924 okay = !cp_parser_error_occurred (parser);
17926 /* We already consumed the ellipsis to mark a
17927 parameter pack, but we have no way to report it,
17928 so abort the tentative parse. We will be exiting
17929 immediately anyway. */
17930 cp_parser_abort_tentative_parse (parser);
17932 else
17933 okay = cp_parser_parse_definitely (parser);
17935 if (!okay)
17936 unqualified_name = error_mark_node;
17937 else if (unqualified_name
17938 && (qualifying_scope
17939 || (!identifier_p (unqualified_name))))
17941 cp_parser_error (parser, "expected unqualified-id");
17942 unqualified_name = error_mark_node;
17946 if (!unqualified_name)
17947 return NULL;
17948 if (unqualified_name == error_mark_node)
17950 declarator = cp_error_declarator;
17951 pack_expansion_p = false;
17952 declarator->parameter_pack_p = false;
17953 break;
17956 attrs = cp_parser_std_attribute_spec_seq (parser);
17958 if (qualifying_scope && at_namespace_scope_p ()
17959 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17961 /* In the declaration of a member of a template class
17962 outside of the class itself, the SCOPE will sometimes
17963 be a TYPENAME_TYPE. For example, given:
17965 template <typename T>
17966 int S<T>::R::i = 3;
17968 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17969 this context, we must resolve S<T>::R to an ordinary
17970 type, rather than a typename type.
17972 The reason we normally avoid resolving TYPENAME_TYPEs
17973 is that a specialization of `S' might render
17974 `S<T>::R' not a type. However, if `S' is
17975 specialized, then this `i' will not be used, so there
17976 is no harm in resolving the types here. */
17977 tree type;
17979 /* Resolve the TYPENAME_TYPE. */
17980 type = resolve_typename_type (qualifying_scope,
17981 /*only_current_p=*/false);
17982 /* If that failed, the declarator is invalid. */
17983 if (TREE_CODE (type) == TYPENAME_TYPE)
17985 if (typedef_variant_p (type))
17986 error_at (declarator_id_start_token->location,
17987 "cannot define member of dependent typedef "
17988 "%qT", type);
17989 else
17990 error_at (declarator_id_start_token->location,
17991 "%<%T::%E%> is not a type",
17992 TYPE_CONTEXT (qualifying_scope),
17993 TYPE_IDENTIFIER (qualifying_scope));
17995 qualifying_scope = type;
17998 sfk = sfk_none;
18000 if (unqualified_name)
18002 tree class_type;
18004 if (qualifying_scope
18005 && CLASS_TYPE_P (qualifying_scope))
18006 class_type = qualifying_scope;
18007 else
18008 class_type = current_class_type;
18010 if (TREE_CODE (unqualified_name) == TYPE_DECL)
18012 tree name_type = TREE_TYPE (unqualified_name);
18013 if (class_type && same_type_p (name_type, class_type))
18015 if (qualifying_scope
18016 && CLASSTYPE_USE_TEMPLATE (name_type))
18018 error_at (declarator_id_start_token->location,
18019 "invalid use of constructor as a template");
18020 inform (declarator_id_start_token->location,
18021 "use %<%T::%D%> instead of %<%T::%D%> to "
18022 "name the constructor in a qualified name",
18023 class_type,
18024 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
18025 class_type, name_type);
18026 declarator = cp_error_declarator;
18027 break;
18029 else
18030 unqualified_name = constructor_name (class_type);
18032 else
18034 /* We do not attempt to print the declarator
18035 here because we do not have enough
18036 information about its original syntactic
18037 form. */
18038 cp_parser_error (parser, "invalid declarator");
18039 declarator = cp_error_declarator;
18040 break;
18044 if (class_type)
18046 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
18047 sfk = sfk_destructor;
18048 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
18049 sfk = sfk_conversion;
18050 else if (/* There's no way to declare a constructor
18051 for an anonymous type, even if the type
18052 got a name for linkage purposes. */
18053 !TYPE_WAS_ANONYMOUS (class_type)
18054 /* Handle correctly (c++/19200):
18056 struct S {
18057 struct T{};
18058 friend void S(T);
18061 and also:
18063 namespace N {
18064 void S();
18067 struct S {
18068 friend void N::S();
18069 }; */
18070 && !(friend_p
18071 && class_type != qualifying_scope)
18072 && constructor_name_p (unqualified_name,
18073 class_type))
18075 unqualified_name = constructor_name (class_type);
18076 sfk = sfk_constructor;
18078 else if (is_overloaded_fn (unqualified_name)
18079 && DECL_CONSTRUCTOR_P (get_first_fn
18080 (unqualified_name)))
18081 sfk = sfk_constructor;
18083 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18084 *ctor_dtor_or_conv_p = -1;
18087 declarator = make_id_declarator (qualifying_scope,
18088 unqualified_name,
18089 sfk);
18090 declarator->std_attributes = attrs;
18091 declarator->id_loc = token->location;
18092 declarator->parameter_pack_p = pack_expansion_p;
18094 if (pack_expansion_p)
18095 maybe_warn_variadic_templates ();
18098 handle_declarator:;
18099 scope = get_scope_of_declarator (declarator);
18100 if (scope)
18102 /* Any names that appear after the declarator-id for a
18103 member are looked up in the containing scope. */
18104 if (at_function_scope_p ())
18106 /* But declarations with qualified-ids can't appear in a
18107 function. */
18108 cp_parser_error (parser, "qualified-id in declaration");
18109 declarator = cp_error_declarator;
18110 break;
18112 pushed_scope = push_scope (scope);
18114 parser->in_declarator_p = true;
18115 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18116 || (declarator && declarator->kind == cdk_id))
18117 /* Default args are only allowed on function
18118 declarations. */
18119 parser->default_arg_ok_p = saved_default_arg_ok_p;
18120 else
18121 parser->default_arg_ok_p = false;
18123 first = false;
18125 /* We're done. */
18126 else
18127 break;
18130 /* For an abstract declarator, we might wind up with nothing at this
18131 point. That's an error; the declarator is not optional. */
18132 if (!declarator)
18133 cp_parser_error (parser, "expected declarator");
18135 /* If we entered a scope, we must exit it now. */
18136 if (pushed_scope)
18137 pop_scope (pushed_scope);
18139 parser->default_arg_ok_p = saved_default_arg_ok_p;
18140 parser->in_declarator_p = saved_in_declarator_p;
18142 return declarator;
18145 /* Parse a ptr-operator.
18147 ptr-operator:
18148 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18149 * cv-qualifier-seq [opt]
18151 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18152 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18154 GNU Extension:
18156 ptr-operator:
18157 & cv-qualifier-seq [opt]
18159 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18160 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18161 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18162 filled in with the TYPE containing the member. *CV_QUALS is
18163 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18164 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18165 Note that the tree codes returned by this function have nothing
18166 to do with the types of trees that will be eventually be created
18167 to represent the pointer or reference type being parsed. They are
18168 just constants with suggestive names. */
18169 static enum tree_code
18170 cp_parser_ptr_operator (cp_parser* parser,
18171 tree* type,
18172 cp_cv_quals *cv_quals,
18173 tree *attributes)
18175 enum tree_code code = ERROR_MARK;
18176 cp_token *token;
18177 tree attrs = NULL_TREE;
18179 /* Assume that it's not a pointer-to-member. */
18180 *type = NULL_TREE;
18181 /* And that there are no cv-qualifiers. */
18182 *cv_quals = TYPE_UNQUALIFIED;
18184 /* Peek at the next token. */
18185 token = cp_lexer_peek_token (parser->lexer);
18187 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18188 if (token->type == CPP_MULT)
18189 code = INDIRECT_REF;
18190 else if (token->type == CPP_AND)
18191 code = ADDR_EXPR;
18192 else if ((cxx_dialect != cxx98) &&
18193 token->type == CPP_AND_AND) /* C++0x only */
18194 code = NON_LVALUE_EXPR;
18196 if (code != ERROR_MARK)
18198 /* Consume the `*', `&' or `&&'. */
18199 cp_lexer_consume_token (parser->lexer);
18201 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18202 `&', if we are allowing GNU extensions. (The only qualifier
18203 that can legally appear after `&' is `restrict', but that is
18204 enforced during semantic analysis. */
18205 if (code == INDIRECT_REF
18206 || cp_parser_allow_gnu_extensions_p (parser))
18207 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18209 attrs = cp_parser_std_attribute_spec_seq (parser);
18210 if (attributes != NULL)
18211 *attributes = attrs;
18213 else
18215 /* Try the pointer-to-member case. */
18216 cp_parser_parse_tentatively (parser);
18217 /* Look for the optional `::' operator. */
18218 cp_parser_global_scope_opt (parser,
18219 /*current_scope_valid_p=*/false);
18220 /* Look for the nested-name specifier. */
18221 token = cp_lexer_peek_token (parser->lexer);
18222 cp_parser_nested_name_specifier (parser,
18223 /*typename_keyword_p=*/false,
18224 /*check_dependency_p=*/true,
18225 /*type_p=*/false,
18226 /*is_declaration=*/false);
18227 /* If we found it, and the next token is a `*', then we are
18228 indeed looking at a pointer-to-member operator. */
18229 if (!cp_parser_error_occurred (parser)
18230 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18232 /* Indicate that the `*' operator was used. */
18233 code = INDIRECT_REF;
18235 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18236 error_at (token->location, "%qD is a namespace", parser->scope);
18237 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18238 error_at (token->location, "cannot form pointer to member of "
18239 "non-class %q#T", parser->scope);
18240 else
18242 /* The type of which the member is a member is given by the
18243 current SCOPE. */
18244 *type = parser->scope;
18245 /* The next name will not be qualified. */
18246 parser->scope = NULL_TREE;
18247 parser->qualifying_scope = NULL_TREE;
18248 parser->object_scope = NULL_TREE;
18249 /* Look for optional c++11 attributes. */
18250 attrs = cp_parser_std_attribute_spec_seq (parser);
18251 if (attributes != NULL)
18252 *attributes = attrs;
18253 /* Look for the optional cv-qualifier-seq. */
18254 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18257 /* If that didn't work we don't have a ptr-operator. */
18258 if (!cp_parser_parse_definitely (parser))
18259 cp_parser_error (parser, "expected ptr-operator");
18262 return code;
18265 /* Parse an (optional) cv-qualifier-seq.
18267 cv-qualifier-seq:
18268 cv-qualifier cv-qualifier-seq [opt]
18270 cv-qualifier:
18271 const
18272 volatile
18274 GNU Extension:
18276 cv-qualifier:
18277 __restrict__
18279 Returns a bitmask representing the cv-qualifiers. */
18281 static cp_cv_quals
18282 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18284 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18286 while (true)
18288 cp_token *token;
18289 cp_cv_quals cv_qualifier;
18291 /* Peek at the next token. */
18292 token = cp_lexer_peek_token (parser->lexer);
18293 /* See if it's a cv-qualifier. */
18294 switch (token->keyword)
18296 case RID_CONST:
18297 cv_qualifier = TYPE_QUAL_CONST;
18298 break;
18300 case RID_VOLATILE:
18301 cv_qualifier = TYPE_QUAL_VOLATILE;
18302 break;
18304 case RID_RESTRICT:
18305 cv_qualifier = TYPE_QUAL_RESTRICT;
18306 break;
18308 default:
18309 cv_qualifier = TYPE_UNQUALIFIED;
18310 break;
18313 if (!cv_qualifier)
18314 break;
18316 if (cv_quals & cv_qualifier)
18318 error_at (token->location, "duplicate cv-qualifier");
18319 cp_lexer_purge_token (parser->lexer);
18321 else
18323 cp_lexer_consume_token (parser->lexer);
18324 cv_quals |= cv_qualifier;
18328 return cv_quals;
18331 /* Parse an (optional) ref-qualifier
18333 ref-qualifier:
18337 Returns cp_ref_qualifier representing ref-qualifier. */
18339 static cp_ref_qualifier
18340 cp_parser_ref_qualifier_opt (cp_parser* parser)
18342 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18344 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18345 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18346 return ref_qual;
18348 while (true)
18350 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18351 cp_token *token = cp_lexer_peek_token (parser->lexer);
18353 switch (token->type)
18355 case CPP_AND:
18356 curr_ref_qual = REF_QUAL_LVALUE;
18357 break;
18359 case CPP_AND_AND:
18360 curr_ref_qual = REF_QUAL_RVALUE;
18361 break;
18363 default:
18364 curr_ref_qual = REF_QUAL_NONE;
18365 break;
18368 if (!curr_ref_qual)
18369 break;
18370 else if (ref_qual)
18372 error_at (token->location, "multiple ref-qualifiers");
18373 cp_lexer_purge_token (parser->lexer);
18375 else
18377 ref_qual = curr_ref_qual;
18378 cp_lexer_consume_token (parser->lexer);
18382 return ref_qual;
18385 /* Parse an (optional) virt-specifier-seq.
18387 virt-specifier-seq:
18388 virt-specifier virt-specifier-seq [opt]
18390 virt-specifier:
18391 override
18392 final
18394 Returns a bitmask representing the virt-specifiers. */
18396 static cp_virt_specifiers
18397 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18399 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18401 while (true)
18403 cp_token *token;
18404 cp_virt_specifiers virt_specifier;
18406 /* Peek at the next token. */
18407 token = cp_lexer_peek_token (parser->lexer);
18408 /* See if it's a virt-specifier-qualifier. */
18409 if (token->type != CPP_NAME)
18410 break;
18411 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18413 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18414 virt_specifier = VIRT_SPEC_OVERRIDE;
18416 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18418 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18419 virt_specifier = VIRT_SPEC_FINAL;
18421 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18423 virt_specifier = VIRT_SPEC_FINAL;
18425 else
18426 break;
18428 if (virt_specifiers & virt_specifier)
18430 error_at (token->location, "duplicate virt-specifier");
18431 cp_lexer_purge_token (parser->lexer);
18433 else
18435 cp_lexer_consume_token (parser->lexer);
18436 virt_specifiers |= virt_specifier;
18439 return virt_specifiers;
18442 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18443 is in scope even though it isn't real. */
18445 void
18446 inject_this_parameter (tree ctype, cp_cv_quals quals)
18448 tree this_parm;
18450 if (current_class_ptr)
18452 /* We don't clear this between NSDMIs. Is it already what we want? */
18453 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18454 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18455 && cp_type_quals (type) == quals)
18456 return;
18459 this_parm = build_this_parm (ctype, quals);
18460 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18461 current_class_ptr = NULL_TREE;
18462 current_class_ref
18463 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18464 current_class_ptr = this_parm;
18467 /* Return true iff our current scope is a non-static data member
18468 initializer. */
18470 bool
18471 parsing_nsdmi (void)
18473 /* We recognize NSDMI context by the context-less 'this' pointer set up
18474 by the function above. */
18475 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18476 return true;
18477 return false;
18480 /* Parse a late-specified return type, if any. This is not a separate
18481 non-terminal, but part of a function declarator, which looks like
18483 -> trailing-type-specifier-seq abstract-declarator(opt)
18485 Returns the type indicated by the type-id.
18487 In addition to this this parses any queued up omp declare simd
18488 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18490 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18491 function. */
18493 static tree
18494 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18495 cp_cv_quals quals)
18497 cp_token *token;
18498 tree type = NULL_TREE;
18499 bool declare_simd_p = (parser->omp_declare_simd
18500 && declarator
18501 && declarator->kind == cdk_id);
18503 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18504 && declarator && declarator->kind == cdk_id);
18506 /* Peek at the next token. */
18507 token = cp_lexer_peek_token (parser->lexer);
18508 /* A late-specified return type is indicated by an initial '->'. */
18509 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18510 return NULL_TREE;
18512 tree save_ccp = current_class_ptr;
18513 tree save_ccr = current_class_ref;
18514 if (quals >= 0)
18516 /* DR 1207: 'this' is in scope in the trailing return type. */
18517 inject_this_parameter (current_class_type, quals);
18520 if (token->type == CPP_DEREF)
18522 /* Consume the ->. */
18523 cp_lexer_consume_token (parser->lexer);
18524 type = cp_parser_trailing_type_id (parser);
18527 if (cilk_simd_fn_vector_p)
18528 declarator->std_attributes
18529 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18530 declarator->std_attributes);
18531 if (declare_simd_p)
18532 declarator->std_attributes
18533 = cp_parser_late_parsing_omp_declare_simd (parser,
18534 declarator->std_attributes);
18536 if (quals >= 0)
18538 current_class_ptr = save_ccp;
18539 current_class_ref = save_ccr;
18542 return type;
18545 /* Parse a declarator-id.
18547 declarator-id:
18548 id-expression
18549 :: [opt] nested-name-specifier [opt] type-name
18551 In the `id-expression' case, the value returned is as for
18552 cp_parser_id_expression if the id-expression was an unqualified-id.
18553 If the id-expression was a qualified-id, then a SCOPE_REF is
18554 returned. The first operand is the scope (either a NAMESPACE_DECL
18555 or TREE_TYPE), but the second is still just a representation of an
18556 unqualified-id. */
18558 static tree
18559 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18561 tree id;
18562 /* The expression must be an id-expression. Assume that qualified
18563 names are the names of types so that:
18565 template <class T>
18566 int S<T>::R::i = 3;
18568 will work; we must treat `S<T>::R' as the name of a type.
18569 Similarly, assume that qualified names are templates, where
18570 required, so that:
18572 template <class T>
18573 int S<T>::R<T>::i = 3;
18575 will work, too. */
18576 id = cp_parser_id_expression (parser,
18577 /*template_keyword_p=*/false,
18578 /*check_dependency_p=*/false,
18579 /*template_p=*/NULL,
18580 /*declarator_p=*/true,
18581 optional_p);
18582 if (id && BASELINK_P (id))
18583 id = BASELINK_FUNCTIONS (id);
18584 return id;
18587 /* Parse a type-id.
18589 type-id:
18590 type-specifier-seq abstract-declarator [opt]
18592 Returns the TYPE specified. */
18594 static tree
18595 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18596 bool is_trailing_return)
18598 cp_decl_specifier_seq type_specifier_seq;
18599 cp_declarator *abstract_declarator;
18601 /* Parse the type-specifier-seq. */
18602 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18603 is_trailing_return,
18604 &type_specifier_seq);
18605 if (type_specifier_seq.type == error_mark_node)
18606 return error_mark_node;
18608 /* There might or might not be an abstract declarator. */
18609 cp_parser_parse_tentatively (parser);
18610 /* Look for the declarator. */
18611 abstract_declarator
18612 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18613 /*parenthesized_p=*/NULL,
18614 /*member_p=*/false,
18615 /*friend_p=*/false);
18616 /* Check to see if there really was a declarator. */
18617 if (!cp_parser_parse_definitely (parser))
18618 abstract_declarator = NULL;
18620 if (type_specifier_seq.type
18621 /* None of the valid uses of 'auto' in C++14 involve the type-id
18622 nonterminal, but it is valid in a trailing-return-type. */
18623 && !(cxx_dialect >= cxx1y && is_trailing_return)
18624 && type_uses_auto (type_specifier_seq.type))
18626 /* A type-id with type 'auto' is only ok if the abstract declarator
18627 is a function declarator with a late-specified return type.
18629 A type-id with 'auto' is also valid in a trailing-return-type
18630 in a compound-requirment. */
18631 if (abstract_declarator
18632 && abstract_declarator->kind == cdk_function
18633 && abstract_declarator->u.function.late_return_type)
18634 /* OK */;
18635 else if (parser->in_result_type_constraint_p)
18636 /* OK */;
18637 else
18639 error ("invalid use of %<auto%>");
18640 return error_mark_node;
18644 return groktypename (&type_specifier_seq, abstract_declarator,
18645 is_template_arg);
18648 static tree cp_parser_type_id (cp_parser *parser)
18650 return cp_parser_type_id_1 (parser, false, false);
18653 static tree cp_parser_template_type_arg (cp_parser *parser)
18655 tree r;
18656 const char *saved_message = parser->type_definition_forbidden_message;
18657 parser->type_definition_forbidden_message
18658 = G_("types may not be defined in template arguments");
18659 r = cp_parser_type_id_1 (parser, true, false);
18660 parser->type_definition_forbidden_message = saved_message;
18661 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18663 error ("invalid use of %<auto%> in template argument");
18664 r = error_mark_node;
18666 return r;
18669 static tree cp_parser_trailing_type_id (cp_parser *parser)
18671 return cp_parser_type_id_1 (parser, false, true);
18674 /* Parse a type-specifier-seq.
18676 type-specifier-seq:
18677 type-specifier type-specifier-seq [opt]
18679 GNU extension:
18681 type-specifier-seq:
18682 attributes type-specifier-seq [opt]
18684 If IS_DECLARATION is true, we are at the start of a "condition" or
18685 exception-declaration, so we might be followed by a declarator-id.
18687 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18688 i.e. we've just seen "->".
18690 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18692 static void
18693 cp_parser_type_specifier_seq (cp_parser* parser,
18694 bool is_declaration,
18695 bool is_trailing_return,
18696 cp_decl_specifier_seq *type_specifier_seq)
18698 bool seen_type_specifier = false;
18699 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18700 cp_token *start_token = NULL;
18702 /* Clear the TYPE_SPECIFIER_SEQ. */
18703 clear_decl_specs (type_specifier_seq);
18705 /* In the context of a trailing return type, enum E { } is an
18706 elaborated-type-specifier followed by a function-body, not an
18707 enum-specifier. */
18708 if (is_trailing_return)
18709 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18711 /* Parse the type-specifiers and attributes. */
18712 while (true)
18714 tree type_specifier;
18715 bool is_cv_qualifier;
18717 /* Check for attributes first. */
18718 if (cp_next_tokens_can_be_attribute_p (parser))
18720 type_specifier_seq->attributes =
18721 chainon (type_specifier_seq->attributes,
18722 cp_parser_attributes_opt (parser));
18723 continue;
18726 /* record the token of the beginning of the type specifier seq,
18727 for error reporting purposes*/
18728 if (!start_token)
18729 start_token = cp_lexer_peek_token (parser->lexer);
18731 /* Look for the type-specifier. */
18732 type_specifier = cp_parser_type_specifier (parser,
18733 flags,
18734 type_specifier_seq,
18735 /*is_declaration=*/false,
18736 NULL,
18737 &is_cv_qualifier);
18738 if (!type_specifier)
18740 /* If the first type-specifier could not be found, this is not a
18741 type-specifier-seq at all. */
18742 if (!seen_type_specifier)
18744 /* Set in_declarator_p to avoid skipping to the semicolon. */
18745 int in_decl = parser->in_declarator_p;
18746 parser->in_declarator_p = true;
18748 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18749 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18750 cp_parser_error (parser, "expected type-specifier");
18752 parser->in_declarator_p = in_decl;
18754 type_specifier_seq->type = error_mark_node;
18755 return;
18757 /* If subsequent type-specifiers could not be found, the
18758 type-specifier-seq is complete. */
18759 break;
18762 seen_type_specifier = true;
18763 /* The standard says that a condition can be:
18765 type-specifier-seq declarator = assignment-expression
18767 However, given:
18769 struct S {};
18770 if (int S = ...)
18772 we should treat the "S" as a declarator, not as a
18773 type-specifier. The standard doesn't say that explicitly for
18774 type-specifier-seq, but it does say that for
18775 decl-specifier-seq in an ordinary declaration. Perhaps it
18776 would be clearer just to allow a decl-specifier-seq here, and
18777 then add a semantic restriction that if any decl-specifiers
18778 that are not type-specifiers appear, the program is invalid. */
18779 if (is_declaration && !is_cv_qualifier)
18780 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18784 /* Return whether the function currently being declared has an associated
18785 template parameter list. */
18787 static bool
18788 function_being_declared_is_template_p (cp_parser* parser)
18790 if (!current_template_parms || processing_template_parmlist)
18791 return false;
18793 if (parser->implicit_template_scope)
18794 return true;
18796 if (at_class_scope_p ()
18797 && TYPE_BEING_DEFINED (current_class_type))
18798 return parser->num_template_parameter_lists != 0;
18800 return ((int) parser->num_template_parameter_lists > template_class_depth
18801 (current_class_type));
18804 /* Parse a parameter-declaration-clause.
18806 parameter-declaration-clause:
18807 parameter-declaration-list [opt] ... [opt]
18808 parameter-declaration-list , ...
18810 Returns a representation for the parameter declarations. A return
18811 value of NULL indicates a parameter-declaration-clause consisting
18812 only of an ellipsis. */
18814 static tree
18815 cp_parser_parameter_declaration_clause (cp_parser* parser)
18817 tree parameters;
18818 cp_token *token;
18819 bool ellipsis_p;
18820 bool is_error;
18822 struct cleanup {
18823 cp_parser* parser;
18824 int auto_is_implicit_function_template_parm_p;
18825 ~cleanup() {
18826 parser->auto_is_implicit_function_template_parm_p
18827 = auto_is_implicit_function_template_parm_p;
18829 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18831 (void) cleanup;
18833 if (!processing_specialization
18834 && !processing_template_parmlist
18835 && !processing_explicit_instantiation)
18836 if (!current_function_decl
18837 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18838 parser->auto_is_implicit_function_template_parm_p = true;
18840 /* Peek at the next token. */
18841 token = cp_lexer_peek_token (parser->lexer);
18842 /* Check for trivial parameter-declaration-clauses. */
18843 if (token->type == CPP_ELLIPSIS)
18845 /* Consume the `...' token. */
18846 cp_lexer_consume_token (parser->lexer);
18847 return NULL_TREE;
18849 else if (token->type == CPP_CLOSE_PAREN)
18850 /* There are no parameters. */
18852 #ifndef NO_IMPLICIT_EXTERN_C
18853 if (in_system_header_at (input_location)
18854 && current_class_type == NULL
18855 && current_lang_name == lang_name_c)
18856 return NULL_TREE;
18857 else
18858 #endif
18859 return void_list_node;
18861 /* Check for `(void)', too, which is a special case. */
18862 else if (token->keyword == RID_VOID
18863 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18864 == CPP_CLOSE_PAREN))
18866 /* Consume the `void' token. */
18867 cp_lexer_consume_token (parser->lexer);
18868 /* There are no parameters. */
18869 return void_list_node;
18872 /* Parse the parameter-declaration-list. */
18873 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18874 /* If a parse error occurred while parsing the
18875 parameter-declaration-list, then the entire
18876 parameter-declaration-clause is erroneous. */
18877 if (is_error)
18878 return NULL;
18880 /* Peek at the next token. */
18881 token = cp_lexer_peek_token (parser->lexer);
18882 /* If it's a `,', the clause should terminate with an ellipsis. */
18883 if (token->type == CPP_COMMA)
18885 /* Consume the `,'. */
18886 cp_lexer_consume_token (parser->lexer);
18887 /* Expect an ellipsis. */
18888 ellipsis_p
18889 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18891 /* It might also be `...' if the optional trailing `,' was
18892 omitted. */
18893 else if (token->type == CPP_ELLIPSIS)
18895 /* Consume the `...' token. */
18896 cp_lexer_consume_token (parser->lexer);
18897 /* And remember that we saw it. */
18898 ellipsis_p = true;
18900 else
18901 ellipsis_p = false;
18903 /* Finish the parameter list. */
18904 if (!ellipsis_p)
18905 parameters = chainon (parameters, void_list_node);
18907 return parameters;
18910 /* Parse a parameter-declaration-list.
18912 parameter-declaration-list:
18913 parameter-declaration
18914 parameter-declaration-list , parameter-declaration
18916 Returns a representation of the parameter-declaration-list, as for
18917 cp_parser_parameter_declaration_clause. However, the
18918 `void_list_node' is never appended to the list. Upon return,
18919 *IS_ERROR will be true iff an error occurred. */
18921 static tree
18922 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18924 tree parameters = NULL_TREE;
18925 tree *tail = &parameters;
18926 bool saved_in_unbraced_linkage_specification_p;
18927 int index = 0;
18929 /* Assume all will go well. */
18930 *is_error = false;
18931 /* The special considerations that apply to a function within an
18932 unbraced linkage specifications do not apply to the parameters
18933 to the function. */
18934 saved_in_unbraced_linkage_specification_p
18935 = parser->in_unbraced_linkage_specification_p;
18936 parser->in_unbraced_linkage_specification_p = false;
18938 /* Look for more parameters. */
18939 while (true)
18941 cp_parameter_declarator *parameter;
18942 tree decl = error_mark_node;
18943 bool parenthesized_p = false;
18944 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18945 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18946 (current_template_parms)) : 0);
18948 /* Parse the parameter. */
18949 parameter
18950 = cp_parser_parameter_declaration (parser,
18951 /*template_parm_p=*/false,
18952 &parenthesized_p);
18954 /* We don't know yet if the enclosing context is deprecated, so wait
18955 and warn in grokparms if appropriate. */
18956 deprecated_state = DEPRECATED_SUPPRESS;
18958 if (parameter)
18960 /* If a function parameter pack was specified and an implicit template
18961 parameter was introduced during cp_parser_parameter_declaration,
18962 change any implicit parameters introduced into packs. */
18963 if (parser->implicit_template_parms
18964 && parameter->declarator
18965 && parameter->declarator->parameter_pack_p)
18967 int latest_template_parm_idx = TREE_VEC_LENGTH
18968 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18970 if (latest_template_parm_idx != template_parm_idx)
18971 parameter->decl_specifiers.type = convert_generic_types_to_packs
18972 (parameter->decl_specifiers.type,
18973 template_parm_idx, latest_template_parm_idx);
18976 decl = grokdeclarator (parameter->declarator,
18977 &parameter->decl_specifiers,
18978 PARM,
18979 parameter->default_argument != NULL_TREE,
18980 &parameter->decl_specifiers.attributes);
18983 deprecated_state = DEPRECATED_NORMAL;
18985 /* If a parse error occurred parsing the parameter declaration,
18986 then the entire parameter-declaration-list is erroneous. */
18987 if (decl == error_mark_node)
18989 *is_error = true;
18990 parameters = error_mark_node;
18991 break;
18994 if (parameter->decl_specifiers.attributes)
18995 cplus_decl_attributes (&decl,
18996 parameter->decl_specifiers.attributes,
18998 if (DECL_NAME (decl))
18999 decl = pushdecl (decl);
19001 if (decl != error_mark_node)
19003 retrofit_lang_decl (decl);
19004 DECL_PARM_INDEX (decl) = ++index;
19005 DECL_PARM_LEVEL (decl) = function_parm_depth ();
19008 /* Add the new parameter to the list. */
19009 *tail = build_tree_list (parameter->default_argument, decl);
19010 tail = &TREE_CHAIN (*tail);
19012 /* Peek at the next token. */
19013 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
19014 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
19015 /* These are for Objective-C++ */
19016 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19017 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19018 /* The parameter-declaration-list is complete. */
19019 break;
19020 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19022 cp_token *token;
19024 /* Peek at the next token. */
19025 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19026 /* If it's an ellipsis, then the list is complete. */
19027 if (token->type == CPP_ELLIPSIS)
19028 break;
19029 /* Otherwise, there must be more parameters. Consume the
19030 `,'. */
19031 cp_lexer_consume_token (parser->lexer);
19032 /* When parsing something like:
19034 int i(float f, double d)
19036 we can tell after seeing the declaration for "f" that we
19037 are not looking at an initialization of a variable "i",
19038 but rather at the declaration of a function "i".
19040 Due to the fact that the parsing of template arguments
19041 (as specified to a template-id) requires backtracking we
19042 cannot use this technique when inside a template argument
19043 list. */
19044 if (!parser->in_template_argument_list_p
19045 && !parser->in_type_id_in_expr_p
19046 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19047 /* However, a parameter-declaration of the form
19048 "float(f)" (which is a valid declaration of a
19049 parameter "f") can also be interpreted as an
19050 expression (the conversion of "f" to "float"). */
19051 && !parenthesized_p)
19052 cp_parser_commit_to_tentative_parse (parser);
19054 else
19056 cp_parser_error (parser, "expected %<,%> or %<...%>");
19057 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19058 cp_parser_skip_to_closing_parenthesis (parser,
19059 /*recovering=*/true,
19060 /*or_comma=*/false,
19061 /*consume_paren=*/false);
19062 break;
19066 parser->in_unbraced_linkage_specification_p
19067 = saved_in_unbraced_linkage_specification_p;
19069 /* Reset implicit_template_scope if we are about to leave the function
19070 parameter list that introduced it. Note that for out-of-line member
19071 definitions, there will be one or more class scopes before we get to
19072 the template parameter scope. */
19074 if (cp_binding_level *its = parser->implicit_template_scope)
19075 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
19077 while (maybe_its->kind == sk_class)
19078 maybe_its = maybe_its->level_chain;
19079 if (maybe_its == its)
19081 parser->implicit_template_parms = 0;
19082 parser->implicit_template_scope = 0;
19086 return parameters;
19089 /* Parse a parameter declaration.
19091 parameter-declaration:
19092 decl-specifier-seq ... [opt] declarator
19093 decl-specifier-seq declarator = assignment-expression
19094 decl-specifier-seq ... [opt] abstract-declarator [opt]
19095 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19097 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19098 declares a template parameter. (In that case, a non-nested `>'
19099 token encountered during the parsing of the assignment-expression
19100 is not interpreted as a greater-than operator.)
19102 Returns a representation of the parameter, or NULL if an error
19103 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19104 true iff the declarator is of the form "(p)". */
19106 static cp_parameter_declarator *
19107 cp_parser_parameter_declaration (cp_parser *parser,
19108 bool template_parm_p,
19109 bool *parenthesized_p)
19111 int declares_class_or_enum;
19112 cp_decl_specifier_seq decl_specifiers;
19113 cp_declarator *declarator;
19114 tree default_argument;
19115 cp_token *token = NULL, *declarator_token_start = NULL;
19116 const char *saved_message;
19118 /* In a template parameter, `>' is not an operator.
19120 [temp.param]
19122 When parsing a default template-argument for a non-type
19123 template-parameter, the first non-nested `>' is taken as the end
19124 of the template parameter-list rather than a greater-than
19125 operator. */
19127 /* Type definitions may not appear in parameter types. */
19128 saved_message = parser->type_definition_forbidden_message;
19129 parser->type_definition_forbidden_message
19130 = G_("types may not be defined in parameter types");
19132 /* Parse the declaration-specifiers. */
19133 cp_parser_decl_specifier_seq (parser,
19134 CP_PARSER_FLAGS_NONE,
19135 &decl_specifiers,
19136 &declares_class_or_enum);
19138 /* Complain about missing 'typename' or other invalid type names. */
19139 if (!decl_specifiers.any_type_specifiers_p
19140 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19141 decl_specifiers.type = error_mark_node;
19143 /* If an error occurred, there's no reason to attempt to parse the
19144 rest of the declaration. */
19145 if (cp_parser_error_occurred (parser))
19147 parser->type_definition_forbidden_message = saved_message;
19148 return NULL;
19151 /* Peek at the next token. */
19152 token = cp_lexer_peek_token (parser->lexer);
19154 /* If the next token is a `)', `,', `=', `>', or `...', then there
19155 is no declarator. However, when variadic templates are enabled,
19156 there may be a declarator following `...'. */
19157 if (token->type == CPP_CLOSE_PAREN
19158 || token->type == CPP_COMMA
19159 || token->type == CPP_EQ
19160 || token->type == CPP_GREATER)
19162 declarator = NULL;
19163 if (parenthesized_p)
19164 *parenthesized_p = false;
19166 /* Otherwise, there should be a declarator. */
19167 else
19169 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19170 parser->default_arg_ok_p = false;
19172 /* After seeing a decl-specifier-seq, if the next token is not a
19173 "(", there is no possibility that the code is a valid
19174 expression. Therefore, if parsing tentatively, we commit at
19175 this point. */
19176 if (!parser->in_template_argument_list_p
19177 /* In an expression context, having seen:
19179 (int((char ...
19181 we cannot be sure whether we are looking at a
19182 function-type (taking a "char" as a parameter) or a cast
19183 of some object of type "char" to "int". */
19184 && !parser->in_type_id_in_expr_p
19185 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19186 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19187 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19188 cp_parser_commit_to_tentative_parse (parser);
19189 /* Parse the declarator. */
19190 declarator_token_start = token;
19191 declarator = cp_parser_declarator (parser,
19192 CP_PARSER_DECLARATOR_EITHER,
19193 /*ctor_dtor_or_conv_p=*/NULL,
19194 parenthesized_p,
19195 /*member_p=*/false,
19196 /*friend_p=*/false);
19197 parser->default_arg_ok_p = saved_default_arg_ok_p;
19198 /* After the declarator, allow more attributes. */
19199 decl_specifiers.attributes
19200 = chainon (decl_specifiers.attributes,
19201 cp_parser_attributes_opt (parser));
19204 /* If the next token is an ellipsis, and we have not seen a
19205 declarator name, and the type of the declarator contains parameter
19206 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19207 a parameter pack expansion expression. Otherwise, leave the
19208 ellipsis for a C-style variadic function. */
19209 token = cp_lexer_peek_token (parser->lexer);
19210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19212 tree type = decl_specifiers.type;
19214 if (type && DECL_P (type))
19215 type = TREE_TYPE (type);
19217 if (type
19218 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19219 && declarator_can_be_parameter_pack (declarator)
19220 && (!declarator || !declarator->parameter_pack_p)
19221 && uses_parameter_packs (type))
19223 /* Consume the `...'. */
19224 cp_lexer_consume_token (parser->lexer);
19225 maybe_warn_variadic_templates ();
19227 /* Build a pack expansion type */
19228 if (declarator)
19229 declarator->parameter_pack_p = true;
19230 else
19231 decl_specifiers.type = make_pack_expansion (type);
19235 /* The restriction on defining new types applies only to the type
19236 of the parameter, not to the default argument. */
19237 parser->type_definition_forbidden_message = saved_message;
19239 /* If the next token is `=', then process a default argument. */
19240 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19242 token = cp_lexer_peek_token (parser->lexer);
19243 /* If we are defining a class, then the tokens that make up the
19244 default argument must be saved and processed later. */
19245 if (!template_parm_p && at_class_scope_p ()
19246 && TYPE_BEING_DEFINED (current_class_type)
19247 && !LAMBDA_TYPE_P (current_class_type))
19248 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19249 /* Outside of a class definition, we can just parse the
19250 assignment-expression. */
19251 else
19252 default_argument
19253 = cp_parser_default_argument (parser, template_parm_p);
19255 if (!parser->default_arg_ok_p)
19257 if (flag_permissive)
19258 warning (0, "deprecated use of default argument for parameter of non-function");
19259 else
19261 error_at (token->location,
19262 "default arguments are only "
19263 "permitted for function parameters");
19264 default_argument = NULL_TREE;
19267 else if ((declarator && declarator->parameter_pack_p)
19268 || (decl_specifiers.type
19269 && PACK_EXPANSION_P (decl_specifiers.type)))
19271 /* Find the name of the parameter pack. */
19272 cp_declarator *id_declarator = declarator;
19273 while (id_declarator && id_declarator->kind != cdk_id)
19274 id_declarator = id_declarator->declarator;
19276 if (id_declarator && id_declarator->kind == cdk_id)
19277 error_at (declarator_token_start->location,
19278 template_parm_p
19279 ? G_("template parameter pack %qD "
19280 "cannot have a default argument")
19281 : G_("parameter pack %qD cannot have "
19282 "a default argument"),
19283 id_declarator->u.id.unqualified_name);
19284 else
19285 error_at (declarator_token_start->location,
19286 template_parm_p
19287 ? G_("template parameter pack cannot have "
19288 "a default argument")
19289 : G_("parameter pack cannot have a "
19290 "default argument"));
19292 default_argument = NULL_TREE;
19295 else
19296 default_argument = NULL_TREE;
19298 return make_parameter_declarator (&decl_specifiers,
19299 declarator,
19300 default_argument);
19303 /* Parse a default argument and return it.
19305 TEMPLATE_PARM_P is true if this is a default argument for a
19306 non-type template parameter. */
19307 static tree
19308 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19310 tree default_argument = NULL_TREE;
19311 bool saved_greater_than_is_operator_p;
19312 bool saved_local_variables_forbidden_p;
19313 bool non_constant_p, is_direct_init;
19315 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19316 set correctly. */
19317 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19318 parser->greater_than_is_operator_p = !template_parm_p;
19319 /* Local variable names (and the `this' keyword) may not
19320 appear in a default argument. */
19321 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19322 parser->local_variables_forbidden_p = true;
19323 /* Parse the assignment-expression. */
19324 if (template_parm_p)
19325 push_deferring_access_checks (dk_no_deferred);
19326 tree saved_class_ptr = NULL_TREE;
19327 tree saved_class_ref = NULL_TREE;
19328 /* The "this" pointer is not valid in a default argument. */
19329 if (cfun)
19331 saved_class_ptr = current_class_ptr;
19332 cp_function_chain->x_current_class_ptr = NULL_TREE;
19333 saved_class_ref = current_class_ref;
19334 cp_function_chain->x_current_class_ref = NULL_TREE;
19336 default_argument
19337 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19338 /* Restore the "this" pointer. */
19339 if (cfun)
19341 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19342 cp_function_chain->x_current_class_ref = saved_class_ref;
19344 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19345 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19346 if (template_parm_p)
19347 pop_deferring_access_checks ();
19348 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19349 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19351 return default_argument;
19354 /* Parse a function-body.
19356 function-body:
19357 compound_statement */
19359 static void
19360 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19362 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19365 /* Parse a ctor-initializer-opt followed by a function-body. Return
19366 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19367 is true we are parsing a function-try-block. */
19369 static bool
19370 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19371 bool in_function_try_block)
19373 tree body, list;
19374 bool ctor_initializer_p;
19375 const bool check_body_p =
19376 DECL_CONSTRUCTOR_P (current_function_decl)
19377 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19378 tree last = NULL;
19380 /* Begin the function body. */
19381 body = begin_function_body ();
19382 /* Parse the optional ctor-initializer. */
19383 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19385 /* If we're parsing a constexpr constructor definition, we need
19386 to check that the constructor body is indeed empty. However,
19387 before we get to cp_parser_function_body lot of junk has been
19388 generated, so we can't just check that we have an empty block.
19389 Rather we take a snapshot of the outermost block, and check whether
19390 cp_parser_function_body changed its state. */
19391 if (check_body_p)
19393 list = cur_stmt_list;
19394 if (STATEMENT_LIST_TAIL (list))
19395 last = STATEMENT_LIST_TAIL (list)->stmt;
19397 /* Parse the function-body. */
19398 cp_parser_function_body (parser, in_function_try_block);
19399 if (check_body_p)
19400 check_constexpr_ctor_body (last, list);
19401 /* Finish the function body. */
19402 finish_function_body (body);
19404 return ctor_initializer_p;
19407 /* Parse an initializer.
19409 initializer:
19410 = initializer-clause
19411 ( expression-list )
19413 Returns an expression representing the initializer. If no
19414 initializer is present, NULL_TREE is returned.
19416 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19417 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19418 set to TRUE if there is no initializer present. If there is an
19419 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19420 is set to true; otherwise it is set to false. */
19422 static tree
19423 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19424 bool* non_constant_p)
19426 cp_token *token;
19427 tree init;
19429 /* Peek at the next token. */
19430 token = cp_lexer_peek_token (parser->lexer);
19432 /* Let our caller know whether or not this initializer was
19433 parenthesized. */
19434 *is_direct_init = (token->type != CPP_EQ);
19435 /* Assume that the initializer is constant. */
19436 *non_constant_p = false;
19438 if (token->type == CPP_EQ)
19440 /* Consume the `='. */
19441 cp_lexer_consume_token (parser->lexer);
19442 /* Parse the initializer-clause. */
19443 init = cp_parser_initializer_clause (parser, non_constant_p);
19445 else if (token->type == CPP_OPEN_PAREN)
19447 vec<tree, va_gc> *vec;
19448 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19449 /*cast_p=*/false,
19450 /*allow_expansion_p=*/true,
19451 non_constant_p);
19452 if (vec == NULL)
19453 return error_mark_node;
19454 init = build_tree_list_vec (vec);
19455 release_tree_vector (vec);
19457 else if (token->type == CPP_OPEN_BRACE)
19459 cp_lexer_set_source_position (parser->lexer);
19460 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19461 init = cp_parser_braced_list (parser, non_constant_p);
19462 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19464 else
19466 /* Anything else is an error. */
19467 cp_parser_error (parser, "expected initializer");
19468 init = error_mark_node;
19471 return init;
19474 /* Parse an initializer-clause.
19476 initializer-clause:
19477 assignment-expression
19478 braced-init-list
19480 Returns an expression representing the initializer.
19482 If the `assignment-expression' production is used the value
19483 returned is simply a representation for the expression.
19485 Otherwise, calls cp_parser_braced_list. */
19487 static tree
19488 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19490 tree initializer;
19492 /* Assume the expression is constant. */
19493 *non_constant_p = false;
19495 /* If it is not a `{', then we are looking at an
19496 assignment-expression. */
19497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19499 initializer
19500 = cp_parser_constant_expression (parser,
19501 /*allow_non_constant_p=*/true,
19502 non_constant_p);
19504 else
19505 initializer = cp_parser_braced_list (parser, non_constant_p);
19507 return initializer;
19510 /* Parse a brace-enclosed initializer list.
19512 braced-init-list:
19513 { initializer-list , [opt] }
19516 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19517 the elements of the initializer-list (or NULL, if the last
19518 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19519 NULL_TREE. There is no way to detect whether or not the optional
19520 trailing `,' was provided. NON_CONSTANT_P is as for
19521 cp_parser_initializer. */
19523 static tree
19524 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19526 tree initializer;
19528 /* Consume the `{' token. */
19529 cp_lexer_consume_token (parser->lexer);
19530 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19531 initializer = make_node (CONSTRUCTOR);
19532 /* If it's not a `}', then there is a non-trivial initializer. */
19533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19535 /* Parse the initializer list. */
19536 CONSTRUCTOR_ELTS (initializer)
19537 = cp_parser_initializer_list (parser, non_constant_p);
19538 /* A trailing `,' token is allowed. */
19539 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19540 cp_lexer_consume_token (parser->lexer);
19542 else
19543 *non_constant_p = false;
19544 /* Now, there should be a trailing `}'. */
19545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19546 TREE_TYPE (initializer) = init_list_type_node;
19547 return initializer;
19550 /* Parse an initializer-list.
19552 initializer-list:
19553 initializer-clause ... [opt]
19554 initializer-list , initializer-clause ... [opt]
19556 GNU Extension:
19558 initializer-list:
19559 designation initializer-clause ...[opt]
19560 initializer-list , designation initializer-clause ...[opt]
19562 designation:
19563 . identifier =
19564 identifier :
19565 [ constant-expression ] =
19567 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19568 for the initializer. If the INDEX of the elt is non-NULL, it is the
19569 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19570 as for cp_parser_initializer. */
19572 static vec<constructor_elt, va_gc> *
19573 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19575 vec<constructor_elt, va_gc> *v = NULL;
19577 /* Assume all of the expressions are constant. */
19578 *non_constant_p = false;
19580 /* Parse the rest of the list. */
19581 while (true)
19583 cp_token *token;
19584 tree designator;
19585 tree initializer;
19586 bool clause_non_constant_p;
19588 /* If the next token is an identifier and the following one is a
19589 colon, we are looking at the GNU designated-initializer
19590 syntax. */
19591 if (cp_parser_allow_gnu_extensions_p (parser)
19592 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19593 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19595 /* Warn the user that they are using an extension. */
19596 pedwarn (input_location, OPT_Wpedantic,
19597 "ISO C++ does not allow designated initializers");
19598 /* Consume the identifier. */
19599 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19600 /* Consume the `:'. */
19601 cp_lexer_consume_token (parser->lexer);
19603 /* Also handle the C99 syntax, '. id ='. */
19604 else if (cp_parser_allow_gnu_extensions_p (parser)
19605 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19606 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19607 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19609 /* Warn the user that they are using an extension. */
19610 pedwarn (input_location, OPT_Wpedantic,
19611 "ISO C++ does not allow C99 designated initializers");
19612 /* Consume the `.'. */
19613 cp_lexer_consume_token (parser->lexer);
19614 /* Consume the identifier. */
19615 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19616 /* Consume the `='. */
19617 cp_lexer_consume_token (parser->lexer);
19619 /* Also handle C99 array designators, '[ const ] ='. */
19620 else if (cp_parser_allow_gnu_extensions_p (parser)
19621 && !c_dialect_objc ()
19622 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19624 /* In C++11, [ could start a lambda-introducer. */
19625 bool non_const = false;
19627 cp_parser_parse_tentatively (parser);
19628 cp_lexer_consume_token (parser->lexer);
19629 designator = cp_parser_constant_expression (parser, true, &non_const);
19630 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19631 cp_parser_require (parser, CPP_EQ, RT_EQ);
19632 if (!cp_parser_parse_definitely (parser))
19633 designator = NULL_TREE;
19634 else if (non_const)
19635 require_potential_rvalue_constant_expression (designator);
19637 else
19638 designator = NULL_TREE;
19640 /* Parse the initializer. */
19641 initializer = cp_parser_initializer_clause (parser,
19642 &clause_non_constant_p);
19643 /* If any clause is non-constant, so is the entire initializer. */
19644 if (clause_non_constant_p)
19645 *non_constant_p = true;
19647 /* If we have an ellipsis, this is an initializer pack
19648 expansion. */
19649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19651 /* Consume the `...'. */
19652 cp_lexer_consume_token (parser->lexer);
19654 /* Turn the initializer into an initializer expansion. */
19655 initializer = make_pack_expansion (initializer);
19658 /* Add it to the vector. */
19659 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19661 /* If the next token is not a comma, we have reached the end of
19662 the list. */
19663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19664 break;
19666 /* Peek at the next token. */
19667 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19668 /* If the next token is a `}', then we're still done. An
19669 initializer-clause can have a trailing `,' after the
19670 initializer-list and before the closing `}'. */
19671 if (token->type == CPP_CLOSE_BRACE)
19672 break;
19674 /* Consume the `,' token. */
19675 cp_lexer_consume_token (parser->lexer);
19678 return v;
19681 /* Classes [gram.class] */
19683 /* Parse a class-name.
19685 class-name:
19686 identifier
19687 template-id
19689 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19690 to indicate that names looked up in dependent types should be
19691 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19692 keyword has been used to indicate that the name that appears next
19693 is a template. TAG_TYPE indicates the explicit tag given before
19694 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19695 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19696 is the class being defined in a class-head.
19698 Returns the TYPE_DECL representing the class. */
19700 static tree
19701 cp_parser_class_name (cp_parser *parser,
19702 bool typename_keyword_p,
19703 bool template_keyword_p,
19704 enum tag_types tag_type,
19705 bool check_dependency_p,
19706 bool class_head_p,
19707 bool is_declaration)
19709 tree decl;
19710 tree scope;
19711 bool typename_p;
19712 cp_token *token;
19713 tree identifier = NULL_TREE;
19715 /* All class-names start with an identifier. */
19716 token = cp_lexer_peek_token (parser->lexer);
19717 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19719 cp_parser_error (parser, "expected class-name");
19720 return error_mark_node;
19723 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19724 to a template-id, so we save it here. */
19725 scope = parser->scope;
19726 if (scope == error_mark_node)
19727 return error_mark_node;
19729 /* Any name names a type if we're following the `typename' keyword
19730 in a qualified name where the enclosing scope is type-dependent. */
19731 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19732 && dependent_type_p (scope));
19733 /* Handle the common case (an identifier, but not a template-id)
19734 efficiently. */
19735 if (token->type == CPP_NAME
19736 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19738 cp_token *identifier_token;
19739 bool ambiguous_p;
19741 /* Look for the identifier. */
19742 identifier_token = cp_lexer_peek_token (parser->lexer);
19743 ambiguous_p = identifier_token->error_reported;
19744 identifier = cp_parser_identifier (parser);
19745 /* If the next token isn't an identifier, we are certainly not
19746 looking at a class-name. */
19747 if (identifier == error_mark_node)
19748 decl = error_mark_node;
19749 /* If we know this is a type-name, there's no need to look it
19750 up. */
19751 else if (typename_p)
19752 decl = identifier;
19753 else
19755 tree ambiguous_decls;
19756 /* If we already know that this lookup is ambiguous, then
19757 we've already issued an error message; there's no reason
19758 to check again. */
19759 if (ambiguous_p)
19761 cp_parser_simulate_error (parser);
19762 return error_mark_node;
19764 /* If the next token is a `::', then the name must be a type
19765 name.
19767 [basic.lookup.qual]
19769 During the lookup for a name preceding the :: scope
19770 resolution operator, object, function, and enumerator
19771 names are ignored. */
19772 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19773 tag_type = typename_type;
19774 /* Look up the name. */
19775 decl = cp_parser_lookup_name (parser, identifier,
19776 tag_type,
19777 /*is_template=*/false,
19778 /*is_namespace=*/false,
19779 check_dependency_p,
19780 &ambiguous_decls,
19781 identifier_token->location);
19782 if (ambiguous_decls)
19784 if (cp_parser_parsing_tentatively (parser))
19785 cp_parser_simulate_error (parser);
19786 return error_mark_node;
19790 else
19792 /* Try a template-id. */
19793 decl = cp_parser_template_id (parser, template_keyword_p,
19794 check_dependency_p,
19795 tag_type,
19796 is_declaration);
19797 if (decl == error_mark_node)
19798 return error_mark_node;
19801 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19803 /* If this is a typename, create a TYPENAME_TYPE. */
19804 if (typename_p && decl != error_mark_node)
19806 decl = make_typename_type (scope, decl, typename_type,
19807 /*complain=*/tf_error);
19808 if (decl != error_mark_node)
19809 decl = TYPE_NAME (decl);
19812 decl = strip_using_decl (decl);
19814 /* Check to see that it is really the name of a class. */
19815 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19816 && identifier_p (TREE_OPERAND (decl, 0))
19817 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19818 /* Situations like this:
19820 template <typename T> struct A {
19821 typename T::template X<int>::I i;
19824 are problematic. Is `T::template X<int>' a class-name? The
19825 standard does not seem to be definitive, but there is no other
19826 valid interpretation of the following `::'. Therefore, those
19827 names are considered class-names. */
19829 decl = make_typename_type (scope, decl, tag_type, tf_error);
19830 if (decl != error_mark_node)
19831 decl = TYPE_NAME (decl);
19833 else if (TREE_CODE (decl) != TYPE_DECL
19834 || TREE_TYPE (decl) == error_mark_node
19835 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19836 /* In Objective-C 2.0, a classname followed by '.' starts a
19837 dot-syntax expression, and it's not a type-name. */
19838 || (c_dialect_objc ()
19839 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19840 && objc_is_class_name (decl)))
19841 decl = error_mark_node;
19843 if (decl == error_mark_node)
19844 cp_parser_error (parser, "expected class-name");
19845 else if (identifier && !parser->scope)
19846 maybe_note_name_used_in_class (identifier, decl);
19848 return decl;
19851 /* Parse a class-specifier.
19853 class-specifier:
19854 class-head { member-specification [opt] }
19856 Returns the TREE_TYPE representing the class. */
19858 static tree
19859 cp_parser_class_specifier_1 (cp_parser* parser)
19861 tree type;
19862 tree attributes = NULL_TREE;
19863 bool nested_name_specifier_p;
19864 unsigned saved_num_template_parameter_lists;
19865 bool saved_in_function_body;
19866 unsigned char in_statement;
19867 bool in_switch_statement_p;
19868 bool saved_in_unbraced_linkage_specification_p;
19869 tree old_scope = NULL_TREE;
19870 tree scope = NULL_TREE;
19871 cp_token *closing_brace;
19873 push_deferring_access_checks (dk_no_deferred);
19875 /* Parse the class-head. */
19876 type = cp_parser_class_head (parser,
19877 &nested_name_specifier_p);
19878 /* If the class-head was a semantic disaster, skip the entire body
19879 of the class. */
19880 if (!type)
19882 cp_parser_skip_to_end_of_block_or_statement (parser);
19883 pop_deferring_access_checks ();
19884 return error_mark_node;
19887 /* Look for the `{'. */
19888 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19890 pop_deferring_access_checks ();
19891 return error_mark_node;
19894 cp_ensure_no_omp_declare_simd (parser);
19896 /* Issue an error message if type-definitions are forbidden here. */
19897 cp_parser_check_type_definition (parser);
19898 /* Remember that we are defining one more class. */
19899 ++parser->num_classes_being_defined;
19900 /* Inside the class, surrounding template-parameter-lists do not
19901 apply. */
19902 saved_num_template_parameter_lists
19903 = parser->num_template_parameter_lists;
19904 parser->num_template_parameter_lists = 0;
19905 /* We are not in a function body. */
19906 saved_in_function_body = parser->in_function_body;
19907 parser->in_function_body = false;
19908 /* Or in a loop. */
19909 in_statement = parser->in_statement;
19910 parser->in_statement = 0;
19911 /* Or in a switch. */
19912 in_switch_statement_p = parser->in_switch_statement_p;
19913 parser->in_switch_statement_p = false;
19914 /* We are not immediately inside an extern "lang" block. */
19915 saved_in_unbraced_linkage_specification_p
19916 = parser->in_unbraced_linkage_specification_p;
19917 parser->in_unbraced_linkage_specification_p = false;
19919 /* Start the class. */
19920 if (nested_name_specifier_p)
19922 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19923 old_scope = push_inner_scope (scope);
19925 type = begin_class_definition (type);
19927 if (type == error_mark_node)
19928 /* If the type is erroneous, skip the entire body of the class. */
19929 cp_parser_skip_to_closing_brace (parser);
19930 else
19931 /* Parse the member-specification. */
19932 cp_parser_member_specification_opt (parser);
19934 /* Look for the trailing `}'. */
19935 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19936 /* Look for trailing attributes to apply to this class. */
19937 if (cp_parser_allow_gnu_extensions_p (parser))
19938 attributes = cp_parser_gnu_attributes_opt (parser);
19939 if (type != error_mark_node)
19940 type = finish_struct (type, attributes);
19941 if (nested_name_specifier_p)
19942 pop_inner_scope (old_scope, scope);
19944 /* We've finished a type definition. Check for the common syntax
19945 error of forgetting a semicolon after the definition. We need to
19946 be careful, as we can't just check for not-a-semicolon and be done
19947 with it; the user might have typed:
19949 class X { } c = ...;
19950 class X { } *p = ...;
19952 and so forth. Instead, enumerate all the possible tokens that
19953 might follow this production; if we don't see one of them, then
19954 complain and silently insert the semicolon. */
19956 cp_token *token = cp_lexer_peek_token (parser->lexer);
19957 bool want_semicolon = true;
19959 if (cp_next_tokens_can_be_std_attribute_p (parser))
19960 /* Don't try to parse c++11 attributes here. As per the
19961 grammar, that should be a task for
19962 cp_parser_decl_specifier_seq. */
19963 want_semicolon = false;
19965 switch (token->type)
19967 case CPP_NAME:
19968 case CPP_SEMICOLON:
19969 case CPP_MULT:
19970 case CPP_AND:
19971 case CPP_OPEN_PAREN:
19972 case CPP_CLOSE_PAREN:
19973 case CPP_COMMA:
19974 want_semicolon = false;
19975 break;
19977 /* While it's legal for type qualifiers and storage class
19978 specifiers to follow type definitions in the grammar, only
19979 compiler testsuites contain code like that. Assume that if
19980 we see such code, then what we're really seeing is a case
19981 like:
19983 class X { }
19984 const <type> var = ...;
19988 class Y { }
19989 static <type> func (...) ...
19991 i.e. the qualifier or specifier applies to the next
19992 declaration. To do so, however, we need to look ahead one
19993 more token to see if *that* token is a type specifier.
19995 This code could be improved to handle:
19997 class Z { }
19998 static const <type> var = ...; */
19999 case CPP_KEYWORD:
20000 if (keyword_is_decl_specifier (token->keyword))
20002 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
20004 /* Handling user-defined types here would be nice, but very
20005 tricky. */
20006 want_semicolon
20007 = (lookahead->type == CPP_KEYWORD
20008 && keyword_begins_type_specifier (lookahead->keyword));
20010 break;
20011 default:
20012 break;
20015 /* If we don't have a type, then something is very wrong and we
20016 shouldn't try to do anything clever. Likewise for not seeing the
20017 closing brace. */
20018 if (closing_brace && TYPE_P (type) && want_semicolon)
20020 cp_token_position prev
20021 = cp_lexer_previous_token_position (parser->lexer);
20022 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20023 location_t loc = prev_token->location;
20025 if (CLASSTYPE_DECLARED_CLASS (type))
20026 error_at (loc, "expected %<;%> after class definition");
20027 else if (TREE_CODE (type) == RECORD_TYPE)
20028 error_at (loc, "expected %<;%> after struct definition");
20029 else if (TREE_CODE (type) == UNION_TYPE)
20030 error_at (loc, "expected %<;%> after union definition");
20031 else
20032 gcc_unreachable ();
20034 /* Unget one token and smash it to look as though we encountered
20035 a semicolon in the input stream. */
20036 cp_lexer_set_token_position (parser->lexer, prev);
20037 token = cp_lexer_peek_token (parser->lexer);
20038 token->type = CPP_SEMICOLON;
20039 token->keyword = RID_MAX;
20043 /* If this class is not itself within the scope of another class,
20044 then we need to parse the bodies of all of the queued function
20045 definitions. Note that the queued functions defined in a class
20046 are not always processed immediately following the
20047 class-specifier for that class. Consider:
20049 struct A {
20050 struct B { void f() { sizeof (A); } };
20053 If `f' were processed before the processing of `A' were
20054 completed, there would be no way to compute the size of `A'.
20055 Note that the nesting we are interested in here is lexical --
20056 not the semantic nesting given by TYPE_CONTEXT. In particular,
20057 for:
20059 struct A { struct B; };
20060 struct A::B { void f() { } };
20062 there is no need to delay the parsing of `A::B::f'. */
20063 if (--parser->num_classes_being_defined == 0)
20065 tree decl;
20066 tree class_type = NULL_TREE;
20067 tree pushed_scope = NULL_TREE;
20068 unsigned ix;
20069 cp_default_arg_entry *e;
20070 tree save_ccp, save_ccr;
20072 /* In a first pass, parse default arguments to the functions.
20073 Then, in a second pass, parse the bodies of the functions.
20074 This two-phased approach handles cases like:
20076 struct S {
20077 void f() { g(); }
20078 void g(int i = 3);
20082 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20084 decl = e->decl;
20085 /* If there are default arguments that have not yet been processed,
20086 take care of them now. */
20087 if (class_type != e->class_type)
20089 if (pushed_scope)
20090 pop_scope (pushed_scope);
20091 class_type = e->class_type;
20092 pushed_scope = push_scope (class_type);
20094 /* Make sure that any template parameters are in scope. */
20095 maybe_begin_member_template_processing (decl);
20096 /* Parse the default argument expressions. */
20097 cp_parser_late_parsing_default_args (parser, decl);
20098 /* Remove any template parameters from the symbol table. */
20099 maybe_end_member_template_processing ();
20101 vec_safe_truncate (unparsed_funs_with_default_args, 0);
20102 /* Now parse any NSDMIs. */
20103 save_ccp = current_class_ptr;
20104 save_ccr = current_class_ref;
20105 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20107 if (class_type != DECL_CONTEXT (decl))
20109 if (pushed_scope)
20110 pop_scope (pushed_scope);
20111 class_type = DECL_CONTEXT (decl);
20112 pushed_scope = push_scope (class_type);
20114 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20115 cp_parser_late_parsing_nsdmi (parser, decl);
20117 vec_safe_truncate (unparsed_nsdmis, 0);
20118 current_class_ptr = save_ccp;
20119 current_class_ref = save_ccr;
20120 if (pushed_scope)
20121 pop_scope (pushed_scope);
20123 /* Now do some post-NSDMI bookkeeping. */
20124 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20125 after_nsdmi_defaulted_late_checks (class_type);
20126 vec_safe_truncate (unparsed_classes, 0);
20127 after_nsdmi_defaulted_late_checks (type);
20129 /* Now parse the body of the functions. */
20130 if (flag_openmp)
20132 /* OpenMP UDRs need to be parsed before all other functions. */
20133 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20134 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20135 cp_parser_late_parsing_for_member (parser, decl);
20136 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20137 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20138 cp_parser_late_parsing_for_member (parser, decl);
20140 else
20141 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20142 cp_parser_late_parsing_for_member (parser, decl);
20143 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20145 else
20146 vec_safe_push (unparsed_classes, type);
20148 /* Put back any saved access checks. */
20149 pop_deferring_access_checks ();
20151 /* Restore saved state. */
20152 parser->in_switch_statement_p = in_switch_statement_p;
20153 parser->in_statement = in_statement;
20154 parser->in_function_body = saved_in_function_body;
20155 parser->num_template_parameter_lists
20156 = saved_num_template_parameter_lists;
20157 parser->in_unbraced_linkage_specification_p
20158 = saved_in_unbraced_linkage_specification_p;
20160 return type;
20163 static tree
20164 cp_parser_class_specifier (cp_parser* parser)
20166 tree ret;
20167 timevar_push (TV_PARSE_STRUCT);
20168 ret = cp_parser_class_specifier_1 (parser);
20169 timevar_pop (TV_PARSE_STRUCT);
20170 return ret;
20173 /* Parse a class-head.
20175 class-head:
20176 class-key identifier [opt] base-clause [opt]
20177 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20178 class-key nested-name-specifier [opt] template-id
20179 base-clause [opt]
20181 class-virt-specifier:
20182 final
20184 GNU Extensions:
20185 class-key attributes identifier [opt] base-clause [opt]
20186 class-key attributes nested-name-specifier identifier base-clause [opt]
20187 class-key attributes nested-name-specifier [opt] template-id
20188 base-clause [opt]
20190 Upon return BASES is initialized to the list of base classes (or
20191 NULL, if there are none) in the same form returned by
20192 cp_parser_base_clause.
20194 Returns the TYPE of the indicated class. Sets
20195 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20196 involving a nested-name-specifier was used, and FALSE otherwise.
20198 Returns error_mark_node if this is not a class-head.
20200 Returns NULL_TREE if the class-head is syntactically valid, but
20201 semantically invalid in a way that means we should skip the entire
20202 body of the class. */
20204 static tree
20205 cp_parser_class_head (cp_parser* parser,
20206 bool* nested_name_specifier_p)
20208 tree nested_name_specifier;
20209 enum tag_types class_key;
20210 tree id = NULL_TREE;
20211 tree type = NULL_TREE;
20212 tree attributes;
20213 tree bases;
20214 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20215 bool template_id_p = false;
20216 bool qualified_p = false;
20217 bool invalid_nested_name_p = false;
20218 bool invalid_explicit_specialization_p = false;
20219 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20220 tree pushed_scope = NULL_TREE;
20221 unsigned num_templates;
20222 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20223 /* Assume no nested-name-specifier will be present. */
20224 *nested_name_specifier_p = false;
20225 /* Assume no template parameter lists will be used in defining the
20226 type. */
20227 num_templates = 0;
20228 parser->colon_corrects_to_scope_p = false;
20230 /* Look for the class-key. */
20231 class_key = cp_parser_class_key (parser);
20232 if (class_key == none_type)
20233 return error_mark_node;
20235 /* Parse the attributes. */
20236 attributes = cp_parser_attributes_opt (parser);
20238 /* If the next token is `::', that is invalid -- but sometimes
20239 people do try to write:
20241 struct ::S {};
20243 Handle this gracefully by accepting the extra qualifier, and then
20244 issuing an error about it later if this really is a
20245 class-head. If it turns out just to be an elaborated type
20246 specifier, remain silent. */
20247 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20248 qualified_p = true;
20250 push_deferring_access_checks (dk_no_check);
20252 /* Determine the name of the class. Begin by looking for an
20253 optional nested-name-specifier. */
20254 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20255 nested_name_specifier
20256 = cp_parser_nested_name_specifier_opt (parser,
20257 /*typename_keyword_p=*/false,
20258 /*check_dependency_p=*/false,
20259 /*type_p=*/true,
20260 /*is_declaration=*/false);
20261 /* If there was a nested-name-specifier, then there *must* be an
20262 identifier. */
20263 if (nested_name_specifier)
20265 type_start_token = cp_lexer_peek_token (parser->lexer);
20266 /* Although the grammar says `identifier', it really means
20267 `class-name' or `template-name'. You are only allowed to
20268 define a class that has already been declared with this
20269 syntax.
20271 The proposed resolution for Core Issue 180 says that wherever
20272 you see `class T::X' you should treat `X' as a type-name.
20274 It is OK to define an inaccessible class; for example:
20276 class A { class B; };
20277 class A::B {};
20279 We do not know if we will see a class-name, or a
20280 template-name. We look for a class-name first, in case the
20281 class-name is a template-id; if we looked for the
20282 template-name first we would stop after the template-name. */
20283 cp_parser_parse_tentatively (parser);
20284 type = cp_parser_class_name (parser,
20285 /*typename_keyword_p=*/false,
20286 /*template_keyword_p=*/false,
20287 class_type,
20288 /*check_dependency_p=*/false,
20289 /*class_head_p=*/true,
20290 /*is_declaration=*/false);
20291 /* If that didn't work, ignore the nested-name-specifier. */
20292 if (!cp_parser_parse_definitely (parser))
20294 invalid_nested_name_p = true;
20295 type_start_token = cp_lexer_peek_token (parser->lexer);
20296 id = cp_parser_identifier (parser);
20297 if (id == error_mark_node)
20298 id = NULL_TREE;
20300 /* If we could not find a corresponding TYPE, treat this
20301 declaration like an unqualified declaration. */
20302 if (type == error_mark_node)
20303 nested_name_specifier = NULL_TREE;
20304 /* Otherwise, count the number of templates used in TYPE and its
20305 containing scopes. */
20306 else
20308 tree scope;
20310 for (scope = TREE_TYPE (type);
20311 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20312 scope = get_containing_scope (scope))
20313 if (TYPE_P (scope)
20314 && CLASS_TYPE_P (scope)
20315 && CLASSTYPE_TEMPLATE_INFO (scope)
20316 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20317 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20318 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20319 ++num_templates;
20322 /* Otherwise, the identifier is optional. */
20323 else
20325 /* We don't know whether what comes next is a template-id,
20326 an identifier, or nothing at all. */
20327 cp_parser_parse_tentatively (parser);
20328 /* Check for a template-id. */
20329 type_start_token = cp_lexer_peek_token (parser->lexer);
20330 id = cp_parser_template_id (parser,
20331 /*template_keyword_p=*/false,
20332 /*check_dependency_p=*/true,
20333 class_key,
20334 /*is_declaration=*/true);
20335 /* If that didn't work, it could still be an identifier. */
20336 if (!cp_parser_parse_definitely (parser))
20338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20340 type_start_token = cp_lexer_peek_token (parser->lexer);
20341 id = cp_parser_identifier (parser);
20343 else
20344 id = NULL_TREE;
20346 else
20348 template_id_p = true;
20349 ++num_templates;
20353 pop_deferring_access_checks ();
20355 if (id)
20357 cp_parser_check_for_invalid_template_id (parser, id,
20358 class_key,
20359 type_start_token->location);
20361 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20363 /* If it's not a `:' or a `{' then we can't really be looking at a
20364 class-head, since a class-head only appears as part of a
20365 class-specifier. We have to detect this situation before calling
20366 xref_tag, since that has irreversible side-effects. */
20367 if (!cp_parser_next_token_starts_class_definition_p (parser))
20369 cp_parser_error (parser, "expected %<{%> or %<:%>");
20370 type = error_mark_node;
20371 goto out;
20374 /* At this point, we're going ahead with the class-specifier, even
20375 if some other problem occurs. */
20376 cp_parser_commit_to_tentative_parse (parser);
20377 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20379 cp_parser_error (parser,
20380 "cannot specify %<override%> for a class");
20381 type = error_mark_node;
20382 goto out;
20384 /* Issue the error about the overly-qualified name now. */
20385 if (qualified_p)
20387 cp_parser_error (parser,
20388 "global qualification of class name is invalid");
20389 type = error_mark_node;
20390 goto out;
20392 else if (invalid_nested_name_p)
20394 cp_parser_error (parser,
20395 "qualified name does not name a class");
20396 type = error_mark_node;
20397 goto out;
20399 else if (nested_name_specifier)
20401 tree scope;
20403 /* Reject typedef-names in class heads. */
20404 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20406 error_at (type_start_token->location,
20407 "invalid class name in declaration of %qD",
20408 type);
20409 type = NULL_TREE;
20410 goto done;
20413 /* Figure out in what scope the declaration is being placed. */
20414 scope = current_scope ();
20415 /* If that scope does not contain the scope in which the
20416 class was originally declared, the program is invalid. */
20417 if (scope && !is_ancestor (scope, nested_name_specifier))
20419 if (at_namespace_scope_p ())
20420 error_at (type_start_token->location,
20421 "declaration of %qD in namespace %qD which does not "
20422 "enclose %qD",
20423 type, scope, nested_name_specifier);
20424 else
20425 error_at (type_start_token->location,
20426 "declaration of %qD in %qD which does not enclose %qD",
20427 type, scope, nested_name_specifier);
20428 type = NULL_TREE;
20429 goto done;
20431 /* [dcl.meaning]
20433 A declarator-id shall not be qualified except for the
20434 definition of a ... nested class outside of its class
20435 ... [or] the definition or explicit instantiation of a
20436 class member of a namespace outside of its namespace. */
20437 if (scope == nested_name_specifier)
20439 permerror (nested_name_specifier_token_start->location,
20440 "extra qualification not allowed");
20441 nested_name_specifier = NULL_TREE;
20442 num_templates = 0;
20445 /* An explicit-specialization must be preceded by "template <>". If
20446 it is not, try to recover gracefully. */
20447 if (at_namespace_scope_p ()
20448 && parser->num_template_parameter_lists == 0
20449 && template_id_p)
20451 error_at (type_start_token->location,
20452 "an explicit specialization must be preceded by %<template <>%>");
20453 invalid_explicit_specialization_p = true;
20454 /* Take the same action that would have been taken by
20455 cp_parser_explicit_specialization. */
20456 ++parser->num_template_parameter_lists;
20457 begin_specialization ();
20459 /* There must be no "return" statements between this point and the
20460 end of this function; set "type "to the correct return value and
20461 use "goto done;" to return. */
20462 /* Make sure that the right number of template parameters were
20463 present. */
20464 if (!cp_parser_check_template_parameters (parser, num_templates,
20465 type_start_token->location,
20466 /*declarator=*/NULL))
20468 /* If something went wrong, there is no point in even trying to
20469 process the class-definition. */
20470 type = NULL_TREE;
20471 goto done;
20474 /* Look up the type. */
20475 if (template_id_p)
20477 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20478 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20479 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20481 error_at (type_start_token->location,
20482 "function template %qD redeclared as a class template", id);
20483 type = error_mark_node;
20485 else
20487 type = TREE_TYPE (id);
20488 type = maybe_process_partial_specialization (type);
20490 if (nested_name_specifier)
20491 pushed_scope = push_scope (nested_name_specifier);
20493 else if (nested_name_specifier)
20495 tree class_type;
20497 /* Given:
20499 template <typename T> struct S { struct T };
20500 template <typename T> struct S<T>::T { };
20502 we will get a TYPENAME_TYPE when processing the definition of
20503 `S::T'. We need to resolve it to the actual type before we
20504 try to define it. */
20505 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20507 class_type = resolve_typename_type (TREE_TYPE (type),
20508 /*only_current_p=*/false);
20509 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20510 type = TYPE_NAME (class_type);
20511 else
20513 cp_parser_error (parser, "could not resolve typename type");
20514 type = error_mark_node;
20518 if (maybe_process_partial_specialization (TREE_TYPE (type))
20519 == error_mark_node)
20521 type = NULL_TREE;
20522 goto done;
20525 class_type = current_class_type;
20526 /* Enter the scope indicated by the nested-name-specifier. */
20527 pushed_scope = push_scope (nested_name_specifier);
20528 /* Get the canonical version of this type. */
20529 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20530 /* Call push_template_decl if it seems like we should be defining a
20531 template either from the template headers or the type we're
20532 defining, so that we diagnose both extra and missing headers. */
20533 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20534 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
20535 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
20536 (TREE_TYPE (type)))))
20537 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20539 type = push_template_decl (type);
20540 if (type == error_mark_node)
20542 type = NULL_TREE;
20543 goto done;
20547 type = TREE_TYPE (type);
20548 *nested_name_specifier_p = true;
20550 else /* The name is not a nested name. */
20552 /* If the class was unnamed, create a dummy name. */
20553 if (!id)
20554 id = make_anon_name ();
20555 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20556 parser->num_template_parameter_lists);
20559 /* Indicate whether this class was declared as a `class' or as a
20560 `struct'. */
20561 if (TREE_CODE (type) == RECORD_TYPE)
20562 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20563 cp_parser_check_class_key (class_key, type);
20565 /* If this type was already complete, and we see another definition,
20566 that's an error. */
20567 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20569 error_at (type_start_token->location, "redefinition of %q#T",
20570 type);
20571 error_at (type_start_token->location, "previous definition of %q+#T",
20572 type);
20573 type = NULL_TREE;
20574 goto done;
20576 else if (type == error_mark_node)
20577 type = NULL_TREE;
20579 if (type)
20581 /* Apply attributes now, before any use of the class as a template
20582 argument in its base list. */
20583 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20584 fixup_attribute_variants (type);
20587 /* We will have entered the scope containing the class; the names of
20588 base classes should be looked up in that context. For example:
20590 struct A { struct B {}; struct C; };
20591 struct A::C : B {};
20593 is valid. */
20595 /* Get the list of base-classes, if there is one. */
20596 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20598 /* PR59482: enter the class scope so that base-specifiers are looked
20599 up correctly. */
20600 if (type)
20601 pushclass (type);
20602 bases = cp_parser_base_clause (parser);
20603 /* PR59482: get out of the previously pushed class scope so that the
20604 subsequent pops pop the right thing. */
20605 if (type)
20606 popclass ();
20608 else
20609 bases = NULL_TREE;
20611 /* If we're really defining a class, process the base classes.
20612 If they're invalid, fail. */
20613 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20614 && !xref_basetypes (type, bases))
20615 type = NULL_TREE;
20617 done:
20618 /* Leave the scope given by the nested-name-specifier. We will
20619 enter the class scope itself while processing the members. */
20620 if (pushed_scope)
20621 pop_scope (pushed_scope);
20623 if (invalid_explicit_specialization_p)
20625 end_specialization ();
20626 --parser->num_template_parameter_lists;
20629 if (type)
20630 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20631 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20632 CLASSTYPE_FINAL (type) = 1;
20633 out:
20634 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20635 return type;
20638 /* Parse a class-key.
20640 class-key:
20641 class
20642 struct
20643 union
20645 Returns the kind of class-key specified, or none_type to indicate
20646 error. */
20648 static enum tag_types
20649 cp_parser_class_key (cp_parser* parser)
20651 cp_token *token;
20652 enum tag_types tag_type;
20654 /* Look for the class-key. */
20655 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20656 if (!token)
20657 return none_type;
20659 /* Check to see if the TOKEN is a class-key. */
20660 tag_type = cp_parser_token_is_class_key (token);
20661 if (!tag_type)
20662 cp_parser_error (parser, "expected class-key");
20663 return tag_type;
20666 /* Parse an (optional) member-specification.
20668 member-specification:
20669 member-declaration member-specification [opt]
20670 access-specifier : member-specification [opt] */
20672 static void
20673 cp_parser_member_specification_opt (cp_parser* parser)
20675 while (true)
20677 cp_token *token;
20678 enum rid keyword;
20680 /* Peek at the next token. */
20681 token = cp_lexer_peek_token (parser->lexer);
20682 /* If it's a `}', or EOF then we've seen all the members. */
20683 if (token->type == CPP_CLOSE_BRACE
20684 || token->type == CPP_EOF
20685 || token->type == CPP_PRAGMA_EOL)
20686 break;
20688 /* See if this token is a keyword. */
20689 keyword = token->keyword;
20690 switch (keyword)
20692 case RID_PUBLIC:
20693 case RID_PROTECTED:
20694 case RID_PRIVATE:
20695 /* Consume the access-specifier. */
20696 cp_lexer_consume_token (parser->lexer);
20697 /* Remember which access-specifier is active. */
20698 current_access_specifier = token->u.value;
20699 /* Look for the `:'. */
20700 cp_parser_require (parser, CPP_COLON, RT_COLON);
20701 break;
20703 default:
20704 /* Accept #pragmas at class scope. */
20705 if (token->type == CPP_PRAGMA)
20707 cp_parser_pragma (parser, pragma_member);
20708 break;
20711 /* Otherwise, the next construction must be a
20712 member-declaration. */
20713 cp_parser_member_declaration (parser);
20718 /* Parse a member-declaration.
20720 member-declaration:
20721 decl-specifier-seq [opt] member-declarator-list [opt] ;
20722 function-definition ; [opt]
20723 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20724 using-declaration
20725 template-declaration
20726 alias-declaration
20728 member-declarator-list:
20729 member-declarator
20730 member-declarator-list , member-declarator
20732 member-declarator:
20733 declarator pure-specifier [opt]
20734 declarator constant-initializer [opt]
20735 identifier [opt] : constant-expression
20737 GNU Extensions:
20739 member-declaration:
20740 __extension__ member-declaration
20742 member-declarator:
20743 declarator attributes [opt] pure-specifier [opt]
20744 declarator attributes [opt] constant-initializer [opt]
20745 identifier [opt] attributes [opt] : constant-expression
20747 C++0x Extensions:
20749 member-declaration:
20750 static_assert-declaration */
20752 static void
20753 cp_parser_member_declaration (cp_parser* parser)
20755 cp_decl_specifier_seq decl_specifiers;
20756 tree prefix_attributes;
20757 tree decl;
20758 int declares_class_or_enum;
20759 bool friend_p;
20760 cp_token *token = NULL;
20761 cp_token *decl_spec_token_start = NULL;
20762 cp_token *initializer_token_start = NULL;
20763 int saved_pedantic;
20764 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20766 /* Check for the `__extension__' keyword. */
20767 if (cp_parser_extension_opt (parser, &saved_pedantic))
20769 /* Recurse. */
20770 cp_parser_member_declaration (parser);
20771 /* Restore the old value of the PEDANTIC flag. */
20772 pedantic = saved_pedantic;
20774 return;
20777 /* Check for a template-declaration. */
20778 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20780 /* An explicit specialization here is an error condition, and we
20781 expect the specialization handler to detect and report this. */
20782 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20783 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20784 cp_parser_explicit_specialization (parser);
20785 else
20786 cp_parser_template_declaration (parser, /*member_p=*/true);
20788 return;
20791 /* Check for a using-declaration. */
20792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20794 if (cxx_dialect < cxx11)
20796 /* Parse the using-declaration. */
20797 cp_parser_using_declaration (parser,
20798 /*access_declaration_p=*/false);
20799 return;
20801 else
20803 tree decl;
20804 bool alias_decl_expected;
20805 cp_parser_parse_tentatively (parser);
20806 decl = cp_parser_alias_declaration (parser);
20807 /* Note that if we actually see the '=' token after the
20808 identifier, cp_parser_alias_declaration commits the
20809 tentative parse. In that case, we really expects an
20810 alias-declaration. Otherwise, we expect a using
20811 declaration. */
20812 alias_decl_expected =
20813 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20814 cp_parser_parse_definitely (parser);
20816 if (alias_decl_expected)
20817 finish_member_declaration (decl);
20818 else
20819 cp_parser_using_declaration (parser,
20820 /*access_declaration_p=*/false);
20821 return;
20825 /* Check for @defs. */
20826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20828 tree ivar, member;
20829 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20830 ivar = ivar_chains;
20831 while (ivar)
20833 member = ivar;
20834 ivar = TREE_CHAIN (member);
20835 TREE_CHAIN (member) = NULL_TREE;
20836 finish_member_declaration (member);
20838 return;
20841 /* If the next token is `static_assert' we have a static assertion. */
20842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20844 cp_parser_static_assert (parser, /*member_p=*/true);
20845 return;
20848 parser->colon_corrects_to_scope_p = false;
20850 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20851 goto out;
20853 /* Parse the decl-specifier-seq. */
20854 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20855 cp_parser_decl_specifier_seq (parser,
20856 CP_PARSER_FLAGS_OPTIONAL,
20857 &decl_specifiers,
20858 &declares_class_or_enum);
20859 /* Check for an invalid type-name. */
20860 if (!decl_specifiers.any_type_specifiers_p
20861 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20862 goto out;
20863 /* If there is no declarator, then the decl-specifier-seq should
20864 specify a type. */
20865 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20867 /* If there was no decl-specifier-seq, and the next token is a
20868 `;', then we have something like:
20870 struct S { ; };
20872 [class.mem]
20874 Each member-declaration shall declare at least one member
20875 name of the class. */
20876 if (!decl_specifiers.any_specifiers_p)
20878 cp_token *token = cp_lexer_peek_token (parser->lexer);
20879 if (!in_system_header_at (token->location))
20880 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20882 else
20884 tree type;
20886 /* See if this declaration is a friend. */
20887 friend_p = cp_parser_friend_p (&decl_specifiers);
20888 /* If there were decl-specifiers, check to see if there was
20889 a class-declaration. */
20890 type = check_tag_decl (&decl_specifiers,
20891 /*explicit_type_instantiation_p=*/false);
20892 /* Nested classes have already been added to the class, but
20893 a `friend' needs to be explicitly registered. */
20894 if (friend_p)
20896 /* If the `friend' keyword was present, the friend must
20897 be introduced with a class-key. */
20898 if (!declares_class_or_enum && cxx_dialect < cxx11)
20899 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20900 "in C++03 a class-key must be used "
20901 "when declaring a friend");
20902 /* In this case:
20904 template <typename T> struct A {
20905 friend struct A<T>::B;
20908 A<T>::B will be represented by a TYPENAME_TYPE, and
20909 therefore not recognized by check_tag_decl. */
20910 if (!type)
20912 type = decl_specifiers.type;
20913 if (type && TREE_CODE (type) == TYPE_DECL)
20914 type = TREE_TYPE (type);
20916 if (!type || !TYPE_P (type))
20917 error_at (decl_spec_token_start->location,
20918 "friend declaration does not name a class or "
20919 "function");
20920 else
20921 make_friend_class (current_class_type, type,
20922 /*complain=*/true);
20924 /* If there is no TYPE, an error message will already have
20925 been issued. */
20926 else if (!type || type == error_mark_node)
20928 /* An anonymous aggregate has to be handled specially; such
20929 a declaration really declares a data member (with a
20930 particular type), as opposed to a nested class. */
20931 else if (ANON_AGGR_TYPE_P (type))
20933 /* C++11 9.5/6. */
20934 if (decl_specifiers.storage_class != sc_none)
20935 error_at (decl_spec_token_start->location,
20936 "a storage class on an anonymous aggregate "
20937 "in class scope is not allowed");
20939 /* Remove constructors and such from TYPE, now that we
20940 know it is an anonymous aggregate. */
20941 fixup_anonymous_aggr (type);
20942 /* And make the corresponding data member. */
20943 decl = build_decl (decl_spec_token_start->location,
20944 FIELD_DECL, NULL_TREE, type);
20945 /* Add it to the class. */
20946 finish_member_declaration (decl);
20948 else
20949 cp_parser_check_access_in_redeclaration
20950 (TYPE_NAME (type),
20951 decl_spec_token_start->location);
20954 else
20956 bool assume_semicolon = false;
20958 /* Clear attributes from the decl_specifiers but keep them
20959 around as prefix attributes that apply them to the entity
20960 being declared. */
20961 prefix_attributes = decl_specifiers.attributes;
20962 decl_specifiers.attributes = NULL_TREE;
20964 /* See if these declarations will be friends. */
20965 friend_p = cp_parser_friend_p (&decl_specifiers);
20967 /* Keep going until we hit the `;' at the end of the
20968 declaration. */
20969 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20971 tree attributes = NULL_TREE;
20972 tree first_attribute;
20974 /* Peek at the next token. */
20975 token = cp_lexer_peek_token (parser->lexer);
20977 /* Check for a bitfield declaration. */
20978 if (token->type == CPP_COLON
20979 || (token->type == CPP_NAME
20980 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20981 == CPP_COLON))
20983 tree identifier;
20984 tree width;
20986 /* Get the name of the bitfield. Note that we cannot just
20987 check TOKEN here because it may have been invalidated by
20988 the call to cp_lexer_peek_nth_token above. */
20989 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20990 identifier = cp_parser_identifier (parser);
20991 else
20992 identifier = NULL_TREE;
20994 /* Consume the `:' token. */
20995 cp_lexer_consume_token (parser->lexer);
20996 /* Get the width of the bitfield. */
20997 width
20998 = cp_parser_constant_expression (parser,
20999 /*allow_non_constant=*/false,
21000 NULL);
21002 /* Look for attributes that apply to the bitfield. */
21003 attributes = cp_parser_attributes_opt (parser);
21004 /* Remember which attributes are prefix attributes and
21005 which are not. */
21006 first_attribute = attributes;
21007 /* Combine the attributes. */
21008 attributes = chainon (prefix_attributes, attributes);
21010 /* Create the bitfield declaration. */
21011 decl = grokbitfield (identifier
21012 ? make_id_declarator (NULL_TREE,
21013 identifier,
21014 sfk_none)
21015 : NULL,
21016 &decl_specifiers,
21017 width,
21018 attributes);
21020 else
21022 cp_declarator *declarator;
21023 tree initializer;
21024 tree asm_specification;
21025 int ctor_dtor_or_conv_p;
21027 /* Parse the declarator. */
21028 declarator
21029 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21030 &ctor_dtor_or_conv_p,
21031 /*parenthesized_p=*/NULL,
21032 /*member_p=*/true,
21033 friend_p);
21035 /* If something went wrong parsing the declarator, make sure
21036 that we at least consume some tokens. */
21037 if (declarator == cp_error_declarator)
21039 /* Skip to the end of the statement. */
21040 cp_parser_skip_to_end_of_statement (parser);
21041 /* If the next token is not a semicolon, that is
21042 probably because we just skipped over the body of
21043 a function. So, we consume a semicolon if
21044 present, but do not issue an error message if it
21045 is not present. */
21046 if (cp_lexer_next_token_is (parser->lexer,
21047 CPP_SEMICOLON))
21048 cp_lexer_consume_token (parser->lexer);
21049 goto out;
21052 if (declares_class_or_enum & 2)
21053 cp_parser_check_for_definition_in_return_type
21054 (declarator, decl_specifiers.type,
21055 decl_specifiers.locations[ds_type_spec]);
21057 /* Look for an asm-specification. */
21058 asm_specification = cp_parser_asm_specification_opt (parser);
21059 /* Look for attributes that apply to the declaration. */
21060 attributes = cp_parser_attributes_opt (parser);
21061 /* Remember which attributes are prefix attributes and
21062 which are not. */
21063 first_attribute = attributes;
21064 /* Combine the attributes. */
21065 attributes = chainon (prefix_attributes, attributes);
21067 /* If it's an `=', then we have a constant-initializer or a
21068 pure-specifier. It is not correct to parse the
21069 initializer before registering the member declaration
21070 since the member declaration should be in scope while
21071 its initializer is processed. However, the rest of the
21072 front end does not yet provide an interface that allows
21073 us to handle this correctly. */
21074 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21076 /* In [class.mem]:
21078 A pure-specifier shall be used only in the declaration of
21079 a virtual function.
21081 A member-declarator can contain a constant-initializer
21082 only if it declares a static member of integral or
21083 enumeration type.
21085 Therefore, if the DECLARATOR is for a function, we look
21086 for a pure-specifier; otherwise, we look for a
21087 constant-initializer. When we call `grokfield', it will
21088 perform more stringent semantics checks. */
21089 initializer_token_start = cp_lexer_peek_token (parser->lexer);
21090 if (function_declarator_p (declarator)
21091 || (decl_specifiers.type
21092 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21093 && declarator->kind == cdk_id
21094 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21095 == FUNCTION_TYPE)))
21096 initializer = cp_parser_pure_specifier (parser);
21097 else if (decl_specifiers.storage_class != sc_static)
21098 initializer = cp_parser_save_nsdmi (parser);
21099 else if (cxx_dialect >= cxx11)
21101 bool nonconst;
21102 /* Don't require a constant rvalue in C++11, since we
21103 might want a reference constant. We'll enforce
21104 constancy later. */
21105 cp_lexer_consume_token (parser->lexer);
21106 /* Parse the initializer. */
21107 initializer = cp_parser_initializer_clause (parser,
21108 &nonconst);
21110 else
21111 /* Parse the initializer. */
21112 initializer = cp_parser_constant_initializer (parser);
21114 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21115 && !function_declarator_p (declarator))
21117 bool x;
21118 if (decl_specifiers.storage_class != sc_static)
21119 initializer = cp_parser_save_nsdmi (parser);
21120 else
21121 initializer = cp_parser_initializer (parser, &x, &x);
21123 /* Otherwise, there is no initializer. */
21124 else
21125 initializer = NULL_TREE;
21127 // Save and reset the current template requirements. Handle
21128 // the trailing requirements, if there are any.
21129 cp_manage_requirements saved_requirements (true);
21130 if (flag_concepts)
21131 cp_parser_trailing_requirements (parser, declarator);
21133 /* See if we are probably looking at a function
21134 definition. We are certainly not looking at a
21135 member-declarator. Calling `grokfield' has
21136 side-effects, so we must not do it unless we are sure
21137 that we are looking at a member-declarator. */
21138 if (cp_parser_token_starts_function_definition_p
21139 (cp_lexer_peek_token (parser->lexer)))
21141 /* The grammar does not allow a pure-specifier to be
21142 used when a member function is defined. (It is
21143 possible that this fact is an oversight in the
21144 standard, since a pure function may be defined
21145 outside of the class-specifier. */
21146 if (initializer && initializer_token_start)
21147 error_at (initializer_token_start->location,
21148 "pure-specifier on function-definition");
21149 decl = cp_parser_save_member_function_body (parser,
21150 &decl_specifiers,
21151 declarator,
21152 attributes);
21153 if (parser->fully_implicit_function_template_p)
21154 decl = finish_fully_implicit_template (parser, decl);
21155 /* If the member was not a friend, declare it here. */
21156 if (!friend_p)
21157 finish_member_declaration (decl);
21159 if (friend_p)
21160 check_constrained_friend (decl, current_template_reqs);
21162 /* Peek at the next token. */
21163 token = cp_lexer_peek_token (parser->lexer);
21164 /* If the next token is a semicolon, consume it. */
21165 if (token->type == CPP_SEMICOLON)
21166 cp_lexer_consume_token (parser->lexer);
21168 goto out;
21170 else
21171 if (declarator->kind == cdk_function)
21172 declarator->id_loc = token->location;
21173 /* Create the declaration. */
21174 decl = grokfield (declarator, &decl_specifiers,
21175 initializer, /*init_const_expr_p=*/true,
21176 asm_specification, attributes);
21177 if (parser->fully_implicit_function_template_p)
21179 if (friend_p)
21180 finish_fully_implicit_template (parser, 0);
21181 else
21182 decl = finish_fully_implicit_template (parser, decl);
21186 cp_finalize_omp_declare_simd (parser, decl);
21188 /* Reset PREFIX_ATTRIBUTES. */
21189 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21190 attributes = TREE_CHAIN (attributes);
21191 if (attributes)
21192 TREE_CHAIN (attributes) = NULL_TREE;
21194 /* If there is any qualification still in effect, clear it
21195 now; we will be starting fresh with the next declarator. */
21196 parser->scope = NULL_TREE;
21197 parser->qualifying_scope = NULL_TREE;
21198 parser->object_scope = NULL_TREE;
21199 /* If it's a `,', then there are more declarators. */
21200 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21202 cp_lexer_consume_token (parser->lexer);
21203 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21205 cp_token *token = cp_lexer_previous_token (parser->lexer);
21206 error_at (token->location,
21207 "stray %<,%> at end of member declaration");
21210 /* If the next token isn't a `;', then we have a parse error. */
21211 else if (cp_lexer_next_token_is_not (parser->lexer,
21212 CPP_SEMICOLON))
21214 /* The next token might be a ways away from where the
21215 actual semicolon is missing. Find the previous token
21216 and use that for our error position. */
21217 cp_token *token = cp_lexer_previous_token (parser->lexer);
21218 error_at (token->location,
21219 "expected %<;%> at end of member declaration");
21221 /* Assume that the user meant to provide a semicolon. If
21222 we were to cp_parser_skip_to_end_of_statement, we might
21223 skip to a semicolon inside a member function definition
21224 and issue nonsensical error messages. */
21225 assume_semicolon = true;
21228 if (decl)
21230 /* Add DECL to the list of members. */
21231 if (!friend_p)
21232 finish_member_declaration (decl);
21234 if (TREE_CODE (decl) == FUNCTION_DECL)
21235 cp_parser_save_default_args (parser, decl);
21236 else if (TREE_CODE (decl) == FIELD_DECL
21237 && !DECL_C_BIT_FIELD (decl)
21238 && DECL_INITIAL (decl))
21239 /* Add DECL to the queue of NSDMI to be parsed later. */
21240 vec_safe_push (unparsed_nsdmis, decl);
21243 if (assume_semicolon)
21244 goto out;
21248 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21249 out:
21250 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21253 /* Parse a pure-specifier.
21255 pure-specifier:
21258 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21259 Otherwise, ERROR_MARK_NODE is returned. */
21261 static tree
21262 cp_parser_pure_specifier (cp_parser* parser)
21264 cp_token *token;
21266 /* Look for the `=' token. */
21267 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21268 return error_mark_node;
21269 /* Look for the `0' token. */
21270 token = cp_lexer_peek_token (parser->lexer);
21272 if (token->type == CPP_EOF
21273 || token->type == CPP_PRAGMA_EOL)
21274 return error_mark_node;
21276 cp_lexer_consume_token (parser->lexer);
21278 /* Accept = default or = delete in c++0x mode. */
21279 if (token->keyword == RID_DEFAULT
21280 || token->keyword == RID_DELETE)
21282 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21283 return token->u.value;
21286 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21287 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21289 cp_parser_error (parser,
21290 "invalid pure specifier (only %<= 0%> is allowed)");
21291 cp_parser_skip_to_end_of_statement (parser);
21292 return error_mark_node;
21294 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21296 error_at (token->location, "templates may not be %<virtual%>");
21297 return error_mark_node;
21300 return integer_zero_node;
21303 /* Parse a constant-initializer.
21305 constant-initializer:
21306 = constant-expression
21308 Returns a representation of the constant-expression. */
21310 static tree
21311 cp_parser_constant_initializer (cp_parser* parser)
21313 /* Look for the `=' token. */
21314 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21315 return error_mark_node;
21317 /* It is invalid to write:
21319 struct S { static const int i = { 7 }; };
21322 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21324 cp_parser_error (parser,
21325 "a brace-enclosed initializer is not allowed here");
21326 /* Consume the opening brace. */
21327 cp_lexer_consume_token (parser->lexer);
21328 /* Skip the initializer. */
21329 cp_parser_skip_to_closing_brace (parser);
21330 /* Look for the trailing `}'. */
21331 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21333 return error_mark_node;
21336 return cp_parser_constant_expression (parser,
21337 /*allow_non_constant=*/false,
21338 NULL);
21341 /* Derived classes [gram.class.derived] */
21343 /* Parse a base-clause.
21345 base-clause:
21346 : base-specifier-list
21348 base-specifier-list:
21349 base-specifier ... [opt]
21350 base-specifier-list , base-specifier ... [opt]
21352 Returns a TREE_LIST representing the base-classes, in the order in
21353 which they were declared. The representation of each node is as
21354 described by cp_parser_base_specifier.
21356 In the case that no bases are specified, this function will return
21357 NULL_TREE, not ERROR_MARK_NODE. */
21359 static tree
21360 cp_parser_base_clause (cp_parser* parser)
21362 tree bases = NULL_TREE;
21364 /* Look for the `:' that begins the list. */
21365 cp_parser_require (parser, CPP_COLON, RT_COLON);
21367 /* Scan the base-specifier-list. */
21368 while (true)
21370 cp_token *token;
21371 tree base;
21372 bool pack_expansion_p = false;
21374 /* Look for the base-specifier. */
21375 base = cp_parser_base_specifier (parser);
21376 /* Look for the (optional) ellipsis. */
21377 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21379 /* Consume the `...'. */
21380 cp_lexer_consume_token (parser->lexer);
21382 pack_expansion_p = true;
21385 /* Add BASE to the front of the list. */
21386 if (base && base != error_mark_node)
21388 if (pack_expansion_p)
21389 /* Make this a pack expansion type. */
21390 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21392 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21394 TREE_CHAIN (base) = bases;
21395 bases = base;
21398 /* Peek at the next token. */
21399 token = cp_lexer_peek_token (parser->lexer);
21400 /* If it's not a comma, then the list is complete. */
21401 if (token->type != CPP_COMMA)
21402 break;
21403 /* Consume the `,'. */
21404 cp_lexer_consume_token (parser->lexer);
21407 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21408 base class had a qualified name. However, the next name that
21409 appears is certainly not qualified. */
21410 parser->scope = NULL_TREE;
21411 parser->qualifying_scope = NULL_TREE;
21412 parser->object_scope = NULL_TREE;
21414 return nreverse (bases);
21417 /* Parse a base-specifier.
21419 base-specifier:
21420 :: [opt] nested-name-specifier [opt] class-name
21421 virtual access-specifier [opt] :: [opt] nested-name-specifier
21422 [opt] class-name
21423 access-specifier virtual [opt] :: [opt] nested-name-specifier
21424 [opt] class-name
21426 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21427 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21428 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21429 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21431 static tree
21432 cp_parser_base_specifier (cp_parser* parser)
21434 cp_token *token;
21435 bool done = false;
21436 bool virtual_p = false;
21437 bool duplicate_virtual_error_issued_p = false;
21438 bool duplicate_access_error_issued_p = false;
21439 bool class_scope_p, template_p;
21440 tree access = access_default_node;
21441 tree type;
21443 /* Process the optional `virtual' and `access-specifier'. */
21444 while (!done)
21446 /* Peek at the next token. */
21447 token = cp_lexer_peek_token (parser->lexer);
21448 /* Process `virtual'. */
21449 switch (token->keyword)
21451 case RID_VIRTUAL:
21452 /* If `virtual' appears more than once, issue an error. */
21453 if (virtual_p && !duplicate_virtual_error_issued_p)
21455 cp_parser_error (parser,
21456 "%<virtual%> specified more than once in base-specified");
21457 duplicate_virtual_error_issued_p = true;
21460 virtual_p = true;
21462 /* Consume the `virtual' token. */
21463 cp_lexer_consume_token (parser->lexer);
21465 break;
21467 case RID_PUBLIC:
21468 case RID_PROTECTED:
21469 case RID_PRIVATE:
21470 /* If more than one access specifier appears, issue an
21471 error. */
21472 if (access != access_default_node
21473 && !duplicate_access_error_issued_p)
21475 cp_parser_error (parser,
21476 "more than one access specifier in base-specified");
21477 duplicate_access_error_issued_p = true;
21480 access = ridpointers[(int) token->keyword];
21482 /* Consume the access-specifier. */
21483 cp_lexer_consume_token (parser->lexer);
21485 break;
21487 default:
21488 done = true;
21489 break;
21492 /* It is not uncommon to see programs mechanically, erroneously, use
21493 the 'typename' keyword to denote (dependent) qualified types
21494 as base classes. */
21495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21497 token = cp_lexer_peek_token (parser->lexer);
21498 if (!processing_template_decl)
21499 error_at (token->location,
21500 "keyword %<typename%> not allowed outside of templates");
21501 else
21502 error_at (token->location,
21503 "keyword %<typename%> not allowed in this context "
21504 "(the base class is implicitly a type)");
21505 cp_lexer_consume_token (parser->lexer);
21508 /* Look for the optional `::' operator. */
21509 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21510 /* Look for the nested-name-specifier. The simplest way to
21511 implement:
21513 [temp.res]
21515 The keyword `typename' is not permitted in a base-specifier or
21516 mem-initializer; in these contexts a qualified name that
21517 depends on a template-parameter is implicitly assumed to be a
21518 type name.
21520 is to pretend that we have seen the `typename' keyword at this
21521 point. */
21522 cp_parser_nested_name_specifier_opt (parser,
21523 /*typename_keyword_p=*/true,
21524 /*check_dependency_p=*/true,
21525 typename_type,
21526 /*is_declaration=*/true);
21527 /* If the base class is given by a qualified name, assume that names
21528 we see are type names or templates, as appropriate. */
21529 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21530 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21532 if (!parser->scope
21533 && cp_lexer_next_token_is_decltype (parser->lexer))
21534 /* DR 950 allows decltype as a base-specifier. */
21535 type = cp_parser_decltype (parser);
21536 else
21538 /* Otherwise, look for the class-name. */
21539 type = cp_parser_class_name (parser,
21540 class_scope_p,
21541 template_p,
21542 typename_type,
21543 /*check_dependency_p=*/true,
21544 /*class_head_p=*/false,
21545 /*is_declaration=*/true);
21546 type = TREE_TYPE (type);
21549 if (type == error_mark_node)
21550 return error_mark_node;
21552 return finish_base_specifier (type, access, virtual_p);
21555 /* Exception handling [gram.exception] */
21557 /* Parse an (optional) noexcept-specification.
21559 noexcept-specification:
21560 noexcept ( constant-expression ) [opt]
21562 If no noexcept-specification is present, returns NULL_TREE.
21563 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21564 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21565 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21566 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21567 in which case a boolean condition is returned instead. */
21569 static tree
21570 cp_parser_noexcept_specification_opt (cp_parser* parser,
21571 bool require_constexpr,
21572 bool* consumed_expr,
21573 bool return_cond)
21575 cp_token *token;
21576 const char *saved_message;
21578 /* Peek at the next token. */
21579 token = cp_lexer_peek_token (parser->lexer);
21581 /* Is it a noexcept-specification? */
21582 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21584 tree expr;
21585 cp_lexer_consume_token (parser->lexer);
21587 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21589 cp_lexer_consume_token (parser->lexer);
21591 if (require_constexpr)
21593 /* Types may not be defined in an exception-specification. */
21594 saved_message = parser->type_definition_forbidden_message;
21595 parser->type_definition_forbidden_message
21596 = G_("types may not be defined in an exception-specification");
21598 expr = cp_parser_constant_expression (parser, false, NULL);
21600 /* Restore the saved message. */
21601 parser->type_definition_forbidden_message = saved_message;
21603 else
21605 expr = cp_parser_expression (parser, false, NULL);
21606 *consumed_expr = true;
21609 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21611 else
21613 expr = boolean_true_node;
21614 if (!require_constexpr)
21615 *consumed_expr = false;
21618 /* We cannot build a noexcept-spec right away because this will check
21619 that expr is a constexpr. */
21620 if (!return_cond)
21621 return build_noexcept_spec (expr, tf_warning_or_error);
21622 else
21623 return expr;
21625 else
21626 return NULL_TREE;
21629 /* Parse an (optional) exception-specification.
21631 exception-specification:
21632 throw ( type-id-list [opt] )
21634 Returns a TREE_LIST representing the exception-specification. The
21635 TREE_VALUE of each node is a type. */
21637 static tree
21638 cp_parser_exception_specification_opt (cp_parser* parser)
21640 cp_token *token;
21641 tree type_id_list;
21642 const char *saved_message;
21644 /* Peek at the next token. */
21645 token = cp_lexer_peek_token (parser->lexer);
21647 /* Is it a noexcept-specification? */
21648 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21649 false);
21650 if (type_id_list != NULL_TREE)
21651 return type_id_list;
21653 /* If it's not `throw', then there's no exception-specification. */
21654 if (!cp_parser_is_keyword (token, RID_THROW))
21655 return NULL_TREE;
21657 #if 0
21658 /* Enable this once a lot of code has transitioned to noexcept? */
21659 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21660 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21661 "deprecated in C++0x; use %<noexcept%> instead");
21662 #endif
21664 /* Consume the `throw'. */
21665 cp_lexer_consume_token (parser->lexer);
21667 /* Look for the `('. */
21668 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21670 /* Peek at the next token. */
21671 token = cp_lexer_peek_token (parser->lexer);
21672 /* If it's not a `)', then there is a type-id-list. */
21673 if (token->type != CPP_CLOSE_PAREN)
21675 /* Types may not be defined in an exception-specification. */
21676 saved_message = parser->type_definition_forbidden_message;
21677 parser->type_definition_forbidden_message
21678 = G_("types may not be defined in an exception-specification");
21679 /* Parse the type-id-list. */
21680 type_id_list = cp_parser_type_id_list (parser);
21681 /* Restore the saved message. */
21682 parser->type_definition_forbidden_message = saved_message;
21684 else
21685 type_id_list = empty_except_spec;
21687 /* Look for the `)'. */
21688 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21690 return type_id_list;
21693 /* Parse an (optional) type-id-list.
21695 type-id-list:
21696 type-id ... [opt]
21697 type-id-list , type-id ... [opt]
21699 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21700 in the order that the types were presented. */
21702 static tree
21703 cp_parser_type_id_list (cp_parser* parser)
21705 tree types = NULL_TREE;
21707 while (true)
21709 cp_token *token;
21710 tree type;
21712 /* Get the next type-id. */
21713 type = cp_parser_type_id (parser);
21714 /* Parse the optional ellipsis. */
21715 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21717 /* Consume the `...'. */
21718 cp_lexer_consume_token (parser->lexer);
21720 /* Turn the type into a pack expansion expression. */
21721 type = make_pack_expansion (type);
21723 /* Add it to the list. */
21724 types = add_exception_specifier (types, type, /*complain=*/1);
21725 /* Peek at the next token. */
21726 token = cp_lexer_peek_token (parser->lexer);
21727 /* If it is not a `,', we are done. */
21728 if (token->type != CPP_COMMA)
21729 break;
21730 /* Consume the `,'. */
21731 cp_lexer_consume_token (parser->lexer);
21734 return nreverse (types);
21737 /* Parse a try-block.
21739 try-block:
21740 try compound-statement handler-seq */
21742 static tree
21743 cp_parser_try_block (cp_parser* parser)
21745 tree try_block;
21747 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21748 try_block = begin_try_block ();
21749 cp_parser_compound_statement (parser, NULL, true, false);
21750 finish_try_block (try_block);
21751 cp_parser_handler_seq (parser);
21752 finish_handler_sequence (try_block);
21754 return try_block;
21757 /* Parse a function-try-block.
21759 function-try-block:
21760 try ctor-initializer [opt] function-body handler-seq */
21762 static bool
21763 cp_parser_function_try_block (cp_parser* parser)
21765 tree compound_stmt;
21766 tree try_block;
21767 bool ctor_initializer_p;
21769 /* Look for the `try' keyword. */
21770 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21771 return false;
21772 /* Let the rest of the front end know where we are. */
21773 try_block = begin_function_try_block (&compound_stmt);
21774 /* Parse the function-body. */
21775 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21776 (parser, /*in_function_try_block=*/true);
21777 /* We're done with the `try' part. */
21778 finish_function_try_block (try_block);
21779 /* Parse the handlers. */
21780 cp_parser_handler_seq (parser);
21781 /* We're done with the handlers. */
21782 finish_function_handler_sequence (try_block, compound_stmt);
21784 return ctor_initializer_p;
21787 /* Parse a handler-seq.
21789 handler-seq:
21790 handler handler-seq [opt] */
21792 static void
21793 cp_parser_handler_seq (cp_parser* parser)
21795 while (true)
21797 cp_token *token;
21799 /* Parse the handler. */
21800 cp_parser_handler (parser);
21801 /* Peek at the next token. */
21802 token = cp_lexer_peek_token (parser->lexer);
21803 /* If it's not `catch' then there are no more handlers. */
21804 if (!cp_parser_is_keyword (token, RID_CATCH))
21805 break;
21809 /* Parse a handler.
21811 handler:
21812 catch ( exception-declaration ) compound-statement */
21814 static void
21815 cp_parser_handler (cp_parser* parser)
21817 tree handler;
21818 tree declaration;
21820 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21821 handler = begin_handler ();
21822 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21823 declaration = cp_parser_exception_declaration (parser);
21824 finish_handler_parms (declaration, handler);
21825 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21826 cp_parser_compound_statement (parser, NULL, false, false);
21827 finish_handler (handler);
21830 /* Parse an exception-declaration.
21832 exception-declaration:
21833 type-specifier-seq declarator
21834 type-specifier-seq abstract-declarator
21835 type-specifier-seq
21838 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21839 ellipsis variant is used. */
21841 static tree
21842 cp_parser_exception_declaration (cp_parser* parser)
21844 cp_decl_specifier_seq type_specifiers;
21845 cp_declarator *declarator;
21846 const char *saved_message;
21848 /* If it's an ellipsis, it's easy to handle. */
21849 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21851 /* Consume the `...' token. */
21852 cp_lexer_consume_token (parser->lexer);
21853 return NULL_TREE;
21856 /* Types may not be defined in exception-declarations. */
21857 saved_message = parser->type_definition_forbidden_message;
21858 parser->type_definition_forbidden_message
21859 = G_("types may not be defined in exception-declarations");
21861 /* Parse the type-specifier-seq. */
21862 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21863 /*is_trailing_return=*/false,
21864 &type_specifiers);
21865 /* If it's a `)', then there is no declarator. */
21866 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21867 declarator = NULL;
21868 else
21869 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21870 /*ctor_dtor_or_conv_p=*/NULL,
21871 /*parenthesized_p=*/NULL,
21872 /*member_p=*/false,
21873 /*friend_p=*/false);
21875 /* Restore the saved message. */
21876 parser->type_definition_forbidden_message = saved_message;
21878 if (!type_specifiers.any_specifiers_p)
21879 return error_mark_node;
21881 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21884 /* Parse a throw-expression.
21886 throw-expression:
21887 throw assignment-expression [opt]
21889 Returns a THROW_EXPR representing the throw-expression. */
21891 static tree
21892 cp_parser_throw_expression (cp_parser* parser)
21894 tree expression;
21895 cp_token* token;
21897 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21898 token = cp_lexer_peek_token (parser->lexer);
21899 /* Figure out whether or not there is an assignment-expression
21900 following the "throw" keyword. */
21901 if (token->type == CPP_COMMA
21902 || token->type == CPP_SEMICOLON
21903 || token->type == CPP_CLOSE_PAREN
21904 || token->type == CPP_CLOSE_SQUARE
21905 || token->type == CPP_CLOSE_BRACE
21906 || token->type == CPP_COLON)
21907 expression = NULL_TREE;
21908 else
21909 expression = cp_parser_assignment_expression (parser,
21910 /*cast_p=*/false, NULL);
21912 return build_throw (expression);
21915 /* GNU Extensions */
21917 /* Parse an (optional) asm-specification.
21919 asm-specification:
21920 asm ( string-literal )
21922 If the asm-specification is present, returns a STRING_CST
21923 corresponding to the string-literal. Otherwise, returns
21924 NULL_TREE. */
21926 static tree
21927 cp_parser_asm_specification_opt (cp_parser* parser)
21929 cp_token *token;
21930 tree asm_specification;
21932 /* Peek at the next token. */
21933 token = cp_lexer_peek_token (parser->lexer);
21934 /* If the next token isn't the `asm' keyword, then there's no
21935 asm-specification. */
21936 if (!cp_parser_is_keyword (token, RID_ASM))
21937 return NULL_TREE;
21939 /* Consume the `asm' token. */
21940 cp_lexer_consume_token (parser->lexer);
21941 /* Look for the `('. */
21942 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21944 /* Look for the string-literal. */
21945 asm_specification = cp_parser_string_literal (parser, false, false);
21947 /* Look for the `)'. */
21948 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21950 return asm_specification;
21953 /* Parse an asm-operand-list.
21955 asm-operand-list:
21956 asm-operand
21957 asm-operand-list , asm-operand
21959 asm-operand:
21960 string-literal ( expression )
21961 [ string-literal ] string-literal ( expression )
21963 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21964 each node is the expression. The TREE_PURPOSE is itself a
21965 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21966 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21967 is a STRING_CST for the string literal before the parenthesis. Returns
21968 ERROR_MARK_NODE if any of the operands are invalid. */
21970 static tree
21971 cp_parser_asm_operand_list (cp_parser* parser)
21973 tree asm_operands = NULL_TREE;
21974 bool invalid_operands = false;
21976 while (true)
21978 tree string_literal;
21979 tree expression;
21980 tree name;
21982 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21984 /* Consume the `[' token. */
21985 cp_lexer_consume_token (parser->lexer);
21986 /* Read the operand name. */
21987 name = cp_parser_identifier (parser);
21988 if (name != error_mark_node)
21989 name = build_string (IDENTIFIER_LENGTH (name),
21990 IDENTIFIER_POINTER (name));
21991 /* Look for the closing `]'. */
21992 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21994 else
21995 name = NULL_TREE;
21996 /* Look for the string-literal. */
21997 string_literal = cp_parser_string_literal (parser, false, false);
21999 /* Look for the `('. */
22000 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22001 /* Parse the expression. */
22002 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22003 /* Look for the `)'. */
22004 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22006 if (name == error_mark_node
22007 || string_literal == error_mark_node
22008 || expression == error_mark_node)
22009 invalid_operands = true;
22011 /* Add this operand to the list. */
22012 asm_operands = tree_cons (build_tree_list (name, string_literal),
22013 expression,
22014 asm_operands);
22015 /* If the next token is not a `,', there are no more
22016 operands. */
22017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22018 break;
22019 /* Consume the `,'. */
22020 cp_lexer_consume_token (parser->lexer);
22023 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22026 /* Parse an asm-clobber-list.
22028 asm-clobber-list:
22029 string-literal
22030 asm-clobber-list , string-literal
22032 Returns a TREE_LIST, indicating the clobbers in the order that they
22033 appeared. The TREE_VALUE of each node is a STRING_CST. */
22035 static tree
22036 cp_parser_asm_clobber_list (cp_parser* parser)
22038 tree clobbers = NULL_TREE;
22040 while (true)
22042 tree string_literal;
22044 /* Look for the string literal. */
22045 string_literal = cp_parser_string_literal (parser, false, false);
22046 /* Add it to the list. */
22047 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22048 /* If the next token is not a `,', then the list is
22049 complete. */
22050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22051 break;
22052 /* Consume the `,' token. */
22053 cp_lexer_consume_token (parser->lexer);
22056 return clobbers;
22059 /* Parse an asm-label-list.
22061 asm-label-list:
22062 identifier
22063 asm-label-list , identifier
22065 Returns a TREE_LIST, indicating the labels in the order that they
22066 appeared. The TREE_VALUE of each node is a label. */
22068 static tree
22069 cp_parser_asm_label_list (cp_parser* parser)
22071 tree labels = NULL_TREE;
22073 while (true)
22075 tree identifier, label, name;
22077 /* Look for the identifier. */
22078 identifier = cp_parser_identifier (parser);
22079 if (!error_operand_p (identifier))
22081 label = lookup_label (identifier);
22082 if (TREE_CODE (label) == LABEL_DECL)
22084 TREE_USED (label) = 1;
22085 check_goto (label);
22086 name = build_string (IDENTIFIER_LENGTH (identifier),
22087 IDENTIFIER_POINTER (identifier));
22088 labels = tree_cons (name, label, labels);
22091 /* If the next token is not a `,', then the list is
22092 complete. */
22093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22094 break;
22095 /* Consume the `,' token. */
22096 cp_lexer_consume_token (parser->lexer);
22099 return nreverse (labels);
22102 /* Return TRUE iff the next tokens in the stream are possibly the
22103 beginning of a GNU extension attribute. */
22105 static bool
22106 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22108 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22111 /* Return TRUE iff the next tokens in the stream are possibly the
22112 beginning of a standard C++-11 attribute specifier. */
22114 static bool
22115 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22117 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22120 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22121 beginning of a standard C++-11 attribute specifier. */
22123 static bool
22124 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22126 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22128 return (cxx_dialect >= cxx11
22129 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22130 || (token->type == CPP_OPEN_SQUARE
22131 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22132 && token->type == CPP_OPEN_SQUARE)));
22135 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22136 beginning of a GNU extension attribute. */
22138 static bool
22139 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22141 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22143 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22146 /* Return true iff the next tokens can be the beginning of either a
22147 GNU attribute list, or a standard C++11 attribute sequence. */
22149 static bool
22150 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22152 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22153 || cp_next_tokens_can_be_std_attribute_p (parser));
22156 /* Return true iff the next Nth tokens can be the beginning of either
22157 a GNU attribute list, or a standard C++11 attribute sequence. */
22159 static bool
22160 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22162 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22163 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22166 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22167 of GNU attributes, or return NULL. */
22169 static tree
22170 cp_parser_attributes_opt (cp_parser *parser)
22172 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22173 return cp_parser_gnu_attributes_opt (parser);
22174 return cp_parser_std_attribute_spec_seq (parser);
22177 #define CILK_SIMD_FN_CLAUSE_MASK \
22178 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22179 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22180 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22181 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22182 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22184 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22185 vector [(<clauses>)] */
22187 static void
22188 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22190 bool first_p = parser->cilk_simd_fn_info == NULL;
22191 cp_token *token = v_token;
22192 if (first_p)
22194 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22195 parser->cilk_simd_fn_info->error_seen = false;
22196 parser->cilk_simd_fn_info->fndecl_seen = false;
22197 parser->cilk_simd_fn_info->tokens = vNULL;
22199 int paren_scope = 0;
22200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22202 cp_lexer_consume_token (parser->lexer);
22203 v_token = cp_lexer_peek_token (parser->lexer);
22204 paren_scope++;
22206 while (paren_scope > 0)
22208 token = cp_lexer_peek_token (parser->lexer);
22209 if (token->type == CPP_OPEN_PAREN)
22210 paren_scope++;
22211 else if (token->type == CPP_CLOSE_PAREN)
22212 paren_scope--;
22213 /* Do not push the last ')' */
22214 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22215 cp_lexer_consume_token (parser->lexer);
22218 token->type = CPP_PRAGMA_EOL;
22219 parser->lexer->next_token = token;
22220 cp_lexer_consume_token (parser->lexer);
22222 struct cp_token_cache *cp
22223 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22224 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22227 /* Parse an (optional) series of attributes.
22229 attributes:
22230 attributes attribute
22232 attribute:
22233 __attribute__ (( attribute-list [opt] ))
22235 The return value is as for cp_parser_gnu_attribute_list. */
22237 static tree
22238 cp_parser_gnu_attributes_opt (cp_parser* parser)
22240 tree attributes = NULL_TREE;
22242 while (true)
22244 cp_token *token;
22245 tree attribute_list;
22246 bool ok = true;
22248 /* Peek at the next token. */
22249 token = cp_lexer_peek_token (parser->lexer);
22250 /* If it's not `__attribute__', then we're done. */
22251 if (token->keyword != RID_ATTRIBUTE)
22252 break;
22254 /* Consume the `__attribute__' keyword. */
22255 cp_lexer_consume_token (parser->lexer);
22256 /* Look for the two `(' tokens. */
22257 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22258 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22260 /* Peek at the next token. */
22261 token = cp_lexer_peek_token (parser->lexer);
22262 if (token->type != CPP_CLOSE_PAREN)
22263 /* Parse the attribute-list. */
22264 attribute_list = cp_parser_gnu_attribute_list (parser);
22265 else
22266 /* If the next token is a `)', then there is no attribute
22267 list. */
22268 attribute_list = NULL;
22270 /* Look for the two `)' tokens. */
22271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22272 ok = false;
22273 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22274 ok = false;
22275 if (!ok)
22276 cp_parser_skip_to_end_of_statement (parser);
22278 /* Add these new attributes to the list. */
22279 attributes = chainon (attributes, attribute_list);
22282 return attributes;
22285 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22286 "__vector" or "__vector__." */
22288 static inline bool
22289 is_cilkplus_vector_p (tree name)
22291 if (flag_cilkplus && is_attribute_p ("vector", name))
22292 return true;
22293 return false;
22296 /* Parse a GNU attribute-list.
22298 attribute-list:
22299 attribute
22300 attribute-list , attribute
22302 attribute:
22303 identifier
22304 identifier ( identifier )
22305 identifier ( identifier , expression-list )
22306 identifier ( expression-list )
22308 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22309 to an attribute. The TREE_PURPOSE of each node is the identifier
22310 indicating which attribute is in use. The TREE_VALUE represents
22311 the arguments, if any. */
22313 static tree
22314 cp_parser_gnu_attribute_list (cp_parser* parser)
22316 tree attribute_list = NULL_TREE;
22317 bool save_translate_strings_p = parser->translate_strings_p;
22319 parser->translate_strings_p = false;
22320 while (true)
22322 cp_token *token;
22323 tree identifier;
22324 tree attribute;
22326 /* Look for the identifier. We also allow keywords here; for
22327 example `__attribute__ ((const))' is legal. */
22328 token = cp_lexer_peek_token (parser->lexer);
22329 if (token->type == CPP_NAME
22330 || token->type == CPP_KEYWORD)
22332 tree arguments = NULL_TREE;
22334 /* Consume the token, but save it since we need it for the
22335 SIMD enabled function parsing. */
22336 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22338 /* Save away the identifier that indicates which attribute
22339 this is. */
22340 identifier = (token->type == CPP_KEYWORD)
22341 /* For keywords, use the canonical spelling, not the
22342 parsed identifier. */
22343 ? ridpointers[(int) token->keyword]
22344 : id_token->u.value;
22346 attribute = build_tree_list (identifier, NULL_TREE);
22348 /* Peek at the next token. */
22349 token = cp_lexer_peek_token (parser->lexer);
22350 /* If it's an `(', then parse the attribute arguments. */
22351 if (token->type == CPP_OPEN_PAREN)
22353 vec<tree, va_gc> *vec;
22354 int attr_flag = (attribute_takes_identifier_p (identifier)
22355 ? id_attr : normal_attr);
22356 if (is_cilkplus_vector_p (identifier))
22358 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22359 continue;
22361 else
22362 vec = cp_parser_parenthesized_expression_list
22363 (parser, attr_flag, /*cast_p=*/false,
22364 /*allow_expansion_p=*/false,
22365 /*non_constant_p=*/NULL);
22366 if (vec == NULL)
22367 arguments = error_mark_node;
22368 else
22370 arguments = build_tree_list_vec (vec);
22371 release_tree_vector (vec);
22373 /* Save the arguments away. */
22374 TREE_VALUE (attribute) = arguments;
22376 else if (is_cilkplus_vector_p (identifier))
22378 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22379 continue;
22382 if (arguments != error_mark_node)
22384 /* Add this attribute to the list. */
22385 TREE_CHAIN (attribute) = attribute_list;
22386 attribute_list = attribute;
22389 token = cp_lexer_peek_token (parser->lexer);
22391 /* Now, look for more attributes. If the next token isn't a
22392 `,', we're done. */
22393 if (token->type != CPP_COMMA)
22394 break;
22396 /* Consume the comma and keep going. */
22397 cp_lexer_consume_token (parser->lexer);
22399 parser->translate_strings_p = save_translate_strings_p;
22401 /* We built up the list in reverse order. */
22402 return nreverse (attribute_list);
22405 /* Parse a standard C++11 attribute.
22407 The returned representation is a TREE_LIST which TREE_PURPOSE is
22408 the scoped name of the attribute, and the TREE_VALUE is its
22409 arguments list.
22411 Note that the scoped name of the attribute is itself a TREE_LIST
22412 which TREE_PURPOSE is the namespace of the attribute, and
22413 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22414 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22415 and which TREE_PURPOSE is directly the attribute name.
22417 Clients of the attribute code should use get_attribute_namespace
22418 and get_attribute_name to get the actual namespace and name of
22419 attributes, regardless of their being GNU or C++11 attributes.
22421 attribute:
22422 attribute-token attribute-argument-clause [opt]
22424 attribute-token:
22425 identifier
22426 attribute-scoped-token
22428 attribute-scoped-token:
22429 attribute-namespace :: identifier
22431 attribute-namespace:
22432 identifier
22434 attribute-argument-clause:
22435 ( balanced-token-seq )
22437 balanced-token-seq:
22438 balanced-token [opt]
22439 balanced-token-seq balanced-token
22441 balanced-token:
22442 ( balanced-token-seq )
22443 [ balanced-token-seq ]
22444 { balanced-token-seq }. */
22446 static tree
22447 cp_parser_std_attribute (cp_parser *parser)
22449 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22450 cp_token *token;
22452 /* First, parse name of the the attribute, a.k.a
22453 attribute-token. */
22455 token = cp_lexer_peek_token (parser->lexer);
22456 if (token->type == CPP_NAME)
22457 attr_id = token->u.value;
22458 else if (token->type == CPP_KEYWORD)
22459 attr_id = ridpointers[(int) token->keyword];
22460 else if (token->flags & NAMED_OP)
22461 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22463 if (attr_id == NULL_TREE)
22464 return NULL_TREE;
22466 cp_lexer_consume_token (parser->lexer);
22468 token = cp_lexer_peek_token (parser->lexer);
22469 if (token->type == CPP_SCOPE)
22471 /* We are seeing a scoped attribute token. */
22473 cp_lexer_consume_token (parser->lexer);
22474 attr_ns = attr_id;
22476 token = cp_lexer_consume_token (parser->lexer);
22477 if (token->type == CPP_NAME)
22478 attr_id = token->u.value;
22479 else if (token->type == CPP_KEYWORD)
22480 attr_id = ridpointers[(int) token->keyword];
22481 else
22483 error_at (token->location,
22484 "expected an identifier for the attribute name");
22485 return error_mark_node;
22487 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22488 NULL_TREE);
22489 token = cp_lexer_peek_token (parser->lexer);
22491 else
22493 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22494 NULL_TREE);
22495 /* C++11 noreturn attribute is equivalent to GNU's. */
22496 if (is_attribute_p ("noreturn", attr_id))
22497 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22498 /* C++14 deprecated attribute is equivalent to GNU's. */
22499 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
22500 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22503 /* Now parse the optional argument clause of the attribute. */
22505 if (token->type != CPP_OPEN_PAREN)
22506 return attribute;
22509 vec<tree, va_gc> *vec;
22510 int attr_flag = normal_attr;
22512 if (attr_ns == get_identifier ("gnu")
22513 && attribute_takes_identifier_p (attr_id))
22514 /* A GNU attribute that takes an identifier in parameter. */
22515 attr_flag = id_attr;
22517 vec = cp_parser_parenthesized_expression_list
22518 (parser, attr_flag, /*cast_p=*/false,
22519 /*allow_expansion_p=*/true,
22520 /*non_constant_p=*/NULL);
22521 if (vec == NULL)
22522 arguments = error_mark_node;
22523 else
22525 arguments = build_tree_list_vec (vec);
22526 release_tree_vector (vec);
22529 if (arguments == error_mark_node)
22530 attribute = error_mark_node;
22531 else
22532 TREE_VALUE (attribute) = arguments;
22535 return attribute;
22538 /* Parse a list of standard C++-11 attributes.
22540 attribute-list:
22541 attribute [opt]
22542 attribute-list , attribute[opt]
22543 attribute ...
22544 attribute-list , attribute ...
22547 static tree
22548 cp_parser_std_attribute_list (cp_parser *parser)
22550 tree attributes = NULL_TREE, attribute = NULL_TREE;
22551 cp_token *token = NULL;
22553 while (true)
22555 attribute = cp_parser_std_attribute (parser);
22556 if (attribute == error_mark_node)
22557 break;
22558 if (attribute != NULL_TREE)
22560 TREE_CHAIN (attribute) = attributes;
22561 attributes = attribute;
22563 token = cp_lexer_peek_token (parser->lexer);
22564 if (token->type != CPP_COMMA)
22565 break;
22566 cp_lexer_consume_token (parser->lexer);
22568 attributes = nreverse (attributes);
22569 return attributes;
22572 /* Parse a standard C++-11 attribute specifier.
22574 attribute-specifier:
22575 [ [ attribute-list ] ]
22576 alignment-specifier
22578 alignment-specifier:
22579 alignas ( type-id ... [opt] )
22580 alignas ( alignment-expression ... [opt] ). */
22582 static tree
22583 cp_parser_std_attribute_spec (cp_parser *parser)
22585 tree attributes = NULL_TREE;
22586 cp_token *token = cp_lexer_peek_token (parser->lexer);
22588 if (token->type == CPP_OPEN_SQUARE
22589 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22591 cp_lexer_consume_token (parser->lexer);
22592 cp_lexer_consume_token (parser->lexer);
22594 attributes = cp_parser_std_attribute_list (parser);
22596 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22597 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22598 cp_parser_skip_to_end_of_statement (parser);
22599 else
22600 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22601 when we are sure that we have actually parsed them. */
22602 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22604 else
22606 tree alignas_expr;
22608 /* Look for an alignment-specifier. */
22610 token = cp_lexer_peek_token (parser->lexer);
22612 if (token->type != CPP_KEYWORD
22613 || token->keyword != RID_ALIGNAS)
22614 return NULL_TREE;
22616 cp_lexer_consume_token (parser->lexer);
22617 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22619 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22621 cp_parser_error (parser, "expected %<(%>");
22622 return error_mark_node;
22625 cp_parser_parse_tentatively (parser);
22626 alignas_expr = cp_parser_type_id (parser);
22628 if (!cp_parser_parse_definitely (parser))
22630 gcc_assert (alignas_expr == error_mark_node
22631 || alignas_expr == NULL_TREE);
22633 alignas_expr =
22634 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22635 /**cp_id_kind=*/NULL);
22636 if (alignas_expr == error_mark_node)
22637 cp_parser_skip_to_end_of_statement (parser);
22638 if (alignas_expr == NULL_TREE
22639 || alignas_expr == error_mark_node)
22640 return alignas_expr;
22643 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22645 cp_parser_error (parser, "expected %<)%>");
22646 return error_mark_node;
22649 alignas_expr = cxx_alignas_expr (alignas_expr);
22651 /* Build the C++-11 representation of an 'aligned'
22652 attribute. */
22653 attributes =
22654 build_tree_list (build_tree_list (get_identifier ("gnu"),
22655 get_identifier ("aligned")),
22656 build_tree_list (NULL_TREE, alignas_expr));
22659 return attributes;
22662 /* Parse a standard C++-11 attribute-specifier-seq.
22664 attribute-specifier-seq:
22665 attribute-specifier-seq [opt] attribute-specifier
22668 static tree
22669 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22671 tree attr_specs = NULL;
22673 while (true)
22675 tree attr_spec = cp_parser_std_attribute_spec (parser);
22676 if (attr_spec == NULL_TREE)
22677 break;
22678 if (attr_spec == error_mark_node)
22679 return error_mark_node;
22681 TREE_CHAIN (attr_spec) = attr_specs;
22682 attr_specs = attr_spec;
22685 attr_specs = nreverse (attr_specs);
22686 return attr_specs;
22689 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22690 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22691 current value of the PEDANTIC flag, regardless of whether or not
22692 the `__extension__' keyword is present. The caller is responsible
22693 for restoring the value of the PEDANTIC flag. */
22695 static bool
22696 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22698 /* Save the old value of the PEDANTIC flag. */
22699 *saved_pedantic = pedantic;
22701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22703 /* Consume the `__extension__' token. */
22704 cp_lexer_consume_token (parser->lexer);
22705 /* We're not being pedantic while the `__extension__' keyword is
22706 in effect. */
22707 pedantic = 0;
22709 return true;
22712 return false;
22715 /* Parse a label declaration.
22717 label-declaration:
22718 __label__ label-declarator-seq ;
22720 label-declarator-seq:
22721 identifier , label-declarator-seq
22722 identifier */
22724 static void
22725 cp_parser_label_declaration (cp_parser* parser)
22727 /* Look for the `__label__' keyword. */
22728 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22730 while (true)
22732 tree identifier;
22734 /* Look for an identifier. */
22735 identifier = cp_parser_identifier (parser);
22736 /* If we failed, stop. */
22737 if (identifier == error_mark_node)
22738 break;
22739 /* Declare it as a label. */
22740 finish_label_decl (identifier);
22741 /* If the next token is a `;', stop. */
22742 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22743 break;
22744 /* Look for the `,' separating the label declarations. */
22745 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22748 /* Look for the final `;'. */
22749 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22752 // -------------------------------------------------------------------------- //
22753 // Requires Clause
22755 // Parse a requires clause.
22757 // requires-clause:
22758 // 'requires' logical-or-expression
22760 // The required logical-or-expression must be a constant expression.
22761 static tree
22762 cp_parser_requires_clause (cp_parser *parser)
22764 // Parse the requires clause so that it is not automatically folded.
22765 ++processing_template_decl;
22766 tree expr =
22767 cp_parser_binary_expression (parser, false, false, PREC_NOT_OPERATOR, NULL);
22768 --processing_template_decl;
22769 if (!require_potential_rvalue_constant_expression (expr))
22770 return error_mark_node;
22771 return expr;
22774 // Optionally parse a requires clause:
22776 // requires-clause:
22777 // 'requires' logical-or-expression
22779 // The required logical-or-expression must be a constant expression.
22780 static tree
22781 cp_parser_requires_clause_opt (cp_parser *parser)
22783 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22784 return NULL_TREE;
22785 cp_lexer_consume_token (parser->lexer);
22786 return cp_parser_requires_clause (parser);
22790 // -------------------------------------------------------------------------- //
22791 // Requires Expression
22794 // An RAII helper that provides scoped control for entering and exiting
22795 // the local scope defined by a requires expression. Name bindings introduced
22796 // within the scope are popped prior to exiting the scope.
22797 struct cp_parser_requires_expr_scope
22799 // Enter a scope of kind K belonging to the decl D.
22800 cp_parser_requires_expr_scope ()
22802 begin_scope (sk_block, NULL_TREE);
22805 ~cp_parser_requires_expr_scope ()
22807 for (tree t = current_binding_level->names; t; t = DECL_CHAIN (t))
22808 pop_binding (DECL_NAME (t), t);
22809 leave_scope ();
22813 // Parse a requires expression
22815 // requirement-expression:
22816 // 'requires' requirement-parameter-list requirement-body
22817 static tree
22818 cp_parser_requires_expression (cp_parser *parser)
22820 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
22821 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
22823 // TODO: Earlier versions of the concepts lite spec did not allow
22824 // requires expressions outside of template declarations. That
22825 // restriction was relaxed in Chicago, but it has not been implemented.
22826 if (!processing_template_decl)
22828 error_at (loc, "a requires expression cannot appear outside a template");
22829 cp_parser_skip_to_end_of_statement (parser);
22830 return error_mark_node;
22834 // TODO: Check that requires expressions are only written inside of
22835 // template declarations. They don't need to be concepts, just templates.
22837 // Parse the optional parameter list. Any local parameter declarations
22838 // are added to a new scope and are visible within the nested
22839 // requirement list.
22840 cp_parser_requires_expr_scope guard;
22841 tree parms = cp_parser_requirement_parameter_list (parser);
22842 if (parms == error_mark_node)
22843 return error_mark_node;
22845 // Parse the requirement body.
22846 tree reqs = cp_parser_requirement_body (parser);
22847 if (reqs == error_mark_node)
22848 return error_mark_node;
22850 return finish_requires_expr (parms, reqs);
22853 // Parse a parameterized requirement.
22855 // requirement-parameter-list:
22856 // '(' parameter-declaration-clause ')'
22857 static tree
22858 cp_parser_requirement_parameter_list (cp_parser *parser)
22860 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22861 return error_mark_node;
22863 // Parse the nested parameter declaration clause.
22864 tree parms = cp_parser_parameter_declaration_clause (parser);
22865 if (parms == error_mark_node)
22866 return error_mark_node;
22868 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22869 return error_mark_node;
22871 return parms;
22874 // Parse the body of a requirement.
22876 // requirement-body:
22877 // '{' requirement-list '}'
22878 static tree
22879 cp_parser_requirement_body (cp_parser *parser)
22881 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22882 return error_mark_node;
22884 tree reqs = cp_parser_requirement_list (parser);
22886 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22887 return error_mark_node;
22889 return reqs;
22892 // Parse a list of requirements.
22894 // requirement-list:
22895 // requirement
22896 // requirement-list ';' requirement[opt]
22897 static tree
22898 cp_parser_requirement_list (cp_parser *parser)
22900 tree result = NULL_TREE;
22901 while (true)
22903 tree req = cp_parser_requirement (parser);
22904 if (req == error_mark_node)
22905 return req;
22907 result = tree_cons (NULL_TREE, req, result);
22909 // If we see a semi-colon, consume it.
22910 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22911 cp_lexer_consume_token (parser->lexer);
22913 // If we've found the end of the list, stop processing
22914 // the list.
22915 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22916 break;
22919 // Reverse the order of requirements so they are analyzed in
22920 // declaration order.
22921 return nreverse (result);
22924 // Parse a syntactic requirement or type requirement.
22926 // requirement:
22927 // simple-requirement
22928 // compound-requirement
22929 // type-requirement
22930 // nested-requirement
22931 static tree
22932 cp_parser_requirement (cp_parser *parser)
22934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
22935 return cp_parser_nested_requirement (parser);
22937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22938 return cp_parser_compound_requirement (parser);
22940 // Try parsing a type requirement first.
22941 cp_parser_parse_tentatively (parser);
22942 tree req = cp_parser_type_requirement (parser);
22943 if (!cp_parser_parse_definitely (parser))
22944 req = cp_parser_simple_requirement (parser);
22946 return req;
22949 // Parse a nested requirement. This is the same as a requires clause.
22951 // nested-requirement:
22952 // requires-clause
22953 static tree
22954 cp_parser_nested_requirement (cp_parser *parser)
22956 cp_lexer_consume_token (parser->lexer);
22957 tree req = cp_parser_requires_clause (parser);
22958 if (req == error_mark_node)
22959 return error_mark_node;
22960 return finish_nested_requirement (req);
22963 // Parse a simple requirement.
22965 // simple-requirement:
22966 // expression
22967 static tree
22968 cp_parser_simple_requirement (cp_parser *parser)
22970 tree expr = cp_parser_expression (parser, false, false, NULL);
22971 if (!expr || expr == error_mark_node)
22972 return error_mark_node;
22973 return finish_expr_requirement (expr);
22976 // Parse a compound requirement
22978 // compound-requirement:
22979 // '{' expression '}' trailing-constraint-specifiers
22981 // trailing-constraint-specifiers:
22982 // constraint-specifiers-seq[opt] result-type-requirement[opt]
22984 // result-type-requirement:
22985 // '->' type-id
22986 static tree
22987 cp_parser_compound_requirement (cp_parser *parser)
22989 // Parse an expression enclosed in '{ }'s.
22990 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22991 return error_mark_node;
22993 tree expr = cp_parser_expression (parser, false, false, NULL);
22994 if (!expr || expr == error_mark_node)
22995 return error_mark_node;
22997 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
22998 return error_mark_node;
23000 // Parse trailing expression specifiers.
23001 tree cs = cp_parser_constraint_specifier_seq (parser, expr);
23003 // Parse the optional trailing type requirement.
23004 tree type = NULL_TREE;
23005 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
23007 cp_lexer_consume_token (parser->lexer);
23008 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
23009 parser->in_result_type_constraint_p = true;
23010 type = cp_parser_trailing_type_id (parser);
23011 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
23012 if (type == error_mark_node)
23013 return error_mark_node;
23016 return finish_expr_requirement (expr, type, cs);
23019 // Parse a type requirement
23021 // type-requirement
23022 // type-id
23023 static tree
23024 cp_parser_type_requirement (cp_parser *parser)
23026 // Try parsing the type-id
23027 tree type = cp_parser_type_id (parser);
23028 if (type == error_mark_node)
23029 return error_mark_node;
23031 // It can only be a type requirement if nothing comes after it.
23032 // For example, this:
23034 // typename T::value_type x = a;
23036 // Is not a type requirement even though it stars with a type-id.
23037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23038 return error_mark_node;
23040 return finish_type_requirement (type);
23043 // Parse an optional constexpr specifier in a constraint expression.
23044 static tree
23045 cp_parser_constexpr_constraint_spec (cp_parser *parser, tree expr)
23047 cp_lexer_consume_token (parser->lexer);
23048 return finish_constexpr_requirement (expr);
23051 // Parse an optional noexcept specifier in a constraint expression.
23052 static tree
23053 cp_parser_noexcept_constraint_spec (cp_parser *parser, tree expr)
23055 cp_lexer_consume_token (parser->lexer);
23056 return finish_noexcept_requirement (expr);
23059 static tree
23060 cp_parser_constraint_spec (cp_parser *parser, tree expr)
23062 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONSTEXPR))
23063 return cp_parser_constexpr_constraint_spec (parser, expr);
23064 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
23065 return cp_parser_noexcept_constraint_spec (parser, expr);
23066 return NULL_TREE;
23069 // Parse an optional expression specifier sequence.
23071 // constraint-specifier-sequence:
23072 // constexpr [opt] noexcept [opt]
23073 static tree
23074 cp_parser_constraint_specifier_seq (cp_parser *parser, tree expr)
23076 tree result = NULL_TREE;
23077 while (1)
23079 // If we can parse a constraint specifier, insert it into
23080 // the list of requirements.
23081 if (tree spec = cp_parser_constraint_spec (parser, expr))
23083 result = tree_cons (NULL_TREE, spec, result);
23084 continue;
23086 break;
23088 return result;
23091 /* Support Functions */
23093 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
23094 NAME should have one of the representations used for an
23095 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
23096 is returned. If PARSER->SCOPE is a dependent type, then a
23097 SCOPE_REF is returned.
23099 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
23100 returned; the name was already resolved when the TEMPLATE_ID_EXPR
23101 was formed. Abstractly, such entities should not be passed to this
23102 function, because they do not need to be looked up, but it is
23103 simpler to check for this special case here, rather than at the
23104 call-sites.
23106 In cases not explicitly covered above, this function returns a
23107 DECL, OVERLOAD, or baselink representing the result of the lookup.
23108 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
23109 is returned.
23111 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
23112 (e.g., "struct") that was used. In that case bindings that do not
23113 refer to types are ignored.
23115 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
23116 ignored.
23118 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
23119 are ignored.
23121 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
23122 types.
23124 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
23125 TREE_LIST of candidates if name-lookup results in an ambiguity, and
23126 NULL_TREE otherwise. */
23128 static tree
23129 cp_parser_lookup_name (cp_parser *parser, tree name,
23130 enum tag_types tag_type,
23131 bool is_template,
23132 bool is_namespace,
23133 bool check_dependency,
23134 tree *ambiguous_decls,
23135 location_t name_location)
23137 tree decl;
23138 tree object_type = parser->context->object_type;
23140 /* Assume that the lookup will be unambiguous. */
23141 if (ambiguous_decls)
23142 *ambiguous_decls = NULL_TREE;
23144 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
23145 no longer valid. Note that if we are parsing tentatively, and
23146 the parse fails, OBJECT_TYPE will be automatically restored. */
23147 parser->context->object_type = NULL_TREE;
23149 if (name == error_mark_node)
23150 return error_mark_node;
23152 /* A template-id has already been resolved; there is no lookup to
23153 do. */
23154 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
23155 return name;
23156 if (BASELINK_P (name))
23158 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
23159 == TEMPLATE_ID_EXPR);
23160 return name;
23163 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
23164 it should already have been checked to make sure that the name
23165 used matches the type being destroyed. */
23166 if (TREE_CODE (name) == BIT_NOT_EXPR)
23168 tree type;
23170 /* Figure out to which type this destructor applies. */
23171 if (parser->scope)
23172 type = parser->scope;
23173 else if (object_type)
23174 type = object_type;
23175 else
23176 type = current_class_type;
23177 /* If that's not a class type, there is no destructor. */
23178 if (!type || !CLASS_TYPE_P (type))
23179 return error_mark_node;
23180 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
23181 lazily_declare_fn (sfk_destructor, type);
23182 if (!CLASSTYPE_DESTRUCTORS (type))
23183 return error_mark_node;
23184 /* If it was a class type, return the destructor. */
23185 return CLASSTYPE_DESTRUCTORS (type);
23188 /* By this point, the NAME should be an ordinary identifier. If
23189 the id-expression was a qualified name, the qualifying scope is
23190 stored in PARSER->SCOPE at this point. */
23191 gcc_assert (identifier_p (name));
23193 /* Perform the lookup. */
23194 if (parser->scope)
23196 bool dependent_p;
23198 if (parser->scope == error_mark_node)
23199 return error_mark_node;
23201 /* If the SCOPE is dependent, the lookup must be deferred until
23202 the template is instantiated -- unless we are explicitly
23203 looking up names in uninstantiated templates. Even then, we
23204 cannot look up the name if the scope is not a class type; it
23205 might, for example, be a template type parameter. */
23206 dependent_p = (TYPE_P (parser->scope)
23207 && dependent_scope_p (parser->scope));
23208 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
23209 && dependent_p)
23210 /* Defer lookup. */
23211 decl = error_mark_node;
23212 else
23214 tree pushed_scope = NULL_TREE;
23216 /* If PARSER->SCOPE is a dependent type, then it must be a
23217 class type, and we must not be checking dependencies;
23218 otherwise, we would have processed this lookup above. So
23219 that PARSER->SCOPE is not considered a dependent base by
23220 lookup_member, we must enter the scope here. */
23221 if (dependent_p)
23222 pushed_scope = push_scope (parser->scope);
23224 /* If the PARSER->SCOPE is a template specialization, it
23225 may be instantiated during name lookup. In that case,
23226 errors may be issued. Even if we rollback the current
23227 tentative parse, those errors are valid. */
23228 decl = lookup_qualified_name (parser->scope, name,
23229 tag_type != none_type,
23230 /*complain=*/true);
23232 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
23233 lookup result and the nested-name-specifier nominates a class C:
23234 * if the name specified after the nested-name-specifier, when
23235 looked up in C, is the injected-class-name of C (Clause 9), or
23236 * if the name specified after the nested-name-specifier is the
23237 same as the identifier or the simple-template-id's template-
23238 name in the last component of the nested-name-specifier,
23239 the name is instead considered to name the constructor of
23240 class C. [ Note: for example, the constructor is not an
23241 acceptable lookup result in an elaborated-type-specifier so
23242 the constructor would not be used in place of the
23243 injected-class-name. --end note ] Such a constructor name
23244 shall be used only in the declarator-id of a declaration that
23245 names a constructor or in a using-declaration. */
23246 if (tag_type == none_type
23247 && DECL_SELF_REFERENCE_P (decl)
23248 && same_type_p (DECL_CONTEXT (decl), parser->scope))
23249 decl = lookup_qualified_name (parser->scope, ctor_identifier,
23250 tag_type != none_type,
23251 /*complain=*/true);
23253 /* If we have a single function from a using decl, pull it out. */
23254 if (TREE_CODE (decl) == OVERLOAD
23255 && !really_overloaded_fn (decl))
23256 decl = OVL_FUNCTION (decl);
23258 if (pushed_scope)
23259 pop_scope (pushed_scope);
23262 /* If the scope is a dependent type and either we deferred lookup or
23263 we did lookup but didn't find the name, rememeber the name. */
23264 if (decl == error_mark_node && TYPE_P (parser->scope)
23265 && dependent_type_p (parser->scope))
23267 if (tag_type)
23269 tree type;
23271 /* The resolution to Core Issue 180 says that `struct
23272 A::B' should be considered a type-name, even if `A'
23273 is dependent. */
23274 type = make_typename_type (parser->scope, name, tag_type,
23275 /*complain=*/tf_error);
23276 if (type != error_mark_node)
23277 decl = TYPE_NAME (type);
23279 else if (is_template
23280 && (cp_parser_next_token_ends_template_argument_p (parser)
23281 || cp_lexer_next_token_is (parser->lexer,
23282 CPP_CLOSE_PAREN)))
23283 decl = make_unbound_class_template (parser->scope,
23284 name, NULL_TREE,
23285 /*complain=*/tf_error);
23286 else
23287 decl = build_qualified_name (/*type=*/NULL_TREE,
23288 parser->scope, name,
23289 is_template);
23291 parser->qualifying_scope = parser->scope;
23292 parser->object_scope = NULL_TREE;
23294 else if (object_type)
23296 /* Look up the name in the scope of the OBJECT_TYPE, unless the
23297 OBJECT_TYPE is not a class. */
23298 if (CLASS_TYPE_P (object_type))
23299 /* If the OBJECT_TYPE is a template specialization, it may
23300 be instantiated during name lookup. In that case, errors
23301 may be issued. Even if we rollback the current tentative
23302 parse, those errors are valid. */
23303 decl = lookup_member (object_type,
23304 name,
23305 /*protect=*/0,
23306 tag_type != none_type,
23307 tf_warning_or_error);
23308 else
23309 decl = NULL_TREE;
23311 if (!decl)
23312 /* Look it up in the enclosing context. */
23313 decl = lookup_name_real (name, tag_type != none_type,
23314 /*nonclass=*/0,
23315 /*block_p=*/true, is_namespace, 0);
23316 parser->object_scope = object_type;
23317 parser->qualifying_scope = NULL_TREE;
23319 else
23321 decl = lookup_name_real (name, tag_type != none_type,
23322 /*nonclass=*/0,
23323 /*block_p=*/true, is_namespace, 0);
23324 parser->qualifying_scope = NULL_TREE;
23325 parser->object_scope = NULL_TREE;
23328 /* If the lookup failed, let our caller know. */
23329 if (!decl || decl == error_mark_node)
23330 return error_mark_node;
23332 /* Pull out the template from an injected-class-name (or multiple). */
23333 if (is_template)
23334 decl = maybe_get_template_decl_from_type_decl (decl);
23336 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23337 if (TREE_CODE (decl) == TREE_LIST)
23339 if (ambiguous_decls)
23340 *ambiguous_decls = decl;
23341 /* The error message we have to print is too complicated for
23342 cp_parser_error, so we incorporate its actions directly. */
23343 if (!cp_parser_simulate_error (parser))
23345 error_at (name_location, "reference to %qD is ambiguous",
23346 name);
23347 print_candidates (decl);
23349 return error_mark_node;
23352 gcc_assert (DECL_P (decl)
23353 || TREE_CODE (decl) == OVERLOAD
23354 || TREE_CODE (decl) == SCOPE_REF
23355 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
23356 || BASELINK_P (decl));
23358 /* If we have resolved the name of a member declaration, check to
23359 see if the declaration is accessible. When the name resolves to
23360 set of overloaded functions, accessibility is checked when
23361 overload resolution is done.
23363 During an explicit instantiation, access is not checked at all,
23364 as per [temp.explicit]. */
23365 if (DECL_P (decl))
23366 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23368 maybe_record_typedef_use (decl);
23370 return decl;
23373 /* Like cp_parser_lookup_name, but for use in the typical case where
23374 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23375 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23377 static tree
23378 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23380 return cp_parser_lookup_name (parser, name,
23381 none_type,
23382 /*is_template=*/false,
23383 /*is_namespace=*/false,
23384 /*check_dependency=*/true,
23385 /*ambiguous_decls=*/NULL,
23386 location);
23389 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23390 the current context, return the TYPE_DECL. If TAG_NAME_P is
23391 true, the DECL indicates the class being defined in a class-head,
23392 or declared in an elaborated-type-specifier.
23394 Otherwise, return DECL. */
23396 static tree
23397 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23399 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23400 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23402 struct A {
23403 template <typename T> struct B;
23406 template <typename T> struct A::B {};
23408 Similarly, in an elaborated-type-specifier:
23410 namespace N { struct X{}; }
23412 struct A {
23413 template <typename T> friend struct N::X;
23416 However, if the DECL refers to a class type, and we are in
23417 the scope of the class, then the name lookup automatically
23418 finds the TYPE_DECL created by build_self_reference rather
23419 than a TEMPLATE_DECL. For example, in:
23421 template <class T> struct S {
23422 S s;
23425 there is no need to handle such case. */
23427 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23428 return DECL_TEMPLATE_RESULT (decl);
23430 return decl;
23433 /* If too many, or too few, template-parameter lists apply to the
23434 declarator, issue an error message. Returns TRUE if all went well,
23435 and FALSE otherwise. */
23437 static bool
23438 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23439 cp_declarator *declarator,
23440 location_t declarator_location)
23442 switch (declarator->kind)
23444 case cdk_id:
23446 unsigned num_templates = 0;
23447 tree scope = declarator->u.id.qualifying_scope;
23449 if (scope)
23450 num_templates = num_template_headers_for_class (scope);
23451 else if (TREE_CODE (declarator->u.id.unqualified_name)
23452 == TEMPLATE_ID_EXPR)
23453 /* If the DECLARATOR has the form `X<y>' then it uses one
23454 additional level of template parameters. */
23455 ++num_templates;
23457 return cp_parser_check_template_parameters
23458 (parser, num_templates, declarator_location, declarator);
23461 case cdk_function:
23462 case cdk_array:
23463 case cdk_pointer:
23464 case cdk_reference:
23465 case cdk_ptrmem:
23466 return (cp_parser_check_declarator_template_parameters
23467 (parser, declarator->declarator, declarator_location));
23469 case cdk_error:
23470 return true;
23472 default:
23473 gcc_unreachable ();
23475 return false;
23478 /* NUM_TEMPLATES were used in the current declaration. If that is
23479 invalid, return FALSE and issue an error messages. Otherwise,
23480 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23481 declarator and we can print more accurate diagnostics. */
23483 static bool
23484 cp_parser_check_template_parameters (cp_parser* parser,
23485 unsigned num_templates,
23486 location_t location,
23487 cp_declarator *declarator)
23489 /* If there are the same number of template classes and parameter
23490 lists, that's OK. */
23491 if (parser->num_template_parameter_lists == num_templates)
23492 return true;
23493 /* If there are more, but only one more, then we are referring to a
23494 member template. That's OK too. */
23495 if (parser->num_template_parameter_lists == num_templates + 1)
23496 return true;
23497 /* If there are more template classes than parameter lists, we have
23498 something like:
23500 template <class T> void S<T>::R<T>::f (); */
23501 if (parser->num_template_parameter_lists < num_templates)
23503 if (declarator && !current_function_decl)
23504 error_at (location, "specializing member %<%T::%E%> "
23505 "requires %<template<>%> syntax",
23506 declarator->u.id.qualifying_scope,
23507 declarator->u.id.unqualified_name);
23508 else if (declarator)
23509 error_at (location, "invalid declaration of %<%T::%E%>",
23510 declarator->u.id.qualifying_scope,
23511 declarator->u.id.unqualified_name);
23512 else
23513 error_at (location, "too few template-parameter-lists");
23514 return false;
23516 /* Otherwise, there are too many template parameter lists. We have
23517 something like:
23519 template <class T> template <class U> void S::f(); */
23520 error_at (location, "too many template-parameter-lists");
23521 return false;
23524 /* Parse an optional `::' token indicating that the following name is
23525 from the global namespace. If so, PARSER->SCOPE is set to the
23526 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23527 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23528 Returns the new value of PARSER->SCOPE, if the `::' token is
23529 present, and NULL_TREE otherwise. */
23531 static tree
23532 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23534 cp_token *token;
23536 /* Peek at the next token. */
23537 token = cp_lexer_peek_token (parser->lexer);
23538 /* If we're looking at a `::' token then we're starting from the
23539 global namespace, not our current location. */
23540 if (token->type == CPP_SCOPE)
23542 /* Consume the `::' token. */
23543 cp_lexer_consume_token (parser->lexer);
23544 /* Set the SCOPE so that we know where to start the lookup. */
23545 parser->scope = global_namespace;
23546 parser->qualifying_scope = global_namespace;
23547 parser->object_scope = NULL_TREE;
23549 return parser->scope;
23551 else if (!current_scope_valid_p)
23553 parser->scope = NULL_TREE;
23554 parser->qualifying_scope = NULL_TREE;
23555 parser->object_scope = NULL_TREE;
23558 return NULL_TREE;
23561 /* Returns TRUE if the upcoming token sequence is the start of a
23562 constructor declarator. If FRIEND_P is true, the declarator is
23563 preceded by the `friend' specifier. */
23565 static bool
23566 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23568 bool constructor_p;
23569 bool outside_class_specifier_p;
23570 tree nested_name_specifier;
23571 cp_token *next_token;
23573 /* The common case is that this is not a constructor declarator, so
23574 try to avoid doing lots of work if at all possible. It's not
23575 valid declare a constructor at function scope. */
23576 if (parser->in_function_body)
23577 return false;
23578 /* And only certain tokens can begin a constructor declarator. */
23579 next_token = cp_lexer_peek_token (parser->lexer);
23580 if (next_token->type != CPP_NAME
23581 && next_token->type != CPP_SCOPE
23582 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23583 && next_token->type != CPP_TEMPLATE_ID)
23584 return false;
23586 /* Parse tentatively; we are going to roll back all of the tokens
23587 consumed here. */
23588 cp_parser_parse_tentatively (parser);
23589 /* Assume that we are looking at a constructor declarator. */
23590 constructor_p = true;
23592 /* Look for the optional `::' operator. */
23593 cp_parser_global_scope_opt (parser,
23594 /*current_scope_valid_p=*/false);
23595 /* Look for the nested-name-specifier. */
23596 nested_name_specifier
23597 = (cp_parser_nested_name_specifier_opt (parser,
23598 /*typename_keyword_p=*/false,
23599 /*check_dependency_p=*/false,
23600 /*type_p=*/false,
23601 /*is_declaration=*/false));
23603 outside_class_specifier_p = (!at_class_scope_p ()
23604 || !TYPE_BEING_DEFINED (current_class_type)
23605 || friend_p);
23607 /* Outside of a class-specifier, there must be a
23608 nested-name-specifier. */
23609 if (!nested_name_specifier && outside_class_specifier_p)
23610 constructor_p = false;
23611 else if (nested_name_specifier == error_mark_node)
23612 constructor_p = false;
23614 /* If we have a class scope, this is easy; DR 147 says that S::S always
23615 names the constructor, and no other qualified name could. */
23616 if (constructor_p && nested_name_specifier
23617 && CLASS_TYPE_P (nested_name_specifier))
23619 tree id = cp_parser_unqualified_id (parser,
23620 /*template_keyword_p=*/false,
23621 /*check_dependency_p=*/false,
23622 /*declarator_p=*/true,
23623 /*optional_p=*/false);
23624 if (is_overloaded_fn (id))
23625 id = DECL_NAME (get_first_fn (id));
23626 if (!constructor_name_p (id, nested_name_specifier))
23627 constructor_p = false;
23629 /* If we still think that this might be a constructor-declarator,
23630 look for a class-name. */
23631 else if (constructor_p)
23633 /* If we have:
23635 template <typename T> struct S {
23636 S();
23639 we must recognize that the nested `S' names a class. */
23640 tree type_decl;
23641 type_decl = cp_parser_class_name (parser,
23642 /*typename_keyword_p=*/false,
23643 /*template_keyword_p=*/false,
23644 none_type,
23645 /*check_dependency_p=*/false,
23646 /*class_head_p=*/false,
23647 /*is_declaration=*/false);
23648 /* If there was no class-name, then this is not a constructor.
23649 Otherwise, if we are in a class-specifier and we aren't
23650 handling a friend declaration, check that its type matches
23651 current_class_type (c++/38313). Note: error_mark_node
23652 is left alone for error recovery purposes. */
23653 constructor_p = (!cp_parser_error_occurred (parser)
23654 && (outside_class_specifier_p
23655 || type_decl == error_mark_node
23656 || same_type_p (current_class_type,
23657 TREE_TYPE (type_decl))));
23659 /* If we're still considering a constructor, we have to see a `(',
23660 to begin the parameter-declaration-clause, followed by either a
23661 `)', an `...', or a decl-specifier. We need to check for a
23662 type-specifier to avoid being fooled into thinking that:
23664 S (f) (int);
23666 is a constructor. (It is actually a function named `f' that
23667 takes one parameter (of type `int') and returns a value of type
23668 `S'. */
23669 if (constructor_p
23670 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23671 constructor_p = false;
23673 if (constructor_p
23674 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23675 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23676 /* A parameter declaration begins with a decl-specifier,
23677 which is either the "attribute" keyword, a storage class
23678 specifier, or (usually) a type-specifier. */
23679 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23681 tree type;
23682 tree pushed_scope = NULL_TREE;
23683 unsigned saved_num_template_parameter_lists;
23685 /* Names appearing in the type-specifier should be looked up
23686 in the scope of the class. */
23687 if (current_class_type)
23688 type = NULL_TREE;
23689 else
23691 type = TREE_TYPE (type_decl);
23692 if (TREE_CODE (type) == TYPENAME_TYPE)
23694 type = resolve_typename_type (type,
23695 /*only_current_p=*/false);
23696 if (TREE_CODE (type) == TYPENAME_TYPE)
23698 cp_parser_abort_tentative_parse (parser);
23699 return false;
23702 pushed_scope = push_scope (type);
23705 /* Inside the constructor parameter list, surrounding
23706 template-parameter-lists do not apply. */
23707 saved_num_template_parameter_lists
23708 = parser->num_template_parameter_lists;
23709 parser->num_template_parameter_lists = 0;
23711 /* Look for the type-specifier. */
23712 cp_parser_type_specifier (parser,
23713 CP_PARSER_FLAGS_NONE,
23714 /*decl_specs=*/NULL,
23715 /*is_declarator=*/true,
23716 /*declares_class_or_enum=*/NULL,
23717 /*is_cv_qualifier=*/NULL);
23719 parser->num_template_parameter_lists
23720 = saved_num_template_parameter_lists;
23722 /* Leave the scope of the class. */
23723 if (pushed_scope)
23724 pop_scope (pushed_scope);
23726 constructor_p = !cp_parser_error_occurred (parser);
23730 /* We did not really want to consume any tokens. */
23731 cp_parser_abort_tentative_parse (parser);
23733 return constructor_p;
23736 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23737 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23738 they must be performed once we are in the scope of the function.
23740 Returns the function defined. */
23742 static tree
23743 cp_parser_function_definition_from_specifiers_and_declarator
23744 (cp_parser* parser,
23745 cp_decl_specifier_seq *decl_specifiers,
23746 tree attributes,
23747 const cp_declarator *declarator)
23749 tree fn;
23750 bool success_p;
23752 /* Begin the function-definition. */
23753 success_p = start_function (decl_specifiers, declarator, attributes);
23755 /* The things we're about to see are not directly qualified by any
23756 template headers we've seen thus far. */
23757 reset_specialization ();
23759 /* If there were names looked up in the decl-specifier-seq that we
23760 did not check, check them now. We must wait until we are in the
23761 scope of the function to perform the checks, since the function
23762 might be a friend. */
23763 perform_deferred_access_checks (tf_warning_or_error);
23765 if (success_p)
23767 cp_finalize_omp_declare_simd (parser, current_function_decl);
23768 parser->omp_declare_simd = NULL;
23771 if (!success_p)
23773 /* Skip the entire function. */
23774 cp_parser_skip_to_end_of_block_or_statement (parser);
23775 fn = error_mark_node;
23777 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23779 /* Seen already, skip it. An error message has already been output. */
23780 cp_parser_skip_to_end_of_block_or_statement (parser);
23781 fn = current_function_decl;
23782 current_function_decl = NULL_TREE;
23783 /* If this is a function from a class, pop the nested class. */
23784 if (current_class_name)
23785 pop_nested_class ();
23787 else
23789 timevar_id_t tv;
23790 if (DECL_DECLARED_INLINE_P (current_function_decl))
23791 tv = TV_PARSE_INLINE;
23792 else
23793 tv = TV_PARSE_FUNC;
23794 timevar_push (tv);
23795 fn = cp_parser_function_definition_after_declarator (parser,
23796 /*inline_p=*/false);
23797 timevar_pop (tv);
23800 return fn;
23803 /* Parse the part of a function-definition that follows the
23804 declarator. INLINE_P is TRUE iff this function is an inline
23805 function defined within a class-specifier.
23807 Returns the function defined. */
23809 static tree
23810 cp_parser_function_definition_after_declarator (cp_parser* parser,
23811 bool inline_p)
23813 tree fn;
23814 bool ctor_initializer_p = false;
23815 bool saved_in_unbraced_linkage_specification_p;
23816 bool saved_in_function_body;
23817 unsigned saved_num_template_parameter_lists;
23818 cp_token *token;
23819 bool fully_implicit_function_template_p
23820 = parser->fully_implicit_function_template_p;
23821 parser->fully_implicit_function_template_p = false;
23822 tree implicit_template_parms
23823 = parser->implicit_template_parms;
23824 parser->implicit_template_parms = 0;
23825 cp_binding_level* implicit_template_scope
23826 = parser->implicit_template_scope;
23827 parser->implicit_template_scope = 0;
23829 saved_in_function_body = parser->in_function_body;
23830 parser->in_function_body = true;
23831 /* If the next token is `return', then the code may be trying to
23832 make use of the "named return value" extension that G++ used to
23833 support. */
23834 token = cp_lexer_peek_token (parser->lexer);
23835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23837 /* Consume the `return' keyword. */
23838 cp_lexer_consume_token (parser->lexer);
23839 /* Look for the identifier that indicates what value is to be
23840 returned. */
23841 cp_parser_identifier (parser);
23842 /* Issue an error message. */
23843 error_at (token->location,
23844 "named return values are no longer supported");
23845 /* Skip tokens until we reach the start of the function body. */
23846 while (true)
23848 cp_token *token = cp_lexer_peek_token (parser->lexer);
23849 if (token->type == CPP_OPEN_BRACE
23850 || token->type == CPP_EOF
23851 || token->type == CPP_PRAGMA_EOL)
23852 break;
23853 cp_lexer_consume_token (parser->lexer);
23856 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23857 anything declared inside `f'. */
23858 saved_in_unbraced_linkage_specification_p
23859 = parser->in_unbraced_linkage_specification_p;
23860 parser->in_unbraced_linkage_specification_p = false;
23861 /* Inside the function, surrounding template-parameter-lists do not
23862 apply. */
23863 saved_num_template_parameter_lists
23864 = parser->num_template_parameter_lists;
23865 parser->num_template_parameter_lists = 0;
23867 start_lambda_scope (current_function_decl);
23869 /* If the next token is `try', `__transaction_atomic', or
23870 `__transaction_relaxed`, then we are looking at either function-try-block
23871 or function-transaction-block. Note that all of these include the
23872 function-body. */
23873 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23874 ctor_initializer_p = cp_parser_function_transaction (parser,
23875 RID_TRANSACTION_ATOMIC);
23876 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23877 RID_TRANSACTION_RELAXED))
23878 ctor_initializer_p = cp_parser_function_transaction (parser,
23879 RID_TRANSACTION_RELAXED);
23880 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23881 ctor_initializer_p = cp_parser_function_try_block (parser);
23882 else
23883 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23884 (parser, /*in_function_try_block=*/false);
23886 finish_lambda_scope ();
23888 /* Finish the function. */
23889 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23890 (inline_p ? 2 : 0));
23891 /* Generate code for it, if necessary. */
23892 expand_or_defer_fn (fn);
23893 /* Restore the saved values. */
23894 parser->in_unbraced_linkage_specification_p
23895 = saved_in_unbraced_linkage_specification_p;
23896 parser->num_template_parameter_lists
23897 = saved_num_template_parameter_lists;
23898 parser->in_function_body = saved_in_function_body;
23900 parser->fully_implicit_function_template_p
23901 = fully_implicit_function_template_p;
23902 parser->implicit_template_parms
23903 = implicit_template_parms;
23904 parser->implicit_template_scope
23905 = implicit_template_scope;
23907 if (parser->fully_implicit_function_template_p)
23908 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23910 return fn;
23913 /* Parse a template-declaration, assuming that the `export' (and
23914 `extern') keywords, if present, has already been scanned. MEMBER_P
23915 is as for cp_parser_template_declaration. */
23917 static void
23918 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23920 tree decl = NULL_TREE;
23921 vec<deferred_access_check, va_gc> *checks;
23922 tree parameter_list;
23923 bool friend_p = false;
23924 bool need_lang_pop;
23925 cp_token *token;
23927 /* Look for the `template' keyword. */
23928 token = cp_lexer_peek_token (parser->lexer);
23929 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23930 return;
23932 /* And the `<'. */
23933 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23934 return;
23935 if (at_class_scope_p () && current_function_decl)
23937 /* 14.5.2.2 [temp.mem]
23939 A local class shall not have member templates. */
23940 error_at (token->location,
23941 "invalid declaration of member template in local class");
23942 cp_parser_skip_to_end_of_block_or_statement (parser);
23943 return;
23945 /* [temp]
23947 A template ... shall not have C linkage. */
23948 if (current_lang_name == lang_name_c)
23950 error_at (token->location, "template with C linkage");
23951 /* Give it C++ linkage to avoid confusing other parts of the
23952 front end. */
23953 push_lang_context (lang_name_cplusplus);
23954 need_lang_pop = true;
23956 else
23957 need_lang_pop = false;
23959 /* We cannot perform access checks on the template parameter
23960 declarations until we know what is being declared, just as we
23961 cannot check the decl-specifier list. */
23962 push_deferring_access_checks (dk_deferred);
23964 // Save the current template requirements.
23965 tree saved_template_reqs = release (current_template_reqs);
23967 /* If the next token is `>', then we have an invalid
23968 specialization. Rather than complain about an invalid template
23969 parameter, issue an error message here. */
23970 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23972 cp_parser_error (parser, "invalid explicit specialization");
23973 begin_specialization ();
23974 parameter_list = NULL_TREE;
23976 else
23978 /* Parse the template parameters. */
23979 parameter_list = cp_parser_template_parameter_list (parser);
23982 /* Get the deferred access checks from the parameter list. These
23983 will be checked once we know what is being declared, as for a
23984 member template the checks must be performed in the scope of the
23985 class containing the member. */
23986 checks = get_deferred_access_checks ();
23988 /* Look for the `>'. */
23989 cp_parser_skip_to_end_of_template_parameter_list (parser);
23990 /* We just processed one more parameter list. */
23991 ++parser->num_template_parameter_lists;
23993 // Manage template requirements
23994 if (flag_concepts)
23996 tree reqs = get_shorthand_requirements (current_template_parms);
23997 if (tree r = cp_parser_requires_clause_opt (parser))
23998 reqs = conjoin_requirements (reqs, r);
23999 current_template_reqs = finish_template_requirements (reqs);
24001 // Attach the constraints to the template parameter list.
24002 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
24003 = current_template_reqs;
24006 /* If the next token is `template', there are more template
24007 parameters. */
24008 if (cp_lexer_next_token_is_keyword (parser->lexer,
24009 RID_TEMPLATE))
24010 cp_parser_template_declaration_after_export (parser, member_p);
24011 else if (cxx_dialect >= cxx11
24012 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24013 decl = cp_parser_alias_declaration (parser);
24014 else
24016 /* There are no access checks when parsing a template, as we do not
24017 know if a specialization will be a friend. */
24018 push_deferring_access_checks (dk_no_check);
24019 token = cp_lexer_peek_token (parser->lexer);
24020 decl = cp_parser_single_declaration (parser,
24021 checks,
24022 member_p,
24023 /*explicit_specialization_p=*/false,
24024 &friend_p);
24025 pop_deferring_access_checks ();
24027 /* If this is a member template declaration, let the front
24028 end know. */
24029 if (member_p && !friend_p && decl)
24031 if (TREE_CODE (decl) == TYPE_DECL)
24032 cp_parser_check_access_in_redeclaration (decl, token->location);
24034 decl = finish_member_template_decl (decl);
24036 else if (friend_p && decl
24037 && DECL_DECLARES_TYPE_P (decl))
24038 make_friend_class (current_class_type, TREE_TYPE (decl),
24039 /*complain=*/true);
24041 /* We are done with the current parameter list. */
24042 --parser->num_template_parameter_lists;
24044 pop_deferring_access_checks ();
24046 /* Finish up. */
24047 finish_template_decl (parameter_list);
24049 // Restore the current template requirements.
24050 current_template_reqs = saved_template_reqs;
24052 /* Check the template arguments for a literal operator template. */
24053 if (decl
24054 && DECL_DECLARES_FUNCTION_P (decl)
24055 && UDLIT_OPER_P (DECL_NAME (decl)))
24057 bool ok = true;
24058 if (parameter_list == NULL_TREE)
24059 ok = false;
24060 else
24062 int num_parms = TREE_VEC_LENGTH (parameter_list);
24063 if (num_parms == 1)
24065 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
24066 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24067 if (TREE_TYPE (parm) != char_type_node
24068 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24069 ok = false;
24071 else if (num_parms == 2 && cxx_dialect >= cxx1y)
24073 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
24074 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
24075 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
24076 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24077 if (TREE_TYPE (parm) != TREE_TYPE (type)
24078 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24079 ok = false;
24081 else
24082 ok = false;
24084 if (!ok)
24085 error ("literal operator template %qD has invalid parameter list."
24086 " Expected non-type template argument pack <char...>"
24087 " or <typename CharT, CharT...>",
24088 decl);
24090 /* Register member declarations. */
24091 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
24092 finish_member_declaration (decl);
24093 /* For the erroneous case of a template with C linkage, we pushed an
24094 implicit C++ linkage scope; exit that scope now. */
24095 if (need_lang_pop)
24096 pop_lang_context ();
24097 /* If DECL is a function template, we must return to parse it later.
24098 (Even though there is no definition, there might be default
24099 arguments that need handling.) */
24100 if (member_p && decl
24101 && DECL_DECLARES_FUNCTION_P (decl))
24102 vec_safe_push (unparsed_funs_with_definitions, decl);
24105 /* Perform the deferred access checks from a template-parameter-list.
24106 CHECKS is a TREE_LIST of access checks, as returned by
24107 get_deferred_access_checks. */
24109 static void
24110 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
24112 ++processing_template_parmlist;
24113 perform_access_checks (checks, tf_warning_or_error);
24114 --processing_template_parmlist;
24117 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
24118 `function-definition' sequence that follows a template header.
24119 If MEMBER_P is true, this declaration appears in a class scope.
24121 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
24122 *FRIEND_P is set to TRUE iff the declaration is a friend. */
24124 static tree
24125 cp_parser_single_declaration (cp_parser* parser,
24126 vec<deferred_access_check, va_gc> *checks,
24127 bool member_p,
24128 bool explicit_specialization_p,
24129 bool* friend_p)
24131 int declares_class_or_enum;
24132 tree decl = NULL_TREE;
24133 cp_decl_specifier_seq decl_specifiers;
24134 bool function_definition_p = false;
24135 cp_token *decl_spec_token_start;
24137 /* This function is only used when processing a template
24138 declaration. */
24139 gcc_assert (innermost_scope_kind () == sk_template_parms
24140 || innermost_scope_kind () == sk_template_spec);
24142 /* Defer access checks until we know what is being declared. */
24143 push_deferring_access_checks (dk_deferred);
24145 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
24146 alternative. */
24147 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24148 cp_parser_decl_specifier_seq (parser,
24149 CP_PARSER_FLAGS_OPTIONAL,
24150 &decl_specifiers,
24151 &declares_class_or_enum);
24152 if (friend_p)
24153 *friend_p = cp_parser_friend_p (&decl_specifiers);
24155 /* There are no template typedefs. */
24156 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
24158 error_at (decl_spec_token_start->location,
24159 "template declaration of %<typedef%>");
24160 decl = error_mark_node;
24163 /* Gather up the access checks that occurred the
24164 decl-specifier-seq. */
24165 stop_deferring_access_checks ();
24167 /* Check for the declaration of a template class. */
24168 if (declares_class_or_enum)
24170 if (cp_parser_declares_only_class_p (parser))
24172 decl = shadow_tag (&decl_specifiers);
24174 /* In this case:
24176 struct C {
24177 friend template <typename T> struct A<T>::B;
24180 A<T>::B will be represented by a TYPENAME_TYPE, and
24181 therefore not recognized by shadow_tag. */
24182 if (friend_p && *friend_p
24183 && !decl
24184 && decl_specifiers.type
24185 && TYPE_P (decl_specifiers.type))
24186 decl = decl_specifiers.type;
24188 if (decl && decl != error_mark_node)
24189 decl = TYPE_NAME (decl);
24190 else
24191 decl = error_mark_node;
24193 /* Perform access checks for template parameters. */
24194 cp_parser_perform_template_parameter_access_checks (checks);
24198 /* Complain about missing 'typename' or other invalid type names. */
24199 if (!decl_specifiers.any_type_specifiers_p
24200 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24202 /* cp_parser_parse_and_diagnose_invalid_type_name calls
24203 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
24204 the rest of this declaration. */
24205 decl = error_mark_node;
24206 goto out;
24209 /* If it's not a template class, try for a template function. If
24210 the next token is a `;', then this declaration does not declare
24211 anything. But, if there were errors in the decl-specifiers, then
24212 the error might well have come from an attempted class-specifier.
24213 In that case, there's no need to warn about a missing declarator. */
24214 if (!decl
24215 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
24216 || decl_specifiers.type != error_mark_node))
24218 decl = cp_parser_init_declarator (parser,
24219 &decl_specifiers,
24220 checks,
24221 /*function_definition_allowed_p=*/true,
24222 member_p,
24223 declares_class_or_enum,
24224 &function_definition_p,
24225 NULL);
24227 /* 7.1.1-1 [dcl.stc]
24229 A storage-class-specifier shall not be specified in an explicit
24230 specialization... */
24231 if (decl
24232 && explicit_specialization_p
24233 && decl_specifiers.storage_class != sc_none)
24235 error_at (decl_spec_token_start->location,
24236 "explicit template specialization cannot have a storage class");
24237 decl = error_mark_node;
24240 if (decl && VAR_P (decl))
24241 check_template_variable (decl);
24244 /* Look for a trailing `;' after the declaration. */
24245 if (!function_definition_p
24246 && (decl == error_mark_node
24247 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
24248 cp_parser_skip_to_end_of_block_or_statement (parser);
24250 out:
24251 pop_deferring_access_checks ();
24253 /* Clear any current qualification; whatever comes next is the start
24254 of something new. */
24255 parser->scope = NULL_TREE;
24256 parser->qualifying_scope = NULL_TREE;
24257 parser->object_scope = NULL_TREE;
24259 return decl;
24262 /* Parse a cast-expression that is not the operand of a unary "&". */
24264 static tree
24265 cp_parser_simple_cast_expression (cp_parser *parser)
24267 return cp_parser_cast_expression (parser, /*address_p=*/false,
24268 /*cast_p=*/false, /*decltype*/false, NULL);
24271 /* Parse a functional cast to TYPE. Returns an expression
24272 representing the cast. */
24274 static tree
24275 cp_parser_functional_cast (cp_parser* parser, tree type)
24277 vec<tree, va_gc> *vec;
24278 tree expression_list;
24279 tree cast;
24280 bool nonconst_p;
24282 if (!type)
24283 type = error_mark_node;
24285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24287 cp_lexer_set_source_position (parser->lexer);
24288 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24289 expression_list = cp_parser_braced_list (parser, &nonconst_p);
24290 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
24291 if (TREE_CODE (type) == TYPE_DECL)
24292 type = TREE_TYPE (type);
24293 return finish_compound_literal (type, expression_list,
24294 tf_warning_or_error);
24298 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
24299 /*cast_p=*/true,
24300 /*allow_expansion_p=*/true,
24301 /*non_constant_p=*/NULL);
24302 if (vec == NULL)
24303 expression_list = error_mark_node;
24304 else
24306 expression_list = build_tree_list_vec (vec);
24307 release_tree_vector (vec);
24310 cast = build_functional_cast (type, expression_list,
24311 tf_warning_or_error);
24312 /* [expr.const]/1: In an integral constant expression "only type
24313 conversions to integral or enumeration type can be used". */
24314 if (TREE_CODE (type) == TYPE_DECL)
24315 type = TREE_TYPE (type);
24316 if (cast != error_mark_node
24317 && !cast_valid_in_integral_constant_expression_p (type)
24318 && cp_parser_non_integral_constant_expression (parser,
24319 NIC_CONSTRUCTOR))
24320 return error_mark_node;
24321 return cast;
24324 /* Save the tokens that make up the body of a member function defined
24325 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
24326 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
24327 specifiers applied to the declaration. Returns the FUNCTION_DECL
24328 for the member function. */
24330 static tree
24331 cp_parser_save_member_function_body (cp_parser* parser,
24332 cp_decl_specifier_seq *decl_specifiers,
24333 cp_declarator *declarator,
24334 tree attributes)
24336 cp_token *first;
24337 cp_token *last;
24338 tree fn;
24340 /* Create the FUNCTION_DECL. */
24341 fn = grokmethod (decl_specifiers, declarator, attributes);
24342 cp_finalize_omp_declare_simd (parser, fn);
24343 /* If something went badly wrong, bail out now. */
24344 if (fn == error_mark_node)
24346 /* If there's a function-body, skip it. */
24347 if (cp_parser_token_starts_function_definition_p
24348 (cp_lexer_peek_token (parser->lexer)))
24349 cp_parser_skip_to_end_of_block_or_statement (parser);
24350 return error_mark_node;
24353 /* Remember it, if there default args to post process. */
24354 cp_parser_save_default_args (parser, fn);
24356 /* Save away the tokens that make up the body of the
24357 function. */
24358 first = parser->lexer->next_token;
24359 /* Handle function try blocks. */
24360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24361 cp_lexer_consume_token (parser->lexer);
24362 /* We can have braced-init-list mem-initializers before the fn body. */
24363 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24365 cp_lexer_consume_token (parser->lexer);
24366 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24368 /* cache_group will stop after an un-nested { } pair, too. */
24369 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24370 break;
24372 /* variadic mem-inits have ... after the ')'. */
24373 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24374 cp_lexer_consume_token (parser->lexer);
24377 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24378 /* Handle function try blocks. */
24379 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24380 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24381 last = parser->lexer->next_token;
24383 /* Save away the inline definition; we will process it when the
24384 class is complete. */
24385 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24386 DECL_PENDING_INLINE_P (fn) = 1;
24388 /* We need to know that this was defined in the class, so that
24389 friend templates are handled correctly. */
24390 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24392 /* Add FN to the queue of functions to be parsed later. */
24393 vec_safe_push (unparsed_funs_with_definitions, fn);
24395 return fn;
24398 /* Save the tokens that make up the in-class initializer for a non-static
24399 data member. Returns a DEFAULT_ARG. */
24401 static tree
24402 cp_parser_save_nsdmi (cp_parser* parser)
24404 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24407 /* Parse a template-argument-list, as well as the trailing ">" (but
24408 not the opening "<"). See cp_parser_template_argument_list for the
24409 return value. */
24411 static tree
24412 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24414 tree arguments;
24415 tree saved_scope;
24416 tree saved_qualifying_scope;
24417 tree saved_object_scope;
24418 bool saved_greater_than_is_operator_p;
24419 int saved_unevaluated_operand;
24420 int saved_inhibit_evaluation_warnings;
24422 /* [temp.names]
24424 When parsing a template-id, the first non-nested `>' is taken as
24425 the end of the template-argument-list rather than a greater-than
24426 operator. */
24427 saved_greater_than_is_operator_p
24428 = parser->greater_than_is_operator_p;
24429 parser->greater_than_is_operator_p = false;
24430 /* Parsing the argument list may modify SCOPE, so we save it
24431 here. */
24432 saved_scope = parser->scope;
24433 saved_qualifying_scope = parser->qualifying_scope;
24434 saved_object_scope = parser->object_scope;
24435 /* We need to evaluate the template arguments, even though this
24436 template-id may be nested within a "sizeof". */
24437 saved_unevaluated_operand = cp_unevaluated_operand;
24438 cp_unevaluated_operand = 0;
24439 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24440 c_inhibit_evaluation_warnings = 0;
24441 /* Parse the template-argument-list itself. */
24442 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24443 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24444 arguments = NULL_TREE;
24445 else
24446 arguments = cp_parser_template_argument_list (parser);
24447 /* Look for the `>' that ends the template-argument-list. If we find
24448 a '>>' instead, it's probably just a typo. */
24449 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24451 if (cxx_dialect != cxx98)
24453 /* In C++0x, a `>>' in a template argument list or cast
24454 expression is considered to be two separate `>'
24455 tokens. So, change the current token to a `>', but don't
24456 consume it: it will be consumed later when the outer
24457 template argument list (or cast expression) is parsed.
24458 Note that this replacement of `>' for `>>' is necessary
24459 even if we are parsing tentatively: in the tentative
24460 case, after calling
24461 cp_parser_enclosed_template_argument_list we will always
24462 throw away all of the template arguments and the first
24463 closing `>', either because the template argument list
24464 was erroneous or because we are replacing those tokens
24465 with a CPP_TEMPLATE_ID token. The second `>' (which will
24466 not have been thrown away) is needed either to close an
24467 outer template argument list or to complete a new-style
24468 cast. */
24469 cp_token *token = cp_lexer_peek_token (parser->lexer);
24470 token->type = CPP_GREATER;
24472 else if (!saved_greater_than_is_operator_p)
24474 /* If we're in a nested template argument list, the '>>' has
24475 to be a typo for '> >'. We emit the error message, but we
24476 continue parsing and we push a '>' as next token, so that
24477 the argument list will be parsed correctly. Note that the
24478 global source location is still on the token before the
24479 '>>', so we need to say explicitly where we want it. */
24480 cp_token *token = cp_lexer_peek_token (parser->lexer);
24481 error_at (token->location, "%<>>%> should be %<> >%> "
24482 "within a nested template argument list");
24484 token->type = CPP_GREATER;
24486 else
24488 /* If this is not a nested template argument list, the '>>'
24489 is a typo for '>'. Emit an error message and continue.
24490 Same deal about the token location, but here we can get it
24491 right by consuming the '>>' before issuing the diagnostic. */
24492 cp_token *token = cp_lexer_consume_token (parser->lexer);
24493 error_at (token->location,
24494 "spurious %<>>%>, use %<>%> to terminate "
24495 "a template argument list");
24498 else
24499 cp_parser_skip_to_end_of_template_parameter_list (parser);
24500 /* The `>' token might be a greater-than operator again now. */
24501 parser->greater_than_is_operator_p
24502 = saved_greater_than_is_operator_p;
24503 /* Restore the SAVED_SCOPE. */
24504 parser->scope = saved_scope;
24505 parser->qualifying_scope = saved_qualifying_scope;
24506 parser->object_scope = saved_object_scope;
24507 cp_unevaluated_operand = saved_unevaluated_operand;
24508 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24510 return arguments;
24513 /* MEMBER_FUNCTION is a member function, or a friend. If default
24514 arguments, or the body of the function have not yet been parsed,
24515 parse them now. */
24517 static void
24518 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24520 timevar_push (TV_PARSE_INMETH);
24521 /* If this member is a template, get the underlying
24522 FUNCTION_DECL. */
24523 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24524 member_function = DECL_TEMPLATE_RESULT (member_function);
24526 /* There should not be any class definitions in progress at this
24527 point; the bodies of members are only parsed outside of all class
24528 definitions. */
24529 gcc_assert (parser->num_classes_being_defined == 0);
24530 /* While we're parsing the member functions we might encounter more
24531 classes. We want to handle them right away, but we don't want
24532 them getting mixed up with functions that are currently in the
24533 queue. */
24534 push_unparsed_function_queues (parser);
24536 /* Make sure that any template parameters are in scope. */
24537 maybe_begin_member_template_processing (member_function);
24539 // Restore the declaration's requirements for the parsing of
24540 // the definition.
24542 tree saved_template_reqs = release (current_template_reqs);
24543 current_template_reqs = get_constraints (member_function);
24545 /* If the body of the function has not yet been parsed, parse it
24546 now. */
24547 if (DECL_PENDING_INLINE_P (member_function))
24549 tree function_scope;
24550 cp_token_cache *tokens;
24552 /* The function is no longer pending; we are processing it. */
24553 tokens = DECL_PENDING_INLINE_INFO (member_function);
24554 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24555 DECL_PENDING_INLINE_P (member_function) = 0;
24557 /* If this is a local class, enter the scope of the containing
24558 function. */
24559 function_scope = current_function_decl;
24560 if (function_scope)
24561 push_function_context ();
24563 /* Push the body of the function onto the lexer stack. */
24564 cp_parser_push_lexer_for_tokens (parser, tokens);
24566 /* Let the front end know that we going to be defining this
24567 function. */
24568 start_preparsed_function (member_function, NULL_TREE,
24569 SF_PRE_PARSED | SF_INCLASS_INLINE);
24571 /* Don't do access checking if it is a templated function. */
24572 if (processing_template_decl)
24573 push_deferring_access_checks (dk_no_check);
24575 /* #pragma omp declare reduction needs special parsing. */
24576 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24578 parser->lexer->in_pragma = true;
24579 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24580 finish_function (/*inline*/2);
24581 cp_check_omp_declare_reduction (member_function);
24583 else
24584 /* Now, parse the body of the function. */
24585 cp_parser_function_definition_after_declarator (parser,
24586 /*inline_p=*/true);
24588 if (processing_template_decl)
24589 pop_deferring_access_checks ();
24591 /* Leave the scope of the containing function. */
24592 if (function_scope)
24593 pop_function_context ();
24594 cp_parser_pop_lexer (parser);
24597 /* Remove any template parameters from the symbol table. */
24598 maybe_end_member_template_processing ();
24600 // Restore the template requirements.
24601 current_template_reqs = saved_template_reqs;
24603 /* Restore the queue. */
24604 pop_unparsed_function_queues (parser);
24605 timevar_pop (TV_PARSE_INMETH);
24608 /* If DECL contains any default args, remember it on the unparsed
24609 functions queue. */
24611 static void
24612 cp_parser_save_default_args (cp_parser* parser, tree decl)
24614 tree probe;
24616 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24617 probe;
24618 probe = TREE_CHAIN (probe))
24619 if (TREE_PURPOSE (probe))
24621 cp_default_arg_entry entry = {current_class_type, decl};
24622 vec_safe_push (unparsed_funs_with_default_args, entry);
24623 break;
24627 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24628 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24629 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24630 from the parameter-type-list. */
24632 static tree
24633 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24634 tree default_arg, tree parmtype)
24636 cp_token_cache *tokens;
24637 tree parsed_arg;
24638 bool dummy;
24640 if (default_arg == error_mark_node)
24641 return error_mark_node;
24643 /* Push the saved tokens for the default argument onto the parser's
24644 lexer stack. */
24645 tokens = DEFARG_TOKENS (default_arg);
24646 cp_parser_push_lexer_for_tokens (parser, tokens);
24648 start_lambda_scope (decl);
24650 /* Parse the default argument. */
24651 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24652 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24653 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24655 finish_lambda_scope ();
24657 if (parsed_arg == error_mark_node)
24658 cp_parser_skip_to_end_of_statement (parser);
24660 if (!processing_template_decl)
24662 /* In a non-template class, check conversions now. In a template,
24663 we'll wait and instantiate these as needed. */
24664 if (TREE_CODE (decl) == PARM_DECL)
24665 parsed_arg = check_default_argument (parmtype, parsed_arg,
24666 tf_warning_or_error);
24667 else
24668 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24671 /* If the token stream has not been completely used up, then
24672 there was extra junk after the end of the default
24673 argument. */
24674 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24676 if (TREE_CODE (decl) == PARM_DECL)
24677 cp_parser_error (parser, "expected %<,%>");
24678 else
24679 cp_parser_error (parser, "expected %<;%>");
24682 /* Revert to the main lexer. */
24683 cp_parser_pop_lexer (parser);
24685 return parsed_arg;
24688 /* FIELD is a non-static data member with an initializer which we saved for
24689 later; parse it now. */
24691 static void
24692 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24694 tree def;
24696 maybe_begin_member_template_processing (field);
24698 push_unparsed_function_queues (parser);
24699 def = cp_parser_late_parse_one_default_arg (parser, field,
24700 DECL_INITIAL (field),
24701 NULL_TREE);
24702 pop_unparsed_function_queues (parser);
24704 maybe_end_member_template_processing ();
24706 DECL_INITIAL (field) = def;
24709 /* FN is a FUNCTION_DECL which may contains a parameter with an
24710 unparsed DEFAULT_ARG. Parse the default args now. This function
24711 assumes that the current scope is the scope in which the default
24712 argument should be processed. */
24714 static void
24715 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24717 bool saved_local_variables_forbidden_p;
24718 tree parm, parmdecl;
24720 /* While we're parsing the default args, we might (due to the
24721 statement expression extension) encounter more classes. We want
24722 to handle them right away, but we don't want them getting mixed
24723 up with default args that are currently in the queue. */
24724 push_unparsed_function_queues (parser);
24726 /* Local variable names (and the `this' keyword) may not appear
24727 in a default argument. */
24728 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24729 parser->local_variables_forbidden_p = true;
24731 push_defarg_context (fn);
24733 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24734 parmdecl = DECL_ARGUMENTS (fn);
24735 parm && parm != void_list_node;
24736 parm = TREE_CHAIN (parm),
24737 parmdecl = DECL_CHAIN (parmdecl))
24739 tree default_arg = TREE_PURPOSE (parm);
24740 tree parsed_arg;
24741 vec<tree, va_gc> *insts;
24742 tree copy;
24743 unsigned ix;
24745 if (!default_arg)
24746 continue;
24748 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24749 /* This can happen for a friend declaration for a function
24750 already declared with default arguments. */
24751 continue;
24753 parsed_arg
24754 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24755 default_arg,
24756 TREE_VALUE (parm));
24757 if (parsed_arg == error_mark_node)
24759 continue;
24762 TREE_PURPOSE (parm) = parsed_arg;
24764 /* Update any instantiations we've already created. */
24765 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24766 vec_safe_iterate (insts, ix, &copy); ix++)
24767 TREE_PURPOSE (copy) = parsed_arg;
24770 pop_defarg_context ();
24772 /* Make sure no default arg is missing. */
24773 check_default_args (fn);
24775 /* Restore the state of local_variables_forbidden_p. */
24776 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24778 /* Restore the queue. */
24779 pop_unparsed_function_queues (parser);
24782 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24784 sizeof ... ( identifier )
24786 where the 'sizeof' token has already been consumed. */
24788 static tree
24789 cp_parser_sizeof_pack (cp_parser *parser)
24791 /* Consume the `...'. */
24792 cp_lexer_consume_token (parser->lexer);
24793 maybe_warn_variadic_templates ();
24795 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24796 if (paren)
24797 cp_lexer_consume_token (parser->lexer);
24798 else
24799 permerror (cp_lexer_peek_token (parser->lexer)->location,
24800 "%<sizeof...%> argument must be surrounded by parentheses");
24802 cp_token *token = cp_lexer_peek_token (parser->lexer);
24803 tree name = cp_parser_identifier (parser);
24804 if (name == error_mark_node)
24805 return error_mark_node;
24806 /* The name is not qualified. */
24807 parser->scope = NULL_TREE;
24808 parser->qualifying_scope = NULL_TREE;
24809 parser->object_scope = NULL_TREE;
24810 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24811 if (expr == error_mark_node)
24812 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24813 token->location);
24814 if (TREE_CODE (expr) == TYPE_DECL)
24815 expr = TREE_TYPE (expr);
24816 else if (TREE_CODE (expr) == CONST_DECL)
24817 expr = DECL_INITIAL (expr);
24818 expr = make_pack_expansion (expr);
24820 if (paren)
24821 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24823 return expr;
24826 /* Parse the operand of `sizeof' (or a similar operator). Returns
24827 either a TYPE or an expression, depending on the form of the
24828 input. The KEYWORD indicates which kind of expression we have
24829 encountered. */
24831 static tree
24832 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24834 tree expr = NULL_TREE;
24835 const char *saved_message;
24836 char *tmp;
24837 bool saved_integral_constant_expression_p;
24838 bool saved_non_integral_constant_expression_p;
24840 /* If it's a `...', then we are computing the length of a parameter
24841 pack. */
24842 if (keyword == RID_SIZEOF
24843 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24844 return cp_parser_sizeof_pack (parser);
24846 /* Types cannot be defined in a `sizeof' expression. Save away the
24847 old message. */
24848 saved_message = parser->type_definition_forbidden_message;
24849 /* And create the new one. */
24850 tmp = concat ("types may not be defined in %<",
24851 IDENTIFIER_POINTER (ridpointers[keyword]),
24852 "%> expressions", NULL);
24853 parser->type_definition_forbidden_message = tmp;
24855 /* The restrictions on constant-expressions do not apply inside
24856 sizeof expressions. */
24857 saved_integral_constant_expression_p
24858 = parser->integral_constant_expression_p;
24859 saved_non_integral_constant_expression_p
24860 = parser->non_integral_constant_expression_p;
24861 parser->integral_constant_expression_p = false;
24863 /* Do not actually evaluate the expression. */
24864 ++cp_unevaluated_operand;
24865 ++c_inhibit_evaluation_warnings;
24866 /* If it's a `(', then we might be looking at the type-id
24867 construction. */
24868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24870 tree type = NULL_TREE;
24872 /* We can't be sure yet whether we're looking at a type-id or an
24873 expression. */
24874 cp_parser_parse_tentatively (parser);
24875 /* Note: as a GNU Extension, compound literals are considered
24876 postfix-expressions as they are in C99, so they are valid
24877 arguments to sizeof. See comment in cp_parser_cast_expression
24878 for details. */
24879 if (cp_parser_compound_literal_p (parser))
24880 cp_parser_simulate_error (parser);
24881 else
24883 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24884 parser->in_type_id_in_expr_p = true;
24885 /* Look for the type-id. */
24886 type = cp_parser_type_id (parser);
24887 /* Look for the closing `)'. */
24888 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24889 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24892 /* If all went well, then we're done. */
24893 if (cp_parser_parse_definitely (parser))
24895 cp_decl_specifier_seq decl_specs;
24897 /* Build a trivial decl-specifier-seq. */
24898 clear_decl_specs (&decl_specs);
24899 decl_specs.type = type;
24901 /* Call grokdeclarator to figure out what type this is. */
24902 expr = grokdeclarator (NULL,
24903 &decl_specs,
24904 TYPENAME,
24905 /*initialized=*/0,
24906 /*attrlist=*/NULL);
24910 /* If the type-id production did not work out, then we must be
24911 looking at the unary-expression production. */
24912 if (!expr)
24913 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24914 /*cast_p=*/false, NULL);
24916 /* Go back to evaluating expressions. */
24917 --cp_unevaluated_operand;
24918 --c_inhibit_evaluation_warnings;
24920 /* Free the message we created. */
24921 free (tmp);
24922 /* And restore the old one. */
24923 parser->type_definition_forbidden_message = saved_message;
24924 parser->integral_constant_expression_p
24925 = saved_integral_constant_expression_p;
24926 parser->non_integral_constant_expression_p
24927 = saved_non_integral_constant_expression_p;
24929 return expr;
24932 /* If the current declaration has no declarator, return true. */
24934 static bool
24935 cp_parser_declares_only_class_p (cp_parser *parser)
24937 /* If the next token is a `;' or a `,' then there is no
24938 declarator. */
24939 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24940 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24943 /* Update the DECL_SPECS to reflect the storage class indicated by
24944 KEYWORD. */
24946 static void
24947 cp_parser_set_storage_class (cp_parser *parser,
24948 cp_decl_specifier_seq *decl_specs,
24949 enum rid keyword,
24950 cp_token *token)
24952 cp_storage_class storage_class;
24954 if (parser->in_unbraced_linkage_specification_p)
24956 error_at (token->location, "invalid use of %qD in linkage specification",
24957 ridpointers[keyword]);
24958 return;
24960 else if (decl_specs->storage_class != sc_none)
24962 decl_specs->conflicting_specifiers_p = true;
24963 return;
24966 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24967 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24968 && decl_specs->gnu_thread_keyword_p)
24970 pedwarn (decl_specs->locations[ds_thread], 0,
24971 "%<__thread%> before %qD", ridpointers[keyword]);
24974 switch (keyword)
24976 case RID_AUTO:
24977 storage_class = sc_auto;
24978 break;
24979 case RID_REGISTER:
24980 storage_class = sc_register;
24981 break;
24982 case RID_STATIC:
24983 storage_class = sc_static;
24984 break;
24985 case RID_EXTERN:
24986 storage_class = sc_extern;
24987 break;
24988 case RID_MUTABLE:
24989 storage_class = sc_mutable;
24990 break;
24991 default:
24992 gcc_unreachable ();
24994 decl_specs->storage_class = storage_class;
24995 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24997 /* A storage class specifier cannot be applied alongside a typedef
24998 specifier. If there is a typedef specifier present then set
24999 conflicting_specifiers_p which will trigger an error later
25000 on in grokdeclarator. */
25001 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
25002 decl_specs->conflicting_specifiers_p = true;
25005 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
25006 is true, the type is a class or enum definition. */
25008 static void
25009 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
25010 tree type_spec,
25011 cp_token *token,
25012 bool type_definition_p)
25014 decl_specs->any_specifiers_p = true;
25016 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
25017 (with, for example, in "typedef int wchar_t;") we remember that
25018 this is what happened. In system headers, we ignore these
25019 declarations so that G++ can work with system headers that are not
25020 C++-safe. */
25021 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
25022 && !type_definition_p
25023 && (type_spec == boolean_type_node
25024 || type_spec == char16_type_node
25025 || type_spec == char32_type_node
25026 || type_spec == wchar_type_node)
25027 && (decl_specs->type
25028 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
25029 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
25030 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
25031 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
25033 decl_specs->redefined_builtin_type = type_spec;
25034 set_and_check_decl_spec_loc (decl_specs,
25035 ds_redefined_builtin_type_spec,
25036 token);
25037 if (!decl_specs->type)
25039 decl_specs->type = type_spec;
25040 decl_specs->type_definition_p = false;
25041 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
25044 else if (decl_specs->type)
25045 decl_specs->multiple_types_p = true;
25046 else
25048 decl_specs->type = type_spec;
25049 decl_specs->type_definition_p = type_definition_p;
25050 decl_specs->redefined_builtin_type = NULL_TREE;
25051 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
25055 /* True iff TOKEN is the GNU keyword __thread. */
25057 static bool
25058 token_is__thread (cp_token *token)
25060 gcc_assert (token->keyword == RID_THREAD);
25061 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
25064 /* Set the location for a declarator specifier and check if it is
25065 duplicated.
25067 DECL_SPECS is the sequence of declarator specifiers onto which to
25068 set the location.
25070 DS is the single declarator specifier to set which location is to
25071 be set onto the existing sequence of declarators.
25073 LOCATION is the location for the declarator specifier to
25074 consider. */
25076 static void
25077 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
25078 cp_decl_spec ds, cp_token *token)
25080 gcc_assert (ds < ds_last);
25082 if (decl_specs == NULL)
25083 return;
25085 source_location location = token->location;
25087 if (decl_specs->locations[ds] == 0)
25089 decl_specs->locations[ds] = location;
25090 if (ds == ds_thread)
25091 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
25093 else
25095 if (ds == ds_long)
25097 if (decl_specs->locations[ds_long_long] != 0)
25098 error_at (location,
25099 "%<long long long%> is too long for GCC");
25100 else
25102 decl_specs->locations[ds_long_long] = location;
25103 pedwarn_cxx98 (location,
25104 OPT_Wlong_long,
25105 "ISO C++ 1998 does not support %<long long%>");
25108 else if (ds == ds_thread)
25110 bool gnu = token_is__thread (token);
25111 if (gnu != decl_specs->gnu_thread_keyword_p)
25112 error_at (location,
25113 "both %<__thread%> and %<thread_local%> specified");
25114 else
25115 error_at (location, "duplicate %qD", token->u.value);
25117 else
25119 static const char *const decl_spec_names[] = {
25120 "signed",
25121 "unsigned",
25122 "short",
25123 "long",
25124 "const",
25125 "volatile",
25126 "restrict",
25127 "inline",
25128 "virtual",
25129 "explicit",
25130 "friend",
25131 "typedef",
25132 "using",
25133 "constexpr",
25134 "__complex"
25136 error_at (location,
25137 "duplicate %qs", decl_spec_names[ds]);
25142 /* Return true iff the declarator specifier DS is present in the
25143 sequence of declarator specifiers DECL_SPECS. */
25145 bool
25146 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
25147 cp_decl_spec ds)
25149 gcc_assert (ds < ds_last);
25151 if (decl_specs == NULL)
25152 return false;
25154 return decl_specs->locations[ds] != 0;
25157 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
25158 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
25160 static bool
25161 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
25163 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
25166 /* Issue an error message indicating that TOKEN_DESC was expected.
25167 If KEYWORD is true, it indicated this function is called by
25168 cp_parser_require_keword and the required token can only be
25169 a indicated keyword. */
25171 static void
25172 cp_parser_required_error (cp_parser *parser,
25173 required_token token_desc,
25174 bool keyword)
25176 switch (token_desc)
25178 case RT_NEW:
25179 cp_parser_error (parser, "expected %<new%>");
25180 return;
25181 case RT_DELETE:
25182 cp_parser_error (parser, "expected %<delete%>");
25183 return;
25184 case RT_RETURN:
25185 cp_parser_error (parser, "expected %<return%>");
25186 return;
25187 case RT_WHILE:
25188 cp_parser_error (parser, "expected %<while%>");
25189 return;
25190 case RT_EXTERN:
25191 cp_parser_error (parser, "expected %<extern%>");
25192 return;
25193 case RT_STATIC_ASSERT:
25194 cp_parser_error (parser, "expected %<static_assert%>");
25195 return;
25196 case RT_DECLTYPE:
25197 cp_parser_error (parser, "expected %<decltype%>");
25198 return;
25199 case RT_OPERATOR:
25200 cp_parser_error (parser, "expected %<operator%>");
25201 return;
25202 case RT_CLASS:
25203 cp_parser_error (parser, "expected %<class%>");
25204 return;
25205 case RT_TEMPLATE:
25206 cp_parser_error (parser, "expected %<template%>");
25207 return;
25208 case RT_NAMESPACE:
25209 cp_parser_error (parser, "expected %<namespace%>");
25210 return;
25211 case RT_USING:
25212 cp_parser_error (parser, "expected %<using%>");
25213 return;
25214 case RT_ASM:
25215 cp_parser_error (parser, "expected %<asm%>");
25216 return;
25217 case RT_TRY:
25218 cp_parser_error (parser, "expected %<try%>");
25219 return;
25220 case RT_CATCH:
25221 cp_parser_error (parser, "expected %<catch%>");
25222 return;
25223 case RT_THROW:
25224 cp_parser_error (parser, "expected %<throw%>");
25225 return;
25226 case RT_LABEL:
25227 cp_parser_error (parser, "expected %<__label__%>");
25228 return;
25229 case RT_AT_TRY:
25230 cp_parser_error (parser, "expected %<@try%>");
25231 return;
25232 case RT_AT_SYNCHRONIZED:
25233 cp_parser_error (parser, "expected %<@synchronized%>");
25234 return;
25235 case RT_AT_THROW:
25236 cp_parser_error (parser, "expected %<@throw%>");
25237 return;
25238 case RT_TRANSACTION_ATOMIC:
25239 cp_parser_error (parser, "expected %<__transaction_atomic%>");
25240 return;
25241 case RT_TRANSACTION_RELAXED:
25242 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
25243 return;
25244 default:
25245 break;
25247 if (!keyword)
25249 switch (token_desc)
25251 case RT_SEMICOLON:
25252 cp_parser_error (parser, "expected %<;%>");
25253 return;
25254 case RT_OPEN_PAREN:
25255 cp_parser_error (parser, "expected %<(%>");
25256 return;
25257 case RT_CLOSE_BRACE:
25258 cp_parser_error (parser, "expected %<}%>");
25259 return;
25260 case RT_OPEN_BRACE:
25261 cp_parser_error (parser, "expected %<{%>");
25262 return;
25263 case RT_CLOSE_SQUARE:
25264 cp_parser_error (parser, "expected %<]%>");
25265 return;
25266 case RT_OPEN_SQUARE:
25267 cp_parser_error (parser, "expected %<[%>");
25268 return;
25269 case RT_COMMA:
25270 cp_parser_error (parser, "expected %<,%>");
25271 return;
25272 case RT_SCOPE:
25273 cp_parser_error (parser, "expected %<::%>");
25274 return;
25275 case RT_LESS:
25276 cp_parser_error (parser, "expected %<<%>");
25277 return;
25278 case RT_GREATER:
25279 cp_parser_error (parser, "expected %<>%>");
25280 return;
25281 case RT_EQ:
25282 cp_parser_error (parser, "expected %<=%>");
25283 return;
25284 case RT_ELLIPSIS:
25285 cp_parser_error (parser, "expected %<...%>");
25286 return;
25287 case RT_MULT:
25288 cp_parser_error (parser, "expected %<*%>");
25289 return;
25290 case RT_COMPL:
25291 cp_parser_error (parser, "expected %<~%>");
25292 return;
25293 case RT_COLON:
25294 cp_parser_error (parser, "expected %<:%>");
25295 return;
25296 case RT_COLON_SCOPE:
25297 cp_parser_error (parser, "expected %<:%> or %<::%>");
25298 return;
25299 case RT_CLOSE_PAREN:
25300 cp_parser_error (parser, "expected %<)%>");
25301 return;
25302 case RT_COMMA_CLOSE_PAREN:
25303 cp_parser_error (parser, "expected %<,%> or %<)%>");
25304 return;
25305 case RT_PRAGMA_EOL:
25306 cp_parser_error (parser, "expected end of line");
25307 return;
25308 case RT_NAME:
25309 cp_parser_error (parser, "expected identifier");
25310 return;
25311 case RT_SELECT:
25312 cp_parser_error (parser, "expected selection-statement");
25313 return;
25314 case RT_INTERATION:
25315 cp_parser_error (parser, "expected iteration-statement");
25316 return;
25317 case RT_JUMP:
25318 cp_parser_error (parser, "expected jump-statement");
25319 return;
25320 case RT_CLASS_KEY:
25321 cp_parser_error (parser, "expected class-key");
25322 return;
25323 case RT_CLASS_TYPENAME_TEMPLATE:
25324 cp_parser_error (parser,
25325 "expected %<class%>, %<typename%>, or %<template%>");
25326 return;
25327 default:
25328 gcc_unreachable ();
25331 else
25332 gcc_unreachable ();
25337 /* If the next token is of the indicated TYPE, consume it. Otherwise,
25338 issue an error message indicating that TOKEN_DESC was expected.
25340 Returns the token consumed, if the token had the appropriate type.
25341 Otherwise, returns NULL. */
25343 static cp_token *
25344 cp_parser_require (cp_parser* parser,
25345 enum cpp_ttype type,
25346 required_token token_desc)
25348 if (cp_lexer_next_token_is (parser->lexer, type))
25349 return cp_lexer_consume_token (parser->lexer);
25350 else
25352 /* Output the MESSAGE -- unless we're parsing tentatively. */
25353 if (!cp_parser_simulate_error (parser))
25354 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25355 return NULL;
25359 /* An error message is produced if the next token is not '>'.
25360 All further tokens are skipped until the desired token is
25361 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25363 static void
25364 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25366 /* Current level of '< ... >'. */
25367 unsigned level = 0;
25368 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25369 unsigned nesting_depth = 0;
25371 /* Are we ready, yet? If not, issue error message. */
25372 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25373 return;
25375 /* Skip tokens until the desired token is found. */
25376 while (true)
25378 /* Peek at the next token. */
25379 switch (cp_lexer_peek_token (parser->lexer)->type)
25381 case CPP_LESS:
25382 if (!nesting_depth)
25383 ++level;
25384 break;
25386 case CPP_RSHIFT:
25387 if (cxx_dialect == cxx98)
25388 /* C++0x views the `>>' operator as two `>' tokens, but
25389 C++98 does not. */
25390 break;
25391 else if (!nesting_depth && level-- == 0)
25393 /* We've hit a `>>' where the first `>' closes the
25394 template argument list, and the second `>' is
25395 spurious. Just consume the `>>' and stop; we've
25396 already produced at least one error. */
25397 cp_lexer_consume_token (parser->lexer);
25398 return;
25400 /* Fall through for C++0x, so we handle the second `>' in
25401 the `>>'. */
25403 case CPP_GREATER:
25404 if (!nesting_depth && level-- == 0)
25406 /* We've reached the token we want, consume it and stop. */
25407 cp_lexer_consume_token (parser->lexer);
25408 return;
25410 break;
25412 case CPP_OPEN_PAREN:
25413 case CPP_OPEN_SQUARE:
25414 ++nesting_depth;
25415 break;
25417 case CPP_CLOSE_PAREN:
25418 case CPP_CLOSE_SQUARE:
25419 if (nesting_depth-- == 0)
25420 return;
25421 break;
25423 case CPP_EOF:
25424 case CPP_PRAGMA_EOL:
25425 case CPP_SEMICOLON:
25426 case CPP_OPEN_BRACE:
25427 case CPP_CLOSE_BRACE:
25428 /* The '>' was probably forgotten, don't look further. */
25429 return;
25431 default:
25432 break;
25435 /* Consume this token. */
25436 cp_lexer_consume_token (parser->lexer);
25440 /* If the next token is the indicated keyword, consume it. Otherwise,
25441 issue an error message indicating that TOKEN_DESC was expected.
25443 Returns the token consumed, if the token had the appropriate type.
25444 Otherwise, returns NULL. */
25446 static cp_token *
25447 cp_parser_require_keyword (cp_parser* parser,
25448 enum rid keyword,
25449 required_token token_desc)
25451 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25453 if (token && token->keyword != keyword)
25455 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
25456 return NULL;
25459 return token;
25462 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25463 function-definition. */
25465 static bool
25466 cp_parser_token_starts_function_definition_p (cp_token* token)
25468 return (/* An ordinary function-body begins with an `{'. */
25469 token->type == CPP_OPEN_BRACE
25470 /* A ctor-initializer begins with a `:'. */
25471 || token->type == CPP_COLON
25472 /* A function-try-block begins with `try'. */
25473 || token->keyword == RID_TRY
25474 /* A function-transaction-block begins with `__transaction_atomic'
25475 or `__transaction_relaxed'. */
25476 || token->keyword == RID_TRANSACTION_ATOMIC
25477 || token->keyword == RID_TRANSACTION_RELAXED
25478 /* The named return value extension begins with `return'. */
25479 || token->keyword == RID_RETURN);
25482 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25483 definition. */
25485 static bool
25486 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25488 cp_token *token;
25490 token = cp_lexer_peek_token (parser->lexer);
25491 return (token->type == CPP_OPEN_BRACE
25492 || (token->type == CPP_COLON
25493 && !parser->colon_doesnt_start_class_def_p));
25496 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25497 C++0x) ending a template-argument. */
25499 static bool
25500 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25502 cp_token *token;
25504 token = cp_lexer_peek_token (parser->lexer);
25505 return (token->type == CPP_COMMA
25506 || token->type == CPP_GREATER
25507 || token->type == CPP_ELLIPSIS
25508 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25511 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25512 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25514 static bool
25515 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25516 size_t n)
25518 cp_token *token;
25520 token = cp_lexer_peek_nth_token (parser->lexer, n);
25521 if (token->type == CPP_LESS)
25522 return true;
25523 /* Check for the sequence `<::' in the original code. It would be lexed as
25524 `[:', where `[' is a digraph, and there is no whitespace before
25525 `:'. */
25526 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25528 cp_token *token2;
25529 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25530 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25531 return true;
25533 return false;
25536 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25537 or none_type otherwise. */
25539 static enum tag_types
25540 cp_parser_token_is_class_key (cp_token* token)
25542 switch (token->keyword)
25544 case RID_CLASS:
25545 return class_type;
25546 case RID_STRUCT:
25547 return record_type;
25548 case RID_UNION:
25549 return union_type;
25551 default:
25552 return none_type;
25556 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25558 static void
25559 cp_parser_check_class_key (enum tag_types class_key, tree type)
25561 if (type == error_mark_node)
25562 return;
25563 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25565 if (permerror (input_location, "%qs tag used in naming %q#T",
25566 class_key == union_type ? "union"
25567 : class_key == record_type ? "struct" : "class",
25568 type))
25569 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25570 "%q#T was previously declared here", type);
25574 /* Issue an error message if DECL is redeclared with different
25575 access than its original declaration [class.access.spec/3].
25576 This applies to nested classes and nested class templates.
25577 [class.mem/1]. */
25579 static void
25580 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25582 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25583 return;
25585 if ((TREE_PRIVATE (decl)
25586 != (current_access_specifier == access_private_node))
25587 || (TREE_PROTECTED (decl)
25588 != (current_access_specifier == access_protected_node)))
25589 error_at (location, "%qD redeclared with different access", decl);
25592 /* Look for the `template' keyword, as a syntactic disambiguator.
25593 Return TRUE iff it is present, in which case it will be
25594 consumed. */
25596 static bool
25597 cp_parser_optional_template_keyword (cp_parser *parser)
25599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25601 /* In C++98 the `template' keyword can only be used within templates;
25602 outside templates the parser can always figure out what is a
25603 template and what is not. In C++11, per the resolution of DR 468,
25604 `template' is allowed in cases where it is not strictly necessary. */
25605 if (!processing_template_decl
25606 && pedantic && cxx_dialect == cxx98)
25608 cp_token *token = cp_lexer_peek_token (parser->lexer);
25609 pedwarn (token->location, OPT_Wpedantic,
25610 "in C++98 %<template%> (as a disambiguator) is only "
25611 "allowed within templates");
25612 /* If this part of the token stream is rescanned, the same
25613 error message would be generated. So, we purge the token
25614 from the stream. */
25615 cp_lexer_purge_token (parser->lexer);
25616 return false;
25618 else
25620 /* Consume the `template' keyword. */
25621 cp_lexer_consume_token (parser->lexer);
25622 return true;
25625 return false;
25628 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25629 set PARSER->SCOPE, and perform other related actions. */
25631 static void
25632 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25634 int i;
25635 struct tree_check *check_value;
25636 deferred_access_check *chk;
25637 vec<deferred_access_check, va_gc> *checks;
25639 /* Get the stored value. */
25640 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25641 /* Perform any access checks that were deferred. */
25642 checks = check_value->checks;
25643 if (checks)
25645 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25646 perform_or_defer_access_check (chk->binfo,
25647 chk->decl,
25648 chk->diag_decl, tf_warning_or_error);
25650 /* Set the scope from the stored value. */
25651 parser->scope = check_value->value;
25652 parser->qualifying_scope = check_value->qualifying_scope;
25653 parser->object_scope = NULL_TREE;
25656 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25657 encounter the end of a block before what we were looking for. */
25659 static bool
25660 cp_parser_cache_group (cp_parser *parser,
25661 enum cpp_ttype end,
25662 unsigned depth)
25664 while (true)
25666 cp_token *token = cp_lexer_peek_token (parser->lexer);
25668 /* Abort a parenthesized expression if we encounter a semicolon. */
25669 if ((end == CPP_CLOSE_PAREN || depth == 0)
25670 && token->type == CPP_SEMICOLON)
25671 return true;
25672 /* If we've reached the end of the file, stop. */
25673 if (token->type == CPP_EOF
25674 || (end != CPP_PRAGMA_EOL
25675 && token->type == CPP_PRAGMA_EOL))
25676 return true;
25677 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25678 /* We've hit the end of an enclosing block, so there's been some
25679 kind of syntax error. */
25680 return true;
25682 /* Consume the token. */
25683 cp_lexer_consume_token (parser->lexer);
25684 /* See if it starts a new group. */
25685 if (token->type == CPP_OPEN_BRACE)
25687 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25688 /* In theory this should probably check end == '}', but
25689 cp_parser_save_member_function_body needs it to exit
25690 after either '}' or ')' when called with ')'. */
25691 if (depth == 0)
25692 return false;
25694 else if (token->type == CPP_OPEN_PAREN)
25696 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25697 if (depth == 0 && end == CPP_CLOSE_PAREN)
25698 return false;
25700 else if (token->type == CPP_PRAGMA)
25701 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25702 else if (token->type == end)
25703 return false;
25707 /* Like above, for caching a default argument or NSDMI. Both of these are
25708 terminated by a non-nested comma, but it can be unclear whether or not a
25709 comma is nested in a template argument list unless we do more parsing.
25710 In order to handle this ambiguity, when we encounter a ',' after a '<'
25711 we try to parse what follows as a parameter-declaration-list (in the
25712 case of a default argument) or a member-declarator (in the case of an
25713 NSDMI). If that succeeds, then we stop caching. */
25715 static tree
25716 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25718 unsigned depth = 0;
25719 int maybe_template_id = 0;
25720 cp_token *first_token;
25721 cp_token *token;
25722 tree default_argument;
25724 /* Add tokens until we have processed the entire default
25725 argument. We add the range [first_token, token). */
25726 first_token = cp_lexer_peek_token (parser->lexer);
25727 if (first_token->type == CPP_OPEN_BRACE)
25729 /* For list-initialization, this is straightforward. */
25730 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25731 token = cp_lexer_peek_token (parser->lexer);
25733 else while (true)
25735 bool done = false;
25737 /* Peek at the next token. */
25738 token = cp_lexer_peek_token (parser->lexer);
25739 /* What we do depends on what token we have. */
25740 switch (token->type)
25742 /* In valid code, a default argument must be
25743 immediately followed by a `,' `)', or `...'. */
25744 case CPP_COMMA:
25745 if (depth == 0 && maybe_template_id)
25747 /* If we've seen a '<', we might be in a
25748 template-argument-list. Until Core issue 325 is
25749 resolved, we don't know how this situation ought
25750 to be handled, so try to DTRT. We check whether
25751 what comes after the comma is a valid parameter
25752 declaration list. If it is, then the comma ends
25753 the default argument; otherwise the default
25754 argument continues. */
25755 bool error = false;
25757 /* Set ITALP so cp_parser_parameter_declaration_list
25758 doesn't decide to commit to this parse. */
25759 bool saved_italp = parser->in_template_argument_list_p;
25760 parser->in_template_argument_list_p = true;
25762 cp_parser_parse_tentatively (parser);
25763 cp_lexer_consume_token (parser->lexer);
25765 if (nsdmi)
25767 int ctor_dtor_or_conv_p;
25768 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25769 &ctor_dtor_or_conv_p,
25770 /*parenthesized_p=*/NULL,
25771 /*member_p=*/true,
25772 /*friend_p=*/false);
25774 else
25776 begin_scope (sk_function_parms, NULL_TREE);
25777 cp_parser_parameter_declaration_list (parser, &error);
25778 pop_bindings_and_leave_scope ();
25780 if (!cp_parser_error_occurred (parser) && !error)
25781 done = true;
25782 cp_parser_abort_tentative_parse (parser);
25784 parser->in_template_argument_list_p = saved_italp;
25785 break;
25787 case CPP_CLOSE_PAREN:
25788 case CPP_ELLIPSIS:
25789 /* If we run into a non-nested `;', `}', or `]',
25790 then the code is invalid -- but the default
25791 argument is certainly over. */
25792 case CPP_SEMICOLON:
25793 case CPP_CLOSE_BRACE:
25794 case CPP_CLOSE_SQUARE:
25795 if (depth == 0
25796 /* Handle correctly int n = sizeof ... ( p ); */
25797 && token->type != CPP_ELLIPSIS)
25798 done = true;
25799 /* Update DEPTH, if necessary. */
25800 else if (token->type == CPP_CLOSE_PAREN
25801 || token->type == CPP_CLOSE_BRACE
25802 || token->type == CPP_CLOSE_SQUARE)
25803 --depth;
25804 break;
25806 case CPP_OPEN_PAREN:
25807 case CPP_OPEN_SQUARE:
25808 case CPP_OPEN_BRACE:
25809 ++depth;
25810 break;
25812 case CPP_LESS:
25813 if (depth == 0)
25814 /* This might be the comparison operator, or it might
25815 start a template argument list. */
25816 ++maybe_template_id;
25817 break;
25819 case CPP_RSHIFT:
25820 if (cxx_dialect == cxx98)
25821 break;
25822 /* Fall through for C++0x, which treats the `>>'
25823 operator like two `>' tokens in certain
25824 cases. */
25826 case CPP_GREATER:
25827 if (depth == 0)
25829 /* This might be an operator, or it might close a
25830 template argument list. But if a previous '<'
25831 started a template argument list, this will have
25832 closed it, so we can't be in one anymore. */
25833 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25834 if (maybe_template_id < 0)
25835 maybe_template_id = 0;
25837 break;
25839 /* If we run out of tokens, issue an error message. */
25840 case CPP_EOF:
25841 case CPP_PRAGMA_EOL:
25842 error_at (token->location, "file ends in default argument");
25843 done = true;
25844 break;
25846 case CPP_NAME:
25847 case CPP_SCOPE:
25848 /* In these cases, we should look for template-ids.
25849 For example, if the default argument is
25850 `X<int, double>()', we need to do name lookup to
25851 figure out whether or not `X' is a template; if
25852 so, the `,' does not end the default argument.
25854 That is not yet done. */
25855 break;
25857 default:
25858 break;
25861 /* If we've reached the end, stop. */
25862 if (done)
25863 break;
25865 /* Add the token to the token block. */
25866 token = cp_lexer_consume_token (parser->lexer);
25869 /* Create a DEFAULT_ARG to represent the unparsed default
25870 argument. */
25871 default_argument = make_node (DEFAULT_ARG);
25872 DEFARG_TOKENS (default_argument)
25873 = cp_token_cache_new (first_token, token);
25874 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25876 return default_argument;
25879 /* Begin parsing tentatively. We always save tokens while parsing
25880 tentatively so that if the tentative parsing fails we can restore the
25881 tokens. */
25883 static void
25884 cp_parser_parse_tentatively (cp_parser* parser)
25886 /* Enter a new parsing context. */
25887 parser->context = cp_parser_context_new (parser->context);
25888 /* Begin saving tokens. */
25889 cp_lexer_save_tokens (parser->lexer);
25890 /* In order to avoid repetitive access control error messages,
25891 access checks are queued up until we are no longer parsing
25892 tentatively. */
25893 push_deferring_access_checks (dk_deferred);
25896 /* Commit to the currently active tentative parse. */
25898 static void
25899 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25901 cp_parser_context *context;
25902 cp_lexer *lexer;
25904 /* Mark all of the levels as committed. */
25905 lexer = parser->lexer;
25906 for (context = parser->context; context->next; context = context->next)
25908 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25909 break;
25910 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25911 while (!cp_lexer_saving_tokens (lexer))
25912 lexer = lexer->next;
25913 cp_lexer_commit_tokens (lexer);
25917 /* Commit to the topmost currently active tentative parse.
25919 Note that this function shouldn't be called when there are
25920 irreversible side-effects while in a tentative state. For
25921 example, we shouldn't create a permanent entry in the symbol
25922 table, or issue an error message that might not apply if the
25923 tentative parse is aborted. */
25925 static void
25926 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25928 cp_parser_context *context = parser->context;
25929 cp_lexer *lexer = parser->lexer;
25931 if (context)
25933 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25934 return;
25935 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25937 while (!cp_lexer_saving_tokens (lexer))
25938 lexer = lexer->next;
25939 cp_lexer_commit_tokens (lexer);
25943 /* Abort the currently active tentative parse. All consumed tokens
25944 will be rolled back, and no diagnostics will be issued. */
25946 static void
25947 cp_parser_abort_tentative_parse (cp_parser* parser)
25949 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25950 || errorcount > 0);
25951 cp_parser_simulate_error (parser);
25952 /* Now, pretend that we want to see if the construct was
25953 successfully parsed. */
25954 cp_parser_parse_definitely (parser);
25957 /* Stop parsing tentatively. If a parse error has occurred, restore the
25958 token stream. Otherwise, commit to the tokens we have consumed.
25959 Returns true if no error occurred; false otherwise. */
25961 static bool
25962 cp_parser_parse_definitely (cp_parser* parser)
25964 bool error_occurred;
25965 cp_parser_context *context;
25967 /* Remember whether or not an error occurred, since we are about to
25968 destroy that information. */
25969 error_occurred = cp_parser_error_occurred (parser);
25970 /* Remove the topmost context from the stack. */
25971 context = parser->context;
25972 parser->context = context->next;
25973 /* If no parse errors occurred, commit to the tentative parse. */
25974 if (!error_occurred)
25976 /* Commit to the tokens read tentatively, unless that was
25977 already done. */
25978 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25979 cp_lexer_commit_tokens (parser->lexer);
25981 pop_to_parent_deferring_access_checks ();
25983 /* Otherwise, if errors occurred, roll back our state so that things
25984 are just as they were before we began the tentative parse. */
25985 else
25987 cp_lexer_rollback_tokens (parser->lexer);
25988 pop_deferring_access_checks ();
25990 /* Add the context to the front of the free list. */
25991 context->next = cp_parser_context_free_list;
25992 cp_parser_context_free_list = context;
25994 return !error_occurred;
25997 /* Returns true if we are parsing tentatively and are not committed to
25998 this tentative parse. */
26000 static bool
26001 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
26003 return (cp_parser_parsing_tentatively (parser)
26004 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
26007 /* Returns nonzero iff an error has occurred during the most recent
26008 tentative parse. */
26010 static bool
26011 cp_parser_error_occurred (cp_parser* parser)
26013 return (cp_parser_parsing_tentatively (parser)
26014 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
26017 /* Returns nonzero if GNU extensions are allowed. */
26019 static bool
26020 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
26022 return parser->allow_gnu_extensions_p;
26025 /* Objective-C++ Productions */
26028 /* Parse an Objective-C expression, which feeds into a primary-expression
26029 above.
26031 objc-expression:
26032 objc-message-expression
26033 objc-string-literal
26034 objc-encode-expression
26035 objc-protocol-expression
26036 objc-selector-expression
26038 Returns a tree representation of the expression. */
26040 static tree
26041 cp_parser_objc_expression (cp_parser* parser)
26043 /* Try to figure out what kind of declaration is present. */
26044 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26046 switch (kwd->type)
26048 case CPP_OPEN_SQUARE:
26049 return cp_parser_objc_message_expression (parser);
26051 case CPP_OBJC_STRING:
26052 kwd = cp_lexer_consume_token (parser->lexer);
26053 return objc_build_string_object (kwd->u.value);
26055 case CPP_KEYWORD:
26056 switch (kwd->keyword)
26058 case RID_AT_ENCODE:
26059 return cp_parser_objc_encode_expression (parser);
26061 case RID_AT_PROTOCOL:
26062 return cp_parser_objc_protocol_expression (parser);
26064 case RID_AT_SELECTOR:
26065 return cp_parser_objc_selector_expression (parser);
26067 default:
26068 break;
26070 default:
26071 error_at (kwd->location,
26072 "misplaced %<@%D%> Objective-C++ construct",
26073 kwd->u.value);
26074 cp_parser_skip_to_end_of_block_or_statement (parser);
26077 return error_mark_node;
26080 /* Parse an Objective-C message expression.
26082 objc-message-expression:
26083 [ objc-message-receiver objc-message-args ]
26085 Returns a representation of an Objective-C message. */
26087 static tree
26088 cp_parser_objc_message_expression (cp_parser* parser)
26090 tree receiver, messageargs;
26092 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
26093 receiver = cp_parser_objc_message_receiver (parser);
26094 messageargs = cp_parser_objc_message_args (parser);
26095 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26097 return objc_build_message_expr (receiver, messageargs);
26100 /* Parse an objc-message-receiver.
26102 objc-message-receiver:
26103 expression
26104 simple-type-specifier
26106 Returns a representation of the type or expression. */
26108 static tree
26109 cp_parser_objc_message_receiver (cp_parser* parser)
26111 tree rcv;
26113 /* An Objective-C message receiver may be either (1) a type
26114 or (2) an expression. */
26115 cp_parser_parse_tentatively (parser);
26116 rcv = cp_parser_expression (parser, false, NULL);
26118 if (cp_parser_parse_definitely (parser))
26119 return rcv;
26121 rcv = cp_parser_simple_type_specifier (parser,
26122 /*decl_specs=*/NULL,
26123 CP_PARSER_FLAGS_NONE);
26125 return objc_get_class_reference (rcv);
26128 /* Parse the arguments and selectors comprising an Objective-C message.
26130 objc-message-args:
26131 objc-selector
26132 objc-selector-args
26133 objc-selector-args , objc-comma-args
26135 objc-selector-args:
26136 objc-selector [opt] : assignment-expression
26137 objc-selector-args objc-selector [opt] : assignment-expression
26139 objc-comma-args:
26140 assignment-expression
26141 objc-comma-args , assignment-expression
26143 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
26144 selector arguments and TREE_VALUE containing a list of comma
26145 arguments. */
26147 static tree
26148 cp_parser_objc_message_args (cp_parser* parser)
26150 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
26151 bool maybe_unary_selector_p = true;
26152 cp_token *token = cp_lexer_peek_token (parser->lexer);
26154 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26156 tree selector = NULL_TREE, arg;
26158 if (token->type != CPP_COLON)
26159 selector = cp_parser_objc_selector (parser);
26161 /* Detect if we have a unary selector. */
26162 if (maybe_unary_selector_p
26163 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26164 return build_tree_list (selector, NULL_TREE);
26166 maybe_unary_selector_p = false;
26167 cp_parser_require (parser, CPP_COLON, RT_COLON);
26168 arg = cp_parser_assignment_expression (parser, false, NULL);
26170 sel_args
26171 = chainon (sel_args,
26172 build_tree_list (selector, arg));
26174 token = cp_lexer_peek_token (parser->lexer);
26177 /* Handle non-selector arguments, if any. */
26178 while (token->type == CPP_COMMA)
26180 tree arg;
26182 cp_lexer_consume_token (parser->lexer);
26183 arg = cp_parser_assignment_expression (parser, false, NULL);
26185 addl_args
26186 = chainon (addl_args,
26187 build_tree_list (NULL_TREE, arg));
26189 token = cp_lexer_peek_token (parser->lexer);
26192 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
26194 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
26195 return build_tree_list (error_mark_node, error_mark_node);
26198 return build_tree_list (sel_args, addl_args);
26201 /* Parse an Objective-C encode expression.
26203 objc-encode-expression:
26204 @encode objc-typename
26206 Returns an encoded representation of the type argument. */
26208 static tree
26209 cp_parser_objc_encode_expression (cp_parser* parser)
26211 tree type;
26212 cp_token *token;
26214 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
26215 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26216 token = cp_lexer_peek_token (parser->lexer);
26217 type = complete_type (cp_parser_type_id (parser));
26218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26220 if (!type)
26222 error_at (token->location,
26223 "%<@encode%> must specify a type as an argument");
26224 return error_mark_node;
26227 /* This happens if we find @encode(T) (where T is a template
26228 typename or something dependent on a template typename) when
26229 parsing a template. In that case, we can't compile it
26230 immediately, but we rather create an AT_ENCODE_EXPR which will
26231 need to be instantiated when the template is used.
26233 if (dependent_type_p (type))
26235 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
26236 TREE_READONLY (value) = 1;
26237 return value;
26240 return objc_build_encode_expr (type);
26243 /* Parse an Objective-C @defs expression. */
26245 static tree
26246 cp_parser_objc_defs_expression (cp_parser *parser)
26248 tree name;
26250 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
26251 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26252 name = cp_parser_identifier (parser);
26253 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26255 return objc_get_class_ivars (name);
26258 /* Parse an Objective-C protocol expression.
26260 objc-protocol-expression:
26261 @protocol ( identifier )
26263 Returns a representation of the protocol expression. */
26265 static tree
26266 cp_parser_objc_protocol_expression (cp_parser* parser)
26268 tree proto;
26270 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26271 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26272 proto = cp_parser_identifier (parser);
26273 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26275 return objc_build_protocol_expr (proto);
26278 /* Parse an Objective-C selector expression.
26280 objc-selector-expression:
26281 @selector ( objc-method-signature )
26283 objc-method-signature:
26284 objc-selector
26285 objc-selector-seq
26287 objc-selector-seq:
26288 objc-selector :
26289 objc-selector-seq objc-selector :
26291 Returns a representation of the method selector. */
26293 static tree
26294 cp_parser_objc_selector_expression (cp_parser* parser)
26296 tree sel_seq = NULL_TREE;
26297 bool maybe_unary_selector_p = true;
26298 cp_token *token;
26299 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26301 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
26302 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26303 token = cp_lexer_peek_token (parser->lexer);
26305 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
26306 || token->type == CPP_SCOPE)
26308 tree selector = NULL_TREE;
26310 if (token->type != CPP_COLON
26311 || token->type == CPP_SCOPE)
26312 selector = cp_parser_objc_selector (parser);
26314 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
26315 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
26317 /* Detect if we have a unary selector. */
26318 if (maybe_unary_selector_p)
26320 sel_seq = selector;
26321 goto finish_selector;
26323 else
26325 cp_parser_error (parser, "expected %<:%>");
26328 maybe_unary_selector_p = false;
26329 token = cp_lexer_consume_token (parser->lexer);
26331 if (token->type == CPP_SCOPE)
26333 sel_seq
26334 = chainon (sel_seq,
26335 build_tree_list (selector, NULL_TREE));
26336 sel_seq
26337 = chainon (sel_seq,
26338 build_tree_list (NULL_TREE, NULL_TREE));
26340 else
26341 sel_seq
26342 = chainon (sel_seq,
26343 build_tree_list (selector, NULL_TREE));
26345 token = cp_lexer_peek_token (parser->lexer);
26348 finish_selector:
26349 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26351 return objc_build_selector_expr (loc, sel_seq);
26354 /* Parse a list of identifiers.
26356 objc-identifier-list:
26357 identifier
26358 objc-identifier-list , identifier
26360 Returns a TREE_LIST of identifier nodes. */
26362 static tree
26363 cp_parser_objc_identifier_list (cp_parser* parser)
26365 tree identifier;
26366 tree list;
26367 cp_token *sep;
26369 identifier = cp_parser_identifier (parser);
26370 if (identifier == error_mark_node)
26371 return error_mark_node;
26373 list = build_tree_list (NULL_TREE, identifier);
26374 sep = cp_lexer_peek_token (parser->lexer);
26376 while (sep->type == CPP_COMMA)
26378 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26379 identifier = cp_parser_identifier (parser);
26380 if (identifier == error_mark_node)
26381 return list;
26383 list = chainon (list, build_tree_list (NULL_TREE,
26384 identifier));
26385 sep = cp_lexer_peek_token (parser->lexer);
26388 return list;
26391 /* Parse an Objective-C alias declaration.
26393 objc-alias-declaration:
26394 @compatibility_alias identifier identifier ;
26396 This function registers the alias mapping with the Objective-C front end.
26397 It returns nothing. */
26399 static void
26400 cp_parser_objc_alias_declaration (cp_parser* parser)
26402 tree alias, orig;
26404 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
26405 alias = cp_parser_identifier (parser);
26406 orig = cp_parser_identifier (parser);
26407 objc_declare_alias (alias, orig);
26408 cp_parser_consume_semicolon_at_end_of_statement (parser);
26411 /* Parse an Objective-C class forward-declaration.
26413 objc-class-declaration:
26414 @class objc-identifier-list ;
26416 The function registers the forward declarations with the Objective-C
26417 front end. It returns nothing. */
26419 static void
26420 cp_parser_objc_class_declaration (cp_parser* parser)
26422 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
26423 while (true)
26425 tree id;
26427 id = cp_parser_identifier (parser);
26428 if (id == error_mark_node)
26429 break;
26431 objc_declare_class (id);
26433 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26434 cp_lexer_consume_token (parser->lexer);
26435 else
26436 break;
26438 cp_parser_consume_semicolon_at_end_of_statement (parser);
26441 /* Parse a list of Objective-C protocol references.
26443 objc-protocol-refs-opt:
26444 objc-protocol-refs [opt]
26446 objc-protocol-refs:
26447 < objc-identifier-list >
26449 Returns a TREE_LIST of identifiers, if any. */
26451 static tree
26452 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26454 tree protorefs = NULL_TREE;
26456 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26458 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26459 protorefs = cp_parser_objc_identifier_list (parser);
26460 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26463 return protorefs;
26466 /* Parse a Objective-C visibility specification. */
26468 static void
26469 cp_parser_objc_visibility_spec (cp_parser* parser)
26471 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26473 switch (vis->keyword)
26475 case RID_AT_PRIVATE:
26476 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26477 break;
26478 case RID_AT_PROTECTED:
26479 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26480 break;
26481 case RID_AT_PUBLIC:
26482 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26483 break;
26484 case RID_AT_PACKAGE:
26485 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26486 break;
26487 default:
26488 return;
26491 /* Eat '@private'/'@protected'/'@public'. */
26492 cp_lexer_consume_token (parser->lexer);
26495 /* Parse an Objective-C method type. Return 'true' if it is a class
26496 (+) method, and 'false' if it is an instance (-) method. */
26498 static inline bool
26499 cp_parser_objc_method_type (cp_parser* parser)
26501 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26502 return true;
26503 else
26504 return false;
26507 /* Parse an Objective-C protocol qualifier. */
26509 static tree
26510 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26512 tree quals = NULL_TREE, node;
26513 cp_token *token = cp_lexer_peek_token (parser->lexer);
26515 node = token->u.value;
26517 while (node && identifier_p (node)
26518 && (node == ridpointers [(int) RID_IN]
26519 || node == ridpointers [(int) RID_OUT]
26520 || node == ridpointers [(int) RID_INOUT]
26521 || node == ridpointers [(int) RID_BYCOPY]
26522 || node == ridpointers [(int) RID_BYREF]
26523 || node == ridpointers [(int) RID_ONEWAY]))
26525 quals = tree_cons (NULL_TREE, node, quals);
26526 cp_lexer_consume_token (parser->lexer);
26527 token = cp_lexer_peek_token (parser->lexer);
26528 node = token->u.value;
26531 return quals;
26534 /* Parse an Objective-C typename. */
26536 static tree
26537 cp_parser_objc_typename (cp_parser* parser)
26539 tree type_name = NULL_TREE;
26541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26543 tree proto_quals, cp_type = NULL_TREE;
26545 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26546 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26548 /* An ObjC type name may consist of just protocol qualifiers, in which
26549 case the type shall default to 'id'. */
26550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26552 cp_type = cp_parser_type_id (parser);
26554 /* If the type could not be parsed, an error has already
26555 been produced. For error recovery, behave as if it had
26556 not been specified, which will use the default type
26557 'id'. */
26558 if (cp_type == error_mark_node)
26560 cp_type = NULL_TREE;
26561 /* We need to skip to the closing parenthesis as
26562 cp_parser_type_id() does not seem to do it for
26563 us. */
26564 cp_parser_skip_to_closing_parenthesis (parser,
26565 /*recovering=*/true,
26566 /*or_comma=*/false,
26567 /*consume_paren=*/false);
26571 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26572 type_name = build_tree_list (proto_quals, cp_type);
26575 return type_name;
26578 /* Check to see if TYPE refers to an Objective-C selector name. */
26580 static bool
26581 cp_parser_objc_selector_p (enum cpp_ttype type)
26583 return (type == CPP_NAME || type == CPP_KEYWORD
26584 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26585 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26586 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26587 || type == CPP_XOR || type == CPP_XOR_EQ);
26590 /* Parse an Objective-C selector. */
26592 static tree
26593 cp_parser_objc_selector (cp_parser* parser)
26595 cp_token *token = cp_lexer_consume_token (parser->lexer);
26597 if (!cp_parser_objc_selector_p (token->type))
26599 error_at (token->location, "invalid Objective-C++ selector name");
26600 return error_mark_node;
26603 /* C++ operator names are allowed to appear in ObjC selectors. */
26604 switch (token->type)
26606 case CPP_AND_AND: return get_identifier ("and");
26607 case CPP_AND_EQ: return get_identifier ("and_eq");
26608 case CPP_AND: return get_identifier ("bitand");
26609 case CPP_OR: return get_identifier ("bitor");
26610 case CPP_COMPL: return get_identifier ("compl");
26611 case CPP_NOT: return get_identifier ("not");
26612 case CPP_NOT_EQ: return get_identifier ("not_eq");
26613 case CPP_OR_OR: return get_identifier ("or");
26614 case CPP_OR_EQ: return get_identifier ("or_eq");
26615 case CPP_XOR: return get_identifier ("xor");
26616 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26617 default: return token->u.value;
26621 /* Parse an Objective-C params list. */
26623 static tree
26624 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26626 tree params = NULL_TREE;
26627 bool maybe_unary_selector_p = true;
26628 cp_token *token = cp_lexer_peek_token (parser->lexer);
26630 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26632 tree selector = NULL_TREE, type_name, identifier;
26633 tree parm_attr = NULL_TREE;
26635 if (token->keyword == RID_ATTRIBUTE)
26636 break;
26638 if (token->type != CPP_COLON)
26639 selector = cp_parser_objc_selector (parser);
26641 /* Detect if we have a unary selector. */
26642 if (maybe_unary_selector_p
26643 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26645 params = selector; /* Might be followed by attributes. */
26646 break;
26649 maybe_unary_selector_p = false;
26650 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26652 /* Something went quite wrong. There should be a colon
26653 here, but there is not. Stop parsing parameters. */
26654 break;
26656 type_name = cp_parser_objc_typename (parser);
26657 /* New ObjC allows attributes on parameters too. */
26658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26659 parm_attr = cp_parser_attributes_opt (parser);
26660 identifier = cp_parser_identifier (parser);
26662 params
26663 = chainon (params,
26664 objc_build_keyword_decl (selector,
26665 type_name,
26666 identifier,
26667 parm_attr));
26669 token = cp_lexer_peek_token (parser->lexer);
26672 if (params == NULL_TREE)
26674 cp_parser_error (parser, "objective-c++ method declaration is expected");
26675 return error_mark_node;
26678 /* We allow tail attributes for the method. */
26679 if (token->keyword == RID_ATTRIBUTE)
26681 *attributes = cp_parser_attributes_opt (parser);
26682 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26683 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26684 return params;
26685 cp_parser_error (parser,
26686 "method attributes must be specified at the end");
26687 return error_mark_node;
26690 if (params == NULL_TREE)
26692 cp_parser_error (parser, "objective-c++ method declaration is expected");
26693 return error_mark_node;
26695 return params;
26698 /* Parse the non-keyword Objective-C params. */
26700 static tree
26701 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26702 tree* attributes)
26704 tree params = make_node (TREE_LIST);
26705 cp_token *token = cp_lexer_peek_token (parser->lexer);
26706 *ellipsisp = false; /* Initially, assume no ellipsis. */
26708 while (token->type == CPP_COMMA)
26710 cp_parameter_declarator *parmdecl;
26711 tree parm;
26713 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26714 token = cp_lexer_peek_token (parser->lexer);
26716 if (token->type == CPP_ELLIPSIS)
26718 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26719 *ellipsisp = true;
26720 token = cp_lexer_peek_token (parser->lexer);
26721 break;
26724 /* TODO: parse attributes for tail parameters. */
26725 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26726 parm = grokdeclarator (parmdecl->declarator,
26727 &parmdecl->decl_specifiers,
26728 PARM, /*initialized=*/0,
26729 /*attrlist=*/NULL);
26731 chainon (params, build_tree_list (NULL_TREE, parm));
26732 token = cp_lexer_peek_token (parser->lexer);
26735 /* We allow tail attributes for the method. */
26736 if (token->keyword == RID_ATTRIBUTE)
26738 if (*attributes == NULL_TREE)
26740 *attributes = cp_parser_attributes_opt (parser);
26741 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26742 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26743 return params;
26745 else
26746 /* We have an error, but parse the attributes, so that we can
26747 carry on. */
26748 *attributes = cp_parser_attributes_opt (parser);
26750 cp_parser_error (parser,
26751 "method attributes must be specified at the end");
26752 return error_mark_node;
26755 return params;
26758 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26760 static void
26761 cp_parser_objc_interstitial_code (cp_parser* parser)
26763 cp_token *token = cp_lexer_peek_token (parser->lexer);
26765 /* If the next token is `extern' and the following token is a string
26766 literal, then we have a linkage specification. */
26767 if (token->keyword == RID_EXTERN
26768 && cp_parser_is_pure_string_literal
26769 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26770 cp_parser_linkage_specification (parser);
26771 /* Handle #pragma, if any. */
26772 else if (token->type == CPP_PRAGMA)
26773 cp_parser_pragma (parser, pragma_objc_icode);
26774 /* Allow stray semicolons. */
26775 else if (token->type == CPP_SEMICOLON)
26776 cp_lexer_consume_token (parser->lexer);
26777 /* Mark methods as optional or required, when building protocols. */
26778 else if (token->keyword == RID_AT_OPTIONAL)
26780 cp_lexer_consume_token (parser->lexer);
26781 objc_set_method_opt (true);
26783 else if (token->keyword == RID_AT_REQUIRED)
26785 cp_lexer_consume_token (parser->lexer);
26786 objc_set_method_opt (false);
26788 else if (token->keyword == RID_NAMESPACE)
26789 cp_parser_namespace_definition (parser);
26790 /* Other stray characters must generate errors. */
26791 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26793 cp_lexer_consume_token (parser->lexer);
26794 error ("stray %qs between Objective-C++ methods",
26795 token->type == CPP_OPEN_BRACE ? "{" : "}");
26797 /* Finally, try to parse a block-declaration, or a function-definition. */
26798 else
26799 cp_parser_block_declaration (parser, /*statement_p=*/false);
26802 /* Parse a method signature. */
26804 static tree
26805 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26807 tree rettype, kwdparms, optparms;
26808 bool ellipsis = false;
26809 bool is_class_method;
26811 is_class_method = cp_parser_objc_method_type (parser);
26812 rettype = cp_parser_objc_typename (parser);
26813 *attributes = NULL_TREE;
26814 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26815 if (kwdparms == error_mark_node)
26816 return error_mark_node;
26817 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26818 if (optparms == error_mark_node)
26819 return error_mark_node;
26821 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26824 static bool
26825 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26827 tree tattr;
26828 cp_lexer_save_tokens (parser->lexer);
26829 tattr = cp_parser_attributes_opt (parser);
26830 gcc_assert (tattr) ;
26832 /* If the attributes are followed by a method introducer, this is not allowed.
26833 Dump the attributes and flag the situation. */
26834 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26835 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26836 return true;
26838 /* Otherwise, the attributes introduce some interstitial code, possibly so
26839 rewind to allow that check. */
26840 cp_lexer_rollback_tokens (parser->lexer);
26841 return false;
26844 /* Parse an Objective-C method prototype list. */
26846 static void
26847 cp_parser_objc_method_prototype_list (cp_parser* parser)
26849 cp_token *token = cp_lexer_peek_token (parser->lexer);
26851 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26853 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26855 tree attributes, sig;
26856 bool is_class_method;
26857 if (token->type == CPP_PLUS)
26858 is_class_method = true;
26859 else
26860 is_class_method = false;
26861 sig = cp_parser_objc_method_signature (parser, &attributes);
26862 if (sig == error_mark_node)
26864 cp_parser_skip_to_end_of_block_or_statement (parser);
26865 token = cp_lexer_peek_token (parser->lexer);
26866 continue;
26868 objc_add_method_declaration (is_class_method, sig, attributes);
26869 cp_parser_consume_semicolon_at_end_of_statement (parser);
26871 else if (token->keyword == RID_AT_PROPERTY)
26872 cp_parser_objc_at_property_declaration (parser);
26873 else if (token->keyword == RID_ATTRIBUTE
26874 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26875 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26876 OPT_Wattributes,
26877 "prefix attributes are ignored for methods");
26878 else
26879 /* Allow for interspersed non-ObjC++ code. */
26880 cp_parser_objc_interstitial_code (parser);
26882 token = cp_lexer_peek_token (parser->lexer);
26885 if (token->type != CPP_EOF)
26886 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26887 else
26888 cp_parser_error (parser, "expected %<@end%>");
26890 objc_finish_interface ();
26893 /* Parse an Objective-C method definition list. */
26895 static void
26896 cp_parser_objc_method_definition_list (cp_parser* parser)
26898 cp_token *token = cp_lexer_peek_token (parser->lexer);
26900 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26902 tree meth;
26904 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26906 cp_token *ptk;
26907 tree sig, attribute;
26908 bool is_class_method;
26909 if (token->type == CPP_PLUS)
26910 is_class_method = true;
26911 else
26912 is_class_method = false;
26913 push_deferring_access_checks (dk_deferred);
26914 sig = cp_parser_objc_method_signature (parser, &attribute);
26915 if (sig == error_mark_node)
26917 cp_parser_skip_to_end_of_block_or_statement (parser);
26918 token = cp_lexer_peek_token (parser->lexer);
26919 continue;
26921 objc_start_method_definition (is_class_method, sig, attribute,
26922 NULL_TREE);
26924 /* For historical reasons, we accept an optional semicolon. */
26925 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26926 cp_lexer_consume_token (parser->lexer);
26928 ptk = cp_lexer_peek_token (parser->lexer);
26929 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26930 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26932 perform_deferred_access_checks (tf_warning_or_error);
26933 stop_deferring_access_checks ();
26934 meth = cp_parser_function_definition_after_declarator (parser,
26935 false);
26936 pop_deferring_access_checks ();
26937 objc_finish_method_definition (meth);
26940 /* The following case will be removed once @synthesize is
26941 completely implemented. */
26942 else if (token->keyword == RID_AT_PROPERTY)
26943 cp_parser_objc_at_property_declaration (parser);
26944 else if (token->keyword == RID_AT_SYNTHESIZE)
26945 cp_parser_objc_at_synthesize_declaration (parser);
26946 else if (token->keyword == RID_AT_DYNAMIC)
26947 cp_parser_objc_at_dynamic_declaration (parser);
26948 else if (token->keyword == RID_ATTRIBUTE
26949 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26950 warning_at (token->location, OPT_Wattributes,
26951 "prefix attributes are ignored for methods");
26952 else
26953 /* Allow for interspersed non-ObjC++ code. */
26954 cp_parser_objc_interstitial_code (parser);
26956 token = cp_lexer_peek_token (parser->lexer);
26959 if (token->type != CPP_EOF)
26960 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26961 else
26962 cp_parser_error (parser, "expected %<@end%>");
26964 objc_finish_implementation ();
26967 /* Parse Objective-C ivars. */
26969 static void
26970 cp_parser_objc_class_ivars (cp_parser* parser)
26972 cp_token *token = cp_lexer_peek_token (parser->lexer);
26974 if (token->type != CPP_OPEN_BRACE)
26975 return; /* No ivars specified. */
26977 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26978 token = cp_lexer_peek_token (parser->lexer);
26980 while (token->type != CPP_CLOSE_BRACE
26981 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26983 cp_decl_specifier_seq declspecs;
26984 int decl_class_or_enum_p;
26985 tree prefix_attributes;
26987 cp_parser_objc_visibility_spec (parser);
26989 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26990 break;
26992 cp_parser_decl_specifier_seq (parser,
26993 CP_PARSER_FLAGS_OPTIONAL,
26994 &declspecs,
26995 &decl_class_or_enum_p);
26997 /* auto, register, static, extern, mutable. */
26998 if (declspecs.storage_class != sc_none)
27000 cp_parser_error (parser, "invalid type for instance variable");
27001 declspecs.storage_class = sc_none;
27004 /* thread_local. */
27005 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27007 cp_parser_error (parser, "invalid type for instance variable");
27008 declspecs.locations[ds_thread] = 0;
27011 /* typedef. */
27012 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27014 cp_parser_error (parser, "invalid type for instance variable");
27015 declspecs.locations[ds_typedef] = 0;
27018 prefix_attributes = declspecs.attributes;
27019 declspecs.attributes = NULL_TREE;
27021 /* Keep going until we hit the `;' at the end of the
27022 declaration. */
27023 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27025 tree width = NULL_TREE, attributes, first_attribute, decl;
27026 cp_declarator *declarator = NULL;
27027 int ctor_dtor_or_conv_p;
27029 /* Check for a (possibly unnamed) bitfield declaration. */
27030 token = cp_lexer_peek_token (parser->lexer);
27031 if (token->type == CPP_COLON)
27032 goto eat_colon;
27034 if (token->type == CPP_NAME
27035 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
27036 == CPP_COLON))
27038 /* Get the name of the bitfield. */
27039 declarator = make_id_declarator (NULL_TREE,
27040 cp_parser_identifier (parser),
27041 sfk_none);
27043 eat_colon:
27044 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
27045 /* Get the width of the bitfield. */
27046 width
27047 = cp_parser_constant_expression (parser,
27048 /*allow_non_constant=*/false,
27049 NULL);
27051 else
27053 /* Parse the declarator. */
27054 declarator
27055 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27056 &ctor_dtor_or_conv_p,
27057 /*parenthesized_p=*/NULL,
27058 /*member_p=*/false,
27059 /*friend_p=*/false);
27062 /* Look for attributes that apply to the ivar. */
27063 attributes = cp_parser_attributes_opt (parser);
27064 /* Remember which attributes are prefix attributes and
27065 which are not. */
27066 first_attribute = attributes;
27067 /* Combine the attributes. */
27068 attributes = chainon (prefix_attributes, attributes);
27070 if (width)
27071 /* Create the bitfield declaration. */
27072 decl = grokbitfield (declarator, &declspecs,
27073 width,
27074 attributes);
27075 else
27076 decl = grokfield (declarator, &declspecs,
27077 NULL_TREE, /*init_const_expr_p=*/false,
27078 NULL_TREE, attributes);
27080 /* Add the instance variable. */
27081 if (decl != error_mark_node && decl != NULL_TREE)
27082 objc_add_instance_variable (decl);
27084 /* Reset PREFIX_ATTRIBUTES. */
27085 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27086 attributes = TREE_CHAIN (attributes);
27087 if (attributes)
27088 TREE_CHAIN (attributes) = NULL_TREE;
27090 token = cp_lexer_peek_token (parser->lexer);
27092 if (token->type == CPP_COMMA)
27094 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27095 continue;
27097 break;
27100 cp_parser_consume_semicolon_at_end_of_statement (parser);
27101 token = cp_lexer_peek_token (parser->lexer);
27104 if (token->keyword == RID_AT_END)
27105 cp_parser_error (parser, "expected %<}%>");
27107 /* Do not consume the RID_AT_END, so it will be read again as terminating
27108 the @interface of @implementation. */
27109 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
27110 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
27112 /* For historical reasons, we accept an optional semicolon. */
27113 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27114 cp_lexer_consume_token (parser->lexer);
27117 /* Parse an Objective-C protocol declaration. */
27119 static void
27120 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
27122 tree proto, protorefs;
27123 cp_token *tok;
27125 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
27128 tok = cp_lexer_peek_token (parser->lexer);
27129 error_at (tok->location, "identifier expected after %<@protocol%>");
27130 cp_parser_consume_semicolon_at_end_of_statement (parser);
27131 return;
27134 /* See if we have a forward declaration or a definition. */
27135 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
27137 /* Try a forward declaration first. */
27138 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
27140 while (true)
27142 tree id;
27144 id = cp_parser_identifier (parser);
27145 if (id == error_mark_node)
27146 break;
27148 objc_declare_protocol (id, attributes);
27150 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27151 cp_lexer_consume_token (parser->lexer);
27152 else
27153 break;
27155 cp_parser_consume_semicolon_at_end_of_statement (parser);
27158 /* Ok, we got a full-fledged definition (or at least should). */
27159 else
27161 proto = cp_parser_identifier (parser);
27162 protorefs = cp_parser_objc_protocol_refs_opt (parser);
27163 objc_start_protocol (proto, protorefs, attributes);
27164 cp_parser_objc_method_prototype_list (parser);
27168 /* Parse an Objective-C superclass or category. */
27170 static void
27171 cp_parser_objc_superclass_or_category (cp_parser *parser,
27172 bool iface_p,
27173 tree *super,
27174 tree *categ, bool *is_class_extension)
27176 cp_token *next = cp_lexer_peek_token (parser->lexer);
27178 *super = *categ = NULL_TREE;
27179 *is_class_extension = false;
27180 if (next->type == CPP_COLON)
27182 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
27183 *super = cp_parser_identifier (parser);
27185 else if (next->type == CPP_OPEN_PAREN)
27187 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27189 /* If there is no category name, and this is an @interface, we
27190 have a class extension. */
27191 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27193 *categ = NULL_TREE;
27194 *is_class_extension = true;
27196 else
27197 *categ = cp_parser_identifier (parser);
27199 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27203 /* Parse an Objective-C class interface. */
27205 static void
27206 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
27208 tree name, super, categ, protos;
27209 bool is_class_extension;
27211 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
27212 name = cp_parser_identifier (parser);
27213 if (name == error_mark_node)
27215 /* It's hard to recover because even if valid @interface stuff
27216 is to follow, we can't compile it (or validate it) if we
27217 don't even know which class it refers to. Let's assume this
27218 was a stray '@interface' token in the stream and skip it.
27220 return;
27222 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
27223 &is_class_extension);
27224 protos = cp_parser_objc_protocol_refs_opt (parser);
27226 /* We have either a class or a category on our hands. */
27227 if (categ || is_class_extension)
27228 objc_start_category_interface (name, categ, protos, attributes);
27229 else
27231 objc_start_class_interface (name, super, protos, attributes);
27232 /* Handle instance variable declarations, if any. */
27233 cp_parser_objc_class_ivars (parser);
27234 objc_continue_interface ();
27237 cp_parser_objc_method_prototype_list (parser);
27240 /* Parse an Objective-C class implementation. */
27242 static void
27243 cp_parser_objc_class_implementation (cp_parser* parser)
27245 tree name, super, categ;
27246 bool is_class_extension;
27248 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
27249 name = cp_parser_identifier (parser);
27250 if (name == error_mark_node)
27252 /* It's hard to recover because even if valid @implementation
27253 stuff is to follow, we can't compile it (or validate it) if
27254 we don't even know which class it refers to. Let's assume
27255 this was a stray '@implementation' token in the stream and
27256 skip it.
27258 return;
27260 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
27261 &is_class_extension);
27263 /* We have either a class or a category on our hands. */
27264 if (categ)
27265 objc_start_category_implementation (name, categ);
27266 else
27268 objc_start_class_implementation (name, super);
27269 /* Handle instance variable declarations, if any. */
27270 cp_parser_objc_class_ivars (parser);
27271 objc_continue_implementation ();
27274 cp_parser_objc_method_definition_list (parser);
27277 /* Consume the @end token and finish off the implementation. */
27279 static void
27280 cp_parser_objc_end_implementation (cp_parser* parser)
27282 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
27283 objc_finish_implementation ();
27286 /* Parse an Objective-C declaration. */
27288 static void
27289 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
27291 /* Try to figure out what kind of declaration is present. */
27292 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27294 if (attributes)
27295 switch (kwd->keyword)
27297 case RID_AT_ALIAS:
27298 case RID_AT_CLASS:
27299 case RID_AT_END:
27300 error_at (kwd->location, "attributes may not be specified before"
27301 " the %<@%D%> Objective-C++ keyword",
27302 kwd->u.value);
27303 attributes = NULL;
27304 break;
27305 case RID_AT_IMPLEMENTATION:
27306 warning_at (kwd->location, OPT_Wattributes,
27307 "prefix attributes are ignored before %<@%D%>",
27308 kwd->u.value);
27309 attributes = NULL;
27310 default:
27311 break;
27314 switch (kwd->keyword)
27316 case RID_AT_ALIAS:
27317 cp_parser_objc_alias_declaration (parser);
27318 break;
27319 case RID_AT_CLASS:
27320 cp_parser_objc_class_declaration (parser);
27321 break;
27322 case RID_AT_PROTOCOL:
27323 cp_parser_objc_protocol_declaration (parser, attributes);
27324 break;
27325 case RID_AT_INTERFACE:
27326 cp_parser_objc_class_interface (parser, attributes);
27327 break;
27328 case RID_AT_IMPLEMENTATION:
27329 cp_parser_objc_class_implementation (parser);
27330 break;
27331 case RID_AT_END:
27332 cp_parser_objc_end_implementation (parser);
27333 break;
27334 default:
27335 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27336 kwd->u.value);
27337 cp_parser_skip_to_end_of_block_or_statement (parser);
27341 /* Parse an Objective-C try-catch-finally statement.
27343 objc-try-catch-finally-stmt:
27344 @try compound-statement objc-catch-clause-seq [opt]
27345 objc-finally-clause [opt]
27347 objc-catch-clause-seq:
27348 objc-catch-clause objc-catch-clause-seq [opt]
27350 objc-catch-clause:
27351 @catch ( objc-exception-declaration ) compound-statement
27353 objc-finally-clause:
27354 @finally compound-statement
27356 objc-exception-declaration:
27357 parameter-declaration
27358 '...'
27360 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27362 Returns NULL_TREE.
27364 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27365 for C. Keep them in sync. */
27367 static tree
27368 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27370 location_t location;
27371 tree stmt;
27373 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27374 location = cp_lexer_peek_token (parser->lexer)->location;
27375 objc_maybe_warn_exceptions (location);
27376 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27377 node, lest it get absorbed into the surrounding block. */
27378 stmt = push_stmt_list ();
27379 cp_parser_compound_statement (parser, NULL, false, false);
27380 objc_begin_try_stmt (location, pop_stmt_list (stmt));
27382 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27384 cp_parameter_declarator *parm;
27385 tree parameter_declaration = error_mark_node;
27386 bool seen_open_paren = false;
27388 cp_lexer_consume_token (parser->lexer);
27389 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27390 seen_open_paren = true;
27391 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27393 /* We have "@catch (...)" (where the '...' are literally
27394 what is in the code). Skip the '...'.
27395 parameter_declaration is set to NULL_TREE, and
27396 objc_being_catch_clauses() knows that that means
27397 '...'. */
27398 cp_lexer_consume_token (parser->lexer);
27399 parameter_declaration = NULL_TREE;
27401 else
27403 /* We have "@catch (NSException *exception)" or something
27404 like that. Parse the parameter declaration. */
27405 parm = cp_parser_parameter_declaration (parser, false, NULL);
27406 if (parm == NULL)
27407 parameter_declaration = error_mark_node;
27408 else
27409 parameter_declaration = grokdeclarator (parm->declarator,
27410 &parm->decl_specifiers,
27411 PARM, /*initialized=*/0,
27412 /*attrlist=*/NULL);
27414 if (seen_open_paren)
27415 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27416 else
27418 /* If there was no open parenthesis, we are recovering from
27419 an error, and we are trying to figure out what mistake
27420 the user has made. */
27422 /* If there is an immediate closing parenthesis, the user
27423 probably forgot the opening one (ie, they typed "@catch
27424 NSException *e)". Parse the closing parenthesis and keep
27425 going. */
27426 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27427 cp_lexer_consume_token (parser->lexer);
27429 /* If these is no immediate closing parenthesis, the user
27430 probably doesn't know that parenthesis are required at
27431 all (ie, they typed "@catch NSException *e"). So, just
27432 forget about the closing parenthesis and keep going. */
27434 objc_begin_catch_clause (parameter_declaration);
27435 cp_parser_compound_statement (parser, NULL, false, false);
27436 objc_finish_catch_clause ();
27438 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27440 cp_lexer_consume_token (parser->lexer);
27441 location = cp_lexer_peek_token (parser->lexer)->location;
27442 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27443 node, lest it get absorbed into the surrounding block. */
27444 stmt = push_stmt_list ();
27445 cp_parser_compound_statement (parser, NULL, false, false);
27446 objc_build_finally_clause (location, pop_stmt_list (stmt));
27449 return objc_finish_try_stmt ();
27452 /* Parse an Objective-C synchronized statement.
27454 objc-synchronized-stmt:
27455 @synchronized ( expression ) compound-statement
27457 Returns NULL_TREE. */
27459 static tree
27460 cp_parser_objc_synchronized_statement (cp_parser *parser)
27462 location_t location;
27463 tree lock, stmt;
27465 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27467 location = cp_lexer_peek_token (parser->lexer)->location;
27468 objc_maybe_warn_exceptions (location);
27469 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27470 lock = cp_parser_expression (parser, false, NULL);
27471 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27473 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27474 node, lest it get absorbed into the surrounding block. */
27475 stmt = push_stmt_list ();
27476 cp_parser_compound_statement (parser, NULL, false, false);
27478 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27481 /* Parse an Objective-C throw statement.
27483 objc-throw-stmt:
27484 @throw assignment-expression [opt] ;
27486 Returns a constructed '@throw' statement. */
27488 static tree
27489 cp_parser_objc_throw_statement (cp_parser *parser)
27491 tree expr = NULL_TREE;
27492 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27494 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27497 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27499 cp_parser_consume_semicolon_at_end_of_statement (parser);
27501 return objc_build_throw_stmt (loc, expr);
27504 /* Parse an Objective-C statement. */
27506 static tree
27507 cp_parser_objc_statement (cp_parser * parser)
27509 /* Try to figure out what kind of declaration is present. */
27510 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27512 switch (kwd->keyword)
27514 case RID_AT_TRY:
27515 return cp_parser_objc_try_catch_finally_statement (parser);
27516 case RID_AT_SYNCHRONIZED:
27517 return cp_parser_objc_synchronized_statement (parser);
27518 case RID_AT_THROW:
27519 return cp_parser_objc_throw_statement (parser);
27520 default:
27521 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27522 kwd->u.value);
27523 cp_parser_skip_to_end_of_block_or_statement (parser);
27526 return error_mark_node;
27529 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27530 look ahead to see if an objc keyword follows the attributes. This
27531 is to detect the use of prefix attributes on ObjC @interface and
27532 @protocol. */
27534 static bool
27535 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27537 cp_lexer_save_tokens (parser->lexer);
27538 *attrib = cp_parser_attributes_opt (parser);
27539 gcc_assert (*attrib);
27540 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27542 cp_lexer_commit_tokens (parser->lexer);
27543 return true;
27545 cp_lexer_rollback_tokens (parser->lexer);
27546 return false;
27549 /* This routine is a minimal replacement for
27550 c_parser_struct_declaration () used when parsing the list of
27551 types/names or ObjC++ properties. For example, when parsing the
27552 code
27554 @property (readonly) int a, b, c;
27556 this function is responsible for parsing "int a, int b, int c" and
27557 returning the declarations as CHAIN of DECLs.
27559 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27560 similar parsing. */
27561 static tree
27562 cp_parser_objc_struct_declaration (cp_parser *parser)
27564 tree decls = NULL_TREE;
27565 cp_decl_specifier_seq declspecs;
27566 int decl_class_or_enum_p;
27567 tree prefix_attributes;
27569 cp_parser_decl_specifier_seq (parser,
27570 CP_PARSER_FLAGS_NONE,
27571 &declspecs,
27572 &decl_class_or_enum_p);
27574 if (declspecs.type == error_mark_node)
27575 return error_mark_node;
27577 /* auto, register, static, extern, mutable. */
27578 if (declspecs.storage_class != sc_none)
27580 cp_parser_error (parser, "invalid type for property");
27581 declspecs.storage_class = sc_none;
27584 /* thread_local. */
27585 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27587 cp_parser_error (parser, "invalid type for property");
27588 declspecs.locations[ds_thread] = 0;
27591 /* typedef. */
27592 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27594 cp_parser_error (parser, "invalid type for property");
27595 declspecs.locations[ds_typedef] = 0;
27598 prefix_attributes = declspecs.attributes;
27599 declspecs.attributes = NULL_TREE;
27601 /* Keep going until we hit the `;' at the end of the declaration. */
27602 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27604 tree attributes, first_attribute, decl;
27605 cp_declarator *declarator;
27606 cp_token *token;
27608 /* Parse the declarator. */
27609 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27610 NULL, NULL, false, false);
27612 /* Look for attributes that apply to the ivar. */
27613 attributes = cp_parser_attributes_opt (parser);
27614 /* Remember which attributes are prefix attributes and
27615 which are not. */
27616 first_attribute = attributes;
27617 /* Combine the attributes. */
27618 attributes = chainon (prefix_attributes, attributes);
27620 decl = grokfield (declarator, &declspecs,
27621 NULL_TREE, /*init_const_expr_p=*/false,
27622 NULL_TREE, attributes);
27624 if (decl == error_mark_node || decl == NULL_TREE)
27625 return error_mark_node;
27627 /* Reset PREFIX_ATTRIBUTES. */
27628 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27629 attributes = TREE_CHAIN (attributes);
27630 if (attributes)
27631 TREE_CHAIN (attributes) = NULL_TREE;
27633 DECL_CHAIN (decl) = decls;
27634 decls = decl;
27636 token = cp_lexer_peek_token (parser->lexer);
27637 if (token->type == CPP_COMMA)
27639 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27640 continue;
27642 else
27643 break;
27645 return decls;
27648 /* Parse an Objective-C @property declaration. The syntax is:
27650 objc-property-declaration:
27651 '@property' objc-property-attributes[opt] struct-declaration ;
27653 objc-property-attributes:
27654 '(' objc-property-attribute-list ')'
27656 objc-property-attribute-list:
27657 objc-property-attribute
27658 objc-property-attribute-list, objc-property-attribute
27660 objc-property-attribute
27661 'getter' = identifier
27662 'setter' = identifier
27663 'readonly'
27664 'readwrite'
27665 'assign'
27666 'retain'
27667 'copy'
27668 'nonatomic'
27670 For example:
27671 @property NSString *name;
27672 @property (readonly) id object;
27673 @property (retain, nonatomic, getter=getTheName) id name;
27674 @property int a, b, c;
27676 PS: This function is identical to
27677 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27678 static void
27679 cp_parser_objc_at_property_declaration (cp_parser *parser)
27681 /* The following variables hold the attributes of the properties as
27682 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27683 seen. When we see an attribute, we set them to 'true' (if they
27684 are boolean properties) or to the identifier (if they have an
27685 argument, ie, for getter and setter). Note that here we only
27686 parse the list of attributes, check the syntax and accumulate the
27687 attributes that we find. objc_add_property_declaration() will
27688 then process the information. */
27689 bool property_assign = false;
27690 bool property_copy = false;
27691 tree property_getter_ident = NULL_TREE;
27692 bool property_nonatomic = false;
27693 bool property_readonly = false;
27694 bool property_readwrite = false;
27695 bool property_retain = false;
27696 tree property_setter_ident = NULL_TREE;
27698 /* 'properties' is the list of properties that we read. Usually a
27699 single one, but maybe more (eg, in "@property int a, b, c;" there
27700 are three). */
27701 tree properties;
27702 location_t loc;
27704 loc = cp_lexer_peek_token (parser->lexer)->location;
27706 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27708 /* Parse the optional attribute list... */
27709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27711 /* Eat the '('. */
27712 cp_lexer_consume_token (parser->lexer);
27714 while (true)
27716 bool syntax_error = false;
27717 cp_token *token = cp_lexer_peek_token (parser->lexer);
27718 enum rid keyword;
27720 if (token->type != CPP_NAME)
27722 cp_parser_error (parser, "expected identifier");
27723 break;
27725 keyword = C_RID_CODE (token->u.value);
27726 cp_lexer_consume_token (parser->lexer);
27727 switch (keyword)
27729 case RID_ASSIGN: property_assign = true; break;
27730 case RID_COPY: property_copy = true; break;
27731 case RID_NONATOMIC: property_nonatomic = true; break;
27732 case RID_READONLY: property_readonly = true; break;
27733 case RID_READWRITE: property_readwrite = true; break;
27734 case RID_RETAIN: property_retain = true; break;
27736 case RID_GETTER:
27737 case RID_SETTER:
27738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27740 if (keyword == RID_GETTER)
27741 cp_parser_error (parser,
27742 "missing %<=%> (after %<getter%> attribute)");
27743 else
27744 cp_parser_error (parser,
27745 "missing %<=%> (after %<setter%> attribute)");
27746 syntax_error = true;
27747 break;
27749 cp_lexer_consume_token (parser->lexer); /* eat the = */
27750 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27752 cp_parser_error (parser, "expected identifier");
27753 syntax_error = true;
27754 break;
27756 if (keyword == RID_SETTER)
27758 if (property_setter_ident != NULL_TREE)
27760 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27761 cp_lexer_consume_token (parser->lexer);
27763 else
27764 property_setter_ident = cp_parser_objc_selector (parser);
27765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27766 cp_parser_error (parser, "setter name must terminate with %<:%>");
27767 else
27768 cp_lexer_consume_token (parser->lexer);
27770 else
27772 if (property_getter_ident != NULL_TREE)
27774 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27775 cp_lexer_consume_token (parser->lexer);
27777 else
27778 property_getter_ident = cp_parser_objc_selector (parser);
27780 break;
27781 default:
27782 cp_parser_error (parser, "unknown property attribute");
27783 syntax_error = true;
27784 break;
27787 if (syntax_error)
27788 break;
27790 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27791 cp_lexer_consume_token (parser->lexer);
27792 else
27793 break;
27796 /* FIXME: "@property (setter, assign);" will generate a spurious
27797 "error: expected ‘)’ before ‘,’ token". This is because
27798 cp_parser_require, unlike the C counterpart, will produce an
27799 error even if we are in error recovery. */
27800 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27802 cp_parser_skip_to_closing_parenthesis (parser,
27803 /*recovering=*/true,
27804 /*or_comma=*/false,
27805 /*consume_paren=*/true);
27809 /* ... and the property declaration(s). */
27810 properties = cp_parser_objc_struct_declaration (parser);
27812 if (properties == error_mark_node)
27814 cp_parser_skip_to_end_of_statement (parser);
27815 /* If the next token is now a `;', consume it. */
27816 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27817 cp_lexer_consume_token (parser->lexer);
27818 return;
27821 if (properties == NULL_TREE)
27822 cp_parser_error (parser, "expected identifier");
27823 else
27825 /* Comma-separated properties are chained together in
27826 reverse order; add them one by one. */
27827 properties = nreverse (properties);
27829 for (; properties; properties = TREE_CHAIN (properties))
27830 objc_add_property_declaration (loc, copy_node (properties),
27831 property_readonly, property_readwrite,
27832 property_assign, property_retain,
27833 property_copy, property_nonatomic,
27834 property_getter_ident, property_setter_ident);
27837 cp_parser_consume_semicolon_at_end_of_statement (parser);
27840 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27842 objc-synthesize-declaration:
27843 @synthesize objc-synthesize-identifier-list ;
27845 objc-synthesize-identifier-list:
27846 objc-synthesize-identifier
27847 objc-synthesize-identifier-list, objc-synthesize-identifier
27849 objc-synthesize-identifier
27850 identifier
27851 identifier = identifier
27853 For example:
27854 @synthesize MyProperty;
27855 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27857 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27858 for C. Keep them in sync.
27860 static void
27861 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27863 tree list = NULL_TREE;
27864 location_t loc;
27865 loc = cp_lexer_peek_token (parser->lexer)->location;
27867 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27868 while (true)
27870 tree property, ivar;
27871 property = cp_parser_identifier (parser);
27872 if (property == error_mark_node)
27874 cp_parser_consume_semicolon_at_end_of_statement (parser);
27875 return;
27877 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27879 cp_lexer_consume_token (parser->lexer);
27880 ivar = cp_parser_identifier (parser);
27881 if (ivar == error_mark_node)
27883 cp_parser_consume_semicolon_at_end_of_statement (parser);
27884 return;
27887 else
27888 ivar = NULL_TREE;
27889 list = chainon (list, build_tree_list (ivar, property));
27890 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27891 cp_lexer_consume_token (parser->lexer);
27892 else
27893 break;
27895 cp_parser_consume_semicolon_at_end_of_statement (parser);
27896 objc_add_synthesize_declaration (loc, list);
27899 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27901 objc-dynamic-declaration:
27902 @dynamic identifier-list ;
27904 For example:
27905 @dynamic MyProperty;
27906 @dynamic MyProperty, AnotherProperty;
27908 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27909 for C. Keep them in sync.
27911 static void
27912 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27914 tree list = NULL_TREE;
27915 location_t loc;
27916 loc = cp_lexer_peek_token (parser->lexer)->location;
27918 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27919 while (true)
27921 tree property;
27922 property = cp_parser_identifier (parser);
27923 if (property == error_mark_node)
27925 cp_parser_consume_semicolon_at_end_of_statement (parser);
27926 return;
27928 list = chainon (list, build_tree_list (NULL, property));
27929 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27930 cp_lexer_consume_token (parser->lexer);
27931 else
27932 break;
27934 cp_parser_consume_semicolon_at_end_of_statement (parser);
27935 objc_add_dynamic_declaration (loc, list);
27939 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27941 /* Returns name of the next clause.
27942 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27943 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27944 returned and the token is consumed. */
27946 static pragma_omp_clause
27947 cp_parser_omp_clause_name (cp_parser *parser)
27949 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27952 result = PRAGMA_OMP_CLAUSE_IF;
27953 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27954 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27955 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27956 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27957 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27958 result = PRAGMA_OMP_CLAUSE_FOR;
27959 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27961 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27962 const char *p = IDENTIFIER_POINTER (id);
27964 switch (p[0])
27966 case 'a':
27967 if (!strcmp ("aligned", p))
27968 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27969 break;
27970 case 'c':
27971 if (!strcmp ("collapse", p))
27972 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27973 else if (!strcmp ("copyin", p))
27974 result = PRAGMA_OMP_CLAUSE_COPYIN;
27975 else if (!strcmp ("copyprivate", p))
27976 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27977 break;
27978 case 'd':
27979 if (!strcmp ("depend", p))
27980 result = PRAGMA_OMP_CLAUSE_DEPEND;
27981 else if (!strcmp ("device", p))
27982 result = PRAGMA_OMP_CLAUSE_DEVICE;
27983 else if (!strcmp ("dist_schedule", p))
27984 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27985 break;
27986 case 'f':
27987 if (!strcmp ("final", p))
27988 result = PRAGMA_OMP_CLAUSE_FINAL;
27989 else if (!strcmp ("firstprivate", p))
27990 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27991 else if (!strcmp ("from", p))
27992 result = PRAGMA_OMP_CLAUSE_FROM;
27993 break;
27994 case 'i':
27995 if (!strcmp ("inbranch", p))
27996 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27997 break;
27998 case 'l':
27999 if (!strcmp ("lastprivate", p))
28000 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
28001 else if (!strcmp ("linear", p))
28002 result = PRAGMA_OMP_CLAUSE_LINEAR;
28003 break;
28004 case 'm':
28005 if (!strcmp ("map", p))
28006 result = PRAGMA_OMP_CLAUSE_MAP;
28007 else if (!strcmp ("mergeable", p))
28008 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
28009 else if (flag_cilkplus && !strcmp ("mask", p))
28010 result = PRAGMA_CILK_CLAUSE_MASK;
28011 break;
28012 case 'n':
28013 if (!strcmp ("notinbranch", p))
28014 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
28015 else if (!strcmp ("nowait", p))
28016 result = PRAGMA_OMP_CLAUSE_NOWAIT;
28017 else if (flag_cilkplus && !strcmp ("nomask", p))
28018 result = PRAGMA_CILK_CLAUSE_NOMASK;
28019 else if (!strcmp ("num_teams", p))
28020 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
28021 else if (!strcmp ("num_threads", p))
28022 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
28023 break;
28024 case 'o':
28025 if (!strcmp ("ordered", p))
28026 result = PRAGMA_OMP_CLAUSE_ORDERED;
28027 break;
28028 case 'p':
28029 if (!strcmp ("parallel", p))
28030 result = PRAGMA_OMP_CLAUSE_PARALLEL;
28031 else if (!strcmp ("proc_bind", p))
28032 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
28033 break;
28034 case 'r':
28035 if (!strcmp ("reduction", p))
28036 result = PRAGMA_OMP_CLAUSE_REDUCTION;
28037 break;
28038 case 's':
28039 if (!strcmp ("safelen", p))
28040 result = PRAGMA_OMP_CLAUSE_SAFELEN;
28041 else if (!strcmp ("schedule", p))
28042 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
28043 else if (!strcmp ("sections", p))
28044 result = PRAGMA_OMP_CLAUSE_SECTIONS;
28045 else if (!strcmp ("shared", p))
28046 result = PRAGMA_OMP_CLAUSE_SHARED;
28047 else if (!strcmp ("simdlen", p))
28048 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
28049 break;
28050 case 't':
28051 if (!strcmp ("taskgroup", p))
28052 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
28053 else if (!strcmp ("thread_limit", p))
28054 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
28055 else if (!strcmp ("to", p))
28056 result = PRAGMA_OMP_CLAUSE_TO;
28057 break;
28058 case 'u':
28059 if (!strcmp ("uniform", p))
28060 result = PRAGMA_OMP_CLAUSE_UNIFORM;
28061 else if (!strcmp ("untied", p))
28062 result = PRAGMA_OMP_CLAUSE_UNTIED;
28063 break;
28064 case 'v':
28065 if (flag_cilkplus && !strcmp ("vectorlength", p))
28066 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
28067 break;
28071 if (result != PRAGMA_OMP_CLAUSE_NONE)
28072 cp_lexer_consume_token (parser->lexer);
28074 return result;
28077 /* Validate that a clause of the given type does not already exist. */
28079 static void
28080 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
28081 const char *name, location_t location)
28083 tree c;
28085 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
28086 if (OMP_CLAUSE_CODE (c) == code)
28088 error_at (location, "too many %qs clauses", name);
28089 break;
28093 /* OpenMP 2.5:
28094 variable-list:
28095 identifier
28096 variable-list , identifier
28098 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
28099 colon). An opening parenthesis will have been consumed by the caller.
28101 If KIND is nonzero, create the appropriate node and install the decl
28102 in OMP_CLAUSE_DECL and add the node to the head of the list.
28104 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
28105 return the list created.
28107 COLON can be NULL if only closing parenthesis should end the list,
28108 or pointer to bool which will receive false if the list is terminated
28109 by closing parenthesis or true if the list is terminated by colon. */
28111 static tree
28112 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
28113 tree list, bool *colon)
28115 cp_token *token;
28116 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28117 if (colon)
28119 parser->colon_corrects_to_scope_p = false;
28120 *colon = false;
28122 while (1)
28124 tree name, decl;
28126 token = cp_lexer_peek_token (parser->lexer);
28127 name = cp_parser_id_expression (parser, /*template_p=*/false,
28128 /*check_dependency_p=*/true,
28129 /*template_p=*/NULL,
28130 /*declarator_p=*/false,
28131 /*optional_p=*/false);
28132 if (name == error_mark_node)
28133 goto skip_comma;
28135 decl = cp_parser_lookup_name_simple (parser, name, token->location);
28136 if (decl == error_mark_node)
28137 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
28138 token->location);
28139 else if (kind != 0)
28141 switch (kind)
28143 case OMP_CLAUSE_MAP:
28144 case OMP_CLAUSE_FROM:
28145 case OMP_CLAUSE_TO:
28146 case OMP_CLAUSE_DEPEND:
28147 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28149 tree low_bound = NULL_TREE, length = NULL_TREE;
28151 parser->colon_corrects_to_scope_p = false;
28152 cp_lexer_consume_token (parser->lexer);
28153 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28154 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
28155 NULL);
28156 if (!colon)
28157 parser->colon_corrects_to_scope_p
28158 = saved_colon_corrects_to_scope_p;
28159 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
28160 length = integer_one_node;
28161 else
28163 /* Look for `:'. */
28164 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28165 goto skip_comma;
28166 if (!cp_lexer_next_token_is (parser->lexer,
28167 CPP_CLOSE_SQUARE))
28168 length = cp_parser_expression (parser,
28169 /*cast_p=*/false,
28170 NULL);
28172 /* Look for the closing `]'. */
28173 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
28174 RT_CLOSE_SQUARE))
28175 goto skip_comma;
28176 decl = tree_cons (low_bound, length, decl);
28178 break;
28179 default:
28180 break;
28183 tree u = build_omp_clause (token->location, kind);
28184 OMP_CLAUSE_DECL (u) = decl;
28185 OMP_CLAUSE_CHAIN (u) = list;
28186 list = u;
28188 else
28189 list = tree_cons (decl, NULL_TREE, list);
28191 get_comma:
28192 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28193 break;
28194 cp_lexer_consume_token (parser->lexer);
28197 if (colon)
28198 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28200 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28202 *colon = true;
28203 cp_parser_require (parser, CPP_COLON, RT_COLON);
28204 return list;
28207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28209 int ending;
28211 /* Try to resync to an unnested comma. Copied from
28212 cp_parser_parenthesized_expression_list. */
28213 skip_comma:
28214 if (colon)
28215 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28216 ending = cp_parser_skip_to_closing_parenthesis (parser,
28217 /*recovering=*/true,
28218 /*or_comma=*/true,
28219 /*consume_paren=*/true);
28220 if (ending < 0)
28221 goto get_comma;
28224 return list;
28227 /* Similarly, but expect leading and trailing parenthesis. This is a very
28228 common case for omp clauses. */
28230 static tree
28231 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
28233 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28234 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
28235 return list;
28238 /* OpenMP 3.0:
28239 collapse ( constant-expression ) */
28241 static tree
28242 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28244 tree c, num;
28245 location_t loc;
28246 HOST_WIDE_INT n;
28248 loc = cp_lexer_peek_token (parser->lexer)->location;
28249 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28250 return list;
28252 num = cp_parser_constant_expression (parser, false, NULL);
28254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28255 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28256 /*or_comma=*/false,
28257 /*consume_paren=*/true);
28259 if (num == error_mark_node)
28260 return list;
28261 num = fold_non_dependent_expr (num);
28262 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28263 || !tree_fits_shwi_p (num)
28264 || (n = tree_to_shwi (num)) <= 0
28265 || (int) n != n)
28267 error_at (loc, "collapse argument needs positive constant integer expression");
28268 return list;
28271 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28272 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28273 OMP_CLAUSE_CHAIN (c) = list;
28274 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28276 return c;
28279 /* OpenMP 2.5:
28280 default ( shared | none ) */
28282 static tree
28283 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28285 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28286 tree c;
28288 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28289 return list;
28290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28293 const char *p = IDENTIFIER_POINTER (id);
28295 switch (p[0])
28297 case 'n':
28298 if (strcmp ("none", p) != 0)
28299 goto invalid_kind;
28300 kind = OMP_CLAUSE_DEFAULT_NONE;
28301 break;
28303 case 's':
28304 if (strcmp ("shared", p) != 0)
28305 goto invalid_kind;
28306 kind = OMP_CLAUSE_DEFAULT_SHARED;
28307 break;
28309 default:
28310 goto invalid_kind;
28313 cp_lexer_consume_token (parser->lexer);
28315 else
28317 invalid_kind:
28318 cp_parser_error (parser, "expected %<none%> or %<shared%>");
28321 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28322 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28323 /*or_comma=*/false,
28324 /*consume_paren=*/true);
28326 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28327 return list;
28329 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28330 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28331 OMP_CLAUSE_CHAIN (c) = list;
28332 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28334 return c;
28337 /* OpenMP 3.1:
28338 final ( expression ) */
28340 static tree
28341 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28343 tree t, c;
28345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28346 return list;
28348 t = cp_parser_condition (parser);
28350 if (t == error_mark_node
28351 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28352 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28353 /*or_comma=*/false,
28354 /*consume_paren=*/true);
28356 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28358 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28359 OMP_CLAUSE_FINAL_EXPR (c) = t;
28360 OMP_CLAUSE_CHAIN (c) = list;
28362 return c;
28365 /* OpenMP 2.5:
28366 if ( expression ) */
28368 static tree
28369 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28371 tree t, c;
28373 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28374 return list;
28376 t = cp_parser_condition (parser);
28378 if (t == error_mark_node
28379 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28380 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28381 /*or_comma=*/false,
28382 /*consume_paren=*/true);
28384 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28386 c = build_omp_clause (location, OMP_CLAUSE_IF);
28387 OMP_CLAUSE_IF_EXPR (c) = t;
28388 OMP_CLAUSE_CHAIN (c) = list;
28390 return c;
28393 /* OpenMP 3.1:
28394 mergeable */
28396 static tree
28397 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28398 tree list, location_t location)
28400 tree c;
28402 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28403 location);
28405 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28406 OMP_CLAUSE_CHAIN (c) = list;
28407 return c;
28410 /* OpenMP 2.5:
28411 nowait */
28413 static tree
28414 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28415 tree list, location_t location)
28417 tree c;
28419 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28421 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28422 OMP_CLAUSE_CHAIN (c) = list;
28423 return c;
28426 /* OpenMP 2.5:
28427 num_threads ( expression ) */
28429 static tree
28430 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28431 location_t location)
28433 tree t, c;
28435 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28436 return list;
28438 t = cp_parser_expression (parser, false, NULL);
28440 if (t == error_mark_node
28441 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28442 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28443 /*or_comma=*/false,
28444 /*consume_paren=*/true);
28446 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28447 "num_threads", location);
28449 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28450 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28451 OMP_CLAUSE_CHAIN (c) = list;
28453 return c;
28456 /* OpenMP 2.5:
28457 ordered */
28459 static tree
28460 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28461 tree list, location_t location)
28463 tree c;
28465 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28466 "ordered", location);
28468 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28469 OMP_CLAUSE_CHAIN (c) = list;
28470 return c;
28473 /* OpenMP 2.5:
28474 reduction ( reduction-operator : variable-list )
28476 reduction-operator:
28477 One of: + * - & ^ | && ||
28479 OpenMP 3.1:
28481 reduction-operator:
28482 One of: + * - & ^ | && || min max
28484 OpenMP 4.0:
28486 reduction-operator:
28487 One of: + * - & ^ | && ||
28488 id-expression */
28490 static tree
28491 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28493 enum tree_code code = ERROR_MARK;
28494 tree nlist, c, id = NULL_TREE;
28496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28497 return list;
28499 switch (cp_lexer_peek_token (parser->lexer)->type)
28501 case CPP_PLUS: code = PLUS_EXPR; break;
28502 case CPP_MULT: code = MULT_EXPR; break;
28503 case CPP_MINUS: code = MINUS_EXPR; break;
28504 case CPP_AND: code = BIT_AND_EXPR; break;
28505 case CPP_XOR: code = BIT_XOR_EXPR; break;
28506 case CPP_OR: code = BIT_IOR_EXPR; break;
28507 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28508 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28509 default: break;
28512 if (code != ERROR_MARK)
28513 cp_lexer_consume_token (parser->lexer);
28514 else
28516 bool saved_colon_corrects_to_scope_p;
28517 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28518 parser->colon_corrects_to_scope_p = false;
28519 id = cp_parser_id_expression (parser, /*template_p=*/false,
28520 /*check_dependency_p=*/true,
28521 /*template_p=*/NULL,
28522 /*declarator_p=*/false,
28523 /*optional_p=*/false);
28524 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28525 if (identifier_p (id))
28527 const char *p = IDENTIFIER_POINTER (id);
28529 if (strcmp (p, "min") == 0)
28530 code = MIN_EXPR;
28531 else if (strcmp (p, "max") == 0)
28532 code = MAX_EXPR;
28533 else if (id == ansi_opname (PLUS_EXPR))
28534 code = PLUS_EXPR;
28535 else if (id == ansi_opname (MULT_EXPR))
28536 code = MULT_EXPR;
28537 else if (id == ansi_opname (MINUS_EXPR))
28538 code = MINUS_EXPR;
28539 else if (id == ansi_opname (BIT_AND_EXPR))
28540 code = BIT_AND_EXPR;
28541 else if (id == ansi_opname (BIT_IOR_EXPR))
28542 code = BIT_IOR_EXPR;
28543 else if (id == ansi_opname (BIT_XOR_EXPR))
28544 code = BIT_XOR_EXPR;
28545 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28546 code = TRUTH_ANDIF_EXPR;
28547 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28548 code = TRUTH_ORIF_EXPR;
28549 id = omp_reduction_id (code, id, NULL_TREE);
28550 tree scope = parser->scope;
28551 if (scope)
28552 id = build_qualified_name (NULL_TREE, scope, id, false);
28553 parser->scope = NULL_TREE;
28554 parser->qualifying_scope = NULL_TREE;
28555 parser->object_scope = NULL_TREE;
28557 else
28559 error ("invalid reduction-identifier");
28560 resync_fail:
28561 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28562 /*or_comma=*/false,
28563 /*consume_paren=*/true);
28564 return list;
28568 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28569 goto resync_fail;
28571 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28572 NULL);
28573 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28575 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28576 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28579 return nlist;
28582 /* OpenMP 2.5:
28583 schedule ( schedule-kind )
28584 schedule ( schedule-kind , expression )
28586 schedule-kind:
28587 static | dynamic | guided | runtime | auto */
28589 static tree
28590 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28592 tree c, t;
28594 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28595 return list;
28597 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28599 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28601 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28602 const char *p = IDENTIFIER_POINTER (id);
28604 switch (p[0])
28606 case 'd':
28607 if (strcmp ("dynamic", p) != 0)
28608 goto invalid_kind;
28609 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28610 break;
28612 case 'g':
28613 if (strcmp ("guided", p) != 0)
28614 goto invalid_kind;
28615 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28616 break;
28618 case 'r':
28619 if (strcmp ("runtime", p) != 0)
28620 goto invalid_kind;
28621 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28622 break;
28624 default:
28625 goto invalid_kind;
28628 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28629 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28630 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28631 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28632 else
28633 goto invalid_kind;
28634 cp_lexer_consume_token (parser->lexer);
28636 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28638 cp_token *token;
28639 cp_lexer_consume_token (parser->lexer);
28641 token = cp_lexer_peek_token (parser->lexer);
28642 t = cp_parser_assignment_expression (parser, false, NULL);
28644 if (t == error_mark_node)
28645 goto resync_fail;
28646 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28647 error_at (token->location, "schedule %<runtime%> does not take "
28648 "a %<chunk_size%> parameter");
28649 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28650 error_at (token->location, "schedule %<auto%> does not take "
28651 "a %<chunk_size%> parameter");
28652 else
28653 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28655 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28656 goto resync_fail;
28658 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28659 goto resync_fail;
28661 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28662 OMP_CLAUSE_CHAIN (c) = list;
28663 return c;
28665 invalid_kind:
28666 cp_parser_error (parser, "invalid schedule kind");
28667 resync_fail:
28668 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28669 /*or_comma=*/false,
28670 /*consume_paren=*/true);
28671 return list;
28674 /* OpenMP 3.0:
28675 untied */
28677 static tree
28678 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28679 tree list, location_t location)
28681 tree c;
28683 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28685 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28686 OMP_CLAUSE_CHAIN (c) = list;
28687 return c;
28690 /* OpenMP 4.0:
28691 inbranch
28692 notinbranch */
28694 static tree
28695 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28696 tree list, location_t location)
28698 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28699 tree c = build_omp_clause (location, code);
28700 OMP_CLAUSE_CHAIN (c) = list;
28701 return c;
28704 /* OpenMP 4.0:
28705 parallel
28707 sections
28708 taskgroup */
28710 static tree
28711 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28712 enum omp_clause_code code,
28713 tree list, location_t location)
28715 tree c = build_omp_clause (location, code);
28716 OMP_CLAUSE_CHAIN (c) = list;
28717 return c;
28720 /* OpenMP 4.0:
28721 num_teams ( expression ) */
28723 static tree
28724 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28725 location_t location)
28727 tree t, c;
28729 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28730 return list;
28732 t = cp_parser_expression (parser, false, NULL);
28734 if (t == error_mark_node
28735 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28736 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28737 /*or_comma=*/false,
28738 /*consume_paren=*/true);
28740 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28741 "num_teams", location);
28743 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28744 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28745 OMP_CLAUSE_CHAIN (c) = list;
28747 return c;
28750 /* OpenMP 4.0:
28751 thread_limit ( expression ) */
28753 static tree
28754 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28755 location_t location)
28757 tree t, c;
28759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28760 return list;
28762 t = cp_parser_expression (parser, false, NULL);
28764 if (t == error_mark_node
28765 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28766 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28767 /*or_comma=*/false,
28768 /*consume_paren=*/true);
28770 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28771 "thread_limit", location);
28773 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28774 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28775 OMP_CLAUSE_CHAIN (c) = list;
28777 return c;
28780 /* OpenMP 4.0:
28781 aligned ( variable-list )
28782 aligned ( variable-list : constant-expression ) */
28784 static tree
28785 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28787 tree nlist, c, alignment = NULL_TREE;
28788 bool colon;
28790 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28791 return list;
28793 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28794 &colon);
28796 if (colon)
28798 alignment = cp_parser_constant_expression (parser, false, NULL);
28800 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28801 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28802 /*or_comma=*/false,
28803 /*consume_paren=*/true);
28805 if (alignment == error_mark_node)
28806 alignment = NULL_TREE;
28809 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28810 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28812 return nlist;
28815 /* OpenMP 4.0:
28816 linear ( variable-list )
28817 linear ( variable-list : expression ) */
28819 static tree
28820 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28821 bool is_cilk_simd_fn)
28823 tree nlist, c, step = integer_one_node;
28824 bool colon;
28826 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28827 return list;
28829 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28830 &colon);
28832 if (colon)
28834 step = cp_parser_expression (parser, false, NULL);
28836 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28838 sorry ("using parameters for %<linear%> step is not supported yet");
28839 step = integer_one_node;
28841 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28843 /*or_comma=*/false,
28844 /*consume_paren=*/true);
28846 if (step == error_mark_node)
28847 return list;
28850 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28851 OMP_CLAUSE_LINEAR_STEP (c) = step;
28853 return nlist;
28856 /* OpenMP 4.0:
28857 safelen ( constant-expression ) */
28859 static tree
28860 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28861 location_t location)
28863 tree t, c;
28865 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28866 return list;
28868 t = cp_parser_constant_expression (parser, false, NULL);
28870 if (t == error_mark_node
28871 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28873 /*or_comma=*/false,
28874 /*consume_paren=*/true);
28876 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28878 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28879 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28880 OMP_CLAUSE_CHAIN (c) = list;
28882 return c;
28885 /* OpenMP 4.0:
28886 simdlen ( constant-expression ) */
28888 static tree
28889 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28890 location_t location)
28892 tree t, c;
28894 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28895 return list;
28897 t = cp_parser_constant_expression (parser, false, NULL);
28899 if (t == error_mark_node
28900 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28901 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28902 /*or_comma=*/false,
28903 /*consume_paren=*/true);
28905 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28907 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28908 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28909 OMP_CLAUSE_CHAIN (c) = list;
28911 return c;
28914 /* OpenMP 4.0:
28915 depend ( depend-kind : variable-list )
28917 depend-kind:
28918 in | out | inout */
28920 static tree
28921 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28923 tree nlist, c;
28924 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28926 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28927 return list;
28929 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28931 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28932 const char *p = IDENTIFIER_POINTER (id);
28934 if (strcmp ("in", p) == 0)
28935 kind = OMP_CLAUSE_DEPEND_IN;
28936 else if (strcmp ("inout", p) == 0)
28937 kind = OMP_CLAUSE_DEPEND_INOUT;
28938 else if (strcmp ("out", p) == 0)
28939 kind = OMP_CLAUSE_DEPEND_OUT;
28940 else
28941 goto invalid_kind;
28943 else
28944 goto invalid_kind;
28946 cp_lexer_consume_token (parser->lexer);
28947 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28948 goto resync_fail;
28950 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28951 NULL);
28953 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28954 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28956 return nlist;
28958 invalid_kind:
28959 cp_parser_error (parser, "invalid depend kind");
28960 resync_fail:
28961 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28962 /*or_comma=*/false,
28963 /*consume_paren=*/true);
28964 return list;
28967 /* OpenMP 4.0:
28968 map ( map-kind : variable-list )
28969 map ( variable-list )
28971 map-kind:
28972 alloc | to | from | tofrom */
28974 static tree
28975 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28977 tree nlist, c;
28978 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28980 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28981 return list;
28983 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28984 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28986 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28987 const char *p = IDENTIFIER_POINTER (id);
28989 if (strcmp ("alloc", p) == 0)
28990 kind = OMP_CLAUSE_MAP_ALLOC;
28991 else if (strcmp ("to", p) == 0)
28992 kind = OMP_CLAUSE_MAP_TO;
28993 else if (strcmp ("from", p) == 0)
28994 kind = OMP_CLAUSE_MAP_FROM;
28995 else if (strcmp ("tofrom", p) == 0)
28996 kind = OMP_CLAUSE_MAP_TOFROM;
28997 else
28999 cp_parser_error (parser, "invalid map kind");
29000 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29001 /*or_comma=*/false,
29002 /*consume_paren=*/true);
29003 return list;
29005 cp_lexer_consume_token (parser->lexer);
29006 cp_lexer_consume_token (parser->lexer);
29009 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29010 NULL);
29012 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29013 OMP_CLAUSE_MAP_KIND (c) = kind;
29015 return nlist;
29018 /* OpenMP 4.0:
29019 device ( expression ) */
29021 static tree
29022 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29023 location_t location)
29025 tree t, c;
29027 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29028 return list;
29030 t = cp_parser_expression (parser, false, NULL);
29032 if (t == error_mark_node
29033 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29034 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29035 /*or_comma=*/false,
29036 /*consume_paren=*/true);
29038 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29039 "device", location);
29041 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29042 OMP_CLAUSE_DEVICE_ID (c) = t;
29043 OMP_CLAUSE_CHAIN (c) = list;
29045 return c;
29048 /* OpenMP 4.0:
29049 dist_schedule ( static )
29050 dist_schedule ( static , expression ) */
29052 static tree
29053 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29054 location_t location)
29056 tree c, t;
29058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29059 return list;
29061 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29063 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29064 goto invalid_kind;
29065 cp_lexer_consume_token (parser->lexer);
29067 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29069 cp_lexer_consume_token (parser->lexer);
29071 t = cp_parser_assignment_expression (parser, false, NULL);
29073 if (t == error_mark_node)
29074 goto resync_fail;
29075 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29077 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29078 goto resync_fail;
29080 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29081 goto resync_fail;
29083 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29084 location);
29085 OMP_CLAUSE_CHAIN (c) = list;
29086 return c;
29088 invalid_kind:
29089 cp_parser_error (parser, "invalid dist_schedule kind");
29090 resync_fail:
29091 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29092 /*or_comma=*/false,
29093 /*consume_paren=*/true);
29094 return list;
29097 /* OpenMP 4.0:
29098 proc_bind ( proc-bind-kind )
29100 proc-bind-kind:
29101 master | close | spread */
29103 static tree
29104 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29105 location_t location)
29107 tree c;
29108 enum omp_clause_proc_bind_kind kind;
29110 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29111 return list;
29113 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29116 const char *p = IDENTIFIER_POINTER (id);
29118 if (strcmp ("master", p) == 0)
29119 kind = OMP_CLAUSE_PROC_BIND_MASTER;
29120 else if (strcmp ("close", p) == 0)
29121 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29122 else if (strcmp ("spread", p) == 0)
29123 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29124 else
29125 goto invalid_kind;
29127 else
29128 goto invalid_kind;
29130 cp_lexer_consume_token (parser->lexer);
29131 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29132 goto resync_fail;
29134 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29135 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29136 location);
29137 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29138 OMP_CLAUSE_CHAIN (c) = list;
29139 return c;
29141 invalid_kind:
29142 cp_parser_error (parser, "invalid depend kind");
29143 resync_fail:
29144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29145 /*or_comma=*/false,
29146 /*consume_paren=*/true);
29147 return list;
29150 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29151 is a bitmask in MASK. Return the list of clauses found; the result
29152 of clause default goes in *pdefault. */
29154 static tree
29155 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29156 const char *where, cp_token *pragma_tok,
29157 bool finish_p = true)
29159 tree clauses = NULL;
29160 bool first = true;
29161 cp_token *token = NULL;
29162 bool cilk_simd_fn = false;
29164 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29166 pragma_omp_clause c_kind;
29167 const char *c_name;
29168 tree prev = clauses;
29170 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29171 cp_lexer_consume_token (parser->lexer);
29173 token = cp_lexer_peek_token (parser->lexer);
29174 c_kind = cp_parser_omp_clause_name (parser);
29176 switch (c_kind)
29178 case PRAGMA_OMP_CLAUSE_COLLAPSE:
29179 clauses = cp_parser_omp_clause_collapse (parser, clauses,
29180 token->location);
29181 c_name = "collapse";
29182 break;
29183 case PRAGMA_OMP_CLAUSE_COPYIN:
29184 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29185 c_name = "copyin";
29186 break;
29187 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29188 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29189 clauses);
29190 c_name = "copyprivate";
29191 break;
29192 case PRAGMA_OMP_CLAUSE_DEFAULT:
29193 clauses = cp_parser_omp_clause_default (parser, clauses,
29194 token->location);
29195 c_name = "default";
29196 break;
29197 case PRAGMA_OMP_CLAUSE_FINAL:
29198 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29199 c_name = "final";
29200 break;
29201 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29203 clauses);
29204 c_name = "firstprivate";
29205 break;
29206 case PRAGMA_OMP_CLAUSE_IF:
29207 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29208 c_name = "if";
29209 break;
29210 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29211 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29212 clauses);
29213 c_name = "lastprivate";
29214 break;
29215 case PRAGMA_OMP_CLAUSE_MERGEABLE:
29216 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29217 token->location);
29218 c_name = "mergeable";
29219 break;
29220 case PRAGMA_OMP_CLAUSE_NOWAIT:
29221 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29222 c_name = "nowait";
29223 break;
29224 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29225 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29226 token->location);
29227 c_name = "num_threads";
29228 break;
29229 case PRAGMA_OMP_CLAUSE_ORDERED:
29230 clauses = cp_parser_omp_clause_ordered (parser, clauses,
29231 token->location);
29232 c_name = "ordered";
29233 break;
29234 case PRAGMA_OMP_CLAUSE_PRIVATE:
29235 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29236 clauses);
29237 c_name = "private";
29238 break;
29239 case PRAGMA_OMP_CLAUSE_REDUCTION:
29240 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29241 c_name = "reduction";
29242 break;
29243 case PRAGMA_OMP_CLAUSE_SCHEDULE:
29244 clauses = cp_parser_omp_clause_schedule (parser, clauses,
29245 token->location);
29246 c_name = "schedule";
29247 break;
29248 case PRAGMA_OMP_CLAUSE_SHARED:
29249 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29250 clauses);
29251 c_name = "shared";
29252 break;
29253 case PRAGMA_OMP_CLAUSE_UNTIED:
29254 clauses = cp_parser_omp_clause_untied (parser, clauses,
29255 token->location);
29256 c_name = "untied";
29257 break;
29258 case PRAGMA_OMP_CLAUSE_INBRANCH:
29259 case PRAGMA_CILK_CLAUSE_MASK:
29260 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29261 clauses, token->location);
29262 c_name = "inbranch";
29263 break;
29264 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29265 case PRAGMA_CILK_CLAUSE_NOMASK:
29266 clauses = cp_parser_omp_clause_branch (parser,
29267 OMP_CLAUSE_NOTINBRANCH,
29268 clauses, token->location);
29269 c_name = "notinbranch";
29270 break;
29271 case PRAGMA_OMP_CLAUSE_PARALLEL:
29272 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29273 clauses, token->location);
29274 c_name = "parallel";
29275 if (!first)
29277 clause_not_first:
29278 error_at (token->location, "%qs must be the first clause of %qs",
29279 c_name, where);
29280 clauses = prev;
29282 break;
29283 case PRAGMA_OMP_CLAUSE_FOR:
29284 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29285 clauses, token->location);
29286 c_name = "for";
29287 if (!first)
29288 goto clause_not_first;
29289 break;
29290 case PRAGMA_OMP_CLAUSE_SECTIONS:
29291 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29292 clauses, token->location);
29293 c_name = "sections";
29294 if (!first)
29295 goto clause_not_first;
29296 break;
29297 case PRAGMA_OMP_CLAUSE_TASKGROUP:
29298 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29299 clauses, token->location);
29300 c_name = "taskgroup";
29301 if (!first)
29302 goto clause_not_first;
29303 break;
29304 case PRAGMA_OMP_CLAUSE_TO:
29305 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29306 clauses);
29307 c_name = "to";
29308 break;
29309 case PRAGMA_OMP_CLAUSE_FROM:
29310 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29311 clauses);
29312 c_name = "from";
29313 break;
29314 case PRAGMA_OMP_CLAUSE_UNIFORM:
29315 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29316 clauses);
29317 c_name = "uniform";
29318 break;
29319 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29320 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29321 token->location);
29322 c_name = "num_teams";
29323 break;
29324 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29325 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29326 token->location);
29327 c_name = "thread_limit";
29328 break;
29329 case PRAGMA_OMP_CLAUSE_ALIGNED:
29330 clauses = cp_parser_omp_clause_aligned (parser, clauses);
29331 c_name = "aligned";
29332 break;
29333 case PRAGMA_OMP_CLAUSE_LINEAR:
29334 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29335 cilk_simd_fn = true;
29336 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29337 c_name = "linear";
29338 break;
29339 case PRAGMA_OMP_CLAUSE_DEPEND:
29340 clauses = cp_parser_omp_clause_depend (parser, clauses);
29341 c_name = "depend";
29342 break;
29343 case PRAGMA_OMP_CLAUSE_MAP:
29344 clauses = cp_parser_omp_clause_map (parser, clauses);
29345 c_name = "map";
29346 break;
29347 case PRAGMA_OMP_CLAUSE_DEVICE:
29348 clauses = cp_parser_omp_clause_device (parser, clauses,
29349 token->location);
29350 c_name = "device";
29351 break;
29352 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29353 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29354 token->location);
29355 c_name = "dist_schedule";
29356 break;
29357 case PRAGMA_OMP_CLAUSE_PROC_BIND:
29358 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29359 token->location);
29360 c_name = "proc_bind";
29361 break;
29362 case PRAGMA_OMP_CLAUSE_SAFELEN:
29363 clauses = cp_parser_omp_clause_safelen (parser, clauses,
29364 token->location);
29365 c_name = "safelen";
29366 break;
29367 case PRAGMA_OMP_CLAUSE_SIMDLEN:
29368 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29369 token->location);
29370 c_name = "simdlen";
29371 break;
29372 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29373 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29374 c_name = "simdlen";
29375 break;
29376 default:
29377 cp_parser_error (parser, "expected %<#pragma omp%> clause");
29378 goto saw_error;
29381 first = false;
29383 if (((mask >> c_kind) & 1) == 0)
29385 /* Remove the invalid clause(s) from the list to avoid
29386 confusing the rest of the compiler. */
29387 clauses = prev;
29388 error_at (token->location, "%qs is not valid for %qs", c_name, where);
29391 saw_error:
29392 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29393 no reason to skip to the end. */
29394 if (!(flag_cilkplus && pragma_tok == NULL))
29395 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29396 if (finish_p)
29397 return finish_omp_clauses (clauses);
29398 return clauses;
29401 /* OpenMP 2.5:
29402 structured-block:
29403 statement
29405 In practice, we're also interested in adding the statement to an
29406 outer node. So it is convenient if we work around the fact that
29407 cp_parser_statement calls add_stmt. */
29409 static unsigned
29410 cp_parser_begin_omp_structured_block (cp_parser *parser)
29412 unsigned save = parser->in_statement;
29414 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29415 This preserves the "not within loop or switch" style error messages
29416 for nonsense cases like
29417 void foo() {
29418 #pragma omp single
29419 break;
29422 if (parser->in_statement)
29423 parser->in_statement = IN_OMP_BLOCK;
29425 return save;
29428 static void
29429 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29431 parser->in_statement = save;
29434 static tree
29435 cp_parser_omp_structured_block (cp_parser *parser)
29437 tree stmt = begin_omp_structured_block ();
29438 unsigned int save = cp_parser_begin_omp_structured_block (parser);
29440 cp_parser_statement (parser, NULL_TREE, false, NULL);
29442 cp_parser_end_omp_structured_block (parser, save);
29443 return finish_omp_structured_block (stmt);
29446 /* OpenMP 2.5:
29447 # pragma omp atomic new-line
29448 expression-stmt
29450 expression-stmt:
29451 x binop= expr | x++ | ++x | x-- | --x
29452 binop:
29453 +, *, -, /, &, ^, |, <<, >>
29455 where x is an lvalue expression with scalar type.
29457 OpenMP 3.1:
29458 # pragma omp atomic new-line
29459 update-stmt
29461 # pragma omp atomic read new-line
29462 read-stmt
29464 # pragma omp atomic write new-line
29465 write-stmt
29467 # pragma omp atomic update new-line
29468 update-stmt
29470 # pragma omp atomic capture new-line
29471 capture-stmt
29473 # pragma omp atomic capture new-line
29474 capture-block
29476 read-stmt:
29477 v = x
29478 write-stmt:
29479 x = expr
29480 update-stmt:
29481 expression-stmt | x = x binop expr
29482 capture-stmt:
29483 v = expression-stmt
29484 capture-block:
29485 { v = x; update-stmt; } | { update-stmt; v = x; }
29487 OpenMP 4.0:
29488 update-stmt:
29489 expression-stmt | x = x binop expr | x = expr binop x
29490 capture-stmt:
29491 v = update-stmt
29492 capture-block:
29493 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29495 where x and v are lvalue expressions with scalar type. */
29497 static void
29498 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29500 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29501 tree rhs1 = NULL_TREE, orig_lhs;
29502 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29503 bool structured_block = false;
29504 bool seq_cst = false;
29506 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29508 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29509 const char *p = IDENTIFIER_POINTER (id);
29511 if (!strcmp (p, "seq_cst"))
29513 seq_cst = true;
29514 cp_lexer_consume_token (parser->lexer);
29515 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29516 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29517 cp_lexer_consume_token (parser->lexer);
29520 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29522 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29523 const char *p = IDENTIFIER_POINTER (id);
29525 if (!strcmp (p, "read"))
29526 code = OMP_ATOMIC_READ;
29527 else if (!strcmp (p, "write"))
29528 code = NOP_EXPR;
29529 else if (!strcmp (p, "update"))
29530 code = OMP_ATOMIC;
29531 else if (!strcmp (p, "capture"))
29532 code = OMP_ATOMIC_CAPTURE_NEW;
29533 else
29534 p = NULL;
29535 if (p)
29536 cp_lexer_consume_token (parser->lexer);
29538 if (!seq_cst)
29540 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29541 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29542 cp_lexer_consume_token (parser->lexer);
29544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29546 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29547 const char *p = IDENTIFIER_POINTER (id);
29549 if (!strcmp (p, "seq_cst"))
29551 seq_cst = true;
29552 cp_lexer_consume_token (parser->lexer);
29556 cp_parser_require_pragma_eol (parser, pragma_tok);
29558 switch (code)
29560 case OMP_ATOMIC_READ:
29561 case NOP_EXPR: /* atomic write */
29562 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29563 /*cast_p=*/false, NULL);
29564 if (v == error_mark_node)
29565 goto saw_error;
29566 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29567 goto saw_error;
29568 if (code == NOP_EXPR)
29569 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
29570 else
29571 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
29572 /*cast_p=*/false, NULL);
29573 if (lhs == error_mark_node)
29574 goto saw_error;
29575 if (code == NOP_EXPR)
29577 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29578 opcode. */
29579 code = OMP_ATOMIC;
29580 rhs = lhs;
29581 lhs = v;
29582 v = NULL_TREE;
29584 goto done;
29585 case OMP_ATOMIC_CAPTURE_NEW:
29586 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29588 cp_lexer_consume_token (parser->lexer);
29589 structured_block = true;
29591 else
29593 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29594 /*cast_p=*/false, NULL);
29595 if (v == error_mark_node)
29596 goto saw_error;
29597 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29598 goto saw_error;
29600 default:
29601 break;
29604 restart:
29605 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
29606 /*cast_p=*/false, NULL);
29607 orig_lhs = lhs;
29608 switch (TREE_CODE (lhs))
29610 case ERROR_MARK:
29611 goto saw_error;
29613 case POSTINCREMENT_EXPR:
29614 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29615 code = OMP_ATOMIC_CAPTURE_OLD;
29616 /* FALLTHROUGH */
29617 case PREINCREMENT_EXPR:
29618 lhs = TREE_OPERAND (lhs, 0);
29619 opcode = PLUS_EXPR;
29620 rhs = integer_one_node;
29621 break;
29623 case POSTDECREMENT_EXPR:
29624 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29625 code = OMP_ATOMIC_CAPTURE_OLD;
29626 /* FALLTHROUGH */
29627 case PREDECREMENT_EXPR:
29628 lhs = TREE_OPERAND (lhs, 0);
29629 opcode = MINUS_EXPR;
29630 rhs = integer_one_node;
29631 break;
29633 case COMPOUND_EXPR:
29634 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29635 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29636 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29637 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29638 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29639 (TREE_OPERAND (lhs, 1), 0), 0)))
29640 == BOOLEAN_TYPE)
29641 /* Undo effects of boolean_increment for post {in,de}crement. */
29642 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29643 /* FALLTHRU */
29644 case MODIFY_EXPR:
29645 if (TREE_CODE (lhs) == MODIFY_EXPR
29646 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29648 /* Undo effects of boolean_increment. */
29649 if (integer_onep (TREE_OPERAND (lhs, 1)))
29651 /* This is pre or post increment. */
29652 rhs = TREE_OPERAND (lhs, 1);
29653 lhs = TREE_OPERAND (lhs, 0);
29654 opcode = NOP_EXPR;
29655 if (code == OMP_ATOMIC_CAPTURE_NEW
29656 && !structured_block
29657 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29658 code = OMP_ATOMIC_CAPTURE_OLD;
29659 break;
29662 /* FALLTHRU */
29663 default:
29664 switch (cp_lexer_peek_token (parser->lexer)->type)
29666 case CPP_MULT_EQ:
29667 opcode = MULT_EXPR;
29668 break;
29669 case CPP_DIV_EQ:
29670 opcode = TRUNC_DIV_EXPR;
29671 break;
29672 case CPP_PLUS_EQ:
29673 opcode = PLUS_EXPR;
29674 break;
29675 case CPP_MINUS_EQ:
29676 opcode = MINUS_EXPR;
29677 break;
29678 case CPP_LSHIFT_EQ:
29679 opcode = LSHIFT_EXPR;
29680 break;
29681 case CPP_RSHIFT_EQ:
29682 opcode = RSHIFT_EXPR;
29683 break;
29684 case CPP_AND_EQ:
29685 opcode = BIT_AND_EXPR;
29686 break;
29687 case CPP_OR_EQ:
29688 opcode = BIT_IOR_EXPR;
29689 break;
29690 case CPP_XOR_EQ:
29691 opcode = BIT_XOR_EXPR;
29692 break;
29693 case CPP_EQ:
29694 enum cp_parser_prec oprec;
29695 cp_token *token;
29696 cp_lexer_consume_token (parser->lexer);
29697 cp_parser_parse_tentatively (parser);
29698 rhs1 = cp_parser_simple_cast_expression (parser);
29699 if (rhs1 == error_mark_node)
29701 cp_parser_abort_tentative_parse (parser);
29702 cp_parser_simple_cast_expression (parser);
29703 goto saw_error;
29705 token = cp_lexer_peek_token (parser->lexer);
29706 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29708 cp_parser_abort_tentative_parse (parser);
29709 cp_parser_parse_tentatively (parser);
29710 rhs = cp_parser_binary_expression (parser, false, true,
29711 PREC_NOT_OPERATOR, NULL);
29712 if (rhs == error_mark_node)
29714 cp_parser_abort_tentative_parse (parser);
29715 cp_parser_binary_expression (parser, false, true,
29716 PREC_NOT_OPERATOR, NULL);
29717 goto saw_error;
29719 switch (TREE_CODE (rhs))
29721 case MULT_EXPR:
29722 case TRUNC_DIV_EXPR:
29723 case PLUS_EXPR:
29724 case MINUS_EXPR:
29725 case LSHIFT_EXPR:
29726 case RSHIFT_EXPR:
29727 case BIT_AND_EXPR:
29728 case BIT_IOR_EXPR:
29729 case BIT_XOR_EXPR:
29730 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29732 if (cp_parser_parse_definitely (parser))
29734 opcode = TREE_CODE (rhs);
29735 rhs1 = TREE_OPERAND (rhs, 0);
29736 rhs = TREE_OPERAND (rhs, 1);
29737 goto stmt_done;
29739 else
29740 goto saw_error;
29742 break;
29743 default:
29744 break;
29746 cp_parser_abort_tentative_parse (parser);
29747 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29749 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
29750 if (rhs == error_mark_node)
29751 goto saw_error;
29752 opcode = NOP_EXPR;
29753 rhs1 = NULL_TREE;
29754 goto stmt_done;
29756 cp_parser_error (parser,
29757 "invalid form of %<#pragma omp atomic%>");
29758 goto saw_error;
29760 if (!cp_parser_parse_definitely (parser))
29761 goto saw_error;
29762 switch (token->type)
29764 case CPP_SEMICOLON:
29765 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29767 code = OMP_ATOMIC_CAPTURE_OLD;
29768 v = lhs;
29769 lhs = NULL_TREE;
29770 lhs1 = rhs1;
29771 rhs1 = NULL_TREE;
29772 cp_lexer_consume_token (parser->lexer);
29773 goto restart;
29775 else if (structured_block)
29777 opcode = NOP_EXPR;
29778 rhs = rhs1;
29779 rhs1 = NULL_TREE;
29780 goto stmt_done;
29782 cp_parser_error (parser,
29783 "invalid form of %<#pragma omp atomic%>");
29784 goto saw_error;
29785 case CPP_MULT:
29786 opcode = MULT_EXPR;
29787 break;
29788 case CPP_DIV:
29789 opcode = TRUNC_DIV_EXPR;
29790 break;
29791 case CPP_PLUS:
29792 opcode = PLUS_EXPR;
29793 break;
29794 case CPP_MINUS:
29795 opcode = MINUS_EXPR;
29796 break;
29797 case CPP_LSHIFT:
29798 opcode = LSHIFT_EXPR;
29799 break;
29800 case CPP_RSHIFT:
29801 opcode = RSHIFT_EXPR;
29802 break;
29803 case CPP_AND:
29804 opcode = BIT_AND_EXPR;
29805 break;
29806 case CPP_OR:
29807 opcode = BIT_IOR_EXPR;
29808 break;
29809 case CPP_XOR:
29810 opcode = BIT_XOR_EXPR;
29811 break;
29812 default:
29813 cp_parser_error (parser,
29814 "invalid operator for %<#pragma omp atomic%>");
29815 goto saw_error;
29817 oprec = TOKEN_PRECEDENCE (token);
29818 gcc_assert (oprec != PREC_NOT_OPERATOR);
29819 if (commutative_tree_code (opcode))
29820 oprec = (enum cp_parser_prec) (oprec - 1);
29821 cp_lexer_consume_token (parser->lexer);
29822 rhs = cp_parser_binary_expression (parser, false, false,
29823 oprec, NULL);
29824 if (rhs == error_mark_node)
29825 goto saw_error;
29826 goto stmt_done;
29827 /* FALLTHROUGH */
29828 default:
29829 cp_parser_error (parser,
29830 "invalid operator for %<#pragma omp atomic%>");
29831 goto saw_error;
29833 cp_lexer_consume_token (parser->lexer);
29835 rhs = cp_parser_expression (parser, false, NULL);
29836 if (rhs == error_mark_node)
29837 goto saw_error;
29838 break;
29840 stmt_done:
29841 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29843 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29844 goto saw_error;
29845 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29846 /*cast_p=*/false, NULL);
29847 if (v == error_mark_node)
29848 goto saw_error;
29849 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29850 goto saw_error;
29851 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29852 /*cast_p=*/false, NULL);
29853 if (lhs1 == error_mark_node)
29854 goto saw_error;
29856 if (structured_block)
29858 cp_parser_consume_semicolon_at_end_of_statement (parser);
29859 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29861 done:
29862 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29863 if (!structured_block)
29864 cp_parser_consume_semicolon_at_end_of_statement (parser);
29865 return;
29867 saw_error:
29868 cp_parser_skip_to_end_of_block_or_statement (parser);
29869 if (structured_block)
29871 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29872 cp_lexer_consume_token (parser->lexer);
29873 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29875 cp_parser_skip_to_end_of_block_or_statement (parser);
29876 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29877 cp_lexer_consume_token (parser->lexer);
29883 /* OpenMP 2.5:
29884 # pragma omp barrier new-line */
29886 static void
29887 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29889 cp_parser_require_pragma_eol (parser, pragma_tok);
29890 finish_omp_barrier ();
29893 /* OpenMP 2.5:
29894 # pragma omp critical [(name)] new-line
29895 structured-block */
29897 static tree
29898 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29900 tree stmt, name = NULL;
29902 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29904 cp_lexer_consume_token (parser->lexer);
29906 name = cp_parser_identifier (parser);
29908 if (name == error_mark_node
29909 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29910 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29911 /*or_comma=*/false,
29912 /*consume_paren=*/true);
29913 if (name == error_mark_node)
29914 name = NULL;
29916 cp_parser_require_pragma_eol (parser, pragma_tok);
29918 stmt = cp_parser_omp_structured_block (parser);
29919 return c_finish_omp_critical (input_location, stmt, name);
29922 /* OpenMP 2.5:
29923 # pragma omp flush flush-vars[opt] new-line
29925 flush-vars:
29926 ( variable-list ) */
29928 static void
29929 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29932 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29933 cp_parser_require_pragma_eol (parser, pragma_tok);
29935 finish_omp_flush ();
29938 /* Helper function, to parse omp for increment expression. */
29940 static tree
29941 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29943 tree cond = cp_parser_binary_expression (parser, false, true,
29944 PREC_NOT_OPERATOR, NULL);
29945 if (cond == error_mark_node
29946 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29948 cp_parser_skip_to_end_of_statement (parser);
29949 return error_mark_node;
29952 switch (TREE_CODE (cond))
29954 case GT_EXPR:
29955 case GE_EXPR:
29956 case LT_EXPR:
29957 case LE_EXPR:
29958 break;
29959 case NE_EXPR:
29960 if (code == CILK_SIMD)
29961 break;
29962 /* Fall through: OpenMP disallows NE_EXPR. */
29963 default:
29964 return error_mark_node;
29967 /* If decl is an iterator, preserve LHS and RHS of the relational
29968 expr until finish_omp_for. */
29969 if (decl
29970 && (type_dependent_expression_p (decl)
29971 || CLASS_TYPE_P (TREE_TYPE (decl))))
29972 return cond;
29974 return build_x_binary_op (input_location, TREE_CODE (cond),
29975 TREE_OPERAND (cond, 0), ERROR_MARK,
29976 TREE_OPERAND (cond, 1), ERROR_MARK,
29977 /*overload=*/NULL, tf_warning_or_error);
29980 /* Helper function, to parse omp for increment expression. */
29982 static tree
29983 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29985 cp_token *token = cp_lexer_peek_token (parser->lexer);
29986 enum tree_code op;
29987 tree lhs, rhs;
29988 cp_id_kind idk;
29989 bool decl_first;
29991 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29993 op = (token->type == CPP_PLUS_PLUS
29994 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29995 cp_lexer_consume_token (parser->lexer);
29996 lhs = cp_parser_simple_cast_expression (parser);
29997 if (lhs != decl)
29998 return error_mark_node;
29999 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30002 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30003 if (lhs != decl)
30004 return error_mark_node;
30006 token = cp_lexer_peek_token (parser->lexer);
30007 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30009 op = (token->type == CPP_PLUS_PLUS
30010 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30011 cp_lexer_consume_token (parser->lexer);
30012 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30015 op = cp_parser_assignment_operator_opt (parser);
30016 if (op == ERROR_MARK)
30017 return error_mark_node;
30019 if (op != NOP_EXPR)
30021 rhs = cp_parser_assignment_expression (parser, false, NULL);
30022 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30023 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30026 lhs = cp_parser_binary_expression (parser, false, false,
30027 PREC_ADDITIVE_EXPRESSION, NULL);
30028 token = cp_lexer_peek_token (parser->lexer);
30029 decl_first = lhs == decl;
30030 if (decl_first)
30031 lhs = NULL_TREE;
30032 if (token->type != CPP_PLUS
30033 && token->type != CPP_MINUS)
30034 return error_mark_node;
30038 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30039 cp_lexer_consume_token (parser->lexer);
30040 rhs = cp_parser_binary_expression (parser, false, false,
30041 PREC_ADDITIVE_EXPRESSION, NULL);
30042 token = cp_lexer_peek_token (parser->lexer);
30043 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30045 if (lhs == NULL_TREE)
30047 if (op == PLUS_EXPR)
30048 lhs = rhs;
30049 else
30050 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30051 tf_warning_or_error);
30053 else
30054 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30055 ERROR_MARK, NULL, tf_warning_or_error);
30058 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30060 if (!decl_first)
30062 if (rhs != decl || op == MINUS_EXPR)
30063 return error_mark_node;
30064 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30066 else
30067 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30069 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30072 /* Parse the initialization statement of either an OpenMP for loop or
30073 a Cilk Plus for loop.
30075 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
30076 Plus.
30078 Return true if the resulting construct should have an
30079 OMP_CLAUSE_PRIVATE added to it. */
30081 static bool
30082 cp_parser_omp_for_loop_init (cp_parser *parser,
30083 bool parsing_openmp,
30084 tree &this_pre_body,
30085 vec<tree, va_gc> *for_block,
30086 tree &init,
30087 tree &decl,
30088 tree &real_decl)
30090 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30091 return false;
30093 bool add_private_clause = false;
30095 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30097 init-expr:
30098 var = lb
30099 integer-type var = lb
30100 random-access-iterator-type var = lb
30101 pointer-type var = lb
30103 cp_decl_specifier_seq type_specifiers;
30105 /* First, try to parse as an initialized declaration. See
30106 cp_parser_condition, from whence the bulk of this is copied. */
30108 cp_parser_parse_tentatively (parser);
30109 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30110 /*is_trailing_return=*/false,
30111 &type_specifiers);
30112 if (cp_parser_parse_definitely (parser))
30114 /* If parsing a type specifier seq succeeded, then this
30115 MUST be a initialized declaration. */
30116 tree asm_specification, attributes;
30117 cp_declarator *declarator;
30119 declarator = cp_parser_declarator (parser,
30120 CP_PARSER_DECLARATOR_NAMED,
30121 /*ctor_dtor_or_conv_p=*/NULL,
30122 /*parenthesized_p=*/NULL,
30123 /*member_p=*/false,
30124 /*friend_p=*/false);
30125 attributes = cp_parser_attributes_opt (parser);
30126 asm_specification = cp_parser_asm_specification_opt (parser);
30128 if (declarator == cp_error_declarator)
30129 cp_parser_skip_to_end_of_statement (parser);
30131 else
30133 tree pushed_scope, auto_node;
30135 decl = start_decl (declarator, &type_specifiers,
30136 SD_INITIALIZED, attributes,
30137 /*prefix_attributes=*/NULL_TREE,
30138 &pushed_scope);
30140 auto_node = type_uses_auto (TREE_TYPE (decl));
30141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30143 if (cp_lexer_next_token_is (parser->lexer,
30144 CPP_OPEN_PAREN))
30146 if (parsing_openmp)
30147 error ("parenthesized initialization is not allowed in "
30148 "OpenMP %<for%> loop");
30149 else
30150 error ("parenthesized initialization is "
30151 "not allowed in for-loop");
30153 else
30154 /* Trigger an error. */
30155 cp_parser_require (parser, CPP_EQ, RT_EQ);
30157 init = error_mark_node;
30158 cp_parser_skip_to_end_of_statement (parser);
30160 else if (CLASS_TYPE_P (TREE_TYPE (decl))
30161 || type_dependent_expression_p (decl)
30162 || auto_node)
30164 bool is_direct_init, is_non_constant_init;
30166 init = cp_parser_initializer (parser,
30167 &is_direct_init,
30168 &is_non_constant_init);
30170 if (auto_node)
30172 TREE_TYPE (decl)
30173 = do_auto_deduction (TREE_TYPE (decl), init,
30174 auto_node);
30176 if (!CLASS_TYPE_P (TREE_TYPE (decl))
30177 && !type_dependent_expression_p (decl))
30178 goto non_class;
30181 cp_finish_decl (decl, init, !is_non_constant_init,
30182 asm_specification,
30183 LOOKUP_ONLYCONVERTING);
30184 if (CLASS_TYPE_P (TREE_TYPE (decl)))
30186 vec_safe_push (for_block, this_pre_body);
30187 init = NULL_TREE;
30189 else
30190 init = pop_stmt_list (this_pre_body);
30191 this_pre_body = NULL_TREE;
30193 else
30195 /* Consume '='. */
30196 cp_lexer_consume_token (parser->lexer);
30197 init = cp_parser_assignment_expression (parser, false, NULL);
30199 non_class:
30200 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30201 init = error_mark_node;
30202 else
30203 cp_finish_decl (decl, NULL_TREE,
30204 /*init_const_expr_p=*/false,
30205 asm_specification,
30206 LOOKUP_ONLYCONVERTING);
30209 if (pushed_scope)
30210 pop_scope (pushed_scope);
30213 else
30215 cp_id_kind idk;
30216 /* If parsing a type specifier sequence failed, then
30217 this MUST be a simple expression. */
30218 cp_parser_parse_tentatively (parser);
30219 decl = cp_parser_primary_expression (parser, false, false,
30220 false, &idk);
30221 if (!cp_parser_error_occurred (parser)
30222 && decl
30223 && DECL_P (decl)
30224 && CLASS_TYPE_P (TREE_TYPE (decl)))
30226 tree rhs;
30228 cp_parser_parse_definitely (parser);
30229 cp_parser_require (parser, CPP_EQ, RT_EQ);
30230 rhs = cp_parser_assignment_expression (parser, false, NULL);
30231 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30232 decl, NOP_EXPR,
30233 rhs,
30234 tf_warning_or_error));
30235 add_private_clause = true;
30237 else
30239 decl = NULL;
30240 cp_parser_abort_tentative_parse (parser);
30241 init = cp_parser_expression (parser, false, NULL);
30242 if (init)
30244 if (TREE_CODE (init) == MODIFY_EXPR
30245 || TREE_CODE (init) == MODOP_EXPR)
30246 real_decl = TREE_OPERAND (init, 0);
30250 return add_private_clause;
30253 /* Parse the restricted form of the for statement allowed by OpenMP. */
30255 static tree
30256 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30257 tree *cclauses)
30259 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30260 tree real_decl, initv, condv, incrv, declv;
30261 tree this_pre_body, cl;
30262 location_t loc_first;
30263 bool collapse_err = false;
30264 int i, collapse = 1, nbraces = 0;
30265 vec<tree, va_gc> *for_block = make_tree_vector ();
30267 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30268 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30269 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30271 gcc_assert (collapse >= 1);
30273 declv = make_tree_vec (collapse);
30274 initv = make_tree_vec (collapse);
30275 condv = make_tree_vec (collapse);
30276 incrv = make_tree_vec (collapse);
30278 loc_first = cp_lexer_peek_token (parser->lexer)->location;
30280 for (i = 0; i < collapse; i++)
30282 int bracecount = 0;
30283 bool add_private_clause = false;
30284 location_t loc;
30286 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30288 cp_parser_error (parser, "for statement expected");
30289 return NULL;
30291 loc = cp_lexer_consume_token (parser->lexer)->location;
30293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30294 return NULL;
30296 init = decl = real_decl = NULL;
30297 this_pre_body = push_stmt_list ();
30299 add_private_clause
30300 |= cp_parser_omp_for_loop_init (parser,
30301 /*parsing_openmp=*/code != CILK_SIMD,
30302 this_pre_body, for_block,
30303 init, decl, real_decl);
30305 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30306 if (this_pre_body)
30308 this_pre_body = pop_stmt_list (this_pre_body);
30309 if (pre_body)
30311 tree t = pre_body;
30312 pre_body = push_stmt_list ();
30313 add_stmt (t);
30314 add_stmt (this_pre_body);
30315 pre_body = pop_stmt_list (pre_body);
30317 else
30318 pre_body = this_pre_body;
30321 if (decl)
30322 real_decl = decl;
30323 if (cclauses != NULL
30324 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30325 && real_decl != NULL_TREE)
30327 tree *c;
30328 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30329 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30330 && OMP_CLAUSE_DECL (*c) == real_decl)
30332 error_at (loc, "iteration variable %qD"
30333 " should not be firstprivate", real_decl);
30334 *c = OMP_CLAUSE_CHAIN (*c);
30336 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30337 && OMP_CLAUSE_DECL (*c) == real_decl)
30339 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30340 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30341 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30342 OMP_CLAUSE_DECL (l) = real_decl;
30343 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30344 if (code == OMP_SIMD)
30346 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30347 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30349 else
30351 OMP_CLAUSE_CHAIN (l) = clauses;
30352 clauses = l;
30354 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30355 CP_OMP_CLAUSE_INFO (*c) = NULL;
30356 add_private_clause = false;
30358 else
30360 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30361 && OMP_CLAUSE_DECL (*c) == real_decl)
30362 add_private_clause = false;
30363 c = &OMP_CLAUSE_CHAIN (*c);
30367 if (add_private_clause)
30369 tree c;
30370 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30372 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30373 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30374 && OMP_CLAUSE_DECL (c) == decl)
30375 break;
30376 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30377 && OMP_CLAUSE_DECL (c) == decl)
30378 error_at (loc, "iteration variable %qD "
30379 "should not be firstprivate",
30380 decl);
30381 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30382 && OMP_CLAUSE_DECL (c) == decl)
30383 error_at (loc, "iteration variable %qD should not be reduction",
30384 decl);
30386 if (c == NULL)
30388 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30389 OMP_CLAUSE_DECL (c) = decl;
30390 c = finish_omp_clauses (c);
30391 if (c)
30393 OMP_CLAUSE_CHAIN (c) = clauses;
30394 clauses = c;
30399 cond = NULL;
30400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30401 cond = cp_parser_omp_for_cond (parser, decl, code);
30402 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30404 incr = NULL;
30405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30407 /* If decl is an iterator, preserve the operator on decl
30408 until finish_omp_for. */
30409 if (real_decl
30410 && ((processing_template_decl
30411 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30412 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30413 incr = cp_parser_omp_for_incr (parser, real_decl);
30414 else
30415 incr = cp_parser_expression (parser, false, NULL);
30416 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30417 SET_EXPR_LOCATION (incr, input_location);
30420 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30421 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30422 /*or_comma=*/false,
30423 /*consume_paren=*/true);
30425 TREE_VEC_ELT (declv, i) = decl;
30426 TREE_VEC_ELT (initv, i) = init;
30427 TREE_VEC_ELT (condv, i) = cond;
30428 TREE_VEC_ELT (incrv, i) = incr;
30430 if (i == collapse - 1)
30431 break;
30433 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30434 in between the collapsed for loops to be still considered perfectly
30435 nested. Hopefully the final version clarifies this.
30436 For now handle (multiple) {'s and empty statements. */
30437 cp_parser_parse_tentatively (parser);
30440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30441 break;
30442 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30444 cp_lexer_consume_token (parser->lexer);
30445 bracecount++;
30447 else if (bracecount
30448 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30449 cp_lexer_consume_token (parser->lexer);
30450 else
30452 loc = cp_lexer_peek_token (parser->lexer)->location;
30453 error_at (loc, "not enough collapsed for loops");
30454 collapse_err = true;
30455 cp_parser_abort_tentative_parse (parser);
30456 declv = NULL_TREE;
30457 break;
30460 while (1);
30462 if (declv)
30464 cp_parser_parse_definitely (parser);
30465 nbraces += bracecount;
30469 /* Note that we saved the original contents of this flag when we entered
30470 the structured block, and so we don't need to re-save it here. */
30471 if (code == CILK_SIMD)
30472 parser->in_statement = IN_CILK_SIMD_FOR;
30473 else
30474 parser->in_statement = IN_OMP_FOR;
30476 /* Note that the grammar doesn't call for a structured block here,
30477 though the loop as a whole is a structured block. */
30478 body = push_stmt_list ();
30479 cp_parser_statement (parser, NULL_TREE, false, NULL);
30480 body = pop_stmt_list (body);
30482 if (declv == NULL_TREE)
30483 ret = NULL_TREE;
30484 else
30485 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30486 pre_body, clauses);
30488 while (nbraces)
30490 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30492 cp_lexer_consume_token (parser->lexer);
30493 nbraces--;
30495 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30496 cp_lexer_consume_token (parser->lexer);
30497 else
30499 if (!collapse_err)
30501 error_at (cp_lexer_peek_token (parser->lexer)->location,
30502 "collapsed loops not perfectly nested");
30504 collapse_err = true;
30505 cp_parser_statement_seq_opt (parser, NULL);
30506 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30507 break;
30511 while (!for_block->is_empty ())
30512 add_stmt (pop_stmt_list (for_block->pop ()));
30513 release_tree_vector (for_block);
30515 return ret;
30518 /* Helper function for OpenMP parsing, split clauses and call
30519 finish_omp_clauses on each of the set of clauses afterwards. */
30521 static void
30522 cp_omp_split_clauses (location_t loc, enum tree_code code,
30523 omp_clause_mask mask, tree clauses, tree *cclauses)
30525 int i;
30526 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30527 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30528 if (cclauses[i])
30529 cclauses[i] = finish_omp_clauses (cclauses[i]);
30532 /* OpenMP 4.0:
30533 #pragma omp simd simd-clause[optseq] new-line
30534 for-loop */
30536 #define OMP_SIMD_CLAUSE_MASK \
30537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30545 static tree
30546 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30547 char *p_name, omp_clause_mask mask, tree *cclauses)
30549 tree clauses, sb, ret;
30550 unsigned int save;
30551 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30553 strcat (p_name, " simd");
30554 mask |= OMP_SIMD_CLAUSE_MASK;
30555 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30557 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30558 cclauses == NULL);
30559 if (cclauses)
30561 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30562 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30565 sb = begin_omp_structured_block ();
30566 save = cp_parser_begin_omp_structured_block (parser);
30568 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30570 cp_parser_end_omp_structured_block (parser, save);
30571 add_stmt (finish_omp_structured_block (sb));
30573 return ret;
30576 /* OpenMP 2.5:
30577 #pragma omp for for-clause[optseq] new-line
30578 for-loop
30580 OpenMP 4.0:
30581 #pragma omp for simd for-simd-clause[optseq] new-line
30582 for-loop */
30584 #define OMP_FOR_CLAUSE_MASK \
30585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30594 static tree
30595 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30596 char *p_name, omp_clause_mask mask, tree *cclauses)
30598 tree clauses, sb, ret;
30599 unsigned int save;
30600 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30602 strcat (p_name, " for");
30603 mask |= OMP_FOR_CLAUSE_MASK;
30604 if (cclauses)
30605 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30607 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30609 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30610 const char *p = IDENTIFIER_POINTER (id);
30612 if (strcmp (p, "simd") == 0)
30614 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30615 if (cclauses == NULL)
30616 cclauses = cclauses_buf;
30618 cp_lexer_consume_token (parser->lexer);
30619 if (!flag_openmp) /* flag_openmp_simd */
30620 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30621 cclauses);
30622 sb = begin_omp_structured_block ();
30623 save = cp_parser_begin_omp_structured_block (parser);
30624 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30625 cclauses);
30626 cp_parser_end_omp_structured_block (parser, save);
30627 tree body = finish_omp_structured_block (sb);
30628 if (ret == NULL)
30629 return ret;
30630 ret = make_node (OMP_FOR);
30631 TREE_TYPE (ret) = void_type_node;
30632 OMP_FOR_BODY (ret) = body;
30633 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30634 SET_EXPR_LOCATION (ret, loc);
30635 add_stmt (ret);
30636 return ret;
30639 if (!flag_openmp) /* flag_openmp_simd */
30641 cp_parser_require_pragma_eol (parser, pragma_tok);
30642 return NULL_TREE;
30645 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30646 cclauses == NULL);
30647 if (cclauses)
30649 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30650 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30653 sb = begin_omp_structured_block ();
30654 save = cp_parser_begin_omp_structured_block (parser);
30656 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30658 cp_parser_end_omp_structured_block (parser, save);
30659 add_stmt (finish_omp_structured_block (sb));
30661 return ret;
30664 /* OpenMP 2.5:
30665 # pragma omp master new-line
30666 structured-block */
30668 static tree
30669 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30671 cp_parser_require_pragma_eol (parser, pragma_tok);
30672 return c_finish_omp_master (input_location,
30673 cp_parser_omp_structured_block (parser));
30676 /* OpenMP 2.5:
30677 # pragma omp ordered new-line
30678 structured-block */
30680 static tree
30681 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30683 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30684 cp_parser_require_pragma_eol (parser, pragma_tok);
30685 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30688 /* OpenMP 2.5:
30690 section-scope:
30691 { section-sequence }
30693 section-sequence:
30694 section-directive[opt] structured-block
30695 section-sequence section-directive structured-block */
30697 static tree
30698 cp_parser_omp_sections_scope (cp_parser *parser)
30700 tree stmt, substmt;
30701 bool error_suppress = false;
30702 cp_token *tok;
30704 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30705 return NULL_TREE;
30707 stmt = push_stmt_list ();
30709 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30711 substmt = cp_parser_omp_structured_block (parser);
30712 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30713 add_stmt (substmt);
30716 while (1)
30718 tok = cp_lexer_peek_token (parser->lexer);
30719 if (tok->type == CPP_CLOSE_BRACE)
30720 break;
30721 if (tok->type == CPP_EOF)
30722 break;
30724 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30726 cp_lexer_consume_token (parser->lexer);
30727 cp_parser_require_pragma_eol (parser, tok);
30728 error_suppress = false;
30730 else if (!error_suppress)
30732 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30733 error_suppress = true;
30736 substmt = cp_parser_omp_structured_block (parser);
30737 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30738 add_stmt (substmt);
30740 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30742 substmt = pop_stmt_list (stmt);
30744 stmt = make_node (OMP_SECTIONS);
30745 TREE_TYPE (stmt) = void_type_node;
30746 OMP_SECTIONS_BODY (stmt) = substmt;
30748 add_stmt (stmt);
30749 return stmt;
30752 /* OpenMP 2.5:
30753 # pragma omp sections sections-clause[optseq] newline
30754 sections-scope */
30756 #define OMP_SECTIONS_CLAUSE_MASK \
30757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30763 static tree
30764 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30765 char *p_name, omp_clause_mask mask, tree *cclauses)
30767 tree clauses, ret;
30768 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30770 strcat (p_name, " sections");
30771 mask |= OMP_SECTIONS_CLAUSE_MASK;
30772 if (cclauses)
30773 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30775 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30776 cclauses == NULL);
30777 if (cclauses)
30779 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30780 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30783 ret = cp_parser_omp_sections_scope (parser);
30784 if (ret)
30785 OMP_SECTIONS_CLAUSES (ret) = clauses;
30787 return ret;
30790 /* OpenMP 2.5:
30791 # pragma omp parallel parallel-clause[optseq] new-line
30792 structured-block
30793 # pragma omp parallel for parallel-for-clause[optseq] new-line
30794 structured-block
30795 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30796 structured-block
30798 OpenMP 4.0:
30799 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30800 structured-block */
30802 #define OMP_PARALLEL_CLAUSE_MASK \
30803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30813 static tree
30814 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30815 char *p_name, omp_clause_mask mask, tree *cclauses)
30817 tree stmt, clauses, block;
30818 unsigned int save;
30819 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30821 strcat (p_name, " parallel");
30822 mask |= OMP_PARALLEL_CLAUSE_MASK;
30824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30826 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30827 if (cclauses == NULL)
30828 cclauses = cclauses_buf;
30830 cp_lexer_consume_token (parser->lexer);
30831 if (!flag_openmp) /* flag_openmp_simd */
30832 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30833 block = begin_omp_parallel ();
30834 save = cp_parser_begin_omp_structured_block (parser);
30835 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30836 cp_parser_end_omp_structured_block (parser, save);
30837 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30838 block);
30839 if (ret == NULL_TREE)
30840 return ret;
30841 OMP_PARALLEL_COMBINED (stmt) = 1;
30842 return stmt;
30844 else if (cclauses)
30846 error_at (loc, "expected %<for%> after %qs", p_name);
30847 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30848 return NULL_TREE;
30850 else if (!flag_openmp) /* flag_openmp_simd */
30852 cp_parser_require_pragma_eol (parser, pragma_tok);
30853 return NULL_TREE;
30855 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30857 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30858 const char *p = IDENTIFIER_POINTER (id);
30859 if (strcmp (p, "sections") == 0)
30861 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30862 cclauses = cclauses_buf;
30864 cp_lexer_consume_token (parser->lexer);
30865 block = begin_omp_parallel ();
30866 save = cp_parser_begin_omp_structured_block (parser);
30867 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30868 cp_parser_end_omp_structured_block (parser, save);
30869 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30870 block);
30871 OMP_PARALLEL_COMBINED (stmt) = 1;
30872 return stmt;
30876 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30878 block = begin_omp_parallel ();
30879 save = cp_parser_begin_omp_structured_block (parser);
30880 cp_parser_statement (parser, NULL_TREE, false, NULL);
30881 cp_parser_end_omp_structured_block (parser, save);
30882 stmt = finish_omp_parallel (clauses, block);
30883 return stmt;
30886 /* OpenMP 2.5:
30887 # pragma omp single single-clause[optseq] new-line
30888 structured-block */
30890 #define OMP_SINGLE_CLAUSE_MASK \
30891 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30896 static tree
30897 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30899 tree stmt = make_node (OMP_SINGLE);
30900 TREE_TYPE (stmt) = void_type_node;
30902 OMP_SINGLE_CLAUSES (stmt)
30903 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30904 "#pragma omp single", pragma_tok);
30905 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30907 return add_stmt (stmt);
30910 /* OpenMP 3.0:
30911 # pragma omp task task-clause[optseq] new-line
30912 structured-block */
30914 #define OMP_TASK_CLAUSE_MASK \
30915 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30925 static tree
30926 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30928 tree clauses, block;
30929 unsigned int save;
30931 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30932 "#pragma omp task", pragma_tok);
30933 block = begin_omp_task ();
30934 save = cp_parser_begin_omp_structured_block (parser);
30935 cp_parser_statement (parser, NULL_TREE, false, NULL);
30936 cp_parser_end_omp_structured_block (parser, save);
30937 return finish_omp_task (clauses, block);
30940 /* OpenMP 3.0:
30941 # pragma omp taskwait new-line */
30943 static void
30944 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30946 cp_parser_require_pragma_eol (parser, pragma_tok);
30947 finish_omp_taskwait ();
30950 /* OpenMP 3.1:
30951 # pragma omp taskyield new-line */
30953 static void
30954 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30956 cp_parser_require_pragma_eol (parser, pragma_tok);
30957 finish_omp_taskyield ();
30960 /* OpenMP 4.0:
30961 # pragma omp taskgroup new-line
30962 structured-block */
30964 static tree
30965 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30967 cp_parser_require_pragma_eol (parser, pragma_tok);
30968 return c_finish_omp_taskgroup (input_location,
30969 cp_parser_omp_structured_block (parser));
30973 /* OpenMP 2.5:
30974 # pragma omp threadprivate (variable-list) */
30976 static void
30977 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30979 tree vars;
30981 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30982 cp_parser_require_pragma_eol (parser, pragma_tok);
30984 finish_omp_threadprivate (vars);
30987 /* OpenMP 4.0:
30988 # pragma omp cancel cancel-clause[optseq] new-line */
30990 #define OMP_CANCEL_CLAUSE_MASK \
30991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30997 static void
30998 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31000 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31001 "#pragma omp cancel", pragma_tok);
31002 finish_omp_cancel (clauses);
31005 /* OpenMP 4.0:
31006 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31008 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31014 static void
31015 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31017 tree clauses;
31018 bool point_seen = false;
31020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31022 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31023 const char *p = IDENTIFIER_POINTER (id);
31025 if (strcmp (p, "point") == 0)
31027 cp_lexer_consume_token (parser->lexer);
31028 point_seen = true;
31031 if (!point_seen)
31033 cp_parser_error (parser, "expected %<point%>");
31034 cp_parser_require_pragma_eol (parser, pragma_tok);
31035 return;
31038 clauses = cp_parser_omp_all_clauses (parser,
31039 OMP_CANCELLATION_POINT_CLAUSE_MASK,
31040 "#pragma omp cancellation point",
31041 pragma_tok);
31042 finish_omp_cancellation_point (clauses);
31045 /* OpenMP 4.0:
31046 #pragma omp distribute distribute-clause[optseq] new-line
31047 for-loop */
31049 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31055 static tree
31056 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31057 char *p_name, omp_clause_mask mask, tree *cclauses)
31059 tree clauses, sb, ret;
31060 unsigned int save;
31061 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31063 strcat (p_name, " distribute");
31064 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31068 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31069 const char *p = IDENTIFIER_POINTER (id);
31070 bool simd = false;
31071 bool parallel = false;
31073 if (strcmp (p, "simd") == 0)
31074 simd = true;
31075 else
31076 parallel = strcmp (p, "parallel") == 0;
31077 if (parallel || simd)
31079 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31080 if (cclauses == NULL)
31081 cclauses = cclauses_buf;
31082 cp_lexer_consume_token (parser->lexer);
31083 if (!flag_openmp) /* flag_openmp_simd */
31085 if (simd)
31086 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31087 cclauses);
31088 else
31089 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31090 cclauses);
31092 sb = begin_omp_structured_block ();
31093 save = cp_parser_begin_omp_structured_block (parser);
31094 if (simd)
31095 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31096 cclauses);
31097 else
31098 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31099 cclauses);
31100 cp_parser_end_omp_structured_block (parser, save);
31101 tree body = finish_omp_structured_block (sb);
31102 if (ret == NULL)
31103 return ret;
31104 ret = make_node (OMP_DISTRIBUTE);
31105 TREE_TYPE (ret) = void_type_node;
31106 OMP_FOR_BODY (ret) = body;
31107 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31108 SET_EXPR_LOCATION (ret, loc);
31109 add_stmt (ret);
31110 return ret;
31113 if (!flag_openmp) /* flag_openmp_simd */
31115 cp_parser_require_pragma_eol (parser, pragma_tok);
31116 return NULL_TREE;
31119 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31120 cclauses == NULL);
31121 if (cclauses)
31123 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31124 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31127 sb = begin_omp_structured_block ();
31128 save = cp_parser_begin_omp_structured_block (parser);
31130 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31132 cp_parser_end_omp_structured_block (parser, save);
31133 add_stmt (finish_omp_structured_block (sb));
31135 return ret;
31138 /* OpenMP 4.0:
31139 # pragma omp teams teams-clause[optseq] new-line
31140 structured-block */
31142 #define OMP_TEAMS_CLAUSE_MASK \
31143 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31151 static tree
31152 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31153 char *p_name, omp_clause_mask mask, tree *cclauses)
31155 tree clauses, sb, ret;
31156 unsigned int save;
31157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31159 strcat (p_name, " teams");
31160 mask |= OMP_TEAMS_CLAUSE_MASK;
31162 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31164 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31165 const char *p = IDENTIFIER_POINTER (id);
31166 if (strcmp (p, "distribute") == 0)
31168 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31169 if (cclauses == NULL)
31170 cclauses = cclauses_buf;
31172 cp_lexer_consume_token (parser->lexer);
31173 if (!flag_openmp) /* flag_openmp_simd */
31174 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31175 cclauses);
31176 sb = begin_omp_structured_block ();
31177 save = cp_parser_begin_omp_structured_block (parser);
31178 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31179 cclauses);
31180 cp_parser_end_omp_structured_block (parser, save);
31181 tree body = finish_omp_structured_block (sb);
31182 if (ret == NULL)
31183 return ret;
31184 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31185 ret = make_node (OMP_TEAMS);
31186 TREE_TYPE (ret) = void_type_node;
31187 OMP_TEAMS_CLAUSES (ret) = clauses;
31188 OMP_TEAMS_BODY (ret) = body;
31189 return add_stmt (ret);
31192 if (!flag_openmp) /* flag_openmp_simd */
31194 cp_parser_require_pragma_eol (parser, pragma_tok);
31195 return NULL_TREE;
31198 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31199 cclauses == NULL);
31200 if (cclauses)
31202 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31203 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31206 tree stmt = make_node (OMP_TEAMS);
31207 TREE_TYPE (stmt) = void_type_node;
31208 OMP_TEAMS_CLAUSES (stmt) = clauses;
31209 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31211 return add_stmt (stmt);
31214 /* OpenMP 4.0:
31215 # pragma omp target data target-data-clause[optseq] new-line
31216 structured-block */
31218 #define OMP_TARGET_DATA_CLAUSE_MASK \
31219 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31223 static tree
31224 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31226 tree stmt = make_node (OMP_TARGET_DATA);
31227 TREE_TYPE (stmt) = void_type_node;
31229 OMP_TARGET_DATA_CLAUSES (stmt)
31230 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31231 "#pragma omp target data", pragma_tok);
31232 keep_next_level (true);
31233 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31235 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31236 return add_stmt (stmt);
31239 /* OpenMP 4.0:
31240 # pragma omp target update target-update-clause[optseq] new-line */
31242 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31243 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31248 static bool
31249 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31250 enum pragma_context context)
31252 if (context == pragma_stmt)
31254 error_at (pragma_tok->location,
31255 "%<#pragma omp target update%> may only be "
31256 "used in compound statements");
31257 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31258 return false;
31261 tree clauses
31262 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31263 "#pragma omp target update", pragma_tok);
31264 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31265 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31267 error_at (pragma_tok->location,
31268 "%<#pragma omp target update must contain at least one "
31269 "%<from%> or %<to%> clauses");
31270 return false;
31273 tree stmt = make_node (OMP_TARGET_UPDATE);
31274 TREE_TYPE (stmt) = void_type_node;
31275 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31276 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31277 add_stmt (stmt);
31278 return false;
31281 /* OpenMP 4.0:
31282 # pragma omp target target-clause[optseq] new-line
31283 structured-block */
31285 #define OMP_TARGET_CLAUSE_MASK \
31286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31290 static bool
31291 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31292 enum pragma_context context)
31294 if (context != pragma_stmt && context != pragma_compound)
31296 cp_parser_error (parser, "expected declaration specifiers");
31297 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31298 return false;
31301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31303 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31304 const char *p = IDENTIFIER_POINTER (id);
31306 if (strcmp (p, "teams") == 0)
31308 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31309 char p_name[sizeof ("#pragma omp target teams distribute "
31310 "parallel for simd")];
31312 cp_lexer_consume_token (parser->lexer);
31313 strcpy (p_name, "#pragma omp target");
31314 if (!flag_openmp) /* flag_openmp_simd */
31316 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31317 OMP_TARGET_CLAUSE_MASK,
31318 cclauses);
31319 return stmt != NULL_TREE;
31321 keep_next_level (true);
31322 tree sb = begin_omp_structured_block ();
31323 unsigned save = cp_parser_begin_omp_structured_block (parser);
31324 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31325 OMP_TARGET_CLAUSE_MASK, cclauses);
31326 cp_parser_end_omp_structured_block (parser, save);
31327 tree body = finish_omp_structured_block (sb);
31328 if (ret == NULL_TREE)
31329 return false;
31330 tree stmt = make_node (OMP_TARGET);
31331 TREE_TYPE (stmt) = void_type_node;
31332 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31333 OMP_TARGET_BODY (stmt) = body;
31334 add_stmt (stmt);
31335 return true;
31337 else if (!flag_openmp) /* flag_openmp_simd */
31339 cp_parser_require_pragma_eol (parser, pragma_tok);
31340 return false;
31342 else if (strcmp (p, "data") == 0)
31344 cp_lexer_consume_token (parser->lexer);
31345 cp_parser_omp_target_data (parser, pragma_tok);
31346 return true;
31348 else if (strcmp (p, "update") == 0)
31350 cp_lexer_consume_token (parser->lexer);
31351 return cp_parser_omp_target_update (parser, pragma_tok, context);
31355 tree stmt = make_node (OMP_TARGET);
31356 TREE_TYPE (stmt) = void_type_node;
31358 OMP_TARGET_CLAUSES (stmt)
31359 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31360 "#pragma omp target", pragma_tok);
31361 keep_next_level (true);
31362 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31364 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31365 add_stmt (stmt);
31366 return true;
31369 /* OpenMP 4.0:
31370 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31372 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31373 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31380 static void
31381 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31382 enum pragma_context context)
31384 bool first_p = parser->omp_declare_simd == NULL;
31385 cp_omp_declare_simd_data data;
31386 if (first_p)
31388 data.error_seen = false;
31389 data.fndecl_seen = false;
31390 data.tokens = vNULL;
31391 parser->omp_declare_simd = &data;
31393 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31394 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31395 cp_lexer_consume_token (parser->lexer);
31396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31397 parser->omp_declare_simd->error_seen = true;
31398 cp_parser_require_pragma_eol (parser, pragma_tok);
31399 struct cp_token_cache *cp
31400 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31401 parser->omp_declare_simd->tokens.safe_push (cp);
31402 if (first_p)
31404 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31405 cp_parser_pragma (parser, context);
31406 switch (context)
31408 case pragma_external:
31409 cp_parser_declaration (parser);
31410 break;
31411 case pragma_member:
31412 cp_parser_member_declaration (parser);
31413 break;
31414 case pragma_objc_icode:
31415 cp_parser_block_declaration (parser, /*statement_p=*/false);
31416 break;
31417 default:
31418 cp_parser_declaration_statement (parser);
31419 break;
31421 if (parser->omp_declare_simd
31422 && !parser->omp_declare_simd->error_seen
31423 && !parser->omp_declare_simd->fndecl_seen)
31424 error_at (pragma_tok->location,
31425 "%<#pragma omp declare simd%> not immediately followed by "
31426 "function declaration or definition");
31427 data.tokens.release ();
31428 parser->omp_declare_simd = NULL;
31432 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31433 This function is modelled similar to the late parsing of omp declare
31434 simd. */
31436 static tree
31437 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31439 struct cp_token_cache *ce;
31440 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31441 int ii = 0;
31443 if (parser->omp_declare_simd != NULL)
31445 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31446 " marked as a Cilk Plus SIMD-enabled function");
31447 XDELETE (parser->cilk_simd_fn_info);
31448 parser->cilk_simd_fn_info = NULL;
31449 return attrs;
31451 if (!info->error_seen && info->fndecl_seen)
31453 error ("vector attribute not immediately followed by a single function"
31454 " declaration or definition");
31455 info->error_seen = true;
31457 if (info->error_seen)
31458 return attrs;
31460 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31462 tree c, cl;
31464 cp_parser_push_lexer_for_tokens (parser, ce);
31465 parser->lexer->in_pragma = true;
31466 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31467 "SIMD-enabled functions attribute",
31468 NULL);
31469 cp_parser_pop_lexer (parser);
31470 if (cl)
31471 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31473 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31474 TREE_CHAIN (c) = attrs;
31475 attrs = c;
31477 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31478 TREE_CHAIN (c) = attrs;
31479 if (processing_template_decl)
31480 ATTR_IS_DEPENDENT (c) = 1;
31481 attrs = c;
31483 info->fndecl_seen = true;
31484 XDELETE (parser->cilk_simd_fn_info);
31485 parser->cilk_simd_fn_info = NULL;
31486 return attrs;
31489 /* Finalize #pragma omp declare simd clauses after direct declarator has
31490 been parsed, and put that into "omp declare simd" attribute. */
31492 static tree
31493 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31495 struct cp_token_cache *ce;
31496 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31497 int i;
31499 if (!data->error_seen && data->fndecl_seen)
31501 error ("%<#pragma omp declare simd%> not immediately followed by "
31502 "a single function declaration or definition");
31503 data->error_seen = true;
31504 return attrs;
31506 if (data->error_seen)
31507 return attrs;
31509 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31511 tree c, cl;
31513 cp_parser_push_lexer_for_tokens (parser, ce);
31514 parser->lexer->in_pragma = true;
31515 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31516 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31517 cp_lexer_consume_token (parser->lexer);
31518 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31519 "#pragma omp declare simd", pragma_tok);
31520 cp_parser_pop_lexer (parser);
31521 if (cl)
31522 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31523 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31524 TREE_CHAIN (c) = attrs;
31525 if (processing_template_decl)
31526 ATTR_IS_DEPENDENT (c) = 1;
31527 attrs = c;
31530 data->fndecl_seen = true;
31531 return attrs;
31535 /* OpenMP 4.0:
31536 # pragma omp declare target new-line
31537 declarations and definitions
31538 # pragma omp end declare target new-line */
31540 static void
31541 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31543 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31544 scope_chain->omp_declare_target_attribute++;
31547 static void
31548 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31550 const char *p = "";
31551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31554 p = IDENTIFIER_POINTER (id);
31556 if (strcmp (p, "declare") == 0)
31558 cp_lexer_consume_token (parser->lexer);
31559 p = "";
31560 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31562 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31563 p = IDENTIFIER_POINTER (id);
31565 if (strcmp (p, "target") == 0)
31566 cp_lexer_consume_token (parser->lexer);
31567 else
31569 cp_parser_error (parser, "expected %<target%>");
31570 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31571 return;
31574 else
31576 cp_parser_error (parser, "expected %<declare%>");
31577 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31578 return;
31580 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31581 if (!scope_chain->omp_declare_target_attribute)
31582 error_at (pragma_tok->location,
31583 "%<#pragma omp end declare target%> without corresponding "
31584 "%<#pragma omp declare target%>");
31585 else
31586 scope_chain->omp_declare_target_attribute--;
31589 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31590 expression and optional initializer clause of
31591 #pragma omp declare reduction. We store the expression(s) as
31592 either 3, 6 or 7 special statements inside of the artificial function's
31593 body. The first two statements are DECL_EXPRs for the artificial
31594 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31595 expression that uses those variables.
31596 If there was any INITIALIZER clause, this is followed by further statements,
31597 the fourth and fifth statements are DECL_EXPRs for the artificial
31598 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31599 constructor variant (first token after open paren is not omp_priv),
31600 then the sixth statement is a statement with the function call expression
31601 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31602 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31603 to initialize the OMP_PRIV artificial variable and there is seventh
31604 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31606 static bool
31607 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31609 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31610 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31611 type = TREE_TYPE (type);
31612 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31613 DECL_ARTIFICIAL (omp_out) = 1;
31614 pushdecl (omp_out);
31615 add_decl_expr (omp_out);
31616 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31617 DECL_ARTIFICIAL (omp_in) = 1;
31618 pushdecl (omp_in);
31619 add_decl_expr (omp_in);
31620 tree combiner;
31621 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31623 keep_next_level (true);
31624 tree block = begin_omp_structured_block ();
31625 combiner = cp_parser_expression (parser, false, NULL);
31626 finish_expr_stmt (combiner);
31627 block = finish_omp_structured_block (block);
31628 add_stmt (block);
31630 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31631 return false;
31633 const char *p = "";
31634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31636 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31637 p = IDENTIFIER_POINTER (id);
31640 if (strcmp (p, "initializer") == 0)
31642 cp_lexer_consume_token (parser->lexer);
31643 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31644 return false;
31646 p = "";
31647 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31649 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31650 p = IDENTIFIER_POINTER (id);
31653 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31654 DECL_ARTIFICIAL (omp_priv) = 1;
31655 pushdecl (omp_priv);
31656 add_decl_expr (omp_priv);
31657 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31658 DECL_ARTIFICIAL (omp_orig) = 1;
31659 pushdecl (omp_orig);
31660 add_decl_expr (omp_orig);
31662 keep_next_level (true);
31663 block = begin_omp_structured_block ();
31665 bool ctor = false;
31666 if (strcmp (p, "omp_priv") == 0)
31668 bool is_direct_init, is_non_constant_init;
31669 ctor = true;
31670 cp_lexer_consume_token (parser->lexer);
31671 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31672 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31673 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31674 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31675 == CPP_CLOSE_PAREN
31676 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31677 == CPP_CLOSE_PAREN))
31679 finish_omp_structured_block (block);
31680 error ("invalid initializer clause");
31681 return false;
31683 initializer = cp_parser_initializer (parser, &is_direct_init,
31684 &is_non_constant_init);
31685 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31686 NULL_TREE, LOOKUP_ONLYCONVERTING);
31688 else
31690 cp_parser_parse_tentatively (parser);
31691 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31692 /*check_dependency_p=*/true,
31693 /*template_p=*/NULL,
31694 /*declarator_p=*/false,
31695 /*optional_p=*/false);
31696 vec<tree, va_gc> *args;
31697 if (fn_name == error_mark_node
31698 || cp_parser_error_occurred (parser)
31699 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31700 || ((args = cp_parser_parenthesized_expression_list
31701 (parser, non_attr, /*cast_p=*/false,
31702 /*allow_expansion_p=*/true,
31703 /*non_constant_p=*/NULL)),
31704 cp_parser_error_occurred (parser)))
31706 finish_omp_structured_block (block);
31707 cp_parser_abort_tentative_parse (parser);
31708 cp_parser_error (parser, "expected id-expression (arguments)");
31709 return false;
31711 unsigned int i;
31712 tree arg;
31713 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31714 if (arg == omp_priv
31715 || (TREE_CODE (arg) == ADDR_EXPR
31716 && TREE_OPERAND (arg, 0) == omp_priv))
31717 break;
31718 cp_parser_abort_tentative_parse (parser);
31719 if (arg == NULL_TREE)
31720 error ("one of the initializer call arguments should be %<omp_priv%>"
31721 " or %<&omp_priv%>");
31722 initializer = cp_parser_postfix_expression (parser, false, false, false,
31723 false, NULL);
31724 finish_expr_stmt (initializer);
31727 block = finish_omp_structured_block (block);
31728 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31729 finish_expr_stmt (block);
31731 if (ctor)
31732 add_decl_expr (omp_orig);
31734 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31735 return false;
31738 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31739 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31741 return true;
31744 /* OpenMP 4.0
31745 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31746 initializer-clause[opt] new-line
31748 initializer-clause:
31749 initializer (omp_priv initializer)
31750 initializer (function-name (argument-list)) */
31752 static void
31753 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31754 enum pragma_context)
31756 auto_vec<tree> types;
31757 enum tree_code reduc_code = ERROR_MARK;
31758 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31759 unsigned int i;
31760 cp_token *first_token;
31761 cp_token_cache *cp;
31762 int errs;
31763 void *p;
31765 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31766 p = obstack_alloc (&declarator_obstack, 0);
31768 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31769 goto fail;
31771 switch (cp_lexer_peek_token (parser->lexer)->type)
31773 case CPP_PLUS:
31774 reduc_code = PLUS_EXPR;
31775 break;
31776 case CPP_MULT:
31777 reduc_code = MULT_EXPR;
31778 break;
31779 case CPP_MINUS:
31780 reduc_code = MINUS_EXPR;
31781 break;
31782 case CPP_AND:
31783 reduc_code = BIT_AND_EXPR;
31784 break;
31785 case CPP_XOR:
31786 reduc_code = BIT_XOR_EXPR;
31787 break;
31788 case CPP_OR:
31789 reduc_code = BIT_IOR_EXPR;
31790 break;
31791 case CPP_AND_AND:
31792 reduc_code = TRUTH_ANDIF_EXPR;
31793 break;
31794 case CPP_OR_OR:
31795 reduc_code = TRUTH_ORIF_EXPR;
31796 break;
31797 case CPP_NAME:
31798 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31799 break;
31800 default:
31801 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31802 "%<|%>, %<&&%>, %<||%> or identifier");
31803 goto fail;
31806 if (reduc_code != ERROR_MARK)
31807 cp_lexer_consume_token (parser->lexer);
31809 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31810 if (reduc_id == error_mark_node)
31811 goto fail;
31813 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31814 goto fail;
31816 /* Types may not be defined in declare reduction type list. */
31817 const char *saved_message;
31818 saved_message = parser->type_definition_forbidden_message;
31819 parser->type_definition_forbidden_message
31820 = G_("types may not be defined in declare reduction type list");
31821 bool saved_colon_corrects_to_scope_p;
31822 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31823 parser->colon_corrects_to_scope_p = false;
31824 bool saved_colon_doesnt_start_class_def_p;
31825 saved_colon_doesnt_start_class_def_p
31826 = parser->colon_doesnt_start_class_def_p;
31827 parser->colon_doesnt_start_class_def_p = true;
31829 while (true)
31831 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31832 type = cp_parser_type_id (parser);
31833 if (type == error_mark_node)
31835 else if (ARITHMETIC_TYPE_P (type)
31836 && (orig_reduc_id == NULL_TREE
31837 || (TREE_CODE (type) != COMPLEX_TYPE
31838 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31839 "min") == 0
31840 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31841 "max") == 0))))
31842 error_at (loc, "predeclared arithmetic type %qT in "
31843 "%<#pragma omp declare reduction%>", type);
31844 else if (TREE_CODE (type) == FUNCTION_TYPE
31845 || TREE_CODE (type) == METHOD_TYPE
31846 || TREE_CODE (type) == ARRAY_TYPE)
31847 error_at (loc, "function or array type %qT in "
31848 "%<#pragma omp declare reduction%>", type);
31849 else if (TREE_CODE (type) == REFERENCE_TYPE)
31850 error_at (loc, "reference type %qT in "
31851 "%<#pragma omp declare reduction%>", type);
31852 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31853 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31854 "%<#pragma omp declare reduction%>", type);
31855 else
31856 types.safe_push (type);
31858 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31859 cp_lexer_consume_token (parser->lexer);
31860 else
31861 break;
31864 /* Restore the saved message. */
31865 parser->type_definition_forbidden_message = saved_message;
31866 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31867 parser->colon_doesnt_start_class_def_p
31868 = saved_colon_doesnt_start_class_def_p;
31870 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31871 || types.is_empty ())
31873 fail:
31874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31875 goto done;
31878 first_token = cp_lexer_peek_token (parser->lexer);
31879 cp = NULL;
31880 errs = errorcount;
31881 FOR_EACH_VEC_ELT (types, i, type)
31883 tree fntype
31884 = build_function_type_list (void_type_node,
31885 cp_build_reference_type (type, false),
31886 NULL_TREE);
31887 tree this_reduc_id = reduc_id;
31888 if (!dependent_type_p (type))
31889 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31890 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31891 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31892 DECL_ARTIFICIAL (fndecl) = 1;
31893 DECL_EXTERNAL (fndecl) = 1;
31894 DECL_DECLARED_INLINE_P (fndecl) = 1;
31895 DECL_IGNORED_P (fndecl) = 1;
31896 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31897 DECL_ATTRIBUTES (fndecl)
31898 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31899 DECL_ATTRIBUTES (fndecl));
31900 if (processing_template_decl)
31901 fndecl = push_template_decl (fndecl);
31902 bool block_scope = false;
31903 tree block = NULL_TREE;
31904 if (current_function_decl)
31906 block_scope = true;
31907 DECL_CONTEXT (fndecl) = global_namespace;
31908 if (!processing_template_decl)
31909 pushdecl (fndecl);
31911 else if (current_class_type)
31913 if (cp == NULL)
31915 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31916 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31917 cp_lexer_consume_token (parser->lexer);
31918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31919 goto fail;
31920 cp = cp_token_cache_new (first_token,
31921 cp_lexer_peek_nth_token (parser->lexer,
31922 2));
31924 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31925 finish_member_declaration (fndecl);
31926 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31927 DECL_PENDING_INLINE_P (fndecl) = 1;
31928 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31929 continue;
31931 else
31933 DECL_CONTEXT (fndecl) = current_namespace;
31934 pushdecl (fndecl);
31936 if (!block_scope)
31937 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31938 else
31939 block = begin_omp_structured_block ();
31940 if (cp)
31942 cp_parser_push_lexer_for_tokens (parser, cp);
31943 parser->lexer->in_pragma = true;
31945 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31947 if (!block_scope)
31948 finish_function (0);
31949 else
31950 DECL_CONTEXT (fndecl) = current_function_decl;
31951 if (cp)
31952 cp_parser_pop_lexer (parser);
31953 goto fail;
31955 if (cp)
31956 cp_parser_pop_lexer (parser);
31957 if (!block_scope)
31958 finish_function (0);
31959 else
31961 DECL_CONTEXT (fndecl) = current_function_decl;
31962 block = finish_omp_structured_block (block);
31963 if (TREE_CODE (block) == BIND_EXPR)
31964 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31965 else if (TREE_CODE (block) == STATEMENT_LIST)
31966 DECL_SAVED_TREE (fndecl) = block;
31967 if (processing_template_decl)
31968 add_decl_expr (fndecl);
31970 cp_check_omp_declare_reduction (fndecl);
31971 if (cp == NULL && types.length () > 1)
31972 cp = cp_token_cache_new (first_token,
31973 cp_lexer_peek_nth_token (parser->lexer, 2));
31974 if (errs != errorcount)
31975 break;
31978 cp_parser_require_pragma_eol (parser, pragma_tok);
31980 done:
31981 /* Free any declarators allocated. */
31982 obstack_free (&declarator_obstack, p);
31985 /* OpenMP 4.0
31986 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31987 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31988 initializer-clause[opt] new-line
31989 #pragma omp declare target new-line */
31991 static void
31992 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31993 enum pragma_context context)
31995 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31997 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31998 const char *p = IDENTIFIER_POINTER (id);
32000 if (strcmp (p, "simd") == 0)
32002 cp_lexer_consume_token (parser->lexer);
32003 cp_parser_omp_declare_simd (parser, pragma_tok,
32004 context);
32005 return;
32007 cp_ensure_no_omp_declare_simd (parser);
32008 if (strcmp (p, "reduction") == 0)
32010 cp_lexer_consume_token (parser->lexer);
32011 cp_parser_omp_declare_reduction (parser, pragma_tok,
32012 context);
32013 return;
32015 if (!flag_openmp) /* flag_openmp_simd */
32017 cp_parser_require_pragma_eol (parser, pragma_tok);
32018 return;
32020 if (strcmp (p, "target") == 0)
32022 cp_lexer_consume_token (parser->lexer);
32023 cp_parser_omp_declare_target (parser, pragma_tok);
32024 return;
32027 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32028 "or %<target%>");
32029 cp_parser_require_pragma_eol (parser, pragma_tok);
32032 /* Main entry point to OpenMP statement pragmas. */
32034 static void
32035 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32037 tree stmt;
32038 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32039 omp_clause_mask mask (0);
32041 switch (pragma_tok->pragma_kind)
32043 case PRAGMA_OMP_ATOMIC:
32044 cp_parser_omp_atomic (parser, pragma_tok);
32045 return;
32046 case PRAGMA_OMP_CRITICAL:
32047 stmt = cp_parser_omp_critical (parser, pragma_tok);
32048 break;
32049 case PRAGMA_OMP_DISTRIBUTE:
32050 strcpy (p_name, "#pragma omp");
32051 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32052 break;
32053 case PRAGMA_OMP_FOR:
32054 strcpy (p_name, "#pragma omp");
32055 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32056 break;
32057 case PRAGMA_OMP_MASTER:
32058 stmt = cp_parser_omp_master (parser, pragma_tok);
32059 break;
32060 case PRAGMA_OMP_ORDERED:
32061 stmt = cp_parser_omp_ordered (parser, pragma_tok);
32062 break;
32063 case PRAGMA_OMP_PARALLEL:
32064 strcpy (p_name, "#pragma omp");
32065 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32066 break;
32067 case PRAGMA_OMP_SECTIONS:
32068 strcpy (p_name, "#pragma omp");
32069 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32070 break;
32071 case PRAGMA_OMP_SIMD:
32072 strcpy (p_name, "#pragma omp");
32073 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32074 break;
32075 case PRAGMA_OMP_SINGLE:
32076 stmt = cp_parser_omp_single (parser, pragma_tok);
32077 break;
32078 case PRAGMA_OMP_TASK:
32079 stmt = cp_parser_omp_task (parser, pragma_tok);
32080 break;
32081 case PRAGMA_OMP_TASKGROUP:
32082 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32083 break;
32084 case PRAGMA_OMP_TEAMS:
32085 strcpy (p_name, "#pragma omp");
32086 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32087 break;
32088 default:
32089 gcc_unreachable ();
32092 if (stmt)
32093 SET_EXPR_LOCATION (stmt, pragma_tok->location);
32096 /* Transactional Memory parsing routines. */
32098 /* Parse a transaction attribute.
32100 txn-attribute:
32101 attribute
32102 [ [ identifier ] ]
32104 ??? Simplify this when C++0x bracket attributes are
32105 implemented properly. */
32107 static tree
32108 cp_parser_txn_attribute_opt (cp_parser *parser)
32110 cp_token *token;
32111 tree attr_name, attr = NULL;
32113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32114 return cp_parser_attributes_opt (parser);
32116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32117 return NULL_TREE;
32118 cp_lexer_consume_token (parser->lexer);
32119 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32120 goto error1;
32122 token = cp_lexer_peek_token (parser->lexer);
32123 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32125 token = cp_lexer_consume_token (parser->lexer);
32127 attr_name = (token->type == CPP_KEYWORD
32128 /* For keywords, use the canonical spelling,
32129 not the parsed identifier. */
32130 ? ridpointers[(int) token->keyword]
32131 : token->u.value);
32132 attr = build_tree_list (attr_name, NULL_TREE);
32134 else
32135 cp_parser_error (parser, "expected identifier");
32137 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32138 error1:
32139 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32140 return attr;
32143 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32145 transaction-statement:
32146 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32147 compound-statement
32148 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32151 static tree
32152 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32154 unsigned char old_in = parser->in_transaction;
32155 unsigned char this_in = 1, new_in;
32156 cp_token *token;
32157 tree stmt, attrs, noex;
32159 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32160 || keyword == RID_TRANSACTION_RELAXED);
32161 token = cp_parser_require_keyword (parser, keyword,
32162 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32163 : RT_TRANSACTION_RELAXED));
32164 gcc_assert (token != NULL);
32166 if (keyword == RID_TRANSACTION_RELAXED)
32167 this_in |= TM_STMT_ATTR_RELAXED;
32168 else
32170 attrs = cp_parser_txn_attribute_opt (parser);
32171 if (attrs)
32172 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32175 /* Parse a noexcept specification. */
32176 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32178 /* Keep track if we're in the lexical scope of an outer transaction. */
32179 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32181 stmt = begin_transaction_stmt (token->location, NULL, this_in);
32183 parser->in_transaction = new_in;
32184 cp_parser_compound_statement (parser, NULL, false, false);
32185 parser->in_transaction = old_in;
32187 finish_transaction_stmt (stmt, NULL, this_in, noex);
32189 return stmt;
32192 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32194 transaction-expression:
32195 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32196 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32199 static tree
32200 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32202 unsigned char old_in = parser->in_transaction;
32203 unsigned char this_in = 1;
32204 cp_token *token;
32205 tree expr, noex;
32206 bool noex_expr;
32208 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32209 || keyword == RID_TRANSACTION_RELAXED);
32211 if (!flag_tm)
32212 error (keyword == RID_TRANSACTION_RELAXED
32213 ? G_("%<__transaction_relaxed%> without transactional memory "
32214 "support enabled")
32215 : G_("%<__transaction_atomic%> without transactional memory "
32216 "support enabled"));
32218 token = cp_parser_require_keyword (parser, keyword,
32219 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32220 : RT_TRANSACTION_RELAXED));
32221 gcc_assert (token != NULL);
32223 if (keyword == RID_TRANSACTION_RELAXED)
32224 this_in |= TM_STMT_ATTR_RELAXED;
32226 /* Set this early. This might mean that we allow transaction_cancel in
32227 an expression that we find out later actually has to be a constexpr.
32228 However, we expect that cxx_constant_value will be able to deal with
32229 this; also, if the noexcept has no constexpr, then what we parse next
32230 really is a transaction's body. */
32231 parser->in_transaction = this_in;
32233 /* Parse a noexcept specification. */
32234 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32235 true);
32237 if (!noex || !noex_expr
32238 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32240 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32242 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
32243 expr = finish_parenthesized_expr (expr);
32245 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32247 else
32249 /* The only expression that is available got parsed for the noexcept
32250 already. noexcept is true then. */
32251 expr = noex;
32252 noex = boolean_true_node;
32255 expr = build_transaction_expr (token->location, expr, this_in, noex);
32256 parser->in_transaction = old_in;
32258 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32259 return error_mark_node;
32261 return (flag_tm ? expr : error_mark_node);
32264 /* Parse a function-transaction-block.
32266 function-transaction-block:
32267 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32268 function-body
32269 __transaction_atomic txn-attribute[opt] function-try-block
32270 __transaction_relaxed ctor-initializer[opt] function-body
32271 __transaction_relaxed function-try-block
32274 static bool
32275 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32277 unsigned char old_in = parser->in_transaction;
32278 unsigned char new_in = 1;
32279 tree compound_stmt, stmt, attrs;
32280 bool ctor_initializer_p;
32281 cp_token *token;
32283 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32284 || keyword == RID_TRANSACTION_RELAXED);
32285 token = cp_parser_require_keyword (parser, keyword,
32286 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32287 : RT_TRANSACTION_RELAXED));
32288 gcc_assert (token != NULL);
32290 if (keyword == RID_TRANSACTION_RELAXED)
32291 new_in |= TM_STMT_ATTR_RELAXED;
32292 else
32294 attrs = cp_parser_txn_attribute_opt (parser);
32295 if (attrs)
32296 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32299 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32301 parser->in_transaction = new_in;
32303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32304 ctor_initializer_p = cp_parser_function_try_block (parser);
32305 else
32306 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32307 (parser, /*in_function_try_block=*/false);
32309 parser->in_transaction = old_in;
32311 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32313 return ctor_initializer_p;
32316 /* Parse a __transaction_cancel statement.
32318 cancel-statement:
32319 __transaction_cancel txn-attribute[opt] ;
32320 __transaction_cancel txn-attribute[opt] throw-expression ;
32322 ??? Cancel and throw is not yet implemented. */
32324 static tree
32325 cp_parser_transaction_cancel (cp_parser *parser)
32327 cp_token *token;
32328 bool is_outer = false;
32329 tree stmt, attrs;
32331 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32332 RT_TRANSACTION_CANCEL);
32333 gcc_assert (token != NULL);
32335 attrs = cp_parser_txn_attribute_opt (parser);
32336 if (attrs)
32337 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32339 /* ??? Parse cancel-and-throw here. */
32341 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32343 if (!flag_tm)
32345 error_at (token->location, "%<__transaction_cancel%> without "
32346 "transactional memory support enabled");
32347 return error_mark_node;
32349 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32351 error_at (token->location, "%<__transaction_cancel%> within a "
32352 "%<__transaction_relaxed%>");
32353 return error_mark_node;
32355 else if (is_outer)
32357 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32358 && !is_tm_may_cancel_outer (current_function_decl))
32360 error_at (token->location, "outer %<__transaction_cancel%> not "
32361 "within outer %<__transaction_atomic%>");
32362 error_at (token->location,
32363 " or a %<transaction_may_cancel_outer%> function");
32364 return error_mark_node;
32367 else if (parser->in_transaction == 0)
32369 error_at (token->location, "%<__transaction_cancel%> not within "
32370 "%<__transaction_atomic%>");
32371 return error_mark_node;
32374 stmt = build_tm_abort_call (token->location, is_outer);
32375 add_stmt (stmt);
32377 return stmt;
32380 /* The parser. */
32382 static GTY (()) cp_parser *the_parser;
32385 /* Special handling for the first token or line in the file. The first
32386 thing in the file might be #pragma GCC pch_preprocess, which loads a
32387 PCH file, which is a GC collection point. So we need to handle this
32388 first pragma without benefit of an existing lexer structure.
32390 Always returns one token to the caller in *FIRST_TOKEN. This is
32391 either the true first token of the file, or the first token after
32392 the initial pragma. */
32394 static void
32395 cp_parser_initial_pragma (cp_token *first_token)
32397 tree name = NULL;
32399 cp_lexer_get_preprocessor_token (NULL, first_token);
32400 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32401 return;
32403 cp_lexer_get_preprocessor_token (NULL, first_token);
32404 if (first_token->type == CPP_STRING)
32406 name = first_token->u.value;
32408 cp_lexer_get_preprocessor_token (NULL, first_token);
32409 if (first_token->type != CPP_PRAGMA_EOL)
32410 error_at (first_token->location,
32411 "junk at end of %<#pragma GCC pch_preprocess%>");
32413 else
32414 error_at (first_token->location, "expected string literal");
32416 /* Skip to the end of the pragma. */
32417 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32418 cp_lexer_get_preprocessor_token (NULL, first_token);
32420 /* Now actually load the PCH file. */
32421 if (name)
32422 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32424 /* Read one more token to return to our caller. We have to do this
32425 after reading the PCH file in, since its pointers have to be
32426 live. */
32427 cp_lexer_get_preprocessor_token (NULL, first_token);
32430 /* Normal parsing of a pragma token. Here we can (and must) use the
32431 regular lexer. */
32433 static bool
32434 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32436 cp_token *pragma_tok;
32437 unsigned int id;
32439 pragma_tok = cp_lexer_consume_token (parser->lexer);
32440 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32441 parser->lexer->in_pragma = true;
32443 id = pragma_tok->pragma_kind;
32444 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32445 cp_ensure_no_omp_declare_simd (parser);
32446 switch (id)
32448 case PRAGMA_GCC_PCH_PREPROCESS:
32449 error_at (pragma_tok->location,
32450 "%<#pragma GCC pch_preprocess%> must be first");
32451 break;
32453 case PRAGMA_OMP_BARRIER:
32454 switch (context)
32456 case pragma_compound:
32457 cp_parser_omp_barrier (parser, pragma_tok);
32458 return false;
32459 case pragma_stmt:
32460 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32461 "used in compound statements");
32462 break;
32463 default:
32464 goto bad_stmt;
32466 break;
32468 case PRAGMA_OMP_FLUSH:
32469 switch (context)
32471 case pragma_compound:
32472 cp_parser_omp_flush (parser, pragma_tok);
32473 return false;
32474 case pragma_stmt:
32475 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32476 "used in compound statements");
32477 break;
32478 default:
32479 goto bad_stmt;
32481 break;
32483 case PRAGMA_OMP_TASKWAIT:
32484 switch (context)
32486 case pragma_compound:
32487 cp_parser_omp_taskwait (parser, pragma_tok);
32488 return false;
32489 case pragma_stmt:
32490 error_at (pragma_tok->location,
32491 "%<#pragma omp taskwait%> may only be "
32492 "used in compound statements");
32493 break;
32494 default:
32495 goto bad_stmt;
32497 break;
32499 case PRAGMA_OMP_TASKYIELD:
32500 switch (context)
32502 case pragma_compound:
32503 cp_parser_omp_taskyield (parser, pragma_tok);
32504 return false;
32505 case pragma_stmt:
32506 error_at (pragma_tok->location,
32507 "%<#pragma omp taskyield%> may only be "
32508 "used in compound statements");
32509 break;
32510 default:
32511 goto bad_stmt;
32513 break;
32515 case PRAGMA_OMP_CANCEL:
32516 switch (context)
32518 case pragma_compound:
32519 cp_parser_omp_cancel (parser, pragma_tok);
32520 return false;
32521 case pragma_stmt:
32522 error_at (pragma_tok->location,
32523 "%<#pragma omp cancel%> may only be "
32524 "used in compound statements");
32525 break;
32526 default:
32527 goto bad_stmt;
32529 break;
32531 case PRAGMA_OMP_CANCELLATION_POINT:
32532 switch (context)
32534 case pragma_compound:
32535 cp_parser_omp_cancellation_point (parser, pragma_tok);
32536 return false;
32537 case pragma_stmt:
32538 error_at (pragma_tok->location,
32539 "%<#pragma omp cancellation point%> may only be "
32540 "used in compound statements");
32541 break;
32542 default:
32543 goto bad_stmt;
32545 break;
32547 case PRAGMA_OMP_THREADPRIVATE:
32548 cp_parser_omp_threadprivate (parser, pragma_tok);
32549 return false;
32551 case PRAGMA_OMP_DECLARE_REDUCTION:
32552 cp_parser_omp_declare (parser, pragma_tok, context);
32553 return false;
32555 case PRAGMA_OMP_ATOMIC:
32556 case PRAGMA_OMP_CRITICAL:
32557 case PRAGMA_OMP_DISTRIBUTE:
32558 case PRAGMA_OMP_FOR:
32559 case PRAGMA_OMP_MASTER:
32560 case PRAGMA_OMP_ORDERED:
32561 case PRAGMA_OMP_PARALLEL:
32562 case PRAGMA_OMP_SECTIONS:
32563 case PRAGMA_OMP_SIMD:
32564 case PRAGMA_OMP_SINGLE:
32565 case PRAGMA_OMP_TASK:
32566 case PRAGMA_OMP_TASKGROUP:
32567 case PRAGMA_OMP_TEAMS:
32568 if (context != pragma_stmt && context != pragma_compound)
32569 goto bad_stmt;
32570 cp_parser_omp_construct (parser, pragma_tok);
32571 return true;
32573 case PRAGMA_OMP_TARGET:
32574 return cp_parser_omp_target (parser, pragma_tok, context);
32576 case PRAGMA_OMP_END_DECLARE_TARGET:
32577 cp_parser_omp_end_declare_target (parser, pragma_tok);
32578 return false;
32580 case PRAGMA_OMP_SECTION:
32581 error_at (pragma_tok->location,
32582 "%<#pragma omp section%> may only be used in "
32583 "%<#pragma omp sections%> construct");
32584 break;
32586 case PRAGMA_IVDEP:
32588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32589 cp_token *tok;
32590 tok = cp_lexer_peek_token (the_parser->lexer);
32591 if (tok->type != CPP_KEYWORD
32592 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32593 && tok->keyword != RID_DO))
32595 cp_parser_error (parser, "for, while or do statement expected");
32596 return false;
32598 cp_parser_iteration_statement (parser, true);
32599 return true;
32602 case PRAGMA_CILK_SIMD:
32603 if (context == pragma_external)
32605 error_at (pragma_tok->location,
32606 "%<#pragma simd%> must be inside a function");
32607 break;
32609 cp_parser_cilk_simd (parser, pragma_tok);
32610 return true;
32612 default:
32613 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32614 c_invoke_pragma_handler (id);
32615 break;
32617 bad_stmt:
32618 cp_parser_error (parser, "expected declaration specifiers");
32619 break;
32622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32623 return false;
32626 /* The interface the pragma parsers have to the lexer. */
32628 enum cpp_ttype
32629 pragma_lex (tree *value)
32631 cp_token *tok;
32632 enum cpp_ttype ret;
32634 tok = cp_lexer_peek_token (the_parser->lexer);
32636 ret = tok->type;
32637 *value = tok->u.value;
32639 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32640 ret = CPP_EOF;
32641 else if (ret == CPP_STRING)
32642 *value = cp_parser_string_literal (the_parser, false, false);
32643 else
32645 cp_lexer_consume_token (the_parser->lexer);
32646 if (ret == CPP_KEYWORD)
32647 ret = CPP_NAME;
32650 return ret;
32654 /* External interface. */
32656 /* Parse one entire translation unit. */
32658 void
32659 c_parse_file (void)
32661 static bool already_called = false;
32663 if (already_called)
32665 sorry ("inter-module optimizations not implemented for C++");
32666 return;
32668 already_called = true;
32670 the_parser = cp_parser_new ();
32671 push_deferring_access_checks (flag_access_control
32672 ? dk_no_deferred : dk_no_check);
32673 cp_parser_translation_unit (the_parser);
32674 the_parser = NULL;
32677 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32678 vectorlength clause:
32679 Syntax:
32680 vectorlength ( constant-expression ) */
32682 static tree
32683 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32684 bool is_simd_fn)
32686 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32687 tree expr;
32688 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32689 safelen clause. Thus, vectorlength is represented as OMP 4.0
32690 safelen. For SIMD-enabled function it is represented by OMP 4.0
32691 simdlen. */
32692 if (!is_simd_fn)
32693 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32694 loc);
32695 else
32696 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32697 loc);
32699 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32700 return error_mark_node;
32702 expr = cp_parser_constant_expression (parser, false, NULL);
32703 expr = maybe_constant_value (expr);
32705 /* If expr == error_mark_node, then don't emit any errors nor
32706 create a clause. if any of the above functions returns
32707 error mark node then they would have emitted an error message. */
32708 if (expr == error_mark_node)
32710 else if (!TREE_TYPE (expr)
32711 || !TREE_CONSTANT (expr)
32712 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32713 error_at (loc, "vectorlength must be an integer constant");
32714 else if (TREE_CONSTANT (expr)
32715 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32716 error_at (loc, "vectorlength must be a power of 2");
32717 else
32719 tree c;
32720 if (!is_simd_fn)
32722 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32723 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32724 OMP_CLAUSE_CHAIN (c) = clauses;
32725 clauses = c;
32727 else
32729 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32730 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32731 OMP_CLAUSE_CHAIN (c) = clauses;
32732 clauses = c;
32736 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32737 return error_mark_node;
32738 return clauses;
32741 /* Handles the Cilk Plus #pragma simd linear clause.
32742 Syntax:
32743 linear ( simd-linear-variable-list )
32745 simd-linear-variable-list:
32746 simd-linear-variable
32747 simd-linear-variable-list , simd-linear-variable
32749 simd-linear-variable:
32750 id-expression
32751 id-expression : simd-linear-step
32753 simd-linear-step:
32754 conditional-expression */
32756 static tree
32757 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32759 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32761 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32762 return clauses;
32763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32765 cp_parser_error (parser, "expected identifier");
32766 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32767 return error_mark_node;
32770 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32771 parser->colon_corrects_to_scope_p = false;
32772 while (1)
32774 cp_token *token = cp_lexer_peek_token (parser->lexer);
32775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32777 cp_parser_error (parser, "expected variable-name");
32778 clauses = error_mark_node;
32779 break;
32782 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32783 false, false);
32784 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32785 token->location);
32786 if (decl == error_mark_node)
32788 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32789 token->location);
32790 clauses = error_mark_node;
32792 else
32794 tree e = NULL_TREE;
32795 tree step_size = integer_one_node;
32797 /* If present, parse the linear step. Otherwise, assume the default
32798 value of 1. */
32799 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32801 cp_lexer_consume_token (parser->lexer);
32803 e = cp_parser_assignment_expression (parser, false, NULL);
32804 e = maybe_constant_value (e);
32806 if (e == error_mark_node)
32808 /* If an error has occurred, then the whole pragma is
32809 considered ill-formed. Thus, no reason to keep
32810 parsing. */
32811 clauses = error_mark_node;
32812 break;
32814 else if (type_dependent_expression_p (e)
32815 || value_dependent_expression_p (e)
32816 || (TREE_TYPE (e)
32817 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32818 && (TREE_CONSTANT (e)
32819 || DECL_P (e))))
32820 step_size = e;
32821 else
32822 cp_parser_error (parser,
32823 "step size must be an integer constant "
32824 "expression or an integer variable");
32827 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32828 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32829 OMP_CLAUSE_DECL (l) = decl;
32830 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32831 OMP_CLAUSE_CHAIN (l) = clauses;
32832 clauses = l;
32834 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32835 cp_lexer_consume_token (parser->lexer);
32836 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32837 break;
32838 else
32840 error_at (cp_lexer_peek_token (parser->lexer)->location,
32841 "expected %<,%> or %<)%> after %qE", decl);
32842 clauses = error_mark_node;
32843 break;
32846 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32847 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32848 return clauses;
32851 /* Returns the name of the next clause. If the clause is not
32852 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32853 token is not consumed. Otherwise, the appropriate enum from the
32854 pragma_simd_clause is returned and the token is consumed. */
32856 static pragma_omp_clause
32857 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32859 pragma_omp_clause clause_type;
32860 cp_token *token = cp_lexer_peek_token (parser->lexer);
32862 if (token->keyword == RID_PRIVATE)
32863 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32864 else if (!token->u.value || token->type != CPP_NAME)
32865 return PRAGMA_CILK_CLAUSE_NONE;
32866 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32867 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32868 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32869 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32870 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32871 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32872 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32873 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32874 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32875 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32876 else
32877 return PRAGMA_CILK_CLAUSE_NONE;
32879 cp_lexer_consume_token (parser->lexer);
32880 return clause_type;
32883 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32885 static tree
32886 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32888 tree clauses = NULL_TREE;
32890 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32891 && clauses != error_mark_node)
32893 pragma_omp_clause c_kind;
32894 c_kind = cp_parser_cilk_simd_clause_name (parser);
32895 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32896 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32897 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32898 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32899 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32900 /* Use the OpenMP 4.0 equivalent function. */
32901 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32902 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32903 /* Use the OpenMP 4.0 equivalent function. */
32904 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32905 clauses);
32906 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32907 /* Use the OMP 4.0 equivalent function. */
32908 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32909 clauses);
32910 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32911 /* Use the OMP 4.0 equivalent function. */
32912 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32913 else
32915 clauses = error_mark_node;
32916 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32917 break;
32921 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32923 if (clauses == error_mark_node)
32924 return error_mark_node;
32925 else
32926 return c_finish_cilk_clauses (clauses);
32929 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32931 static void
32932 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32934 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32936 if (clauses == error_mark_node)
32937 return;
32939 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32941 error_at (cp_lexer_peek_token (parser->lexer)->location,
32942 "for statement expected");
32943 return;
32946 tree sb = begin_omp_structured_block ();
32947 int save = cp_parser_begin_omp_structured_block (parser);
32948 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32949 if (ret)
32950 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32951 cp_parser_end_omp_structured_block (parser, save);
32952 add_stmt (finish_omp_structured_block (sb));
32953 return;
32956 /* Create an identifier for a generic parameter type (a synthesized
32957 template parameter implied by `auto' or a concept identifier). */
32959 static GTY(()) int generic_parm_count;
32960 static tree
32961 make_generic_type_name ()
32963 char buf[32];
32964 sprintf (buf, "auto:%d", ++generic_parm_count);
32965 return get_identifier (buf);
32968 /* Predicate that behaves as is_auto_or_concept but matches the parent
32969 node of the generic type rather than the generic type itself. This
32970 allows for type transformation in add_implicit_template_parms. */
32972 static inline bool
32973 tree_type_is_auto_or_concept (const_tree t)
32975 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32978 // Return the template decl being called or evaluated as part of the
32979 // constraint check.
32981 // TODO: This is a bit of a hack. When we finish the template parameter
32982 // the constraint is just a call expression, but we don't have the full
32983 // context that we used to build that call expression. Since we're going
32984 // to be comparing declarations, it would helpful to have that. This
32985 // means we'll have to make the TREE_TYPE of the parameter node a pair
32986 // containing the context (the TYPE_DECL) and the constraint.
32987 static tree
32988 get_concept_from_constraint (tree t)
32990 gcc_assert (TREE_CODE (t) == CALL_EXPR);
32991 tree fn = CALL_EXPR_FN (t);
32992 tree ovl = TREE_OPERAND (fn, 0);
32993 tree tmpl = OVL_FUNCTION (ovl);
32994 return DECL_TEMPLATE_RESULT (tmpl);
32997 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32998 (creating a new template parameter list if necessary). Returns the newly
32999 created template type parm. */
33001 tree
33002 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
33004 gcc_assert (current_binding_level->kind == sk_function_parms);
33006 // Before committing to modifying any scope, if we're in an implicit
33007 // template scope, and we're trying to synthesize a constrained
33008 // parameter, try to find a previous parameter with the same name.
33009 if (parser->implicit_template_scope && constr)
33011 tree t = parser->implicit_template_parms;
33012 while (t)
33014 tree c = get_concept_from_constraint (TREE_TYPE (t));
33015 if (c == DECL_SIZE_UNIT (constr))
33016 return TREE_VALUE (t);
33017 t = TREE_CHAIN (t);
33022 /* We are either continuing a function template that already contains implicit
33023 template parameters, creating a new fully-implicit function template, or
33024 extending an existing explicit function template with implicit template
33025 parameters. */
33027 cp_binding_level *const entry_scope = current_binding_level;
33029 bool become_template = false;
33030 cp_binding_level *parent_scope = 0;
33032 if (parser->implicit_template_scope)
33034 gcc_assert (parser->implicit_template_parms);
33036 current_binding_level = parser->implicit_template_scope;
33038 else
33040 /* Roll back to the existing template parameter scope (in the case of
33041 extending an explicit function template) or introduce a new template
33042 parameter scope ahead of the function parameter scope (or class scope
33043 in the case of out-of-line member definitions). The function scope is
33044 added back after template parameter synthesis below. */
33046 cp_binding_level *scope = entry_scope;
33048 while (scope->kind == sk_function_parms)
33050 parent_scope = scope;
33051 scope = scope->level_chain;
33053 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33055 /* If not defining a class, then any class scope is a scope level in
33056 an out-of-line member definition. In this case simply wind back
33057 beyond the first such scope to inject the template parameter list.
33058 Otherwise wind back to the class being defined. The latter can
33059 occur in class member friend declarations such as:
33061 class A {
33062 void foo (auto);
33064 class B {
33065 friend void A::foo (auto);
33068 The template parameter list synthesized for the friend declaration
33069 must be injected in the scope of 'B'. This can also occur in
33070 erroneous cases such as:
33072 struct A {
33073 struct B {
33074 void foo (auto);
33076 void B::foo (auto) {}
33079 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33080 but, nevertheless, the template parameter list synthesized for the
33081 declarator should be injected into the scope of 'A' as if the
33082 ill-formed template was specified explicitly. */
33084 while (scope->kind == sk_class && !scope->defining_class_p)
33086 parent_scope = scope;
33087 scope = scope->level_chain;
33091 current_binding_level = scope;
33093 if (scope->kind != sk_template_parms
33094 || !function_being_declared_is_template_p (parser))
33096 /* Introduce a new template parameter list for implicit template
33097 parameters. */
33099 become_template = true;
33101 parser->implicit_template_scope
33102 = begin_scope (sk_template_parms, NULL);
33104 ++processing_template_decl;
33106 parser->fully_implicit_function_template_p = true;
33107 ++parser->num_template_parameter_lists;
33109 else
33111 /* Synthesize implicit template parameters at the end of the explicit
33112 template parameter list. */
33114 gcc_assert (current_template_parms);
33116 parser->implicit_template_scope = scope;
33118 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33119 parser->implicit_template_parms
33120 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33124 /* Synthesize a new template parameter and track the current template
33125 parameter chain with implicit_template_parms. */
33127 tree synth_id = make_generic_type_name ();
33128 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33129 synth_id);
33131 // Attach the constraint to the parm before processing.
33132 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
33133 TREE_TYPE (node) = constr;
33135 tree new_parm
33136 = process_template_parm (parser->implicit_template_parms,
33137 input_location,
33138 node,
33139 /*non_type=*/false,
33140 /*param_pack=*/false);
33143 if (parser->implicit_template_parms)
33144 parser->implicit_template_parms
33145 = TREE_CHAIN (parser->implicit_template_parms);
33146 else
33147 parser->implicit_template_parms = new_parm;
33149 tree new_type = TREE_TYPE (getdecls ());
33151 /* If creating a fully implicit function template, start the new implicit
33152 template parameter list with this synthesized type, otherwise grow the
33153 current template parameter list. */
33155 if (become_template)
33157 parent_scope->level_chain = current_binding_level;
33159 tree new_parms = make_tree_vec (1);
33160 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33161 current_template_parms = tree_cons (size_int (processing_template_decl),
33162 new_parms, current_template_parms);
33164 else
33166 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33167 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33168 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33169 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33172 current_binding_level = entry_scope;
33174 return new_type;
33177 /* Finish the declaration of a fully implicit function template. Such a
33178 template has no explicit template parameter list so has not been through the
33179 normal template head and tail processing. synthesize_implicit_template_parm
33180 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33181 provided if the declaration is a class member such that its template
33182 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33183 form is returned. Otherwise NULL_TREE is returned. */
33185 tree
33186 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33188 gcc_assert (parser->fully_implicit_function_template_p);
33190 if (member_decl_opt && member_decl_opt != error_mark_node
33191 && DECL_VIRTUAL_P (member_decl_opt))
33193 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33194 "implicit templates may not be %<virtual%>");
33195 DECL_VIRTUAL_P (member_decl_opt) = false;
33198 if (member_decl_opt)
33199 member_decl_opt = finish_member_template_decl (member_decl_opt);
33200 end_template_decl ();
33202 parser->fully_implicit_function_template_p = false;
33203 --parser->num_template_parameter_lists;
33205 return member_decl_opt;
33208 #include "gt-cp-parser.h"