gcc/
[official-gcc.git] / gcc / cp / parser.c
blobad99cab38cf64969e3e50aa6e4cc9a7e67cbd5f7
1 /* C++ Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 } non_integral_constant;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 } name_lookup_error;
130 /* The various kinds of required token */
131 typedef enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_INTERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
186 /* Prototypes. */
188 static cp_lexer *cp_lexer_new_main
189 (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191 (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193 (cp_lexer *);
194 static int cp_lexer_saving_tokens
195 (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197 (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201 (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203 (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211 (cp_lexer *);
212 static void cp_lexer_purge_token
213 (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217 (cp_lexer *);
218 static void cp_lexer_commit_tokens
219 (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221 (cp_lexer *);
222 static void cp_lexer_print_token
223 (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225 (cp_lexer *);
226 static void cp_lexer_start_debugging
227 (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229 (cp_lexer *) ATTRIBUTE_UNUSED;
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 static tree cp_literal_operator_id
238 (const char *);
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static bool cp_parser_omp_declare_reduction_exprs
243 (tree, cp_parser *);
244 static tree cp_parser_cilk_simd_vectorlength
245 (cp_parser *, tree, bool);
247 /* Manifest constants. */
248 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
249 #define CP_SAVED_TOKEN_STACK 5
251 /* Variables. */
253 /* The stream to which debugging output should be written. */
254 static FILE *cp_lexer_debug_stream;
256 /* Nonzero if we are parsing an unevaluated operand: an operand to
257 sizeof, typeof, or alignof. */
258 int cp_unevaluated_operand;
260 /* Dump up to NUM tokens in BUFFER to FILE starting with token
261 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
262 first token in BUFFER. If NUM is 0, dump all the tokens. If
263 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
264 highlighted by surrounding it in [[ ]]. */
266 static void
267 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
268 cp_token *start_token, unsigned num,
269 cp_token *curr_token)
271 unsigned i, nprinted;
272 cp_token *token;
273 bool do_print;
275 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
277 if (buffer == NULL)
278 return;
280 if (num == 0)
281 num = buffer->length ();
283 if (start_token == NULL)
284 start_token = buffer->address ();
286 if (start_token > buffer->address ())
288 cp_lexer_print_token (file, &(*buffer)[0]);
289 fprintf (file, " ... ");
292 do_print = false;
293 nprinted = 0;
294 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
296 if (token == start_token)
297 do_print = true;
299 if (!do_print)
300 continue;
302 nprinted++;
303 if (token == curr_token)
304 fprintf (file, "[[");
306 cp_lexer_print_token (file, token);
308 if (token == curr_token)
309 fprintf (file, "]]");
311 switch (token->type)
313 case CPP_SEMICOLON:
314 case CPP_OPEN_BRACE:
315 case CPP_CLOSE_BRACE:
316 case CPP_EOF:
317 fputc ('\n', file);
318 break;
320 default:
321 fputc (' ', file);
325 if (i == num && i < buffer->length ())
327 fprintf (file, " ... ");
328 cp_lexer_print_token (file, &buffer->last ());
331 fprintf (file, "\n");
335 /* Dump all tokens in BUFFER to stderr. */
337 void
338 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
340 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
343 DEBUG_FUNCTION void
344 debug (vec<cp_token, va_gc> &ref)
346 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
349 DEBUG_FUNCTION void
350 debug (vec<cp_token, va_gc> *ptr)
352 if (ptr)
353 debug (*ptr);
354 else
355 fprintf (stderr, "<nil>\n");
359 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
360 description for T. */
362 static void
363 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
365 if (t)
367 fprintf (file, "%s: ", desc);
368 print_node_brief (file, "", t, 0);
373 /* Dump parser context C to FILE. */
375 static void
376 cp_debug_print_context (FILE *file, cp_parser_context *c)
378 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
379 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
380 print_node_brief (file, "", c->object_type, 0);
381 fprintf (file, "}\n");
385 /* Print the stack of parsing contexts to FILE starting with FIRST. */
387 static void
388 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
390 unsigned i;
391 cp_parser_context *c;
393 fprintf (file, "Parsing context stack:\n");
394 for (i = 0, c = first; c; c = c->next, i++)
396 fprintf (file, "\t#%u: ", i);
397 cp_debug_print_context (file, c);
402 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
404 static void
405 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
407 if (flag)
408 fprintf (file, "%s: true\n", desc);
412 /* Print an unparsed function entry UF to FILE. */
414 static void
415 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
417 unsigned i;
418 cp_default_arg_entry *default_arg_fn;
419 tree fn;
421 fprintf (file, "\tFunctions with default args:\n");
422 for (i = 0;
423 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
424 i++)
426 fprintf (file, "\t\tClass type: ");
427 print_node_brief (file, "", default_arg_fn->class_type, 0);
428 fprintf (file, "\t\tDeclaration: ");
429 print_node_brief (file, "", default_arg_fn->decl, 0);
430 fprintf (file, "\n");
433 fprintf (file, "\n\tFunctions with definitions that require "
434 "post-processing\n\t\t");
435 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
437 print_node_brief (file, "", fn, 0);
438 fprintf (file, " ");
440 fprintf (file, "\n");
442 fprintf (file, "\n\tNon-static data members with initializers that require "
443 "post-processing\n\t\t");
444 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
446 print_node_brief (file, "", fn, 0);
447 fprintf (file, " ");
449 fprintf (file, "\n");
453 /* Print the stack of unparsed member functions S to FILE. */
455 static void
456 cp_debug_print_unparsed_queues (FILE *file,
457 vec<cp_unparsed_functions_entry, va_gc> *s)
459 unsigned i;
460 cp_unparsed_functions_entry *uf;
462 fprintf (file, "Unparsed functions\n");
463 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
465 fprintf (file, "#%u:\n", i);
466 cp_debug_print_unparsed_function (file, uf);
471 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
472 the given PARSER. If FILE is NULL, the output is printed on stderr. */
474 static void
475 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
477 cp_token *next_token, *first_token, *start_token;
479 if (file == NULL)
480 file = stderr;
482 next_token = parser->lexer->next_token;
483 first_token = parser->lexer->buffer->address ();
484 start_token = (next_token > first_token + window_size / 2)
485 ? next_token - window_size / 2
486 : first_token;
487 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
488 next_token);
492 /* Dump debugging information for the given PARSER. If FILE is NULL,
493 the output is printed on stderr. */
495 void
496 cp_debug_parser (FILE *file, cp_parser *parser)
498 const size_t window_size = 20;
499 cp_token *token;
500 expanded_location eloc;
502 if (file == NULL)
503 file = stderr;
505 fprintf (file, "Parser state\n\n");
506 fprintf (file, "Number of tokens: %u\n",
507 vec_safe_length (parser->lexer->buffer));
508 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
509 cp_debug_print_tree_if_set (file, "Object scope",
510 parser->object_scope);
511 cp_debug_print_tree_if_set (file, "Qualifying scope",
512 parser->qualifying_scope);
513 cp_debug_print_context_stack (file, parser->context);
514 cp_debug_print_flag (file, "Allow GNU extensions",
515 parser->allow_gnu_extensions_p);
516 cp_debug_print_flag (file, "'>' token is greater-than",
517 parser->greater_than_is_operator_p);
518 cp_debug_print_flag (file, "Default args allowed in current "
519 "parameter list", parser->default_arg_ok_p);
520 cp_debug_print_flag (file, "Parsing integral constant-expression",
521 parser->integral_constant_expression_p);
522 cp_debug_print_flag (file, "Allow non-constant expression in current "
523 "constant-expression",
524 parser->allow_non_integral_constant_expression_p);
525 cp_debug_print_flag (file, "Seen non-constant expression",
526 parser->non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
528 "current context",
529 parser->local_variables_forbidden_p);
530 cp_debug_print_flag (file, "In unbraced linkage specification",
531 parser->in_unbraced_linkage_specification_p);
532 cp_debug_print_flag (file, "Parsing a declarator",
533 parser->in_declarator_p);
534 cp_debug_print_flag (file, "In template argument list",
535 parser->in_template_argument_list_p);
536 cp_debug_print_flag (file, "Parsing an iteration statement",
537 parser->in_statement & IN_ITERATION_STMT);
538 cp_debug_print_flag (file, "Parsing a switch statement",
539 parser->in_statement & IN_SWITCH_STMT);
540 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
541 parser->in_statement & IN_OMP_BLOCK);
542 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
543 parser->in_statement & IN_CILK_SIMD_FOR);
544 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
545 parser->in_statement & IN_OMP_FOR);
546 cp_debug_print_flag (file, "Parsing an if statement",
547 parser->in_statement & IN_IF_STMT);
548 cp_debug_print_flag (file, "Parsing a type-id in an expression "
549 "context", parser->in_type_id_in_expr_p);
550 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
551 parser->implicit_extern_c);
552 cp_debug_print_flag (file, "String expressions should be translated "
553 "to execution character set",
554 parser->translate_strings_p);
555 cp_debug_print_flag (file, "Parsing function body outside of a "
556 "local class", parser->in_function_body);
557 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
558 parser->colon_corrects_to_scope_p);
559 cp_debug_print_flag (file, "Colon doesn't start a class definition",
560 parser->colon_doesnt_start_class_def_p);
561 if (parser->type_definition_forbidden_message)
562 fprintf (file, "Error message for forbidden type definitions: %s\n",
563 parser->type_definition_forbidden_message);
564 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
565 fprintf (file, "Number of class definitions in progress: %u\n",
566 parser->num_classes_being_defined);
567 fprintf (file, "Number of template parameter lists for the current "
568 "declaration: %u\n", parser->num_template_parameter_lists);
569 cp_debug_parser_tokens (file, parser, window_size);
570 token = parser->lexer->next_token;
571 fprintf (file, "Next token to parse:\n");
572 fprintf (file, "\tToken: ");
573 cp_lexer_print_token (file, token);
574 eloc = expand_location (token->location);
575 fprintf (file, "\n\tFile: %s\n", eloc.file);
576 fprintf (file, "\tLine: %d\n", eloc.line);
577 fprintf (file, "\tColumn: %d\n", eloc.column);
580 DEBUG_FUNCTION void
581 debug (cp_parser &ref)
583 cp_debug_parser (stderr, &ref);
586 DEBUG_FUNCTION void
587 debug (cp_parser *ptr)
589 if (ptr)
590 debug (*ptr);
591 else
592 fprintf (stderr, "<nil>\n");
595 /* Allocate memory for a new lexer object and return it. */
597 static cp_lexer *
598 cp_lexer_alloc (void)
600 cp_lexer *lexer;
602 c_common_no_more_pch ();
604 /* Allocate the memory. */
605 lexer = ggc_cleared_alloc<cp_lexer> ();
607 /* Initially we are not debugging. */
608 lexer->debugging_p = false;
610 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
612 /* Create the buffer. */
613 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
615 return lexer;
619 /* Create a new main C++ lexer, the lexer that gets tokens from the
620 preprocessor. */
622 static cp_lexer *
623 cp_lexer_new_main (void)
625 cp_lexer *lexer;
626 cp_token token;
628 /* It's possible that parsing the first pragma will load a PCH file,
629 which is a GC collection point. So we have to do that before
630 allocating any memory. */
631 cp_parser_initial_pragma (&token);
633 lexer = cp_lexer_alloc ();
635 /* Put the first token in the buffer. */
636 lexer->buffer->quick_push (token);
638 /* Get the remaining tokens from the preprocessor. */
639 while (token.type != CPP_EOF)
641 cp_lexer_get_preprocessor_token (lexer, &token);
642 vec_safe_push (lexer->buffer, token);
645 lexer->last_token = lexer->buffer->address ()
646 + lexer->buffer->length ()
647 - 1;
648 lexer->next_token = lexer->buffer->length ()
649 ? lexer->buffer->address ()
650 : &eof_token;
652 /* Subsequent preprocessor diagnostics should use compiler
653 diagnostic functions to get the compiler source location. */
654 done_lexing = true;
656 gcc_assert (!lexer->next_token->purged_p);
657 return lexer;
660 /* Create a new lexer whose token stream is primed with the tokens in
661 CACHE. When these tokens are exhausted, no new tokens will be read. */
663 static cp_lexer *
664 cp_lexer_new_from_tokens (cp_token_cache *cache)
666 cp_token *first = cache->first;
667 cp_token *last = cache->last;
668 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
670 /* We do not own the buffer. */
671 lexer->buffer = NULL;
672 lexer->next_token = first == last ? &eof_token : first;
673 lexer->last_token = last;
675 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
677 /* Initially we are not debugging. */
678 lexer->debugging_p = false;
680 gcc_assert (!lexer->next_token->purged_p);
681 return lexer;
684 /* Frees all resources associated with LEXER. */
686 static void
687 cp_lexer_destroy (cp_lexer *lexer)
689 vec_free (lexer->buffer);
690 lexer->saved_tokens.release ();
691 ggc_free (lexer);
694 /* Returns nonzero if debugging information should be output. */
696 static inline bool
697 cp_lexer_debugging_p (cp_lexer *lexer)
699 return lexer->debugging_p;
703 static inline cp_token_position
704 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
706 gcc_assert (!previous_p || lexer->next_token != &eof_token);
708 return lexer->next_token - previous_p;
711 static inline cp_token *
712 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
714 return pos;
717 static inline void
718 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
720 lexer->next_token = cp_lexer_token_at (lexer, pos);
723 static inline cp_token_position
724 cp_lexer_previous_token_position (cp_lexer *lexer)
726 if (lexer->next_token == &eof_token)
727 return lexer->last_token - 1;
728 else
729 return cp_lexer_token_position (lexer, true);
732 static inline cp_token *
733 cp_lexer_previous_token (cp_lexer *lexer)
735 cp_token_position tp = cp_lexer_previous_token_position (lexer);
737 return cp_lexer_token_at (lexer, tp);
740 /* nonzero if we are presently saving tokens. */
742 static inline int
743 cp_lexer_saving_tokens (const cp_lexer* lexer)
745 return lexer->saved_tokens.length () != 0;
748 /* Store the next token from the preprocessor in *TOKEN. Return true
749 if we reach EOF. If LEXER is NULL, assume we are handling an
750 initial #pragma pch_preprocess, and thus want the lexer to return
751 processed strings. */
753 static void
754 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
756 static int is_extern_c = 0;
758 /* Get a new token from the preprocessor. */
759 token->type
760 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
761 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
762 token->keyword = RID_MAX;
763 token->pragma_kind = PRAGMA_NONE;
764 token->purged_p = false;
765 token->error_reported = false;
767 /* On some systems, some header files are surrounded by an
768 implicit extern "C" block. Set a flag in the token if it
769 comes from such a header. */
770 is_extern_c += pending_lang_change;
771 pending_lang_change = 0;
772 token->implicit_extern_c = is_extern_c > 0;
774 /* Check to see if this token is a keyword. */
775 if (token->type == CPP_NAME)
777 if (C_IS_RESERVED_WORD (token->u.value))
779 /* Mark this token as a keyword. */
780 token->type = CPP_KEYWORD;
781 /* Record which keyword. */
782 token->keyword = C_RID_CODE (token->u.value);
784 else
786 if (warn_cxx0x_compat
787 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
788 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
790 /* Warn about the C++0x keyword (but still treat it as
791 an identifier). */
792 warning (OPT_Wc__0x_compat,
793 "identifier %qE is a keyword in C++11",
794 token->u.value);
796 /* Clear out the C_RID_CODE so we don't warn about this
797 particular identifier-turned-keyword again. */
798 C_SET_RID_CODE (token->u.value, RID_MAX);
801 token->keyword = RID_MAX;
804 else if (token->type == CPP_AT_NAME)
806 /* This only happens in Objective-C++; it must be a keyword. */
807 token->type = CPP_KEYWORD;
808 switch (C_RID_CODE (token->u.value))
810 /* Replace 'class' with '@class', 'private' with '@private',
811 etc. This prevents confusion with the C++ keyword
812 'class', and makes the tokens consistent with other
813 Objective-C 'AT' keywords. For example '@class' is
814 reported as RID_AT_CLASS which is consistent with
815 '@synchronized', which is reported as
816 RID_AT_SYNCHRONIZED.
818 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
819 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
820 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
821 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
822 case RID_THROW: token->keyword = RID_AT_THROW; break;
823 case RID_TRY: token->keyword = RID_AT_TRY; break;
824 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
825 default: token->keyword = C_RID_CODE (token->u.value);
828 else if (token->type == CPP_PRAGMA)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token->pragma_kind = ((enum pragma_kind)
832 TREE_INT_CST_LOW (token->u.value));
833 token->u.value = NULL_TREE;
837 /* Update the globals input_location and the input file stack from TOKEN. */
838 static inline void
839 cp_lexer_set_source_position_from_token (cp_token *token)
841 if (token->type != CPP_EOF)
843 input_location = token->location;
847 /* Update the globals input_location and the input file stack from LEXER. */
848 static inline void
849 cp_lexer_set_source_position (cp_lexer *lexer)
851 cp_token *token = cp_lexer_peek_token (lexer);
852 cp_lexer_set_source_position_from_token (token);
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
861 if (cp_lexer_debugging_p (lexer))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
867 return lexer->next_token;
870 /* Return true if the next token has the indicated TYPE. */
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
875 return cp_lexer_peek_token (lexer)->type == type;
878 /* Return true if the next token does not have the indicated TYPE. */
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
883 return !cp_lexer_next_token_is (lexer, type);
886 /* Return true if the next token is the indicated KEYWORD. */
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
894 static inline bool
895 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
897 return cp_lexer_peek_nth_token (lexer, n)->type == type;
900 static inline bool
901 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
903 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
906 /* Return true if the next token is not the indicated KEYWORD. */
908 static inline bool
909 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
911 return cp_lexer_peek_token (lexer)->keyword != keyword;
914 /* Return true if the next token is a keyword for a decl-specifier. */
916 static bool
917 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
919 cp_token *token;
921 token = cp_lexer_peek_token (lexer);
922 switch (token->keyword)
924 /* auto specifier: storage-class-specifier in C++,
925 simple-type-specifier in C++0x. */
926 case RID_AUTO:
927 /* Storage classes. */
928 case RID_REGISTER:
929 case RID_STATIC:
930 case RID_EXTERN:
931 case RID_MUTABLE:
932 case RID_THREAD:
933 /* Elaborated type specifiers. */
934 case RID_ENUM:
935 case RID_CLASS:
936 case RID_STRUCT:
937 case RID_UNION:
938 case RID_TYPENAME:
939 /* Simple type specifiers. */
940 case RID_CHAR:
941 case RID_CHAR16:
942 case RID_CHAR32:
943 case RID_WCHAR:
944 case RID_BOOL:
945 case RID_SHORT:
946 case RID_INT:
947 case RID_LONG:
948 case RID_INT128:
949 case RID_SIGNED:
950 case RID_UNSIGNED:
951 case RID_FLOAT:
952 case RID_DOUBLE:
953 case RID_VOID:
954 /* GNU extensions. */
955 case RID_ATTRIBUTE:
956 case RID_TYPEOF:
957 /* C++0x extensions. */
958 case RID_DECLTYPE:
959 case RID_UNDERLYING_TYPE:
960 return true;
962 default:
963 return false;
967 /* Returns TRUE iff the token T begins a decltype type. */
969 static bool
970 token_is_decltype (cp_token *t)
972 return (t->keyword == RID_DECLTYPE
973 || t->type == CPP_DECLTYPE);
976 /* Returns TRUE iff the next token begins a decltype type. */
978 static bool
979 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
981 cp_token *t = cp_lexer_peek_token (lexer);
982 return token_is_decltype (t);
985 /* Return a pointer to the Nth token in the token stream. If N is 1,
986 then this is precisely equivalent to cp_lexer_peek_token (except
987 that it is not inline). One would like to disallow that case, but
988 there is one case (cp_parser_nth_token_starts_template_id) where
989 the caller passes a variable for N and it might be 1. */
991 static cp_token *
992 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
994 cp_token *token;
996 /* N is 1-based, not zero-based. */
997 gcc_assert (n > 0);
999 if (cp_lexer_debugging_p (lexer))
1000 fprintf (cp_lexer_debug_stream,
1001 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1003 --n;
1004 token = lexer->next_token;
1005 gcc_assert (!n || token != &eof_token);
1006 while (n != 0)
1008 ++token;
1009 if (token == lexer->last_token)
1011 token = &eof_token;
1012 break;
1015 if (!token->purged_p)
1016 --n;
1019 if (cp_lexer_debugging_p (lexer))
1021 cp_lexer_print_token (cp_lexer_debug_stream, token);
1022 putc ('\n', cp_lexer_debug_stream);
1025 return token;
1028 /* Return the next token, and advance the lexer's next_token pointer
1029 to point to the next non-purged token. */
1031 static cp_token *
1032 cp_lexer_consume_token (cp_lexer* lexer)
1034 cp_token *token = lexer->next_token;
1036 gcc_assert (token != &eof_token);
1037 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1041 lexer->next_token++;
1042 if (lexer->next_token == lexer->last_token)
1044 lexer->next_token = &eof_token;
1045 break;
1049 while (lexer->next_token->purged_p);
1051 cp_lexer_set_source_position_from_token (token);
1053 /* Provide debugging output. */
1054 if (cp_lexer_debugging_p (lexer))
1056 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1057 cp_lexer_print_token (cp_lexer_debug_stream, token);
1058 putc ('\n', cp_lexer_debug_stream);
1061 return token;
1064 /* Permanently remove the next token from the token stream, and
1065 advance the next_token pointer to refer to the next non-purged
1066 token. */
1068 static void
1069 cp_lexer_purge_token (cp_lexer *lexer)
1071 cp_token *tok = lexer->next_token;
1073 gcc_assert (tok != &eof_token);
1074 tok->purged_p = true;
1075 tok->location = UNKNOWN_LOCATION;
1076 tok->u.value = NULL_TREE;
1077 tok->keyword = RID_MAX;
1081 tok++;
1082 if (tok == lexer->last_token)
1084 tok = &eof_token;
1085 break;
1088 while (tok->purged_p);
1089 lexer->next_token = tok;
1092 /* Permanently remove all tokens after TOK, up to, but not
1093 including, the token that will be returned next by
1094 cp_lexer_peek_token. */
1096 static void
1097 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1099 cp_token *peek = lexer->next_token;
1101 if (peek == &eof_token)
1102 peek = lexer->last_token;
1104 gcc_assert (tok < peek);
1106 for ( tok += 1; tok != peek; tok += 1)
1108 tok->purged_p = true;
1109 tok->location = UNKNOWN_LOCATION;
1110 tok->u.value = NULL_TREE;
1111 tok->keyword = RID_MAX;
1115 /* Begin saving tokens. All tokens consumed after this point will be
1116 preserved. */
1118 static void
1119 cp_lexer_save_tokens (cp_lexer* lexer)
1121 /* Provide debugging output. */
1122 if (cp_lexer_debugging_p (lexer))
1123 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1125 lexer->saved_tokens.safe_push (lexer->next_token);
1128 /* Commit to the portion of the token stream most recently saved. */
1130 static void
1131 cp_lexer_commit_tokens (cp_lexer* lexer)
1133 /* Provide debugging output. */
1134 if (cp_lexer_debugging_p (lexer))
1135 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1137 lexer->saved_tokens.pop ();
1140 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1141 to the token stream. Stop saving tokens. */
1143 static void
1144 cp_lexer_rollback_tokens (cp_lexer* lexer)
1146 /* Provide debugging output. */
1147 if (cp_lexer_debugging_p (lexer))
1148 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1150 lexer->next_token = lexer->saved_tokens.pop ();
1153 /* Print a representation of the TOKEN on the STREAM. */
1155 static void
1156 cp_lexer_print_token (FILE * stream, cp_token *token)
1158 /* We don't use cpp_type2name here because the parser defines
1159 a few tokens of its own. */
1160 static const char *const token_names[] = {
1161 /* cpplib-defined token types */
1162 #define OP(e, s) #e,
1163 #define TK(e, s) #e,
1164 TTYPE_TABLE
1165 #undef OP
1166 #undef TK
1167 /* C++ parser token types - see "Manifest constants", above. */
1168 "KEYWORD",
1169 "TEMPLATE_ID",
1170 "NESTED_NAME_SPECIFIER",
1173 /* For some tokens, print the associated data. */
1174 switch (token->type)
1176 case CPP_KEYWORD:
1177 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1178 For example, `struct' is mapped to an INTEGER_CST. */
1179 if (!identifier_p (token->u.value))
1180 break;
1181 /* else fall through */
1182 case CPP_NAME:
1183 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1184 break;
1186 case CPP_STRING:
1187 case CPP_STRING16:
1188 case CPP_STRING32:
1189 case CPP_WSTRING:
1190 case CPP_UTF8STRING:
1191 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1192 break;
1194 case CPP_NUMBER:
1195 print_generic_expr (stream, token->u.value, 0);
1196 break;
1198 default:
1199 /* If we have a name for the token, print it out. Otherwise, we
1200 simply give the numeric code. */
1201 if (token->type < ARRAY_SIZE(token_names))
1202 fputs (token_names[token->type], stream);
1203 else
1204 fprintf (stream, "[%d]", token->type);
1205 break;
1209 DEBUG_FUNCTION void
1210 debug (cp_token &ref)
1212 cp_lexer_print_token (stderr, &ref);
1213 fprintf (stderr, "\n");
1216 DEBUG_FUNCTION void
1217 debug (cp_token *ptr)
1219 if (ptr)
1220 debug (*ptr);
1221 else
1222 fprintf (stderr, "<nil>\n");
1226 /* Start emitting debugging information. */
1228 static void
1229 cp_lexer_start_debugging (cp_lexer* lexer)
1231 lexer->debugging_p = true;
1232 cp_lexer_debug_stream = stderr;
1235 /* Stop emitting debugging information. */
1237 static void
1238 cp_lexer_stop_debugging (cp_lexer* lexer)
1240 lexer->debugging_p = false;
1241 cp_lexer_debug_stream = NULL;
1244 /* Create a new cp_token_cache, representing a range of tokens. */
1246 static cp_token_cache *
1247 cp_token_cache_new (cp_token *first, cp_token *last)
1249 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1250 cache->first = first;
1251 cache->last = last;
1252 return cache;
1255 /* Diagnose if #pragma omp declare simd isn't followed immediately
1256 by function declaration or definition. */
1258 static inline void
1259 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1261 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1263 error ("%<#pragma omp declare simd%> not immediately followed by "
1264 "function declaration or definition");
1265 parser->omp_declare_simd = NULL;
1269 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1270 and put that into "omp declare simd" attribute. */
1272 static inline void
1273 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1275 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1277 if (fndecl == error_mark_node)
1279 parser->omp_declare_simd = NULL;
1280 return;
1282 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1284 cp_ensure_no_omp_declare_simd (parser);
1285 return;
1290 /* Decl-specifiers. */
1292 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1294 static void
1295 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1297 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1300 /* Declarators. */
1302 /* Nothing other than the parser should be creating declarators;
1303 declarators are a semi-syntactic representation of C++ entities.
1304 Other parts of the front end that need to create entities (like
1305 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1307 static cp_declarator *make_call_declarator
1308 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1309 static cp_declarator *make_array_declarator
1310 (cp_declarator *, tree);
1311 static cp_declarator *make_pointer_declarator
1312 (cp_cv_quals, cp_declarator *, tree);
1313 static cp_declarator *make_reference_declarator
1314 (cp_cv_quals, cp_declarator *, bool, tree);
1315 static cp_parameter_declarator *make_parameter_declarator
1316 (cp_decl_specifier_seq *, cp_declarator *, tree);
1317 static cp_declarator *make_ptrmem_declarator
1318 (cp_cv_quals, tree, cp_declarator *, tree);
1320 /* An erroneous declarator. */
1321 static cp_declarator *cp_error_declarator;
1323 /* The obstack on which declarators and related data structures are
1324 allocated. */
1325 static struct obstack declarator_obstack;
1327 /* Alloc BYTES from the declarator memory pool. */
1329 static inline void *
1330 alloc_declarator (size_t bytes)
1332 return obstack_alloc (&declarator_obstack, bytes);
1335 /* Allocate a declarator of the indicated KIND. Clear fields that are
1336 common to all declarators. */
1338 static cp_declarator *
1339 make_declarator (cp_declarator_kind kind)
1341 cp_declarator *declarator;
1343 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1344 declarator->kind = kind;
1345 declarator->attributes = NULL_TREE;
1346 declarator->std_attributes = NULL_TREE;
1347 declarator->declarator = NULL;
1348 declarator->parameter_pack_p = false;
1349 declarator->id_loc = UNKNOWN_LOCATION;
1351 return declarator;
1354 /* Make a declarator for a generalized identifier. If
1355 QUALIFYING_SCOPE is non-NULL, the identifier is
1356 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1357 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1358 is, if any. */
1360 static cp_declarator *
1361 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1362 special_function_kind sfk)
1364 cp_declarator *declarator;
1366 /* It is valid to write:
1368 class C { void f(); };
1369 typedef C D;
1370 void D::f();
1372 The standard is not clear about whether `typedef const C D' is
1373 legal; as of 2002-09-15 the committee is considering that
1374 question. EDG 3.0 allows that syntax. Therefore, we do as
1375 well. */
1376 if (qualifying_scope && TYPE_P (qualifying_scope))
1377 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1379 gcc_assert (identifier_p (unqualified_name)
1380 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1381 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1383 declarator = make_declarator (cdk_id);
1384 declarator->u.id.qualifying_scope = qualifying_scope;
1385 declarator->u.id.unqualified_name = unqualified_name;
1386 declarator->u.id.sfk = sfk;
1388 return declarator;
1391 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1392 of modifiers such as const or volatile to apply to the pointer
1393 type, represented as identifiers. ATTRIBUTES represent the attributes that
1394 appertain to the pointer or reference. */
1396 cp_declarator *
1397 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1398 tree attributes)
1400 cp_declarator *declarator;
1402 declarator = make_declarator (cdk_pointer);
1403 declarator->declarator = target;
1404 declarator->u.pointer.qualifiers = cv_qualifiers;
1405 declarator->u.pointer.class_type = NULL_TREE;
1406 if (target)
1408 declarator->id_loc = target->id_loc;
1409 declarator->parameter_pack_p = target->parameter_pack_p;
1410 target->parameter_pack_p = false;
1412 else
1413 declarator->parameter_pack_p = false;
1415 declarator->std_attributes = attributes;
1417 return declarator;
1420 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1421 represent the attributes that appertain to the pointer or
1422 reference. */
1424 cp_declarator *
1425 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1426 bool rvalue_ref, tree attributes)
1428 cp_declarator *declarator;
1430 declarator = make_declarator (cdk_reference);
1431 declarator->declarator = target;
1432 declarator->u.reference.qualifiers = cv_qualifiers;
1433 declarator->u.reference.rvalue_ref = rvalue_ref;
1434 if (target)
1436 declarator->id_loc = target->id_loc;
1437 declarator->parameter_pack_p = target->parameter_pack_p;
1438 target->parameter_pack_p = false;
1440 else
1441 declarator->parameter_pack_p = false;
1443 declarator->std_attributes = attributes;
1445 return declarator;
1448 /* Like make_pointer_declarator -- but for a pointer to a non-static
1449 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1450 appertain to the pointer or reference. */
1452 cp_declarator *
1453 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1454 cp_declarator *pointee,
1455 tree attributes)
1457 cp_declarator *declarator;
1459 declarator = make_declarator (cdk_ptrmem);
1460 declarator->declarator = pointee;
1461 declarator->u.pointer.qualifiers = cv_qualifiers;
1462 declarator->u.pointer.class_type = class_type;
1464 if (pointee)
1466 declarator->parameter_pack_p = pointee->parameter_pack_p;
1467 pointee->parameter_pack_p = false;
1469 else
1470 declarator->parameter_pack_p = false;
1472 declarator->std_attributes = attributes;
1474 return declarator;
1477 /* Make a declarator for the function given by TARGET, with the
1478 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1479 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1480 indicates what exceptions can be thrown. */
1482 cp_declarator *
1483 make_call_declarator (cp_declarator *target,
1484 tree parms,
1485 cp_cv_quals cv_qualifiers,
1486 cp_virt_specifiers virt_specifiers,
1487 cp_ref_qualifier ref_qualifier,
1488 tree exception_specification,
1489 tree late_return_type)
1491 cp_declarator *declarator;
1493 declarator = make_declarator (cdk_function);
1494 declarator->declarator = target;
1495 declarator->u.function.parameters = parms;
1496 declarator->u.function.qualifiers = cv_qualifiers;
1497 declarator->u.function.virt_specifiers = virt_specifiers;
1498 declarator->u.function.ref_qualifier = ref_qualifier;
1499 declarator->u.function.exception_specification = exception_specification;
1500 declarator->u.function.late_return_type = late_return_type;
1501 if (target)
1503 declarator->id_loc = target->id_loc;
1504 declarator->parameter_pack_p = target->parameter_pack_p;
1505 target->parameter_pack_p = false;
1507 else
1508 declarator->parameter_pack_p = false;
1510 return declarator;
1513 /* Make a declarator for an array of BOUNDS elements, each of which is
1514 defined by ELEMENT. */
1516 cp_declarator *
1517 make_array_declarator (cp_declarator *element, tree bounds)
1519 cp_declarator *declarator;
1521 declarator = make_declarator (cdk_array);
1522 declarator->declarator = element;
1523 declarator->u.array.bounds = bounds;
1524 if (element)
1526 declarator->id_loc = element->id_loc;
1527 declarator->parameter_pack_p = element->parameter_pack_p;
1528 element->parameter_pack_p = false;
1530 else
1531 declarator->parameter_pack_p = false;
1533 return declarator;
1536 /* Determine whether the declarator we've seen so far can be a
1537 parameter pack, when followed by an ellipsis. */
1538 static bool
1539 declarator_can_be_parameter_pack (cp_declarator *declarator)
1541 /* Search for a declarator name, or any other declarator that goes
1542 after the point where the ellipsis could appear in a parameter
1543 pack. If we find any of these, then this declarator can not be
1544 made into a parameter pack. */
1545 bool found = false;
1546 while (declarator && !found)
1548 switch ((int)declarator->kind)
1550 case cdk_id:
1551 case cdk_array:
1552 found = true;
1553 break;
1555 case cdk_error:
1556 return true;
1558 default:
1559 declarator = declarator->declarator;
1560 break;
1564 return !found;
1567 cp_parameter_declarator *no_parameters;
1569 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1570 DECLARATOR and DEFAULT_ARGUMENT. */
1572 cp_parameter_declarator *
1573 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1574 cp_declarator *declarator,
1575 tree default_argument)
1577 cp_parameter_declarator *parameter;
1579 parameter = ((cp_parameter_declarator *)
1580 alloc_declarator (sizeof (cp_parameter_declarator)));
1581 parameter->next = NULL;
1582 if (decl_specifiers)
1583 parameter->decl_specifiers = *decl_specifiers;
1584 else
1585 clear_decl_specs (&parameter->decl_specifiers);
1586 parameter->declarator = declarator;
1587 parameter->default_argument = default_argument;
1588 parameter->ellipsis_p = false;
1590 return parameter;
1593 /* Returns true iff DECLARATOR is a declaration for a function. */
1595 static bool
1596 function_declarator_p (const cp_declarator *declarator)
1598 while (declarator)
1600 if (declarator->kind == cdk_function
1601 && declarator->declarator->kind == cdk_id)
1602 return true;
1603 if (declarator->kind == cdk_id
1604 || declarator->kind == cdk_error)
1605 return false;
1606 declarator = declarator->declarator;
1608 return false;
1611 /* The parser. */
1613 /* Overview
1614 --------
1616 A cp_parser parses the token stream as specified by the C++
1617 grammar. Its job is purely parsing, not semantic analysis. For
1618 example, the parser breaks the token stream into declarators,
1619 expressions, statements, and other similar syntactic constructs.
1620 It does not check that the types of the expressions on either side
1621 of an assignment-statement are compatible, or that a function is
1622 not declared with a parameter of type `void'.
1624 The parser invokes routines elsewhere in the compiler to perform
1625 semantic analysis and to build up the abstract syntax tree for the
1626 code processed.
1628 The parser (and the template instantiation code, which is, in a
1629 way, a close relative of parsing) are the only parts of the
1630 compiler that should be calling push_scope and pop_scope, or
1631 related functions. The parser (and template instantiation code)
1632 keeps track of what scope is presently active; everything else
1633 should simply honor that. (The code that generates static
1634 initializers may also need to set the scope, in order to check
1635 access control correctly when emitting the initializers.)
1637 Methodology
1638 -----------
1640 The parser is of the standard recursive-descent variety. Upcoming
1641 tokens in the token stream are examined in order to determine which
1642 production to use when parsing a non-terminal. Some C++ constructs
1643 require arbitrary look ahead to disambiguate. For example, it is
1644 impossible, in the general case, to tell whether a statement is an
1645 expression or declaration without scanning the entire statement.
1646 Therefore, the parser is capable of "parsing tentatively." When the
1647 parser is not sure what construct comes next, it enters this mode.
1648 Then, while we attempt to parse the construct, the parser queues up
1649 error messages, rather than issuing them immediately, and saves the
1650 tokens it consumes. If the construct is parsed successfully, the
1651 parser "commits", i.e., it issues any queued error messages and
1652 the tokens that were being preserved are permanently discarded.
1653 If, however, the construct is not parsed successfully, the parser
1654 rolls back its state completely so that it can resume parsing using
1655 a different alternative.
1657 Future Improvements
1658 -------------------
1660 The performance of the parser could probably be improved substantially.
1661 We could often eliminate the need to parse tentatively by looking ahead
1662 a little bit. In some places, this approach might not entirely eliminate
1663 the need to parse tentatively, but it might still speed up the average
1664 case. */
1666 /* Flags that are passed to some parsing functions. These values can
1667 be bitwise-ored together. */
1669 enum
1671 /* No flags. */
1672 CP_PARSER_FLAGS_NONE = 0x0,
1673 /* The construct is optional. If it is not present, then no error
1674 should be issued. */
1675 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1676 /* When parsing a type-specifier, treat user-defined type-names
1677 as non-type identifiers. */
1678 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1679 /* When parsing a type-specifier, do not try to parse a class-specifier
1680 or enum-specifier. */
1681 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1682 /* When parsing a decl-specifier-seq, only allow type-specifier or
1683 constexpr. */
1684 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1687 /* This type is used for parameters and variables which hold
1688 combinations of the above flags. */
1689 typedef int cp_parser_flags;
1691 /* The different kinds of declarators we want to parse. */
1693 typedef enum cp_parser_declarator_kind
1695 /* We want an abstract declarator. */
1696 CP_PARSER_DECLARATOR_ABSTRACT,
1697 /* We want a named declarator. */
1698 CP_PARSER_DECLARATOR_NAMED,
1699 /* We don't mind, but the name must be an unqualified-id. */
1700 CP_PARSER_DECLARATOR_EITHER
1701 } cp_parser_declarator_kind;
1703 /* The precedence values used to parse binary expressions. The minimum value
1704 of PREC must be 1, because zero is reserved to quickly discriminate
1705 binary operators from other tokens. */
1707 enum cp_parser_prec
1709 PREC_NOT_OPERATOR,
1710 PREC_LOGICAL_OR_EXPRESSION,
1711 PREC_LOGICAL_AND_EXPRESSION,
1712 PREC_INCLUSIVE_OR_EXPRESSION,
1713 PREC_EXCLUSIVE_OR_EXPRESSION,
1714 PREC_AND_EXPRESSION,
1715 PREC_EQUALITY_EXPRESSION,
1716 PREC_RELATIONAL_EXPRESSION,
1717 PREC_SHIFT_EXPRESSION,
1718 PREC_ADDITIVE_EXPRESSION,
1719 PREC_MULTIPLICATIVE_EXPRESSION,
1720 PREC_PM_EXPRESSION,
1721 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1724 /* A mapping from a token type to a corresponding tree node type, with a
1725 precedence value. */
1727 typedef struct cp_parser_binary_operations_map_node
1729 /* The token type. */
1730 enum cpp_ttype token_type;
1731 /* The corresponding tree code. */
1732 enum tree_code tree_type;
1733 /* The precedence of this operator. */
1734 enum cp_parser_prec prec;
1735 } cp_parser_binary_operations_map_node;
1737 typedef struct cp_parser_expression_stack_entry
1739 /* Left hand side of the binary operation we are currently
1740 parsing. */
1741 tree lhs;
1742 /* Original tree code for left hand side, if it was a binary
1743 expression itself (used for -Wparentheses). */
1744 enum tree_code lhs_type;
1745 /* Tree code for the binary operation we are parsing. */
1746 enum tree_code tree_type;
1747 /* Precedence of the binary operation we are parsing. */
1748 enum cp_parser_prec prec;
1749 /* Location of the binary operation we are parsing. */
1750 location_t loc;
1751 } cp_parser_expression_stack_entry;
1753 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1754 entries because precedence levels on the stack are monotonically
1755 increasing. */
1756 typedef struct cp_parser_expression_stack_entry
1757 cp_parser_expression_stack[NUM_PREC_VALUES];
1759 /* Prototypes. */
1761 /* Constructors and destructors. */
1763 static cp_parser_context *cp_parser_context_new
1764 (cp_parser_context *);
1766 /* Class variables. */
1768 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1770 /* The operator-precedence table used by cp_parser_binary_expression.
1771 Transformed into an associative array (binops_by_token) by
1772 cp_parser_new. */
1774 static const cp_parser_binary_operations_map_node binops[] = {
1775 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1776 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1778 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1779 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1780 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1782 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1783 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1785 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1786 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1788 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1789 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1790 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1791 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1793 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1794 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1796 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1798 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1800 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1802 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1804 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1807 /* The same as binops, but initialized by cp_parser_new so that
1808 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1809 for speed. */
1810 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1812 /* Constructors and destructors. */
1814 /* Construct a new context. The context below this one on the stack
1815 is given by NEXT. */
1817 static cp_parser_context *
1818 cp_parser_context_new (cp_parser_context* next)
1820 cp_parser_context *context;
1822 /* Allocate the storage. */
1823 if (cp_parser_context_free_list != NULL)
1825 /* Pull the first entry from the free list. */
1826 context = cp_parser_context_free_list;
1827 cp_parser_context_free_list = context->next;
1828 memset (context, 0, sizeof (*context));
1830 else
1831 context = ggc_cleared_alloc<cp_parser_context> ();
1833 /* No errors have occurred yet in this context. */
1834 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1835 /* If this is not the bottommost context, copy information that we
1836 need from the previous context. */
1837 if (next)
1839 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1840 expression, then we are parsing one in this context, too. */
1841 context->object_type = next->object_type;
1842 /* Thread the stack. */
1843 context->next = next;
1846 return context;
1849 /* Managing the unparsed function queues. */
1851 #define unparsed_funs_with_default_args \
1852 parser->unparsed_queues->last ().funs_with_default_args
1853 #define unparsed_funs_with_definitions \
1854 parser->unparsed_queues->last ().funs_with_definitions
1855 #define unparsed_nsdmis \
1856 parser->unparsed_queues->last ().nsdmis
1857 #define unparsed_classes \
1858 parser->unparsed_queues->last ().classes
1860 static void
1861 push_unparsed_function_queues (cp_parser *parser)
1863 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1864 vec_safe_push (parser->unparsed_queues, e);
1867 static void
1868 pop_unparsed_function_queues (cp_parser *parser)
1870 release_tree_vector (unparsed_funs_with_definitions);
1871 parser->unparsed_queues->pop ();
1874 /* Prototypes. */
1876 /* Constructors and destructors. */
1878 static cp_parser *cp_parser_new
1879 (void);
1881 /* Routines to parse various constructs.
1883 Those that return `tree' will return the error_mark_node (rather
1884 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1885 Sometimes, they will return an ordinary node if error-recovery was
1886 attempted, even though a parse error occurred. So, to check
1887 whether or not a parse error occurred, you should always use
1888 cp_parser_error_occurred. If the construct is optional (indicated
1889 either by an `_opt' in the name of the function that does the
1890 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1891 the construct is not present. */
1893 /* Lexical conventions [gram.lex] */
1895 static tree cp_parser_identifier
1896 (cp_parser *);
1897 static tree cp_parser_string_literal
1898 (cp_parser *, bool, bool, bool);
1899 static tree cp_parser_userdef_char_literal
1900 (cp_parser *);
1901 static tree cp_parser_userdef_string_literal
1902 (tree);
1903 static tree cp_parser_userdef_numeric_literal
1904 (cp_parser *);
1906 /* Basic concepts [gram.basic] */
1908 static bool cp_parser_translation_unit
1909 (cp_parser *);
1911 /* Expressions [gram.expr] */
1913 static tree cp_parser_primary_expression
1914 (cp_parser *, bool, bool, bool, cp_id_kind *);
1915 static tree cp_parser_id_expression
1916 (cp_parser *, bool, bool, bool *, bool, bool);
1917 static tree cp_parser_unqualified_id
1918 (cp_parser *, bool, bool, bool, bool);
1919 static tree cp_parser_nested_name_specifier_opt
1920 (cp_parser *, bool, bool, bool, bool);
1921 static tree cp_parser_nested_name_specifier
1922 (cp_parser *, bool, bool, bool, bool);
1923 static tree cp_parser_qualifying_entity
1924 (cp_parser *, bool, bool, bool, bool, bool);
1925 static tree cp_parser_postfix_expression
1926 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1927 static tree cp_parser_postfix_open_square_expression
1928 (cp_parser *, tree, bool, bool);
1929 static tree cp_parser_postfix_dot_deref_expression
1930 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1931 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1932 (cp_parser *, int, bool, bool, bool *, bool = false);
1933 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1934 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1935 static void cp_parser_pseudo_destructor_name
1936 (cp_parser *, tree, tree *, tree *);
1937 static tree cp_parser_unary_expression
1938 (cp_parser *, bool, bool, cp_id_kind *);
1939 static enum tree_code cp_parser_unary_operator
1940 (cp_token *);
1941 static tree cp_parser_new_expression
1942 (cp_parser *);
1943 static vec<tree, va_gc> *cp_parser_new_placement
1944 (cp_parser *);
1945 static tree cp_parser_new_type_id
1946 (cp_parser *, tree *);
1947 static cp_declarator *cp_parser_new_declarator_opt
1948 (cp_parser *);
1949 static cp_declarator *cp_parser_direct_new_declarator
1950 (cp_parser *);
1951 static vec<tree, va_gc> *cp_parser_new_initializer
1952 (cp_parser *);
1953 static tree cp_parser_delete_expression
1954 (cp_parser *);
1955 static tree cp_parser_cast_expression
1956 (cp_parser *, bool, bool, bool, cp_id_kind *);
1957 static tree cp_parser_binary_expression
1958 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1959 static tree cp_parser_question_colon_clause
1960 (cp_parser *, tree);
1961 static tree cp_parser_assignment_expression
1962 (cp_parser *, bool, cp_id_kind *);
1963 static enum tree_code cp_parser_assignment_operator_opt
1964 (cp_parser *);
1965 static tree cp_parser_expression
1966 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
1967 static tree cp_parser_constant_expression
1968 (cp_parser *, bool, bool *);
1969 static tree cp_parser_builtin_offsetof
1970 (cp_parser *);
1971 static tree cp_parser_lambda_expression
1972 (cp_parser *);
1973 static void cp_parser_lambda_introducer
1974 (cp_parser *, tree);
1975 static bool cp_parser_lambda_declarator_opt
1976 (cp_parser *, tree);
1977 static void cp_parser_lambda_body
1978 (cp_parser *, tree);
1980 /* Statements [gram.stmt.stmt] */
1982 static void cp_parser_statement
1983 (cp_parser *, tree, bool, bool *);
1984 static void cp_parser_label_for_labeled_statement
1985 (cp_parser *, tree);
1986 static tree cp_parser_expression_statement
1987 (cp_parser *, tree);
1988 static tree cp_parser_compound_statement
1989 (cp_parser *, tree, bool, bool);
1990 static void cp_parser_statement_seq_opt
1991 (cp_parser *, tree);
1992 static tree cp_parser_selection_statement
1993 (cp_parser *, bool *);
1994 static tree cp_parser_condition
1995 (cp_parser *);
1996 static tree cp_parser_iteration_statement
1997 (cp_parser *, bool);
1998 static bool cp_parser_for_init_statement
1999 (cp_parser *, tree *decl);
2000 static tree cp_parser_for
2001 (cp_parser *, bool);
2002 static tree cp_parser_c_for
2003 (cp_parser *, tree, tree, bool);
2004 static tree cp_parser_range_for
2005 (cp_parser *, tree, tree, tree, bool);
2006 static void do_range_for_auto_deduction
2007 (tree, tree);
2008 static tree cp_parser_perform_range_for_lookup
2009 (tree, tree *, tree *);
2010 static tree cp_parser_range_for_member_function
2011 (tree, tree);
2012 static tree cp_parser_jump_statement
2013 (cp_parser *);
2014 static void cp_parser_declaration_statement
2015 (cp_parser *);
2017 static tree cp_parser_implicitly_scoped_statement
2018 (cp_parser *, bool *);
2019 static void cp_parser_already_scoped_statement
2020 (cp_parser *);
2022 /* Declarations [gram.dcl.dcl] */
2024 static void cp_parser_declaration_seq_opt
2025 (cp_parser *);
2026 static void cp_parser_declaration
2027 (cp_parser *);
2028 static void cp_parser_block_declaration
2029 (cp_parser *, bool);
2030 static void cp_parser_simple_declaration
2031 (cp_parser *, bool, tree *);
2032 static void cp_parser_decl_specifier_seq
2033 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2034 static tree cp_parser_storage_class_specifier_opt
2035 (cp_parser *);
2036 static tree cp_parser_function_specifier_opt
2037 (cp_parser *, cp_decl_specifier_seq *);
2038 static tree cp_parser_type_specifier
2039 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2040 int *, bool *);
2041 static tree cp_parser_simple_type_specifier
2042 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2043 static tree cp_parser_type_name
2044 (cp_parser *);
2045 static tree cp_parser_nonclass_name
2046 (cp_parser* parser);
2047 static tree cp_parser_elaborated_type_specifier
2048 (cp_parser *, bool, bool);
2049 static tree cp_parser_enum_specifier
2050 (cp_parser *);
2051 static void cp_parser_enumerator_list
2052 (cp_parser *, tree);
2053 static void cp_parser_enumerator_definition
2054 (cp_parser *, tree);
2055 static tree cp_parser_namespace_name
2056 (cp_parser *);
2057 static void cp_parser_namespace_definition
2058 (cp_parser *);
2059 static void cp_parser_namespace_body
2060 (cp_parser *);
2061 static tree cp_parser_qualified_namespace_specifier
2062 (cp_parser *);
2063 static void cp_parser_namespace_alias_definition
2064 (cp_parser *);
2065 static bool cp_parser_using_declaration
2066 (cp_parser *, bool);
2067 static void cp_parser_using_directive
2068 (cp_parser *);
2069 static tree cp_parser_alias_declaration
2070 (cp_parser *);
2071 static void cp_parser_asm_definition
2072 (cp_parser *);
2073 static void cp_parser_linkage_specification
2074 (cp_parser *);
2075 static void cp_parser_static_assert
2076 (cp_parser *, bool);
2077 static tree cp_parser_decltype
2078 (cp_parser *);
2080 /* Declarators [gram.dcl.decl] */
2082 static tree cp_parser_init_declarator
2083 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2084 static cp_declarator *cp_parser_declarator
2085 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2086 static cp_declarator *cp_parser_direct_declarator
2087 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2088 static enum tree_code cp_parser_ptr_operator
2089 (cp_parser *, tree *, cp_cv_quals *, tree *);
2090 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2091 (cp_parser *);
2092 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2093 (cp_parser *);
2094 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2095 (cp_parser *);
2096 static tree cp_parser_late_return_type_opt
2097 (cp_parser *, cp_declarator *, cp_cv_quals);
2098 static tree cp_parser_declarator_id
2099 (cp_parser *, bool);
2100 static tree cp_parser_type_id
2101 (cp_parser *);
2102 static tree cp_parser_template_type_arg
2103 (cp_parser *);
2104 static tree cp_parser_trailing_type_id (cp_parser *);
2105 static tree cp_parser_type_id_1
2106 (cp_parser *, bool, bool);
2107 static void cp_parser_type_specifier_seq
2108 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2109 static tree cp_parser_parameter_declaration_clause
2110 (cp_parser *);
2111 static tree cp_parser_parameter_declaration_list
2112 (cp_parser *, bool *);
2113 static cp_parameter_declarator *cp_parser_parameter_declaration
2114 (cp_parser *, bool, bool *);
2115 static tree cp_parser_default_argument
2116 (cp_parser *, bool);
2117 static void cp_parser_function_body
2118 (cp_parser *, bool);
2119 static tree cp_parser_initializer
2120 (cp_parser *, bool *, bool *);
2121 static tree cp_parser_initializer_clause
2122 (cp_parser *, bool *);
2123 static tree cp_parser_braced_list
2124 (cp_parser*, bool*);
2125 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2126 (cp_parser *, bool *);
2128 static bool cp_parser_ctor_initializer_opt_and_function_body
2129 (cp_parser *, bool);
2131 static tree cp_parser_late_parsing_omp_declare_simd
2132 (cp_parser *, tree);
2134 static tree cp_parser_late_parsing_cilk_simd_fn_info
2135 (cp_parser *, tree);
2137 static tree synthesize_implicit_template_parm
2138 (cp_parser *);
2139 static tree finish_fully_implicit_template
2140 (cp_parser *, tree);
2142 /* Classes [gram.class] */
2144 static tree cp_parser_class_name
2145 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2146 static tree cp_parser_class_specifier
2147 (cp_parser *);
2148 static tree cp_parser_class_head
2149 (cp_parser *, bool *);
2150 static enum tag_types cp_parser_class_key
2151 (cp_parser *);
2152 static void cp_parser_type_parameter_key
2153 (cp_parser* parser);
2154 static void cp_parser_member_specification_opt
2155 (cp_parser *);
2156 static void cp_parser_member_declaration
2157 (cp_parser *);
2158 static tree cp_parser_pure_specifier
2159 (cp_parser *);
2160 static tree cp_parser_constant_initializer
2161 (cp_parser *);
2163 /* Derived classes [gram.class.derived] */
2165 static tree cp_parser_base_clause
2166 (cp_parser *);
2167 static tree cp_parser_base_specifier
2168 (cp_parser *);
2170 /* Special member functions [gram.special] */
2172 static tree cp_parser_conversion_function_id
2173 (cp_parser *);
2174 static tree cp_parser_conversion_type_id
2175 (cp_parser *);
2176 static cp_declarator *cp_parser_conversion_declarator_opt
2177 (cp_parser *);
2178 static bool cp_parser_ctor_initializer_opt
2179 (cp_parser *);
2180 static void cp_parser_mem_initializer_list
2181 (cp_parser *);
2182 static tree cp_parser_mem_initializer
2183 (cp_parser *);
2184 static tree cp_parser_mem_initializer_id
2185 (cp_parser *);
2187 /* Overloading [gram.over] */
2189 static tree cp_parser_operator_function_id
2190 (cp_parser *);
2191 static tree cp_parser_operator
2192 (cp_parser *);
2194 /* Templates [gram.temp] */
2196 static void cp_parser_template_declaration
2197 (cp_parser *, bool);
2198 static tree cp_parser_template_parameter_list
2199 (cp_parser *);
2200 static tree cp_parser_template_parameter
2201 (cp_parser *, bool *, bool *);
2202 static tree cp_parser_type_parameter
2203 (cp_parser *, bool *);
2204 static tree cp_parser_template_id
2205 (cp_parser *, bool, bool, enum tag_types, bool);
2206 static tree cp_parser_template_name
2207 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2208 static tree cp_parser_template_argument_list
2209 (cp_parser *);
2210 static tree cp_parser_template_argument
2211 (cp_parser *);
2212 static void cp_parser_explicit_instantiation
2213 (cp_parser *);
2214 static void cp_parser_explicit_specialization
2215 (cp_parser *);
2217 /* Exception handling [gram.exception] */
2219 static tree cp_parser_try_block
2220 (cp_parser *);
2221 static bool cp_parser_function_try_block
2222 (cp_parser *);
2223 static void cp_parser_handler_seq
2224 (cp_parser *);
2225 static void cp_parser_handler
2226 (cp_parser *);
2227 static tree cp_parser_exception_declaration
2228 (cp_parser *);
2229 static tree cp_parser_throw_expression
2230 (cp_parser *);
2231 static tree cp_parser_exception_specification_opt
2232 (cp_parser *);
2233 static tree cp_parser_type_id_list
2234 (cp_parser *);
2236 /* GNU Extensions */
2238 static tree cp_parser_asm_specification_opt
2239 (cp_parser *);
2240 static tree cp_parser_asm_operand_list
2241 (cp_parser *);
2242 static tree cp_parser_asm_clobber_list
2243 (cp_parser *);
2244 static tree cp_parser_asm_label_list
2245 (cp_parser *);
2246 static bool cp_next_tokens_can_be_attribute_p
2247 (cp_parser *);
2248 static bool cp_next_tokens_can_be_gnu_attribute_p
2249 (cp_parser *);
2250 static bool cp_next_tokens_can_be_std_attribute_p
2251 (cp_parser *);
2252 static bool cp_nth_tokens_can_be_std_attribute_p
2253 (cp_parser *, size_t);
2254 static bool cp_nth_tokens_can_be_gnu_attribute_p
2255 (cp_parser *, size_t);
2256 static bool cp_nth_tokens_can_be_attribute_p
2257 (cp_parser *, size_t);
2258 static tree cp_parser_attributes_opt
2259 (cp_parser *);
2260 static tree cp_parser_gnu_attributes_opt
2261 (cp_parser *);
2262 static tree cp_parser_gnu_attribute_list
2263 (cp_parser *);
2264 static tree cp_parser_std_attribute
2265 (cp_parser *);
2266 static tree cp_parser_std_attribute_spec
2267 (cp_parser *);
2268 static tree cp_parser_std_attribute_spec_seq
2269 (cp_parser *);
2270 static bool cp_parser_extension_opt
2271 (cp_parser *, int *);
2272 static void cp_parser_label_declaration
2273 (cp_parser *);
2275 /* Transactional Memory Extensions */
2277 static tree cp_parser_transaction
2278 (cp_parser *, enum rid);
2279 static tree cp_parser_transaction_expression
2280 (cp_parser *, enum rid);
2281 static bool cp_parser_function_transaction
2282 (cp_parser *, enum rid);
2283 static tree cp_parser_transaction_cancel
2284 (cp_parser *);
2286 enum pragma_context {
2287 pragma_external,
2288 pragma_member,
2289 pragma_objc_icode,
2290 pragma_stmt,
2291 pragma_compound
2293 static bool cp_parser_pragma
2294 (cp_parser *, enum pragma_context);
2296 /* Objective-C++ Productions */
2298 static tree cp_parser_objc_message_receiver
2299 (cp_parser *);
2300 static tree cp_parser_objc_message_args
2301 (cp_parser *);
2302 static tree cp_parser_objc_message_expression
2303 (cp_parser *);
2304 static tree cp_parser_objc_encode_expression
2305 (cp_parser *);
2306 static tree cp_parser_objc_defs_expression
2307 (cp_parser *);
2308 static tree cp_parser_objc_protocol_expression
2309 (cp_parser *);
2310 static tree cp_parser_objc_selector_expression
2311 (cp_parser *);
2312 static tree cp_parser_objc_expression
2313 (cp_parser *);
2314 static bool cp_parser_objc_selector_p
2315 (enum cpp_ttype);
2316 static tree cp_parser_objc_selector
2317 (cp_parser *);
2318 static tree cp_parser_objc_protocol_refs_opt
2319 (cp_parser *);
2320 static void cp_parser_objc_declaration
2321 (cp_parser *, tree);
2322 static tree cp_parser_objc_statement
2323 (cp_parser *);
2324 static bool cp_parser_objc_valid_prefix_attributes
2325 (cp_parser *, tree *);
2326 static void cp_parser_objc_at_property_declaration
2327 (cp_parser *) ;
2328 static void cp_parser_objc_at_synthesize_declaration
2329 (cp_parser *) ;
2330 static void cp_parser_objc_at_dynamic_declaration
2331 (cp_parser *) ;
2332 static tree cp_parser_objc_struct_declaration
2333 (cp_parser *) ;
2335 /* Utility Routines */
2337 static tree cp_parser_lookup_name
2338 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2339 static tree cp_parser_lookup_name_simple
2340 (cp_parser *, tree, location_t);
2341 static tree cp_parser_maybe_treat_template_as_class
2342 (tree, bool);
2343 static bool cp_parser_check_declarator_template_parameters
2344 (cp_parser *, cp_declarator *, location_t);
2345 static bool cp_parser_check_template_parameters
2346 (cp_parser *, unsigned, location_t, cp_declarator *);
2347 static tree cp_parser_simple_cast_expression
2348 (cp_parser *);
2349 static tree cp_parser_global_scope_opt
2350 (cp_parser *, bool);
2351 static bool cp_parser_constructor_declarator_p
2352 (cp_parser *, bool);
2353 static tree cp_parser_function_definition_from_specifiers_and_declarator
2354 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2355 static tree cp_parser_function_definition_after_declarator
2356 (cp_parser *, bool);
2357 static void cp_parser_template_declaration_after_export
2358 (cp_parser *, bool);
2359 static void cp_parser_perform_template_parameter_access_checks
2360 (vec<deferred_access_check, va_gc> *);
2361 static tree cp_parser_single_declaration
2362 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2363 static tree cp_parser_functional_cast
2364 (cp_parser *, tree);
2365 static tree cp_parser_save_member_function_body
2366 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2367 static tree cp_parser_save_nsdmi
2368 (cp_parser *);
2369 static tree cp_parser_enclosed_template_argument_list
2370 (cp_parser *);
2371 static void cp_parser_save_default_args
2372 (cp_parser *, tree);
2373 static void cp_parser_late_parsing_for_member
2374 (cp_parser *, tree);
2375 static tree cp_parser_late_parse_one_default_arg
2376 (cp_parser *, tree, tree, tree);
2377 static void cp_parser_late_parsing_nsdmi
2378 (cp_parser *, tree);
2379 static void cp_parser_late_parsing_default_args
2380 (cp_parser *, tree);
2381 static tree cp_parser_sizeof_operand
2382 (cp_parser *, enum rid);
2383 static tree cp_parser_trait_expr
2384 (cp_parser *, enum rid);
2385 static bool cp_parser_declares_only_class_p
2386 (cp_parser *);
2387 static void cp_parser_set_storage_class
2388 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2389 static void cp_parser_set_decl_spec_type
2390 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2391 static void set_and_check_decl_spec_loc
2392 (cp_decl_specifier_seq *decl_specs,
2393 cp_decl_spec ds, cp_token *);
2394 static bool cp_parser_friend_p
2395 (const cp_decl_specifier_seq *);
2396 static void cp_parser_required_error
2397 (cp_parser *, required_token, bool);
2398 static cp_token *cp_parser_require
2399 (cp_parser *, enum cpp_ttype, required_token);
2400 static cp_token *cp_parser_require_keyword
2401 (cp_parser *, enum rid, required_token);
2402 static bool cp_parser_token_starts_function_definition_p
2403 (cp_token *);
2404 static bool cp_parser_next_token_starts_class_definition_p
2405 (cp_parser *);
2406 static bool cp_parser_next_token_ends_template_argument_p
2407 (cp_parser *);
2408 static bool cp_parser_nth_token_starts_template_argument_list_p
2409 (cp_parser *, size_t);
2410 static enum tag_types cp_parser_token_is_class_key
2411 (cp_token *);
2412 static enum tag_types cp_parser_token_is_type_parameter_key
2413 (cp_token *);
2414 static void cp_parser_check_class_key
2415 (enum tag_types, tree type);
2416 static void cp_parser_check_access_in_redeclaration
2417 (tree type, location_t location);
2418 static bool cp_parser_optional_template_keyword
2419 (cp_parser *);
2420 static void cp_parser_pre_parsed_nested_name_specifier
2421 (cp_parser *);
2422 static bool cp_parser_cache_group
2423 (cp_parser *, enum cpp_ttype, unsigned);
2424 static tree cp_parser_cache_defarg
2425 (cp_parser *parser, bool nsdmi);
2426 static void cp_parser_parse_tentatively
2427 (cp_parser *);
2428 static void cp_parser_commit_to_tentative_parse
2429 (cp_parser *);
2430 static void cp_parser_commit_to_topmost_tentative_parse
2431 (cp_parser *);
2432 static void cp_parser_abort_tentative_parse
2433 (cp_parser *);
2434 static bool cp_parser_parse_definitely
2435 (cp_parser *);
2436 static inline bool cp_parser_parsing_tentatively
2437 (cp_parser *);
2438 static bool cp_parser_uncommitted_to_tentative_parse_p
2439 (cp_parser *);
2440 static void cp_parser_error
2441 (cp_parser *, const char *);
2442 static void cp_parser_name_lookup_error
2443 (cp_parser *, tree, tree, name_lookup_error, location_t);
2444 static bool cp_parser_simulate_error
2445 (cp_parser *);
2446 static bool cp_parser_check_type_definition
2447 (cp_parser *);
2448 static void cp_parser_check_for_definition_in_return_type
2449 (cp_declarator *, tree, location_t type_location);
2450 static void cp_parser_check_for_invalid_template_id
2451 (cp_parser *, tree, enum tag_types, location_t location);
2452 static bool cp_parser_non_integral_constant_expression
2453 (cp_parser *, non_integral_constant);
2454 static void cp_parser_diagnose_invalid_type_name
2455 (cp_parser *, tree, location_t);
2456 static bool cp_parser_parse_and_diagnose_invalid_type_name
2457 (cp_parser *);
2458 static int cp_parser_skip_to_closing_parenthesis
2459 (cp_parser *, bool, bool, bool);
2460 static void cp_parser_skip_to_end_of_statement
2461 (cp_parser *);
2462 static void cp_parser_consume_semicolon_at_end_of_statement
2463 (cp_parser *);
2464 static void cp_parser_skip_to_end_of_block_or_statement
2465 (cp_parser *);
2466 static bool cp_parser_skip_to_closing_brace
2467 (cp_parser *);
2468 static void cp_parser_skip_to_end_of_template_parameter_list
2469 (cp_parser *);
2470 static void cp_parser_skip_to_pragma_eol
2471 (cp_parser*, cp_token *);
2472 static bool cp_parser_error_occurred
2473 (cp_parser *);
2474 static bool cp_parser_allow_gnu_extensions_p
2475 (cp_parser *);
2476 static bool cp_parser_is_pure_string_literal
2477 (cp_token *);
2478 static bool cp_parser_is_string_literal
2479 (cp_token *);
2480 static bool cp_parser_is_keyword
2481 (cp_token *, enum rid);
2482 static tree cp_parser_make_typename_type
2483 (cp_parser *, tree, location_t location);
2484 static cp_declarator * cp_parser_make_indirect_declarator
2485 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2486 static bool cp_parser_compound_literal_p
2487 (cp_parser *);
2489 /* Returns nonzero if we are parsing tentatively. */
2491 static inline bool
2492 cp_parser_parsing_tentatively (cp_parser* parser)
2494 return parser->context->next != NULL;
2497 /* Returns nonzero if TOKEN is a string literal. */
2499 static bool
2500 cp_parser_is_pure_string_literal (cp_token* token)
2502 return (token->type == CPP_STRING ||
2503 token->type == CPP_STRING16 ||
2504 token->type == CPP_STRING32 ||
2505 token->type == CPP_WSTRING ||
2506 token->type == CPP_UTF8STRING);
2509 /* Returns nonzero if TOKEN is a string literal
2510 of a user-defined string literal. */
2512 static bool
2513 cp_parser_is_string_literal (cp_token* token)
2515 return (cp_parser_is_pure_string_literal (token) ||
2516 token->type == CPP_STRING_USERDEF ||
2517 token->type == CPP_STRING16_USERDEF ||
2518 token->type == CPP_STRING32_USERDEF ||
2519 token->type == CPP_WSTRING_USERDEF ||
2520 token->type == CPP_UTF8STRING_USERDEF);
2523 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2525 static bool
2526 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2528 return token->keyword == keyword;
2531 /* If not parsing tentatively, issue a diagnostic of the form
2532 FILE:LINE: MESSAGE before TOKEN
2533 where TOKEN is the next token in the input stream. MESSAGE
2534 (specified by the caller) is usually of the form "expected
2535 OTHER-TOKEN". */
2537 static void
2538 cp_parser_error (cp_parser* parser, const char* gmsgid)
2540 if (!cp_parser_simulate_error (parser))
2542 cp_token *token = cp_lexer_peek_token (parser->lexer);
2543 /* This diagnostic makes more sense if it is tagged to the line
2544 of the token we just peeked at. */
2545 cp_lexer_set_source_position_from_token (token);
2547 if (token->type == CPP_PRAGMA)
2549 error_at (token->location,
2550 "%<#pragma%> is not allowed here");
2551 cp_parser_skip_to_pragma_eol (parser, token);
2552 return;
2555 c_parse_error (gmsgid,
2556 /* Because c_parser_error does not understand
2557 CPP_KEYWORD, keywords are treated like
2558 identifiers. */
2559 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2560 token->u.value, token->flags);
2564 /* Issue an error about name-lookup failing. NAME is the
2565 IDENTIFIER_NODE DECL is the result of
2566 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2567 the thing that we hoped to find. */
2569 static void
2570 cp_parser_name_lookup_error (cp_parser* parser,
2571 tree name,
2572 tree decl,
2573 name_lookup_error desired,
2574 location_t location)
2576 /* If name lookup completely failed, tell the user that NAME was not
2577 declared. */
2578 if (decl == error_mark_node)
2580 if (parser->scope && parser->scope != global_namespace)
2581 error_at (location, "%<%E::%E%> has not been declared",
2582 parser->scope, name);
2583 else if (parser->scope == global_namespace)
2584 error_at (location, "%<::%E%> has not been declared", name);
2585 else if (parser->object_scope
2586 && !CLASS_TYPE_P (parser->object_scope))
2587 error_at (location, "request for member %qE in non-class type %qT",
2588 name, parser->object_scope);
2589 else if (parser->object_scope)
2590 error_at (location, "%<%T::%E%> has not been declared",
2591 parser->object_scope, name);
2592 else
2593 error_at (location, "%qE has not been declared", name);
2595 else if (parser->scope && parser->scope != global_namespace)
2597 switch (desired)
2599 case NLE_TYPE:
2600 error_at (location, "%<%E::%E%> is not a type",
2601 parser->scope, name);
2602 break;
2603 case NLE_CXX98:
2604 error_at (location, "%<%E::%E%> is not a class or namespace",
2605 parser->scope, name);
2606 break;
2607 case NLE_NOT_CXX98:
2608 error_at (location,
2609 "%<%E::%E%> is not a class, namespace, or enumeration",
2610 parser->scope, name);
2611 break;
2612 default:
2613 gcc_unreachable ();
2617 else if (parser->scope == global_namespace)
2619 switch (desired)
2621 case NLE_TYPE:
2622 error_at (location, "%<::%E%> is not a type", name);
2623 break;
2624 case NLE_CXX98:
2625 error_at (location, "%<::%E%> is not a class or namespace", name);
2626 break;
2627 case NLE_NOT_CXX98:
2628 error_at (location,
2629 "%<::%E%> is not a class, namespace, or enumeration",
2630 name);
2631 break;
2632 default:
2633 gcc_unreachable ();
2636 else
2638 switch (desired)
2640 case NLE_TYPE:
2641 error_at (location, "%qE is not a type", name);
2642 break;
2643 case NLE_CXX98:
2644 error_at (location, "%qE is not a class or namespace", name);
2645 break;
2646 case NLE_NOT_CXX98:
2647 error_at (location,
2648 "%qE is not a class, namespace, or enumeration", name);
2649 break;
2650 default:
2651 gcc_unreachable ();
2656 /* If we are parsing tentatively, remember that an error has occurred
2657 during this tentative parse. Returns true if the error was
2658 simulated; false if a message should be issued by the caller. */
2660 static bool
2661 cp_parser_simulate_error (cp_parser* parser)
2663 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2665 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2666 return true;
2668 return false;
2671 /* This function is called when a type is defined. If type
2672 definitions are forbidden at this point, an error message is
2673 issued. */
2675 static bool
2676 cp_parser_check_type_definition (cp_parser* parser)
2678 /* If types are forbidden here, issue a message. */
2679 if (parser->type_definition_forbidden_message)
2681 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2682 in the message need to be interpreted. */
2683 error (parser->type_definition_forbidden_message);
2684 return false;
2686 return true;
2689 /* This function is called when the DECLARATOR is processed. The TYPE
2690 was a type defined in the decl-specifiers. If it is invalid to
2691 define a type in the decl-specifiers for DECLARATOR, an error is
2692 issued. TYPE_LOCATION is the location of TYPE and is used
2693 for error reporting. */
2695 static void
2696 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2697 tree type, location_t type_location)
2699 /* [dcl.fct] forbids type definitions in return types.
2700 Unfortunately, it's not easy to know whether or not we are
2701 processing a return type until after the fact. */
2702 while (declarator
2703 && (declarator->kind == cdk_pointer
2704 || declarator->kind == cdk_reference
2705 || declarator->kind == cdk_ptrmem))
2706 declarator = declarator->declarator;
2707 if (declarator
2708 && declarator->kind == cdk_function)
2710 error_at (type_location,
2711 "new types may not be defined in a return type");
2712 inform (type_location,
2713 "(perhaps a semicolon is missing after the definition of %qT)",
2714 type);
2718 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2719 "<" in any valid C++ program. If the next token is indeed "<",
2720 issue a message warning the user about what appears to be an
2721 invalid attempt to form a template-id. LOCATION is the location
2722 of the type-specifier (TYPE) */
2724 static void
2725 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2726 tree type,
2727 enum tag_types tag_type,
2728 location_t location)
2730 cp_token_position start = 0;
2732 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2734 if (TYPE_P (type))
2735 error_at (location, "%qT is not a template", type);
2736 else if (identifier_p (type))
2738 if (tag_type != none_type)
2739 error_at (location, "%qE is not a class template", type);
2740 else
2741 error_at (location, "%qE is not a template", type);
2743 else
2744 error_at (location, "invalid template-id");
2745 /* Remember the location of the invalid "<". */
2746 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2747 start = cp_lexer_token_position (parser->lexer, true);
2748 /* Consume the "<". */
2749 cp_lexer_consume_token (parser->lexer);
2750 /* Parse the template arguments. */
2751 cp_parser_enclosed_template_argument_list (parser);
2752 /* Permanently remove the invalid template arguments so that
2753 this error message is not issued again. */
2754 if (start)
2755 cp_lexer_purge_tokens_after (parser->lexer, start);
2759 /* If parsing an integral constant-expression, issue an error message
2760 about the fact that THING appeared and return true. Otherwise,
2761 return false. In either case, set
2762 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2764 static bool
2765 cp_parser_non_integral_constant_expression (cp_parser *parser,
2766 non_integral_constant thing)
2768 parser->non_integral_constant_expression_p = true;
2769 if (parser->integral_constant_expression_p)
2771 if (!parser->allow_non_integral_constant_expression_p)
2773 const char *msg = NULL;
2774 switch (thing)
2776 case NIC_FLOAT:
2777 error ("floating-point literal "
2778 "cannot appear in a constant-expression");
2779 return true;
2780 case NIC_CAST:
2781 error ("a cast to a type other than an integral or "
2782 "enumeration type cannot appear in a "
2783 "constant-expression");
2784 return true;
2785 case NIC_TYPEID:
2786 error ("%<typeid%> operator "
2787 "cannot appear in a constant-expression");
2788 return true;
2789 case NIC_NCC:
2790 error ("non-constant compound literals "
2791 "cannot appear in a constant-expression");
2792 return true;
2793 case NIC_FUNC_CALL:
2794 error ("a function call "
2795 "cannot appear in a constant-expression");
2796 return true;
2797 case NIC_INC:
2798 error ("an increment "
2799 "cannot appear in a constant-expression");
2800 return true;
2801 case NIC_DEC:
2802 error ("an decrement "
2803 "cannot appear in a constant-expression");
2804 return true;
2805 case NIC_ARRAY_REF:
2806 error ("an array reference "
2807 "cannot appear in a constant-expression");
2808 return true;
2809 case NIC_ADDR_LABEL:
2810 error ("the address of a label "
2811 "cannot appear in a constant-expression");
2812 return true;
2813 case NIC_OVERLOADED:
2814 error ("calls to overloaded operators "
2815 "cannot appear in a constant-expression");
2816 return true;
2817 case NIC_ASSIGNMENT:
2818 error ("an assignment cannot appear in a constant-expression");
2819 return true;
2820 case NIC_COMMA:
2821 error ("a comma operator "
2822 "cannot appear in a constant-expression");
2823 return true;
2824 case NIC_CONSTRUCTOR:
2825 error ("a call to a constructor "
2826 "cannot appear in a constant-expression");
2827 return true;
2828 case NIC_TRANSACTION:
2829 error ("a transaction expression "
2830 "cannot appear in a constant-expression");
2831 return true;
2832 case NIC_THIS:
2833 msg = "this";
2834 break;
2835 case NIC_FUNC_NAME:
2836 msg = "__FUNCTION__";
2837 break;
2838 case NIC_PRETTY_FUNC:
2839 msg = "__PRETTY_FUNCTION__";
2840 break;
2841 case NIC_C99_FUNC:
2842 msg = "__func__";
2843 break;
2844 case NIC_VA_ARG:
2845 msg = "va_arg";
2846 break;
2847 case NIC_ARROW:
2848 msg = "->";
2849 break;
2850 case NIC_POINT:
2851 msg = ".";
2852 break;
2853 case NIC_STAR:
2854 msg = "*";
2855 break;
2856 case NIC_ADDR:
2857 msg = "&";
2858 break;
2859 case NIC_PREINCREMENT:
2860 msg = "++";
2861 break;
2862 case NIC_PREDECREMENT:
2863 msg = "--";
2864 break;
2865 case NIC_NEW:
2866 msg = "new";
2867 break;
2868 case NIC_DEL:
2869 msg = "delete";
2870 break;
2871 default:
2872 gcc_unreachable ();
2874 if (msg)
2875 error ("%qs cannot appear in a constant-expression", msg);
2876 return true;
2879 return false;
2882 /* Emit a diagnostic for an invalid type name. This function commits
2883 to the current active tentative parse, if any. (Otherwise, the
2884 problematic construct might be encountered again later, resulting
2885 in duplicate error messages.) LOCATION is the location of ID. */
2887 static void
2888 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2889 location_t location)
2891 tree decl, ambiguous_decls;
2892 cp_parser_commit_to_tentative_parse (parser);
2893 /* Try to lookup the identifier. */
2894 decl = cp_parser_lookup_name (parser, id, none_type,
2895 /*is_template=*/false,
2896 /*is_namespace=*/false,
2897 /*check_dependency=*/true,
2898 &ambiguous_decls, location);
2899 if (ambiguous_decls)
2900 /* If the lookup was ambiguous, an error will already have
2901 been issued. */
2902 return;
2903 /* If the lookup found a template-name, it means that the user forgot
2904 to specify an argument list. Emit a useful error message. */
2905 if (TREE_CODE (decl) == TEMPLATE_DECL)
2906 error_at (location,
2907 "invalid use of template-name %qE without an argument list",
2908 decl);
2909 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2910 error_at (location, "invalid use of destructor %qD as a type", id);
2911 else if (TREE_CODE (decl) == TYPE_DECL)
2912 /* Something like 'unsigned A a;' */
2913 error_at (location, "invalid combination of multiple type-specifiers");
2914 else if (!parser->scope)
2916 /* Issue an error message. */
2917 error_at (location, "%qE does not name a type", id);
2918 /* If we're in a template class, it's possible that the user was
2919 referring to a type from a base class. For example:
2921 template <typename T> struct A { typedef T X; };
2922 template <typename T> struct B : public A<T> { X x; };
2924 The user should have said "typename A<T>::X". */
2925 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2926 inform (location, "C++11 %<constexpr%> only available with "
2927 "-std=c++11 or -std=gnu++11");
2928 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2929 inform (location, "C++11 %<noexcept%> only available with "
2930 "-std=c++11 or -std=gnu++11");
2931 else if (cxx_dialect < cxx11
2932 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2933 inform (location, "C++11 %<thread_local%> only available with "
2934 "-std=c++11 or -std=gnu++11");
2935 else if (processing_template_decl && current_class_type
2936 && TYPE_BINFO (current_class_type))
2938 tree b;
2940 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2942 b = TREE_CHAIN (b))
2944 tree base_type = BINFO_TYPE (b);
2945 if (CLASS_TYPE_P (base_type)
2946 && dependent_type_p (base_type))
2948 tree field;
2949 /* Go from a particular instantiation of the
2950 template (which will have an empty TYPE_FIELDs),
2951 to the main version. */
2952 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2953 for (field = TYPE_FIELDS (base_type);
2954 field;
2955 field = DECL_CHAIN (field))
2956 if (TREE_CODE (field) == TYPE_DECL
2957 && DECL_NAME (field) == id)
2959 inform (location,
2960 "(perhaps %<typename %T::%E%> was intended)",
2961 BINFO_TYPE (b), id);
2962 break;
2964 if (field)
2965 break;
2970 /* Here we diagnose qualified-ids where the scope is actually correct,
2971 but the identifier does not resolve to a valid type name. */
2972 else if (parser->scope != error_mark_node)
2974 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2976 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2977 error_at (location_of (id),
2978 "%qE in namespace %qE does not name a template type",
2979 id, parser->scope);
2980 else
2981 error_at (location_of (id),
2982 "%qE in namespace %qE does not name a type",
2983 id, parser->scope);
2985 else if (CLASS_TYPE_P (parser->scope)
2986 && constructor_name_p (id, parser->scope))
2988 /* A<T>::A<T>() */
2989 error_at (location, "%<%T::%E%> names the constructor, not"
2990 " the type", parser->scope, id);
2991 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2992 error_at (location, "and %qT has no template constructors",
2993 parser->scope);
2995 else if (TYPE_P (parser->scope)
2996 && dependent_scope_p (parser->scope))
2997 error_at (location, "need %<typename%> before %<%T::%E%> because "
2998 "%qT is a dependent scope",
2999 parser->scope, id, parser->scope);
3000 else if (TYPE_P (parser->scope))
3002 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3003 error_at (location_of (id),
3004 "%qE in %q#T does not name a template type",
3005 id, parser->scope);
3006 else
3007 error_at (location_of (id),
3008 "%qE in %q#T does not name a type",
3009 id, parser->scope);
3011 else
3012 gcc_unreachable ();
3016 /* Check for a common situation where a type-name should be present,
3017 but is not, and issue a sensible error message. Returns true if an
3018 invalid type-name was detected.
3020 The situation handled by this function are variable declarations of the
3021 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3022 Usually, `ID' should name a type, but if we got here it means that it
3023 does not. We try to emit the best possible error message depending on
3024 how exactly the id-expression looks like. */
3026 static bool
3027 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3029 tree id;
3030 cp_token *token = cp_lexer_peek_token (parser->lexer);
3032 /* Avoid duplicate error about ambiguous lookup. */
3033 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3035 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3036 if (next->type == CPP_NAME && next->error_reported)
3037 goto out;
3040 cp_parser_parse_tentatively (parser);
3041 id = cp_parser_id_expression (parser,
3042 /*template_keyword_p=*/false,
3043 /*check_dependency_p=*/true,
3044 /*template_p=*/NULL,
3045 /*declarator_p=*/true,
3046 /*optional_p=*/false);
3047 /* If the next token is a (, this is a function with no explicit return
3048 type, i.e. constructor, destructor or conversion op. */
3049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3050 || TREE_CODE (id) == TYPE_DECL)
3052 cp_parser_abort_tentative_parse (parser);
3053 return false;
3055 if (!cp_parser_parse_definitely (parser))
3056 return false;
3058 /* Emit a diagnostic for the invalid type. */
3059 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3060 out:
3061 /* If we aren't in the middle of a declarator (i.e. in a
3062 parameter-declaration-clause), skip to the end of the declaration;
3063 there's no point in trying to process it. */
3064 if (!parser->in_declarator_p)
3065 cp_parser_skip_to_end_of_block_or_statement (parser);
3066 return true;
3069 /* Consume tokens up to, and including, the next non-nested closing `)'.
3070 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3071 are doing error recovery. Returns -1 if OR_COMMA is true and we
3072 found an unnested comma. */
3074 static int
3075 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3076 bool recovering,
3077 bool or_comma,
3078 bool consume_paren)
3080 unsigned paren_depth = 0;
3081 unsigned brace_depth = 0;
3082 unsigned square_depth = 0;
3084 if (recovering && !or_comma
3085 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3086 return 0;
3088 while (true)
3090 cp_token * token = cp_lexer_peek_token (parser->lexer);
3092 switch (token->type)
3094 case CPP_EOF:
3095 case CPP_PRAGMA_EOL:
3096 /* If we've run out of tokens, then there is no closing `)'. */
3097 return 0;
3099 /* This is good for lambda expression capture-lists. */
3100 case CPP_OPEN_SQUARE:
3101 ++square_depth;
3102 break;
3103 case CPP_CLOSE_SQUARE:
3104 if (!square_depth--)
3105 return 0;
3106 break;
3108 case CPP_SEMICOLON:
3109 /* This matches the processing in skip_to_end_of_statement. */
3110 if (!brace_depth)
3111 return 0;
3112 break;
3114 case CPP_OPEN_BRACE:
3115 ++brace_depth;
3116 break;
3117 case CPP_CLOSE_BRACE:
3118 if (!brace_depth--)
3119 return 0;
3120 break;
3122 case CPP_COMMA:
3123 if (recovering && or_comma && !brace_depth && !paren_depth
3124 && !square_depth)
3125 return -1;
3126 break;
3128 case CPP_OPEN_PAREN:
3129 if (!brace_depth)
3130 ++paren_depth;
3131 break;
3133 case CPP_CLOSE_PAREN:
3134 if (!brace_depth && !paren_depth--)
3136 if (consume_paren)
3137 cp_lexer_consume_token (parser->lexer);
3138 return 1;
3140 break;
3142 default:
3143 break;
3146 /* Consume the token. */
3147 cp_lexer_consume_token (parser->lexer);
3151 /* Consume tokens until we reach the end of the current statement.
3152 Normally, that will be just before consuming a `;'. However, if a
3153 non-nested `}' comes first, then we stop before consuming that. */
3155 static void
3156 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3158 unsigned nesting_depth = 0;
3160 /* Unwind generic function template scope if necessary. */
3161 if (parser->fully_implicit_function_template_p)
3162 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3164 while (true)
3166 cp_token *token = cp_lexer_peek_token (parser->lexer);
3168 switch (token->type)
3170 case CPP_EOF:
3171 case CPP_PRAGMA_EOL:
3172 /* If we've run out of tokens, stop. */
3173 return;
3175 case CPP_SEMICOLON:
3176 /* If the next token is a `;', we have reached the end of the
3177 statement. */
3178 if (!nesting_depth)
3179 return;
3180 break;
3182 case CPP_CLOSE_BRACE:
3183 /* If this is a non-nested '}', stop before consuming it.
3184 That way, when confronted with something like:
3186 { 3 + }
3188 we stop before consuming the closing '}', even though we
3189 have not yet reached a `;'. */
3190 if (nesting_depth == 0)
3191 return;
3193 /* If it is the closing '}' for a block that we have
3194 scanned, stop -- but only after consuming the token.
3195 That way given:
3197 void f g () { ... }
3198 typedef int I;
3200 we will stop after the body of the erroneously declared
3201 function, but before consuming the following `typedef'
3202 declaration. */
3203 if (--nesting_depth == 0)
3205 cp_lexer_consume_token (parser->lexer);
3206 return;
3209 case CPP_OPEN_BRACE:
3210 ++nesting_depth;
3211 break;
3213 default:
3214 break;
3217 /* Consume the token. */
3218 cp_lexer_consume_token (parser->lexer);
3222 /* This function is called at the end of a statement or declaration.
3223 If the next token is a semicolon, it is consumed; otherwise, error
3224 recovery is attempted. */
3226 static void
3227 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3229 /* Look for the trailing `;'. */
3230 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3232 /* If there is additional (erroneous) input, skip to the end of
3233 the statement. */
3234 cp_parser_skip_to_end_of_statement (parser);
3235 /* If the next token is now a `;', consume it. */
3236 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3237 cp_lexer_consume_token (parser->lexer);
3241 /* Skip tokens until we have consumed an entire block, or until we
3242 have consumed a non-nested `;'. */
3244 static void
3245 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3247 int nesting_depth = 0;
3249 /* Unwind generic function template scope if necessary. */
3250 if (parser->fully_implicit_function_template_p)
3251 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3253 while (nesting_depth >= 0)
3255 cp_token *token = cp_lexer_peek_token (parser->lexer);
3257 switch (token->type)
3259 case CPP_EOF:
3260 case CPP_PRAGMA_EOL:
3261 /* If we've run out of tokens, stop. */
3262 return;
3264 case CPP_SEMICOLON:
3265 /* Stop if this is an unnested ';'. */
3266 if (!nesting_depth)
3267 nesting_depth = -1;
3268 break;
3270 case CPP_CLOSE_BRACE:
3271 /* Stop if this is an unnested '}', or closes the outermost
3272 nesting level. */
3273 nesting_depth--;
3274 if (nesting_depth < 0)
3275 return;
3276 if (!nesting_depth)
3277 nesting_depth = -1;
3278 break;
3280 case CPP_OPEN_BRACE:
3281 /* Nest. */
3282 nesting_depth++;
3283 break;
3285 default:
3286 break;
3289 /* Consume the token. */
3290 cp_lexer_consume_token (parser->lexer);
3294 /* Skip tokens until a non-nested closing curly brace is the next
3295 token, or there are no more tokens. Return true in the first case,
3296 false otherwise. */
3298 static bool
3299 cp_parser_skip_to_closing_brace (cp_parser *parser)
3301 unsigned nesting_depth = 0;
3303 while (true)
3305 cp_token *token = cp_lexer_peek_token (parser->lexer);
3307 switch (token->type)
3309 case CPP_EOF:
3310 case CPP_PRAGMA_EOL:
3311 /* If we've run out of tokens, stop. */
3312 return false;
3314 case CPP_CLOSE_BRACE:
3315 /* If the next token is a non-nested `}', then we have reached
3316 the end of the current block. */
3317 if (nesting_depth-- == 0)
3318 return true;
3319 break;
3321 case CPP_OPEN_BRACE:
3322 /* If it the next token is a `{', then we are entering a new
3323 block. Consume the entire block. */
3324 ++nesting_depth;
3325 break;
3327 default:
3328 break;
3331 /* Consume the token. */
3332 cp_lexer_consume_token (parser->lexer);
3336 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3337 parameter is the PRAGMA token, allowing us to purge the entire pragma
3338 sequence. */
3340 static void
3341 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3343 cp_token *token;
3345 parser->lexer->in_pragma = false;
3348 token = cp_lexer_consume_token (parser->lexer);
3349 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3351 /* Ensure that the pragma is not parsed again. */
3352 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3355 /* Require pragma end of line, resyncing with it as necessary. The
3356 arguments are as for cp_parser_skip_to_pragma_eol. */
3358 static void
3359 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3361 parser->lexer->in_pragma = false;
3362 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3363 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3366 /* This is a simple wrapper around make_typename_type. When the id is
3367 an unresolved identifier node, we can provide a superior diagnostic
3368 using cp_parser_diagnose_invalid_type_name. */
3370 static tree
3371 cp_parser_make_typename_type (cp_parser *parser, tree id,
3372 location_t id_location)
3374 tree result;
3375 if (identifier_p (id))
3377 result = make_typename_type (parser->scope, id, typename_type,
3378 /*complain=*/tf_none);
3379 if (result == error_mark_node)
3380 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3381 return result;
3383 return make_typename_type (parser->scope, id, typename_type, tf_error);
3386 /* This is a wrapper around the
3387 make_{pointer,ptrmem,reference}_declarator functions that decides
3388 which one to call based on the CODE and CLASS_TYPE arguments. The
3389 CODE argument should be one of the values returned by
3390 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3391 appertain to the pointer or reference. */
3393 static cp_declarator *
3394 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3395 cp_cv_quals cv_qualifiers,
3396 cp_declarator *target,
3397 tree attributes)
3399 if (code == ERROR_MARK)
3400 return cp_error_declarator;
3402 if (code == INDIRECT_REF)
3403 if (class_type == NULL_TREE)
3404 return make_pointer_declarator (cv_qualifiers, target, attributes);
3405 else
3406 return make_ptrmem_declarator (cv_qualifiers, class_type,
3407 target, attributes);
3408 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3409 return make_reference_declarator (cv_qualifiers, target,
3410 false, attributes);
3411 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3412 return make_reference_declarator (cv_qualifiers, target,
3413 true, attributes);
3414 gcc_unreachable ();
3417 /* Create a new C++ parser. */
3419 static cp_parser *
3420 cp_parser_new (void)
3422 cp_parser *parser;
3423 cp_lexer *lexer;
3424 unsigned i;
3426 /* cp_lexer_new_main is called before doing GC allocation because
3427 cp_lexer_new_main might load a PCH file. */
3428 lexer = cp_lexer_new_main ();
3430 /* Initialize the binops_by_token so that we can get the tree
3431 directly from the token. */
3432 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3433 binops_by_token[binops[i].token_type] = binops[i];
3435 parser = ggc_cleared_alloc<cp_parser> ();
3436 parser->lexer = lexer;
3437 parser->context = cp_parser_context_new (NULL);
3439 /* For now, we always accept GNU extensions. */
3440 parser->allow_gnu_extensions_p = 1;
3442 /* The `>' token is a greater-than operator, not the end of a
3443 template-id. */
3444 parser->greater_than_is_operator_p = true;
3446 parser->default_arg_ok_p = true;
3448 /* We are not parsing a constant-expression. */
3449 parser->integral_constant_expression_p = false;
3450 parser->allow_non_integral_constant_expression_p = false;
3451 parser->non_integral_constant_expression_p = false;
3453 /* Local variable names are not forbidden. */
3454 parser->local_variables_forbidden_p = false;
3456 /* We are not processing an `extern "C"' declaration. */
3457 parser->in_unbraced_linkage_specification_p = false;
3459 /* We are not processing a declarator. */
3460 parser->in_declarator_p = false;
3462 /* We are not processing a template-argument-list. */
3463 parser->in_template_argument_list_p = false;
3465 /* We are not in an iteration statement. */
3466 parser->in_statement = 0;
3468 /* We are not in a switch statement. */
3469 parser->in_switch_statement_p = false;
3471 /* We are not parsing a type-id inside an expression. */
3472 parser->in_type_id_in_expr_p = false;
3474 /* Declarations aren't implicitly extern "C". */
3475 parser->implicit_extern_c = false;
3477 /* String literals should be translated to the execution character set. */
3478 parser->translate_strings_p = true;
3480 /* We are not parsing a function body. */
3481 parser->in_function_body = false;
3483 /* We can correct until told otherwise. */
3484 parser->colon_corrects_to_scope_p = true;
3486 /* The unparsed function queue is empty. */
3487 push_unparsed_function_queues (parser);
3489 /* There are no classes being defined. */
3490 parser->num_classes_being_defined = 0;
3492 /* No template parameters apply. */
3493 parser->num_template_parameter_lists = 0;
3495 /* Not declaring an implicit function template. */
3496 parser->auto_is_implicit_function_template_parm_p = false;
3497 parser->fully_implicit_function_template_p = false;
3498 parser->implicit_template_parms = 0;
3499 parser->implicit_template_scope = 0;
3501 return parser;
3504 /* Create a cp_lexer structure which will emit the tokens in CACHE
3505 and push it onto the parser's lexer stack. This is used for delayed
3506 parsing of in-class method bodies and default arguments, and should
3507 not be confused with tentative parsing. */
3508 static void
3509 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3511 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3512 lexer->next = parser->lexer;
3513 parser->lexer = lexer;
3515 /* Move the current source position to that of the first token in the
3516 new lexer. */
3517 cp_lexer_set_source_position_from_token (lexer->next_token);
3520 /* Pop the top lexer off the parser stack. This is never used for the
3521 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3522 static void
3523 cp_parser_pop_lexer (cp_parser *parser)
3525 cp_lexer *lexer = parser->lexer;
3526 parser->lexer = lexer->next;
3527 cp_lexer_destroy (lexer);
3529 /* Put the current source position back where it was before this
3530 lexer was pushed. */
3531 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3534 /* Lexical conventions [gram.lex] */
3536 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3537 identifier. */
3539 static tree
3540 cp_parser_identifier (cp_parser* parser)
3542 cp_token *token;
3544 /* Look for the identifier. */
3545 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3546 /* Return the value. */
3547 return token ? token->u.value : error_mark_node;
3550 /* Parse a sequence of adjacent string constants. Returns a
3551 TREE_STRING representing the combined, nul-terminated string
3552 constant. If TRANSLATE is true, translate the string to the
3553 execution character set. If WIDE_OK is true, a wide string is
3554 invalid here.
3556 C++98 [lex.string] says that if a narrow string literal token is
3557 adjacent to a wide string literal token, the behavior is undefined.
3558 However, C99 6.4.5p4 says that this results in a wide string literal.
3559 We follow C99 here, for consistency with the C front end.
3561 This code is largely lifted from lex_string() in c-lex.c.
3563 FUTURE: ObjC++ will need to handle @-strings here. */
3564 static tree
3565 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3566 bool lookup_udlit = true)
3568 tree value;
3569 size_t count;
3570 struct obstack str_ob;
3571 cpp_string str, istr, *strs;
3572 cp_token *tok;
3573 enum cpp_ttype type, curr_type;
3574 int have_suffix_p = 0;
3575 tree string_tree;
3576 tree suffix_id = NULL_TREE;
3577 bool curr_tok_is_userdef_p = false;
3579 tok = cp_lexer_peek_token (parser->lexer);
3580 if (!cp_parser_is_string_literal (tok))
3582 cp_parser_error (parser, "expected string-literal");
3583 return error_mark_node;
3586 if (cpp_userdef_string_p (tok->type))
3588 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3589 curr_type = cpp_userdef_string_remove_type (tok->type);
3590 curr_tok_is_userdef_p = true;
3592 else
3594 string_tree = tok->u.value;
3595 curr_type = tok->type;
3597 type = curr_type;
3599 /* Try to avoid the overhead of creating and destroying an obstack
3600 for the common case of just one string. */
3601 if (!cp_parser_is_string_literal
3602 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3604 cp_lexer_consume_token (parser->lexer);
3606 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3607 str.len = TREE_STRING_LENGTH (string_tree);
3608 count = 1;
3610 if (curr_tok_is_userdef_p)
3612 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3613 have_suffix_p = 1;
3614 curr_type = cpp_userdef_string_remove_type (tok->type);
3616 else
3617 curr_type = tok->type;
3619 strs = &str;
3621 else
3623 gcc_obstack_init (&str_ob);
3624 count = 0;
3628 cp_lexer_consume_token (parser->lexer);
3629 count++;
3630 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3631 str.len = TREE_STRING_LENGTH (string_tree);
3633 if (curr_tok_is_userdef_p)
3635 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3636 if (have_suffix_p == 0)
3638 suffix_id = curr_suffix_id;
3639 have_suffix_p = 1;
3641 else if (have_suffix_p == 1
3642 && curr_suffix_id != suffix_id)
3644 error ("inconsistent user-defined literal suffixes"
3645 " %qD and %qD in string literal",
3646 suffix_id, curr_suffix_id);
3647 have_suffix_p = -1;
3649 curr_type = cpp_userdef_string_remove_type (tok->type);
3651 else
3652 curr_type = tok->type;
3654 if (type != curr_type)
3656 if (type == CPP_STRING)
3657 type = curr_type;
3658 else if (curr_type != CPP_STRING)
3659 error_at (tok->location,
3660 "unsupported non-standard concatenation "
3661 "of string literals");
3664 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3666 tok = cp_lexer_peek_token (parser->lexer);
3667 if (cpp_userdef_string_p (tok->type))
3669 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3670 curr_type = cpp_userdef_string_remove_type (tok->type);
3671 curr_tok_is_userdef_p = true;
3673 else
3675 string_tree = tok->u.value;
3676 curr_type = tok->type;
3677 curr_tok_is_userdef_p = false;
3680 while (cp_parser_is_string_literal (tok));
3682 strs = (cpp_string *) obstack_finish (&str_ob);
3685 if (type != CPP_STRING && !wide_ok)
3687 cp_parser_error (parser, "a wide string is invalid in this context");
3688 type = CPP_STRING;
3691 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3692 (parse_in, strs, count, &istr, type))
3694 value = build_string (istr.len, (const char *)istr.text);
3695 free (CONST_CAST (unsigned char *, istr.text));
3697 switch (type)
3699 default:
3700 case CPP_STRING:
3701 case CPP_UTF8STRING:
3702 TREE_TYPE (value) = char_array_type_node;
3703 break;
3704 case CPP_STRING16:
3705 TREE_TYPE (value) = char16_array_type_node;
3706 break;
3707 case CPP_STRING32:
3708 TREE_TYPE (value) = char32_array_type_node;
3709 break;
3710 case CPP_WSTRING:
3711 TREE_TYPE (value) = wchar_array_type_node;
3712 break;
3715 value = fix_string_type (value);
3717 if (have_suffix_p)
3719 tree literal = build_userdef_literal (suffix_id, value,
3720 OT_NONE, NULL_TREE);
3721 if (lookup_udlit)
3722 value = cp_parser_userdef_string_literal (literal);
3723 else
3724 value = literal;
3727 else
3728 /* cpp_interpret_string has issued an error. */
3729 value = error_mark_node;
3731 if (count > 1)
3732 obstack_free (&str_ob, 0);
3734 return value;
3737 /* Look up a literal operator with the name and the exact arguments. */
3739 static tree
3740 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3742 tree decl, fns;
3743 decl = lookup_name (name);
3744 if (!decl || !is_overloaded_fn (decl))
3745 return error_mark_node;
3747 for (fns = decl; fns; fns = OVL_NEXT (fns))
3749 unsigned int ix;
3750 bool found = true;
3751 tree fn = OVL_CURRENT (fns);
3752 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3753 if (parmtypes != NULL_TREE)
3755 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3756 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3758 tree tparm = TREE_VALUE (parmtypes);
3759 tree targ = TREE_TYPE ((*args)[ix]);
3760 bool ptr = TYPE_PTR_P (tparm);
3761 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3762 if ((ptr || arr || !same_type_p (tparm, targ))
3763 && (!ptr || !arr
3764 || !same_type_p (TREE_TYPE (tparm),
3765 TREE_TYPE (targ))))
3766 found = false;
3768 if (found
3769 && ix == vec_safe_length (args)
3770 /* May be this should be sufficient_parms_p instead,
3771 depending on how exactly should user-defined literals
3772 work in presence of default arguments on the literal
3773 operator parameters. */
3774 && parmtypes == void_list_node)
3775 return fn;
3779 return error_mark_node;
3782 /* Parse a user-defined char constant. Returns a call to a user-defined
3783 literal operator taking the character as an argument. */
3785 static tree
3786 cp_parser_userdef_char_literal (cp_parser *parser)
3788 cp_token *token = cp_lexer_consume_token (parser->lexer);
3789 tree literal = token->u.value;
3790 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3791 tree value = USERDEF_LITERAL_VALUE (literal);
3792 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3793 tree decl, result;
3795 /* Build up a call to the user-defined operator */
3796 /* Lookup the name we got back from the id-expression. */
3797 vec<tree, va_gc> *args = make_tree_vector ();
3798 vec_safe_push (args, value);
3799 decl = lookup_literal_operator (name, args);
3800 if (!decl || decl == error_mark_node)
3802 error ("unable to find character literal operator %qD with %qT argument",
3803 name, TREE_TYPE (value));
3804 release_tree_vector (args);
3805 return error_mark_node;
3807 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3808 release_tree_vector (args);
3809 if (result != error_mark_node)
3810 return result;
3812 error ("unable to find character literal operator %qD with %qT argument",
3813 name, TREE_TYPE (value));
3814 return error_mark_node;
3817 /* A subroutine of cp_parser_userdef_numeric_literal to
3818 create a char... template parameter pack from a string node. */
3820 static tree
3821 make_char_string_pack (tree value)
3823 tree charvec;
3824 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3825 const char *str = TREE_STRING_POINTER (value);
3826 int i, len = TREE_STRING_LENGTH (value) - 1;
3827 tree argvec = make_tree_vec (1);
3829 /* Fill in CHARVEC with all of the parameters. */
3830 charvec = make_tree_vec (len);
3831 for (i = 0; i < len; ++i)
3832 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3834 /* Build the argument packs. */
3835 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3836 TREE_TYPE (argpack) = char_type_node;
3838 TREE_VEC_ELT (argvec, 0) = argpack;
3840 return argvec;
3843 /* A subroutine of cp_parser_userdef_numeric_literal to
3844 create a char... template parameter pack from a string node. */
3846 static tree
3847 make_string_pack (tree value)
3849 tree charvec;
3850 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3851 const unsigned char *str
3852 = (const unsigned char *) TREE_STRING_POINTER (value);
3853 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3854 int len = TREE_STRING_LENGTH (value) / sz - 1;
3855 tree argvec = make_tree_vec (2);
3857 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3858 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3860 /* First template parm is character type. */
3861 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3863 /* Fill in CHARVEC with all of the parameters. */
3864 charvec = make_tree_vec (len);
3865 for (int i = 0; i < len; ++i)
3866 TREE_VEC_ELT (charvec, i)
3867 = double_int_to_tree (str_char_type_node,
3868 double_int::from_buffer (str + i * sz, sz));
3870 /* Build the argument packs. */
3871 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3872 TREE_TYPE (argpack) = str_char_type_node;
3874 TREE_VEC_ELT (argvec, 1) = argpack;
3876 return argvec;
3879 /* Parse a user-defined numeric constant. returns a call to a user-defined
3880 literal operator. */
3882 static tree
3883 cp_parser_userdef_numeric_literal (cp_parser *parser)
3885 cp_token *token = cp_lexer_consume_token (parser->lexer);
3886 tree literal = token->u.value;
3887 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3888 tree value = USERDEF_LITERAL_VALUE (literal);
3889 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3890 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3891 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3892 tree decl, result;
3893 vec<tree, va_gc> *args;
3895 /* Look for a literal operator taking the exact type of numeric argument
3896 as the literal value. */
3897 args = make_tree_vector ();
3898 vec_safe_push (args, value);
3899 decl = lookup_literal_operator (name, args);
3900 if (decl && decl != error_mark_node)
3902 result = finish_call_expr (decl, &args, false, true, tf_none);
3903 if (result != error_mark_node)
3905 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3906 warning_at (token->location, OPT_Woverflow,
3907 "integer literal exceeds range of %qT type",
3908 long_long_unsigned_type_node);
3909 else
3911 if (overflow > 0)
3912 warning_at (token->location, OPT_Woverflow,
3913 "floating literal exceeds range of %qT type",
3914 long_double_type_node);
3915 else if (overflow < 0)
3916 warning_at (token->location, OPT_Woverflow,
3917 "floating literal truncated to zero");
3919 release_tree_vector (args);
3920 return result;
3923 release_tree_vector (args);
3925 /* If the numeric argument didn't work, look for a raw literal
3926 operator taking a const char* argument consisting of the number
3927 in string format. */
3928 args = make_tree_vector ();
3929 vec_safe_push (args, num_string);
3930 decl = lookup_literal_operator (name, args);
3931 if (decl && decl != error_mark_node)
3933 result = finish_call_expr (decl, &args, false, true, tf_none);
3934 if (result != error_mark_node)
3936 release_tree_vector (args);
3937 return result;
3940 release_tree_vector (args);
3942 /* If the raw literal didn't work, look for a non-type template
3943 function with parameter pack char.... Call the function with
3944 template parameter characters representing the number. */
3945 args = make_tree_vector ();
3946 decl = lookup_literal_operator (name, args);
3947 if (decl && decl != error_mark_node)
3949 tree tmpl_args = make_char_string_pack (num_string);
3950 decl = lookup_template_function (decl, tmpl_args);
3951 result = finish_call_expr (decl, &args, false, true, tf_none);
3952 if (result != error_mark_node)
3954 release_tree_vector (args);
3955 return result;
3958 release_tree_vector (args);
3960 error ("unable to find numeric literal operator %qD", name);
3961 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3962 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3963 "to enable more built-in suffixes");
3964 return error_mark_node;
3967 /* Parse a user-defined string constant. Returns a call to a user-defined
3968 literal operator taking a character pointer and the length of the string
3969 as arguments. */
3971 static tree
3972 cp_parser_userdef_string_literal (tree literal)
3974 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3975 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3976 tree value = USERDEF_LITERAL_VALUE (literal);
3977 int len = TREE_STRING_LENGTH (value)
3978 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3979 tree decl, result;
3980 vec<tree, va_gc> *args;
3982 /* Look for a template function with typename parameter CharT
3983 and parameter pack CharT... Call the function with
3984 template parameter characters representing the string. */
3985 args = make_tree_vector ();
3986 decl = lookup_literal_operator (name, args);
3987 if (decl && decl != error_mark_node)
3989 tree tmpl_args = make_string_pack (value);
3990 decl = lookup_template_function (decl, tmpl_args);
3991 result = finish_call_expr (decl, &args, false, true, tf_none);
3992 if (result != error_mark_node)
3994 release_tree_vector (args);
3995 return result;
3998 release_tree_vector (args);
4000 /* Build up a call to the user-defined operator */
4001 /* Lookup the name we got back from the id-expression. */
4002 args = make_tree_vector ();
4003 vec_safe_push (args, value);
4004 vec_safe_push (args, build_int_cst (size_type_node, len));
4005 decl = lookup_name (name);
4006 if (!decl || decl == error_mark_node)
4008 error ("unable to find string literal operator %qD", name);
4009 release_tree_vector (args);
4010 return error_mark_node;
4012 result = finish_call_expr (decl, &args, false, true, tf_none);
4013 release_tree_vector (args);
4014 if (result != error_mark_node)
4015 return result;
4017 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4018 name, TREE_TYPE (value), size_type_node);
4019 return error_mark_node;
4023 /* Basic concepts [gram.basic] */
4025 /* Parse a translation-unit.
4027 translation-unit:
4028 declaration-seq [opt]
4030 Returns TRUE if all went well. */
4032 static bool
4033 cp_parser_translation_unit (cp_parser* parser)
4035 /* The address of the first non-permanent object on the declarator
4036 obstack. */
4037 static void *declarator_obstack_base;
4039 bool success;
4041 /* Create the declarator obstack, if necessary. */
4042 if (!cp_error_declarator)
4044 gcc_obstack_init (&declarator_obstack);
4045 /* Create the error declarator. */
4046 cp_error_declarator = make_declarator (cdk_error);
4047 /* Create the empty parameter list. */
4048 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4049 /* Remember where the base of the declarator obstack lies. */
4050 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4053 cp_parser_declaration_seq_opt (parser);
4055 /* If there are no tokens left then all went well. */
4056 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4058 /* Get rid of the token array; we don't need it any more. */
4059 cp_lexer_destroy (parser->lexer);
4060 parser->lexer = NULL;
4062 /* This file might have been a context that's implicitly extern
4063 "C". If so, pop the lang context. (Only relevant for PCH.) */
4064 if (parser->implicit_extern_c)
4066 pop_lang_context ();
4067 parser->implicit_extern_c = false;
4070 /* Finish up. */
4071 finish_translation_unit ();
4073 success = true;
4075 else
4077 cp_parser_error (parser, "expected declaration");
4078 success = false;
4081 /* Make sure the declarator obstack was fully cleaned up. */
4082 gcc_assert (obstack_next_free (&declarator_obstack)
4083 == declarator_obstack_base);
4085 /* All went well. */
4086 return success;
4089 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4090 decltype context. */
4092 static inline tsubst_flags_t
4093 complain_flags (bool decltype_p)
4095 tsubst_flags_t complain = tf_warning_or_error;
4096 if (decltype_p)
4097 complain |= tf_decltype;
4098 return complain;
4101 /* Expressions [gram.expr] */
4103 /* Parse a primary-expression.
4105 primary-expression:
4106 literal
4107 this
4108 ( expression )
4109 id-expression
4110 lambda-expression (C++11)
4112 GNU Extensions:
4114 primary-expression:
4115 ( compound-statement )
4116 __builtin_va_arg ( assignment-expression , type-id )
4117 __builtin_offsetof ( type-id , offsetof-expression )
4119 C++ Extensions:
4120 __has_nothrow_assign ( type-id )
4121 __has_nothrow_constructor ( type-id )
4122 __has_nothrow_copy ( type-id )
4123 __has_trivial_assign ( type-id )
4124 __has_trivial_constructor ( type-id )
4125 __has_trivial_copy ( type-id )
4126 __has_trivial_destructor ( type-id )
4127 __has_virtual_destructor ( type-id )
4128 __is_abstract ( type-id )
4129 __is_base_of ( type-id , type-id )
4130 __is_class ( type-id )
4131 __is_convertible_to ( type-id , type-id )
4132 __is_empty ( type-id )
4133 __is_enum ( type-id )
4134 __is_final ( type-id )
4135 __is_literal_type ( type-id )
4136 __is_pod ( type-id )
4137 __is_polymorphic ( type-id )
4138 __is_std_layout ( type-id )
4139 __is_trivial ( type-id )
4140 __is_union ( type-id )
4142 Objective-C++ Extension:
4144 primary-expression:
4145 objc-expression
4147 literal:
4148 __null
4150 ADDRESS_P is true iff this expression was immediately preceded by
4151 "&" and therefore might denote a pointer-to-member. CAST_P is true
4152 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4153 true iff this expression is a template argument.
4155 Returns a representation of the expression. Upon return, *IDK
4156 indicates what kind of id-expression (if any) was present. */
4158 static tree
4159 cp_parser_primary_expression (cp_parser *parser,
4160 bool address_p,
4161 bool cast_p,
4162 bool template_arg_p,
4163 bool decltype_p,
4164 cp_id_kind *idk)
4166 cp_token *token = NULL;
4168 /* Assume the primary expression is not an id-expression. */
4169 *idk = CP_ID_KIND_NONE;
4171 /* Peek at the next token. */
4172 token = cp_lexer_peek_token (parser->lexer);
4173 switch (token->type)
4175 /* literal:
4176 integer-literal
4177 character-literal
4178 floating-literal
4179 string-literal
4180 boolean-literal
4181 pointer-literal
4182 user-defined-literal */
4183 case CPP_CHAR:
4184 case CPP_CHAR16:
4185 case CPP_CHAR32:
4186 case CPP_WCHAR:
4187 case CPP_NUMBER:
4188 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4189 return cp_parser_userdef_numeric_literal (parser);
4190 token = cp_lexer_consume_token (parser->lexer);
4191 if (TREE_CODE (token->u.value) == FIXED_CST)
4193 error_at (token->location,
4194 "fixed-point types not supported in C++");
4195 return error_mark_node;
4197 /* Floating-point literals are only allowed in an integral
4198 constant expression if they are cast to an integral or
4199 enumeration type. */
4200 if (TREE_CODE (token->u.value) == REAL_CST
4201 && parser->integral_constant_expression_p
4202 && pedantic)
4204 /* CAST_P will be set even in invalid code like "int(2.7 +
4205 ...)". Therefore, we have to check that the next token
4206 is sure to end the cast. */
4207 if (cast_p)
4209 cp_token *next_token;
4211 next_token = cp_lexer_peek_token (parser->lexer);
4212 if (/* The comma at the end of an
4213 enumerator-definition. */
4214 next_token->type != CPP_COMMA
4215 /* The curly brace at the end of an enum-specifier. */
4216 && next_token->type != CPP_CLOSE_BRACE
4217 /* The end of a statement. */
4218 && next_token->type != CPP_SEMICOLON
4219 /* The end of the cast-expression. */
4220 && next_token->type != CPP_CLOSE_PAREN
4221 /* The end of an array bound. */
4222 && next_token->type != CPP_CLOSE_SQUARE
4223 /* The closing ">" in a template-argument-list. */
4224 && (next_token->type != CPP_GREATER
4225 || parser->greater_than_is_operator_p)
4226 /* C++0x only: A ">>" treated like two ">" tokens,
4227 in a template-argument-list. */
4228 && (next_token->type != CPP_RSHIFT
4229 || (cxx_dialect == cxx98)
4230 || parser->greater_than_is_operator_p))
4231 cast_p = false;
4234 /* If we are within a cast, then the constraint that the
4235 cast is to an integral or enumeration type will be
4236 checked at that point. If we are not within a cast, then
4237 this code is invalid. */
4238 if (!cast_p)
4239 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4241 return token->u.value;
4243 case CPP_CHAR_USERDEF:
4244 case CPP_CHAR16_USERDEF:
4245 case CPP_CHAR32_USERDEF:
4246 case CPP_WCHAR_USERDEF:
4247 return cp_parser_userdef_char_literal (parser);
4249 case CPP_STRING:
4250 case CPP_STRING16:
4251 case CPP_STRING32:
4252 case CPP_WSTRING:
4253 case CPP_UTF8STRING:
4254 case CPP_STRING_USERDEF:
4255 case CPP_STRING16_USERDEF:
4256 case CPP_STRING32_USERDEF:
4257 case CPP_WSTRING_USERDEF:
4258 case CPP_UTF8STRING_USERDEF:
4259 /* ??? Should wide strings be allowed when parser->translate_strings_p
4260 is false (i.e. in attributes)? If not, we can kill the third
4261 argument to cp_parser_string_literal. */
4262 return cp_parser_string_literal (parser,
4263 parser->translate_strings_p,
4264 true);
4266 case CPP_OPEN_PAREN:
4268 tree expr;
4269 bool saved_greater_than_is_operator_p;
4271 /* Consume the `('. */
4272 cp_lexer_consume_token (parser->lexer);
4273 /* Within a parenthesized expression, a `>' token is always
4274 the greater-than operator. */
4275 saved_greater_than_is_operator_p
4276 = parser->greater_than_is_operator_p;
4277 parser->greater_than_is_operator_p = true;
4278 /* If we see `( { ' then we are looking at the beginning of
4279 a GNU statement-expression. */
4280 if (cp_parser_allow_gnu_extensions_p (parser)
4281 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4283 /* Statement-expressions are not allowed by the standard. */
4284 pedwarn (token->location, OPT_Wpedantic,
4285 "ISO C++ forbids braced-groups within expressions");
4287 /* And they're not allowed outside of a function-body; you
4288 cannot, for example, write:
4290 int i = ({ int j = 3; j + 1; });
4292 at class or namespace scope. */
4293 if (!parser->in_function_body
4294 || parser->in_template_argument_list_p)
4296 error_at (token->location,
4297 "statement-expressions are not allowed outside "
4298 "functions nor in template-argument lists");
4299 cp_parser_skip_to_end_of_block_or_statement (parser);
4300 expr = error_mark_node;
4302 else
4304 /* Start the statement-expression. */
4305 expr = begin_stmt_expr ();
4306 /* Parse the compound-statement. */
4307 cp_parser_compound_statement (parser, expr, false, false);
4308 /* Finish up. */
4309 expr = finish_stmt_expr (expr, false);
4312 else
4314 /* Parse the parenthesized expression. */
4315 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4316 /* Let the front end know that this expression was
4317 enclosed in parentheses. This matters in case, for
4318 example, the expression is of the form `A::B', since
4319 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4320 not. */
4321 expr = finish_parenthesized_expr (expr);
4322 /* DR 705: Wrapping an unqualified name in parentheses
4323 suppresses arg-dependent lookup. We want to pass back
4324 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4325 (c++/37862), but none of the others. */
4326 if (*idk != CP_ID_KIND_QUALIFIED)
4327 *idk = CP_ID_KIND_NONE;
4329 /* The `>' token might be the end of a template-id or
4330 template-parameter-list now. */
4331 parser->greater_than_is_operator_p
4332 = saved_greater_than_is_operator_p;
4333 /* Consume the `)'. */
4334 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4335 cp_parser_skip_to_end_of_statement (parser);
4337 return expr;
4340 case CPP_OPEN_SQUARE:
4341 if (c_dialect_objc ())
4342 /* We have an Objective-C++ message. */
4343 return cp_parser_objc_expression (parser);
4345 tree lam = cp_parser_lambda_expression (parser);
4346 /* Don't warn about a failed tentative parse. */
4347 if (cp_parser_error_occurred (parser))
4348 return error_mark_node;
4349 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4350 return lam;
4353 case CPP_OBJC_STRING:
4354 if (c_dialect_objc ())
4355 /* We have an Objective-C++ string literal. */
4356 return cp_parser_objc_expression (parser);
4357 cp_parser_error (parser, "expected primary-expression");
4358 return error_mark_node;
4360 case CPP_KEYWORD:
4361 switch (token->keyword)
4363 /* These two are the boolean literals. */
4364 case RID_TRUE:
4365 cp_lexer_consume_token (parser->lexer);
4366 return boolean_true_node;
4367 case RID_FALSE:
4368 cp_lexer_consume_token (parser->lexer);
4369 return boolean_false_node;
4371 /* The `__null' literal. */
4372 case RID_NULL:
4373 cp_lexer_consume_token (parser->lexer);
4374 return null_node;
4376 /* The `nullptr' literal. */
4377 case RID_NULLPTR:
4378 cp_lexer_consume_token (parser->lexer);
4379 return nullptr_node;
4381 /* Recognize the `this' keyword. */
4382 case RID_THIS:
4383 cp_lexer_consume_token (parser->lexer);
4384 if (parser->local_variables_forbidden_p)
4386 error_at (token->location,
4387 "%<this%> may not be used in this context");
4388 return error_mark_node;
4390 /* Pointers cannot appear in constant-expressions. */
4391 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4392 return error_mark_node;
4393 return finish_this_expr ();
4395 /* The `operator' keyword can be the beginning of an
4396 id-expression. */
4397 case RID_OPERATOR:
4398 goto id_expression;
4400 case RID_FUNCTION_NAME:
4401 case RID_PRETTY_FUNCTION_NAME:
4402 case RID_C99_FUNCTION_NAME:
4404 non_integral_constant name;
4406 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4407 __func__ are the names of variables -- but they are
4408 treated specially. Therefore, they are handled here,
4409 rather than relying on the generic id-expression logic
4410 below. Grammatically, these names are id-expressions.
4412 Consume the token. */
4413 token = cp_lexer_consume_token (parser->lexer);
4415 switch (token->keyword)
4417 case RID_FUNCTION_NAME:
4418 name = NIC_FUNC_NAME;
4419 break;
4420 case RID_PRETTY_FUNCTION_NAME:
4421 name = NIC_PRETTY_FUNC;
4422 break;
4423 case RID_C99_FUNCTION_NAME:
4424 name = NIC_C99_FUNC;
4425 break;
4426 default:
4427 gcc_unreachable ();
4430 if (cp_parser_non_integral_constant_expression (parser, name))
4431 return error_mark_node;
4433 /* Look up the name. */
4434 return finish_fname (token->u.value);
4437 case RID_VA_ARG:
4439 tree expression;
4440 tree type;
4441 source_location type_location;
4443 /* The `__builtin_va_arg' construct is used to handle
4444 `va_arg'. Consume the `__builtin_va_arg' token. */
4445 cp_lexer_consume_token (parser->lexer);
4446 /* Look for the opening `('. */
4447 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4448 /* Now, parse the assignment-expression. */
4449 expression = cp_parser_assignment_expression (parser,
4450 /*cast_p=*/false, NULL);
4451 /* Look for the `,'. */
4452 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4453 type_location = cp_lexer_peek_token (parser->lexer)->location;
4454 /* Parse the type-id. */
4455 type = cp_parser_type_id (parser);
4456 /* Look for the closing `)'. */
4457 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4458 /* Using `va_arg' in a constant-expression is not
4459 allowed. */
4460 if (cp_parser_non_integral_constant_expression (parser,
4461 NIC_VA_ARG))
4462 return error_mark_node;
4463 return build_x_va_arg (type_location, expression, type);
4466 case RID_OFFSETOF:
4467 return cp_parser_builtin_offsetof (parser);
4469 case RID_HAS_NOTHROW_ASSIGN:
4470 case RID_HAS_NOTHROW_CONSTRUCTOR:
4471 case RID_HAS_NOTHROW_COPY:
4472 case RID_HAS_TRIVIAL_ASSIGN:
4473 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4474 case RID_HAS_TRIVIAL_COPY:
4475 case RID_HAS_TRIVIAL_DESTRUCTOR:
4476 case RID_HAS_VIRTUAL_DESTRUCTOR:
4477 case RID_IS_ABSTRACT:
4478 case RID_IS_BASE_OF:
4479 case RID_IS_CLASS:
4480 case RID_IS_CONVERTIBLE_TO:
4481 case RID_IS_EMPTY:
4482 case RID_IS_ENUM:
4483 case RID_IS_FINAL:
4484 case RID_IS_LITERAL_TYPE:
4485 case RID_IS_POD:
4486 case RID_IS_POLYMORPHIC:
4487 case RID_IS_STD_LAYOUT:
4488 case RID_IS_TRIVIAL:
4489 case RID_IS_UNION:
4490 return cp_parser_trait_expr (parser, token->keyword);
4492 /* Objective-C++ expressions. */
4493 case RID_AT_ENCODE:
4494 case RID_AT_PROTOCOL:
4495 case RID_AT_SELECTOR:
4496 return cp_parser_objc_expression (parser);
4498 case RID_TEMPLATE:
4499 if (parser->in_function_body
4500 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4501 == CPP_LESS))
4503 error_at (token->location,
4504 "a template declaration cannot appear at block scope");
4505 cp_parser_skip_to_end_of_block_or_statement (parser);
4506 return error_mark_node;
4508 default:
4509 cp_parser_error (parser, "expected primary-expression");
4510 return error_mark_node;
4513 /* An id-expression can start with either an identifier, a
4514 `::' as the beginning of a qualified-id, or the "operator"
4515 keyword. */
4516 case CPP_NAME:
4517 case CPP_SCOPE:
4518 case CPP_TEMPLATE_ID:
4519 case CPP_NESTED_NAME_SPECIFIER:
4521 tree id_expression;
4522 tree decl;
4523 const char *error_msg;
4524 bool template_p;
4525 bool done;
4526 cp_token *id_expr_token;
4528 id_expression:
4529 /* Parse the id-expression. */
4530 id_expression
4531 = cp_parser_id_expression (parser,
4532 /*template_keyword_p=*/false,
4533 /*check_dependency_p=*/true,
4534 &template_p,
4535 /*declarator_p=*/false,
4536 /*optional_p=*/false);
4537 if (id_expression == error_mark_node)
4538 return error_mark_node;
4539 id_expr_token = token;
4540 token = cp_lexer_peek_token (parser->lexer);
4541 done = (token->type != CPP_OPEN_SQUARE
4542 && token->type != CPP_OPEN_PAREN
4543 && token->type != CPP_DOT
4544 && token->type != CPP_DEREF
4545 && token->type != CPP_PLUS_PLUS
4546 && token->type != CPP_MINUS_MINUS);
4547 /* If we have a template-id, then no further lookup is
4548 required. If the template-id was for a template-class, we
4549 will sometimes have a TYPE_DECL at this point. */
4550 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4551 || TREE_CODE (id_expression) == TYPE_DECL)
4552 decl = id_expression;
4553 /* Look up the name. */
4554 else
4556 tree ambiguous_decls;
4558 /* If we already know that this lookup is ambiguous, then
4559 we've already issued an error message; there's no reason
4560 to check again. */
4561 if (id_expr_token->type == CPP_NAME
4562 && id_expr_token->error_reported)
4564 cp_parser_simulate_error (parser);
4565 return error_mark_node;
4568 decl = cp_parser_lookup_name (parser, id_expression,
4569 none_type,
4570 template_p,
4571 /*is_namespace=*/false,
4572 /*check_dependency=*/true,
4573 &ambiguous_decls,
4574 id_expr_token->location);
4575 /* If the lookup was ambiguous, an error will already have
4576 been issued. */
4577 if (ambiguous_decls)
4578 return error_mark_node;
4580 /* In Objective-C++, we may have an Objective-C 2.0
4581 dot-syntax for classes here. */
4582 if (c_dialect_objc ()
4583 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4584 && TREE_CODE (decl) == TYPE_DECL
4585 && objc_is_class_name (decl))
4587 tree component;
4588 cp_lexer_consume_token (parser->lexer);
4589 component = cp_parser_identifier (parser);
4590 if (component == error_mark_node)
4591 return error_mark_node;
4593 return objc_build_class_component_ref (id_expression, component);
4596 /* In Objective-C++, an instance variable (ivar) may be preferred
4597 to whatever cp_parser_lookup_name() found. */
4598 decl = objc_lookup_ivar (decl, id_expression);
4600 /* If name lookup gives us a SCOPE_REF, then the
4601 qualifying scope was dependent. */
4602 if (TREE_CODE (decl) == SCOPE_REF)
4604 /* At this point, we do not know if DECL is a valid
4605 integral constant expression. We assume that it is
4606 in fact such an expression, so that code like:
4608 template <int N> struct A {
4609 int a[B<N>::i];
4612 is accepted. At template-instantiation time, we
4613 will check that B<N>::i is actually a constant. */
4614 return decl;
4616 /* Check to see if DECL is a local variable in a context
4617 where that is forbidden. */
4618 if (parser->local_variables_forbidden_p
4619 && local_variable_p (decl))
4621 /* It might be that we only found DECL because we are
4622 trying to be generous with pre-ISO scoping rules.
4623 For example, consider:
4625 int i;
4626 void g() {
4627 for (int i = 0; i < 10; ++i) {}
4628 extern void f(int j = i);
4631 Here, name look up will originally find the out
4632 of scope `i'. We need to issue a warning message,
4633 but then use the global `i'. */
4634 decl = check_for_out_of_scope_variable (decl);
4635 if (local_variable_p (decl))
4637 error_at (id_expr_token->location,
4638 "local variable %qD may not appear in this context",
4639 decl);
4640 return error_mark_node;
4645 decl = (finish_id_expression
4646 (id_expression, decl, parser->scope,
4647 idk,
4648 parser->integral_constant_expression_p,
4649 parser->allow_non_integral_constant_expression_p,
4650 &parser->non_integral_constant_expression_p,
4651 template_p, done, address_p,
4652 template_arg_p,
4653 &error_msg,
4654 id_expr_token->location));
4655 if (error_msg)
4656 cp_parser_error (parser, error_msg);
4657 return decl;
4660 /* Anything else is an error. */
4661 default:
4662 cp_parser_error (parser, "expected primary-expression");
4663 return error_mark_node;
4667 static inline tree
4668 cp_parser_primary_expression (cp_parser *parser,
4669 bool address_p,
4670 bool cast_p,
4671 bool template_arg_p,
4672 cp_id_kind *idk)
4674 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4675 /*decltype*/false, idk);
4678 /* Parse an id-expression.
4680 id-expression:
4681 unqualified-id
4682 qualified-id
4684 qualified-id:
4685 :: [opt] nested-name-specifier template [opt] unqualified-id
4686 :: identifier
4687 :: operator-function-id
4688 :: template-id
4690 Return a representation of the unqualified portion of the
4691 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4692 a `::' or nested-name-specifier.
4694 Often, if the id-expression was a qualified-id, the caller will
4695 want to make a SCOPE_REF to represent the qualified-id. This
4696 function does not do this in order to avoid wastefully creating
4697 SCOPE_REFs when they are not required.
4699 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4700 `template' keyword.
4702 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4703 uninstantiated templates.
4705 If *TEMPLATE_P is non-NULL, it is set to true iff the
4706 `template' keyword is used to explicitly indicate that the entity
4707 named is a template.
4709 If DECLARATOR_P is true, the id-expression is appearing as part of
4710 a declarator, rather than as part of an expression. */
4712 static tree
4713 cp_parser_id_expression (cp_parser *parser,
4714 bool template_keyword_p,
4715 bool check_dependency_p,
4716 bool *template_p,
4717 bool declarator_p,
4718 bool optional_p)
4720 bool global_scope_p;
4721 bool nested_name_specifier_p;
4723 /* Assume the `template' keyword was not used. */
4724 if (template_p)
4725 *template_p = template_keyword_p;
4727 /* Look for the optional `::' operator. */
4728 global_scope_p
4729 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4730 != NULL_TREE);
4731 /* Look for the optional nested-name-specifier. */
4732 nested_name_specifier_p
4733 = (cp_parser_nested_name_specifier_opt (parser,
4734 /*typename_keyword_p=*/false,
4735 check_dependency_p,
4736 /*type_p=*/false,
4737 declarator_p)
4738 != NULL_TREE);
4739 /* If there is a nested-name-specifier, then we are looking at
4740 the first qualified-id production. */
4741 if (nested_name_specifier_p)
4743 tree saved_scope;
4744 tree saved_object_scope;
4745 tree saved_qualifying_scope;
4746 tree unqualified_id;
4747 bool is_template;
4749 /* See if the next token is the `template' keyword. */
4750 if (!template_p)
4751 template_p = &is_template;
4752 *template_p = cp_parser_optional_template_keyword (parser);
4753 /* Name lookup we do during the processing of the
4754 unqualified-id might obliterate SCOPE. */
4755 saved_scope = parser->scope;
4756 saved_object_scope = parser->object_scope;
4757 saved_qualifying_scope = parser->qualifying_scope;
4758 /* Process the final unqualified-id. */
4759 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4760 check_dependency_p,
4761 declarator_p,
4762 /*optional_p=*/false);
4763 /* Restore the SAVED_SCOPE for our caller. */
4764 parser->scope = saved_scope;
4765 parser->object_scope = saved_object_scope;
4766 parser->qualifying_scope = saved_qualifying_scope;
4768 return unqualified_id;
4770 /* Otherwise, if we are in global scope, then we are looking at one
4771 of the other qualified-id productions. */
4772 else if (global_scope_p)
4774 cp_token *token;
4775 tree id;
4777 /* Peek at the next token. */
4778 token = cp_lexer_peek_token (parser->lexer);
4780 /* If it's an identifier, and the next token is not a "<", then
4781 we can avoid the template-id case. This is an optimization
4782 for this common case. */
4783 if (token->type == CPP_NAME
4784 && !cp_parser_nth_token_starts_template_argument_list_p
4785 (parser, 2))
4786 return cp_parser_identifier (parser);
4788 cp_parser_parse_tentatively (parser);
4789 /* Try a template-id. */
4790 id = cp_parser_template_id (parser,
4791 /*template_keyword_p=*/false,
4792 /*check_dependency_p=*/true,
4793 none_type,
4794 declarator_p);
4795 /* If that worked, we're done. */
4796 if (cp_parser_parse_definitely (parser))
4797 return id;
4799 /* Peek at the next token. (Changes in the token buffer may
4800 have invalidated the pointer obtained above.) */
4801 token = cp_lexer_peek_token (parser->lexer);
4803 switch (token->type)
4805 case CPP_NAME:
4806 return cp_parser_identifier (parser);
4808 case CPP_KEYWORD:
4809 if (token->keyword == RID_OPERATOR)
4810 return cp_parser_operator_function_id (parser);
4811 /* Fall through. */
4813 default:
4814 cp_parser_error (parser, "expected id-expression");
4815 return error_mark_node;
4818 else
4819 return cp_parser_unqualified_id (parser, template_keyword_p,
4820 /*check_dependency_p=*/true,
4821 declarator_p,
4822 optional_p);
4825 /* Parse an unqualified-id.
4827 unqualified-id:
4828 identifier
4829 operator-function-id
4830 conversion-function-id
4831 ~ class-name
4832 template-id
4834 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4835 keyword, in a construct like `A::template ...'.
4837 Returns a representation of unqualified-id. For the `identifier'
4838 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4839 production a BIT_NOT_EXPR is returned; the operand of the
4840 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4841 other productions, see the documentation accompanying the
4842 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4843 names are looked up in uninstantiated templates. If DECLARATOR_P
4844 is true, the unqualified-id is appearing as part of a declarator,
4845 rather than as part of an expression. */
4847 static tree
4848 cp_parser_unqualified_id (cp_parser* parser,
4849 bool template_keyword_p,
4850 bool check_dependency_p,
4851 bool declarator_p,
4852 bool optional_p)
4854 cp_token *token;
4856 /* Peek at the next token. */
4857 token = cp_lexer_peek_token (parser->lexer);
4859 switch (token->type)
4861 case CPP_NAME:
4863 tree id;
4865 /* We don't know yet whether or not this will be a
4866 template-id. */
4867 cp_parser_parse_tentatively (parser);
4868 /* Try a template-id. */
4869 id = cp_parser_template_id (parser, template_keyword_p,
4870 check_dependency_p,
4871 none_type,
4872 declarator_p);
4873 /* If it worked, we're done. */
4874 if (cp_parser_parse_definitely (parser))
4875 return id;
4876 /* Otherwise, it's an ordinary identifier. */
4877 return cp_parser_identifier (parser);
4880 case CPP_TEMPLATE_ID:
4881 return cp_parser_template_id (parser, template_keyword_p,
4882 check_dependency_p,
4883 none_type,
4884 declarator_p);
4886 case CPP_COMPL:
4888 tree type_decl;
4889 tree qualifying_scope;
4890 tree object_scope;
4891 tree scope;
4892 bool done;
4894 /* Consume the `~' token. */
4895 cp_lexer_consume_token (parser->lexer);
4896 /* Parse the class-name. The standard, as written, seems to
4897 say that:
4899 template <typename T> struct S { ~S (); };
4900 template <typename T> S<T>::~S() {}
4902 is invalid, since `~' must be followed by a class-name, but
4903 `S<T>' is dependent, and so not known to be a class.
4904 That's not right; we need to look in uninstantiated
4905 templates. A further complication arises from:
4907 template <typename T> void f(T t) {
4908 t.T::~T();
4911 Here, it is not possible to look up `T' in the scope of `T'
4912 itself. We must look in both the current scope, and the
4913 scope of the containing complete expression.
4915 Yet another issue is:
4917 struct S {
4918 int S;
4919 ~S();
4922 S::~S() {}
4924 The standard does not seem to say that the `S' in `~S'
4925 should refer to the type `S' and not the data member
4926 `S::S'. */
4928 /* DR 244 says that we look up the name after the "~" in the
4929 same scope as we looked up the qualifying name. That idea
4930 isn't fully worked out; it's more complicated than that. */
4931 scope = parser->scope;
4932 object_scope = parser->object_scope;
4933 qualifying_scope = parser->qualifying_scope;
4935 /* Check for invalid scopes. */
4936 if (scope == error_mark_node)
4938 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4939 cp_lexer_consume_token (parser->lexer);
4940 return error_mark_node;
4942 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4944 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4945 error_at (token->location,
4946 "scope %qT before %<~%> is not a class-name",
4947 scope);
4948 cp_parser_simulate_error (parser);
4949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4950 cp_lexer_consume_token (parser->lexer);
4951 return error_mark_node;
4953 gcc_assert (!scope || TYPE_P (scope));
4955 /* If the name is of the form "X::~X" it's OK even if X is a
4956 typedef. */
4957 token = cp_lexer_peek_token (parser->lexer);
4958 if (scope
4959 && token->type == CPP_NAME
4960 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4961 != CPP_LESS)
4962 && (token->u.value == TYPE_IDENTIFIER (scope)
4963 || (CLASS_TYPE_P (scope)
4964 && constructor_name_p (token->u.value, scope))))
4966 cp_lexer_consume_token (parser->lexer);
4967 return build_nt (BIT_NOT_EXPR, scope);
4970 /* ~auto means the destructor of whatever the object is. */
4971 if (cp_parser_is_keyword (token, RID_AUTO))
4973 if (cxx_dialect < cxx14)
4974 pedwarn (input_location, 0,
4975 "%<~auto%> only available with "
4976 "-std=c++14 or -std=gnu++14");
4977 cp_lexer_consume_token (parser->lexer);
4978 return build_nt (BIT_NOT_EXPR, make_auto ());
4981 /* If there was an explicit qualification (S::~T), first look
4982 in the scope given by the qualification (i.e., S).
4984 Note: in the calls to cp_parser_class_name below we pass
4985 typename_type so that lookup finds the injected-class-name
4986 rather than the constructor. */
4987 done = false;
4988 type_decl = NULL_TREE;
4989 if (scope)
4991 cp_parser_parse_tentatively (parser);
4992 type_decl = cp_parser_class_name (parser,
4993 /*typename_keyword_p=*/false,
4994 /*template_keyword_p=*/false,
4995 typename_type,
4996 /*check_dependency=*/false,
4997 /*class_head_p=*/false,
4998 declarator_p);
4999 if (cp_parser_parse_definitely (parser))
5000 done = true;
5002 /* In "N::S::~S", look in "N" as well. */
5003 if (!done && scope && qualifying_scope)
5005 cp_parser_parse_tentatively (parser);
5006 parser->scope = qualifying_scope;
5007 parser->object_scope = NULL_TREE;
5008 parser->qualifying_scope = NULL_TREE;
5009 type_decl
5010 = cp_parser_class_name (parser,
5011 /*typename_keyword_p=*/false,
5012 /*template_keyword_p=*/false,
5013 typename_type,
5014 /*check_dependency=*/false,
5015 /*class_head_p=*/false,
5016 declarator_p);
5017 if (cp_parser_parse_definitely (parser))
5018 done = true;
5020 /* In "p->S::~T", look in the scope given by "*p" as well. */
5021 else if (!done && object_scope)
5023 cp_parser_parse_tentatively (parser);
5024 parser->scope = object_scope;
5025 parser->object_scope = NULL_TREE;
5026 parser->qualifying_scope = NULL_TREE;
5027 type_decl
5028 = cp_parser_class_name (parser,
5029 /*typename_keyword_p=*/false,
5030 /*template_keyword_p=*/false,
5031 typename_type,
5032 /*check_dependency=*/false,
5033 /*class_head_p=*/false,
5034 declarator_p);
5035 if (cp_parser_parse_definitely (parser))
5036 done = true;
5038 /* Look in the surrounding context. */
5039 if (!done)
5041 parser->scope = NULL_TREE;
5042 parser->object_scope = NULL_TREE;
5043 parser->qualifying_scope = NULL_TREE;
5044 if (processing_template_decl)
5045 cp_parser_parse_tentatively (parser);
5046 type_decl
5047 = cp_parser_class_name (parser,
5048 /*typename_keyword_p=*/false,
5049 /*template_keyword_p=*/false,
5050 typename_type,
5051 /*check_dependency=*/false,
5052 /*class_head_p=*/false,
5053 declarator_p);
5054 if (processing_template_decl
5055 && ! cp_parser_parse_definitely (parser))
5057 /* We couldn't find a type with this name, so just accept
5058 it and check for a match at instantiation time. */
5059 type_decl = cp_parser_identifier (parser);
5060 if (type_decl != error_mark_node)
5061 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5062 return type_decl;
5065 /* If an error occurred, assume that the name of the
5066 destructor is the same as the name of the qualifying
5067 class. That allows us to keep parsing after running
5068 into ill-formed destructor names. */
5069 if (type_decl == error_mark_node && scope)
5070 return build_nt (BIT_NOT_EXPR, scope);
5071 else if (type_decl == error_mark_node)
5072 return error_mark_node;
5074 /* Check that destructor name and scope match. */
5075 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5077 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5078 error_at (token->location,
5079 "declaration of %<~%T%> as member of %qT",
5080 type_decl, scope);
5081 cp_parser_simulate_error (parser);
5082 return error_mark_node;
5085 /* [class.dtor]
5087 A typedef-name that names a class shall not be used as the
5088 identifier in the declarator for a destructor declaration. */
5089 if (declarator_p
5090 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5091 && !DECL_SELF_REFERENCE_P (type_decl)
5092 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5093 error_at (token->location,
5094 "typedef-name %qD used as destructor declarator",
5095 type_decl);
5097 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5100 case CPP_KEYWORD:
5101 if (token->keyword == RID_OPERATOR)
5103 tree id;
5105 /* This could be a template-id, so we try that first. */
5106 cp_parser_parse_tentatively (parser);
5107 /* Try a template-id. */
5108 id = cp_parser_template_id (parser, template_keyword_p,
5109 /*check_dependency_p=*/true,
5110 none_type,
5111 declarator_p);
5112 /* If that worked, we're done. */
5113 if (cp_parser_parse_definitely (parser))
5114 return id;
5115 /* We still don't know whether we're looking at an
5116 operator-function-id or a conversion-function-id. */
5117 cp_parser_parse_tentatively (parser);
5118 /* Try an operator-function-id. */
5119 id = cp_parser_operator_function_id (parser);
5120 /* If that didn't work, try a conversion-function-id. */
5121 if (!cp_parser_parse_definitely (parser))
5122 id = cp_parser_conversion_function_id (parser);
5123 else if (UDLIT_OPER_P (id))
5125 /* 17.6.3.3.5 */
5126 const char *name = UDLIT_OP_SUFFIX (id);
5127 if (name[0] != '_' && !in_system_header_at (input_location)
5128 && declarator_p)
5129 warning (0, "literal operator suffixes not preceded by %<_%>"
5130 " are reserved for future standardization");
5133 return id;
5135 /* Fall through. */
5137 default:
5138 if (optional_p)
5139 return NULL_TREE;
5140 cp_parser_error (parser, "expected unqualified-id");
5141 return error_mark_node;
5145 /* Parse an (optional) nested-name-specifier.
5147 nested-name-specifier: [C++98]
5148 class-or-namespace-name :: nested-name-specifier [opt]
5149 class-or-namespace-name :: template nested-name-specifier [opt]
5151 nested-name-specifier: [C++0x]
5152 type-name ::
5153 namespace-name ::
5154 nested-name-specifier identifier ::
5155 nested-name-specifier template [opt] simple-template-id ::
5157 PARSER->SCOPE should be set appropriately before this function is
5158 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5159 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5160 in name lookups.
5162 Sets PARSER->SCOPE to the class (TYPE) or namespace
5163 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5164 it unchanged if there is no nested-name-specifier. Returns the new
5165 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5167 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5168 part of a declaration and/or decl-specifier. */
5170 static tree
5171 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5172 bool typename_keyword_p,
5173 bool check_dependency_p,
5174 bool type_p,
5175 bool is_declaration)
5177 bool success = false;
5178 cp_token_position start = 0;
5179 cp_token *token;
5181 /* Remember where the nested-name-specifier starts. */
5182 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5184 start = cp_lexer_token_position (parser->lexer, false);
5185 push_deferring_access_checks (dk_deferred);
5188 while (true)
5190 tree new_scope;
5191 tree old_scope;
5192 tree saved_qualifying_scope;
5193 bool template_keyword_p;
5195 /* Spot cases that cannot be the beginning of a
5196 nested-name-specifier. */
5197 token = cp_lexer_peek_token (parser->lexer);
5199 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5200 the already parsed nested-name-specifier. */
5201 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5203 /* Grab the nested-name-specifier and continue the loop. */
5204 cp_parser_pre_parsed_nested_name_specifier (parser);
5205 /* If we originally encountered this nested-name-specifier
5206 with IS_DECLARATION set to false, we will not have
5207 resolved TYPENAME_TYPEs, so we must do so here. */
5208 if (is_declaration
5209 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5211 new_scope = resolve_typename_type (parser->scope,
5212 /*only_current_p=*/false);
5213 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5214 parser->scope = new_scope;
5216 success = true;
5217 continue;
5220 /* Spot cases that cannot be the beginning of a
5221 nested-name-specifier. On the second and subsequent times
5222 through the loop, we look for the `template' keyword. */
5223 if (success && token->keyword == RID_TEMPLATE)
5225 /* A template-id can start a nested-name-specifier. */
5226 else if (token->type == CPP_TEMPLATE_ID)
5228 /* DR 743: decltype can be used in a nested-name-specifier. */
5229 else if (token_is_decltype (token))
5231 else
5233 /* If the next token is not an identifier, then it is
5234 definitely not a type-name or namespace-name. */
5235 if (token->type != CPP_NAME)
5236 break;
5237 /* If the following token is neither a `<' (to begin a
5238 template-id), nor a `::', then we are not looking at a
5239 nested-name-specifier. */
5240 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5242 if (token->type == CPP_COLON
5243 && parser->colon_corrects_to_scope_p
5244 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5246 error_at (token->location,
5247 "found %<:%> in nested-name-specifier, expected %<::%>");
5248 token->type = CPP_SCOPE;
5251 if (token->type != CPP_SCOPE
5252 && !cp_parser_nth_token_starts_template_argument_list_p
5253 (parser, 2))
5254 break;
5257 /* The nested-name-specifier is optional, so we parse
5258 tentatively. */
5259 cp_parser_parse_tentatively (parser);
5261 /* Look for the optional `template' keyword, if this isn't the
5262 first time through the loop. */
5263 if (success)
5264 template_keyword_p = cp_parser_optional_template_keyword (parser);
5265 else
5266 template_keyword_p = false;
5268 /* Save the old scope since the name lookup we are about to do
5269 might destroy it. */
5270 old_scope = parser->scope;
5271 saved_qualifying_scope = parser->qualifying_scope;
5272 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5273 look up names in "X<T>::I" in order to determine that "Y" is
5274 a template. So, if we have a typename at this point, we make
5275 an effort to look through it. */
5276 if (is_declaration
5277 && !typename_keyword_p
5278 && parser->scope
5279 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5280 parser->scope = resolve_typename_type (parser->scope,
5281 /*only_current_p=*/false);
5282 /* Parse the qualifying entity. */
5283 new_scope
5284 = cp_parser_qualifying_entity (parser,
5285 typename_keyword_p,
5286 template_keyword_p,
5287 check_dependency_p,
5288 type_p,
5289 is_declaration);
5290 /* Look for the `::' token. */
5291 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5293 /* If we found what we wanted, we keep going; otherwise, we're
5294 done. */
5295 if (!cp_parser_parse_definitely (parser))
5297 bool error_p = false;
5299 /* Restore the OLD_SCOPE since it was valid before the
5300 failed attempt at finding the last
5301 class-or-namespace-name. */
5302 parser->scope = old_scope;
5303 parser->qualifying_scope = saved_qualifying_scope;
5305 /* If the next token is a decltype, and the one after that is a
5306 `::', then the decltype has failed to resolve to a class or
5307 enumeration type. Give this error even when parsing
5308 tentatively since it can't possibly be valid--and we're going
5309 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5310 won't get another chance.*/
5311 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5312 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5313 == CPP_SCOPE))
5315 token = cp_lexer_consume_token (parser->lexer);
5316 error_at (token->location, "decltype evaluates to %qT, "
5317 "which is not a class or enumeration type",
5318 token->u.value);
5319 parser->scope = error_mark_node;
5320 error_p = true;
5321 /* As below. */
5322 success = true;
5323 cp_lexer_consume_token (parser->lexer);
5326 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5327 break;
5328 /* If the next token is an identifier, and the one after
5329 that is a `::', then any valid interpretation would have
5330 found a class-or-namespace-name. */
5331 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5332 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5333 == CPP_SCOPE)
5334 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5335 != CPP_COMPL))
5337 token = cp_lexer_consume_token (parser->lexer);
5338 if (!error_p)
5340 if (!token->error_reported)
5342 tree decl;
5343 tree ambiguous_decls;
5345 decl = cp_parser_lookup_name (parser, token->u.value,
5346 none_type,
5347 /*is_template=*/false,
5348 /*is_namespace=*/false,
5349 /*check_dependency=*/true,
5350 &ambiguous_decls,
5351 token->location);
5352 if (TREE_CODE (decl) == TEMPLATE_DECL)
5353 error_at (token->location,
5354 "%qD used without template parameters",
5355 decl);
5356 else if (ambiguous_decls)
5358 // cp_parser_lookup_name has the same diagnostic,
5359 // thus make sure to emit it at most once.
5360 if (cp_parser_uncommitted_to_tentative_parse_p
5361 (parser))
5363 error_at (token->location,
5364 "reference to %qD is ambiguous",
5365 token->u.value);
5366 print_candidates (ambiguous_decls);
5368 decl = error_mark_node;
5370 else
5372 if (cxx_dialect != cxx98)
5373 cp_parser_name_lookup_error
5374 (parser, token->u.value, decl, NLE_NOT_CXX98,
5375 token->location);
5376 else
5377 cp_parser_name_lookup_error
5378 (parser, token->u.value, decl, NLE_CXX98,
5379 token->location);
5382 parser->scope = error_mark_node;
5383 error_p = true;
5384 /* Treat this as a successful nested-name-specifier
5385 due to:
5387 [basic.lookup.qual]
5389 If the name found is not a class-name (clause
5390 _class_) or namespace-name (_namespace.def_), the
5391 program is ill-formed. */
5392 success = true;
5394 cp_lexer_consume_token (parser->lexer);
5396 break;
5398 /* We've found one valid nested-name-specifier. */
5399 success = true;
5400 /* Name lookup always gives us a DECL. */
5401 if (TREE_CODE (new_scope) == TYPE_DECL)
5402 new_scope = TREE_TYPE (new_scope);
5403 /* Uses of "template" must be followed by actual templates. */
5404 if (template_keyword_p
5405 && !(CLASS_TYPE_P (new_scope)
5406 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5407 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5408 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5409 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5410 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5411 == TEMPLATE_ID_EXPR)))
5412 permerror (input_location, TYPE_P (new_scope)
5413 ? G_("%qT is not a template")
5414 : G_("%qD is not a template"),
5415 new_scope);
5416 /* If it is a class scope, try to complete it; we are about to
5417 be looking up names inside the class. */
5418 if (TYPE_P (new_scope)
5419 /* Since checking types for dependency can be expensive,
5420 avoid doing it if the type is already complete. */
5421 && !COMPLETE_TYPE_P (new_scope)
5422 /* Do not try to complete dependent types. */
5423 && !dependent_type_p (new_scope))
5425 new_scope = complete_type (new_scope);
5426 /* If it is a typedef to current class, use the current
5427 class instead, as the typedef won't have any names inside
5428 it yet. */
5429 if (!COMPLETE_TYPE_P (new_scope)
5430 && currently_open_class (new_scope))
5431 new_scope = TYPE_MAIN_VARIANT (new_scope);
5433 /* Make sure we look in the right scope the next time through
5434 the loop. */
5435 parser->scope = new_scope;
5438 /* If parsing tentatively, replace the sequence of tokens that makes
5439 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5440 token. That way, should we re-parse the token stream, we will
5441 not have to repeat the effort required to do the parse, nor will
5442 we issue duplicate error messages. */
5443 if (success && start)
5445 cp_token *token;
5447 token = cp_lexer_token_at (parser->lexer, start);
5448 /* Reset the contents of the START token. */
5449 token->type = CPP_NESTED_NAME_SPECIFIER;
5450 /* Retrieve any deferred checks. Do not pop this access checks yet
5451 so the memory will not be reclaimed during token replacing below. */
5452 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5453 token->u.tree_check_value->value = parser->scope;
5454 token->u.tree_check_value->checks = get_deferred_access_checks ();
5455 token->u.tree_check_value->qualifying_scope =
5456 parser->qualifying_scope;
5457 token->keyword = RID_MAX;
5459 /* Purge all subsequent tokens. */
5460 cp_lexer_purge_tokens_after (parser->lexer, start);
5463 if (start)
5464 pop_to_parent_deferring_access_checks ();
5466 return success ? parser->scope : NULL_TREE;
5469 /* Parse a nested-name-specifier. See
5470 cp_parser_nested_name_specifier_opt for details. This function
5471 behaves identically, except that it will an issue an error if no
5472 nested-name-specifier is present. */
5474 static tree
5475 cp_parser_nested_name_specifier (cp_parser *parser,
5476 bool typename_keyword_p,
5477 bool check_dependency_p,
5478 bool type_p,
5479 bool is_declaration)
5481 tree scope;
5483 /* Look for the nested-name-specifier. */
5484 scope = cp_parser_nested_name_specifier_opt (parser,
5485 typename_keyword_p,
5486 check_dependency_p,
5487 type_p,
5488 is_declaration);
5489 /* If it was not present, issue an error message. */
5490 if (!scope)
5492 cp_parser_error (parser, "expected nested-name-specifier");
5493 parser->scope = NULL_TREE;
5496 return scope;
5499 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5500 this is either a class-name or a namespace-name (which corresponds
5501 to the class-or-namespace-name production in the grammar). For
5502 C++0x, it can also be a type-name that refers to an enumeration
5503 type or a simple-template-id.
5505 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5506 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5507 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5508 TYPE_P is TRUE iff the next name should be taken as a class-name,
5509 even the same name is declared to be another entity in the same
5510 scope.
5512 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5513 specified by the class-or-namespace-name. If neither is found the
5514 ERROR_MARK_NODE is returned. */
5516 static tree
5517 cp_parser_qualifying_entity (cp_parser *parser,
5518 bool typename_keyword_p,
5519 bool template_keyword_p,
5520 bool check_dependency_p,
5521 bool type_p,
5522 bool is_declaration)
5524 tree saved_scope;
5525 tree saved_qualifying_scope;
5526 tree saved_object_scope;
5527 tree scope;
5528 bool only_class_p;
5529 bool successful_parse_p;
5531 /* DR 743: decltype can appear in a nested-name-specifier. */
5532 if (cp_lexer_next_token_is_decltype (parser->lexer))
5534 scope = cp_parser_decltype (parser);
5535 if (TREE_CODE (scope) != ENUMERAL_TYPE
5536 && !MAYBE_CLASS_TYPE_P (scope))
5538 cp_parser_simulate_error (parser);
5539 return error_mark_node;
5541 if (TYPE_NAME (scope))
5542 scope = TYPE_NAME (scope);
5543 return scope;
5546 /* Before we try to parse the class-name, we must save away the
5547 current PARSER->SCOPE since cp_parser_class_name will destroy
5548 it. */
5549 saved_scope = parser->scope;
5550 saved_qualifying_scope = parser->qualifying_scope;
5551 saved_object_scope = parser->object_scope;
5552 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5553 there is no need to look for a namespace-name. */
5554 only_class_p = template_keyword_p
5555 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5556 if (!only_class_p)
5557 cp_parser_parse_tentatively (parser);
5558 scope = cp_parser_class_name (parser,
5559 typename_keyword_p,
5560 template_keyword_p,
5561 type_p ? class_type : none_type,
5562 check_dependency_p,
5563 /*class_head_p=*/false,
5564 is_declaration);
5565 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5566 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5567 if (!only_class_p
5568 && cxx_dialect != cxx98
5569 && !successful_parse_p)
5571 /* Restore the saved scope. */
5572 parser->scope = saved_scope;
5573 parser->qualifying_scope = saved_qualifying_scope;
5574 parser->object_scope = saved_object_scope;
5576 /* Parse tentatively. */
5577 cp_parser_parse_tentatively (parser);
5579 /* Parse a type-name */
5580 scope = cp_parser_type_name (parser);
5582 /* "If the name found does not designate a namespace or a class,
5583 enumeration, or dependent type, the program is ill-formed."
5585 We cover classes and dependent types above and namespaces below,
5586 so this code is only looking for enums. */
5587 if (!scope || TREE_CODE (scope) != TYPE_DECL
5588 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5589 cp_parser_simulate_error (parser);
5591 successful_parse_p = cp_parser_parse_definitely (parser);
5593 /* If that didn't work, try for a namespace-name. */
5594 if (!only_class_p && !successful_parse_p)
5596 /* Restore the saved scope. */
5597 parser->scope = saved_scope;
5598 parser->qualifying_scope = saved_qualifying_scope;
5599 parser->object_scope = saved_object_scope;
5600 /* If we are not looking at an identifier followed by the scope
5601 resolution operator, then this is not part of a
5602 nested-name-specifier. (Note that this function is only used
5603 to parse the components of a nested-name-specifier.) */
5604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5605 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5606 return error_mark_node;
5607 scope = cp_parser_namespace_name (parser);
5610 return scope;
5613 /* Return true if we are looking at a compound-literal, false otherwise. */
5615 static bool
5616 cp_parser_compound_literal_p (cp_parser *parser)
5618 /* Consume the `('. */
5619 cp_lexer_consume_token (parser->lexer);
5621 cp_lexer_save_tokens (parser->lexer);
5623 /* Skip tokens until the next token is a closing parenthesis.
5624 If we find the closing `)', and the next token is a `{', then
5625 we are looking at a compound-literal. */
5626 bool compound_literal_p
5627 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5628 /*consume_paren=*/true)
5629 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5631 /* Roll back the tokens we skipped. */
5632 cp_lexer_rollback_tokens (parser->lexer);
5634 return compound_literal_p;
5637 /* Parse a postfix-expression.
5639 postfix-expression:
5640 primary-expression
5641 postfix-expression [ expression ]
5642 postfix-expression ( expression-list [opt] )
5643 simple-type-specifier ( expression-list [opt] )
5644 typename :: [opt] nested-name-specifier identifier
5645 ( expression-list [opt] )
5646 typename :: [opt] nested-name-specifier template [opt] template-id
5647 ( expression-list [opt] )
5648 postfix-expression . template [opt] id-expression
5649 postfix-expression -> template [opt] id-expression
5650 postfix-expression . pseudo-destructor-name
5651 postfix-expression -> pseudo-destructor-name
5652 postfix-expression ++
5653 postfix-expression --
5654 dynamic_cast < type-id > ( expression )
5655 static_cast < type-id > ( expression )
5656 reinterpret_cast < type-id > ( expression )
5657 const_cast < type-id > ( expression )
5658 typeid ( expression )
5659 typeid ( type-id )
5661 GNU Extension:
5663 postfix-expression:
5664 ( type-id ) { initializer-list , [opt] }
5666 This extension is a GNU version of the C99 compound-literal
5667 construct. (The C99 grammar uses `type-name' instead of `type-id',
5668 but they are essentially the same concept.)
5670 If ADDRESS_P is true, the postfix expression is the operand of the
5671 `&' operator. CAST_P is true if this expression is the target of a
5672 cast.
5674 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5675 class member access expressions [expr.ref].
5677 Returns a representation of the expression. */
5679 static tree
5680 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5681 bool member_access_only_p, bool decltype_p,
5682 cp_id_kind * pidk_return)
5684 cp_token *token;
5685 location_t loc;
5686 enum rid keyword;
5687 cp_id_kind idk = CP_ID_KIND_NONE;
5688 tree postfix_expression = NULL_TREE;
5689 bool is_member_access = false;
5690 int saved_in_statement = -1;
5692 /* Peek at the next token. */
5693 token = cp_lexer_peek_token (parser->lexer);
5694 loc = token->location;
5695 /* Some of the productions are determined by keywords. */
5696 keyword = token->keyword;
5697 switch (keyword)
5699 case RID_DYNCAST:
5700 case RID_STATCAST:
5701 case RID_REINTCAST:
5702 case RID_CONSTCAST:
5704 tree type;
5705 tree expression;
5706 const char *saved_message;
5707 bool saved_in_type_id_in_expr_p;
5709 /* All of these can be handled in the same way from the point
5710 of view of parsing. Begin by consuming the token
5711 identifying the cast. */
5712 cp_lexer_consume_token (parser->lexer);
5714 /* New types cannot be defined in the cast. */
5715 saved_message = parser->type_definition_forbidden_message;
5716 parser->type_definition_forbidden_message
5717 = G_("types may not be defined in casts");
5719 /* Look for the opening `<'. */
5720 cp_parser_require (parser, CPP_LESS, RT_LESS);
5721 /* Parse the type to which we are casting. */
5722 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5723 parser->in_type_id_in_expr_p = true;
5724 type = cp_parser_type_id (parser);
5725 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5726 /* Look for the closing `>'. */
5727 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5728 /* Restore the old message. */
5729 parser->type_definition_forbidden_message = saved_message;
5731 bool saved_greater_than_is_operator_p
5732 = parser->greater_than_is_operator_p;
5733 parser->greater_than_is_operator_p = true;
5735 /* And the expression which is being cast. */
5736 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5737 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5738 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5740 parser->greater_than_is_operator_p
5741 = saved_greater_than_is_operator_p;
5743 /* Only type conversions to integral or enumeration types
5744 can be used in constant-expressions. */
5745 if (!cast_valid_in_integral_constant_expression_p (type)
5746 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5747 return error_mark_node;
5749 switch (keyword)
5751 case RID_DYNCAST:
5752 postfix_expression
5753 = build_dynamic_cast (type, expression, tf_warning_or_error);
5754 break;
5755 case RID_STATCAST:
5756 postfix_expression
5757 = build_static_cast (type, expression, tf_warning_or_error);
5758 break;
5759 case RID_REINTCAST:
5760 postfix_expression
5761 = build_reinterpret_cast (type, expression,
5762 tf_warning_or_error);
5763 break;
5764 case RID_CONSTCAST:
5765 postfix_expression
5766 = build_const_cast (type, expression, tf_warning_or_error);
5767 break;
5768 default:
5769 gcc_unreachable ();
5772 break;
5774 case RID_TYPEID:
5776 tree type;
5777 const char *saved_message;
5778 bool saved_in_type_id_in_expr_p;
5780 /* Consume the `typeid' token. */
5781 cp_lexer_consume_token (parser->lexer);
5782 /* Look for the `(' token. */
5783 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5784 /* Types cannot be defined in a `typeid' expression. */
5785 saved_message = parser->type_definition_forbidden_message;
5786 parser->type_definition_forbidden_message
5787 = G_("types may not be defined in a %<typeid%> expression");
5788 /* We can't be sure yet whether we're looking at a type-id or an
5789 expression. */
5790 cp_parser_parse_tentatively (parser);
5791 /* Try a type-id first. */
5792 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5793 parser->in_type_id_in_expr_p = true;
5794 type = cp_parser_type_id (parser);
5795 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5796 /* Look for the `)' token. Otherwise, we can't be sure that
5797 we're not looking at an expression: consider `typeid (int
5798 (3))', for example. */
5799 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5800 /* If all went well, simply lookup the type-id. */
5801 if (cp_parser_parse_definitely (parser))
5802 postfix_expression = get_typeid (type, tf_warning_or_error);
5803 /* Otherwise, fall back to the expression variant. */
5804 else
5806 tree expression;
5808 /* Look for an expression. */
5809 expression = cp_parser_expression (parser, & idk);
5810 /* Compute its typeid. */
5811 postfix_expression = build_typeid (expression, tf_warning_or_error);
5812 /* Look for the `)' token. */
5813 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5815 /* Restore the saved message. */
5816 parser->type_definition_forbidden_message = saved_message;
5817 /* `typeid' may not appear in an integral constant expression. */
5818 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5819 return error_mark_node;
5821 break;
5823 case RID_TYPENAME:
5825 tree type;
5826 /* The syntax permitted here is the same permitted for an
5827 elaborated-type-specifier. */
5828 type = cp_parser_elaborated_type_specifier (parser,
5829 /*is_friend=*/false,
5830 /*is_declaration=*/false);
5831 postfix_expression = cp_parser_functional_cast (parser, type);
5833 break;
5835 case RID_CILK_SPAWN:
5837 cp_lexer_consume_token (parser->lexer);
5838 token = cp_lexer_peek_token (parser->lexer);
5839 if (token->type == CPP_SEMICOLON)
5841 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5842 "an expression");
5843 postfix_expression = error_mark_node;
5844 break;
5846 else if (!current_function_decl)
5848 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5849 "inside a function");
5850 postfix_expression = error_mark_node;
5851 break;
5853 else
5855 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5856 saved_in_statement = parser->in_statement;
5857 parser->in_statement |= IN_CILK_SPAWN;
5859 cfun->calls_cilk_spawn = 1;
5860 postfix_expression =
5861 cp_parser_postfix_expression (parser, false, false,
5862 false, false, &idk);
5863 if (!flag_cilkplus)
5865 error_at (token->location, "-fcilkplus must be enabled to use"
5866 " %<_Cilk_spawn%>");
5867 cfun->calls_cilk_spawn = 0;
5869 else if (saved_in_statement & IN_CILK_SPAWN)
5871 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5872 "are not permitted");
5873 postfix_expression = error_mark_node;
5874 cfun->calls_cilk_spawn = 0;
5876 else
5878 postfix_expression = build_cilk_spawn (token->location,
5879 postfix_expression);
5880 if (postfix_expression != error_mark_node)
5881 SET_EXPR_LOCATION (postfix_expression, input_location);
5882 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5884 break;
5887 case RID_BUILTIN_SHUFFLE:
5889 vec<tree, va_gc> *vec;
5890 unsigned int i;
5891 tree p;
5893 cp_lexer_consume_token (parser->lexer);
5894 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5895 /*cast_p=*/false, /*allow_expansion_p=*/true,
5896 /*non_constant_p=*/NULL);
5897 if (vec == NULL)
5898 return error_mark_node;
5900 FOR_EACH_VEC_ELT (*vec, i, p)
5901 mark_exp_read (p);
5903 if (vec->length () == 2)
5904 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5905 tf_warning_or_error);
5906 else if (vec->length () == 3)
5907 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5908 tf_warning_or_error);
5909 else
5911 error_at (loc, "wrong number of arguments to "
5912 "%<__builtin_shuffle%>");
5913 return error_mark_node;
5915 break;
5918 default:
5920 tree type;
5922 /* If the next thing is a simple-type-specifier, we may be
5923 looking at a functional cast. We could also be looking at
5924 an id-expression. So, we try the functional cast, and if
5925 that doesn't work we fall back to the primary-expression. */
5926 cp_parser_parse_tentatively (parser);
5927 /* Look for the simple-type-specifier. */
5928 type = cp_parser_simple_type_specifier (parser,
5929 /*decl_specs=*/NULL,
5930 CP_PARSER_FLAGS_NONE);
5931 /* Parse the cast itself. */
5932 if (!cp_parser_error_occurred (parser))
5933 postfix_expression
5934 = cp_parser_functional_cast (parser, type);
5935 /* If that worked, we're done. */
5936 if (cp_parser_parse_definitely (parser))
5937 break;
5939 /* If the functional-cast didn't work out, try a
5940 compound-literal. */
5941 if (cp_parser_allow_gnu_extensions_p (parser)
5942 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5944 tree initializer = NULL_TREE;
5946 cp_parser_parse_tentatively (parser);
5948 /* Avoid calling cp_parser_type_id pointlessly, see comment
5949 in cp_parser_cast_expression about c++/29234. */
5950 if (!cp_parser_compound_literal_p (parser))
5951 cp_parser_simulate_error (parser);
5952 else
5954 /* Parse the type. */
5955 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5956 parser->in_type_id_in_expr_p = true;
5957 type = cp_parser_type_id (parser);
5958 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5959 /* Look for the `)'. */
5960 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5963 /* If things aren't going well, there's no need to
5964 keep going. */
5965 if (!cp_parser_error_occurred (parser))
5967 bool non_constant_p;
5968 /* Parse the brace-enclosed initializer list. */
5969 initializer = cp_parser_braced_list (parser,
5970 &non_constant_p);
5972 /* If that worked, we're definitely looking at a
5973 compound-literal expression. */
5974 if (cp_parser_parse_definitely (parser))
5976 /* Warn the user that a compound literal is not
5977 allowed in standard C++. */
5978 pedwarn (input_location, OPT_Wpedantic,
5979 "ISO C++ forbids compound-literals");
5980 /* For simplicity, we disallow compound literals in
5981 constant-expressions. We could
5982 allow compound literals of integer type, whose
5983 initializer was a constant, in constant
5984 expressions. Permitting that usage, as a further
5985 extension, would not change the meaning of any
5986 currently accepted programs. (Of course, as
5987 compound literals are not part of ISO C++, the
5988 standard has nothing to say.) */
5989 if (cp_parser_non_integral_constant_expression (parser,
5990 NIC_NCC))
5992 postfix_expression = error_mark_node;
5993 break;
5995 /* Form the representation of the compound-literal. */
5996 postfix_expression
5997 = finish_compound_literal (type, initializer,
5998 tf_warning_or_error);
5999 break;
6003 /* It must be a primary-expression. */
6004 postfix_expression
6005 = cp_parser_primary_expression (parser, address_p, cast_p,
6006 /*template_arg_p=*/false,
6007 decltype_p,
6008 &idk);
6010 break;
6013 /* Note that we don't need to worry about calling build_cplus_new on a
6014 class-valued CALL_EXPR in decltype when it isn't the end of the
6015 postfix-expression; unary_complex_lvalue will take care of that for
6016 all these cases. */
6018 /* Keep looping until the postfix-expression is complete. */
6019 while (true)
6021 if (idk == CP_ID_KIND_UNQUALIFIED
6022 && identifier_p (postfix_expression)
6023 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6024 /* It is not a Koenig lookup function call. */
6025 postfix_expression
6026 = unqualified_name_lookup_error (postfix_expression);
6028 /* Peek at the next token. */
6029 token = cp_lexer_peek_token (parser->lexer);
6031 switch (token->type)
6033 case CPP_OPEN_SQUARE:
6034 if (cp_next_tokens_can_be_std_attribute_p (parser))
6036 cp_parser_error (parser,
6037 "two consecutive %<[%> shall "
6038 "only introduce an attribute");
6039 return error_mark_node;
6041 postfix_expression
6042 = cp_parser_postfix_open_square_expression (parser,
6043 postfix_expression,
6044 false,
6045 decltype_p);
6046 idk = CP_ID_KIND_NONE;
6047 is_member_access = false;
6048 break;
6050 case CPP_OPEN_PAREN:
6051 /* postfix-expression ( expression-list [opt] ) */
6053 bool koenig_p;
6054 bool is_builtin_constant_p;
6055 bool saved_integral_constant_expression_p = false;
6056 bool saved_non_integral_constant_expression_p = false;
6057 tsubst_flags_t complain = complain_flags (decltype_p);
6058 vec<tree, va_gc> *args;
6060 is_member_access = false;
6062 is_builtin_constant_p
6063 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6064 if (is_builtin_constant_p)
6066 /* The whole point of __builtin_constant_p is to allow
6067 non-constant expressions to appear as arguments. */
6068 saved_integral_constant_expression_p
6069 = parser->integral_constant_expression_p;
6070 saved_non_integral_constant_expression_p
6071 = parser->non_integral_constant_expression_p;
6072 parser->integral_constant_expression_p = false;
6074 args = (cp_parser_parenthesized_expression_list
6075 (parser, non_attr,
6076 /*cast_p=*/false, /*allow_expansion_p=*/true,
6077 /*non_constant_p=*/NULL,
6078 /*want_literal_zero_p=*/warn_memset_transposed_args));
6079 if (is_builtin_constant_p)
6081 parser->integral_constant_expression_p
6082 = saved_integral_constant_expression_p;
6083 parser->non_integral_constant_expression_p
6084 = saved_non_integral_constant_expression_p;
6087 if (args == NULL)
6089 postfix_expression = error_mark_node;
6090 break;
6093 /* Function calls are not permitted in
6094 constant-expressions. */
6095 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6096 && cp_parser_non_integral_constant_expression (parser,
6097 NIC_FUNC_CALL))
6099 postfix_expression = error_mark_node;
6100 release_tree_vector (args);
6101 break;
6104 koenig_p = false;
6105 if (idk == CP_ID_KIND_UNQUALIFIED
6106 || idk == CP_ID_KIND_TEMPLATE_ID)
6108 if (identifier_p (postfix_expression))
6110 if (!args->is_empty ())
6112 koenig_p = true;
6113 if (!any_type_dependent_arguments_p (args))
6114 postfix_expression
6115 = perform_koenig_lookup (postfix_expression, args,
6116 complain);
6118 else
6119 postfix_expression
6120 = unqualified_fn_lookup_error (postfix_expression);
6122 /* We do not perform argument-dependent lookup if
6123 normal lookup finds a non-function, in accordance
6124 with the expected resolution of DR 218. */
6125 else if (!args->is_empty ()
6126 && is_overloaded_fn (postfix_expression))
6128 tree fn = get_first_fn (postfix_expression);
6129 fn = STRIP_TEMPLATE (fn);
6131 /* Do not do argument dependent lookup if regular
6132 lookup finds a member function or a block-scope
6133 function declaration. [basic.lookup.argdep]/3 */
6134 if (!DECL_FUNCTION_MEMBER_P (fn)
6135 && !DECL_LOCAL_FUNCTION_P (fn))
6137 koenig_p = true;
6138 if (!any_type_dependent_arguments_p (args))
6139 postfix_expression
6140 = perform_koenig_lookup (postfix_expression, args,
6141 complain);
6146 if (warn_memset_transposed_args)
6148 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6149 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6150 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6151 && vec_safe_length (args) == 3
6152 && integer_zerop ((*args)[2])
6153 && LITERAL_ZERO_P ((*args)[2])
6154 && !(integer_zerop ((*args)[1])
6155 && LITERAL_ZERO_P ((*args)[1])))
6156 warning (OPT_Wmemset_transposed_args,
6157 "%<memset%> used with constant zero length "
6158 "parameter; this could be due to transposed "
6159 "parameters");
6161 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6162 to avoid leaking those into folder and middle-end. */
6163 unsigned int i;
6164 tree arg;
6165 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6166 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6167 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6170 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6172 tree instance = TREE_OPERAND (postfix_expression, 0);
6173 tree fn = TREE_OPERAND (postfix_expression, 1);
6175 if (processing_template_decl
6176 && (type_dependent_expression_p (instance)
6177 || (!BASELINK_P (fn)
6178 && TREE_CODE (fn) != FIELD_DECL)
6179 || type_dependent_expression_p (fn)
6180 || any_type_dependent_arguments_p (args)))
6182 postfix_expression
6183 = build_nt_call_vec (postfix_expression, args);
6184 release_tree_vector (args);
6185 break;
6188 if (BASELINK_P (fn))
6190 postfix_expression
6191 = (build_new_method_call
6192 (instance, fn, &args, NULL_TREE,
6193 (idk == CP_ID_KIND_QUALIFIED
6194 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6195 : LOOKUP_NORMAL),
6196 /*fn_p=*/NULL,
6197 complain));
6199 else
6200 postfix_expression
6201 = finish_call_expr (postfix_expression, &args,
6202 /*disallow_virtual=*/false,
6203 /*koenig_p=*/false,
6204 complain);
6206 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6207 || TREE_CODE (postfix_expression) == MEMBER_REF
6208 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6209 postfix_expression = (build_offset_ref_call_from_tree
6210 (postfix_expression, &args,
6211 complain));
6212 else if (idk == CP_ID_KIND_QUALIFIED)
6213 /* A call to a static class member, or a namespace-scope
6214 function. */
6215 postfix_expression
6216 = finish_call_expr (postfix_expression, &args,
6217 /*disallow_virtual=*/true,
6218 koenig_p,
6219 complain);
6220 else
6221 /* All other function calls. */
6222 postfix_expression
6223 = finish_call_expr (postfix_expression, &args,
6224 /*disallow_virtual=*/false,
6225 koenig_p,
6226 complain);
6228 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6229 idk = CP_ID_KIND_NONE;
6231 release_tree_vector (args);
6233 break;
6235 case CPP_DOT:
6236 case CPP_DEREF:
6237 /* postfix-expression . template [opt] id-expression
6238 postfix-expression . pseudo-destructor-name
6239 postfix-expression -> template [opt] id-expression
6240 postfix-expression -> pseudo-destructor-name */
6242 /* Consume the `.' or `->' operator. */
6243 cp_lexer_consume_token (parser->lexer);
6245 postfix_expression
6246 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6247 postfix_expression,
6248 false, &idk, loc);
6250 is_member_access = true;
6251 break;
6253 case CPP_PLUS_PLUS:
6254 /* postfix-expression ++ */
6255 /* Consume the `++' token. */
6256 cp_lexer_consume_token (parser->lexer);
6257 /* Generate a representation for the complete expression. */
6258 postfix_expression
6259 = finish_increment_expr (postfix_expression,
6260 POSTINCREMENT_EXPR);
6261 /* Increments may not appear in constant-expressions. */
6262 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6263 postfix_expression = error_mark_node;
6264 idk = CP_ID_KIND_NONE;
6265 is_member_access = false;
6266 break;
6268 case CPP_MINUS_MINUS:
6269 /* postfix-expression -- */
6270 /* Consume the `--' token. */
6271 cp_lexer_consume_token (parser->lexer);
6272 /* Generate a representation for the complete expression. */
6273 postfix_expression
6274 = finish_increment_expr (postfix_expression,
6275 POSTDECREMENT_EXPR);
6276 /* Decrements may not appear in constant-expressions. */
6277 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6278 postfix_expression = error_mark_node;
6279 idk = CP_ID_KIND_NONE;
6280 is_member_access = false;
6281 break;
6283 default:
6284 if (pidk_return != NULL)
6285 * pidk_return = idk;
6286 if (member_access_only_p)
6287 return is_member_access? postfix_expression : error_mark_node;
6288 else
6289 return postfix_expression;
6293 /* We should never get here. */
6294 gcc_unreachable ();
6295 return error_mark_node;
6298 /* This function parses Cilk Plus array notations. If a normal array expr. is
6299 parsed then the array index is passed back to the caller through *INIT_INDEX
6300 and the function returns a NULL_TREE. If array notation expr. is parsed,
6301 then *INIT_INDEX is ignored by the caller and the function returns
6302 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6303 error_mark_node. */
6305 static tree
6306 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6307 tree array_value)
6309 cp_token *token = NULL;
6310 tree length_index, stride = NULL_TREE, value_tree, array_type;
6311 if (!array_value || array_value == error_mark_node)
6313 cp_parser_skip_to_end_of_statement (parser);
6314 return error_mark_node;
6317 array_type = TREE_TYPE (array_value);
6319 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6320 parser->colon_corrects_to_scope_p = false;
6321 token = cp_lexer_peek_token (parser->lexer);
6323 if (!token)
6325 cp_parser_error (parser, "expected %<:%> or numeral");
6326 return error_mark_node;
6328 else if (token->type == CPP_COLON)
6330 /* Consume the ':'. */
6331 cp_lexer_consume_token (parser->lexer);
6333 /* If we are here, then we have a case like this A[:]. */
6334 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6336 cp_parser_error (parser, "expected %<]%>");
6337 cp_parser_skip_to_end_of_statement (parser);
6338 return error_mark_node;
6340 *init_index = NULL_TREE;
6341 stride = NULL_TREE;
6342 length_index = NULL_TREE;
6344 else
6346 /* If we are here, then there are three valid possibilities:
6347 1. ARRAY [ EXP ]
6348 2. ARRAY [ EXP : EXP ]
6349 3. ARRAY [ EXP : EXP : EXP ] */
6351 *init_index = cp_parser_expression (parser);
6352 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6354 /* This indicates that we have a normal array expression. */
6355 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6356 return NULL_TREE;
6359 /* Consume the ':'. */
6360 cp_lexer_consume_token (parser->lexer);
6361 length_index = cp_parser_expression (parser);
6362 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6364 cp_lexer_consume_token (parser->lexer);
6365 stride = cp_parser_expression (parser);
6368 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6370 if (*init_index == error_mark_node || length_index == error_mark_node
6371 || stride == error_mark_node || array_type == error_mark_node)
6373 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6374 cp_lexer_consume_token (parser->lexer);
6375 return error_mark_node;
6377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6379 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6380 length_index, stride, array_type);
6381 return value_tree;
6384 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6385 by cp_parser_builtin_offsetof. We're looking for
6387 postfix-expression [ expression ]
6388 postfix-expression [ braced-init-list ] (C++11)
6390 FOR_OFFSETOF is set if we're being called in that context, which
6391 changes how we deal with integer constant expressions. */
6393 static tree
6394 cp_parser_postfix_open_square_expression (cp_parser *parser,
6395 tree postfix_expression,
6396 bool for_offsetof,
6397 bool decltype_p)
6399 tree index = NULL_TREE;
6400 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6401 bool saved_greater_than_is_operator_p;
6403 /* Consume the `[' token. */
6404 cp_lexer_consume_token (parser->lexer);
6406 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6407 parser->greater_than_is_operator_p = true;
6409 /* Parse the index expression. */
6410 /* ??? For offsetof, there is a question of what to allow here. If
6411 offsetof is not being used in an integral constant expression context,
6412 then we *could* get the right answer by computing the value at runtime.
6413 If we are in an integral constant expression context, then we might
6414 could accept any constant expression; hard to say without analysis.
6415 Rather than open the barn door too wide right away, allow only integer
6416 constant expressions here. */
6417 if (for_offsetof)
6418 index = cp_parser_constant_expression (parser, false, NULL);
6419 else
6421 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6423 bool expr_nonconst_p;
6424 cp_lexer_set_source_position (parser->lexer);
6425 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6426 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6427 if (flag_cilkplus
6428 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6430 error_at (cp_lexer_peek_token (parser->lexer)->location,
6431 "braced list index is not allowed with array "
6432 "notation");
6433 cp_parser_skip_to_end_of_statement (parser);
6434 return error_mark_node;
6437 else if (flag_cilkplus)
6439 /* Here are have these two options:
6440 ARRAY[EXP : EXP] - Array notation expr with default
6441 stride of 1.
6442 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6443 stride. */
6444 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6445 postfix_expression);
6446 if (an_exp)
6447 return an_exp;
6449 else
6450 index = cp_parser_expression (parser);
6453 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6455 /* Look for the closing `]'. */
6456 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6458 /* Build the ARRAY_REF. */
6459 postfix_expression = grok_array_decl (loc, postfix_expression,
6460 index, decltype_p);
6462 /* When not doing offsetof, array references are not permitted in
6463 constant-expressions. */
6464 if (!for_offsetof
6465 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6466 postfix_expression = error_mark_node;
6468 return postfix_expression;
6471 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6472 by cp_parser_builtin_offsetof. We're looking for
6474 postfix-expression . template [opt] id-expression
6475 postfix-expression . pseudo-destructor-name
6476 postfix-expression -> template [opt] id-expression
6477 postfix-expression -> pseudo-destructor-name
6479 FOR_OFFSETOF is set if we're being called in that context. That sorta
6480 limits what of the above we'll actually accept, but nevermind.
6481 TOKEN_TYPE is the "." or "->" token, which will already have been
6482 removed from the stream. */
6484 static tree
6485 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6486 enum cpp_ttype token_type,
6487 tree postfix_expression,
6488 bool for_offsetof, cp_id_kind *idk,
6489 location_t location)
6491 tree name;
6492 bool dependent_p;
6493 bool pseudo_destructor_p;
6494 tree scope = NULL_TREE;
6496 /* If this is a `->' operator, dereference the pointer. */
6497 if (token_type == CPP_DEREF)
6498 postfix_expression = build_x_arrow (location, postfix_expression,
6499 tf_warning_or_error);
6500 /* Check to see whether or not the expression is type-dependent. */
6501 dependent_p = type_dependent_expression_p (postfix_expression);
6502 /* The identifier following the `->' or `.' is not qualified. */
6503 parser->scope = NULL_TREE;
6504 parser->qualifying_scope = NULL_TREE;
6505 parser->object_scope = NULL_TREE;
6506 *idk = CP_ID_KIND_NONE;
6508 /* Enter the scope corresponding to the type of the object
6509 given by the POSTFIX_EXPRESSION. */
6510 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6512 scope = TREE_TYPE (postfix_expression);
6513 /* According to the standard, no expression should ever have
6514 reference type. Unfortunately, we do not currently match
6515 the standard in this respect in that our internal representation
6516 of an expression may have reference type even when the standard
6517 says it does not. Therefore, we have to manually obtain the
6518 underlying type here. */
6519 scope = non_reference (scope);
6520 /* The type of the POSTFIX_EXPRESSION must be complete. */
6521 if (scope == unknown_type_node)
6523 error_at (location, "%qE does not have class type",
6524 postfix_expression);
6525 scope = NULL_TREE;
6527 /* Unlike the object expression in other contexts, *this is not
6528 required to be of complete type for purposes of class member
6529 access (5.2.5) outside the member function body. */
6530 else if (postfix_expression != current_class_ref
6531 && !(processing_template_decl && scope == current_class_type))
6532 scope = complete_type_or_else (scope, NULL_TREE);
6533 /* Let the name lookup machinery know that we are processing a
6534 class member access expression. */
6535 parser->context->object_type = scope;
6536 /* If something went wrong, we want to be able to discern that case,
6537 as opposed to the case where there was no SCOPE due to the type
6538 of expression being dependent. */
6539 if (!scope)
6540 scope = error_mark_node;
6541 /* If the SCOPE was erroneous, make the various semantic analysis
6542 functions exit quickly -- and without issuing additional error
6543 messages. */
6544 if (scope == error_mark_node)
6545 postfix_expression = error_mark_node;
6548 /* Assume this expression is not a pseudo-destructor access. */
6549 pseudo_destructor_p = false;
6551 /* If the SCOPE is a scalar type, then, if this is a valid program,
6552 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6553 is type dependent, it can be pseudo-destructor-name or something else.
6554 Try to parse it as pseudo-destructor-name first. */
6555 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6557 tree s;
6558 tree type;
6560 cp_parser_parse_tentatively (parser);
6561 /* Parse the pseudo-destructor-name. */
6562 s = NULL_TREE;
6563 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6564 &s, &type);
6565 if (dependent_p
6566 && (cp_parser_error_occurred (parser)
6567 || !SCALAR_TYPE_P (type)))
6568 cp_parser_abort_tentative_parse (parser);
6569 else if (cp_parser_parse_definitely (parser))
6571 pseudo_destructor_p = true;
6572 postfix_expression
6573 = finish_pseudo_destructor_expr (postfix_expression,
6574 s, type, location);
6578 if (!pseudo_destructor_p)
6580 /* If the SCOPE is not a scalar type, we are looking at an
6581 ordinary class member access expression, rather than a
6582 pseudo-destructor-name. */
6583 bool template_p;
6584 cp_token *token = cp_lexer_peek_token (parser->lexer);
6585 /* Parse the id-expression. */
6586 name = (cp_parser_id_expression
6587 (parser,
6588 cp_parser_optional_template_keyword (parser),
6589 /*check_dependency_p=*/true,
6590 &template_p,
6591 /*declarator_p=*/false,
6592 /*optional_p=*/false));
6593 /* In general, build a SCOPE_REF if the member name is qualified.
6594 However, if the name was not dependent and has already been
6595 resolved; there is no need to build the SCOPE_REF. For example;
6597 struct X { void f(); };
6598 template <typename T> void f(T* t) { t->X::f(); }
6600 Even though "t" is dependent, "X::f" is not and has been resolved
6601 to a BASELINK; there is no need to include scope information. */
6603 /* But we do need to remember that there was an explicit scope for
6604 virtual function calls. */
6605 if (parser->scope)
6606 *idk = CP_ID_KIND_QUALIFIED;
6608 /* If the name is a template-id that names a type, we will get a
6609 TYPE_DECL here. That is invalid code. */
6610 if (TREE_CODE (name) == TYPE_DECL)
6612 error_at (token->location, "invalid use of %qD", name);
6613 postfix_expression = error_mark_node;
6615 else
6617 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6619 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6621 error_at (token->location, "%<%D::%D%> is not a class member",
6622 parser->scope, name);
6623 postfix_expression = error_mark_node;
6625 else
6626 name = build_qualified_name (/*type=*/NULL_TREE,
6627 parser->scope,
6628 name,
6629 template_p);
6630 parser->scope = NULL_TREE;
6631 parser->qualifying_scope = NULL_TREE;
6632 parser->object_scope = NULL_TREE;
6634 if (parser->scope && name && BASELINK_P (name))
6635 adjust_result_of_qualified_name_lookup
6636 (name, parser->scope, scope);
6637 postfix_expression
6638 = finish_class_member_access_expr (postfix_expression, name,
6639 template_p,
6640 tf_warning_or_error);
6644 /* We no longer need to look up names in the scope of the object on
6645 the left-hand side of the `.' or `->' operator. */
6646 parser->context->object_type = NULL_TREE;
6648 /* Outside of offsetof, these operators may not appear in
6649 constant-expressions. */
6650 if (!for_offsetof
6651 && (cp_parser_non_integral_constant_expression
6652 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6653 postfix_expression = error_mark_node;
6655 return postfix_expression;
6658 /* Cache of LITERAL_ZERO_P constants. */
6660 static GTY(()) tree literal_zeros[itk_none];
6662 /* Parse a parenthesized expression-list.
6664 expression-list:
6665 assignment-expression
6666 expression-list, assignment-expression
6668 attribute-list:
6669 expression-list
6670 identifier
6671 identifier, expression-list
6673 CAST_P is true if this expression is the target of a cast.
6675 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6676 argument pack.
6678 Returns a vector of trees. Each element is a representation of an
6679 assignment-expression. NULL is returned if the ( and or ) are
6680 missing. An empty, but allocated, vector is returned on no
6681 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6682 if we are parsing an attribute list for an attribute that wants a
6683 plain identifier argument, normal_attr for an attribute that wants
6684 an expression, or non_attr if we aren't parsing an attribute list. If
6685 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6686 not all of the expressions in the list were constant.
6687 WANT_LITERAL_ZERO_P is true if the caller is interested in
6688 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6689 immediately, this can be removed. */
6691 static vec<tree, va_gc> *
6692 cp_parser_parenthesized_expression_list (cp_parser* parser,
6693 int is_attribute_list,
6694 bool cast_p,
6695 bool allow_expansion_p,
6696 bool *non_constant_p,
6697 bool want_literal_zero_p)
6699 vec<tree, va_gc> *expression_list;
6700 bool fold_expr_p = is_attribute_list != non_attr;
6701 tree identifier = NULL_TREE;
6702 bool saved_greater_than_is_operator_p;
6704 /* Assume all the expressions will be constant. */
6705 if (non_constant_p)
6706 *non_constant_p = false;
6708 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6709 return NULL;
6711 expression_list = make_tree_vector ();
6713 /* Within a parenthesized expression, a `>' token is always
6714 the greater-than operator. */
6715 saved_greater_than_is_operator_p
6716 = parser->greater_than_is_operator_p;
6717 parser->greater_than_is_operator_p = true;
6719 /* Consume expressions until there are no more. */
6720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6721 while (true)
6723 tree expr;
6725 /* At the beginning of attribute lists, check to see if the
6726 next token is an identifier. */
6727 if (is_attribute_list == id_attr
6728 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6730 cp_token *token;
6732 /* Consume the identifier. */
6733 token = cp_lexer_consume_token (parser->lexer);
6734 /* Save the identifier. */
6735 identifier = token->u.value;
6737 else
6739 bool expr_non_constant_p;
6741 /* Parse the next assignment-expression. */
6742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6744 /* A braced-init-list. */
6745 cp_lexer_set_source_position (parser->lexer);
6746 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6747 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6748 if (non_constant_p && expr_non_constant_p)
6749 *non_constant_p = true;
6751 else if (non_constant_p)
6753 expr = (cp_parser_constant_expression
6754 (parser, /*allow_non_constant_p=*/true,
6755 &expr_non_constant_p));
6756 if (expr_non_constant_p)
6757 *non_constant_p = true;
6759 else
6761 expr = NULL_TREE;
6762 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6763 switch (tok->type)
6765 case CPP_NUMBER:
6766 case CPP_CHAR:
6767 case CPP_WCHAR:
6768 case CPP_CHAR16:
6769 case CPP_CHAR32:
6770 /* If a parameter is literal zero alone, remember it
6771 for -Wmemset-transposed-args warning. */
6772 if (integer_zerop (tok->u.value)
6773 && !TREE_OVERFLOW (tok->u.value)
6774 && want_literal_zero_p
6775 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6776 == CPP_COMMA
6777 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6778 == CPP_CLOSE_PAREN))
6780 unsigned int i;
6781 for (i = 0; i < itk_none; ++i)
6782 if (TREE_TYPE (tok->u.value) == integer_types[i])
6783 break;
6784 if (i < itk_none && literal_zeros[i])
6785 expr = literal_zeros[i];
6786 else
6788 expr = copy_node (tok->u.value);
6789 LITERAL_ZERO_P (expr) = 1;
6790 if (i < itk_none)
6791 literal_zeros[i] = expr;
6793 /* Consume the 0 token (or '\0', 0LL etc.). */
6794 cp_lexer_consume_token (parser->lexer);
6796 break;
6797 default:
6798 break;
6800 if (expr == NULL_TREE)
6801 expr = cp_parser_assignment_expression (parser, cast_p,
6802 NULL);
6805 if (fold_expr_p)
6806 expr = fold_non_dependent_expr (expr);
6808 /* If we have an ellipsis, then this is an expression
6809 expansion. */
6810 if (allow_expansion_p
6811 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6813 /* Consume the `...'. */
6814 cp_lexer_consume_token (parser->lexer);
6816 /* Build the argument pack. */
6817 expr = make_pack_expansion (expr);
6820 /* Add it to the list. We add error_mark_node
6821 expressions to the list, so that we can still tell if
6822 the correct form for a parenthesized expression-list
6823 is found. That gives better errors. */
6824 vec_safe_push (expression_list, expr);
6826 if (expr == error_mark_node)
6827 goto skip_comma;
6830 /* After the first item, attribute lists look the same as
6831 expression lists. */
6832 is_attribute_list = non_attr;
6834 get_comma:;
6835 /* If the next token isn't a `,', then we are done. */
6836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6837 break;
6839 /* Otherwise, consume the `,' and keep going. */
6840 cp_lexer_consume_token (parser->lexer);
6843 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6845 int ending;
6847 skip_comma:;
6848 /* We try and resync to an unnested comma, as that will give the
6849 user better diagnostics. */
6850 ending = cp_parser_skip_to_closing_parenthesis (parser,
6851 /*recovering=*/true,
6852 /*or_comma=*/true,
6853 /*consume_paren=*/true);
6854 if (ending < 0)
6855 goto get_comma;
6856 if (!ending)
6858 parser->greater_than_is_operator_p
6859 = saved_greater_than_is_operator_p;
6860 return NULL;
6864 parser->greater_than_is_operator_p
6865 = saved_greater_than_is_operator_p;
6867 if (identifier)
6868 vec_safe_insert (expression_list, 0, identifier);
6870 return expression_list;
6873 /* Parse a pseudo-destructor-name.
6875 pseudo-destructor-name:
6876 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6877 :: [opt] nested-name-specifier template template-id :: ~ type-name
6878 :: [opt] nested-name-specifier [opt] ~ type-name
6880 If either of the first two productions is used, sets *SCOPE to the
6881 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6882 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6883 or ERROR_MARK_NODE if the parse fails. */
6885 static void
6886 cp_parser_pseudo_destructor_name (cp_parser* parser,
6887 tree object,
6888 tree* scope,
6889 tree* type)
6891 bool nested_name_specifier_p;
6893 /* Handle ~auto. */
6894 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6895 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6896 && !type_dependent_expression_p (object))
6898 if (cxx_dialect < cxx14)
6899 pedwarn (input_location, 0,
6900 "%<~auto%> only available with "
6901 "-std=c++14 or -std=gnu++14");
6902 cp_lexer_consume_token (parser->lexer);
6903 cp_lexer_consume_token (parser->lexer);
6904 *scope = NULL_TREE;
6905 *type = TREE_TYPE (object);
6906 return;
6909 /* Assume that things will not work out. */
6910 *type = error_mark_node;
6912 /* Look for the optional `::' operator. */
6913 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6914 /* Look for the optional nested-name-specifier. */
6915 nested_name_specifier_p
6916 = (cp_parser_nested_name_specifier_opt (parser,
6917 /*typename_keyword_p=*/false,
6918 /*check_dependency_p=*/true,
6919 /*type_p=*/false,
6920 /*is_declaration=*/false)
6921 != NULL_TREE);
6922 /* Now, if we saw a nested-name-specifier, we might be doing the
6923 second production. */
6924 if (nested_name_specifier_p
6925 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6927 /* Consume the `template' keyword. */
6928 cp_lexer_consume_token (parser->lexer);
6929 /* Parse the template-id. */
6930 cp_parser_template_id (parser,
6931 /*template_keyword_p=*/true,
6932 /*check_dependency_p=*/false,
6933 class_type,
6934 /*is_declaration=*/true);
6935 /* Look for the `::' token. */
6936 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6938 /* If the next token is not a `~', then there might be some
6939 additional qualification. */
6940 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6942 /* At this point, we're looking for "type-name :: ~". The type-name
6943 must not be a class-name, since this is a pseudo-destructor. So,
6944 it must be either an enum-name, or a typedef-name -- both of which
6945 are just identifiers. So, we peek ahead to check that the "::"
6946 and "~" tokens are present; if they are not, then we can avoid
6947 calling type_name. */
6948 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6949 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6950 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6952 cp_parser_error (parser, "non-scalar type");
6953 return;
6956 /* Look for the type-name. */
6957 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6958 if (*scope == error_mark_node)
6959 return;
6961 /* Look for the `::' token. */
6962 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6964 else
6965 *scope = NULL_TREE;
6967 /* Look for the `~'. */
6968 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6970 /* Once we see the ~, this has to be a pseudo-destructor. */
6971 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6972 cp_parser_commit_to_topmost_tentative_parse (parser);
6974 /* Look for the type-name again. We are not responsible for
6975 checking that it matches the first type-name. */
6976 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6979 /* Parse a unary-expression.
6981 unary-expression:
6982 postfix-expression
6983 ++ cast-expression
6984 -- cast-expression
6985 unary-operator cast-expression
6986 sizeof unary-expression
6987 sizeof ( type-id )
6988 alignof ( type-id ) [C++0x]
6989 new-expression
6990 delete-expression
6992 GNU Extensions:
6994 unary-expression:
6995 __extension__ cast-expression
6996 __alignof__ unary-expression
6997 __alignof__ ( type-id )
6998 alignof unary-expression [C++0x]
6999 __real__ cast-expression
7000 __imag__ cast-expression
7001 && identifier
7002 sizeof ( type-id ) { initializer-list , [opt] }
7003 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7004 __alignof__ ( type-id ) { initializer-list , [opt] }
7006 ADDRESS_P is true iff the unary-expression is appearing as the
7007 operand of the `&' operator. CAST_P is true if this expression is
7008 the target of a cast.
7010 Returns a representation of the expression. */
7012 static tree
7013 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7014 bool decltype_p, cp_id_kind * pidk)
7016 cp_token *token;
7017 enum tree_code unary_operator;
7019 /* Peek at the next token. */
7020 token = cp_lexer_peek_token (parser->lexer);
7021 /* Some keywords give away the kind of expression. */
7022 if (token->type == CPP_KEYWORD)
7024 enum rid keyword = token->keyword;
7026 switch (keyword)
7028 case RID_ALIGNOF:
7029 case RID_SIZEOF:
7031 tree operand, ret;
7032 enum tree_code op;
7033 location_t first_loc;
7035 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7036 /* Consume the token. */
7037 cp_lexer_consume_token (parser->lexer);
7038 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7039 /* Parse the operand. */
7040 operand = cp_parser_sizeof_operand (parser, keyword);
7042 if (TYPE_P (operand))
7043 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7044 else
7046 /* ISO C++ defines alignof only with types, not with
7047 expressions. So pedwarn if alignof is used with a non-
7048 type expression. However, __alignof__ is ok. */
7049 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7050 pedwarn (token->location, OPT_Wpedantic,
7051 "ISO C++ does not allow %<alignof%> "
7052 "with a non-type");
7054 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7056 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7057 SIZEOF_EXPR with the original operand. */
7058 if (op == SIZEOF_EXPR && ret != error_mark_node)
7060 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7062 if (!processing_template_decl && TYPE_P (operand))
7064 ret = build_min (SIZEOF_EXPR, size_type_node,
7065 build1 (NOP_EXPR, operand,
7066 error_mark_node));
7067 SIZEOF_EXPR_TYPE_P (ret) = 1;
7069 else
7070 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7071 TREE_SIDE_EFFECTS (ret) = 0;
7072 TREE_READONLY (ret) = 1;
7074 SET_EXPR_LOCATION (ret, first_loc);
7076 return ret;
7079 case RID_NEW:
7080 return cp_parser_new_expression (parser);
7082 case RID_DELETE:
7083 return cp_parser_delete_expression (parser);
7085 case RID_EXTENSION:
7087 /* The saved value of the PEDANTIC flag. */
7088 int saved_pedantic;
7089 tree expr;
7091 /* Save away the PEDANTIC flag. */
7092 cp_parser_extension_opt (parser, &saved_pedantic);
7093 /* Parse the cast-expression. */
7094 expr = cp_parser_simple_cast_expression (parser);
7095 /* Restore the PEDANTIC flag. */
7096 pedantic = saved_pedantic;
7098 return expr;
7101 case RID_REALPART:
7102 case RID_IMAGPART:
7104 tree expression;
7106 /* Consume the `__real__' or `__imag__' token. */
7107 cp_lexer_consume_token (parser->lexer);
7108 /* Parse the cast-expression. */
7109 expression = cp_parser_simple_cast_expression (parser);
7110 /* Create the complete representation. */
7111 return build_x_unary_op (token->location,
7112 (keyword == RID_REALPART
7113 ? REALPART_EXPR : IMAGPART_EXPR),
7114 expression,
7115 tf_warning_or_error);
7117 break;
7119 case RID_TRANSACTION_ATOMIC:
7120 case RID_TRANSACTION_RELAXED:
7121 return cp_parser_transaction_expression (parser, keyword);
7123 case RID_NOEXCEPT:
7125 tree expr;
7126 const char *saved_message;
7127 bool saved_integral_constant_expression_p;
7128 bool saved_non_integral_constant_expression_p;
7129 bool saved_greater_than_is_operator_p;
7131 cp_lexer_consume_token (parser->lexer);
7132 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7134 saved_message = parser->type_definition_forbidden_message;
7135 parser->type_definition_forbidden_message
7136 = G_("types may not be defined in %<noexcept%> expressions");
7138 saved_integral_constant_expression_p
7139 = parser->integral_constant_expression_p;
7140 saved_non_integral_constant_expression_p
7141 = parser->non_integral_constant_expression_p;
7142 parser->integral_constant_expression_p = false;
7144 saved_greater_than_is_operator_p
7145 = parser->greater_than_is_operator_p;
7146 parser->greater_than_is_operator_p = true;
7148 ++cp_unevaluated_operand;
7149 ++c_inhibit_evaluation_warnings;
7150 expr = cp_parser_expression (parser);
7151 --c_inhibit_evaluation_warnings;
7152 --cp_unevaluated_operand;
7154 parser->greater_than_is_operator_p
7155 = saved_greater_than_is_operator_p;
7157 parser->integral_constant_expression_p
7158 = saved_integral_constant_expression_p;
7159 parser->non_integral_constant_expression_p
7160 = saved_non_integral_constant_expression_p;
7162 parser->type_definition_forbidden_message = saved_message;
7164 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7165 return finish_noexcept_expr (expr, tf_warning_or_error);
7168 default:
7169 break;
7173 /* Look for the `:: new' and `:: delete', which also signal the
7174 beginning of a new-expression, or delete-expression,
7175 respectively. If the next token is `::', then it might be one of
7176 these. */
7177 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7179 enum rid keyword;
7181 /* See if the token after the `::' is one of the keywords in
7182 which we're interested. */
7183 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7184 /* If it's `new', we have a new-expression. */
7185 if (keyword == RID_NEW)
7186 return cp_parser_new_expression (parser);
7187 /* Similarly, for `delete'. */
7188 else if (keyword == RID_DELETE)
7189 return cp_parser_delete_expression (parser);
7192 /* Look for a unary operator. */
7193 unary_operator = cp_parser_unary_operator (token);
7194 /* The `++' and `--' operators can be handled similarly, even though
7195 they are not technically unary-operators in the grammar. */
7196 if (unary_operator == ERROR_MARK)
7198 if (token->type == CPP_PLUS_PLUS)
7199 unary_operator = PREINCREMENT_EXPR;
7200 else if (token->type == CPP_MINUS_MINUS)
7201 unary_operator = PREDECREMENT_EXPR;
7202 /* Handle the GNU address-of-label extension. */
7203 else if (cp_parser_allow_gnu_extensions_p (parser)
7204 && token->type == CPP_AND_AND)
7206 tree identifier;
7207 tree expression;
7208 location_t loc = token->location;
7210 /* Consume the '&&' token. */
7211 cp_lexer_consume_token (parser->lexer);
7212 /* Look for the identifier. */
7213 identifier = cp_parser_identifier (parser);
7214 /* Create an expression representing the address. */
7215 expression = finish_label_address_expr (identifier, loc);
7216 if (cp_parser_non_integral_constant_expression (parser,
7217 NIC_ADDR_LABEL))
7218 expression = error_mark_node;
7219 return expression;
7222 if (unary_operator != ERROR_MARK)
7224 tree cast_expression;
7225 tree expression = error_mark_node;
7226 non_integral_constant non_constant_p = NIC_NONE;
7227 location_t loc = token->location;
7228 tsubst_flags_t complain = complain_flags (decltype_p);
7230 /* Consume the operator token. */
7231 token = cp_lexer_consume_token (parser->lexer);
7232 /* Parse the cast-expression. */
7233 cast_expression
7234 = cp_parser_cast_expression (parser,
7235 unary_operator == ADDR_EXPR,
7236 /*cast_p=*/false,
7237 /*decltype*/false,
7238 pidk);
7239 /* Now, build an appropriate representation. */
7240 switch (unary_operator)
7242 case INDIRECT_REF:
7243 non_constant_p = NIC_STAR;
7244 expression = build_x_indirect_ref (loc, cast_expression,
7245 RO_UNARY_STAR,
7246 complain);
7247 break;
7249 case ADDR_EXPR:
7250 non_constant_p = NIC_ADDR;
7251 /* Fall through. */
7252 case BIT_NOT_EXPR:
7253 expression = build_x_unary_op (loc, unary_operator,
7254 cast_expression,
7255 complain);
7256 break;
7258 case PREINCREMENT_EXPR:
7259 case PREDECREMENT_EXPR:
7260 non_constant_p = unary_operator == PREINCREMENT_EXPR
7261 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7262 /* Fall through. */
7263 case UNARY_PLUS_EXPR:
7264 case NEGATE_EXPR:
7265 case TRUTH_NOT_EXPR:
7266 expression = finish_unary_op_expr (loc, unary_operator,
7267 cast_expression, complain);
7268 break;
7270 default:
7271 gcc_unreachable ();
7274 if (non_constant_p != NIC_NONE
7275 && cp_parser_non_integral_constant_expression (parser,
7276 non_constant_p))
7277 expression = error_mark_node;
7279 return expression;
7282 return cp_parser_postfix_expression (parser, address_p, cast_p,
7283 /*member_access_only_p=*/false,
7284 decltype_p,
7285 pidk);
7288 static inline tree
7289 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7290 cp_id_kind * pidk)
7292 return cp_parser_unary_expression (parser, address_p, cast_p,
7293 /*decltype*/false, pidk);
7296 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7297 unary-operator, the corresponding tree code is returned. */
7299 static enum tree_code
7300 cp_parser_unary_operator (cp_token* token)
7302 switch (token->type)
7304 case CPP_MULT:
7305 return INDIRECT_REF;
7307 case CPP_AND:
7308 return ADDR_EXPR;
7310 case CPP_PLUS:
7311 return UNARY_PLUS_EXPR;
7313 case CPP_MINUS:
7314 return NEGATE_EXPR;
7316 case CPP_NOT:
7317 return TRUTH_NOT_EXPR;
7319 case CPP_COMPL:
7320 return BIT_NOT_EXPR;
7322 default:
7323 return ERROR_MARK;
7327 /* Parse a new-expression.
7329 new-expression:
7330 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7331 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7333 Returns a representation of the expression. */
7335 static tree
7336 cp_parser_new_expression (cp_parser* parser)
7338 bool global_scope_p;
7339 vec<tree, va_gc> *placement;
7340 tree type;
7341 vec<tree, va_gc> *initializer;
7342 tree nelts = NULL_TREE;
7343 tree ret;
7345 /* Look for the optional `::' operator. */
7346 global_scope_p
7347 = (cp_parser_global_scope_opt (parser,
7348 /*current_scope_valid_p=*/false)
7349 != NULL_TREE);
7350 /* Look for the `new' operator. */
7351 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7352 /* There's no easy way to tell a new-placement from the
7353 `( type-id )' construct. */
7354 cp_parser_parse_tentatively (parser);
7355 /* Look for a new-placement. */
7356 placement = cp_parser_new_placement (parser);
7357 /* If that didn't work out, there's no new-placement. */
7358 if (!cp_parser_parse_definitely (parser))
7360 if (placement != NULL)
7361 release_tree_vector (placement);
7362 placement = NULL;
7365 /* If the next token is a `(', then we have a parenthesized
7366 type-id. */
7367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7369 cp_token *token;
7370 const char *saved_message = parser->type_definition_forbidden_message;
7372 /* Consume the `('. */
7373 cp_lexer_consume_token (parser->lexer);
7375 /* Parse the type-id. */
7376 parser->type_definition_forbidden_message
7377 = G_("types may not be defined in a new-expression");
7378 type = cp_parser_type_id (parser);
7379 parser->type_definition_forbidden_message = saved_message;
7381 /* Look for the closing `)'. */
7382 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7383 token = cp_lexer_peek_token (parser->lexer);
7384 /* There should not be a direct-new-declarator in this production,
7385 but GCC used to allowed this, so we check and emit a sensible error
7386 message for this case. */
7387 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7389 error_at (token->location,
7390 "array bound forbidden after parenthesized type-id");
7391 inform (token->location,
7392 "try removing the parentheses around the type-id");
7393 cp_parser_direct_new_declarator (parser);
7396 /* Otherwise, there must be a new-type-id. */
7397 else
7398 type = cp_parser_new_type_id (parser, &nelts);
7400 /* If the next token is a `(' or '{', then we have a new-initializer. */
7401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7402 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7403 initializer = cp_parser_new_initializer (parser);
7404 else
7405 initializer = NULL;
7407 /* A new-expression may not appear in an integral constant
7408 expression. */
7409 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7410 ret = error_mark_node;
7411 else
7413 /* Create a representation of the new-expression. */
7414 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7415 tf_warning_or_error);
7418 if (placement != NULL)
7419 release_tree_vector (placement);
7420 if (initializer != NULL)
7421 release_tree_vector (initializer);
7423 return ret;
7426 /* Parse a new-placement.
7428 new-placement:
7429 ( expression-list )
7431 Returns the same representation as for an expression-list. */
7433 static vec<tree, va_gc> *
7434 cp_parser_new_placement (cp_parser* parser)
7436 vec<tree, va_gc> *expression_list;
7438 /* Parse the expression-list. */
7439 expression_list = (cp_parser_parenthesized_expression_list
7440 (parser, non_attr, /*cast_p=*/false,
7441 /*allow_expansion_p=*/true,
7442 /*non_constant_p=*/NULL));
7444 return expression_list;
7447 /* Parse a new-type-id.
7449 new-type-id:
7450 type-specifier-seq new-declarator [opt]
7452 Returns the TYPE allocated. If the new-type-id indicates an array
7453 type, *NELTS is set to the number of elements in the last array
7454 bound; the TYPE will not include the last array bound. */
7456 static tree
7457 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7459 cp_decl_specifier_seq type_specifier_seq;
7460 cp_declarator *new_declarator;
7461 cp_declarator *declarator;
7462 cp_declarator *outer_declarator;
7463 const char *saved_message;
7465 /* The type-specifier sequence must not contain type definitions.
7466 (It cannot contain declarations of new types either, but if they
7467 are not definitions we will catch that because they are not
7468 complete.) */
7469 saved_message = parser->type_definition_forbidden_message;
7470 parser->type_definition_forbidden_message
7471 = G_("types may not be defined in a new-type-id");
7472 /* Parse the type-specifier-seq. */
7473 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7474 /*is_trailing_return=*/false,
7475 &type_specifier_seq);
7476 /* Restore the old message. */
7477 parser->type_definition_forbidden_message = saved_message;
7479 if (type_specifier_seq.type == error_mark_node)
7480 return error_mark_node;
7482 /* Parse the new-declarator. */
7483 new_declarator = cp_parser_new_declarator_opt (parser);
7485 /* Determine the number of elements in the last array dimension, if
7486 any. */
7487 *nelts = NULL_TREE;
7488 /* Skip down to the last array dimension. */
7489 declarator = new_declarator;
7490 outer_declarator = NULL;
7491 while (declarator && (declarator->kind == cdk_pointer
7492 || declarator->kind == cdk_ptrmem))
7494 outer_declarator = declarator;
7495 declarator = declarator->declarator;
7497 while (declarator
7498 && declarator->kind == cdk_array
7499 && declarator->declarator
7500 && declarator->declarator->kind == cdk_array)
7502 outer_declarator = declarator;
7503 declarator = declarator->declarator;
7506 if (declarator && declarator->kind == cdk_array)
7508 *nelts = declarator->u.array.bounds;
7509 if (*nelts == error_mark_node)
7510 *nelts = integer_one_node;
7512 if (outer_declarator)
7513 outer_declarator->declarator = declarator->declarator;
7514 else
7515 new_declarator = NULL;
7518 return groktypename (&type_specifier_seq, new_declarator, false);
7521 /* Parse an (optional) new-declarator.
7523 new-declarator:
7524 ptr-operator new-declarator [opt]
7525 direct-new-declarator
7527 Returns the declarator. */
7529 static cp_declarator *
7530 cp_parser_new_declarator_opt (cp_parser* parser)
7532 enum tree_code code;
7533 tree type, std_attributes = NULL_TREE;
7534 cp_cv_quals cv_quals;
7536 /* We don't know if there's a ptr-operator next, or not. */
7537 cp_parser_parse_tentatively (parser);
7538 /* Look for a ptr-operator. */
7539 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7540 /* If that worked, look for more new-declarators. */
7541 if (cp_parser_parse_definitely (parser))
7543 cp_declarator *declarator;
7545 /* Parse another optional declarator. */
7546 declarator = cp_parser_new_declarator_opt (parser);
7548 declarator = cp_parser_make_indirect_declarator
7549 (code, type, cv_quals, declarator, std_attributes);
7551 return declarator;
7554 /* If the next token is a `[', there is a direct-new-declarator. */
7555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7556 return cp_parser_direct_new_declarator (parser);
7558 return NULL;
7561 /* Parse a direct-new-declarator.
7563 direct-new-declarator:
7564 [ expression ]
7565 direct-new-declarator [constant-expression]
7569 static cp_declarator *
7570 cp_parser_direct_new_declarator (cp_parser* parser)
7572 cp_declarator *declarator = NULL;
7574 while (true)
7576 tree expression;
7577 cp_token *token;
7579 /* Look for the opening `['. */
7580 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7582 token = cp_lexer_peek_token (parser->lexer);
7583 expression = cp_parser_expression (parser);
7584 /* The standard requires that the expression have integral
7585 type. DR 74 adds enumeration types. We believe that the
7586 real intent is that these expressions be handled like the
7587 expression in a `switch' condition, which also allows
7588 classes with a single conversion to integral or
7589 enumeration type. */
7590 if (!processing_template_decl)
7592 expression
7593 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7594 expression,
7595 /*complain=*/true);
7596 if (!expression)
7598 error_at (token->location,
7599 "expression in new-declarator must have integral "
7600 "or enumeration type");
7601 expression = error_mark_node;
7605 /* Look for the closing `]'. */
7606 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7608 /* Add this bound to the declarator. */
7609 declarator = make_array_declarator (declarator, expression);
7611 /* If the next token is not a `[', then there are no more
7612 bounds. */
7613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7614 break;
7617 return declarator;
7620 /* Parse a new-initializer.
7622 new-initializer:
7623 ( expression-list [opt] )
7624 braced-init-list
7626 Returns a representation of the expression-list. */
7628 static vec<tree, va_gc> *
7629 cp_parser_new_initializer (cp_parser* parser)
7631 vec<tree, va_gc> *expression_list;
7633 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7635 tree t;
7636 bool expr_non_constant_p;
7637 cp_lexer_set_source_position (parser->lexer);
7638 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7639 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7640 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7641 expression_list = make_tree_vector_single (t);
7643 else
7644 expression_list = (cp_parser_parenthesized_expression_list
7645 (parser, non_attr, /*cast_p=*/false,
7646 /*allow_expansion_p=*/true,
7647 /*non_constant_p=*/NULL));
7649 return expression_list;
7652 /* Parse a delete-expression.
7654 delete-expression:
7655 :: [opt] delete cast-expression
7656 :: [opt] delete [ ] cast-expression
7658 Returns a representation of the expression. */
7660 static tree
7661 cp_parser_delete_expression (cp_parser* parser)
7663 bool global_scope_p;
7664 bool array_p;
7665 tree expression;
7667 /* Look for the optional `::' operator. */
7668 global_scope_p
7669 = (cp_parser_global_scope_opt (parser,
7670 /*current_scope_valid_p=*/false)
7671 != NULL_TREE);
7672 /* Look for the `delete' keyword. */
7673 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7674 /* See if the array syntax is in use. */
7675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7677 /* Consume the `[' token. */
7678 cp_lexer_consume_token (parser->lexer);
7679 /* Look for the `]' token. */
7680 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7681 /* Remember that this is the `[]' construct. */
7682 array_p = true;
7684 else
7685 array_p = false;
7687 /* Parse the cast-expression. */
7688 expression = cp_parser_simple_cast_expression (parser);
7690 /* A delete-expression may not appear in an integral constant
7691 expression. */
7692 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7693 return error_mark_node;
7695 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7696 tf_warning_or_error);
7699 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7700 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7701 0 otherwise. */
7703 static int
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 case CPP_ELLIPSIS:
7747 return 0;
7749 case CPP_OPEN_PAREN:
7750 /* In ((type ()) () the last () isn't a valid cast-expression,
7751 so the whole must be parsed as postfix-expression. */
7752 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7753 != CPP_CLOSE_PAREN;
7755 case CPP_OPEN_SQUARE:
7756 /* '[' may start a primary-expression in obj-c++ and in C++11,
7757 as a lambda-expression, eg, '(void)[]{}'. */
7758 if (cxx_dialect >= cxx11)
7759 return -1;
7760 return c_dialect_objc ();
7762 case CPP_PLUS_PLUS:
7763 case CPP_MINUS_MINUS:
7764 /* '++' and '--' may or may not start a cast-expression:
7766 struct T { void operator++(int); };
7767 void f() { (T())++; }
7771 int a;
7772 (int)++a; */
7773 return -1;
7775 default:
7776 return 1;
7780 /* Parse a cast-expression.
7782 cast-expression:
7783 unary-expression
7784 ( type-id ) cast-expression
7786 ADDRESS_P is true iff the unary-expression is appearing as the
7787 operand of the `&' operator. CAST_P is true if this expression is
7788 the target of a cast.
7790 Returns a representation of the expression. */
7792 static tree
7793 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7794 bool decltype_p, cp_id_kind * pidk)
7796 /* If it's a `(', then we might be looking at a cast. */
7797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7799 tree type = NULL_TREE;
7800 tree expr = NULL_TREE;
7801 int cast_expression = 0;
7802 const char *saved_message;
7804 /* There's no way to know yet whether or not this is a cast.
7805 For example, `(int (3))' is a unary-expression, while `(int)
7806 3' is a cast. So, we resort to parsing tentatively. */
7807 cp_parser_parse_tentatively (parser);
7808 /* Types may not be defined in a cast. */
7809 saved_message = parser->type_definition_forbidden_message;
7810 parser->type_definition_forbidden_message
7811 = G_("types may not be defined in casts");
7812 /* Consume the `('. */
7813 cp_lexer_consume_token (parser->lexer);
7814 /* A very tricky bit is that `(struct S) { 3 }' is a
7815 compound-literal (which we permit in C++ as an extension).
7816 But, that construct is not a cast-expression -- it is a
7817 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7818 is legal; if the compound-literal were a cast-expression,
7819 you'd need an extra set of parentheses.) But, if we parse
7820 the type-id, and it happens to be a class-specifier, then we
7821 will commit to the parse at that point, because we cannot
7822 undo the action that is done when creating a new class. So,
7823 then we cannot back up and do a postfix-expression.
7825 Another tricky case is the following (c++/29234):
7827 struct S { void operator () (); };
7829 void foo ()
7831 ( S()() );
7834 As a type-id we parse the parenthesized S()() as a function
7835 returning a function, groktypename complains and we cannot
7836 back up in this case either.
7838 Therefore, we scan ahead to the closing `)', and check to see
7839 if the tokens after the `)' can start a cast-expression. Otherwise
7840 we are dealing with an unary-expression, a postfix-expression
7841 or something else.
7843 Yet another tricky case, in C++11, is the following (c++/54891):
7845 (void)[]{};
7847 The issue is that usually, besides the case of lambda-expressions,
7848 the parenthesized type-id cannot be followed by '[', and, eg, we
7849 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7850 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7851 we don't commit, we try a cast-expression, then an unary-expression.
7853 Save tokens so that we can put them back. */
7854 cp_lexer_save_tokens (parser->lexer);
7856 /* We may be looking at a cast-expression. */
7857 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7858 /*consume_paren=*/true))
7859 cast_expression
7860 = cp_parser_tokens_start_cast_expression (parser);
7862 /* Roll back the tokens we skipped. */
7863 cp_lexer_rollback_tokens (parser->lexer);
7864 /* If we aren't looking at a cast-expression, simulate an error so
7865 that the call to cp_parser_error_occurred below returns true. */
7866 if (!cast_expression)
7867 cp_parser_simulate_error (parser);
7868 else
7870 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7871 parser->in_type_id_in_expr_p = true;
7872 /* Look for the type-id. */
7873 type = cp_parser_type_id (parser);
7874 /* Look for the closing `)'. */
7875 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7876 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7879 /* Restore the saved message. */
7880 parser->type_definition_forbidden_message = saved_message;
7882 /* At this point this can only be either a cast or a
7883 parenthesized ctor such as `(T ())' that looks like a cast to
7884 function returning T. */
7885 if (!cp_parser_error_occurred (parser))
7887 /* Only commit if the cast-expression doesn't start with
7888 '++', '--', or '[' in C++11. */
7889 if (cast_expression > 0)
7890 cp_parser_commit_to_topmost_tentative_parse (parser);
7892 expr = cp_parser_cast_expression (parser,
7893 /*address_p=*/false,
7894 /*cast_p=*/true,
7895 /*decltype_p=*/false,
7896 pidk);
7898 if (cp_parser_parse_definitely (parser))
7900 /* Warn about old-style casts, if so requested. */
7901 if (warn_old_style_cast
7902 && !in_system_header_at (input_location)
7903 && !VOID_TYPE_P (type)
7904 && current_lang_name != lang_name_c)
7905 warning (OPT_Wold_style_cast, "use of old-style cast");
7907 /* Only type conversions to integral or enumeration types
7908 can be used in constant-expressions. */
7909 if (!cast_valid_in_integral_constant_expression_p (type)
7910 && cp_parser_non_integral_constant_expression (parser,
7911 NIC_CAST))
7912 return error_mark_node;
7914 /* Perform the cast. */
7915 expr = build_c_cast (input_location, type, expr);
7916 return expr;
7919 else
7920 cp_parser_abort_tentative_parse (parser);
7923 /* If we get here, then it's not a cast, so it must be a
7924 unary-expression. */
7925 return cp_parser_unary_expression (parser, address_p, cast_p,
7926 decltype_p, pidk);
7929 /* Parse a binary expression of the general form:
7931 pm-expression:
7932 cast-expression
7933 pm-expression .* cast-expression
7934 pm-expression ->* cast-expression
7936 multiplicative-expression:
7937 pm-expression
7938 multiplicative-expression * pm-expression
7939 multiplicative-expression / pm-expression
7940 multiplicative-expression % pm-expression
7942 additive-expression:
7943 multiplicative-expression
7944 additive-expression + multiplicative-expression
7945 additive-expression - multiplicative-expression
7947 shift-expression:
7948 additive-expression
7949 shift-expression << additive-expression
7950 shift-expression >> additive-expression
7952 relational-expression:
7953 shift-expression
7954 relational-expression < shift-expression
7955 relational-expression > shift-expression
7956 relational-expression <= shift-expression
7957 relational-expression >= shift-expression
7959 GNU Extension:
7961 relational-expression:
7962 relational-expression <? shift-expression
7963 relational-expression >? shift-expression
7965 equality-expression:
7966 relational-expression
7967 equality-expression == relational-expression
7968 equality-expression != relational-expression
7970 and-expression:
7971 equality-expression
7972 and-expression & equality-expression
7974 exclusive-or-expression:
7975 and-expression
7976 exclusive-or-expression ^ and-expression
7978 inclusive-or-expression:
7979 exclusive-or-expression
7980 inclusive-or-expression | exclusive-or-expression
7982 logical-and-expression:
7983 inclusive-or-expression
7984 logical-and-expression && inclusive-or-expression
7986 logical-or-expression:
7987 logical-and-expression
7988 logical-or-expression || logical-and-expression
7990 All these are implemented with a single function like:
7992 binary-expression:
7993 simple-cast-expression
7994 binary-expression <token> binary-expression
7996 CAST_P is true if this expression is the target of a cast.
7998 The binops_by_token map is used to get the tree codes for each <token> type.
7999 binary-expressions are associated according to a precedence table. */
8001 #define TOKEN_PRECEDENCE(token) \
8002 (((token->type == CPP_GREATER \
8003 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8004 && !parser->greater_than_is_operator_p) \
8005 ? PREC_NOT_OPERATOR \
8006 : binops_by_token[token->type].prec)
8008 static tree
8009 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8010 bool no_toplevel_fold_p,
8011 bool decltype_p,
8012 enum cp_parser_prec prec,
8013 cp_id_kind * pidk)
8015 cp_parser_expression_stack stack;
8016 cp_parser_expression_stack_entry *sp = &stack[0];
8017 cp_parser_expression_stack_entry current;
8018 tree rhs;
8019 cp_token *token;
8020 enum tree_code rhs_type;
8021 enum cp_parser_prec new_prec, lookahead_prec;
8022 tree overload;
8024 /* Parse the first expression. */
8025 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8026 ? TRUTH_NOT_EXPR : ERROR_MARK);
8027 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8028 cast_p, decltype_p, pidk);
8029 current.prec = prec;
8031 if (cp_parser_error_occurred (parser))
8032 return error_mark_node;
8034 for (;;)
8036 /* Get an operator token. */
8037 token = cp_lexer_peek_token (parser->lexer);
8039 if (warn_cxx0x_compat
8040 && token->type == CPP_RSHIFT
8041 && !parser->greater_than_is_operator_p)
8043 if (warning_at (token->location, OPT_Wc__0x_compat,
8044 "%<>>%> operator is treated"
8045 " as two right angle brackets in C++11"))
8046 inform (token->location,
8047 "suggest parentheses around %<>>%> expression");
8050 new_prec = TOKEN_PRECEDENCE (token);
8052 /* Popping an entry off the stack means we completed a subexpression:
8053 - either we found a token which is not an operator (`>' where it is not
8054 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8055 will happen repeatedly;
8056 - or, we found an operator which has lower priority. This is the case
8057 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8058 parsing `3 * 4'. */
8059 if (new_prec <= current.prec)
8061 if (sp == stack)
8062 break;
8063 else
8064 goto pop;
8067 get_rhs:
8068 current.tree_type = binops_by_token[token->type].tree_type;
8069 current.loc = token->location;
8071 /* We used the operator token. */
8072 cp_lexer_consume_token (parser->lexer);
8074 /* For "false && x" or "true || x", x will never be executed;
8075 disable warnings while evaluating it. */
8076 if (current.tree_type == TRUTH_ANDIF_EXPR)
8077 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8078 else if (current.tree_type == TRUTH_ORIF_EXPR)
8079 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8081 /* Extract another operand. It may be the RHS of this expression
8082 or the LHS of a new, higher priority expression. */
8083 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8084 ? TRUTH_NOT_EXPR : ERROR_MARK);
8085 rhs = cp_parser_simple_cast_expression (parser);
8087 /* Get another operator token. Look up its precedence to avoid
8088 building a useless (immediately popped) stack entry for common
8089 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8090 token = cp_lexer_peek_token (parser->lexer);
8091 lookahead_prec = TOKEN_PRECEDENCE (token);
8092 if (lookahead_prec > new_prec)
8094 /* ... and prepare to parse the RHS of the new, higher priority
8095 expression. Since precedence levels on the stack are
8096 monotonically increasing, we do not have to care about
8097 stack overflows. */
8098 *sp = current;
8099 ++sp;
8100 current.lhs = rhs;
8101 current.lhs_type = rhs_type;
8102 current.prec = new_prec;
8103 new_prec = lookahead_prec;
8104 goto get_rhs;
8106 pop:
8107 lookahead_prec = new_prec;
8108 /* If the stack is not empty, we have parsed into LHS the right side
8109 (`4' in the example above) of an expression we had suspended.
8110 We can use the information on the stack to recover the LHS (`3')
8111 from the stack together with the tree code (`MULT_EXPR'), and
8112 the precedence of the higher level subexpression
8113 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8114 which will be used to actually build the additive expression. */
8115 rhs = current.lhs;
8116 rhs_type = current.lhs_type;
8117 --sp;
8118 current = *sp;
8121 /* Undo the disabling of warnings done above. */
8122 if (current.tree_type == TRUTH_ANDIF_EXPR)
8123 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8124 else if (current.tree_type == TRUTH_ORIF_EXPR)
8125 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8127 if (warn_logical_not_paren
8128 && current.lhs_type == TRUTH_NOT_EXPR)
8129 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8131 overload = NULL;
8132 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8133 ERROR_MARK for everything that is not a binary expression.
8134 This makes warn_about_parentheses miss some warnings that
8135 involve unary operators. For unary expressions we should
8136 pass the correct tree_code unless the unary expression was
8137 surrounded by parentheses.
8139 if (no_toplevel_fold_p
8140 && lookahead_prec <= current.prec
8141 && sp == stack)
8142 current.lhs = build2 (current.tree_type,
8143 TREE_CODE_CLASS (current.tree_type)
8144 == tcc_comparison
8145 ? boolean_type_node : TREE_TYPE (current.lhs),
8146 current.lhs, rhs);
8147 else
8148 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8149 current.lhs, current.lhs_type,
8150 rhs, rhs_type, &overload,
8151 complain_flags (decltype_p));
8152 current.lhs_type = current.tree_type;
8153 if (EXPR_P (current.lhs))
8154 SET_EXPR_LOCATION (current.lhs, current.loc);
8156 /* If the binary operator required the use of an overloaded operator,
8157 then this expression cannot be an integral constant-expression.
8158 An overloaded operator can be used even if both operands are
8159 otherwise permissible in an integral constant-expression if at
8160 least one of the operands is of enumeration type. */
8162 if (overload
8163 && cp_parser_non_integral_constant_expression (parser,
8164 NIC_OVERLOADED))
8165 return error_mark_node;
8168 return current.lhs;
8171 static tree
8172 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8173 bool no_toplevel_fold_p,
8174 enum cp_parser_prec prec,
8175 cp_id_kind * pidk)
8177 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8178 /*decltype*/false, prec, pidk);
8181 /* Parse the `? expression : assignment-expression' part of a
8182 conditional-expression. The LOGICAL_OR_EXPR is the
8183 logical-or-expression that started the conditional-expression.
8184 Returns a representation of the entire conditional-expression.
8186 This routine is used by cp_parser_assignment_expression.
8188 ? expression : assignment-expression
8190 GNU Extensions:
8192 ? : assignment-expression */
8194 static tree
8195 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8197 tree expr;
8198 tree assignment_expr;
8199 struct cp_token *token;
8200 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8202 /* Consume the `?' token. */
8203 cp_lexer_consume_token (parser->lexer);
8204 token = cp_lexer_peek_token (parser->lexer);
8205 if (cp_parser_allow_gnu_extensions_p (parser)
8206 && token->type == CPP_COLON)
8208 pedwarn (token->location, OPT_Wpedantic,
8209 "ISO C++ does not allow ?: with omitted middle operand");
8210 /* Implicit true clause. */
8211 expr = NULL_TREE;
8212 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8213 warn_for_omitted_condop (token->location, logical_or_expr);
8215 else
8217 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8218 parser->colon_corrects_to_scope_p = false;
8219 /* Parse the expression. */
8220 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8221 expr = cp_parser_expression (parser);
8222 c_inhibit_evaluation_warnings +=
8223 ((logical_or_expr == truthvalue_true_node)
8224 - (logical_or_expr == truthvalue_false_node));
8225 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8228 /* The next token should be a `:'. */
8229 cp_parser_require (parser, CPP_COLON, RT_COLON);
8230 /* Parse the assignment-expression. */
8231 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8232 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8234 /* Build the conditional-expression. */
8235 return build_x_conditional_expr (loc, logical_or_expr,
8236 expr,
8237 assignment_expr,
8238 tf_warning_or_error);
8241 /* Parse an assignment-expression.
8243 assignment-expression:
8244 conditional-expression
8245 logical-or-expression assignment-operator assignment_expression
8246 throw-expression
8248 CAST_P is true if this expression is the target of a cast.
8249 DECLTYPE_P is true if this expression is the operand of decltype.
8251 Returns a representation for the expression. */
8253 static tree
8254 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8255 bool decltype_p, cp_id_kind * pidk)
8257 tree expr;
8259 /* If the next token is the `throw' keyword, then we're looking at
8260 a throw-expression. */
8261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8262 expr = cp_parser_throw_expression (parser);
8263 /* Otherwise, it must be that we are looking at a
8264 logical-or-expression. */
8265 else
8267 /* Parse the binary expressions (logical-or-expression). */
8268 expr = cp_parser_binary_expression (parser, cast_p, false,
8269 decltype_p,
8270 PREC_NOT_OPERATOR, pidk);
8271 /* If the next token is a `?' then we're actually looking at a
8272 conditional-expression. */
8273 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8274 return cp_parser_question_colon_clause (parser, expr);
8275 else
8277 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8279 /* If it's an assignment-operator, we're using the second
8280 production. */
8281 enum tree_code assignment_operator
8282 = cp_parser_assignment_operator_opt (parser);
8283 if (assignment_operator != ERROR_MARK)
8285 bool non_constant_p;
8286 location_t saved_input_location;
8288 /* Parse the right-hand side of the assignment. */
8289 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8291 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8292 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8294 /* An assignment may not appear in a
8295 constant-expression. */
8296 if (cp_parser_non_integral_constant_expression (parser,
8297 NIC_ASSIGNMENT))
8298 return error_mark_node;
8299 /* Build the assignment expression. Its default
8300 location is the location of the '=' token. */
8301 saved_input_location = input_location;
8302 input_location = loc;
8303 expr = build_x_modify_expr (loc, expr,
8304 assignment_operator,
8305 rhs,
8306 complain_flags (decltype_p));
8307 input_location = saved_input_location;
8312 return expr;
8315 static tree
8316 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8317 cp_id_kind * pidk)
8319 return cp_parser_assignment_expression (parser, cast_p,
8320 /*decltype*/false, pidk);
8323 /* Parse an (optional) assignment-operator.
8325 assignment-operator: one of
8326 = *= /= %= += -= >>= <<= &= ^= |=
8328 GNU Extension:
8330 assignment-operator: one of
8331 <?= >?=
8333 If the next token is an assignment operator, the corresponding tree
8334 code is returned, and the token is consumed. For example, for
8335 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8336 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8337 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8338 operator, ERROR_MARK is returned. */
8340 static enum tree_code
8341 cp_parser_assignment_operator_opt (cp_parser* parser)
8343 enum tree_code op;
8344 cp_token *token;
8346 /* Peek at the next token. */
8347 token = cp_lexer_peek_token (parser->lexer);
8349 switch (token->type)
8351 case CPP_EQ:
8352 op = NOP_EXPR;
8353 break;
8355 case CPP_MULT_EQ:
8356 op = MULT_EXPR;
8357 break;
8359 case CPP_DIV_EQ:
8360 op = TRUNC_DIV_EXPR;
8361 break;
8363 case CPP_MOD_EQ:
8364 op = TRUNC_MOD_EXPR;
8365 break;
8367 case CPP_PLUS_EQ:
8368 op = PLUS_EXPR;
8369 break;
8371 case CPP_MINUS_EQ:
8372 op = MINUS_EXPR;
8373 break;
8375 case CPP_RSHIFT_EQ:
8376 op = RSHIFT_EXPR;
8377 break;
8379 case CPP_LSHIFT_EQ:
8380 op = LSHIFT_EXPR;
8381 break;
8383 case CPP_AND_EQ:
8384 op = BIT_AND_EXPR;
8385 break;
8387 case CPP_XOR_EQ:
8388 op = BIT_XOR_EXPR;
8389 break;
8391 case CPP_OR_EQ:
8392 op = BIT_IOR_EXPR;
8393 break;
8395 default:
8396 /* Nothing else is an assignment operator. */
8397 op = ERROR_MARK;
8400 /* If it was an assignment operator, consume it. */
8401 if (op != ERROR_MARK)
8402 cp_lexer_consume_token (parser->lexer);
8404 return op;
8407 /* Parse an expression.
8409 expression:
8410 assignment-expression
8411 expression , assignment-expression
8413 CAST_P is true if this expression is the target of a cast.
8414 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8415 except possibly parenthesized or on the RHS of a comma (N3276).
8417 Returns a representation of the expression. */
8419 static tree
8420 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8421 bool cast_p, bool decltype_p)
8423 tree expression = NULL_TREE;
8424 location_t loc = UNKNOWN_LOCATION;
8426 while (true)
8428 tree assignment_expression;
8430 /* Parse the next assignment-expression. */
8431 assignment_expression
8432 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8434 /* We don't create a temporary for a call that is the immediate operand
8435 of decltype or on the RHS of a comma. But when we see a comma, we
8436 need to create a temporary for a call on the LHS. */
8437 if (decltype_p && !processing_template_decl
8438 && TREE_CODE (assignment_expression) == CALL_EXPR
8439 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8440 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8441 assignment_expression
8442 = build_cplus_new (TREE_TYPE (assignment_expression),
8443 assignment_expression, tf_warning_or_error);
8445 /* If this is the first assignment-expression, we can just
8446 save it away. */
8447 if (!expression)
8448 expression = assignment_expression;
8449 else
8450 expression = build_x_compound_expr (loc, expression,
8451 assignment_expression,
8452 complain_flags (decltype_p));
8453 /* If the next token is not a comma, then we are done with the
8454 expression. */
8455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8456 break;
8457 /* Consume the `,'. */
8458 loc = cp_lexer_peek_token (parser->lexer)->location;
8459 cp_lexer_consume_token (parser->lexer);
8460 /* A comma operator cannot appear in a constant-expression. */
8461 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8462 expression = error_mark_node;
8465 return expression;
8468 /* Parse a constant-expression.
8470 constant-expression:
8471 conditional-expression
8473 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8474 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8475 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8476 is false, NON_CONSTANT_P should be NULL. */
8478 static tree
8479 cp_parser_constant_expression (cp_parser* parser,
8480 bool allow_non_constant_p,
8481 bool *non_constant_p)
8483 bool saved_integral_constant_expression_p;
8484 bool saved_allow_non_integral_constant_expression_p;
8485 bool saved_non_integral_constant_expression_p;
8486 tree expression;
8488 /* It might seem that we could simply parse the
8489 conditional-expression, and then check to see if it were
8490 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8491 one that the compiler can figure out is constant, possibly after
8492 doing some simplifications or optimizations. The standard has a
8493 precise definition of constant-expression, and we must honor
8494 that, even though it is somewhat more restrictive.
8496 For example:
8498 int i[(2, 3)];
8500 is not a legal declaration, because `(2, 3)' is not a
8501 constant-expression. The `,' operator is forbidden in a
8502 constant-expression. However, GCC's constant-folding machinery
8503 will fold this operation to an INTEGER_CST for `3'. */
8505 /* Save the old settings. */
8506 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8507 saved_allow_non_integral_constant_expression_p
8508 = parser->allow_non_integral_constant_expression_p;
8509 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8510 /* We are now parsing a constant-expression. */
8511 parser->integral_constant_expression_p = true;
8512 parser->allow_non_integral_constant_expression_p
8513 = (allow_non_constant_p || cxx_dialect >= cxx11);
8514 parser->non_integral_constant_expression_p = false;
8515 /* Although the grammar says "conditional-expression", we parse an
8516 "assignment-expression", which also permits "throw-expression"
8517 and the use of assignment operators. In the case that
8518 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8519 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8520 actually essential that we look for an assignment-expression.
8521 For example, cp_parser_initializer_clauses uses this function to
8522 determine whether a particular assignment-expression is in fact
8523 constant. */
8524 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8525 /* Restore the old settings. */
8526 parser->integral_constant_expression_p
8527 = saved_integral_constant_expression_p;
8528 parser->allow_non_integral_constant_expression_p
8529 = saved_allow_non_integral_constant_expression_p;
8530 if (cxx_dialect >= cxx11)
8532 /* Require an rvalue constant expression here; that's what our
8533 callers expect. Reference constant expressions are handled
8534 separately in e.g. cp_parser_template_argument. */
8535 bool is_const = potential_rvalue_constant_expression (expression);
8536 parser->non_integral_constant_expression_p = !is_const;
8537 if (!is_const && !allow_non_constant_p)
8538 require_potential_rvalue_constant_expression (expression);
8540 if (allow_non_constant_p)
8541 *non_constant_p = parser->non_integral_constant_expression_p;
8542 parser->non_integral_constant_expression_p
8543 = saved_non_integral_constant_expression_p;
8545 return expression;
8548 /* Parse __builtin_offsetof.
8550 offsetof-expression:
8551 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8553 offsetof-member-designator:
8554 id-expression
8555 | offsetof-member-designator "." id-expression
8556 | offsetof-member-designator "[" expression "]"
8557 | offsetof-member-designator "->" id-expression */
8559 static tree
8560 cp_parser_builtin_offsetof (cp_parser *parser)
8562 int save_ice_p, save_non_ice_p;
8563 tree type, expr;
8564 cp_id_kind dummy;
8565 cp_token *token;
8567 /* We're about to accept non-integral-constant things, but will
8568 definitely yield an integral constant expression. Save and
8569 restore these values around our local parsing. */
8570 save_ice_p = parser->integral_constant_expression_p;
8571 save_non_ice_p = parser->non_integral_constant_expression_p;
8573 /* Consume the "__builtin_offsetof" token. */
8574 cp_lexer_consume_token (parser->lexer);
8575 /* Consume the opening `('. */
8576 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8577 /* Parse the type-id. */
8578 type = cp_parser_type_id (parser);
8579 /* Look for the `,'. */
8580 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8581 token = cp_lexer_peek_token (parser->lexer);
8583 /* Build the (type *)null that begins the traditional offsetof macro. */
8584 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8585 tf_warning_or_error);
8587 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8588 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8589 true, &dummy, token->location);
8590 while (true)
8592 token = cp_lexer_peek_token (parser->lexer);
8593 switch (token->type)
8595 case CPP_OPEN_SQUARE:
8596 /* offsetof-member-designator "[" expression "]" */
8597 expr = cp_parser_postfix_open_square_expression (parser, expr,
8598 true, false);
8599 break;
8601 case CPP_DEREF:
8602 /* offsetof-member-designator "->" identifier */
8603 expr = grok_array_decl (token->location, expr,
8604 integer_zero_node, false);
8605 /* FALLTHRU */
8607 case CPP_DOT:
8608 /* offsetof-member-designator "." identifier */
8609 cp_lexer_consume_token (parser->lexer);
8610 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8611 expr, true, &dummy,
8612 token->location);
8613 break;
8615 case CPP_CLOSE_PAREN:
8616 /* Consume the ")" token. */
8617 cp_lexer_consume_token (parser->lexer);
8618 goto success;
8620 default:
8621 /* Error. We know the following require will fail, but
8622 that gives the proper error message. */
8623 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8624 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8625 expr = error_mark_node;
8626 goto failure;
8630 success:
8631 /* If we're processing a template, we can't finish the semantics yet.
8632 Otherwise we can fold the entire expression now. */
8633 if (processing_template_decl)
8634 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8635 else
8636 expr = finish_offsetof (expr);
8638 failure:
8639 parser->integral_constant_expression_p = save_ice_p;
8640 parser->non_integral_constant_expression_p = save_non_ice_p;
8642 return expr;
8645 /* Parse a trait expression.
8647 Returns a representation of the expression, the underlying type
8648 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8650 static tree
8651 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8653 cp_trait_kind kind;
8654 tree type1, type2 = NULL_TREE;
8655 bool binary = false;
8656 cp_decl_specifier_seq decl_specs;
8658 switch (keyword)
8660 case RID_HAS_NOTHROW_ASSIGN:
8661 kind = CPTK_HAS_NOTHROW_ASSIGN;
8662 break;
8663 case RID_HAS_NOTHROW_CONSTRUCTOR:
8664 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8665 break;
8666 case RID_HAS_NOTHROW_COPY:
8667 kind = CPTK_HAS_NOTHROW_COPY;
8668 break;
8669 case RID_HAS_TRIVIAL_ASSIGN:
8670 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8671 break;
8672 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8673 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8674 break;
8675 case RID_HAS_TRIVIAL_COPY:
8676 kind = CPTK_HAS_TRIVIAL_COPY;
8677 break;
8678 case RID_HAS_TRIVIAL_DESTRUCTOR:
8679 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8680 break;
8681 case RID_HAS_VIRTUAL_DESTRUCTOR:
8682 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8683 break;
8684 case RID_IS_ABSTRACT:
8685 kind = CPTK_IS_ABSTRACT;
8686 break;
8687 case RID_IS_BASE_OF:
8688 kind = CPTK_IS_BASE_OF;
8689 binary = true;
8690 break;
8691 case RID_IS_CLASS:
8692 kind = CPTK_IS_CLASS;
8693 break;
8694 case RID_IS_CONVERTIBLE_TO:
8695 kind = CPTK_IS_CONVERTIBLE_TO;
8696 binary = true;
8697 break;
8698 case RID_IS_EMPTY:
8699 kind = CPTK_IS_EMPTY;
8700 break;
8701 case RID_IS_ENUM:
8702 kind = CPTK_IS_ENUM;
8703 break;
8704 case RID_IS_FINAL:
8705 kind = CPTK_IS_FINAL;
8706 break;
8707 case RID_IS_LITERAL_TYPE:
8708 kind = CPTK_IS_LITERAL_TYPE;
8709 break;
8710 case RID_IS_POD:
8711 kind = CPTK_IS_POD;
8712 break;
8713 case RID_IS_POLYMORPHIC:
8714 kind = CPTK_IS_POLYMORPHIC;
8715 break;
8716 case RID_IS_STD_LAYOUT:
8717 kind = CPTK_IS_STD_LAYOUT;
8718 break;
8719 case RID_IS_TRIVIAL:
8720 kind = CPTK_IS_TRIVIAL;
8721 break;
8722 case RID_IS_UNION:
8723 kind = CPTK_IS_UNION;
8724 break;
8725 case RID_UNDERLYING_TYPE:
8726 kind = CPTK_UNDERLYING_TYPE;
8727 break;
8728 case RID_BASES:
8729 kind = CPTK_BASES;
8730 break;
8731 case RID_DIRECT_BASES:
8732 kind = CPTK_DIRECT_BASES;
8733 break;
8734 default:
8735 gcc_unreachable ();
8738 /* Consume the token. */
8739 cp_lexer_consume_token (parser->lexer);
8741 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8743 type1 = cp_parser_type_id (parser);
8745 if (type1 == error_mark_node)
8746 return error_mark_node;
8748 /* Build a trivial decl-specifier-seq. */
8749 clear_decl_specs (&decl_specs);
8750 decl_specs.type = type1;
8752 /* Call grokdeclarator to figure out what type this is. */
8753 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8754 /*initialized=*/0, /*attrlist=*/NULL);
8756 if (binary)
8758 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8760 type2 = cp_parser_type_id (parser);
8762 if (type2 == error_mark_node)
8763 return error_mark_node;
8765 /* Build a trivial decl-specifier-seq. */
8766 clear_decl_specs (&decl_specs);
8767 decl_specs.type = type2;
8769 /* Call grokdeclarator to figure out what type this is. */
8770 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8771 /*initialized=*/0, /*attrlist=*/NULL);
8774 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8776 /* Complete the trait expression, which may mean either processing
8777 the trait expr now or saving it for template instantiation. */
8778 switch(kind)
8780 case CPTK_UNDERLYING_TYPE:
8781 return finish_underlying_type (type1);
8782 case CPTK_BASES:
8783 return finish_bases (type1, false);
8784 case CPTK_DIRECT_BASES:
8785 return finish_bases (type1, true);
8786 default:
8787 return finish_trait_expr (kind, type1, type2);
8791 /* Lambdas that appear in variable initializer or default argument scope
8792 get that in their mangling, so we need to record it. We might as well
8793 use the count for function and namespace scopes as well. */
8794 static GTY(()) tree lambda_scope;
8795 static GTY(()) int lambda_count;
8796 typedef struct GTY(()) tree_int
8798 tree t;
8799 int i;
8800 } tree_int;
8801 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8803 static void
8804 start_lambda_scope (tree decl)
8806 tree_int ti;
8807 gcc_assert (decl);
8808 /* Once we're inside a function, we ignore other scopes and just push
8809 the function again so that popping works properly. */
8810 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8811 decl = current_function_decl;
8812 ti.t = lambda_scope;
8813 ti.i = lambda_count;
8814 vec_safe_push (lambda_scope_stack, ti);
8815 if (lambda_scope != decl)
8817 /* Don't reset the count if we're still in the same function. */
8818 lambda_scope = decl;
8819 lambda_count = 0;
8823 static void
8824 record_lambda_scope (tree lambda)
8826 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8827 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8830 static void
8831 finish_lambda_scope (void)
8833 tree_int *p = &lambda_scope_stack->last ();
8834 if (lambda_scope != p->t)
8836 lambda_scope = p->t;
8837 lambda_count = p->i;
8839 lambda_scope_stack->pop ();
8842 /* Parse a lambda expression.
8844 lambda-expression:
8845 lambda-introducer lambda-declarator [opt] compound-statement
8847 Returns a representation of the expression. */
8849 static tree
8850 cp_parser_lambda_expression (cp_parser* parser)
8852 tree lambda_expr = build_lambda_expr ();
8853 tree type;
8854 bool ok = true;
8855 cp_token *token = cp_lexer_peek_token (parser->lexer);
8857 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8859 if (cp_unevaluated_operand)
8861 if (!token->error_reported)
8863 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8864 "lambda-expression in unevaluated context");
8865 token->error_reported = true;
8867 ok = false;
8870 /* We may be in the middle of deferred access check. Disable
8871 it now. */
8872 push_deferring_access_checks (dk_no_deferred);
8874 cp_parser_lambda_introducer (parser, lambda_expr);
8876 type = begin_lambda_type (lambda_expr);
8877 if (type == error_mark_node)
8878 return error_mark_node;
8880 record_lambda_scope (lambda_expr);
8882 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8883 determine_visibility (TYPE_NAME (type));
8885 /* Now that we've started the type, add the capture fields for any
8886 explicit captures. */
8887 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8890 /* Inside the class, surrounding template-parameter-lists do not apply. */
8891 unsigned int saved_num_template_parameter_lists
8892 = parser->num_template_parameter_lists;
8893 unsigned char in_statement = parser->in_statement;
8894 bool in_switch_statement_p = parser->in_switch_statement_p;
8895 bool fully_implicit_function_template_p
8896 = parser->fully_implicit_function_template_p;
8897 tree implicit_template_parms = parser->implicit_template_parms;
8898 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8899 bool auto_is_implicit_function_template_parm_p
8900 = parser->auto_is_implicit_function_template_parm_p;
8902 parser->num_template_parameter_lists = 0;
8903 parser->in_statement = 0;
8904 parser->in_switch_statement_p = false;
8905 parser->fully_implicit_function_template_p = false;
8906 parser->implicit_template_parms = 0;
8907 parser->implicit_template_scope = 0;
8908 parser->auto_is_implicit_function_template_parm_p = false;
8910 /* By virtue of defining a local class, a lambda expression has access to
8911 the private variables of enclosing classes. */
8913 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8915 if (ok)
8916 cp_parser_lambda_body (parser, lambda_expr);
8917 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8919 if (cp_parser_skip_to_closing_brace (parser))
8920 cp_lexer_consume_token (parser->lexer);
8923 /* The capture list was built up in reverse order; fix that now. */
8924 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8925 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8927 if (ok)
8928 maybe_add_lambda_conv_op (type);
8930 type = finish_struct (type, /*attributes=*/NULL_TREE);
8932 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8933 parser->in_statement = in_statement;
8934 parser->in_switch_statement_p = in_switch_statement_p;
8935 parser->fully_implicit_function_template_p
8936 = fully_implicit_function_template_p;
8937 parser->implicit_template_parms = implicit_template_parms;
8938 parser->implicit_template_scope = implicit_template_scope;
8939 parser->auto_is_implicit_function_template_parm_p
8940 = auto_is_implicit_function_template_parm_p;
8943 pop_deferring_access_checks ();
8945 /* This field is only used during parsing of the lambda. */
8946 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8948 /* This lambda shouldn't have any proxies left at this point. */
8949 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8950 /* And now that we're done, push proxies for an enclosing lambda. */
8951 insert_pending_capture_proxies ();
8953 if (ok)
8954 return build_lambda_object (lambda_expr);
8955 else
8956 return error_mark_node;
8959 /* Parse the beginning of a lambda expression.
8961 lambda-introducer:
8962 [ lambda-capture [opt] ]
8964 LAMBDA_EXPR is the current representation of the lambda expression. */
8966 static void
8967 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8969 /* Need commas after the first capture. */
8970 bool first = true;
8972 /* Eat the leading `['. */
8973 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8975 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8976 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8977 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8978 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8979 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8980 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8982 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8984 cp_lexer_consume_token (parser->lexer);
8985 first = false;
8988 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8990 cp_token* capture_token;
8991 tree capture_id;
8992 tree capture_init_expr;
8993 cp_id_kind idk = CP_ID_KIND_NONE;
8994 bool explicit_init_p = false;
8996 enum capture_kind_type
8998 BY_COPY,
8999 BY_REFERENCE
9001 enum capture_kind_type capture_kind = BY_COPY;
9003 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9005 error ("expected end of capture-list");
9006 return;
9009 if (first)
9010 first = false;
9011 else
9012 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9014 /* Possibly capture `this'. */
9015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9017 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9018 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9019 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9020 "with by-copy capture default");
9021 cp_lexer_consume_token (parser->lexer);
9022 add_capture (lambda_expr,
9023 /*id=*/this_identifier,
9024 /*initializer=*/finish_this_expr(),
9025 /*by_reference_p=*/false,
9026 explicit_init_p);
9027 continue;
9030 /* Remember whether we want to capture as a reference or not. */
9031 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9033 capture_kind = BY_REFERENCE;
9034 cp_lexer_consume_token (parser->lexer);
9037 /* Get the identifier. */
9038 capture_token = cp_lexer_peek_token (parser->lexer);
9039 capture_id = cp_parser_identifier (parser);
9041 if (capture_id == error_mark_node)
9042 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9043 delimiters, but I modified this to stop on unnested ']' as well. It
9044 was already changed to stop on unnested '}', so the
9045 "closing_parenthesis" name is no more misleading with my change. */
9047 cp_parser_skip_to_closing_parenthesis (parser,
9048 /*recovering=*/true,
9049 /*or_comma=*/true,
9050 /*consume_paren=*/true);
9051 break;
9054 /* Find the initializer for this capture. */
9055 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9056 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9057 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9059 bool direct, non_constant;
9060 /* An explicit initializer exists. */
9061 if (cxx_dialect < cxx14)
9062 pedwarn (input_location, 0,
9063 "lambda capture initializers "
9064 "only available with -std=c++14 or -std=gnu++14");
9065 capture_init_expr = cp_parser_initializer (parser, &direct,
9066 &non_constant);
9067 explicit_init_p = true;
9068 if (capture_init_expr == NULL_TREE)
9070 error ("empty initializer for lambda init-capture");
9071 capture_init_expr = error_mark_node;
9074 else
9076 const char* error_msg;
9078 /* Turn the identifier into an id-expression. */
9079 capture_init_expr
9080 = cp_parser_lookup_name_simple (parser, capture_id,
9081 capture_token->location);
9083 if (capture_init_expr == error_mark_node)
9085 unqualified_name_lookup_error (capture_id);
9086 continue;
9088 else if (DECL_P (capture_init_expr)
9089 && (!VAR_P (capture_init_expr)
9090 && TREE_CODE (capture_init_expr) != PARM_DECL))
9092 error_at (capture_token->location,
9093 "capture of non-variable %qD ",
9094 capture_init_expr);
9095 inform (0, "%q+#D declared here", capture_init_expr);
9096 continue;
9098 if (VAR_P (capture_init_expr)
9099 && decl_storage_duration (capture_init_expr) != dk_auto)
9101 if (pedwarn (capture_token->location, 0, "capture of variable "
9102 "%qD with non-automatic storage duration",
9103 capture_init_expr))
9104 inform (0, "%q+#D declared here", capture_init_expr);
9105 continue;
9108 capture_init_expr
9109 = finish_id_expression
9110 (capture_id,
9111 capture_init_expr,
9112 parser->scope,
9113 &idk,
9114 /*integral_constant_expression_p=*/false,
9115 /*allow_non_integral_constant_expression_p=*/false,
9116 /*non_integral_constant_expression_p=*/NULL,
9117 /*template_p=*/false,
9118 /*done=*/true,
9119 /*address_p=*/false,
9120 /*template_arg_p=*/false,
9121 &error_msg,
9122 capture_token->location);
9124 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9126 cp_lexer_consume_token (parser->lexer);
9127 capture_init_expr = make_pack_expansion (capture_init_expr);
9129 else
9130 check_for_bare_parameter_packs (capture_init_expr);
9133 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9134 && !explicit_init_p)
9136 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9137 && capture_kind == BY_COPY)
9138 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9139 "of %qD redundant with by-copy capture default",
9140 capture_id);
9141 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9142 && capture_kind == BY_REFERENCE)
9143 pedwarn (capture_token->location, 0, "explicit by-reference "
9144 "capture of %qD redundant with by-reference capture "
9145 "default", capture_id);
9148 add_capture (lambda_expr,
9149 capture_id,
9150 capture_init_expr,
9151 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9152 explicit_init_p);
9155 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9158 /* Parse the (optional) middle of a lambda expression.
9160 lambda-declarator:
9161 < template-parameter-list [opt] >
9162 ( parameter-declaration-clause [opt] )
9163 attribute-specifier [opt]
9164 mutable [opt]
9165 exception-specification [opt]
9166 lambda-return-type-clause [opt]
9168 LAMBDA_EXPR is the current representation of the lambda expression. */
9170 static bool
9171 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9173 /* 5.1.1.4 of the standard says:
9174 If a lambda-expression does not include a lambda-declarator, it is as if
9175 the lambda-declarator were ().
9176 This means an empty parameter list, no attributes, and no exception
9177 specification. */
9178 tree param_list = void_list_node;
9179 tree attributes = NULL_TREE;
9180 tree exception_spec = NULL_TREE;
9181 tree template_param_list = NULL_TREE;
9183 /* The template-parameter-list is optional, but must begin with
9184 an opening angle if present. */
9185 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9187 if (cxx_dialect < cxx14)
9188 pedwarn (parser->lexer->next_token->location, 0,
9189 "lambda templates are only available with "
9190 "-std=c++14 or -std=gnu++14");
9192 cp_lexer_consume_token (parser->lexer);
9194 template_param_list = cp_parser_template_parameter_list (parser);
9196 cp_parser_skip_to_end_of_template_parameter_list (parser);
9198 /* We just processed one more parameter list. */
9199 ++parser->num_template_parameter_lists;
9202 /* The parameter-declaration-clause is optional (unless
9203 template-parameter-list was given), but must begin with an
9204 opening parenthesis if present. */
9205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9207 cp_lexer_consume_token (parser->lexer);
9209 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9211 /* Parse parameters. */
9212 param_list = cp_parser_parameter_declaration_clause (parser);
9214 /* Default arguments shall not be specified in the
9215 parameter-declaration-clause of a lambda-declarator. */
9216 for (tree t = param_list; t; t = TREE_CHAIN (t))
9217 if (TREE_PURPOSE (t))
9218 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9219 "default argument specified for lambda parameter");
9221 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9223 attributes = cp_parser_attributes_opt (parser);
9225 /* Parse optional `mutable' keyword. */
9226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9228 cp_lexer_consume_token (parser->lexer);
9229 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9232 /* Parse optional exception specification. */
9233 exception_spec = cp_parser_exception_specification_opt (parser);
9235 /* Parse optional trailing return type. */
9236 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9238 cp_lexer_consume_token (parser->lexer);
9239 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9240 = cp_parser_trailing_type_id (parser);
9243 /* The function parameters must be in scope all the way until after the
9244 trailing-return-type in case of decltype. */
9245 pop_bindings_and_leave_scope ();
9247 else if (template_param_list != NULL_TREE) // generate diagnostic
9248 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9250 /* Create the function call operator.
9252 Messing with declarators like this is no uglier than building up the
9253 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9254 other code. */
9256 cp_decl_specifier_seq return_type_specs;
9257 cp_declarator* declarator;
9258 tree fco;
9259 int quals;
9260 void *p;
9262 clear_decl_specs (&return_type_specs);
9263 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9264 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9265 else
9266 /* Maybe we will deduce the return type later. */
9267 return_type_specs.type = make_auto ();
9269 p = obstack_alloc (&declarator_obstack, 0);
9271 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9272 sfk_none);
9274 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9275 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9276 declarator = make_call_declarator (declarator, param_list, quals,
9277 VIRT_SPEC_UNSPECIFIED,
9278 REF_QUAL_NONE,
9279 exception_spec,
9280 /*late_return_type=*/NULL_TREE);
9281 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9283 fco = grokmethod (&return_type_specs,
9284 declarator,
9285 attributes);
9286 if (fco != error_mark_node)
9288 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9289 DECL_ARTIFICIAL (fco) = 1;
9290 /* Give the object parameter a different name. */
9291 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9292 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9293 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9295 if (template_param_list)
9297 fco = finish_member_template_decl (fco);
9298 finish_template_decl (template_param_list);
9299 --parser->num_template_parameter_lists;
9301 else if (parser->fully_implicit_function_template_p)
9302 fco = finish_fully_implicit_template (parser, fco);
9304 finish_member_declaration (fco);
9306 obstack_free (&declarator_obstack, p);
9308 return (fco != error_mark_node);
9312 /* Parse the body of a lambda expression, which is simply
9314 compound-statement
9316 but which requires special handling.
9317 LAMBDA_EXPR is the current representation of the lambda expression. */
9319 static void
9320 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9322 bool nested = (current_function_decl != NULL_TREE);
9323 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9324 if (nested)
9325 push_function_context ();
9326 else
9327 /* Still increment function_depth so that we don't GC in the
9328 middle of an expression. */
9329 ++function_depth;
9330 /* Clear this in case we're in the middle of a default argument. */
9331 parser->local_variables_forbidden_p = false;
9333 /* Finish the function call operator
9334 - class_specifier
9335 + late_parsing_for_member
9336 + function_definition_after_declarator
9337 + ctor_initializer_opt_and_function_body */
9339 tree fco = lambda_function (lambda_expr);
9340 tree body;
9341 bool done = false;
9342 tree compound_stmt;
9343 tree cap;
9345 /* Let the front end know that we are going to be defining this
9346 function. */
9347 start_preparsed_function (fco,
9348 NULL_TREE,
9349 SF_PRE_PARSED | SF_INCLASS_INLINE);
9351 start_lambda_scope (fco);
9352 body = begin_function_body ();
9354 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9355 goto out;
9357 /* Push the proxies for any explicit captures. */
9358 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9359 cap = TREE_CHAIN (cap))
9360 build_capture_proxy (TREE_PURPOSE (cap));
9362 compound_stmt = begin_compound_stmt (0);
9364 /* 5.1.1.4 of the standard says:
9365 If a lambda-expression does not include a trailing-return-type, it
9366 is as if the trailing-return-type denotes the following type:
9367 * if the compound-statement is of the form
9368 { return attribute-specifier [opt] expression ; }
9369 the type of the returned expression after lvalue-to-rvalue
9370 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9371 (_conv.array_ 4.2), and function-to-pointer conversion
9372 (_conv.func_ 4.3);
9373 * otherwise, void. */
9375 /* In a lambda that has neither a lambda-return-type-clause
9376 nor a deducible form, errors should be reported for return statements
9377 in the body. Since we used void as the placeholder return type, parsing
9378 the body as usual will give such desired behavior. */
9379 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9380 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9381 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9383 tree expr = NULL_TREE;
9384 cp_id_kind idk = CP_ID_KIND_NONE;
9386 /* Parse tentatively in case there's more after the initial return
9387 statement. */
9388 cp_parser_parse_tentatively (parser);
9390 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9392 expr = cp_parser_expression (parser, &idk);
9394 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9395 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9397 if (cp_parser_parse_definitely (parser))
9399 if (!processing_template_decl)
9400 apply_deduced_return_type (fco, lambda_return_type (expr));
9402 /* Will get error here if type not deduced yet. */
9403 finish_return_stmt (expr);
9405 done = true;
9409 if (!done)
9411 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9412 cp_parser_label_declaration (parser);
9413 cp_parser_statement_seq_opt (parser, NULL_TREE);
9414 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9417 finish_compound_stmt (compound_stmt);
9419 out:
9420 finish_function_body (body);
9421 finish_lambda_scope ();
9423 /* Finish the function and generate code for it if necessary. */
9424 tree fn = finish_function (/*inline*/2);
9426 /* Only expand if the call op is not a template. */
9427 if (!DECL_TEMPLATE_INFO (fco))
9428 expand_or_defer_fn (fn);
9431 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9432 if (nested)
9433 pop_function_context();
9434 else
9435 --function_depth;
9438 /* Statements [gram.stmt.stmt] */
9440 /* Parse a statement.
9442 statement:
9443 labeled-statement
9444 expression-statement
9445 compound-statement
9446 selection-statement
9447 iteration-statement
9448 jump-statement
9449 declaration-statement
9450 try-block
9452 C++11:
9454 statement:
9455 labeled-statement
9456 attribute-specifier-seq (opt) expression-statement
9457 attribute-specifier-seq (opt) compound-statement
9458 attribute-specifier-seq (opt) selection-statement
9459 attribute-specifier-seq (opt) iteration-statement
9460 attribute-specifier-seq (opt) jump-statement
9461 declaration-statement
9462 attribute-specifier-seq (opt) try-block
9464 TM Extension:
9466 statement:
9467 atomic-statement
9469 IN_COMPOUND is true when the statement is nested inside a
9470 cp_parser_compound_statement; this matters for certain pragmas.
9472 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9473 is a (possibly labeled) if statement which is not enclosed in braces
9474 and has an else clause. This is used to implement -Wparentheses. */
9476 static void
9477 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9478 bool in_compound, bool *if_p)
9480 tree statement, std_attrs = NULL_TREE;
9481 cp_token *token;
9482 location_t statement_location, attrs_location;
9484 restart:
9485 if (if_p != NULL)
9486 *if_p = false;
9487 /* There is no statement yet. */
9488 statement = NULL_TREE;
9490 cp_lexer_save_tokens (parser->lexer);
9491 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9492 if (c_dialect_objc ())
9493 /* In obj-c++, seeing '[[' might be the either the beginning of
9494 c++11 attributes, or a nested objc-message-expression. So
9495 let's parse the c++11 attributes tentatively. */
9496 cp_parser_parse_tentatively (parser);
9497 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9498 if (c_dialect_objc ())
9500 if (!cp_parser_parse_definitely (parser))
9501 std_attrs = NULL_TREE;
9504 /* Peek at the next token. */
9505 token = cp_lexer_peek_token (parser->lexer);
9506 /* Remember the location of the first token in the statement. */
9507 statement_location = token->location;
9508 /* If this is a keyword, then that will often determine what kind of
9509 statement we have. */
9510 if (token->type == CPP_KEYWORD)
9512 enum rid keyword = token->keyword;
9514 switch (keyword)
9516 case RID_CASE:
9517 case RID_DEFAULT:
9518 /* Looks like a labeled-statement with a case label.
9519 Parse the label, and then use tail recursion to parse
9520 the statement. */
9521 cp_parser_label_for_labeled_statement (parser, std_attrs);
9522 goto restart;
9524 case RID_IF:
9525 case RID_SWITCH:
9526 statement = cp_parser_selection_statement (parser, if_p);
9527 break;
9529 case RID_WHILE:
9530 case RID_DO:
9531 case RID_FOR:
9532 statement = cp_parser_iteration_statement (parser, false);
9533 break;
9535 case RID_BREAK:
9536 case RID_CONTINUE:
9537 case RID_RETURN:
9538 case RID_GOTO:
9539 statement = cp_parser_jump_statement (parser);
9540 break;
9542 case RID_CILK_SYNC:
9543 cp_lexer_consume_token (parser->lexer);
9544 if (flag_cilkplus)
9546 tree sync_expr = build_cilk_sync ();
9547 SET_EXPR_LOCATION (sync_expr,
9548 token->location);
9549 statement = finish_expr_stmt (sync_expr);
9551 else
9553 error_at (token->location, "-fcilkplus must be enabled to use"
9554 " %<_Cilk_sync%>");
9555 statement = error_mark_node;
9557 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9558 break;
9560 /* Objective-C++ exception-handling constructs. */
9561 case RID_AT_TRY:
9562 case RID_AT_CATCH:
9563 case RID_AT_FINALLY:
9564 case RID_AT_SYNCHRONIZED:
9565 case RID_AT_THROW:
9566 statement = cp_parser_objc_statement (parser);
9567 break;
9569 case RID_TRY:
9570 statement = cp_parser_try_block (parser);
9571 break;
9573 case RID_NAMESPACE:
9574 /* This must be a namespace alias definition. */
9575 cp_parser_declaration_statement (parser);
9576 return;
9578 case RID_TRANSACTION_ATOMIC:
9579 case RID_TRANSACTION_RELAXED:
9580 statement = cp_parser_transaction (parser, keyword);
9581 break;
9582 case RID_TRANSACTION_CANCEL:
9583 statement = cp_parser_transaction_cancel (parser);
9584 break;
9586 default:
9587 /* It might be a keyword like `int' that can start a
9588 declaration-statement. */
9589 break;
9592 else if (token->type == CPP_NAME)
9594 /* If the next token is a `:', then we are looking at a
9595 labeled-statement. */
9596 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9597 if (token->type == CPP_COLON)
9599 /* Looks like a labeled-statement with an ordinary label.
9600 Parse the label, and then use tail recursion to parse
9601 the statement. */
9603 cp_parser_label_for_labeled_statement (parser, std_attrs);
9604 goto restart;
9607 /* Anything that starts with a `{' must be a compound-statement. */
9608 else if (token->type == CPP_OPEN_BRACE)
9609 statement = cp_parser_compound_statement (parser, NULL, false, false);
9610 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9611 a statement all its own. */
9612 else if (token->type == CPP_PRAGMA)
9614 /* Only certain OpenMP pragmas are attached to statements, and thus
9615 are considered statements themselves. All others are not. In
9616 the context of a compound, accept the pragma as a "statement" and
9617 return so that we can check for a close brace. Otherwise we
9618 require a real statement and must go back and read one. */
9619 if (in_compound)
9620 cp_parser_pragma (parser, pragma_compound);
9621 else if (!cp_parser_pragma (parser, pragma_stmt))
9622 goto restart;
9623 return;
9625 else if (token->type == CPP_EOF)
9627 cp_parser_error (parser, "expected statement");
9628 return;
9631 /* Everything else must be a declaration-statement or an
9632 expression-statement. Try for the declaration-statement
9633 first, unless we are looking at a `;', in which case we know that
9634 we have an expression-statement. */
9635 if (!statement)
9637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9639 if (std_attrs != NULL_TREE)
9641 /* Attributes should be parsed as part of the the
9642 declaration, so let's un-parse them. */
9643 cp_lexer_rollback_tokens (parser->lexer);
9644 std_attrs = NULL_TREE;
9647 cp_parser_parse_tentatively (parser);
9648 /* Try to parse the declaration-statement. */
9649 cp_parser_declaration_statement (parser);
9650 /* If that worked, we're done. */
9651 if (cp_parser_parse_definitely (parser))
9652 return;
9654 /* Look for an expression-statement instead. */
9655 statement = cp_parser_expression_statement (parser, in_statement_expr);
9658 /* Set the line number for the statement. */
9659 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9660 SET_EXPR_LOCATION (statement, statement_location);
9662 /* Note that for now, we don't do anything with c++11 statements
9663 parsed at this level. */
9664 if (std_attrs != NULL_TREE)
9665 warning_at (attrs_location,
9666 OPT_Wattributes,
9667 "attributes at the beginning of statement are ignored");
9670 /* Parse the label for a labeled-statement, i.e.
9672 identifier :
9673 case constant-expression :
9674 default :
9676 GNU Extension:
9677 case constant-expression ... constant-expression : statement
9679 When a label is parsed without errors, the label is added to the
9680 parse tree by the finish_* functions, so this function doesn't
9681 have to return the label. */
9683 static void
9684 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9686 cp_token *token;
9687 tree label = NULL_TREE;
9688 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9690 /* The next token should be an identifier. */
9691 token = cp_lexer_peek_token (parser->lexer);
9692 if (token->type != CPP_NAME
9693 && token->type != CPP_KEYWORD)
9695 cp_parser_error (parser, "expected labeled-statement");
9696 return;
9699 parser->colon_corrects_to_scope_p = false;
9700 switch (token->keyword)
9702 case RID_CASE:
9704 tree expr, expr_hi;
9705 cp_token *ellipsis;
9707 /* Consume the `case' token. */
9708 cp_lexer_consume_token (parser->lexer);
9709 /* Parse the constant-expression. */
9710 expr = cp_parser_constant_expression (parser,
9711 /*allow_non_constant_p=*/false,
9712 NULL);
9714 ellipsis = cp_lexer_peek_token (parser->lexer);
9715 if (ellipsis->type == CPP_ELLIPSIS)
9717 /* Consume the `...' token. */
9718 cp_lexer_consume_token (parser->lexer);
9719 expr_hi =
9720 cp_parser_constant_expression (parser,
9721 /*allow_non_constant_p=*/false,
9722 NULL);
9723 /* We don't need to emit warnings here, as the common code
9724 will do this for us. */
9726 else
9727 expr_hi = NULL_TREE;
9729 if (parser->in_switch_statement_p)
9730 finish_case_label (token->location, expr, expr_hi);
9731 else
9732 error_at (token->location,
9733 "case label %qE not within a switch statement",
9734 expr);
9736 break;
9738 case RID_DEFAULT:
9739 /* Consume the `default' token. */
9740 cp_lexer_consume_token (parser->lexer);
9742 if (parser->in_switch_statement_p)
9743 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9744 else
9745 error_at (token->location, "case label not within a switch statement");
9746 break;
9748 default:
9749 /* Anything else must be an ordinary label. */
9750 label = finish_label_stmt (cp_parser_identifier (parser));
9751 break;
9754 /* Require the `:' token. */
9755 cp_parser_require (parser, CPP_COLON, RT_COLON);
9757 /* An ordinary label may optionally be followed by attributes.
9758 However, this is only permitted if the attributes are then
9759 followed by a semicolon. This is because, for backward
9760 compatibility, when parsing
9761 lab: __attribute__ ((unused)) int i;
9762 we want the attribute to attach to "i", not "lab". */
9763 if (label != NULL_TREE
9764 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9766 tree attrs;
9767 cp_parser_parse_tentatively (parser);
9768 attrs = cp_parser_gnu_attributes_opt (parser);
9769 if (attrs == NULL_TREE
9770 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9771 cp_parser_abort_tentative_parse (parser);
9772 else if (!cp_parser_parse_definitely (parser))
9774 else
9775 attributes = chainon (attributes, attrs);
9778 if (attributes != NULL_TREE)
9779 cplus_decl_attributes (&label, attributes, 0);
9781 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9784 /* Parse an expression-statement.
9786 expression-statement:
9787 expression [opt] ;
9789 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9790 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9791 indicates whether this expression-statement is part of an
9792 expression statement. */
9794 static tree
9795 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9797 tree statement = NULL_TREE;
9798 cp_token *token = cp_lexer_peek_token (parser->lexer);
9800 /* If the next token is a ';', then there is no expression
9801 statement. */
9802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9804 statement = cp_parser_expression (parser);
9805 if (statement == error_mark_node
9806 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9808 cp_parser_skip_to_end_of_block_or_statement (parser);
9809 return error_mark_node;
9813 /* Give a helpful message for "A<T>::type t;" and the like. */
9814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9815 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9817 if (TREE_CODE (statement) == SCOPE_REF)
9818 error_at (token->location, "need %<typename%> before %qE because "
9819 "%qT is a dependent scope",
9820 statement, TREE_OPERAND (statement, 0));
9821 else if (is_overloaded_fn (statement)
9822 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9824 /* A::A a; */
9825 tree fn = get_first_fn (statement);
9826 error_at (token->location,
9827 "%<%T::%D%> names the constructor, not the type",
9828 DECL_CONTEXT (fn), DECL_NAME (fn));
9832 /* Consume the final `;'. */
9833 cp_parser_consume_semicolon_at_end_of_statement (parser);
9835 if (in_statement_expr
9836 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9837 /* This is the final expression statement of a statement
9838 expression. */
9839 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9840 else if (statement)
9841 statement = finish_expr_stmt (statement);
9843 return statement;
9846 /* Parse a compound-statement.
9848 compound-statement:
9849 { statement-seq [opt] }
9851 GNU extension:
9853 compound-statement:
9854 { label-declaration-seq [opt] statement-seq [opt] }
9856 label-declaration-seq:
9857 label-declaration
9858 label-declaration-seq label-declaration
9860 Returns a tree representing the statement. */
9862 static tree
9863 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9864 bool in_try, bool function_body)
9866 tree compound_stmt;
9868 /* Consume the `{'. */
9869 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9870 return error_mark_node;
9871 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9872 && !function_body)
9873 pedwarn (input_location, OPT_Wpedantic,
9874 "compound-statement in constexpr function");
9875 /* Begin the compound-statement. */
9876 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9877 /* If the next keyword is `__label__' we have a label declaration. */
9878 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9879 cp_parser_label_declaration (parser);
9880 /* Parse an (optional) statement-seq. */
9881 cp_parser_statement_seq_opt (parser, in_statement_expr);
9882 /* Finish the compound-statement. */
9883 finish_compound_stmt (compound_stmt);
9884 /* Consume the `}'. */
9885 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9887 return compound_stmt;
9890 /* Parse an (optional) statement-seq.
9892 statement-seq:
9893 statement
9894 statement-seq [opt] statement */
9896 static void
9897 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9899 /* Scan statements until there aren't any more. */
9900 while (true)
9902 cp_token *token = cp_lexer_peek_token (parser->lexer);
9904 /* If we are looking at a `}', then we have run out of
9905 statements; the same is true if we have reached the end
9906 of file, or have stumbled upon a stray '@end'. */
9907 if (token->type == CPP_CLOSE_BRACE
9908 || token->type == CPP_EOF
9909 || token->type == CPP_PRAGMA_EOL
9910 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9911 break;
9913 /* If we are in a compound statement and find 'else' then
9914 something went wrong. */
9915 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9917 if (parser->in_statement & IN_IF_STMT)
9918 break;
9919 else
9921 token = cp_lexer_consume_token (parser->lexer);
9922 error_at (token->location, "%<else%> without a previous %<if%>");
9926 /* Parse the statement. */
9927 cp_parser_statement (parser, in_statement_expr, true, NULL);
9931 /* Parse a selection-statement.
9933 selection-statement:
9934 if ( condition ) statement
9935 if ( condition ) statement else statement
9936 switch ( condition ) statement
9938 Returns the new IF_STMT or SWITCH_STMT.
9940 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9941 is a (possibly labeled) if statement which is not enclosed in
9942 braces and has an else clause. This is used to implement
9943 -Wparentheses. */
9945 static tree
9946 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9948 cp_token *token;
9949 enum rid keyword;
9951 if (if_p != NULL)
9952 *if_p = false;
9954 /* Peek at the next token. */
9955 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9957 /* See what kind of keyword it is. */
9958 keyword = token->keyword;
9959 switch (keyword)
9961 case RID_IF:
9962 case RID_SWITCH:
9964 tree statement;
9965 tree condition;
9967 /* Look for the `('. */
9968 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9970 cp_parser_skip_to_end_of_statement (parser);
9971 return error_mark_node;
9974 /* Begin the selection-statement. */
9975 if (keyword == RID_IF)
9976 statement = begin_if_stmt ();
9977 else
9978 statement = begin_switch_stmt ();
9980 /* Parse the condition. */
9981 condition = cp_parser_condition (parser);
9982 /* Look for the `)'. */
9983 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9984 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9985 /*consume_paren=*/true);
9987 if (keyword == RID_IF)
9989 bool nested_if;
9990 unsigned char in_statement;
9992 /* Add the condition. */
9993 finish_if_stmt_cond (condition, statement);
9995 /* Parse the then-clause. */
9996 in_statement = parser->in_statement;
9997 parser->in_statement |= IN_IF_STMT;
9998 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10000 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10001 add_stmt (build_empty_stmt (loc));
10002 cp_lexer_consume_token (parser->lexer);
10003 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10004 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10005 "empty body in an %<if%> statement");
10006 nested_if = false;
10008 else
10009 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10010 parser->in_statement = in_statement;
10012 finish_then_clause (statement);
10014 /* If the next token is `else', parse the else-clause. */
10015 if (cp_lexer_next_token_is_keyword (parser->lexer,
10016 RID_ELSE))
10018 /* Consume the `else' keyword. */
10019 cp_lexer_consume_token (parser->lexer);
10020 begin_else_clause (statement);
10021 /* Parse the else-clause. */
10022 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10024 location_t loc;
10025 loc = cp_lexer_peek_token (parser->lexer)->location;
10026 warning_at (loc,
10027 OPT_Wempty_body, "suggest braces around "
10028 "empty body in an %<else%> statement");
10029 add_stmt (build_empty_stmt (loc));
10030 cp_lexer_consume_token (parser->lexer);
10032 else
10033 cp_parser_implicitly_scoped_statement (parser, NULL);
10035 finish_else_clause (statement);
10037 /* If we are currently parsing a then-clause, then
10038 IF_P will not be NULL. We set it to true to
10039 indicate that this if statement has an else clause.
10040 This may trigger the Wparentheses warning below
10041 when we get back up to the parent if statement. */
10042 if (if_p != NULL)
10043 *if_p = true;
10045 else
10047 /* This if statement does not have an else clause. If
10048 NESTED_IF is true, then the then-clause is an if
10049 statement which does have an else clause. We warn
10050 about the potential ambiguity. */
10051 if (nested_if)
10052 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10053 "suggest explicit braces to avoid ambiguous"
10054 " %<else%>");
10057 /* Now we're all done with the if-statement. */
10058 finish_if_stmt (statement);
10060 else
10062 bool in_switch_statement_p;
10063 unsigned char in_statement;
10065 /* Add the condition. */
10066 finish_switch_cond (condition, statement);
10068 /* Parse the body of the switch-statement. */
10069 in_switch_statement_p = parser->in_switch_statement_p;
10070 in_statement = parser->in_statement;
10071 parser->in_switch_statement_p = true;
10072 parser->in_statement |= IN_SWITCH_STMT;
10073 cp_parser_implicitly_scoped_statement (parser, NULL);
10074 parser->in_switch_statement_p = in_switch_statement_p;
10075 parser->in_statement = in_statement;
10077 /* Now we're all done with the switch-statement. */
10078 finish_switch_stmt (statement);
10081 return statement;
10083 break;
10085 default:
10086 cp_parser_error (parser, "expected selection-statement");
10087 return error_mark_node;
10091 /* Parse a condition.
10093 condition:
10094 expression
10095 type-specifier-seq declarator = initializer-clause
10096 type-specifier-seq declarator braced-init-list
10098 GNU Extension:
10100 condition:
10101 type-specifier-seq declarator asm-specification [opt]
10102 attributes [opt] = assignment-expression
10104 Returns the expression that should be tested. */
10106 static tree
10107 cp_parser_condition (cp_parser* parser)
10109 cp_decl_specifier_seq type_specifiers;
10110 const char *saved_message;
10111 int declares_class_or_enum;
10113 /* Try the declaration first. */
10114 cp_parser_parse_tentatively (parser);
10115 /* New types are not allowed in the type-specifier-seq for a
10116 condition. */
10117 saved_message = parser->type_definition_forbidden_message;
10118 parser->type_definition_forbidden_message
10119 = G_("types may not be defined in conditions");
10120 /* Parse the type-specifier-seq. */
10121 cp_parser_decl_specifier_seq (parser,
10122 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10123 &type_specifiers,
10124 &declares_class_or_enum);
10125 /* Restore the saved message. */
10126 parser->type_definition_forbidden_message = saved_message;
10127 /* If all is well, we might be looking at a declaration. */
10128 if (!cp_parser_error_occurred (parser))
10130 tree decl;
10131 tree asm_specification;
10132 tree attributes;
10133 cp_declarator *declarator;
10134 tree initializer = NULL_TREE;
10136 /* Parse the declarator. */
10137 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10138 /*ctor_dtor_or_conv_p=*/NULL,
10139 /*parenthesized_p=*/NULL,
10140 /*member_p=*/false,
10141 /*friend_p=*/false);
10142 /* Parse the attributes. */
10143 attributes = cp_parser_attributes_opt (parser);
10144 /* Parse the asm-specification. */
10145 asm_specification = cp_parser_asm_specification_opt (parser);
10146 /* If the next token is not an `=' or '{', then we might still be
10147 looking at an expression. For example:
10149 if (A(a).x)
10151 looks like a decl-specifier-seq and a declarator -- but then
10152 there is no `=', so this is an expression. */
10153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10154 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10155 cp_parser_simulate_error (parser);
10157 /* If we did see an `=' or '{', then we are looking at a declaration
10158 for sure. */
10159 if (cp_parser_parse_definitely (parser))
10161 tree pushed_scope;
10162 bool non_constant_p;
10163 bool flags = LOOKUP_ONLYCONVERTING;
10165 /* Create the declaration. */
10166 decl = start_decl (declarator, &type_specifiers,
10167 /*initialized_p=*/true,
10168 attributes, /*prefix_attributes=*/NULL_TREE,
10169 &pushed_scope);
10171 /* Parse the initializer. */
10172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10174 initializer = cp_parser_braced_list (parser, &non_constant_p);
10175 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10176 flags = 0;
10178 else
10180 /* Consume the `='. */
10181 cp_parser_require (parser, CPP_EQ, RT_EQ);
10182 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10184 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10185 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10187 /* Process the initializer. */
10188 cp_finish_decl (decl,
10189 initializer, !non_constant_p,
10190 asm_specification,
10191 flags);
10193 if (pushed_scope)
10194 pop_scope (pushed_scope);
10196 return convert_from_reference (decl);
10199 /* If we didn't even get past the declarator successfully, we are
10200 definitely not looking at a declaration. */
10201 else
10202 cp_parser_abort_tentative_parse (parser);
10204 /* Otherwise, we are looking at an expression. */
10205 return cp_parser_expression (parser);
10208 /* Parses a for-statement or range-for-statement until the closing ')',
10209 not included. */
10211 static tree
10212 cp_parser_for (cp_parser *parser, bool ivdep)
10214 tree init, scope, decl;
10215 bool is_range_for;
10217 /* Begin the for-statement. */
10218 scope = begin_for_scope (&init);
10220 /* Parse the initialization. */
10221 is_range_for = cp_parser_for_init_statement (parser, &decl);
10223 if (is_range_for)
10224 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10225 else
10226 return cp_parser_c_for (parser, scope, init, ivdep);
10229 static tree
10230 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10232 /* Normal for loop */
10233 tree condition = NULL_TREE;
10234 tree expression = NULL_TREE;
10235 tree stmt;
10237 stmt = begin_for_stmt (scope, init);
10238 /* The for-init-statement has already been parsed in
10239 cp_parser_for_init_statement, so no work is needed here. */
10240 finish_for_init_stmt (stmt);
10242 /* If there's a condition, process it. */
10243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10244 condition = cp_parser_condition (parser);
10245 else if (ivdep)
10247 cp_parser_error (parser, "missing loop condition in loop with "
10248 "%<GCC ivdep%> pragma");
10249 condition = error_mark_node;
10251 finish_for_cond (condition, stmt, ivdep);
10252 /* Look for the `;'. */
10253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10255 /* If there's an expression, process it. */
10256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10257 expression = cp_parser_expression (parser);
10258 finish_for_expr (expression, stmt);
10260 return stmt;
10263 /* Tries to parse a range-based for-statement:
10265 range-based-for:
10266 decl-specifier-seq declarator : expression
10268 The decl-specifier-seq declarator and the `:' are already parsed by
10269 cp_parser_for_init_statement. If processing_template_decl it returns a
10270 newly created RANGE_FOR_STMT; if not, it is converted to a
10271 regular FOR_STMT. */
10273 static tree
10274 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10275 bool ivdep)
10277 tree stmt, range_expr;
10279 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10281 bool expr_non_constant_p;
10282 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10284 else
10285 range_expr = cp_parser_expression (parser);
10287 /* If in template, STMT is converted to a normal for-statement
10288 at instantiation. If not, it is done just ahead. */
10289 if (processing_template_decl)
10291 if (check_for_bare_parameter_packs (range_expr))
10292 range_expr = error_mark_node;
10293 stmt = begin_range_for_stmt (scope, init);
10294 if (ivdep)
10295 RANGE_FOR_IVDEP (stmt) = 1;
10296 finish_range_for_decl (stmt, range_decl, range_expr);
10297 if (!type_dependent_expression_p (range_expr)
10298 /* do_auto_deduction doesn't mess with template init-lists. */
10299 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10300 do_range_for_auto_deduction (range_decl, range_expr);
10302 else
10304 stmt = begin_for_stmt (scope, init);
10305 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10307 return stmt;
10310 /* Subroutine of cp_convert_range_for: given the initializer expression,
10311 builds up the range temporary. */
10313 static tree
10314 build_range_temp (tree range_expr)
10316 tree range_type, range_temp;
10318 /* Find out the type deduced by the declaration
10319 `auto &&__range = range_expr'. */
10320 range_type = cp_build_reference_type (make_auto (), true);
10321 range_type = do_auto_deduction (range_type, range_expr,
10322 type_uses_auto (range_type));
10324 /* Create the __range variable. */
10325 range_temp = build_decl (input_location, VAR_DECL,
10326 get_identifier ("__for_range"), range_type);
10327 TREE_USED (range_temp) = 1;
10328 DECL_ARTIFICIAL (range_temp) = 1;
10330 return range_temp;
10333 /* Used by cp_parser_range_for in template context: we aren't going to
10334 do a full conversion yet, but we still need to resolve auto in the
10335 type of the for-range-declaration if present. This is basically
10336 a shortcut version of cp_convert_range_for. */
10338 static void
10339 do_range_for_auto_deduction (tree decl, tree range_expr)
10341 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10342 if (auto_node)
10344 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10345 range_temp = convert_from_reference (build_range_temp (range_expr));
10346 iter_type = (cp_parser_perform_range_for_lookup
10347 (range_temp, &begin_dummy, &end_dummy));
10348 if (iter_type)
10350 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10351 iter_type);
10352 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10353 tf_warning_or_error);
10354 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10355 iter_decl, auto_node);
10360 /* Converts a range-based for-statement into a normal
10361 for-statement, as per the definition.
10363 for (RANGE_DECL : RANGE_EXPR)
10364 BLOCK
10366 should be equivalent to:
10369 auto &&__range = RANGE_EXPR;
10370 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10371 __begin != __end;
10372 ++__begin)
10374 RANGE_DECL = *__begin;
10375 BLOCK
10379 If RANGE_EXPR is an array:
10380 BEGIN_EXPR = __range
10381 END_EXPR = __range + ARRAY_SIZE(__range)
10382 Else if RANGE_EXPR has a member 'begin' or 'end':
10383 BEGIN_EXPR = __range.begin()
10384 END_EXPR = __range.end()
10385 Else:
10386 BEGIN_EXPR = begin(__range)
10387 END_EXPR = end(__range);
10389 If __range has a member 'begin' but not 'end', or vice versa, we must
10390 still use the second alternative (it will surely fail, however).
10391 When calling begin()/end() in the third alternative we must use
10392 argument dependent lookup, but always considering 'std' as an associated
10393 namespace. */
10395 tree
10396 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10397 bool ivdep)
10399 tree begin, end;
10400 tree iter_type, begin_expr, end_expr;
10401 tree condition, expression;
10403 if (range_decl == error_mark_node || range_expr == error_mark_node)
10404 /* If an error happened previously do nothing or else a lot of
10405 unhelpful errors would be issued. */
10406 begin_expr = end_expr = iter_type = error_mark_node;
10407 else
10409 tree range_temp;
10411 if (TREE_CODE (range_expr) == VAR_DECL
10412 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10413 /* Can't bind a reference to an array of runtime bound. */
10414 range_temp = range_expr;
10415 else
10417 range_temp = build_range_temp (range_expr);
10418 pushdecl (range_temp);
10419 cp_finish_decl (range_temp, range_expr,
10420 /*is_constant_init*/false, NULL_TREE,
10421 LOOKUP_ONLYCONVERTING);
10422 range_temp = convert_from_reference (range_temp);
10424 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10425 &begin_expr, &end_expr);
10428 /* The new for initialization statement. */
10429 begin = build_decl (input_location, VAR_DECL,
10430 get_identifier ("__for_begin"), iter_type);
10431 TREE_USED (begin) = 1;
10432 DECL_ARTIFICIAL (begin) = 1;
10433 pushdecl (begin);
10434 cp_finish_decl (begin, begin_expr,
10435 /*is_constant_init*/false, NULL_TREE,
10436 LOOKUP_ONLYCONVERTING);
10438 end = build_decl (input_location, VAR_DECL,
10439 get_identifier ("__for_end"), iter_type);
10440 TREE_USED (end) = 1;
10441 DECL_ARTIFICIAL (end) = 1;
10442 pushdecl (end);
10443 cp_finish_decl (end, end_expr,
10444 /*is_constant_init*/false, NULL_TREE,
10445 LOOKUP_ONLYCONVERTING);
10447 finish_for_init_stmt (statement);
10449 /* The new for condition. */
10450 condition = build_x_binary_op (input_location, NE_EXPR,
10451 begin, ERROR_MARK,
10452 end, ERROR_MARK,
10453 NULL, tf_warning_or_error);
10454 finish_for_cond (condition, statement, ivdep);
10456 /* The new increment expression. */
10457 expression = finish_unary_op_expr (input_location,
10458 PREINCREMENT_EXPR, begin,
10459 tf_warning_or_error);
10460 finish_for_expr (expression, statement);
10462 /* The declaration is initialized with *__begin inside the loop body. */
10463 cp_finish_decl (range_decl,
10464 build_x_indirect_ref (input_location, begin, RO_NULL,
10465 tf_warning_or_error),
10466 /*is_constant_init*/false, NULL_TREE,
10467 LOOKUP_ONLYCONVERTING);
10469 return statement;
10472 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10473 We need to solve both at the same time because the method used
10474 depends on the existence of members begin or end.
10475 Returns the type deduced for the iterator expression. */
10477 static tree
10478 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10480 if (error_operand_p (range))
10482 *begin = *end = error_mark_node;
10483 return error_mark_node;
10486 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10488 error ("range-based %<for%> expression of type %qT "
10489 "has incomplete type", TREE_TYPE (range));
10490 *begin = *end = error_mark_node;
10491 return error_mark_node;
10493 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10495 /* If RANGE is an array, we will use pointer arithmetic. */
10496 *begin = range;
10497 *end = build_binary_op (input_location, PLUS_EXPR,
10498 range,
10499 array_type_nelts_top (TREE_TYPE (range)),
10501 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10503 else
10505 /* If it is not an array, we must do a bit of magic. */
10506 tree id_begin, id_end;
10507 tree member_begin, member_end;
10509 *begin = *end = error_mark_node;
10511 id_begin = get_identifier ("begin");
10512 id_end = get_identifier ("end");
10513 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10514 /*protect=*/2, /*want_type=*/false,
10515 tf_warning_or_error);
10516 member_end = lookup_member (TREE_TYPE (range), id_end,
10517 /*protect=*/2, /*want_type=*/false,
10518 tf_warning_or_error);
10520 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10522 /* Use the member functions. */
10523 if (member_begin != NULL_TREE)
10524 *begin = cp_parser_range_for_member_function (range, id_begin);
10525 else
10526 error ("range-based %<for%> expression of type %qT has an "
10527 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10529 if (member_end != NULL_TREE)
10530 *end = cp_parser_range_for_member_function (range, id_end);
10531 else
10532 error ("range-based %<for%> expression of type %qT has a "
10533 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10535 else
10537 /* Use global functions with ADL. */
10538 vec<tree, va_gc> *vec;
10539 vec = make_tree_vector ();
10541 vec_safe_push (vec, range);
10543 member_begin = perform_koenig_lookup (id_begin, vec,
10544 tf_warning_or_error);
10545 *begin = finish_call_expr (member_begin, &vec, false, true,
10546 tf_warning_or_error);
10547 member_end = perform_koenig_lookup (id_end, vec,
10548 tf_warning_or_error);
10549 *end = finish_call_expr (member_end, &vec, false, true,
10550 tf_warning_or_error);
10552 release_tree_vector (vec);
10555 /* Last common checks. */
10556 if (*begin == error_mark_node || *end == error_mark_node)
10558 /* If one of the expressions is an error do no more checks. */
10559 *begin = *end = error_mark_node;
10560 return error_mark_node;
10562 else if (type_dependent_expression_p (*begin)
10563 || type_dependent_expression_p (*end))
10564 /* Can happen, when, eg, in a template context, Koenig lookup
10565 can't resolve begin/end (c++/58503). */
10566 return NULL_TREE;
10567 else
10569 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10570 /* The unqualified type of the __begin and __end temporaries should
10571 be the same, as required by the multiple auto declaration. */
10572 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10573 error ("inconsistent begin/end types in range-based %<for%> "
10574 "statement: %qT and %qT",
10575 TREE_TYPE (*begin), TREE_TYPE (*end));
10576 return iter_type;
10581 /* Helper function for cp_parser_perform_range_for_lookup.
10582 Builds a tree for RANGE.IDENTIFIER(). */
10584 static tree
10585 cp_parser_range_for_member_function (tree range, tree identifier)
10587 tree member, res;
10588 vec<tree, va_gc> *vec;
10590 member = finish_class_member_access_expr (range, identifier,
10591 false, tf_warning_or_error);
10592 if (member == error_mark_node)
10593 return error_mark_node;
10595 vec = make_tree_vector ();
10596 res = finish_call_expr (member, &vec,
10597 /*disallow_virtual=*/false,
10598 /*koenig_p=*/false,
10599 tf_warning_or_error);
10600 release_tree_vector (vec);
10601 return res;
10604 /* Parse an iteration-statement.
10606 iteration-statement:
10607 while ( condition ) statement
10608 do statement while ( expression ) ;
10609 for ( for-init-statement condition [opt] ; expression [opt] )
10610 statement
10612 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10614 static tree
10615 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10617 cp_token *token;
10618 enum rid keyword;
10619 tree statement;
10620 unsigned char in_statement;
10622 /* Peek at the next token. */
10623 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10624 if (!token)
10625 return error_mark_node;
10627 /* Remember whether or not we are already within an iteration
10628 statement. */
10629 in_statement = parser->in_statement;
10631 /* See what kind of keyword it is. */
10632 keyword = token->keyword;
10633 switch (keyword)
10635 case RID_WHILE:
10637 tree condition;
10639 /* Begin the while-statement. */
10640 statement = begin_while_stmt ();
10641 /* Look for the `('. */
10642 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10643 /* Parse the condition. */
10644 condition = cp_parser_condition (parser);
10645 finish_while_stmt_cond (condition, statement, ivdep);
10646 /* Look for the `)'. */
10647 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10648 /* Parse the dependent statement. */
10649 parser->in_statement = IN_ITERATION_STMT;
10650 cp_parser_already_scoped_statement (parser);
10651 parser->in_statement = in_statement;
10652 /* We're done with the while-statement. */
10653 finish_while_stmt (statement);
10655 break;
10657 case RID_DO:
10659 tree expression;
10661 /* Begin the do-statement. */
10662 statement = begin_do_stmt ();
10663 /* Parse the body of the do-statement. */
10664 parser->in_statement = IN_ITERATION_STMT;
10665 cp_parser_implicitly_scoped_statement (parser, NULL);
10666 parser->in_statement = in_statement;
10667 finish_do_body (statement);
10668 /* Look for the `while' keyword. */
10669 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10670 /* Look for the `('. */
10671 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10672 /* Parse the expression. */
10673 expression = cp_parser_expression (parser);
10674 /* We're done with the do-statement. */
10675 finish_do_stmt (expression, statement, ivdep);
10676 /* Look for the `)'. */
10677 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10678 /* Look for the `;'. */
10679 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10681 break;
10683 case RID_FOR:
10685 /* Look for the `('. */
10686 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10688 statement = cp_parser_for (parser, ivdep);
10690 /* Look for the `)'. */
10691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10693 /* Parse the body of the for-statement. */
10694 parser->in_statement = IN_ITERATION_STMT;
10695 cp_parser_already_scoped_statement (parser);
10696 parser->in_statement = in_statement;
10698 /* We're done with the for-statement. */
10699 finish_for_stmt (statement);
10701 break;
10703 default:
10704 cp_parser_error (parser, "expected iteration-statement");
10705 statement = error_mark_node;
10706 break;
10709 return statement;
10712 /* Parse a for-init-statement or the declarator of a range-based-for.
10713 Returns true if a range-based-for declaration is seen.
10715 for-init-statement:
10716 expression-statement
10717 simple-declaration */
10719 static bool
10720 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10722 /* If the next token is a `;', then we have an empty
10723 expression-statement. Grammatically, this is also a
10724 simple-declaration, but an invalid one, because it does not
10725 declare anything. Therefore, if we did not handle this case
10726 specially, we would issue an error message about an invalid
10727 declaration. */
10728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10730 bool is_range_for = false;
10731 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10734 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10736 /* N3994 -- for (id : init) ... */
10737 if (cxx_dialect < cxx1z)
10738 pedwarn (input_location, 0, "range-based for loop without a "
10739 "type-specifier only available with "
10740 "-std=c++1z or -std=gnu++1z");
10741 tree name = cp_parser_identifier (parser);
10742 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10743 *decl = build_decl (input_location, VAR_DECL, name, type);
10744 pushdecl (*decl);
10745 cp_lexer_consume_token (parser->lexer);
10746 return true;
10749 /* A colon is used in range-based for. */
10750 parser->colon_corrects_to_scope_p = false;
10752 /* We're going to speculatively look for a declaration, falling back
10753 to an expression, if necessary. */
10754 cp_parser_parse_tentatively (parser);
10755 /* Parse the declaration. */
10756 cp_parser_simple_declaration (parser,
10757 /*function_definition_allowed_p=*/false,
10758 decl);
10759 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10760 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10762 /* It is a range-for, consume the ':' */
10763 cp_lexer_consume_token (parser->lexer);
10764 is_range_for = true;
10765 if (cxx_dialect < cxx11)
10767 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10768 "range-based %<for%> loops only available with "
10769 "-std=c++11 or -std=gnu++11");
10770 *decl = error_mark_node;
10773 else
10774 /* The ';' is not consumed yet because we told
10775 cp_parser_simple_declaration not to. */
10776 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10778 if (cp_parser_parse_definitely (parser))
10779 return is_range_for;
10780 /* If the tentative parse failed, then we shall need to look for an
10781 expression-statement. */
10783 /* If we are here, it is an expression-statement. */
10784 cp_parser_expression_statement (parser, NULL_TREE);
10785 return false;
10788 /* Parse a jump-statement.
10790 jump-statement:
10791 break ;
10792 continue ;
10793 return expression [opt] ;
10794 return braced-init-list ;
10795 goto identifier ;
10797 GNU extension:
10799 jump-statement:
10800 goto * expression ;
10802 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10804 static tree
10805 cp_parser_jump_statement (cp_parser* parser)
10807 tree statement = error_mark_node;
10808 cp_token *token;
10809 enum rid keyword;
10810 unsigned char in_statement;
10812 /* Peek at the next token. */
10813 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10814 if (!token)
10815 return error_mark_node;
10817 /* See what kind of keyword it is. */
10818 keyword = token->keyword;
10819 switch (keyword)
10821 case RID_BREAK:
10822 in_statement = parser->in_statement & ~IN_IF_STMT;
10823 switch (in_statement)
10825 case 0:
10826 error_at (token->location, "break statement not within loop or switch");
10827 break;
10828 default:
10829 gcc_assert ((in_statement & IN_SWITCH_STMT)
10830 || in_statement == IN_ITERATION_STMT);
10831 statement = finish_break_stmt ();
10832 if (in_statement == IN_ITERATION_STMT)
10833 break_maybe_infinite_loop ();
10834 break;
10835 case IN_OMP_BLOCK:
10836 error_at (token->location, "invalid exit from OpenMP structured block");
10837 break;
10838 case IN_OMP_FOR:
10839 error_at (token->location, "break statement used with OpenMP for loop");
10840 break;
10841 case IN_CILK_SIMD_FOR:
10842 error_at (token->location, "break statement used with Cilk Plus for loop");
10843 break;
10845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10846 break;
10848 case RID_CONTINUE:
10849 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10851 case 0:
10852 error_at (token->location, "continue statement not within a loop");
10853 break;
10854 case IN_CILK_SIMD_FOR:
10855 error_at (token->location,
10856 "continue statement within %<#pragma simd%> loop body");
10857 /* Fall through. */
10858 case IN_ITERATION_STMT:
10859 case IN_OMP_FOR:
10860 statement = finish_continue_stmt ();
10861 break;
10862 case IN_OMP_BLOCK:
10863 error_at (token->location, "invalid exit from OpenMP structured block");
10864 break;
10865 default:
10866 gcc_unreachable ();
10868 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10869 break;
10871 case RID_RETURN:
10873 tree expr;
10874 bool expr_non_constant_p;
10876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10878 cp_lexer_set_source_position (parser->lexer);
10879 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10880 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10882 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10883 expr = cp_parser_expression (parser);
10884 else
10885 /* If the next token is a `;', then there is no
10886 expression. */
10887 expr = NULL_TREE;
10888 /* Build the return-statement. */
10889 statement = finish_return_stmt (expr);
10890 /* Look for the final `;'. */
10891 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10893 break;
10895 case RID_GOTO:
10896 /* Create the goto-statement. */
10897 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10899 /* Issue a warning about this use of a GNU extension. */
10900 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10901 /* Consume the '*' token. */
10902 cp_lexer_consume_token (parser->lexer);
10903 /* Parse the dependent expression. */
10904 finish_goto_stmt (cp_parser_expression (parser));
10906 else
10907 finish_goto_stmt (cp_parser_identifier (parser));
10908 /* Look for the final `;'. */
10909 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10910 break;
10912 default:
10913 cp_parser_error (parser, "expected jump-statement");
10914 break;
10917 return statement;
10920 /* Parse a declaration-statement.
10922 declaration-statement:
10923 block-declaration */
10925 static void
10926 cp_parser_declaration_statement (cp_parser* parser)
10928 void *p;
10930 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10931 p = obstack_alloc (&declarator_obstack, 0);
10933 /* Parse the block-declaration. */
10934 cp_parser_block_declaration (parser, /*statement_p=*/true);
10936 /* Free any declarators allocated. */
10937 obstack_free (&declarator_obstack, p);
10940 /* Some dependent statements (like `if (cond) statement'), are
10941 implicitly in their own scope. In other words, if the statement is
10942 a single statement (as opposed to a compound-statement), it is
10943 none-the-less treated as if it were enclosed in braces. Any
10944 declarations appearing in the dependent statement are out of scope
10945 after control passes that point. This function parses a statement,
10946 but ensures that is in its own scope, even if it is not a
10947 compound-statement.
10949 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10950 is a (possibly labeled) if statement which is not enclosed in
10951 braces and has an else clause. This is used to implement
10952 -Wparentheses.
10954 Returns the new statement. */
10956 static tree
10957 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10959 tree statement;
10961 if (if_p != NULL)
10962 *if_p = false;
10964 /* Mark if () ; with a special NOP_EXPR. */
10965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10967 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10968 cp_lexer_consume_token (parser->lexer);
10969 statement = add_stmt (build_empty_stmt (loc));
10971 /* if a compound is opened, we simply parse the statement directly. */
10972 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10973 statement = cp_parser_compound_statement (parser, NULL, false, false);
10974 /* If the token is not a `{', then we must take special action. */
10975 else
10977 /* Create a compound-statement. */
10978 statement = begin_compound_stmt (0);
10979 /* Parse the dependent-statement. */
10980 cp_parser_statement (parser, NULL_TREE, false, if_p);
10981 /* Finish the dummy compound-statement. */
10982 finish_compound_stmt (statement);
10985 /* Return the statement. */
10986 return statement;
10989 /* For some dependent statements (like `while (cond) statement'), we
10990 have already created a scope. Therefore, even if the dependent
10991 statement is a compound-statement, we do not want to create another
10992 scope. */
10994 static void
10995 cp_parser_already_scoped_statement (cp_parser* parser)
10997 /* If the token is a `{', then we must take special action. */
10998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10999 cp_parser_statement (parser, NULL_TREE, false, NULL);
11000 else
11002 /* Avoid calling cp_parser_compound_statement, so that we
11003 don't create a new scope. Do everything else by hand. */
11004 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11005 /* If the next keyword is `__label__' we have a label declaration. */
11006 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11007 cp_parser_label_declaration (parser);
11008 /* Parse an (optional) statement-seq. */
11009 cp_parser_statement_seq_opt (parser, NULL_TREE);
11010 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11014 /* Declarations [gram.dcl.dcl] */
11016 /* Parse an optional declaration-sequence.
11018 declaration-seq:
11019 declaration
11020 declaration-seq declaration */
11022 static void
11023 cp_parser_declaration_seq_opt (cp_parser* parser)
11025 while (true)
11027 cp_token *token;
11029 token = cp_lexer_peek_token (parser->lexer);
11031 if (token->type == CPP_CLOSE_BRACE
11032 || token->type == CPP_EOF
11033 || token->type == CPP_PRAGMA_EOL)
11034 break;
11036 if (token->type == CPP_SEMICOLON)
11038 /* A declaration consisting of a single semicolon is
11039 invalid. Allow it unless we're being pedantic. */
11040 cp_lexer_consume_token (parser->lexer);
11041 if (!in_system_header_at (input_location))
11042 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11043 continue;
11046 /* If we're entering or exiting a region that's implicitly
11047 extern "C", modify the lang context appropriately. */
11048 if (!parser->implicit_extern_c && token->implicit_extern_c)
11050 push_lang_context (lang_name_c);
11051 parser->implicit_extern_c = true;
11053 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11055 pop_lang_context ();
11056 parser->implicit_extern_c = false;
11059 if (token->type == CPP_PRAGMA)
11061 /* A top-level declaration can consist solely of a #pragma.
11062 A nested declaration cannot, so this is done here and not
11063 in cp_parser_declaration. (A #pragma at block scope is
11064 handled in cp_parser_statement.) */
11065 cp_parser_pragma (parser, pragma_external);
11066 continue;
11069 /* Parse the declaration itself. */
11070 cp_parser_declaration (parser);
11074 /* Parse a declaration.
11076 declaration:
11077 block-declaration
11078 function-definition
11079 template-declaration
11080 explicit-instantiation
11081 explicit-specialization
11082 linkage-specification
11083 namespace-definition
11085 GNU extension:
11087 declaration:
11088 __extension__ declaration */
11090 static void
11091 cp_parser_declaration (cp_parser* parser)
11093 cp_token token1;
11094 cp_token token2;
11095 int saved_pedantic;
11096 void *p;
11097 tree attributes = NULL_TREE;
11099 /* Check for the `__extension__' keyword. */
11100 if (cp_parser_extension_opt (parser, &saved_pedantic))
11102 /* Parse the qualified declaration. */
11103 cp_parser_declaration (parser);
11104 /* Restore the PEDANTIC flag. */
11105 pedantic = saved_pedantic;
11107 return;
11110 /* Try to figure out what kind of declaration is present. */
11111 token1 = *cp_lexer_peek_token (parser->lexer);
11113 if (token1.type != CPP_EOF)
11114 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11115 else
11117 token2.type = CPP_EOF;
11118 token2.keyword = RID_MAX;
11121 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11122 p = obstack_alloc (&declarator_obstack, 0);
11124 /* If the next token is `extern' and the following token is a string
11125 literal, then we have a linkage specification. */
11126 if (token1.keyword == RID_EXTERN
11127 && cp_parser_is_pure_string_literal (&token2))
11128 cp_parser_linkage_specification (parser);
11129 /* If the next token is `template', then we have either a template
11130 declaration, an explicit instantiation, or an explicit
11131 specialization. */
11132 else if (token1.keyword == RID_TEMPLATE)
11134 /* `template <>' indicates a template specialization. */
11135 if (token2.type == CPP_LESS
11136 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11137 cp_parser_explicit_specialization (parser);
11138 /* `template <' indicates a template declaration. */
11139 else if (token2.type == CPP_LESS)
11140 cp_parser_template_declaration (parser, /*member_p=*/false);
11141 /* Anything else must be an explicit instantiation. */
11142 else
11143 cp_parser_explicit_instantiation (parser);
11145 /* If the next token is `export', then we have a template
11146 declaration. */
11147 else if (token1.keyword == RID_EXPORT)
11148 cp_parser_template_declaration (parser, /*member_p=*/false);
11149 /* If the next token is `extern', 'static' or 'inline' and the one
11150 after that is `template', we have a GNU extended explicit
11151 instantiation directive. */
11152 else if (cp_parser_allow_gnu_extensions_p (parser)
11153 && (token1.keyword == RID_EXTERN
11154 || token1.keyword == RID_STATIC
11155 || token1.keyword == RID_INLINE)
11156 && token2.keyword == RID_TEMPLATE)
11157 cp_parser_explicit_instantiation (parser);
11158 /* If the next token is `namespace', check for a named or unnamed
11159 namespace definition. */
11160 else if (token1.keyword == RID_NAMESPACE
11161 && (/* A named namespace definition. */
11162 (token2.type == CPP_NAME
11163 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11164 != CPP_EQ))
11165 /* An unnamed namespace definition. */
11166 || token2.type == CPP_OPEN_BRACE
11167 || token2.keyword == RID_ATTRIBUTE))
11168 cp_parser_namespace_definition (parser);
11169 /* An inline (associated) namespace definition. */
11170 else if (token1.keyword == RID_INLINE
11171 && token2.keyword == RID_NAMESPACE)
11172 cp_parser_namespace_definition (parser);
11173 /* Objective-C++ declaration/definition. */
11174 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11175 cp_parser_objc_declaration (parser, NULL_TREE);
11176 else if (c_dialect_objc ()
11177 && token1.keyword == RID_ATTRIBUTE
11178 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11179 cp_parser_objc_declaration (parser, attributes);
11180 /* We must have either a block declaration or a function
11181 definition. */
11182 else
11183 /* Try to parse a block-declaration, or a function-definition. */
11184 cp_parser_block_declaration (parser, /*statement_p=*/false);
11186 /* Free any declarators allocated. */
11187 obstack_free (&declarator_obstack, p);
11190 /* Parse a block-declaration.
11192 block-declaration:
11193 simple-declaration
11194 asm-definition
11195 namespace-alias-definition
11196 using-declaration
11197 using-directive
11199 GNU Extension:
11201 block-declaration:
11202 __extension__ block-declaration
11204 C++0x Extension:
11206 block-declaration:
11207 static_assert-declaration
11209 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11210 part of a declaration-statement. */
11212 static void
11213 cp_parser_block_declaration (cp_parser *parser,
11214 bool statement_p)
11216 cp_token *token1;
11217 int saved_pedantic;
11219 /* Check for the `__extension__' keyword. */
11220 if (cp_parser_extension_opt (parser, &saved_pedantic))
11222 /* Parse the qualified declaration. */
11223 cp_parser_block_declaration (parser, statement_p);
11224 /* Restore the PEDANTIC flag. */
11225 pedantic = saved_pedantic;
11227 return;
11230 /* Peek at the next token to figure out which kind of declaration is
11231 present. */
11232 token1 = cp_lexer_peek_token (parser->lexer);
11234 /* If the next keyword is `asm', we have an asm-definition. */
11235 if (token1->keyword == RID_ASM)
11237 if (statement_p)
11238 cp_parser_commit_to_tentative_parse (parser);
11239 cp_parser_asm_definition (parser);
11241 /* If the next keyword is `namespace', we have a
11242 namespace-alias-definition. */
11243 else if (token1->keyword == RID_NAMESPACE)
11244 cp_parser_namespace_alias_definition (parser);
11245 /* If the next keyword is `using', we have a
11246 using-declaration, a using-directive, or an alias-declaration. */
11247 else if (token1->keyword == RID_USING)
11249 cp_token *token2;
11251 if (statement_p)
11252 cp_parser_commit_to_tentative_parse (parser);
11253 /* If the token after `using' is `namespace', then we have a
11254 using-directive. */
11255 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11256 if (token2->keyword == RID_NAMESPACE)
11257 cp_parser_using_directive (parser);
11258 /* If the second token after 'using' is '=', then we have an
11259 alias-declaration. */
11260 else if (cxx_dialect >= cxx11
11261 && token2->type == CPP_NAME
11262 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11263 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11264 cp_parser_alias_declaration (parser);
11265 /* Otherwise, it's a using-declaration. */
11266 else
11267 cp_parser_using_declaration (parser,
11268 /*access_declaration_p=*/false);
11270 /* If the next keyword is `__label__' we have a misplaced label
11271 declaration. */
11272 else if (token1->keyword == RID_LABEL)
11274 cp_lexer_consume_token (parser->lexer);
11275 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11276 cp_parser_skip_to_end_of_statement (parser);
11277 /* If the next token is now a `;', consume it. */
11278 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11279 cp_lexer_consume_token (parser->lexer);
11281 /* If the next token is `static_assert' we have a static assertion. */
11282 else if (token1->keyword == RID_STATIC_ASSERT)
11283 cp_parser_static_assert (parser, /*member_p=*/false);
11284 /* Anything else must be a simple-declaration. */
11285 else
11286 cp_parser_simple_declaration (parser, !statement_p,
11287 /*maybe_range_for_decl*/NULL);
11290 /* Parse a simple-declaration.
11292 simple-declaration:
11293 decl-specifier-seq [opt] init-declarator-list [opt] ;
11295 init-declarator-list:
11296 init-declarator
11297 init-declarator-list , init-declarator
11299 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11300 function-definition as a simple-declaration.
11302 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11303 parsed declaration if it is an uninitialized single declarator not followed
11304 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11305 if present, will not be consumed. */
11307 static void
11308 cp_parser_simple_declaration (cp_parser* parser,
11309 bool function_definition_allowed_p,
11310 tree *maybe_range_for_decl)
11312 cp_decl_specifier_seq decl_specifiers;
11313 int declares_class_or_enum;
11314 bool saw_declarator;
11316 if (maybe_range_for_decl)
11317 *maybe_range_for_decl = NULL_TREE;
11319 /* Defer access checks until we know what is being declared; the
11320 checks for names appearing in the decl-specifier-seq should be
11321 done as if we were in the scope of the thing being declared. */
11322 push_deferring_access_checks (dk_deferred);
11324 /* Parse the decl-specifier-seq. We have to keep track of whether
11325 or not the decl-specifier-seq declares a named class or
11326 enumeration type, since that is the only case in which the
11327 init-declarator-list is allowed to be empty.
11329 [dcl.dcl]
11331 In a simple-declaration, the optional init-declarator-list can be
11332 omitted only when declaring a class or enumeration, that is when
11333 the decl-specifier-seq contains either a class-specifier, an
11334 elaborated-type-specifier, or an enum-specifier. */
11335 cp_parser_decl_specifier_seq (parser,
11336 CP_PARSER_FLAGS_OPTIONAL,
11337 &decl_specifiers,
11338 &declares_class_or_enum);
11339 /* We no longer need to defer access checks. */
11340 stop_deferring_access_checks ();
11342 /* In a block scope, a valid declaration must always have a
11343 decl-specifier-seq. By not trying to parse declarators, we can
11344 resolve the declaration/expression ambiguity more quickly. */
11345 if (!function_definition_allowed_p
11346 && !decl_specifiers.any_specifiers_p)
11348 cp_parser_error (parser, "expected declaration");
11349 goto done;
11352 /* If the next two tokens are both identifiers, the code is
11353 erroneous. The usual cause of this situation is code like:
11355 T t;
11357 where "T" should name a type -- but does not. */
11358 if (!decl_specifiers.any_type_specifiers_p
11359 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11361 /* If parsing tentatively, we should commit; we really are
11362 looking at a declaration. */
11363 cp_parser_commit_to_tentative_parse (parser);
11364 /* Give up. */
11365 goto done;
11368 /* If we have seen at least one decl-specifier, and the next token
11369 is not a parenthesis, then we must be looking at a declaration.
11370 (After "int (" we might be looking at a functional cast.) */
11371 if (decl_specifiers.any_specifiers_p
11372 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11373 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11374 && !cp_parser_error_occurred (parser))
11375 cp_parser_commit_to_tentative_parse (parser);
11377 /* Keep going until we hit the `;' at the end of the simple
11378 declaration. */
11379 saw_declarator = false;
11380 while (cp_lexer_next_token_is_not (parser->lexer,
11381 CPP_SEMICOLON))
11383 cp_token *token;
11384 bool function_definition_p;
11385 tree decl;
11387 if (saw_declarator)
11389 /* If we are processing next declarator, coma is expected */
11390 token = cp_lexer_peek_token (parser->lexer);
11391 gcc_assert (token->type == CPP_COMMA);
11392 cp_lexer_consume_token (parser->lexer);
11393 if (maybe_range_for_decl)
11394 *maybe_range_for_decl = error_mark_node;
11396 else
11397 saw_declarator = true;
11399 /* Parse the init-declarator. */
11400 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11401 /*checks=*/NULL,
11402 function_definition_allowed_p,
11403 /*member_p=*/false,
11404 declares_class_or_enum,
11405 &function_definition_p,
11406 maybe_range_for_decl);
11407 /* If an error occurred while parsing tentatively, exit quickly.
11408 (That usually happens when in the body of a function; each
11409 statement is treated as a declaration-statement until proven
11410 otherwise.) */
11411 if (cp_parser_error_occurred (parser))
11412 goto done;
11413 /* Handle function definitions specially. */
11414 if (function_definition_p)
11416 /* If the next token is a `,', then we are probably
11417 processing something like:
11419 void f() {}, *p;
11421 which is erroneous. */
11422 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11424 cp_token *token = cp_lexer_peek_token (parser->lexer);
11425 error_at (token->location,
11426 "mixing"
11427 " declarations and function-definitions is forbidden");
11429 /* Otherwise, we're done with the list of declarators. */
11430 else
11432 pop_deferring_access_checks ();
11433 return;
11436 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11437 *maybe_range_for_decl = decl;
11438 /* The next token should be either a `,' or a `;'. */
11439 token = cp_lexer_peek_token (parser->lexer);
11440 /* If it's a `,', there are more declarators to come. */
11441 if (token->type == CPP_COMMA)
11442 /* will be consumed next time around */;
11443 /* If it's a `;', we are done. */
11444 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11445 break;
11446 /* Anything else is an error. */
11447 else
11449 /* If we have already issued an error message we don't need
11450 to issue another one. */
11451 if (decl != error_mark_node
11452 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11453 cp_parser_error (parser, "expected %<,%> or %<;%>");
11454 /* Skip tokens until we reach the end of the statement. */
11455 cp_parser_skip_to_end_of_statement (parser);
11456 /* If the next token is now a `;', consume it. */
11457 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11458 cp_lexer_consume_token (parser->lexer);
11459 goto done;
11461 /* After the first time around, a function-definition is not
11462 allowed -- even if it was OK at first. For example:
11464 int i, f() {}
11466 is not valid. */
11467 function_definition_allowed_p = false;
11470 /* Issue an error message if no declarators are present, and the
11471 decl-specifier-seq does not itself declare a class or
11472 enumeration: [dcl.dcl]/3. */
11473 if (!saw_declarator)
11475 if (cp_parser_declares_only_class_p (parser))
11477 if (!declares_class_or_enum
11478 && decl_specifiers.type
11479 && OVERLOAD_TYPE_P (decl_specifiers.type))
11480 /* Ensure an error is issued anyway when finish_decltype_type,
11481 called via cp_parser_decl_specifier_seq, returns a class or
11482 an enumeration (c++/51786). */
11483 decl_specifiers.type = NULL_TREE;
11484 shadow_tag (&decl_specifiers);
11486 /* Perform any deferred access checks. */
11487 perform_deferred_access_checks (tf_warning_or_error);
11490 /* Consume the `;'. */
11491 if (!maybe_range_for_decl)
11492 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11494 done:
11495 pop_deferring_access_checks ();
11498 /* Parse a decl-specifier-seq.
11500 decl-specifier-seq:
11501 decl-specifier-seq [opt] decl-specifier
11502 decl-specifier attribute-specifier-seq [opt] (C++11)
11504 decl-specifier:
11505 storage-class-specifier
11506 type-specifier
11507 function-specifier
11508 friend
11509 typedef
11511 GNU Extension:
11513 decl-specifier:
11514 attributes
11516 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11518 The parser flags FLAGS is used to control type-specifier parsing.
11520 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11521 flags:
11523 1: one of the decl-specifiers is an elaborated-type-specifier
11524 (i.e., a type declaration)
11525 2: one of the decl-specifiers is an enum-specifier or a
11526 class-specifier (i.e., a type definition)
11530 static void
11531 cp_parser_decl_specifier_seq (cp_parser* parser,
11532 cp_parser_flags flags,
11533 cp_decl_specifier_seq *decl_specs,
11534 int* declares_class_or_enum)
11536 bool constructor_possible_p = !parser->in_declarator_p;
11537 bool found_decl_spec = false;
11538 cp_token *start_token = NULL;
11539 cp_decl_spec ds;
11541 /* Clear DECL_SPECS. */
11542 clear_decl_specs (decl_specs);
11544 /* Assume no class or enumeration type is declared. */
11545 *declares_class_or_enum = 0;
11547 /* Keep reading specifiers until there are no more to read. */
11548 while (true)
11550 bool constructor_p;
11551 cp_token *token;
11552 ds = ds_last;
11554 /* Peek at the next token. */
11555 token = cp_lexer_peek_token (parser->lexer);
11557 /* Save the first token of the decl spec list for error
11558 reporting. */
11559 if (!start_token)
11560 start_token = token;
11561 /* Handle attributes. */
11562 if (cp_next_tokens_can_be_attribute_p (parser))
11564 /* Parse the attributes. */
11565 tree attrs = cp_parser_attributes_opt (parser);
11567 /* In a sequence of declaration specifiers, c++11 attributes
11568 appertain to the type that precede them. In that case
11569 [dcl.spec]/1 says:
11571 The attribute-specifier-seq affects the type only for
11572 the declaration it appears in, not other declarations
11573 involving the same type.
11575 But for now let's force the user to position the
11576 attribute either at the beginning of the declaration or
11577 after the declarator-id, which would clearly mean that it
11578 applies to the declarator. */
11579 if (cxx11_attribute_p (attrs))
11581 if (!found_decl_spec)
11582 /* The c++11 attribute is at the beginning of the
11583 declaration. It appertains to the entity being
11584 declared. */;
11585 else
11587 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11589 /* This is an attribute following a
11590 class-specifier. */
11591 if (decl_specs->type_definition_p)
11592 warn_misplaced_attr_for_class_type (token->location,
11593 decl_specs->type);
11594 attrs = NULL_TREE;
11596 else
11598 decl_specs->std_attributes
11599 = chainon (decl_specs->std_attributes,
11600 attrs);
11601 if (decl_specs->locations[ds_std_attribute] == 0)
11602 decl_specs->locations[ds_std_attribute] = token->location;
11604 continue;
11608 decl_specs->attributes
11609 = chainon (decl_specs->attributes,
11610 attrs);
11611 if (decl_specs->locations[ds_attribute] == 0)
11612 decl_specs->locations[ds_attribute] = token->location;
11613 continue;
11615 /* Assume we will find a decl-specifier keyword. */
11616 found_decl_spec = true;
11617 /* If the next token is an appropriate keyword, we can simply
11618 add it to the list. */
11619 switch (token->keyword)
11621 /* decl-specifier:
11622 friend
11623 constexpr */
11624 case RID_FRIEND:
11625 if (!at_class_scope_p ())
11627 error_at (token->location, "%<friend%> used outside of class");
11628 cp_lexer_purge_token (parser->lexer);
11630 else
11632 ds = ds_friend;
11633 /* Consume the token. */
11634 cp_lexer_consume_token (parser->lexer);
11636 break;
11638 case RID_CONSTEXPR:
11639 ds = ds_constexpr;
11640 cp_lexer_consume_token (parser->lexer);
11641 break;
11643 /* function-specifier:
11644 inline
11645 virtual
11646 explicit */
11647 case RID_INLINE:
11648 case RID_VIRTUAL:
11649 case RID_EXPLICIT:
11650 cp_parser_function_specifier_opt (parser, decl_specs);
11651 break;
11653 /* decl-specifier:
11654 typedef */
11655 case RID_TYPEDEF:
11656 ds = ds_typedef;
11657 /* Consume the token. */
11658 cp_lexer_consume_token (parser->lexer);
11659 /* A constructor declarator cannot appear in a typedef. */
11660 constructor_possible_p = false;
11661 /* The "typedef" keyword can only occur in a declaration; we
11662 may as well commit at this point. */
11663 cp_parser_commit_to_tentative_parse (parser);
11665 if (decl_specs->storage_class != sc_none)
11666 decl_specs->conflicting_specifiers_p = true;
11667 break;
11669 /* storage-class-specifier:
11670 auto
11671 register
11672 static
11673 extern
11674 mutable
11676 GNU Extension:
11677 thread */
11678 case RID_AUTO:
11679 if (cxx_dialect == cxx98)
11681 /* Consume the token. */
11682 cp_lexer_consume_token (parser->lexer);
11684 /* Complain about `auto' as a storage specifier, if
11685 we're complaining about C++0x compatibility. */
11686 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11687 " changes meaning in C++11; please remove it");
11689 /* Set the storage class anyway. */
11690 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11691 token);
11693 else
11694 /* C++0x auto type-specifier. */
11695 found_decl_spec = false;
11696 break;
11698 case RID_REGISTER:
11699 case RID_STATIC:
11700 case RID_EXTERN:
11701 case RID_MUTABLE:
11702 /* Consume the token. */
11703 cp_lexer_consume_token (parser->lexer);
11704 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11705 token);
11706 break;
11707 case RID_THREAD:
11708 /* Consume the token. */
11709 ds = ds_thread;
11710 cp_lexer_consume_token (parser->lexer);
11711 break;
11713 default:
11714 /* We did not yet find a decl-specifier yet. */
11715 found_decl_spec = false;
11716 break;
11719 if (found_decl_spec
11720 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11721 && token->keyword != RID_CONSTEXPR)
11722 error ("decl-specifier invalid in condition");
11724 if (ds != ds_last)
11725 set_and_check_decl_spec_loc (decl_specs, ds, token);
11727 /* Constructors are a special case. The `S' in `S()' is not a
11728 decl-specifier; it is the beginning of the declarator. */
11729 constructor_p
11730 = (!found_decl_spec
11731 && constructor_possible_p
11732 && (cp_parser_constructor_declarator_p
11733 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11735 /* If we don't have a DECL_SPEC yet, then we must be looking at
11736 a type-specifier. */
11737 if (!found_decl_spec && !constructor_p)
11739 int decl_spec_declares_class_or_enum;
11740 bool is_cv_qualifier;
11741 tree type_spec;
11743 type_spec
11744 = cp_parser_type_specifier (parser, flags,
11745 decl_specs,
11746 /*is_declaration=*/true,
11747 &decl_spec_declares_class_or_enum,
11748 &is_cv_qualifier);
11749 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11751 /* If this type-specifier referenced a user-defined type
11752 (a typedef, class-name, etc.), then we can't allow any
11753 more such type-specifiers henceforth.
11755 [dcl.spec]
11757 The longest sequence of decl-specifiers that could
11758 possibly be a type name is taken as the
11759 decl-specifier-seq of a declaration. The sequence shall
11760 be self-consistent as described below.
11762 [dcl.type]
11764 As a general rule, at most one type-specifier is allowed
11765 in the complete decl-specifier-seq of a declaration. The
11766 only exceptions are the following:
11768 -- const or volatile can be combined with any other
11769 type-specifier.
11771 -- signed or unsigned can be combined with char, long,
11772 short, or int.
11774 -- ..
11776 Example:
11778 typedef char* Pc;
11779 void g (const int Pc);
11781 Here, Pc is *not* part of the decl-specifier seq; it's
11782 the declarator. Therefore, once we see a type-specifier
11783 (other than a cv-qualifier), we forbid any additional
11784 user-defined types. We *do* still allow things like `int
11785 int' to be considered a decl-specifier-seq, and issue the
11786 error message later. */
11787 if (type_spec && !is_cv_qualifier)
11788 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11789 /* A constructor declarator cannot follow a type-specifier. */
11790 if (type_spec)
11792 constructor_possible_p = false;
11793 found_decl_spec = true;
11794 if (!is_cv_qualifier)
11795 decl_specs->any_type_specifiers_p = true;
11799 /* If we still do not have a DECL_SPEC, then there are no more
11800 decl-specifiers. */
11801 if (!found_decl_spec)
11802 break;
11804 decl_specs->any_specifiers_p = true;
11805 /* After we see one decl-specifier, further decl-specifiers are
11806 always optional. */
11807 flags |= CP_PARSER_FLAGS_OPTIONAL;
11810 /* Don't allow a friend specifier with a class definition. */
11811 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11812 && (*declares_class_or_enum & 2))
11813 error_at (decl_specs->locations[ds_friend],
11814 "class definition may not be declared a friend");
11817 /* Parse an (optional) storage-class-specifier.
11819 storage-class-specifier:
11820 auto
11821 register
11822 static
11823 extern
11824 mutable
11826 GNU Extension:
11828 storage-class-specifier:
11829 thread
11831 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11833 static tree
11834 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11836 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11838 case RID_AUTO:
11839 if (cxx_dialect != cxx98)
11840 return NULL_TREE;
11841 /* Fall through for C++98. */
11843 case RID_REGISTER:
11844 case RID_STATIC:
11845 case RID_EXTERN:
11846 case RID_MUTABLE:
11847 case RID_THREAD:
11848 /* Consume the token. */
11849 return cp_lexer_consume_token (parser->lexer)->u.value;
11851 default:
11852 return NULL_TREE;
11856 /* Parse an (optional) function-specifier.
11858 function-specifier:
11859 inline
11860 virtual
11861 explicit
11863 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11864 Updates DECL_SPECS, if it is non-NULL. */
11866 static tree
11867 cp_parser_function_specifier_opt (cp_parser* parser,
11868 cp_decl_specifier_seq *decl_specs)
11870 cp_token *token = cp_lexer_peek_token (parser->lexer);
11871 switch (token->keyword)
11873 case RID_INLINE:
11874 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11875 break;
11877 case RID_VIRTUAL:
11878 /* 14.5.2.3 [temp.mem]
11880 A member function template shall not be virtual. */
11881 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11882 error_at (token->location, "templates may not be %<virtual%>");
11883 else
11884 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11885 break;
11887 case RID_EXPLICIT:
11888 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11889 break;
11891 default:
11892 return NULL_TREE;
11895 /* Consume the token. */
11896 return cp_lexer_consume_token (parser->lexer)->u.value;
11899 /* Parse a linkage-specification.
11901 linkage-specification:
11902 extern string-literal { declaration-seq [opt] }
11903 extern string-literal declaration */
11905 static void
11906 cp_parser_linkage_specification (cp_parser* parser)
11908 tree linkage;
11910 /* Look for the `extern' keyword. */
11911 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11913 /* Look for the string-literal. */
11914 linkage = cp_parser_string_literal (parser, false, false);
11916 /* Transform the literal into an identifier. If the literal is a
11917 wide-character string, or contains embedded NULs, then we can't
11918 handle it as the user wants. */
11919 if (strlen (TREE_STRING_POINTER (linkage))
11920 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11922 cp_parser_error (parser, "invalid linkage-specification");
11923 /* Assume C++ linkage. */
11924 linkage = lang_name_cplusplus;
11926 else
11927 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11929 /* We're now using the new linkage. */
11930 push_lang_context (linkage);
11932 /* If the next token is a `{', then we're using the first
11933 production. */
11934 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11936 cp_ensure_no_omp_declare_simd (parser);
11938 /* Consume the `{' token. */
11939 cp_lexer_consume_token (parser->lexer);
11940 /* Parse the declarations. */
11941 cp_parser_declaration_seq_opt (parser);
11942 /* Look for the closing `}'. */
11943 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11945 /* Otherwise, there's just one declaration. */
11946 else
11948 bool saved_in_unbraced_linkage_specification_p;
11950 saved_in_unbraced_linkage_specification_p
11951 = parser->in_unbraced_linkage_specification_p;
11952 parser->in_unbraced_linkage_specification_p = true;
11953 cp_parser_declaration (parser);
11954 parser->in_unbraced_linkage_specification_p
11955 = saved_in_unbraced_linkage_specification_p;
11958 /* We're done with the linkage-specification. */
11959 pop_lang_context ();
11962 /* Parse a static_assert-declaration.
11964 static_assert-declaration:
11965 static_assert ( constant-expression , string-literal ) ;
11967 If MEMBER_P, this static_assert is a class member. */
11969 static void
11970 cp_parser_static_assert(cp_parser *parser, bool member_p)
11972 tree condition;
11973 tree message;
11974 cp_token *token;
11975 location_t saved_loc;
11976 bool dummy;
11978 /* Peek at the `static_assert' token so we can keep track of exactly
11979 where the static assertion started. */
11980 token = cp_lexer_peek_token (parser->lexer);
11981 saved_loc = token->location;
11983 /* Look for the `static_assert' keyword. */
11984 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11985 RT_STATIC_ASSERT))
11986 return;
11988 /* We know we are in a static assertion; commit to any tentative
11989 parse. */
11990 if (cp_parser_parsing_tentatively (parser))
11991 cp_parser_commit_to_tentative_parse (parser);
11993 /* Parse the `(' starting the static assertion condition. */
11994 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11996 /* Parse the constant-expression. Allow a non-constant expression
11997 here in order to give better diagnostics in finish_static_assert. */
11998 condition =
11999 cp_parser_constant_expression (parser,
12000 /*allow_non_constant_p=*/true,
12001 /*non_constant_p=*/&dummy);
12003 /* Parse the separating `,'. */
12004 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12006 /* Parse the string-literal message. */
12007 message = cp_parser_string_literal (parser,
12008 /*translate=*/false,
12009 /*wide_ok=*/true);
12011 /* A `)' completes the static assertion. */
12012 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12013 cp_parser_skip_to_closing_parenthesis (parser,
12014 /*recovering=*/true,
12015 /*or_comma=*/false,
12016 /*consume_paren=*/true);
12018 /* A semicolon terminates the declaration. */
12019 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12021 /* Complete the static assertion, which may mean either processing
12022 the static assert now or saving it for template instantiation. */
12023 finish_static_assert (condition, message, saved_loc, member_p);
12026 /* Parse the expression in decltype ( expression ). */
12028 static tree
12029 cp_parser_decltype_expr (cp_parser *parser,
12030 bool &id_expression_or_member_access_p)
12032 cp_token *id_expr_start_token;
12033 tree expr;
12035 /* First, try parsing an id-expression. */
12036 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12037 cp_parser_parse_tentatively (parser);
12038 expr = cp_parser_id_expression (parser,
12039 /*template_keyword_p=*/false,
12040 /*check_dependency_p=*/true,
12041 /*template_p=*/NULL,
12042 /*declarator_p=*/false,
12043 /*optional_p=*/false);
12045 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12047 bool non_integral_constant_expression_p = false;
12048 tree id_expression = expr;
12049 cp_id_kind idk;
12050 const char *error_msg;
12052 if (identifier_p (expr))
12053 /* Lookup the name we got back from the id-expression. */
12054 expr = cp_parser_lookup_name_simple (parser, expr,
12055 id_expr_start_token->location);
12057 if (expr
12058 && expr != error_mark_node
12059 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12060 && TREE_CODE (expr) != TYPE_DECL
12061 && (TREE_CODE (expr) != BIT_NOT_EXPR
12062 || !TYPE_P (TREE_OPERAND (expr, 0)))
12063 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12065 /* Complete lookup of the id-expression. */
12066 expr = (finish_id_expression
12067 (id_expression, expr, parser->scope, &idk,
12068 /*integral_constant_expression_p=*/false,
12069 /*allow_non_integral_constant_expression_p=*/true,
12070 &non_integral_constant_expression_p,
12071 /*template_p=*/false,
12072 /*done=*/true,
12073 /*address_p=*/false,
12074 /*template_arg_p=*/false,
12075 &error_msg,
12076 id_expr_start_token->location));
12078 if (expr == error_mark_node)
12079 /* We found an id-expression, but it was something that we
12080 should not have found. This is an error, not something
12081 we can recover from, so note that we found an
12082 id-expression and we'll recover as gracefully as
12083 possible. */
12084 id_expression_or_member_access_p = true;
12087 if (expr
12088 && expr != error_mark_node
12089 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12090 /* We have an id-expression. */
12091 id_expression_or_member_access_p = true;
12094 if (!id_expression_or_member_access_p)
12096 /* Abort the id-expression parse. */
12097 cp_parser_abort_tentative_parse (parser);
12099 /* Parsing tentatively, again. */
12100 cp_parser_parse_tentatively (parser);
12102 /* Parse a class member access. */
12103 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12104 /*cast_p=*/false, /*decltype*/true,
12105 /*member_access_only_p=*/true, NULL);
12107 if (expr
12108 && expr != error_mark_node
12109 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12110 /* We have an id-expression. */
12111 id_expression_or_member_access_p = true;
12114 if (id_expression_or_member_access_p)
12115 /* We have parsed the complete id-expression or member access. */
12116 cp_parser_parse_definitely (parser);
12117 else
12119 /* Abort our attempt to parse an id-expression or member access
12120 expression. */
12121 cp_parser_abort_tentative_parse (parser);
12123 /* Parse a full expression. */
12124 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12125 /*decltype_p=*/true);
12128 return expr;
12131 /* Parse a `decltype' type. Returns the type.
12133 simple-type-specifier:
12134 decltype ( expression )
12135 C++14 proposal:
12136 decltype ( auto ) */
12138 static tree
12139 cp_parser_decltype (cp_parser *parser)
12141 tree expr;
12142 bool id_expression_or_member_access_p = false;
12143 const char *saved_message;
12144 bool saved_integral_constant_expression_p;
12145 bool saved_non_integral_constant_expression_p;
12146 bool saved_greater_than_is_operator_p;
12147 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12149 if (start_token->type == CPP_DECLTYPE)
12151 /* Already parsed. */
12152 cp_lexer_consume_token (parser->lexer);
12153 return start_token->u.value;
12156 /* Look for the `decltype' token. */
12157 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12158 return error_mark_node;
12160 /* Parse the opening `('. */
12161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12162 return error_mark_node;
12164 /* decltype (auto) */
12165 if (cxx_dialect >= cxx14
12166 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12168 cp_lexer_consume_token (parser->lexer);
12169 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12170 return error_mark_node;
12171 expr = make_decltype_auto ();
12172 AUTO_IS_DECLTYPE (expr) = true;
12173 goto rewrite;
12176 /* Types cannot be defined in a `decltype' expression. Save away the
12177 old message. */
12178 saved_message = parser->type_definition_forbidden_message;
12180 /* And create the new one. */
12181 parser->type_definition_forbidden_message
12182 = G_("types may not be defined in %<decltype%> expressions");
12184 /* The restrictions on constant-expressions do not apply inside
12185 decltype expressions. */
12186 saved_integral_constant_expression_p
12187 = parser->integral_constant_expression_p;
12188 saved_non_integral_constant_expression_p
12189 = parser->non_integral_constant_expression_p;
12190 parser->integral_constant_expression_p = false;
12192 /* Within a parenthesized expression, a `>' token is always
12193 the greater-than operator. */
12194 saved_greater_than_is_operator_p
12195 = parser->greater_than_is_operator_p;
12196 parser->greater_than_is_operator_p = true;
12198 /* Do not actually evaluate the expression. */
12199 ++cp_unevaluated_operand;
12201 /* Do not warn about problems with the expression. */
12202 ++c_inhibit_evaluation_warnings;
12204 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12206 /* Go back to evaluating expressions. */
12207 --cp_unevaluated_operand;
12208 --c_inhibit_evaluation_warnings;
12210 /* The `>' token might be the end of a template-id or
12211 template-parameter-list now. */
12212 parser->greater_than_is_operator_p
12213 = saved_greater_than_is_operator_p;
12215 /* Restore the old message and the integral constant expression
12216 flags. */
12217 parser->type_definition_forbidden_message = saved_message;
12218 parser->integral_constant_expression_p
12219 = saved_integral_constant_expression_p;
12220 parser->non_integral_constant_expression_p
12221 = saved_non_integral_constant_expression_p;
12223 /* Parse to the closing `)'. */
12224 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12226 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12227 /*consume_paren=*/true);
12228 return error_mark_node;
12231 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12232 tf_warning_or_error);
12234 rewrite:
12235 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12236 it again. */
12237 start_token->type = CPP_DECLTYPE;
12238 start_token->u.value = expr;
12239 start_token->keyword = RID_MAX;
12240 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12242 return expr;
12245 /* Special member functions [gram.special] */
12247 /* Parse a conversion-function-id.
12249 conversion-function-id:
12250 operator conversion-type-id
12252 Returns an IDENTIFIER_NODE representing the operator. */
12254 static tree
12255 cp_parser_conversion_function_id (cp_parser* parser)
12257 tree type;
12258 tree saved_scope;
12259 tree saved_qualifying_scope;
12260 tree saved_object_scope;
12261 tree pushed_scope = NULL_TREE;
12263 /* Look for the `operator' token. */
12264 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12265 return error_mark_node;
12266 /* When we parse the conversion-type-id, the current scope will be
12267 reset. However, we need that information in able to look up the
12268 conversion function later, so we save it here. */
12269 saved_scope = parser->scope;
12270 saved_qualifying_scope = parser->qualifying_scope;
12271 saved_object_scope = parser->object_scope;
12272 /* We must enter the scope of the class so that the names of
12273 entities declared within the class are available in the
12274 conversion-type-id. For example, consider:
12276 struct S {
12277 typedef int I;
12278 operator I();
12281 S::operator I() { ... }
12283 In order to see that `I' is a type-name in the definition, we
12284 must be in the scope of `S'. */
12285 if (saved_scope)
12286 pushed_scope = push_scope (saved_scope);
12287 /* Parse the conversion-type-id. */
12288 type = cp_parser_conversion_type_id (parser);
12289 /* Leave the scope of the class, if any. */
12290 if (pushed_scope)
12291 pop_scope (pushed_scope);
12292 /* Restore the saved scope. */
12293 parser->scope = saved_scope;
12294 parser->qualifying_scope = saved_qualifying_scope;
12295 parser->object_scope = saved_object_scope;
12296 /* If the TYPE is invalid, indicate failure. */
12297 if (type == error_mark_node)
12298 return error_mark_node;
12299 return mangle_conv_op_name_for_type (type);
12302 /* Parse a conversion-type-id:
12304 conversion-type-id:
12305 type-specifier-seq conversion-declarator [opt]
12307 Returns the TYPE specified. */
12309 static tree
12310 cp_parser_conversion_type_id (cp_parser* parser)
12312 tree attributes;
12313 cp_decl_specifier_seq type_specifiers;
12314 cp_declarator *declarator;
12315 tree type_specified;
12316 const char *saved_message;
12318 /* Parse the attributes. */
12319 attributes = cp_parser_attributes_opt (parser);
12321 saved_message = parser->type_definition_forbidden_message;
12322 parser->type_definition_forbidden_message
12323 = G_("types may not be defined in a conversion-type-id");
12325 /* Parse the type-specifiers. */
12326 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12327 /*is_trailing_return=*/false,
12328 &type_specifiers);
12330 parser->type_definition_forbidden_message = saved_message;
12332 /* If that didn't work, stop. */
12333 if (type_specifiers.type == error_mark_node)
12334 return error_mark_node;
12335 /* Parse the conversion-declarator. */
12336 declarator = cp_parser_conversion_declarator_opt (parser);
12338 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12339 /*initialized=*/0, &attributes);
12340 if (attributes)
12341 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12343 /* Don't give this error when parsing tentatively. This happens to
12344 work because we always parse this definitively once. */
12345 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12346 && type_uses_auto (type_specified))
12348 if (cxx_dialect < cxx14)
12350 error ("invalid use of %<auto%> in conversion operator");
12351 return error_mark_node;
12353 else if (template_parm_scope_p ())
12354 warning (0, "use of %<auto%> in member template "
12355 "conversion operator can never be deduced");
12358 return type_specified;
12361 /* Parse an (optional) conversion-declarator.
12363 conversion-declarator:
12364 ptr-operator conversion-declarator [opt]
12368 static cp_declarator *
12369 cp_parser_conversion_declarator_opt (cp_parser* parser)
12371 enum tree_code code;
12372 tree class_type, std_attributes = NULL_TREE;
12373 cp_cv_quals cv_quals;
12375 /* We don't know if there's a ptr-operator next, or not. */
12376 cp_parser_parse_tentatively (parser);
12377 /* Try the ptr-operator. */
12378 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12379 &std_attributes);
12380 /* If it worked, look for more conversion-declarators. */
12381 if (cp_parser_parse_definitely (parser))
12383 cp_declarator *declarator;
12385 /* Parse another optional declarator. */
12386 declarator = cp_parser_conversion_declarator_opt (parser);
12388 declarator = cp_parser_make_indirect_declarator
12389 (code, class_type, cv_quals, declarator, std_attributes);
12391 return declarator;
12394 return NULL;
12397 /* Parse an (optional) ctor-initializer.
12399 ctor-initializer:
12400 : mem-initializer-list
12402 Returns TRUE iff the ctor-initializer was actually present. */
12404 static bool
12405 cp_parser_ctor_initializer_opt (cp_parser* parser)
12407 /* If the next token is not a `:', then there is no
12408 ctor-initializer. */
12409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12411 /* Do default initialization of any bases and members. */
12412 if (DECL_CONSTRUCTOR_P (current_function_decl))
12413 finish_mem_initializers (NULL_TREE);
12415 return false;
12418 /* Consume the `:' token. */
12419 cp_lexer_consume_token (parser->lexer);
12420 /* And the mem-initializer-list. */
12421 cp_parser_mem_initializer_list (parser);
12423 return true;
12426 /* Parse a mem-initializer-list.
12428 mem-initializer-list:
12429 mem-initializer ... [opt]
12430 mem-initializer ... [opt] , mem-initializer-list */
12432 static void
12433 cp_parser_mem_initializer_list (cp_parser* parser)
12435 tree mem_initializer_list = NULL_TREE;
12436 tree target_ctor = error_mark_node;
12437 cp_token *token = cp_lexer_peek_token (parser->lexer);
12439 /* Let the semantic analysis code know that we are starting the
12440 mem-initializer-list. */
12441 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12442 error_at (token->location,
12443 "only constructors take member initializers");
12445 /* Loop through the list. */
12446 while (true)
12448 tree mem_initializer;
12450 token = cp_lexer_peek_token (parser->lexer);
12451 /* Parse the mem-initializer. */
12452 mem_initializer = cp_parser_mem_initializer (parser);
12453 /* If the next token is a `...', we're expanding member initializers. */
12454 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12456 /* Consume the `...'. */
12457 cp_lexer_consume_token (parser->lexer);
12459 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12460 can be expanded but members cannot. */
12461 if (mem_initializer != error_mark_node
12462 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12464 error_at (token->location,
12465 "cannot expand initializer for member %<%D%>",
12466 TREE_PURPOSE (mem_initializer));
12467 mem_initializer = error_mark_node;
12470 /* Construct the pack expansion type. */
12471 if (mem_initializer != error_mark_node)
12472 mem_initializer = make_pack_expansion (mem_initializer);
12474 if (target_ctor != error_mark_node
12475 && mem_initializer != error_mark_node)
12477 error ("mem-initializer for %qD follows constructor delegation",
12478 TREE_PURPOSE (mem_initializer));
12479 mem_initializer = error_mark_node;
12481 /* Look for a target constructor. */
12482 if (mem_initializer != error_mark_node
12483 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12484 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12486 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12487 if (mem_initializer_list)
12489 error ("constructor delegation follows mem-initializer for %qD",
12490 TREE_PURPOSE (mem_initializer_list));
12491 mem_initializer = error_mark_node;
12493 target_ctor = mem_initializer;
12495 /* Add it to the list, unless it was erroneous. */
12496 if (mem_initializer != error_mark_node)
12498 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12499 mem_initializer_list = mem_initializer;
12501 /* If the next token is not a `,', we're done. */
12502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12503 break;
12504 /* Consume the `,' token. */
12505 cp_lexer_consume_token (parser->lexer);
12508 /* Perform semantic analysis. */
12509 if (DECL_CONSTRUCTOR_P (current_function_decl))
12510 finish_mem_initializers (mem_initializer_list);
12513 /* Parse a mem-initializer.
12515 mem-initializer:
12516 mem-initializer-id ( expression-list [opt] )
12517 mem-initializer-id braced-init-list
12519 GNU extension:
12521 mem-initializer:
12522 ( expression-list [opt] )
12524 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12525 class) or FIELD_DECL (for a non-static data member) to initialize;
12526 the TREE_VALUE is the expression-list. An empty initialization
12527 list is represented by void_list_node. */
12529 static tree
12530 cp_parser_mem_initializer (cp_parser* parser)
12532 tree mem_initializer_id;
12533 tree expression_list;
12534 tree member;
12535 cp_token *token = cp_lexer_peek_token (parser->lexer);
12537 /* Find out what is being initialized. */
12538 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12540 permerror (token->location,
12541 "anachronistic old-style base class initializer");
12542 mem_initializer_id = NULL_TREE;
12544 else
12546 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12547 if (mem_initializer_id == error_mark_node)
12548 return mem_initializer_id;
12550 member = expand_member_init (mem_initializer_id);
12551 if (member && !DECL_P (member))
12552 in_base_initializer = 1;
12554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12556 bool expr_non_constant_p;
12557 cp_lexer_set_source_position (parser->lexer);
12558 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12559 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12560 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12561 expression_list = build_tree_list (NULL_TREE, expression_list);
12563 else
12565 vec<tree, va_gc> *vec;
12566 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12567 /*cast_p=*/false,
12568 /*allow_expansion_p=*/true,
12569 /*non_constant_p=*/NULL);
12570 if (vec == NULL)
12571 return error_mark_node;
12572 expression_list = build_tree_list_vec (vec);
12573 release_tree_vector (vec);
12576 if (expression_list == error_mark_node)
12577 return error_mark_node;
12578 if (!expression_list)
12579 expression_list = void_type_node;
12581 in_base_initializer = 0;
12583 return member ? build_tree_list (member, expression_list) : error_mark_node;
12586 /* Parse a mem-initializer-id.
12588 mem-initializer-id:
12589 :: [opt] nested-name-specifier [opt] class-name
12590 identifier
12592 Returns a TYPE indicating the class to be initializer for the first
12593 production. Returns an IDENTIFIER_NODE indicating the data member
12594 to be initialized for the second production. */
12596 static tree
12597 cp_parser_mem_initializer_id (cp_parser* parser)
12599 bool global_scope_p;
12600 bool nested_name_specifier_p;
12601 bool template_p = false;
12602 tree id;
12604 cp_token *token = cp_lexer_peek_token (parser->lexer);
12606 /* `typename' is not allowed in this context ([temp.res]). */
12607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12609 error_at (token->location,
12610 "keyword %<typename%> not allowed in this context (a qualified "
12611 "member initializer is implicitly a type)");
12612 cp_lexer_consume_token (parser->lexer);
12614 /* Look for the optional `::' operator. */
12615 global_scope_p
12616 = (cp_parser_global_scope_opt (parser,
12617 /*current_scope_valid_p=*/false)
12618 != NULL_TREE);
12619 /* Look for the optional nested-name-specifier. The simplest way to
12620 implement:
12622 [temp.res]
12624 The keyword `typename' is not permitted in a base-specifier or
12625 mem-initializer; in these contexts a qualified name that
12626 depends on a template-parameter is implicitly assumed to be a
12627 type name.
12629 is to assume that we have seen the `typename' keyword at this
12630 point. */
12631 nested_name_specifier_p
12632 = (cp_parser_nested_name_specifier_opt (parser,
12633 /*typename_keyword_p=*/true,
12634 /*check_dependency_p=*/true,
12635 /*type_p=*/true,
12636 /*is_declaration=*/true)
12637 != NULL_TREE);
12638 if (nested_name_specifier_p)
12639 template_p = cp_parser_optional_template_keyword (parser);
12640 /* If there is a `::' operator or a nested-name-specifier, then we
12641 are definitely looking for a class-name. */
12642 if (global_scope_p || nested_name_specifier_p)
12643 return cp_parser_class_name (parser,
12644 /*typename_keyword_p=*/true,
12645 /*template_keyword_p=*/template_p,
12646 typename_type,
12647 /*check_dependency_p=*/true,
12648 /*class_head_p=*/false,
12649 /*is_declaration=*/true);
12650 /* Otherwise, we could also be looking for an ordinary identifier. */
12651 cp_parser_parse_tentatively (parser);
12652 /* Try a class-name. */
12653 id = cp_parser_class_name (parser,
12654 /*typename_keyword_p=*/true,
12655 /*template_keyword_p=*/false,
12656 none_type,
12657 /*check_dependency_p=*/true,
12658 /*class_head_p=*/false,
12659 /*is_declaration=*/true);
12660 /* If we found one, we're done. */
12661 if (cp_parser_parse_definitely (parser))
12662 return id;
12663 /* Otherwise, look for an ordinary identifier. */
12664 return cp_parser_identifier (parser);
12667 /* Overloading [gram.over] */
12669 /* Parse an operator-function-id.
12671 operator-function-id:
12672 operator operator
12674 Returns an IDENTIFIER_NODE for the operator which is a
12675 human-readable spelling of the identifier, e.g., `operator +'. */
12677 static tree
12678 cp_parser_operator_function_id (cp_parser* parser)
12680 /* Look for the `operator' keyword. */
12681 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12682 return error_mark_node;
12683 /* And then the name of the operator itself. */
12684 return cp_parser_operator (parser);
12687 /* Return an identifier node for a user-defined literal operator.
12688 The suffix identifier is chained to the operator name identifier. */
12690 static tree
12691 cp_literal_operator_id (const char* name)
12693 tree identifier;
12694 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12695 + strlen (name) + 10);
12696 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12697 identifier = get_identifier (buffer);
12699 return identifier;
12702 /* Parse an operator.
12704 operator:
12705 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12706 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12707 || ++ -- , ->* -> () []
12709 GNU Extensions:
12711 operator:
12712 <? >? <?= >?=
12714 Returns an IDENTIFIER_NODE for the operator which is a
12715 human-readable spelling of the identifier, e.g., `operator +'. */
12717 static tree
12718 cp_parser_operator (cp_parser* parser)
12720 tree id = NULL_TREE;
12721 cp_token *token;
12722 bool utf8 = false;
12724 /* Peek at the next token. */
12725 token = cp_lexer_peek_token (parser->lexer);
12726 /* Figure out which operator we have. */
12727 switch (token->type)
12729 case CPP_KEYWORD:
12731 enum tree_code op;
12733 /* The keyword should be either `new' or `delete'. */
12734 if (token->keyword == RID_NEW)
12735 op = NEW_EXPR;
12736 else if (token->keyword == RID_DELETE)
12737 op = DELETE_EXPR;
12738 else
12739 break;
12741 /* Consume the `new' or `delete' token. */
12742 cp_lexer_consume_token (parser->lexer);
12744 /* Peek at the next token. */
12745 token = cp_lexer_peek_token (parser->lexer);
12746 /* If it's a `[' token then this is the array variant of the
12747 operator. */
12748 if (token->type == CPP_OPEN_SQUARE)
12750 /* Consume the `[' token. */
12751 cp_lexer_consume_token (parser->lexer);
12752 /* Look for the `]' token. */
12753 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12754 id = ansi_opname (op == NEW_EXPR
12755 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12757 /* Otherwise, we have the non-array variant. */
12758 else
12759 id = ansi_opname (op);
12761 return id;
12764 case CPP_PLUS:
12765 id = ansi_opname (PLUS_EXPR);
12766 break;
12768 case CPP_MINUS:
12769 id = ansi_opname (MINUS_EXPR);
12770 break;
12772 case CPP_MULT:
12773 id = ansi_opname (MULT_EXPR);
12774 break;
12776 case CPP_DIV:
12777 id = ansi_opname (TRUNC_DIV_EXPR);
12778 break;
12780 case CPP_MOD:
12781 id = ansi_opname (TRUNC_MOD_EXPR);
12782 break;
12784 case CPP_XOR:
12785 id = ansi_opname (BIT_XOR_EXPR);
12786 break;
12788 case CPP_AND:
12789 id = ansi_opname (BIT_AND_EXPR);
12790 break;
12792 case CPP_OR:
12793 id = ansi_opname (BIT_IOR_EXPR);
12794 break;
12796 case CPP_COMPL:
12797 id = ansi_opname (BIT_NOT_EXPR);
12798 break;
12800 case CPP_NOT:
12801 id = ansi_opname (TRUTH_NOT_EXPR);
12802 break;
12804 case CPP_EQ:
12805 id = ansi_assopname (NOP_EXPR);
12806 break;
12808 case CPP_LESS:
12809 id = ansi_opname (LT_EXPR);
12810 break;
12812 case CPP_GREATER:
12813 id = ansi_opname (GT_EXPR);
12814 break;
12816 case CPP_PLUS_EQ:
12817 id = ansi_assopname (PLUS_EXPR);
12818 break;
12820 case CPP_MINUS_EQ:
12821 id = ansi_assopname (MINUS_EXPR);
12822 break;
12824 case CPP_MULT_EQ:
12825 id = ansi_assopname (MULT_EXPR);
12826 break;
12828 case CPP_DIV_EQ:
12829 id = ansi_assopname (TRUNC_DIV_EXPR);
12830 break;
12832 case CPP_MOD_EQ:
12833 id = ansi_assopname (TRUNC_MOD_EXPR);
12834 break;
12836 case CPP_XOR_EQ:
12837 id = ansi_assopname (BIT_XOR_EXPR);
12838 break;
12840 case CPP_AND_EQ:
12841 id = ansi_assopname (BIT_AND_EXPR);
12842 break;
12844 case CPP_OR_EQ:
12845 id = ansi_assopname (BIT_IOR_EXPR);
12846 break;
12848 case CPP_LSHIFT:
12849 id = ansi_opname (LSHIFT_EXPR);
12850 break;
12852 case CPP_RSHIFT:
12853 id = ansi_opname (RSHIFT_EXPR);
12854 break;
12856 case CPP_LSHIFT_EQ:
12857 id = ansi_assopname (LSHIFT_EXPR);
12858 break;
12860 case CPP_RSHIFT_EQ:
12861 id = ansi_assopname (RSHIFT_EXPR);
12862 break;
12864 case CPP_EQ_EQ:
12865 id = ansi_opname (EQ_EXPR);
12866 break;
12868 case CPP_NOT_EQ:
12869 id = ansi_opname (NE_EXPR);
12870 break;
12872 case CPP_LESS_EQ:
12873 id = ansi_opname (LE_EXPR);
12874 break;
12876 case CPP_GREATER_EQ:
12877 id = ansi_opname (GE_EXPR);
12878 break;
12880 case CPP_AND_AND:
12881 id = ansi_opname (TRUTH_ANDIF_EXPR);
12882 break;
12884 case CPP_OR_OR:
12885 id = ansi_opname (TRUTH_ORIF_EXPR);
12886 break;
12888 case CPP_PLUS_PLUS:
12889 id = ansi_opname (POSTINCREMENT_EXPR);
12890 break;
12892 case CPP_MINUS_MINUS:
12893 id = ansi_opname (PREDECREMENT_EXPR);
12894 break;
12896 case CPP_COMMA:
12897 id = ansi_opname (COMPOUND_EXPR);
12898 break;
12900 case CPP_DEREF_STAR:
12901 id = ansi_opname (MEMBER_REF);
12902 break;
12904 case CPP_DEREF:
12905 id = ansi_opname (COMPONENT_REF);
12906 break;
12908 case CPP_OPEN_PAREN:
12909 /* Consume the `('. */
12910 cp_lexer_consume_token (parser->lexer);
12911 /* Look for the matching `)'. */
12912 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12913 return ansi_opname (CALL_EXPR);
12915 case CPP_OPEN_SQUARE:
12916 /* Consume the `['. */
12917 cp_lexer_consume_token (parser->lexer);
12918 /* Look for the matching `]'. */
12919 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12920 return ansi_opname (ARRAY_REF);
12922 case CPP_UTF8STRING:
12923 case CPP_UTF8STRING_USERDEF:
12924 utf8 = true;
12925 case CPP_STRING:
12926 case CPP_WSTRING:
12927 case CPP_STRING16:
12928 case CPP_STRING32:
12929 case CPP_STRING_USERDEF:
12930 case CPP_WSTRING_USERDEF:
12931 case CPP_STRING16_USERDEF:
12932 case CPP_STRING32_USERDEF:
12934 tree str, string_tree;
12935 int sz, len;
12937 if (cxx_dialect == cxx98)
12938 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12940 /* Consume the string. */
12941 str = cp_parser_string_literal (parser, /*translate=*/true,
12942 /*wide_ok=*/true, /*lookup_udlit=*/false);
12943 if (str == error_mark_node)
12944 return error_mark_node;
12945 else if (TREE_CODE (str) == USERDEF_LITERAL)
12947 string_tree = USERDEF_LITERAL_VALUE (str);
12948 id = USERDEF_LITERAL_SUFFIX_ID (str);
12950 else
12952 string_tree = str;
12953 /* Look for the suffix identifier. */
12954 token = cp_lexer_peek_token (parser->lexer);
12955 if (token->type == CPP_NAME)
12956 id = cp_parser_identifier (parser);
12957 else if (token->type == CPP_KEYWORD)
12959 error ("unexpected keyword;"
12960 " remove space between quotes and suffix identifier");
12961 return error_mark_node;
12963 else
12965 error ("expected suffix identifier");
12966 return error_mark_node;
12969 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
12970 (TREE_TYPE (TREE_TYPE (string_tree))));
12971 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
12972 if (len != 0)
12974 error ("expected empty string after %<operator%> keyword");
12975 return error_mark_node;
12977 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
12978 != char_type_node)
12980 error ("invalid encoding prefix in literal operator");
12981 return error_mark_node;
12983 if (id != error_mark_node)
12985 const char *name = IDENTIFIER_POINTER (id);
12986 id = cp_literal_operator_id (name);
12988 return id;
12991 default:
12992 /* Anything else is an error. */
12993 break;
12996 /* If we have selected an identifier, we need to consume the
12997 operator token. */
12998 if (id)
12999 cp_lexer_consume_token (parser->lexer);
13000 /* Otherwise, no valid operator name was present. */
13001 else
13003 cp_parser_error (parser, "expected operator");
13004 id = error_mark_node;
13007 return id;
13010 /* Parse a template-declaration.
13012 template-declaration:
13013 export [opt] template < template-parameter-list > declaration
13015 If MEMBER_P is TRUE, this template-declaration occurs within a
13016 class-specifier.
13018 The grammar rule given by the standard isn't correct. What
13019 is really meant is:
13021 template-declaration:
13022 export [opt] template-parameter-list-seq
13023 decl-specifier-seq [opt] init-declarator [opt] ;
13024 export [opt] template-parameter-list-seq
13025 function-definition
13027 template-parameter-list-seq:
13028 template-parameter-list-seq [opt]
13029 template < template-parameter-list > */
13031 static void
13032 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13034 /* Check for `export'. */
13035 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13037 /* Consume the `export' token. */
13038 cp_lexer_consume_token (parser->lexer);
13039 /* Warn that we do not support `export'. */
13040 warning (0, "keyword %<export%> not implemented, and will be ignored");
13043 cp_parser_template_declaration_after_export (parser, member_p);
13046 /* Parse a template-parameter-list.
13048 template-parameter-list:
13049 template-parameter
13050 template-parameter-list , template-parameter
13052 Returns a TREE_LIST. Each node represents a template parameter.
13053 The nodes are connected via their TREE_CHAINs. */
13055 static tree
13056 cp_parser_template_parameter_list (cp_parser* parser)
13058 tree parameter_list = NULL_TREE;
13060 begin_template_parm_list ();
13062 /* The loop below parses the template parms. We first need to know
13063 the total number of template parms to be able to compute proper
13064 canonical types of each dependent type. So after the loop, when
13065 we know the total number of template parms,
13066 end_template_parm_list computes the proper canonical types and
13067 fixes up the dependent types accordingly. */
13068 while (true)
13070 tree parameter;
13071 bool is_non_type;
13072 bool is_parameter_pack;
13073 location_t parm_loc;
13075 /* Parse the template-parameter. */
13076 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13077 parameter = cp_parser_template_parameter (parser,
13078 &is_non_type,
13079 &is_parameter_pack);
13080 /* Add it to the list. */
13081 if (parameter != error_mark_node)
13082 parameter_list = process_template_parm (parameter_list,
13083 parm_loc,
13084 parameter,
13085 is_non_type,
13086 is_parameter_pack);
13087 else
13089 tree err_parm = build_tree_list (parameter, parameter);
13090 parameter_list = chainon (parameter_list, err_parm);
13093 /* If the next token is not a `,', we're done. */
13094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13095 break;
13096 /* Otherwise, consume the `,' token. */
13097 cp_lexer_consume_token (parser->lexer);
13100 return end_template_parm_list (parameter_list);
13103 /* Parse a template-parameter.
13105 template-parameter:
13106 type-parameter
13107 parameter-declaration
13109 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13110 the parameter. The TREE_PURPOSE is the default value, if any.
13111 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13112 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13113 set to true iff this parameter is a parameter pack. */
13115 static tree
13116 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13117 bool *is_parameter_pack)
13119 cp_token *token;
13120 cp_parameter_declarator *parameter_declarator;
13121 cp_declarator *id_declarator;
13122 tree parm;
13124 /* Assume it is a type parameter or a template parameter. */
13125 *is_non_type = false;
13126 /* Assume it not a parameter pack. */
13127 *is_parameter_pack = false;
13128 /* Peek at the next token. */
13129 token = cp_lexer_peek_token (parser->lexer);
13130 /* If it is `class' or `template', we have a type-parameter. */
13131 if (token->keyword == RID_TEMPLATE)
13132 return cp_parser_type_parameter (parser, is_parameter_pack);
13133 /* If it is `class' or `typename' we do not know yet whether it is a
13134 type parameter or a non-type parameter. Consider:
13136 template <typename T, typename T::X X> ...
13140 template <class C, class D*> ...
13142 Here, the first parameter is a type parameter, and the second is
13143 a non-type parameter. We can tell by looking at the token after
13144 the identifier -- if it is a `,', `=', or `>' then we have a type
13145 parameter. */
13146 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13148 /* Peek at the token after `class' or `typename'. */
13149 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13150 /* If it's an ellipsis, we have a template type parameter
13151 pack. */
13152 if (token->type == CPP_ELLIPSIS)
13153 return cp_parser_type_parameter (parser, is_parameter_pack);
13154 /* If it's an identifier, skip it. */
13155 if (token->type == CPP_NAME)
13156 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13157 /* Now, see if the token looks like the end of a template
13158 parameter. */
13159 if (token->type == CPP_COMMA
13160 || token->type == CPP_EQ
13161 || token->type == CPP_GREATER)
13162 return cp_parser_type_parameter (parser, is_parameter_pack);
13165 /* Otherwise, it is a non-type parameter.
13167 [temp.param]
13169 When parsing a default template-argument for a non-type
13170 template-parameter, the first non-nested `>' is taken as the end
13171 of the template parameter-list rather than a greater-than
13172 operator. */
13173 *is_non_type = true;
13174 parameter_declarator
13175 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13176 /*parenthesized_p=*/NULL);
13178 if (!parameter_declarator)
13179 return error_mark_node;
13181 /* If the parameter declaration is marked as a parameter pack, set
13182 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13183 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13184 grokdeclarator. */
13185 if (parameter_declarator->declarator
13186 && parameter_declarator->declarator->parameter_pack_p)
13188 *is_parameter_pack = true;
13189 parameter_declarator->declarator->parameter_pack_p = false;
13192 if (parameter_declarator->default_argument)
13194 /* Can happen in some cases of erroneous input (c++/34892). */
13195 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13196 /* Consume the `...' for better error recovery. */
13197 cp_lexer_consume_token (parser->lexer);
13199 /* If the next token is an ellipsis, and we don't already have it
13200 marked as a parameter pack, then we have a parameter pack (that
13201 has no declarator). */
13202 else if (!*is_parameter_pack
13203 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13204 && (declarator_can_be_parameter_pack
13205 (parameter_declarator->declarator)))
13207 /* Consume the `...'. */
13208 cp_lexer_consume_token (parser->lexer);
13209 maybe_warn_variadic_templates ();
13211 *is_parameter_pack = true;
13213 /* We might end up with a pack expansion as the type of the non-type
13214 template parameter, in which case this is a non-type template
13215 parameter pack. */
13216 else if (parameter_declarator->decl_specifiers.type
13217 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13219 *is_parameter_pack = true;
13220 parameter_declarator->decl_specifiers.type =
13221 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13224 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13226 /* Parameter packs cannot have default arguments. However, a
13227 user may try to do so, so we'll parse them and give an
13228 appropriate diagnostic here. */
13230 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13232 /* Find the name of the parameter pack. */
13233 id_declarator = parameter_declarator->declarator;
13234 while (id_declarator && id_declarator->kind != cdk_id)
13235 id_declarator = id_declarator->declarator;
13237 if (id_declarator && id_declarator->kind == cdk_id)
13238 error_at (start_token->location,
13239 "template parameter pack %qD cannot have a default argument",
13240 id_declarator->u.id.unqualified_name);
13241 else
13242 error_at (start_token->location,
13243 "template parameter pack cannot have a default argument");
13245 /* Parse the default argument, but throw away the result. */
13246 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13249 parm = grokdeclarator (parameter_declarator->declarator,
13250 &parameter_declarator->decl_specifiers,
13251 TPARM, /*initialized=*/0,
13252 /*attrlist=*/NULL);
13253 if (parm == error_mark_node)
13254 return error_mark_node;
13256 return build_tree_list (parameter_declarator->default_argument, parm);
13259 /* Parse a type-parameter.
13261 type-parameter:
13262 class identifier [opt]
13263 class identifier [opt] = type-id
13264 typename identifier [opt]
13265 typename identifier [opt] = type-id
13266 template < template-parameter-list > class identifier [opt]
13267 template < template-parameter-list > class identifier [opt]
13268 = id-expression
13270 GNU Extension (variadic templates):
13272 type-parameter:
13273 class ... identifier [opt]
13274 typename ... identifier [opt]
13276 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13277 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13278 the declaration of the parameter.
13280 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13282 static tree
13283 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13285 cp_token *token;
13286 tree parameter;
13288 /* Look for a keyword to tell us what kind of parameter this is. */
13289 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13290 if (!token)
13291 return error_mark_node;
13293 switch (token->keyword)
13295 case RID_CLASS:
13296 case RID_TYPENAME:
13298 tree identifier;
13299 tree default_argument;
13301 /* If the next token is an ellipsis, we have a template
13302 argument pack. */
13303 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13305 /* Consume the `...' token. */
13306 cp_lexer_consume_token (parser->lexer);
13307 maybe_warn_variadic_templates ();
13309 *is_parameter_pack = true;
13312 /* If the next token is an identifier, then it names the
13313 parameter. */
13314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13315 identifier = cp_parser_identifier (parser);
13316 else
13317 identifier = NULL_TREE;
13319 /* Create the parameter. */
13320 parameter = finish_template_type_parm (class_type_node, identifier);
13322 /* If the next token is an `=', we have a default argument. */
13323 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13325 /* Consume the `=' token. */
13326 cp_lexer_consume_token (parser->lexer);
13327 /* Parse the default-argument. */
13328 push_deferring_access_checks (dk_no_deferred);
13329 default_argument = cp_parser_type_id (parser);
13331 /* Template parameter packs cannot have default
13332 arguments. */
13333 if (*is_parameter_pack)
13335 if (identifier)
13336 error_at (token->location,
13337 "template parameter pack %qD cannot have a "
13338 "default argument", identifier);
13339 else
13340 error_at (token->location,
13341 "template parameter packs cannot have "
13342 "default arguments");
13343 default_argument = NULL_TREE;
13345 pop_deferring_access_checks ();
13347 else
13348 default_argument = NULL_TREE;
13350 /* Create the combined representation of the parameter and the
13351 default argument. */
13352 parameter = build_tree_list (default_argument, parameter);
13354 break;
13356 case RID_TEMPLATE:
13358 tree identifier;
13359 tree default_argument;
13361 /* Look for the `<'. */
13362 cp_parser_require (parser, CPP_LESS, RT_LESS);
13363 /* Parse the template-parameter-list. */
13364 cp_parser_template_parameter_list (parser);
13365 /* Look for the `>'. */
13366 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13367 /* Look for the `class' or 'typename' keywords. */
13368 cp_parser_type_parameter_key (parser);
13369 /* If the next token is an ellipsis, we have a template
13370 argument pack. */
13371 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13373 /* Consume the `...' token. */
13374 cp_lexer_consume_token (parser->lexer);
13375 maybe_warn_variadic_templates ();
13377 *is_parameter_pack = true;
13379 /* If the next token is an `=', then there is a
13380 default-argument. If the next token is a `>', we are at
13381 the end of the parameter-list. If the next token is a `,',
13382 then we are at the end of this parameter. */
13383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13384 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13385 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13387 identifier = cp_parser_identifier (parser);
13388 /* Treat invalid names as if the parameter were nameless. */
13389 if (identifier == error_mark_node)
13390 identifier = NULL_TREE;
13392 else
13393 identifier = NULL_TREE;
13395 /* Create the template parameter. */
13396 parameter = finish_template_template_parm (class_type_node,
13397 identifier);
13399 /* If the next token is an `=', then there is a
13400 default-argument. */
13401 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13403 bool is_template;
13405 /* Consume the `='. */
13406 cp_lexer_consume_token (parser->lexer);
13407 /* Parse the id-expression. */
13408 push_deferring_access_checks (dk_no_deferred);
13409 /* save token before parsing the id-expression, for error
13410 reporting */
13411 token = cp_lexer_peek_token (parser->lexer);
13412 default_argument
13413 = cp_parser_id_expression (parser,
13414 /*template_keyword_p=*/false,
13415 /*check_dependency_p=*/true,
13416 /*template_p=*/&is_template,
13417 /*declarator_p=*/false,
13418 /*optional_p=*/false);
13419 if (TREE_CODE (default_argument) == TYPE_DECL)
13420 /* If the id-expression was a template-id that refers to
13421 a template-class, we already have the declaration here,
13422 so no further lookup is needed. */
13424 else
13425 /* Look up the name. */
13426 default_argument
13427 = cp_parser_lookup_name (parser, default_argument,
13428 none_type,
13429 /*is_template=*/is_template,
13430 /*is_namespace=*/false,
13431 /*check_dependency=*/true,
13432 /*ambiguous_decls=*/NULL,
13433 token->location);
13434 /* See if the default argument is valid. */
13435 default_argument
13436 = check_template_template_default_arg (default_argument);
13438 /* Template parameter packs cannot have default
13439 arguments. */
13440 if (*is_parameter_pack)
13442 if (identifier)
13443 error_at (token->location,
13444 "template parameter pack %qD cannot "
13445 "have a default argument",
13446 identifier);
13447 else
13448 error_at (token->location, "template parameter packs cannot "
13449 "have default arguments");
13450 default_argument = NULL_TREE;
13452 pop_deferring_access_checks ();
13454 else
13455 default_argument = NULL_TREE;
13457 /* Create the combined representation of the parameter and the
13458 default argument. */
13459 parameter = build_tree_list (default_argument, parameter);
13461 break;
13463 default:
13464 gcc_unreachable ();
13465 break;
13468 return parameter;
13471 /* Parse a template-id.
13473 template-id:
13474 template-name < template-argument-list [opt] >
13476 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13477 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13478 returned. Otherwise, if the template-name names a function, or set
13479 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13480 names a class, returns a TYPE_DECL for the specialization.
13482 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13483 uninstantiated templates. */
13485 static tree
13486 cp_parser_template_id (cp_parser *parser,
13487 bool template_keyword_p,
13488 bool check_dependency_p,
13489 enum tag_types tag_type,
13490 bool is_declaration)
13492 int i;
13493 tree templ;
13494 tree arguments;
13495 tree template_id;
13496 cp_token_position start_of_id = 0;
13497 deferred_access_check *chk;
13498 vec<deferred_access_check, va_gc> *access_check;
13499 cp_token *next_token = NULL, *next_token_2 = NULL;
13500 bool is_identifier;
13502 /* If the next token corresponds to a template-id, there is no need
13503 to reparse it. */
13504 next_token = cp_lexer_peek_token (parser->lexer);
13505 if (next_token->type == CPP_TEMPLATE_ID)
13507 struct tree_check *check_value;
13509 /* Get the stored value. */
13510 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13511 /* Perform any access checks that were deferred. */
13512 access_check = check_value->checks;
13513 if (access_check)
13515 FOR_EACH_VEC_ELT (*access_check, i, chk)
13516 perform_or_defer_access_check (chk->binfo,
13517 chk->decl,
13518 chk->diag_decl,
13519 tf_warning_or_error);
13521 /* Return the stored value. */
13522 return check_value->value;
13525 /* Avoid performing name lookup if there is no possibility of
13526 finding a template-id. */
13527 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13528 || (next_token->type == CPP_NAME
13529 && !cp_parser_nth_token_starts_template_argument_list_p
13530 (parser, 2)))
13532 cp_parser_error (parser, "expected template-id");
13533 return error_mark_node;
13536 /* Remember where the template-id starts. */
13537 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13538 start_of_id = cp_lexer_token_position (parser->lexer, false);
13540 push_deferring_access_checks (dk_deferred);
13542 /* Parse the template-name. */
13543 is_identifier = false;
13544 templ = cp_parser_template_name (parser, template_keyword_p,
13545 check_dependency_p,
13546 is_declaration,
13547 tag_type,
13548 &is_identifier);
13549 if (templ == error_mark_node || is_identifier)
13551 pop_deferring_access_checks ();
13552 return templ;
13555 /* If we find the sequence `[:' after a template-name, it's probably
13556 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13557 parse correctly the argument list. */
13558 next_token = cp_lexer_peek_token (parser->lexer);
13559 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13560 if (next_token->type == CPP_OPEN_SQUARE
13561 && next_token->flags & DIGRAPH
13562 && next_token_2->type == CPP_COLON
13563 && !(next_token_2->flags & PREV_WHITE))
13565 cp_parser_parse_tentatively (parser);
13566 /* Change `:' into `::'. */
13567 next_token_2->type = CPP_SCOPE;
13568 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13569 CPP_LESS. */
13570 cp_lexer_consume_token (parser->lexer);
13572 /* Parse the arguments. */
13573 arguments = cp_parser_enclosed_template_argument_list (parser);
13574 if (!cp_parser_parse_definitely (parser))
13576 /* If we couldn't parse an argument list, then we revert our changes
13577 and return simply an error. Maybe this is not a template-id
13578 after all. */
13579 next_token_2->type = CPP_COLON;
13580 cp_parser_error (parser, "expected %<<%>");
13581 pop_deferring_access_checks ();
13582 return error_mark_node;
13584 /* Otherwise, emit an error about the invalid digraph, but continue
13585 parsing because we got our argument list. */
13586 if (permerror (next_token->location,
13587 "%<<::%> cannot begin a template-argument list"))
13589 static bool hint = false;
13590 inform (next_token->location,
13591 "%<<:%> is an alternate spelling for %<[%>."
13592 " Insert whitespace between %<<%> and %<::%>");
13593 if (!hint && !flag_permissive)
13595 inform (next_token->location, "(if you use %<-fpermissive%> "
13596 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13597 "accept your code)");
13598 hint = true;
13602 else
13604 /* Look for the `<' that starts the template-argument-list. */
13605 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13607 pop_deferring_access_checks ();
13608 return error_mark_node;
13610 /* Parse the arguments. */
13611 arguments = cp_parser_enclosed_template_argument_list (parser);
13614 /* Build a representation of the specialization. */
13615 if (identifier_p (templ))
13616 template_id = build_min_nt_loc (next_token->location,
13617 TEMPLATE_ID_EXPR,
13618 templ, arguments);
13619 else if (DECL_TYPE_TEMPLATE_P (templ)
13620 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13622 bool entering_scope;
13623 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13624 template (rather than some instantiation thereof) only if
13625 is not nested within some other construct. For example, in
13626 "template <typename T> void f(T) { A<T>::", A<T> is just an
13627 instantiation of A. */
13628 entering_scope = (template_parm_scope_p ()
13629 && cp_lexer_next_token_is (parser->lexer,
13630 CPP_SCOPE));
13631 template_id
13632 = finish_template_type (templ, arguments, entering_scope);
13634 else if (variable_template_p (templ))
13636 template_id = lookup_template_variable (templ, arguments);
13638 else
13640 /* If it's not a class-template or a template-template, it should be
13641 a function-template. */
13642 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13643 || TREE_CODE (templ) == OVERLOAD
13644 || BASELINK_P (templ)));
13646 template_id = lookup_template_function (templ, arguments);
13649 /* If parsing tentatively, replace the sequence of tokens that makes
13650 up the template-id with a CPP_TEMPLATE_ID token. That way,
13651 should we re-parse the token stream, we will not have to repeat
13652 the effort required to do the parse, nor will we issue duplicate
13653 error messages about problems during instantiation of the
13654 template. */
13655 if (start_of_id
13656 /* Don't do this if we had a parse error in a declarator; re-parsing
13657 might succeed if a name changes meaning (60361). */
13658 && !(cp_parser_error_occurred (parser)
13659 && cp_parser_parsing_tentatively (parser)
13660 && parser->in_declarator_p))
13662 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13664 /* Reset the contents of the START_OF_ID token. */
13665 token->type = CPP_TEMPLATE_ID;
13666 /* Retrieve any deferred checks. Do not pop this access checks yet
13667 so the memory will not be reclaimed during token replacing below. */
13668 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13669 token->u.tree_check_value->value = template_id;
13670 token->u.tree_check_value->checks = get_deferred_access_checks ();
13671 token->keyword = RID_MAX;
13673 /* Purge all subsequent tokens. */
13674 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13676 /* ??? Can we actually assume that, if template_id ==
13677 error_mark_node, we will have issued a diagnostic to the
13678 user, as opposed to simply marking the tentative parse as
13679 failed? */
13680 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13681 error_at (token->location, "parse error in template argument list");
13684 pop_to_parent_deferring_access_checks ();
13685 return template_id;
13688 /* Parse a template-name.
13690 template-name:
13691 identifier
13693 The standard should actually say:
13695 template-name:
13696 identifier
13697 operator-function-id
13699 A defect report has been filed about this issue.
13701 A conversion-function-id cannot be a template name because they cannot
13702 be part of a template-id. In fact, looking at this code:
13704 a.operator K<int>()
13706 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13707 It is impossible to call a templated conversion-function-id with an
13708 explicit argument list, since the only allowed template parameter is
13709 the type to which it is converting.
13711 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13712 `template' keyword, in a construction like:
13714 T::template f<3>()
13716 In that case `f' is taken to be a template-name, even though there
13717 is no way of knowing for sure.
13719 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13720 name refers to a set of overloaded functions, at least one of which
13721 is a template, or an IDENTIFIER_NODE with the name of the template,
13722 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13723 names are looked up inside uninstantiated templates. */
13725 static tree
13726 cp_parser_template_name (cp_parser* parser,
13727 bool template_keyword_p,
13728 bool check_dependency_p,
13729 bool is_declaration,
13730 enum tag_types tag_type,
13731 bool *is_identifier)
13733 tree identifier;
13734 tree decl;
13735 tree fns;
13736 cp_token *token = cp_lexer_peek_token (parser->lexer);
13738 /* If the next token is `operator', then we have either an
13739 operator-function-id or a conversion-function-id. */
13740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13742 /* We don't know whether we're looking at an
13743 operator-function-id or a conversion-function-id. */
13744 cp_parser_parse_tentatively (parser);
13745 /* Try an operator-function-id. */
13746 identifier = cp_parser_operator_function_id (parser);
13747 /* If that didn't work, try a conversion-function-id. */
13748 if (!cp_parser_parse_definitely (parser))
13750 cp_parser_error (parser, "expected template-name");
13751 return error_mark_node;
13754 /* Look for the identifier. */
13755 else
13756 identifier = cp_parser_identifier (parser);
13758 /* If we didn't find an identifier, we don't have a template-id. */
13759 if (identifier == error_mark_node)
13760 return error_mark_node;
13762 /* If the name immediately followed the `template' keyword, then it
13763 is a template-name. However, if the next token is not `<', then
13764 we do not treat it as a template-name, since it is not being used
13765 as part of a template-id. This enables us to handle constructs
13766 like:
13768 template <typename T> struct S { S(); };
13769 template <typename T> S<T>::S();
13771 correctly. We would treat `S' as a template -- if it were `S<T>'
13772 -- but we do not if there is no `<'. */
13774 if (processing_template_decl
13775 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13777 /* In a declaration, in a dependent context, we pretend that the
13778 "template" keyword was present in order to improve error
13779 recovery. For example, given:
13781 template <typename T> void f(T::X<int>);
13783 we want to treat "X<int>" as a template-id. */
13784 if (is_declaration
13785 && !template_keyword_p
13786 && parser->scope && TYPE_P (parser->scope)
13787 && check_dependency_p
13788 && dependent_scope_p (parser->scope)
13789 /* Do not do this for dtors (or ctors), since they never
13790 need the template keyword before their name. */
13791 && !constructor_name_p (identifier, parser->scope))
13793 cp_token_position start = 0;
13795 /* Explain what went wrong. */
13796 error_at (token->location, "non-template %qD used as template",
13797 identifier);
13798 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13799 parser->scope, identifier);
13800 /* If parsing tentatively, find the location of the "<" token. */
13801 if (cp_parser_simulate_error (parser))
13802 start = cp_lexer_token_position (parser->lexer, true);
13803 /* Parse the template arguments so that we can issue error
13804 messages about them. */
13805 cp_lexer_consume_token (parser->lexer);
13806 cp_parser_enclosed_template_argument_list (parser);
13807 /* Skip tokens until we find a good place from which to
13808 continue parsing. */
13809 cp_parser_skip_to_closing_parenthesis (parser,
13810 /*recovering=*/true,
13811 /*or_comma=*/true,
13812 /*consume_paren=*/false);
13813 /* If parsing tentatively, permanently remove the
13814 template argument list. That will prevent duplicate
13815 error messages from being issued about the missing
13816 "template" keyword. */
13817 if (start)
13818 cp_lexer_purge_tokens_after (parser->lexer, start);
13819 if (is_identifier)
13820 *is_identifier = true;
13821 return identifier;
13824 /* If the "template" keyword is present, then there is generally
13825 no point in doing name-lookup, so we just return IDENTIFIER.
13826 But, if the qualifying scope is non-dependent then we can
13827 (and must) do name-lookup normally. */
13828 if (template_keyword_p
13829 && (!parser->scope
13830 || (TYPE_P (parser->scope)
13831 && dependent_type_p (parser->scope))))
13832 return identifier;
13835 /* Look up the name. */
13836 decl = cp_parser_lookup_name (parser, identifier,
13837 tag_type,
13838 /*is_template=*/true,
13839 /*is_namespace=*/false,
13840 check_dependency_p,
13841 /*ambiguous_decls=*/NULL,
13842 token->location);
13844 /* If DECL is a template, then the name was a template-name. */
13845 if (TREE_CODE (decl) == TEMPLATE_DECL)
13847 else
13849 tree fn = NULL_TREE;
13851 /* The standard does not explicitly indicate whether a name that
13852 names a set of overloaded declarations, some of which are
13853 templates, is a template-name. However, such a name should
13854 be a template-name; otherwise, there is no way to form a
13855 template-id for the overloaded templates. */
13856 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13857 if (TREE_CODE (fns) == OVERLOAD)
13858 for (fn = fns; fn; fn = OVL_NEXT (fn))
13859 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13860 break;
13862 if (!fn)
13864 /* The name does not name a template. */
13865 cp_parser_error (parser, "expected template-name");
13866 return error_mark_node;
13870 /* If DECL is dependent, and refers to a function, then just return
13871 its name; we will look it up again during template instantiation. */
13872 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13874 tree scope = ovl_scope (decl);
13875 if (TYPE_P (scope) && dependent_type_p (scope))
13876 return identifier;
13879 return decl;
13882 /* Parse a template-argument-list.
13884 template-argument-list:
13885 template-argument ... [opt]
13886 template-argument-list , template-argument ... [opt]
13888 Returns a TREE_VEC containing the arguments. */
13890 static tree
13891 cp_parser_template_argument_list (cp_parser* parser)
13893 tree fixed_args[10];
13894 unsigned n_args = 0;
13895 unsigned alloced = 10;
13896 tree *arg_ary = fixed_args;
13897 tree vec;
13898 bool saved_in_template_argument_list_p;
13899 bool saved_ice_p;
13900 bool saved_non_ice_p;
13902 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13903 parser->in_template_argument_list_p = true;
13904 /* Even if the template-id appears in an integral
13905 constant-expression, the contents of the argument list do
13906 not. */
13907 saved_ice_p = parser->integral_constant_expression_p;
13908 parser->integral_constant_expression_p = false;
13909 saved_non_ice_p = parser->non_integral_constant_expression_p;
13910 parser->non_integral_constant_expression_p = false;
13912 /* Parse the arguments. */
13915 tree argument;
13917 if (n_args)
13918 /* Consume the comma. */
13919 cp_lexer_consume_token (parser->lexer);
13921 /* Parse the template-argument. */
13922 argument = cp_parser_template_argument (parser);
13924 /* If the next token is an ellipsis, we're expanding a template
13925 argument pack. */
13926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13928 if (argument == error_mark_node)
13930 cp_token *token = cp_lexer_peek_token (parser->lexer);
13931 error_at (token->location,
13932 "expected parameter pack before %<...%>");
13934 /* Consume the `...' token. */
13935 cp_lexer_consume_token (parser->lexer);
13937 /* Make the argument into a TYPE_PACK_EXPANSION or
13938 EXPR_PACK_EXPANSION. */
13939 argument = make_pack_expansion (argument);
13942 if (n_args == alloced)
13944 alloced *= 2;
13946 if (arg_ary == fixed_args)
13948 arg_ary = XNEWVEC (tree, alloced);
13949 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13951 else
13952 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13954 arg_ary[n_args++] = argument;
13956 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13958 vec = make_tree_vec (n_args);
13960 while (n_args--)
13961 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13963 if (arg_ary != fixed_args)
13964 free (arg_ary);
13965 parser->non_integral_constant_expression_p = saved_non_ice_p;
13966 parser->integral_constant_expression_p = saved_ice_p;
13967 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13968 #ifdef ENABLE_CHECKING
13969 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13970 #endif
13971 return vec;
13974 /* Parse a template-argument.
13976 template-argument:
13977 assignment-expression
13978 type-id
13979 id-expression
13981 The representation is that of an assignment-expression, type-id, or
13982 id-expression -- except that the qualified id-expression is
13983 evaluated, so that the value returned is either a DECL or an
13984 OVERLOAD.
13986 Although the standard says "assignment-expression", it forbids
13987 throw-expressions or assignments in the template argument.
13988 Therefore, we use "conditional-expression" instead. */
13990 static tree
13991 cp_parser_template_argument (cp_parser* parser)
13993 tree argument;
13994 bool template_p;
13995 bool address_p;
13996 bool maybe_type_id = false;
13997 cp_token *token = NULL, *argument_start_token = NULL;
13998 location_t loc = 0;
13999 cp_id_kind idk;
14001 /* There's really no way to know what we're looking at, so we just
14002 try each alternative in order.
14004 [temp.arg]
14006 In a template-argument, an ambiguity between a type-id and an
14007 expression is resolved to a type-id, regardless of the form of
14008 the corresponding template-parameter.
14010 Therefore, we try a type-id first. */
14011 cp_parser_parse_tentatively (parser);
14012 argument = cp_parser_template_type_arg (parser);
14013 /* If there was no error parsing the type-id but the next token is a
14014 '>>', our behavior depends on which dialect of C++ we're
14015 parsing. In C++98, we probably found a typo for '> >'. But there
14016 are type-id which are also valid expressions. For instance:
14018 struct X { int operator >> (int); };
14019 template <int V> struct Foo {};
14020 Foo<X () >> 5> r;
14022 Here 'X()' is a valid type-id of a function type, but the user just
14023 wanted to write the expression "X() >> 5". Thus, we remember that we
14024 found a valid type-id, but we still try to parse the argument as an
14025 expression to see what happens.
14027 In C++0x, the '>>' will be considered two separate '>'
14028 tokens. */
14029 if (!cp_parser_error_occurred (parser)
14030 && cxx_dialect == cxx98
14031 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14033 maybe_type_id = true;
14034 cp_parser_abort_tentative_parse (parser);
14036 else
14038 /* If the next token isn't a `,' or a `>', then this argument wasn't
14039 really finished. This means that the argument is not a valid
14040 type-id. */
14041 if (!cp_parser_next_token_ends_template_argument_p (parser))
14042 cp_parser_error (parser, "expected template-argument");
14043 /* If that worked, we're done. */
14044 if (cp_parser_parse_definitely (parser))
14045 return argument;
14047 /* We're still not sure what the argument will be. */
14048 cp_parser_parse_tentatively (parser);
14049 /* Try a template. */
14050 argument_start_token = cp_lexer_peek_token (parser->lexer);
14051 argument = cp_parser_id_expression (parser,
14052 /*template_keyword_p=*/false,
14053 /*check_dependency_p=*/true,
14054 &template_p,
14055 /*declarator_p=*/false,
14056 /*optional_p=*/false);
14057 /* If the next token isn't a `,' or a `>', then this argument wasn't
14058 really finished. */
14059 if (!cp_parser_next_token_ends_template_argument_p (parser))
14060 cp_parser_error (parser, "expected template-argument");
14061 if (!cp_parser_error_occurred (parser))
14063 /* Figure out what is being referred to. If the id-expression
14064 was for a class template specialization, then we will have a
14065 TYPE_DECL at this point. There is no need to do name lookup
14066 at this point in that case. */
14067 if (TREE_CODE (argument) != TYPE_DECL)
14068 argument = cp_parser_lookup_name (parser, argument,
14069 none_type,
14070 /*is_template=*/template_p,
14071 /*is_namespace=*/false,
14072 /*check_dependency=*/true,
14073 /*ambiguous_decls=*/NULL,
14074 argument_start_token->location);
14075 if (TREE_CODE (argument) != TEMPLATE_DECL
14076 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14077 cp_parser_error (parser, "expected template-name");
14079 if (cp_parser_parse_definitely (parser))
14080 return argument;
14081 /* It must be a non-type argument. There permitted cases are given
14082 in [temp.arg.nontype]:
14084 -- an integral constant-expression of integral or enumeration
14085 type; or
14087 -- the name of a non-type template-parameter; or
14089 -- the name of an object or function with external linkage...
14091 -- the address of an object or function with external linkage...
14093 -- a pointer to member... */
14094 /* Look for a non-type template parameter. */
14095 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14097 cp_parser_parse_tentatively (parser);
14098 argument = cp_parser_primary_expression (parser,
14099 /*address_p=*/false,
14100 /*cast_p=*/false,
14101 /*template_arg_p=*/true,
14102 &idk);
14103 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14104 || !cp_parser_next_token_ends_template_argument_p (parser))
14105 cp_parser_simulate_error (parser);
14106 if (cp_parser_parse_definitely (parser))
14107 return argument;
14110 /* If the next token is "&", the argument must be the address of an
14111 object or function with external linkage. */
14112 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14113 if (address_p)
14115 loc = cp_lexer_peek_token (parser->lexer)->location;
14116 cp_lexer_consume_token (parser->lexer);
14118 /* See if we might have an id-expression. */
14119 token = cp_lexer_peek_token (parser->lexer);
14120 if (token->type == CPP_NAME
14121 || token->keyword == RID_OPERATOR
14122 || token->type == CPP_SCOPE
14123 || token->type == CPP_TEMPLATE_ID
14124 || token->type == CPP_NESTED_NAME_SPECIFIER)
14126 cp_parser_parse_tentatively (parser);
14127 argument = cp_parser_primary_expression (parser,
14128 address_p,
14129 /*cast_p=*/false,
14130 /*template_arg_p=*/true,
14131 &idk);
14132 if (cp_parser_error_occurred (parser)
14133 || !cp_parser_next_token_ends_template_argument_p (parser))
14134 cp_parser_abort_tentative_parse (parser);
14135 else
14137 tree probe;
14139 if (INDIRECT_REF_P (argument))
14141 /* Strip the dereference temporarily. */
14142 gcc_assert (REFERENCE_REF_P (argument));
14143 argument = TREE_OPERAND (argument, 0);
14146 /* If we're in a template, we represent a qualified-id referring
14147 to a static data member as a SCOPE_REF even if the scope isn't
14148 dependent so that we can check access control later. */
14149 probe = argument;
14150 if (TREE_CODE (probe) == SCOPE_REF)
14151 probe = TREE_OPERAND (probe, 1);
14152 if (VAR_P (probe))
14154 /* A variable without external linkage might still be a
14155 valid constant-expression, so no error is issued here
14156 if the external-linkage check fails. */
14157 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14158 cp_parser_simulate_error (parser);
14160 else if (is_overloaded_fn (argument))
14161 /* All overloaded functions are allowed; if the external
14162 linkage test does not pass, an error will be issued
14163 later. */
14165 else if (address_p
14166 && (TREE_CODE (argument) == OFFSET_REF
14167 || TREE_CODE (argument) == SCOPE_REF))
14168 /* A pointer-to-member. */
14170 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14172 else
14173 cp_parser_simulate_error (parser);
14175 if (cp_parser_parse_definitely (parser))
14177 if (address_p)
14178 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14179 tf_warning_or_error);
14180 else
14181 argument = convert_from_reference (argument);
14182 return argument;
14186 /* If the argument started with "&", there are no other valid
14187 alternatives at this point. */
14188 if (address_p)
14190 cp_parser_error (parser, "invalid non-type template argument");
14191 return error_mark_node;
14194 /* If the argument wasn't successfully parsed as a type-id followed
14195 by '>>', the argument can only be a constant expression now.
14196 Otherwise, we try parsing the constant-expression tentatively,
14197 because the argument could really be a type-id. */
14198 if (maybe_type_id)
14199 cp_parser_parse_tentatively (parser);
14200 argument = cp_parser_constant_expression (parser,
14201 /*allow_non_constant_p=*/false,
14202 /*non_constant_p=*/NULL);
14203 if (!maybe_type_id)
14204 return argument;
14205 if (!cp_parser_next_token_ends_template_argument_p (parser))
14206 cp_parser_error (parser, "expected template-argument");
14207 if (cp_parser_parse_definitely (parser))
14208 return argument;
14209 /* We did our best to parse the argument as a non type-id, but that
14210 was the only alternative that matched (albeit with a '>' after
14211 it). We can assume it's just a typo from the user, and a
14212 diagnostic will then be issued. */
14213 return cp_parser_template_type_arg (parser);
14216 /* Parse an explicit-instantiation.
14218 explicit-instantiation:
14219 template declaration
14221 Although the standard says `declaration', what it really means is:
14223 explicit-instantiation:
14224 template decl-specifier-seq [opt] declarator [opt] ;
14226 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14227 supposed to be allowed. A defect report has been filed about this
14228 issue.
14230 GNU Extension:
14232 explicit-instantiation:
14233 storage-class-specifier template
14234 decl-specifier-seq [opt] declarator [opt] ;
14235 function-specifier template
14236 decl-specifier-seq [opt] declarator [opt] ; */
14238 static void
14239 cp_parser_explicit_instantiation (cp_parser* parser)
14241 int declares_class_or_enum;
14242 cp_decl_specifier_seq decl_specifiers;
14243 tree extension_specifier = NULL_TREE;
14245 timevar_push (TV_TEMPLATE_INST);
14247 /* Look for an (optional) storage-class-specifier or
14248 function-specifier. */
14249 if (cp_parser_allow_gnu_extensions_p (parser))
14251 extension_specifier
14252 = cp_parser_storage_class_specifier_opt (parser);
14253 if (!extension_specifier)
14254 extension_specifier
14255 = cp_parser_function_specifier_opt (parser,
14256 /*decl_specs=*/NULL);
14259 /* Look for the `template' keyword. */
14260 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14261 /* Let the front end know that we are processing an explicit
14262 instantiation. */
14263 begin_explicit_instantiation ();
14264 /* [temp.explicit] says that we are supposed to ignore access
14265 control while processing explicit instantiation directives. */
14266 push_deferring_access_checks (dk_no_check);
14267 /* Parse a decl-specifier-seq. */
14268 cp_parser_decl_specifier_seq (parser,
14269 CP_PARSER_FLAGS_OPTIONAL,
14270 &decl_specifiers,
14271 &declares_class_or_enum);
14272 /* If there was exactly one decl-specifier, and it declared a class,
14273 and there's no declarator, then we have an explicit type
14274 instantiation. */
14275 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14277 tree type;
14279 type = check_tag_decl (&decl_specifiers,
14280 /*explicit_type_instantiation_p=*/true);
14281 /* Turn access control back on for names used during
14282 template instantiation. */
14283 pop_deferring_access_checks ();
14284 if (type)
14285 do_type_instantiation (type, extension_specifier,
14286 /*complain=*/tf_error);
14288 else
14290 cp_declarator *declarator;
14291 tree decl;
14293 /* Parse the declarator. */
14294 declarator
14295 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14296 /*ctor_dtor_or_conv_p=*/NULL,
14297 /*parenthesized_p=*/NULL,
14298 /*member_p=*/false,
14299 /*friend_p=*/false);
14300 if (declares_class_or_enum & 2)
14301 cp_parser_check_for_definition_in_return_type (declarator,
14302 decl_specifiers.type,
14303 decl_specifiers.locations[ds_type_spec]);
14304 if (declarator != cp_error_declarator)
14306 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14307 permerror (decl_specifiers.locations[ds_inline],
14308 "explicit instantiation shall not use"
14309 " %<inline%> specifier");
14310 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14311 permerror (decl_specifiers.locations[ds_constexpr],
14312 "explicit instantiation shall not use"
14313 " %<constexpr%> specifier");
14315 decl = grokdeclarator (declarator, &decl_specifiers,
14316 NORMAL, 0, &decl_specifiers.attributes);
14317 /* Turn access control back on for names used during
14318 template instantiation. */
14319 pop_deferring_access_checks ();
14320 /* Do the explicit instantiation. */
14321 do_decl_instantiation (decl, extension_specifier);
14323 else
14325 pop_deferring_access_checks ();
14326 /* Skip the body of the explicit instantiation. */
14327 cp_parser_skip_to_end_of_statement (parser);
14330 /* We're done with the instantiation. */
14331 end_explicit_instantiation ();
14333 cp_parser_consume_semicolon_at_end_of_statement (parser);
14335 timevar_pop (TV_TEMPLATE_INST);
14338 /* Parse an explicit-specialization.
14340 explicit-specialization:
14341 template < > declaration
14343 Although the standard says `declaration', what it really means is:
14345 explicit-specialization:
14346 template <> decl-specifier [opt] init-declarator [opt] ;
14347 template <> function-definition
14348 template <> explicit-specialization
14349 template <> template-declaration */
14351 static void
14352 cp_parser_explicit_specialization (cp_parser* parser)
14354 bool need_lang_pop;
14355 cp_token *token = cp_lexer_peek_token (parser->lexer);
14357 /* Look for the `template' keyword. */
14358 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14359 /* Look for the `<'. */
14360 cp_parser_require (parser, CPP_LESS, RT_LESS);
14361 /* Look for the `>'. */
14362 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14363 /* We have processed another parameter list. */
14364 ++parser->num_template_parameter_lists;
14365 /* [temp]
14367 A template ... explicit specialization ... shall not have C
14368 linkage. */
14369 if (current_lang_name == lang_name_c)
14371 error_at (token->location, "template specialization with C linkage");
14372 /* Give it C++ linkage to avoid confusing other parts of the
14373 front end. */
14374 push_lang_context (lang_name_cplusplus);
14375 need_lang_pop = true;
14377 else
14378 need_lang_pop = false;
14379 /* Let the front end know that we are beginning a specialization. */
14380 if (!begin_specialization ())
14382 end_specialization ();
14383 return;
14386 /* If the next keyword is `template', we need to figure out whether
14387 or not we're looking a template-declaration. */
14388 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14390 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14391 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14392 cp_parser_template_declaration_after_export (parser,
14393 /*member_p=*/false);
14394 else
14395 cp_parser_explicit_specialization (parser);
14397 else
14398 /* Parse the dependent declaration. */
14399 cp_parser_single_declaration (parser,
14400 /*checks=*/NULL,
14401 /*member_p=*/false,
14402 /*explicit_specialization_p=*/true,
14403 /*friend_p=*/NULL);
14404 /* We're done with the specialization. */
14405 end_specialization ();
14406 /* For the erroneous case of a template with C linkage, we pushed an
14407 implicit C++ linkage scope; exit that scope now. */
14408 if (need_lang_pop)
14409 pop_lang_context ();
14410 /* We're done with this parameter list. */
14411 --parser->num_template_parameter_lists;
14414 /* Parse a type-specifier.
14416 type-specifier:
14417 simple-type-specifier
14418 class-specifier
14419 enum-specifier
14420 elaborated-type-specifier
14421 cv-qualifier
14423 GNU Extension:
14425 type-specifier:
14426 __complex__
14428 Returns a representation of the type-specifier. For a
14429 class-specifier, enum-specifier, or elaborated-type-specifier, a
14430 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14432 The parser flags FLAGS is used to control type-specifier parsing.
14434 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14435 in a decl-specifier-seq.
14437 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14438 class-specifier, enum-specifier, or elaborated-type-specifier, then
14439 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14440 if a type is declared; 2 if it is defined. Otherwise, it is set to
14441 zero.
14443 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14444 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14445 is set to FALSE. */
14447 static tree
14448 cp_parser_type_specifier (cp_parser* parser,
14449 cp_parser_flags flags,
14450 cp_decl_specifier_seq *decl_specs,
14451 bool is_declaration,
14452 int* declares_class_or_enum,
14453 bool* is_cv_qualifier)
14455 tree type_spec = NULL_TREE;
14456 cp_token *token;
14457 enum rid keyword;
14458 cp_decl_spec ds = ds_last;
14460 /* Assume this type-specifier does not declare a new type. */
14461 if (declares_class_or_enum)
14462 *declares_class_or_enum = 0;
14463 /* And that it does not specify a cv-qualifier. */
14464 if (is_cv_qualifier)
14465 *is_cv_qualifier = false;
14466 /* Peek at the next token. */
14467 token = cp_lexer_peek_token (parser->lexer);
14469 /* If we're looking at a keyword, we can use that to guide the
14470 production we choose. */
14471 keyword = token->keyword;
14472 switch (keyword)
14474 case RID_ENUM:
14475 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14476 goto elaborated_type_specifier;
14478 /* Look for the enum-specifier. */
14479 type_spec = cp_parser_enum_specifier (parser);
14480 /* If that worked, we're done. */
14481 if (type_spec)
14483 if (declares_class_or_enum)
14484 *declares_class_or_enum = 2;
14485 if (decl_specs)
14486 cp_parser_set_decl_spec_type (decl_specs,
14487 type_spec,
14488 token,
14489 /*type_definition_p=*/true);
14490 return type_spec;
14492 else
14493 goto elaborated_type_specifier;
14495 /* Any of these indicate either a class-specifier, or an
14496 elaborated-type-specifier. */
14497 case RID_CLASS:
14498 case RID_STRUCT:
14499 case RID_UNION:
14500 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14501 goto elaborated_type_specifier;
14503 /* Parse tentatively so that we can back up if we don't find a
14504 class-specifier. */
14505 cp_parser_parse_tentatively (parser);
14506 /* Look for the class-specifier. */
14507 type_spec = cp_parser_class_specifier (parser);
14508 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14509 /* If that worked, we're done. */
14510 if (cp_parser_parse_definitely (parser))
14512 if (declares_class_or_enum)
14513 *declares_class_or_enum = 2;
14514 if (decl_specs)
14515 cp_parser_set_decl_spec_type (decl_specs,
14516 type_spec,
14517 token,
14518 /*type_definition_p=*/true);
14519 return type_spec;
14522 /* Fall through. */
14523 elaborated_type_specifier:
14524 /* We're declaring (not defining) a class or enum. */
14525 if (declares_class_or_enum)
14526 *declares_class_or_enum = 1;
14528 /* Fall through. */
14529 case RID_TYPENAME:
14530 /* Look for an elaborated-type-specifier. */
14531 type_spec
14532 = (cp_parser_elaborated_type_specifier
14533 (parser,
14534 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14535 is_declaration));
14536 if (decl_specs)
14537 cp_parser_set_decl_spec_type (decl_specs,
14538 type_spec,
14539 token,
14540 /*type_definition_p=*/false);
14541 return type_spec;
14543 case RID_CONST:
14544 ds = ds_const;
14545 if (is_cv_qualifier)
14546 *is_cv_qualifier = true;
14547 break;
14549 case RID_VOLATILE:
14550 ds = ds_volatile;
14551 if (is_cv_qualifier)
14552 *is_cv_qualifier = true;
14553 break;
14555 case RID_RESTRICT:
14556 ds = ds_restrict;
14557 if (is_cv_qualifier)
14558 *is_cv_qualifier = true;
14559 break;
14561 case RID_COMPLEX:
14562 /* The `__complex__' keyword is a GNU extension. */
14563 ds = ds_complex;
14564 break;
14566 default:
14567 break;
14570 /* Handle simple keywords. */
14571 if (ds != ds_last)
14573 if (decl_specs)
14575 set_and_check_decl_spec_loc (decl_specs, ds, token);
14576 decl_specs->any_specifiers_p = true;
14578 return cp_lexer_consume_token (parser->lexer)->u.value;
14581 /* If we do not already have a type-specifier, assume we are looking
14582 at a simple-type-specifier. */
14583 type_spec = cp_parser_simple_type_specifier (parser,
14584 decl_specs,
14585 flags);
14587 /* If we didn't find a type-specifier, and a type-specifier was not
14588 optional in this context, issue an error message. */
14589 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14591 cp_parser_error (parser, "expected type specifier");
14592 return error_mark_node;
14595 return type_spec;
14598 /* Parse a simple-type-specifier.
14600 simple-type-specifier:
14601 :: [opt] nested-name-specifier [opt] type-name
14602 :: [opt] nested-name-specifier template template-id
14603 char
14604 wchar_t
14605 bool
14606 short
14608 long
14609 signed
14610 unsigned
14611 float
14612 double
14613 void
14615 C++0x Extension:
14617 simple-type-specifier:
14618 auto
14619 decltype ( expression )
14620 char16_t
14621 char32_t
14622 __underlying_type ( type-id )
14624 GNU Extension:
14626 simple-type-specifier:
14627 __int128
14628 __typeof__ unary-expression
14629 __typeof__ ( type-id )
14630 __typeof__ ( type-id ) { initializer-list , [opt] }
14632 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14633 appropriately updated. */
14635 static tree
14636 cp_parser_simple_type_specifier (cp_parser* parser,
14637 cp_decl_specifier_seq *decl_specs,
14638 cp_parser_flags flags)
14640 tree type = NULL_TREE;
14641 cp_token *token;
14643 /* Peek at the next token. */
14644 token = cp_lexer_peek_token (parser->lexer);
14646 /* If we're looking at a keyword, things are easy. */
14647 switch (token->keyword)
14649 case RID_CHAR:
14650 if (decl_specs)
14651 decl_specs->explicit_char_p = true;
14652 type = char_type_node;
14653 break;
14654 case RID_CHAR16:
14655 type = char16_type_node;
14656 break;
14657 case RID_CHAR32:
14658 type = char32_type_node;
14659 break;
14660 case RID_WCHAR:
14661 type = wchar_type_node;
14662 break;
14663 case RID_BOOL:
14664 type = boolean_type_node;
14665 break;
14666 case RID_SHORT:
14667 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14668 type = short_integer_type_node;
14669 break;
14670 case RID_INT:
14671 if (decl_specs)
14672 decl_specs->explicit_int_p = true;
14673 type = integer_type_node;
14674 break;
14675 case RID_INT128:
14676 if (!int128_integer_type_node)
14677 break;
14678 if (decl_specs)
14679 decl_specs->explicit_int128_p = true;
14680 type = int128_integer_type_node;
14681 break;
14682 case RID_LONG:
14683 if (decl_specs)
14684 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14685 type = long_integer_type_node;
14686 break;
14687 case RID_SIGNED:
14688 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14689 type = integer_type_node;
14690 break;
14691 case RID_UNSIGNED:
14692 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14693 type = unsigned_type_node;
14694 break;
14695 case RID_FLOAT:
14696 type = float_type_node;
14697 break;
14698 case RID_DOUBLE:
14699 type = double_type_node;
14700 break;
14701 case RID_VOID:
14702 type = void_type_node;
14703 break;
14705 case RID_AUTO:
14706 maybe_warn_cpp0x (CPP0X_AUTO);
14707 if (parser->auto_is_implicit_function_template_parm_p)
14709 type = synthesize_implicit_template_parm (parser);
14711 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14713 if (cxx_dialect < cxx14)
14714 pedwarn (location_of (type), 0,
14715 "use of %<auto%> in lambda parameter declaration "
14716 "only available with "
14717 "-std=c++14 or -std=gnu++14");
14719 else if (cxx_dialect < cxx14)
14720 pedwarn (location_of (type), 0,
14721 "use of %<auto%> in parameter declaration "
14722 "only available with "
14723 "-std=c++14 or -std=gnu++14");
14724 else
14725 pedwarn (location_of (type), OPT_Wpedantic,
14726 "ISO C++ forbids use of %<auto%> in parameter "
14727 "declaration");
14729 else
14730 type = make_auto ();
14731 break;
14733 case RID_DECLTYPE:
14734 /* Since DR 743, decltype can either be a simple-type-specifier by
14735 itself or begin a nested-name-specifier. Parsing it will replace
14736 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14737 handling below decide what to do. */
14738 cp_parser_decltype (parser);
14739 cp_lexer_set_token_position (parser->lexer, token);
14740 break;
14742 case RID_TYPEOF:
14743 /* Consume the `typeof' token. */
14744 cp_lexer_consume_token (parser->lexer);
14745 /* Parse the operand to `typeof'. */
14746 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14747 /* If it is not already a TYPE, take its type. */
14748 if (!TYPE_P (type))
14749 type = finish_typeof (type);
14751 if (decl_specs)
14752 cp_parser_set_decl_spec_type (decl_specs, type,
14753 token,
14754 /*type_definition_p=*/false);
14756 return type;
14758 case RID_UNDERLYING_TYPE:
14759 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14760 if (decl_specs)
14761 cp_parser_set_decl_spec_type (decl_specs, type,
14762 token,
14763 /*type_definition_p=*/false);
14765 return type;
14767 case RID_BASES:
14768 case RID_DIRECT_BASES:
14769 type = cp_parser_trait_expr (parser, token->keyword);
14770 if (decl_specs)
14771 cp_parser_set_decl_spec_type (decl_specs, type,
14772 token,
14773 /*type_definition_p=*/false);
14774 return type;
14775 default:
14776 break;
14779 /* If token is an already-parsed decltype not followed by ::,
14780 it's a simple-type-specifier. */
14781 if (token->type == CPP_DECLTYPE
14782 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14784 type = token->u.value;
14785 if (decl_specs)
14786 cp_parser_set_decl_spec_type (decl_specs, type,
14787 token,
14788 /*type_definition_p=*/false);
14789 cp_lexer_consume_token (parser->lexer);
14790 return type;
14793 /* If the type-specifier was for a built-in type, we're done. */
14794 if (type)
14796 /* Record the type. */
14797 if (decl_specs
14798 && (token->keyword != RID_SIGNED
14799 && token->keyword != RID_UNSIGNED
14800 && token->keyword != RID_SHORT
14801 && token->keyword != RID_LONG))
14802 cp_parser_set_decl_spec_type (decl_specs,
14803 type,
14804 token,
14805 /*type_definition_p=*/false);
14806 if (decl_specs)
14807 decl_specs->any_specifiers_p = true;
14809 /* Consume the token. */
14810 cp_lexer_consume_token (parser->lexer);
14812 /* There is no valid C++ program where a non-template type is
14813 followed by a "<". That usually indicates that the user thought
14814 that the type was a template. */
14815 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14816 token->location);
14818 return TYPE_NAME (type);
14821 /* The type-specifier must be a user-defined type. */
14822 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14824 bool qualified_p;
14825 bool global_p;
14827 /* Don't gobble tokens or issue error messages if this is an
14828 optional type-specifier. */
14829 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14830 cp_parser_parse_tentatively (parser);
14832 /* Look for the optional `::' operator. */
14833 global_p
14834 = (cp_parser_global_scope_opt (parser,
14835 /*current_scope_valid_p=*/false)
14836 != NULL_TREE);
14837 /* Look for the nested-name specifier. */
14838 qualified_p
14839 = (cp_parser_nested_name_specifier_opt (parser,
14840 /*typename_keyword_p=*/false,
14841 /*check_dependency_p=*/true,
14842 /*type_p=*/false,
14843 /*is_declaration=*/false)
14844 != NULL_TREE);
14845 token = cp_lexer_peek_token (parser->lexer);
14846 /* If we have seen a nested-name-specifier, and the next token
14847 is `template', then we are using the template-id production. */
14848 if (parser->scope
14849 && cp_parser_optional_template_keyword (parser))
14851 /* Look for the template-id. */
14852 type = cp_parser_template_id (parser,
14853 /*template_keyword_p=*/true,
14854 /*check_dependency_p=*/true,
14855 none_type,
14856 /*is_declaration=*/false);
14857 /* If the template-id did not name a type, we are out of
14858 luck. */
14859 if (TREE_CODE (type) != TYPE_DECL)
14861 cp_parser_error (parser, "expected template-id for type");
14862 type = NULL_TREE;
14865 /* Otherwise, look for a type-name. */
14866 else
14867 type = cp_parser_type_name (parser);
14868 /* Keep track of all name-lookups performed in class scopes. */
14869 if (type
14870 && !global_p
14871 && !qualified_p
14872 && TREE_CODE (type) == TYPE_DECL
14873 && identifier_p (DECL_NAME (type)))
14874 maybe_note_name_used_in_class (DECL_NAME (type), type);
14875 /* If it didn't work out, we don't have a TYPE. */
14876 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14877 && !cp_parser_parse_definitely (parser))
14878 type = NULL_TREE;
14879 if (type && decl_specs)
14880 cp_parser_set_decl_spec_type (decl_specs, type,
14881 token,
14882 /*type_definition_p=*/false);
14885 /* If we didn't get a type-name, issue an error message. */
14886 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14888 cp_parser_error (parser, "expected type-name");
14889 return error_mark_node;
14892 if (type && type != error_mark_node)
14894 /* See if TYPE is an Objective-C type, and if so, parse and
14895 accept any protocol references following it. Do this before
14896 the cp_parser_check_for_invalid_template_id() call, because
14897 Objective-C types can be followed by '<...>' which would
14898 enclose protocol names rather than template arguments, and so
14899 everything is fine. */
14900 if (c_dialect_objc () && !parser->scope
14901 && (objc_is_id (type) || objc_is_class_name (type)))
14903 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14904 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14906 /* Clobber the "unqualified" type previously entered into
14907 DECL_SPECS with the new, improved protocol-qualified version. */
14908 if (decl_specs)
14909 decl_specs->type = qual_type;
14911 return qual_type;
14914 /* There is no valid C++ program where a non-template type is
14915 followed by a "<". That usually indicates that the user
14916 thought that the type was a template. */
14917 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14918 none_type,
14919 token->location);
14922 return type;
14925 /* Parse a type-name.
14927 type-name:
14928 class-name
14929 enum-name
14930 typedef-name
14931 simple-template-id [in c++0x]
14933 enum-name:
14934 identifier
14936 typedef-name:
14937 identifier
14939 Returns a TYPE_DECL for the type. */
14941 static tree
14942 cp_parser_type_name (cp_parser* parser)
14944 tree type_decl;
14946 /* We can't know yet whether it is a class-name or not. */
14947 cp_parser_parse_tentatively (parser);
14948 /* Try a class-name. */
14949 type_decl = cp_parser_class_name (parser,
14950 /*typename_keyword_p=*/false,
14951 /*template_keyword_p=*/false,
14952 none_type,
14953 /*check_dependency_p=*/true,
14954 /*class_head_p=*/false,
14955 /*is_declaration=*/false);
14956 /* If it's not a class-name, keep looking. */
14957 if (!cp_parser_parse_definitely (parser))
14959 if (cxx_dialect < cxx11)
14960 /* It must be a typedef-name or an enum-name. */
14961 return cp_parser_nonclass_name (parser);
14963 cp_parser_parse_tentatively (parser);
14964 /* It is either a simple-template-id representing an
14965 instantiation of an alias template... */
14966 type_decl = cp_parser_template_id (parser,
14967 /*template_keyword_p=*/false,
14968 /*check_dependency_p=*/true,
14969 none_type,
14970 /*is_declaration=*/false);
14971 /* Note that this must be an instantiation of an alias template
14972 because [temp.names]/6 says:
14974 A template-id that names an alias template specialization
14975 is a type-name.
14977 Whereas [temp.names]/7 says:
14979 A simple-template-id that names a class template
14980 specialization is a class-name. */
14981 if (type_decl != NULL_TREE
14982 && TREE_CODE (type_decl) == TYPE_DECL
14983 && TYPE_DECL_ALIAS_P (type_decl))
14984 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14985 else
14986 cp_parser_simulate_error (parser);
14988 if (!cp_parser_parse_definitely (parser))
14989 /* ... Or a typedef-name or an enum-name. */
14990 return cp_parser_nonclass_name (parser);
14993 return type_decl;
14996 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14998 enum-name:
14999 identifier
15001 typedef-name:
15002 identifier
15004 Returns a TYPE_DECL for the type. */
15006 static tree
15007 cp_parser_nonclass_name (cp_parser* parser)
15009 tree type_decl;
15010 tree identifier;
15012 cp_token *token = cp_lexer_peek_token (parser->lexer);
15013 identifier = cp_parser_identifier (parser);
15014 if (identifier == error_mark_node)
15015 return error_mark_node;
15017 /* Look up the type-name. */
15018 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15020 type_decl = strip_using_decl (type_decl);
15022 if (TREE_CODE (type_decl) != TYPE_DECL
15023 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15025 /* See if this is an Objective-C type. */
15026 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15027 tree type = objc_get_protocol_qualified_type (identifier, protos);
15028 if (type)
15029 type_decl = TYPE_NAME (type);
15032 /* Issue an error if we did not find a type-name. */
15033 if (TREE_CODE (type_decl) != TYPE_DECL
15034 /* In Objective-C, we have the complication that class names are
15035 normally type names and start declarations (eg, the
15036 "NSObject" in "NSObject *object;"), but can be used in an
15037 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15038 is an expression. So, a classname followed by a dot is not a
15039 valid type-name. */
15040 || (objc_is_class_name (TREE_TYPE (type_decl))
15041 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15043 if (!cp_parser_simulate_error (parser))
15044 cp_parser_name_lookup_error (parser, identifier, type_decl,
15045 NLE_TYPE, token->location);
15046 return error_mark_node;
15048 /* Remember that the name was used in the definition of the
15049 current class so that we can check later to see if the
15050 meaning would have been different after the class was
15051 entirely defined. */
15052 else if (type_decl != error_mark_node
15053 && !parser->scope)
15054 maybe_note_name_used_in_class (identifier, type_decl);
15056 return type_decl;
15059 /* Parse an elaborated-type-specifier. Note that the grammar given
15060 here incorporates the resolution to DR68.
15062 elaborated-type-specifier:
15063 class-key :: [opt] nested-name-specifier [opt] identifier
15064 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15065 enum-key :: [opt] nested-name-specifier [opt] identifier
15066 typename :: [opt] nested-name-specifier identifier
15067 typename :: [opt] nested-name-specifier template [opt]
15068 template-id
15070 GNU extension:
15072 elaborated-type-specifier:
15073 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15074 class-key attributes :: [opt] nested-name-specifier [opt]
15075 template [opt] template-id
15076 enum attributes :: [opt] nested-name-specifier [opt] identifier
15078 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15079 declared `friend'. If IS_DECLARATION is TRUE, then this
15080 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15081 something is being declared.
15083 Returns the TYPE specified. */
15085 static tree
15086 cp_parser_elaborated_type_specifier (cp_parser* parser,
15087 bool is_friend,
15088 bool is_declaration)
15090 enum tag_types tag_type;
15091 tree identifier;
15092 tree type = NULL_TREE;
15093 tree attributes = NULL_TREE;
15094 tree globalscope;
15095 cp_token *token = NULL;
15097 /* See if we're looking at the `enum' keyword. */
15098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15100 /* Consume the `enum' token. */
15101 cp_lexer_consume_token (parser->lexer);
15102 /* Remember that it's an enumeration type. */
15103 tag_type = enum_type;
15104 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15105 enums) is used here. */
15106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15107 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15109 pedwarn (input_location, 0, "elaborated-type-specifier "
15110 "for a scoped enum must not use the %<%D%> keyword",
15111 cp_lexer_peek_token (parser->lexer)->u.value);
15112 /* Consume the `struct' or `class' and parse it anyway. */
15113 cp_lexer_consume_token (parser->lexer);
15115 /* Parse the attributes. */
15116 attributes = cp_parser_attributes_opt (parser);
15118 /* Or, it might be `typename'. */
15119 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15120 RID_TYPENAME))
15122 /* Consume the `typename' token. */
15123 cp_lexer_consume_token (parser->lexer);
15124 /* Remember that it's a `typename' type. */
15125 tag_type = typename_type;
15127 /* Otherwise it must be a class-key. */
15128 else
15130 tag_type = cp_parser_class_key (parser);
15131 if (tag_type == none_type)
15132 return error_mark_node;
15133 /* Parse the attributes. */
15134 attributes = cp_parser_attributes_opt (parser);
15137 /* Look for the `::' operator. */
15138 globalscope = cp_parser_global_scope_opt (parser,
15139 /*current_scope_valid_p=*/false);
15140 /* Look for the nested-name-specifier. */
15141 if (tag_type == typename_type && !globalscope)
15143 if (!cp_parser_nested_name_specifier (parser,
15144 /*typename_keyword_p=*/true,
15145 /*check_dependency_p=*/true,
15146 /*type_p=*/true,
15147 is_declaration))
15148 return error_mark_node;
15150 else
15151 /* Even though `typename' is not present, the proposed resolution
15152 to Core Issue 180 says that in `class A<T>::B', `B' should be
15153 considered a type-name, even if `A<T>' is dependent. */
15154 cp_parser_nested_name_specifier_opt (parser,
15155 /*typename_keyword_p=*/true,
15156 /*check_dependency_p=*/true,
15157 /*type_p=*/true,
15158 is_declaration);
15159 /* For everything but enumeration types, consider a template-id.
15160 For an enumeration type, consider only a plain identifier. */
15161 if (tag_type != enum_type)
15163 bool template_p = false;
15164 tree decl;
15166 /* Allow the `template' keyword. */
15167 template_p = cp_parser_optional_template_keyword (parser);
15168 /* If we didn't see `template', we don't know if there's a
15169 template-id or not. */
15170 if (!template_p)
15171 cp_parser_parse_tentatively (parser);
15172 /* Parse the template-id. */
15173 token = cp_lexer_peek_token (parser->lexer);
15174 decl = cp_parser_template_id (parser, template_p,
15175 /*check_dependency_p=*/true,
15176 tag_type,
15177 is_declaration);
15178 /* If we didn't find a template-id, look for an ordinary
15179 identifier. */
15180 if (!template_p && !cp_parser_parse_definitely (parser))
15182 /* We can get here when cp_parser_template_id, called by
15183 cp_parser_class_name with tag_type == none_type, succeeds
15184 and caches a BASELINK. Then, when called again here,
15185 instead of failing and returning an error_mark_node
15186 returns it (see template/typename17.C in C++11).
15187 ??? Could we diagnose this earlier? */
15188 else if (tag_type == typename_type && BASELINK_P (decl))
15190 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15191 type = error_mark_node;
15193 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15194 in effect, then we must assume that, upon instantiation, the
15195 template will correspond to a class. */
15196 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15197 && tag_type == typename_type)
15198 type = make_typename_type (parser->scope, decl,
15199 typename_type,
15200 /*complain=*/tf_error);
15201 /* If the `typename' keyword is in effect and DECL is not a type
15202 decl, then type is non existent. */
15203 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15205 else if (TREE_CODE (decl) == TYPE_DECL)
15206 type = check_elaborated_type_specifier (tag_type, decl,
15207 /*allow_template_p=*/true);
15208 else if (decl == error_mark_node)
15209 type = error_mark_node;
15212 if (!type)
15214 token = cp_lexer_peek_token (parser->lexer);
15215 identifier = cp_parser_identifier (parser);
15217 if (identifier == error_mark_node)
15219 parser->scope = NULL_TREE;
15220 return error_mark_node;
15223 /* For a `typename', we needn't call xref_tag. */
15224 if (tag_type == typename_type
15225 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15226 return cp_parser_make_typename_type (parser, identifier,
15227 token->location);
15229 /* Template parameter lists apply only if we are not within a
15230 function parameter list. */
15231 bool template_parm_lists_apply
15232 = parser->num_template_parameter_lists;
15233 if (template_parm_lists_apply)
15234 for (cp_binding_level *s = current_binding_level;
15235 s && s->kind != sk_template_parms;
15236 s = s->level_chain)
15237 if (s->kind == sk_function_parms)
15238 template_parm_lists_apply = false;
15240 /* Look up a qualified name in the usual way. */
15241 if (parser->scope)
15243 tree decl;
15244 tree ambiguous_decls;
15246 decl = cp_parser_lookup_name (parser, identifier,
15247 tag_type,
15248 /*is_template=*/false,
15249 /*is_namespace=*/false,
15250 /*check_dependency=*/true,
15251 &ambiguous_decls,
15252 token->location);
15254 /* If the lookup was ambiguous, an error will already have been
15255 issued. */
15256 if (ambiguous_decls)
15257 return error_mark_node;
15259 /* If we are parsing friend declaration, DECL may be a
15260 TEMPLATE_DECL tree node here. However, we need to check
15261 whether this TEMPLATE_DECL results in valid code. Consider
15262 the following example:
15264 namespace N {
15265 template <class T> class C {};
15267 class X {
15268 template <class T> friend class N::C; // #1, valid code
15270 template <class T> class Y {
15271 friend class N::C; // #2, invalid code
15274 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15275 name lookup of `N::C'. We see that friend declaration must
15276 be template for the code to be valid. Note that
15277 processing_template_decl does not work here since it is
15278 always 1 for the above two cases. */
15280 decl = (cp_parser_maybe_treat_template_as_class
15281 (decl, /*tag_name_p=*/is_friend
15282 && template_parm_lists_apply));
15284 if (TREE_CODE (decl) != TYPE_DECL)
15286 cp_parser_diagnose_invalid_type_name (parser,
15287 identifier,
15288 token->location);
15289 return error_mark_node;
15292 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15294 bool allow_template = (template_parm_lists_apply
15295 || DECL_SELF_REFERENCE_P (decl));
15296 type = check_elaborated_type_specifier (tag_type, decl,
15297 allow_template);
15299 if (type == error_mark_node)
15300 return error_mark_node;
15303 /* Forward declarations of nested types, such as
15305 class C1::C2;
15306 class C1::C2::C3;
15308 are invalid unless all components preceding the final '::'
15309 are complete. If all enclosing types are complete, these
15310 declarations become merely pointless.
15312 Invalid forward declarations of nested types are errors
15313 caught elsewhere in parsing. Those that are pointless arrive
15314 here. */
15316 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15317 && !is_friend && !processing_explicit_instantiation)
15318 warning (0, "declaration %qD does not declare anything", decl);
15320 type = TREE_TYPE (decl);
15322 else
15324 /* An elaborated-type-specifier sometimes introduces a new type and
15325 sometimes names an existing type. Normally, the rule is that it
15326 introduces a new type only if there is not an existing type of
15327 the same name already in scope. For example, given:
15329 struct S {};
15330 void f() { struct S s; }
15332 the `struct S' in the body of `f' is the same `struct S' as in
15333 the global scope; the existing definition is used. However, if
15334 there were no global declaration, this would introduce a new
15335 local class named `S'.
15337 An exception to this rule applies to the following code:
15339 namespace N { struct S; }
15341 Here, the elaborated-type-specifier names a new type
15342 unconditionally; even if there is already an `S' in the
15343 containing scope this declaration names a new type.
15344 This exception only applies if the elaborated-type-specifier
15345 forms the complete declaration:
15347 [class.name]
15349 A declaration consisting solely of `class-key identifier ;' is
15350 either a redeclaration of the name in the current scope or a
15351 forward declaration of the identifier as a class name. It
15352 introduces the name into the current scope.
15354 We are in this situation precisely when the next token is a `;'.
15356 An exception to the exception is that a `friend' declaration does
15357 *not* name a new type; i.e., given:
15359 struct S { friend struct T; };
15361 `T' is not a new type in the scope of `S'.
15363 Also, `new struct S' or `sizeof (struct S)' never results in the
15364 definition of a new type; a new type can only be declared in a
15365 declaration context. */
15367 tag_scope ts;
15368 bool template_p;
15370 if (is_friend)
15371 /* Friends have special name lookup rules. */
15372 ts = ts_within_enclosing_non_class;
15373 else if (is_declaration
15374 && cp_lexer_next_token_is (parser->lexer,
15375 CPP_SEMICOLON))
15376 /* This is a `class-key identifier ;' */
15377 ts = ts_current;
15378 else
15379 ts = ts_global;
15381 template_p =
15382 (template_parm_lists_apply
15383 && (cp_parser_next_token_starts_class_definition_p (parser)
15384 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15385 /* An unqualified name was used to reference this type, so
15386 there were no qualifying templates. */
15387 if (template_parm_lists_apply
15388 && !cp_parser_check_template_parameters (parser,
15389 /*num_templates=*/0,
15390 token->location,
15391 /*declarator=*/NULL))
15392 return error_mark_node;
15393 type = xref_tag (tag_type, identifier, ts, template_p);
15397 if (type == error_mark_node)
15398 return error_mark_node;
15400 /* Allow attributes on forward declarations of classes. */
15401 if (attributes)
15403 if (TREE_CODE (type) == TYPENAME_TYPE)
15404 warning (OPT_Wattributes,
15405 "attributes ignored on uninstantiated type");
15406 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15407 && ! processing_explicit_instantiation)
15408 warning (OPT_Wattributes,
15409 "attributes ignored on template instantiation");
15410 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15411 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15412 else
15413 warning (OPT_Wattributes,
15414 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15417 if (tag_type != enum_type)
15419 /* Indicate whether this class was declared as a `class' or as a
15420 `struct'. */
15421 if (TREE_CODE (type) == RECORD_TYPE)
15422 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15423 cp_parser_check_class_key (tag_type, type);
15426 /* A "<" cannot follow an elaborated type specifier. If that
15427 happens, the user was probably trying to form a template-id. */
15428 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15429 token->location);
15431 return type;
15434 /* Parse an enum-specifier.
15436 enum-specifier:
15437 enum-head { enumerator-list [opt] }
15438 enum-head { enumerator-list , } [C++0x]
15440 enum-head:
15441 enum-key identifier [opt] enum-base [opt]
15442 enum-key nested-name-specifier identifier enum-base [opt]
15444 enum-key:
15445 enum
15446 enum class [C++0x]
15447 enum struct [C++0x]
15449 enum-base: [C++0x]
15450 : type-specifier-seq
15452 opaque-enum-specifier:
15453 enum-key identifier enum-base [opt] ;
15455 GNU Extensions:
15456 enum-key attributes[opt] identifier [opt] enum-base [opt]
15457 { enumerator-list [opt] }attributes[opt]
15458 enum-key attributes[opt] identifier [opt] enum-base [opt]
15459 { enumerator-list, }attributes[opt] [C++0x]
15461 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15462 if the token stream isn't an enum-specifier after all. */
15464 static tree
15465 cp_parser_enum_specifier (cp_parser* parser)
15467 tree identifier;
15468 tree type = NULL_TREE;
15469 tree prev_scope;
15470 tree nested_name_specifier = NULL_TREE;
15471 tree attributes;
15472 bool scoped_enum_p = false;
15473 bool has_underlying_type = false;
15474 bool nested_being_defined = false;
15475 bool new_value_list = false;
15476 bool is_new_type = false;
15477 bool is_anonymous = false;
15478 tree underlying_type = NULL_TREE;
15479 cp_token *type_start_token = NULL;
15480 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15482 parser->colon_corrects_to_scope_p = false;
15484 /* Parse tentatively so that we can back up if we don't find a
15485 enum-specifier. */
15486 cp_parser_parse_tentatively (parser);
15488 /* Caller guarantees that the current token is 'enum', an identifier
15489 possibly follows, and the token after that is an opening brace.
15490 If we don't have an identifier, fabricate an anonymous name for
15491 the enumeration being defined. */
15492 cp_lexer_consume_token (parser->lexer);
15494 /* Parse the "class" or "struct", which indicates a scoped
15495 enumeration type in C++0x. */
15496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15497 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15499 if (cxx_dialect < cxx11)
15500 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15502 /* Consume the `struct' or `class' token. */
15503 cp_lexer_consume_token (parser->lexer);
15505 scoped_enum_p = true;
15508 attributes = cp_parser_attributes_opt (parser);
15510 /* Clear the qualification. */
15511 parser->scope = NULL_TREE;
15512 parser->qualifying_scope = NULL_TREE;
15513 parser->object_scope = NULL_TREE;
15515 /* Figure out in what scope the declaration is being placed. */
15516 prev_scope = current_scope ();
15518 type_start_token = cp_lexer_peek_token (parser->lexer);
15520 push_deferring_access_checks (dk_no_check);
15521 nested_name_specifier
15522 = cp_parser_nested_name_specifier_opt (parser,
15523 /*typename_keyword_p=*/true,
15524 /*check_dependency_p=*/false,
15525 /*type_p=*/false,
15526 /*is_declaration=*/false);
15528 if (nested_name_specifier)
15530 tree name;
15532 identifier = cp_parser_identifier (parser);
15533 name = cp_parser_lookup_name (parser, identifier,
15534 enum_type,
15535 /*is_template=*/false,
15536 /*is_namespace=*/false,
15537 /*check_dependency=*/true,
15538 /*ambiguous_decls=*/NULL,
15539 input_location);
15540 if (name && name != error_mark_node)
15542 type = TREE_TYPE (name);
15543 if (TREE_CODE (type) == TYPENAME_TYPE)
15545 /* Are template enums allowed in ISO? */
15546 if (template_parm_scope_p ())
15547 pedwarn (type_start_token->location, OPT_Wpedantic,
15548 "%qD is an enumeration template", name);
15549 /* ignore a typename reference, for it will be solved by name
15550 in start_enum. */
15551 type = NULL_TREE;
15554 else if (nested_name_specifier == error_mark_node)
15555 /* We already issued an error. */;
15556 else
15557 error_at (type_start_token->location,
15558 "%qD is not an enumerator-name", identifier);
15560 else
15562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15563 identifier = cp_parser_identifier (parser);
15564 else
15566 identifier = make_anon_name ();
15567 is_anonymous = true;
15568 if (scoped_enum_p)
15569 error_at (type_start_token->location,
15570 "anonymous scoped enum is not allowed");
15573 pop_deferring_access_checks ();
15575 /* Check for the `:' that denotes a specified underlying type in C++0x.
15576 Note that a ':' could also indicate a bitfield width, however. */
15577 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15579 cp_decl_specifier_seq type_specifiers;
15581 /* Consume the `:'. */
15582 cp_lexer_consume_token (parser->lexer);
15584 /* Parse the type-specifier-seq. */
15585 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15586 /*is_trailing_return=*/false,
15587 &type_specifiers);
15589 /* At this point this is surely not elaborated type specifier. */
15590 if (!cp_parser_parse_definitely (parser))
15591 return NULL_TREE;
15593 if (cxx_dialect < cxx11)
15594 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15596 has_underlying_type = true;
15598 /* If that didn't work, stop. */
15599 if (type_specifiers.type != error_mark_node)
15601 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15602 /*initialized=*/0, NULL);
15603 if (underlying_type == error_mark_node
15604 || check_for_bare_parameter_packs (underlying_type))
15605 underlying_type = NULL_TREE;
15609 /* Look for the `{' but don't consume it yet. */
15610 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15612 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15614 cp_parser_error (parser, "expected %<{%>");
15615 if (has_underlying_type)
15617 type = NULL_TREE;
15618 goto out;
15621 /* An opaque-enum-specifier must have a ';' here. */
15622 if ((scoped_enum_p || underlying_type)
15623 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15625 cp_parser_error (parser, "expected %<;%> or %<{%>");
15626 if (has_underlying_type)
15628 type = NULL_TREE;
15629 goto out;
15634 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15635 return NULL_TREE;
15637 if (nested_name_specifier)
15639 if (CLASS_TYPE_P (nested_name_specifier))
15641 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15642 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15643 push_scope (nested_name_specifier);
15645 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15647 push_nested_namespace (nested_name_specifier);
15651 /* Issue an error message if type-definitions are forbidden here. */
15652 if (!cp_parser_check_type_definition (parser))
15653 type = error_mark_node;
15654 else
15655 /* Create the new type. We do this before consuming the opening
15656 brace so the enum will be recorded as being on the line of its
15657 tag (or the 'enum' keyword, if there is no tag). */
15658 type = start_enum (identifier, type, underlying_type,
15659 scoped_enum_p, &is_new_type);
15661 /* If the next token is not '{' it is an opaque-enum-specifier or an
15662 elaborated-type-specifier. */
15663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15665 timevar_push (TV_PARSE_ENUM);
15666 if (nested_name_specifier
15667 && nested_name_specifier != error_mark_node)
15669 /* The following catches invalid code such as:
15670 enum class S<int>::E { A, B, C }; */
15671 if (!processing_specialization
15672 && CLASS_TYPE_P (nested_name_specifier)
15673 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15674 error_at (type_start_token->location, "cannot add an enumerator "
15675 "list to a template instantiation");
15677 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15679 error_at (type_start_token->location,
15680 "%<%T::%E%> has not been declared",
15681 TYPE_CONTEXT (nested_name_specifier),
15682 nested_name_specifier);
15683 type = error_mark_node;
15685 /* If that scope does not contain the scope in which the
15686 class was originally declared, the program is invalid. */
15687 else if (prev_scope && !is_ancestor (prev_scope,
15688 nested_name_specifier))
15690 if (at_namespace_scope_p ())
15691 error_at (type_start_token->location,
15692 "declaration of %qD in namespace %qD which does not "
15693 "enclose %qD",
15694 type, prev_scope, nested_name_specifier);
15695 else
15696 error_at (type_start_token->location,
15697 "declaration of %qD in %qD which does not "
15698 "enclose %qD",
15699 type, prev_scope, nested_name_specifier);
15700 type = error_mark_node;
15704 if (scoped_enum_p)
15705 begin_scope (sk_scoped_enum, type);
15707 /* Consume the opening brace. */
15708 cp_lexer_consume_token (parser->lexer);
15710 if (type == error_mark_node)
15711 ; /* Nothing to add */
15712 else if (OPAQUE_ENUM_P (type)
15713 || (cxx_dialect > cxx98 && processing_specialization))
15715 new_value_list = true;
15716 SET_OPAQUE_ENUM_P (type, false);
15717 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15719 else
15721 error_at (type_start_token->location,
15722 "multiple definition of %q#T", type);
15723 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15724 "previous definition here");
15725 type = error_mark_node;
15728 if (type == error_mark_node)
15729 cp_parser_skip_to_end_of_block_or_statement (parser);
15730 /* If the next token is not '}', then there are some enumerators. */
15731 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15733 if (is_anonymous && !scoped_enum_p)
15734 pedwarn (type_start_token->location, OPT_Wpedantic,
15735 "ISO C++ forbids empty anonymous enum");
15737 else
15738 cp_parser_enumerator_list (parser, type);
15740 /* Consume the final '}'. */
15741 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15743 if (scoped_enum_p)
15744 finish_scope ();
15745 timevar_pop (TV_PARSE_ENUM);
15747 else
15749 /* If a ';' follows, then it is an opaque-enum-specifier
15750 and additional restrictions apply. */
15751 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15753 if (is_anonymous)
15754 error_at (type_start_token->location,
15755 "opaque-enum-specifier without name");
15756 else if (nested_name_specifier)
15757 error_at (type_start_token->location,
15758 "opaque-enum-specifier must use a simple identifier");
15762 /* Look for trailing attributes to apply to this enumeration, and
15763 apply them if appropriate. */
15764 if (cp_parser_allow_gnu_extensions_p (parser))
15766 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15767 trailing_attr = chainon (trailing_attr, attributes);
15768 cplus_decl_attributes (&type,
15769 trailing_attr,
15770 (int) ATTR_FLAG_TYPE_IN_PLACE);
15773 /* Finish up the enumeration. */
15774 if (type != error_mark_node)
15776 if (new_value_list)
15777 finish_enum_value_list (type);
15778 if (is_new_type)
15779 finish_enum (type);
15782 if (nested_name_specifier)
15784 if (CLASS_TYPE_P (nested_name_specifier))
15786 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15787 pop_scope (nested_name_specifier);
15789 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15791 pop_nested_namespace (nested_name_specifier);
15794 out:
15795 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15796 return type;
15799 /* Parse an enumerator-list. The enumerators all have the indicated
15800 TYPE.
15802 enumerator-list:
15803 enumerator-definition
15804 enumerator-list , enumerator-definition */
15806 static void
15807 cp_parser_enumerator_list (cp_parser* parser, tree type)
15809 while (true)
15811 /* Parse an enumerator-definition. */
15812 cp_parser_enumerator_definition (parser, type);
15814 /* If the next token is not a ',', we've reached the end of
15815 the list. */
15816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15817 break;
15818 /* Otherwise, consume the `,' and keep going. */
15819 cp_lexer_consume_token (parser->lexer);
15820 /* If the next token is a `}', there is a trailing comma. */
15821 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15823 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15824 pedwarn (input_location, OPT_Wpedantic,
15825 "comma at end of enumerator list");
15826 break;
15831 /* Parse an enumerator-definition. The enumerator has the indicated
15832 TYPE.
15834 enumerator-definition:
15835 enumerator
15836 enumerator = constant-expression
15838 enumerator:
15839 identifier */
15841 static void
15842 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15844 tree identifier;
15845 tree value;
15846 location_t loc;
15848 /* Save the input location because we are interested in the location
15849 of the identifier and not the location of the explicit value. */
15850 loc = cp_lexer_peek_token (parser->lexer)->location;
15852 /* Look for the identifier. */
15853 identifier = cp_parser_identifier (parser);
15854 if (identifier == error_mark_node)
15855 return;
15857 /* If the next token is an '=', then there is an explicit value. */
15858 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15860 /* Consume the `=' token. */
15861 cp_lexer_consume_token (parser->lexer);
15862 /* Parse the value. */
15863 value = cp_parser_constant_expression (parser,
15864 /*allow_non_constant_p=*/false,
15865 NULL);
15867 else
15868 value = NULL_TREE;
15870 /* If we are processing a template, make sure the initializer of the
15871 enumerator doesn't contain any bare template parameter pack. */
15872 if (check_for_bare_parameter_packs (value))
15873 value = error_mark_node;
15875 /* integral_constant_value will pull out this expression, so make sure
15876 it's folded as appropriate. */
15877 value = fold_non_dependent_expr (value);
15879 /* Create the enumerator. */
15880 build_enumerator (identifier, value, type, loc);
15883 /* Parse a namespace-name.
15885 namespace-name:
15886 original-namespace-name
15887 namespace-alias
15889 Returns the NAMESPACE_DECL for the namespace. */
15891 static tree
15892 cp_parser_namespace_name (cp_parser* parser)
15894 tree identifier;
15895 tree namespace_decl;
15897 cp_token *token = cp_lexer_peek_token (parser->lexer);
15899 /* Get the name of the namespace. */
15900 identifier = cp_parser_identifier (parser);
15901 if (identifier == error_mark_node)
15902 return error_mark_node;
15904 /* Look up the identifier in the currently active scope. Look only
15905 for namespaces, due to:
15907 [basic.lookup.udir]
15909 When looking up a namespace-name in a using-directive or alias
15910 definition, only namespace names are considered.
15912 And:
15914 [basic.lookup.qual]
15916 During the lookup of a name preceding the :: scope resolution
15917 operator, object, function, and enumerator names are ignored.
15919 (Note that cp_parser_qualifying_entity only calls this
15920 function if the token after the name is the scope resolution
15921 operator.) */
15922 namespace_decl = cp_parser_lookup_name (parser, identifier,
15923 none_type,
15924 /*is_template=*/false,
15925 /*is_namespace=*/true,
15926 /*check_dependency=*/true,
15927 /*ambiguous_decls=*/NULL,
15928 token->location);
15929 /* If it's not a namespace, issue an error. */
15930 if (namespace_decl == error_mark_node
15931 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15933 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15934 error_at (token->location, "%qD is not a namespace-name", identifier);
15935 cp_parser_error (parser, "expected namespace-name");
15936 namespace_decl = error_mark_node;
15939 return namespace_decl;
15942 /* Parse a namespace-definition.
15944 namespace-definition:
15945 named-namespace-definition
15946 unnamed-namespace-definition
15948 named-namespace-definition:
15949 original-namespace-definition
15950 extension-namespace-definition
15952 original-namespace-definition:
15953 namespace identifier { namespace-body }
15955 extension-namespace-definition:
15956 namespace original-namespace-name { namespace-body }
15958 unnamed-namespace-definition:
15959 namespace { namespace-body } */
15961 static void
15962 cp_parser_namespace_definition (cp_parser* parser)
15964 tree identifier, attribs;
15965 bool has_visibility;
15966 bool is_inline;
15968 cp_ensure_no_omp_declare_simd (parser);
15969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15971 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15972 is_inline = true;
15973 cp_lexer_consume_token (parser->lexer);
15975 else
15976 is_inline = false;
15978 /* Look for the `namespace' keyword. */
15979 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15981 /* Get the name of the namespace. We do not attempt to distinguish
15982 between an original-namespace-definition and an
15983 extension-namespace-definition at this point. The semantic
15984 analysis routines are responsible for that. */
15985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15986 identifier = cp_parser_identifier (parser);
15987 else
15988 identifier = NULL_TREE;
15990 /* Parse any specified attributes. */
15991 attribs = cp_parser_attributes_opt (parser);
15993 /* Look for the `{' to start the namespace. */
15994 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15995 /* Start the namespace. */
15996 push_namespace (identifier);
15998 /* "inline namespace" is equivalent to a stub namespace definition
15999 followed by a strong using directive. */
16000 if (is_inline)
16002 tree name_space = current_namespace;
16003 /* Set up namespace association. */
16004 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16005 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16006 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16007 /* Import the contents of the inline namespace. */
16008 pop_namespace ();
16009 do_using_directive (name_space);
16010 push_namespace (identifier);
16013 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16015 /* Parse the body of the namespace. */
16016 cp_parser_namespace_body (parser);
16018 if (has_visibility)
16019 pop_visibility (1);
16021 /* Finish the namespace. */
16022 pop_namespace ();
16023 /* Look for the final `}'. */
16024 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16027 /* Parse a namespace-body.
16029 namespace-body:
16030 declaration-seq [opt] */
16032 static void
16033 cp_parser_namespace_body (cp_parser* parser)
16035 cp_parser_declaration_seq_opt (parser);
16038 /* Parse a namespace-alias-definition.
16040 namespace-alias-definition:
16041 namespace identifier = qualified-namespace-specifier ; */
16043 static void
16044 cp_parser_namespace_alias_definition (cp_parser* parser)
16046 tree identifier;
16047 tree namespace_specifier;
16049 cp_token *token = cp_lexer_peek_token (parser->lexer);
16051 /* Look for the `namespace' keyword. */
16052 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16053 /* Look for the identifier. */
16054 identifier = cp_parser_identifier (parser);
16055 if (identifier == error_mark_node)
16056 return;
16057 /* Look for the `=' token. */
16058 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16059 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16061 error_at (token->location, "%<namespace%> definition is not allowed here");
16062 /* Skip the definition. */
16063 cp_lexer_consume_token (parser->lexer);
16064 if (cp_parser_skip_to_closing_brace (parser))
16065 cp_lexer_consume_token (parser->lexer);
16066 return;
16068 cp_parser_require (parser, CPP_EQ, RT_EQ);
16069 /* Look for the qualified-namespace-specifier. */
16070 namespace_specifier
16071 = cp_parser_qualified_namespace_specifier (parser);
16072 /* Look for the `;' token. */
16073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16075 /* Register the alias in the symbol table. */
16076 do_namespace_alias (identifier, namespace_specifier);
16079 /* Parse a qualified-namespace-specifier.
16081 qualified-namespace-specifier:
16082 :: [opt] nested-name-specifier [opt] namespace-name
16084 Returns a NAMESPACE_DECL corresponding to the specified
16085 namespace. */
16087 static tree
16088 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16090 /* Look for the optional `::'. */
16091 cp_parser_global_scope_opt (parser,
16092 /*current_scope_valid_p=*/false);
16094 /* Look for the optional nested-name-specifier. */
16095 cp_parser_nested_name_specifier_opt (parser,
16096 /*typename_keyword_p=*/false,
16097 /*check_dependency_p=*/true,
16098 /*type_p=*/false,
16099 /*is_declaration=*/true);
16101 return cp_parser_namespace_name (parser);
16104 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16105 access declaration.
16107 using-declaration:
16108 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16109 using :: unqualified-id ;
16111 access-declaration:
16112 qualified-id ;
16116 static bool
16117 cp_parser_using_declaration (cp_parser* parser,
16118 bool access_declaration_p)
16120 cp_token *token;
16121 bool typename_p = false;
16122 bool global_scope_p;
16123 tree decl;
16124 tree identifier;
16125 tree qscope;
16126 int oldcount = errorcount;
16127 cp_token *diag_token = NULL;
16129 if (access_declaration_p)
16131 diag_token = cp_lexer_peek_token (parser->lexer);
16132 cp_parser_parse_tentatively (parser);
16134 else
16136 /* Look for the `using' keyword. */
16137 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16139 /* Peek at the next token. */
16140 token = cp_lexer_peek_token (parser->lexer);
16141 /* See if it's `typename'. */
16142 if (token->keyword == RID_TYPENAME)
16144 /* Remember that we've seen it. */
16145 typename_p = true;
16146 /* Consume the `typename' token. */
16147 cp_lexer_consume_token (parser->lexer);
16151 /* Look for the optional global scope qualification. */
16152 global_scope_p
16153 = (cp_parser_global_scope_opt (parser,
16154 /*current_scope_valid_p=*/false)
16155 != NULL_TREE);
16157 /* If we saw `typename', or didn't see `::', then there must be a
16158 nested-name-specifier present. */
16159 if (typename_p || !global_scope_p)
16161 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16162 /*check_dependency_p=*/true,
16163 /*type_p=*/false,
16164 /*is_declaration=*/true);
16165 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16167 cp_parser_skip_to_end_of_block_or_statement (parser);
16168 return false;
16171 /* Otherwise, we could be in either of the two productions. In that
16172 case, treat the nested-name-specifier as optional. */
16173 else
16174 qscope = cp_parser_nested_name_specifier_opt (parser,
16175 /*typename_keyword_p=*/false,
16176 /*check_dependency_p=*/true,
16177 /*type_p=*/false,
16178 /*is_declaration=*/true);
16179 if (!qscope)
16180 qscope = global_namespace;
16181 else if (UNSCOPED_ENUM_P (qscope))
16182 qscope = CP_TYPE_CONTEXT (qscope);
16184 if (access_declaration_p && cp_parser_error_occurred (parser))
16185 /* Something has already gone wrong; there's no need to parse
16186 further. Since an error has occurred, the return value of
16187 cp_parser_parse_definitely will be false, as required. */
16188 return cp_parser_parse_definitely (parser);
16190 token = cp_lexer_peek_token (parser->lexer);
16191 /* Parse the unqualified-id. */
16192 identifier = cp_parser_unqualified_id (parser,
16193 /*template_keyword_p=*/false,
16194 /*check_dependency_p=*/true,
16195 /*declarator_p=*/true,
16196 /*optional_p=*/false);
16198 if (access_declaration_p)
16200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16201 cp_parser_simulate_error (parser);
16202 if (!cp_parser_parse_definitely (parser))
16203 return false;
16206 /* The function we call to handle a using-declaration is different
16207 depending on what scope we are in. */
16208 if (qscope == error_mark_node || identifier == error_mark_node)
16210 else if (!identifier_p (identifier)
16211 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16212 /* [namespace.udecl]
16214 A using declaration shall not name a template-id. */
16215 error_at (token->location,
16216 "a template-id may not appear in a using-declaration");
16217 else
16219 if (at_class_scope_p ())
16221 /* Create the USING_DECL. */
16222 decl = do_class_using_decl (parser->scope, identifier);
16224 if (decl && typename_p)
16225 USING_DECL_TYPENAME_P (decl) = 1;
16227 if (check_for_bare_parameter_packs (decl))
16229 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16230 return false;
16232 else
16233 /* Add it to the list of members in this class. */
16234 finish_member_declaration (decl);
16236 else
16238 decl = cp_parser_lookup_name_simple (parser,
16239 identifier,
16240 token->location);
16241 if (decl == error_mark_node)
16242 cp_parser_name_lookup_error (parser, identifier,
16243 decl, NLE_NULL,
16244 token->location);
16245 else if (check_for_bare_parameter_packs (decl))
16247 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16248 return false;
16250 else if (!at_namespace_scope_p ())
16251 do_local_using_decl (decl, qscope, identifier);
16252 else
16253 do_toplevel_using_decl (decl, qscope, identifier);
16257 /* Look for the final `;'. */
16258 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16260 if (access_declaration_p && errorcount == oldcount)
16261 warning_at (diag_token->location, OPT_Wdeprecated,
16262 "access declarations are deprecated "
16263 "in favour of using-declarations; "
16264 "suggestion: add the %<using%> keyword");
16266 return true;
16269 /* Parse an alias-declaration.
16271 alias-declaration:
16272 using identifier attribute-specifier-seq [opt] = type-id */
16274 static tree
16275 cp_parser_alias_declaration (cp_parser* parser)
16277 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16278 location_t id_location;
16279 cp_declarator *declarator;
16280 cp_decl_specifier_seq decl_specs;
16281 bool member_p;
16282 const char *saved_message = NULL;
16284 /* Look for the `using' keyword. */
16285 cp_token *using_token
16286 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16287 if (using_token == NULL)
16288 return error_mark_node;
16290 id_location = cp_lexer_peek_token (parser->lexer)->location;
16291 id = cp_parser_identifier (parser);
16292 if (id == error_mark_node)
16293 return error_mark_node;
16295 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16296 attributes = cp_parser_attributes_opt (parser);
16297 if (attributes == error_mark_node)
16298 return error_mark_node;
16300 cp_parser_require (parser, CPP_EQ, RT_EQ);
16302 if (cp_parser_error_occurred (parser))
16303 return error_mark_node;
16305 cp_parser_commit_to_tentative_parse (parser);
16307 /* Now we are going to parse the type-id of the declaration. */
16310 [dcl.type]/3 says:
16312 "A type-specifier-seq shall not define a class or enumeration
16313 unless it appears in the type-id of an alias-declaration (7.1.3) that
16314 is not the declaration of a template-declaration."
16316 In other words, if we currently are in an alias template, the
16317 type-id should not define a type.
16319 So let's set parser->type_definition_forbidden_message in that
16320 case; cp_parser_check_type_definition (called by
16321 cp_parser_class_specifier) will then emit an error if a type is
16322 defined in the type-id. */
16323 if (parser->num_template_parameter_lists)
16325 saved_message = parser->type_definition_forbidden_message;
16326 parser->type_definition_forbidden_message =
16327 G_("types may not be defined in alias template declarations");
16330 type = cp_parser_type_id (parser);
16332 /* Restore the error message if need be. */
16333 if (parser->num_template_parameter_lists)
16334 parser->type_definition_forbidden_message = saved_message;
16336 if (type == error_mark_node
16337 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16339 cp_parser_skip_to_end_of_block_or_statement (parser);
16340 return error_mark_node;
16343 /* A typedef-name can also be introduced by an alias-declaration. The
16344 identifier following the using keyword becomes a typedef-name. It has
16345 the same semantics as if it were introduced by the typedef
16346 specifier. In particular, it does not define a new type and it shall
16347 not appear in the type-id. */
16349 clear_decl_specs (&decl_specs);
16350 decl_specs.type = type;
16351 if (attributes != NULL_TREE)
16353 decl_specs.attributes = attributes;
16354 set_and_check_decl_spec_loc (&decl_specs,
16355 ds_attribute,
16356 attrs_token);
16358 set_and_check_decl_spec_loc (&decl_specs,
16359 ds_typedef,
16360 using_token);
16361 set_and_check_decl_spec_loc (&decl_specs,
16362 ds_alias,
16363 using_token);
16365 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16366 declarator->id_loc = id_location;
16368 member_p = at_class_scope_p ();
16369 if (member_p)
16370 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16371 NULL_TREE, attributes);
16372 else
16373 decl = start_decl (declarator, &decl_specs, 0,
16374 attributes, NULL_TREE, &pushed_scope);
16375 if (decl == error_mark_node)
16376 return decl;
16378 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16380 if (pushed_scope)
16381 pop_scope (pushed_scope);
16383 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16384 added into the symbol table; otherwise, return the TYPE_DECL. */
16385 if (DECL_LANG_SPECIFIC (decl)
16386 && DECL_TEMPLATE_INFO (decl)
16387 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16389 decl = DECL_TI_TEMPLATE (decl);
16390 if (member_p)
16391 check_member_template (decl);
16394 return decl;
16397 /* Parse a using-directive.
16399 using-directive:
16400 using namespace :: [opt] nested-name-specifier [opt]
16401 namespace-name ; */
16403 static void
16404 cp_parser_using_directive (cp_parser* parser)
16406 tree namespace_decl;
16407 tree attribs;
16409 /* Look for the `using' keyword. */
16410 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16411 /* And the `namespace' keyword. */
16412 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16413 /* Look for the optional `::' operator. */
16414 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16415 /* And the optional nested-name-specifier. */
16416 cp_parser_nested_name_specifier_opt (parser,
16417 /*typename_keyword_p=*/false,
16418 /*check_dependency_p=*/true,
16419 /*type_p=*/false,
16420 /*is_declaration=*/true);
16421 /* Get the namespace being used. */
16422 namespace_decl = cp_parser_namespace_name (parser);
16423 /* And any specified attributes. */
16424 attribs = cp_parser_attributes_opt (parser);
16425 /* Update the symbol table. */
16426 parse_using_directive (namespace_decl, attribs);
16427 /* Look for the final `;'. */
16428 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16431 /* Parse an asm-definition.
16433 asm-definition:
16434 asm ( string-literal ) ;
16436 GNU Extension:
16438 asm-definition:
16439 asm volatile [opt] ( string-literal ) ;
16440 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16441 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16442 : asm-operand-list [opt] ) ;
16443 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16444 : asm-operand-list [opt]
16445 : asm-clobber-list [opt] ) ;
16446 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16447 : asm-clobber-list [opt]
16448 : asm-goto-list ) ; */
16450 static void
16451 cp_parser_asm_definition (cp_parser* parser)
16453 tree string;
16454 tree outputs = NULL_TREE;
16455 tree inputs = NULL_TREE;
16456 tree clobbers = NULL_TREE;
16457 tree labels = NULL_TREE;
16458 tree asm_stmt;
16459 bool volatile_p = false;
16460 bool extended_p = false;
16461 bool invalid_inputs_p = false;
16462 bool invalid_outputs_p = false;
16463 bool goto_p = false;
16464 required_token missing = RT_NONE;
16466 /* Look for the `asm' keyword. */
16467 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16468 /* See if the next token is `volatile'. */
16469 if (cp_parser_allow_gnu_extensions_p (parser)
16470 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16472 /* Remember that we saw the `volatile' keyword. */
16473 volatile_p = true;
16474 /* Consume the token. */
16475 cp_lexer_consume_token (parser->lexer);
16477 if (cp_parser_allow_gnu_extensions_p (parser)
16478 && parser->in_function_body
16479 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16481 /* Remember that we saw the `goto' keyword. */
16482 goto_p = true;
16483 /* Consume the token. */
16484 cp_lexer_consume_token (parser->lexer);
16486 /* Look for the opening `('. */
16487 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16488 return;
16489 /* Look for the string. */
16490 string = cp_parser_string_literal (parser, false, false);
16491 if (string == error_mark_node)
16493 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16494 /*consume_paren=*/true);
16495 return;
16498 /* If we're allowing GNU extensions, check for the extended assembly
16499 syntax. Unfortunately, the `:' tokens need not be separated by
16500 a space in C, and so, for compatibility, we tolerate that here
16501 too. Doing that means that we have to treat the `::' operator as
16502 two `:' tokens. */
16503 if (cp_parser_allow_gnu_extensions_p (parser)
16504 && parser->in_function_body
16505 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16506 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16508 bool inputs_p = false;
16509 bool clobbers_p = false;
16510 bool labels_p = false;
16512 /* The extended syntax was used. */
16513 extended_p = true;
16515 /* Look for outputs. */
16516 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16518 /* Consume the `:'. */
16519 cp_lexer_consume_token (parser->lexer);
16520 /* Parse the output-operands. */
16521 if (cp_lexer_next_token_is_not (parser->lexer,
16522 CPP_COLON)
16523 && cp_lexer_next_token_is_not (parser->lexer,
16524 CPP_SCOPE)
16525 && cp_lexer_next_token_is_not (parser->lexer,
16526 CPP_CLOSE_PAREN)
16527 && !goto_p)
16528 outputs = cp_parser_asm_operand_list (parser);
16530 if (outputs == error_mark_node)
16531 invalid_outputs_p = true;
16533 /* If the next token is `::', there are no outputs, and the
16534 next token is the beginning of the inputs. */
16535 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16536 /* The inputs are coming next. */
16537 inputs_p = true;
16539 /* Look for inputs. */
16540 if (inputs_p
16541 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16543 /* Consume the `:' or `::'. */
16544 cp_lexer_consume_token (parser->lexer);
16545 /* Parse the output-operands. */
16546 if (cp_lexer_next_token_is_not (parser->lexer,
16547 CPP_COLON)
16548 && cp_lexer_next_token_is_not (parser->lexer,
16549 CPP_SCOPE)
16550 && cp_lexer_next_token_is_not (parser->lexer,
16551 CPP_CLOSE_PAREN))
16552 inputs = cp_parser_asm_operand_list (parser);
16554 if (inputs == error_mark_node)
16555 invalid_inputs_p = true;
16557 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16558 /* The clobbers are coming next. */
16559 clobbers_p = true;
16561 /* Look for clobbers. */
16562 if (clobbers_p
16563 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16565 clobbers_p = true;
16566 /* Consume the `:' or `::'. */
16567 cp_lexer_consume_token (parser->lexer);
16568 /* Parse the clobbers. */
16569 if (cp_lexer_next_token_is_not (parser->lexer,
16570 CPP_COLON)
16571 && cp_lexer_next_token_is_not (parser->lexer,
16572 CPP_CLOSE_PAREN))
16573 clobbers = cp_parser_asm_clobber_list (parser);
16575 else if (goto_p
16576 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16577 /* The labels are coming next. */
16578 labels_p = true;
16580 /* Look for labels. */
16581 if (labels_p
16582 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16584 labels_p = true;
16585 /* Consume the `:' or `::'. */
16586 cp_lexer_consume_token (parser->lexer);
16587 /* Parse the labels. */
16588 labels = cp_parser_asm_label_list (parser);
16591 if (goto_p && !labels_p)
16592 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16594 else if (goto_p)
16595 missing = RT_COLON_SCOPE;
16597 /* Look for the closing `)'. */
16598 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16599 missing ? missing : RT_CLOSE_PAREN))
16600 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16601 /*consume_paren=*/true);
16602 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16604 if (!invalid_inputs_p && !invalid_outputs_p)
16606 /* Create the ASM_EXPR. */
16607 if (parser->in_function_body)
16609 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16610 inputs, clobbers, labels);
16611 /* If the extended syntax was not used, mark the ASM_EXPR. */
16612 if (!extended_p)
16614 tree temp = asm_stmt;
16615 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16616 temp = TREE_OPERAND (temp, 0);
16618 ASM_INPUT_P (temp) = 1;
16621 else
16622 symtab->finalize_toplevel_asm (string);
16626 /* Declarators [gram.dcl.decl] */
16628 /* Parse an init-declarator.
16630 init-declarator:
16631 declarator initializer [opt]
16633 GNU Extension:
16635 init-declarator:
16636 declarator asm-specification [opt] attributes [opt] initializer [opt]
16638 function-definition:
16639 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16640 function-body
16641 decl-specifier-seq [opt] declarator function-try-block
16643 GNU Extension:
16645 function-definition:
16646 __extension__ function-definition
16648 TM Extension:
16650 function-definition:
16651 decl-specifier-seq [opt] declarator function-transaction-block
16653 The DECL_SPECIFIERS apply to this declarator. Returns a
16654 representation of the entity declared. If MEMBER_P is TRUE, then
16655 this declarator appears in a class scope. The new DECL created by
16656 this declarator is returned.
16658 The CHECKS are access checks that should be performed once we know
16659 what entity is being declared (and, therefore, what classes have
16660 befriended it).
16662 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16663 for a function-definition here as well. If the declarator is a
16664 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16665 be TRUE upon return. By that point, the function-definition will
16666 have been completely parsed.
16668 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16669 is FALSE.
16671 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16672 parsed declaration if it is an uninitialized single declarator not followed
16673 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16674 if present, will not be consumed. If returned, this declarator will be
16675 created with SD_INITIALIZED but will not call cp_finish_decl. */
16677 static tree
16678 cp_parser_init_declarator (cp_parser* parser,
16679 cp_decl_specifier_seq *decl_specifiers,
16680 vec<deferred_access_check, va_gc> *checks,
16681 bool function_definition_allowed_p,
16682 bool member_p,
16683 int declares_class_or_enum,
16684 bool* function_definition_p,
16685 tree* maybe_range_for_decl)
16687 cp_token *token = NULL, *asm_spec_start_token = NULL,
16688 *attributes_start_token = NULL;
16689 cp_declarator *declarator;
16690 tree prefix_attributes;
16691 tree attributes = NULL;
16692 tree asm_specification;
16693 tree initializer;
16694 tree decl = NULL_TREE;
16695 tree scope;
16696 int is_initialized;
16697 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16698 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16699 "(...)". */
16700 enum cpp_ttype initialization_kind;
16701 bool is_direct_init = false;
16702 bool is_non_constant_init;
16703 int ctor_dtor_or_conv_p;
16704 bool friend_p = cp_parser_friend_p (decl_specifiers);
16705 tree pushed_scope = NULL_TREE;
16706 bool range_for_decl_p = false;
16707 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16709 /* Gather the attributes that were provided with the
16710 decl-specifiers. */
16711 prefix_attributes = decl_specifiers->attributes;
16713 /* Assume that this is not the declarator for a function
16714 definition. */
16715 if (function_definition_p)
16716 *function_definition_p = false;
16718 /* Default arguments are only permitted for function parameters. */
16719 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16720 parser->default_arg_ok_p = false;
16722 /* Defer access checks while parsing the declarator; we cannot know
16723 what names are accessible until we know what is being
16724 declared. */
16725 resume_deferring_access_checks ();
16727 /* Parse the declarator. */
16728 token = cp_lexer_peek_token (parser->lexer);
16729 declarator
16730 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16731 &ctor_dtor_or_conv_p,
16732 /*parenthesized_p=*/NULL,
16733 member_p, friend_p);
16734 /* Gather up the deferred checks. */
16735 stop_deferring_access_checks ();
16737 parser->default_arg_ok_p = saved_default_arg_ok_p;
16739 /* If the DECLARATOR was erroneous, there's no need to go
16740 further. */
16741 if (declarator == cp_error_declarator)
16742 return error_mark_node;
16744 /* Check that the number of template-parameter-lists is OK. */
16745 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16746 token->location))
16747 return error_mark_node;
16749 if (declares_class_or_enum & 2)
16750 cp_parser_check_for_definition_in_return_type (declarator,
16751 decl_specifiers->type,
16752 decl_specifiers->locations[ds_type_spec]);
16754 /* Figure out what scope the entity declared by the DECLARATOR is
16755 located in. `grokdeclarator' sometimes changes the scope, so
16756 we compute it now. */
16757 scope = get_scope_of_declarator (declarator);
16759 /* Perform any lookups in the declared type which were thought to be
16760 dependent, but are not in the scope of the declarator. */
16761 decl_specifiers->type
16762 = maybe_update_decl_type (decl_specifiers->type, scope);
16764 /* If we're allowing GNU extensions, look for an
16765 asm-specification. */
16766 if (cp_parser_allow_gnu_extensions_p (parser))
16768 /* Look for an asm-specification. */
16769 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16770 asm_specification = cp_parser_asm_specification_opt (parser);
16772 else
16773 asm_specification = NULL_TREE;
16775 /* Look for attributes. */
16776 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16777 attributes = cp_parser_attributes_opt (parser);
16779 /* Peek at the next token. */
16780 token = cp_lexer_peek_token (parser->lexer);
16782 bool bogus_implicit_tmpl = false;
16784 if (function_declarator_p (declarator))
16786 /* Check to see if the token indicates the start of a
16787 function-definition. */
16788 if (cp_parser_token_starts_function_definition_p (token))
16790 if (!function_definition_allowed_p)
16792 /* If a function-definition should not appear here, issue an
16793 error message. */
16794 cp_parser_error (parser,
16795 "a function-definition is not allowed here");
16796 return error_mark_node;
16799 location_t func_brace_location
16800 = cp_lexer_peek_token (parser->lexer)->location;
16802 /* Neither attributes nor an asm-specification are allowed
16803 on a function-definition. */
16804 if (asm_specification)
16805 error_at (asm_spec_start_token->location,
16806 "an asm-specification is not allowed "
16807 "on a function-definition");
16808 if (attributes)
16809 error_at (attributes_start_token->location,
16810 "attributes are not allowed "
16811 "on a function-definition");
16812 /* This is a function-definition. */
16813 *function_definition_p = true;
16815 /* Parse the function definition. */
16816 if (member_p)
16817 decl = cp_parser_save_member_function_body (parser,
16818 decl_specifiers,
16819 declarator,
16820 prefix_attributes);
16821 else
16822 decl =
16823 (cp_parser_function_definition_from_specifiers_and_declarator
16824 (parser, decl_specifiers, prefix_attributes, declarator));
16826 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16828 /* This is where the prologue starts... */
16829 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16830 = func_brace_location;
16833 return decl;
16836 else if (parser->fully_implicit_function_template_p)
16838 /* A non-template declaration involving a function parameter list
16839 containing an implicit template parameter will be made into a
16840 template. If the resulting declaration is not going to be an
16841 actual function then finish the template scope here to prevent it.
16842 An error message will be issued once we have a decl to talk about.
16844 FIXME probably we should do type deduction rather than create an
16845 implicit template, but the standard currently doesn't allow it. */
16846 bogus_implicit_tmpl = true;
16847 finish_fully_implicit_template (parser, NULL_TREE);
16850 /* [dcl.dcl]
16852 Only in function declarations for constructors, destructors, and
16853 type conversions can the decl-specifier-seq be omitted.
16855 We explicitly postpone this check past the point where we handle
16856 function-definitions because we tolerate function-definitions
16857 that are missing their return types in some modes. */
16858 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16860 cp_parser_error (parser,
16861 "expected constructor, destructor, or type conversion");
16862 return error_mark_node;
16865 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16866 if (token->type == CPP_EQ
16867 || token->type == CPP_OPEN_PAREN
16868 || token->type == CPP_OPEN_BRACE)
16870 is_initialized = SD_INITIALIZED;
16871 initialization_kind = token->type;
16872 if (maybe_range_for_decl)
16873 *maybe_range_for_decl = error_mark_node;
16875 if (token->type == CPP_EQ
16876 && function_declarator_p (declarator))
16878 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16879 if (t2->keyword == RID_DEFAULT)
16880 is_initialized = SD_DEFAULTED;
16881 else if (t2->keyword == RID_DELETE)
16882 is_initialized = SD_DELETED;
16885 else
16887 /* If the init-declarator isn't initialized and isn't followed by a
16888 `,' or `;', it's not a valid init-declarator. */
16889 if (token->type != CPP_COMMA
16890 && token->type != CPP_SEMICOLON)
16892 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16893 range_for_decl_p = true;
16894 else
16896 cp_parser_error (parser, "expected initializer");
16897 return error_mark_node;
16900 is_initialized = SD_UNINITIALIZED;
16901 initialization_kind = CPP_EOF;
16904 /* Because start_decl has side-effects, we should only call it if we
16905 know we're going ahead. By this point, we know that we cannot
16906 possibly be looking at any other construct. */
16907 cp_parser_commit_to_tentative_parse (parser);
16909 /* Enter the newly declared entry in the symbol table. If we're
16910 processing a declaration in a class-specifier, we wait until
16911 after processing the initializer. */
16912 if (!member_p)
16914 if (parser->in_unbraced_linkage_specification_p)
16915 decl_specifiers->storage_class = sc_extern;
16916 decl = start_decl (declarator, decl_specifiers,
16917 range_for_decl_p? SD_INITIALIZED : is_initialized,
16918 attributes, prefix_attributes, &pushed_scope);
16919 cp_finalize_omp_declare_simd (parser, decl);
16920 /* Adjust location of decl if declarator->id_loc is more appropriate:
16921 set, and decl wasn't merged with another decl, in which case its
16922 location would be different from input_location, and more accurate. */
16923 if (DECL_P (decl)
16924 && declarator->id_loc != UNKNOWN_LOCATION
16925 && DECL_SOURCE_LOCATION (decl) == input_location)
16926 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16928 else if (scope)
16929 /* Enter the SCOPE. That way unqualified names appearing in the
16930 initializer will be looked up in SCOPE. */
16931 pushed_scope = push_scope (scope);
16933 /* Perform deferred access control checks, now that we know in which
16934 SCOPE the declared entity resides. */
16935 if (!member_p && decl)
16937 tree saved_current_function_decl = NULL_TREE;
16939 /* If the entity being declared is a function, pretend that we
16940 are in its scope. If it is a `friend', it may have access to
16941 things that would not otherwise be accessible. */
16942 if (TREE_CODE (decl) == FUNCTION_DECL)
16944 saved_current_function_decl = current_function_decl;
16945 current_function_decl = decl;
16948 /* Perform access checks for template parameters. */
16949 cp_parser_perform_template_parameter_access_checks (checks);
16951 /* Perform the access control checks for the declarator and the
16952 decl-specifiers. */
16953 perform_deferred_access_checks (tf_warning_or_error);
16955 /* Restore the saved value. */
16956 if (TREE_CODE (decl) == FUNCTION_DECL)
16957 current_function_decl = saved_current_function_decl;
16960 /* Parse the initializer. */
16961 initializer = NULL_TREE;
16962 is_direct_init = false;
16963 is_non_constant_init = true;
16964 if (is_initialized)
16966 if (function_declarator_p (declarator))
16968 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16969 if (initialization_kind == CPP_EQ)
16970 initializer = cp_parser_pure_specifier (parser);
16971 else
16973 /* If the declaration was erroneous, we don't really
16974 know what the user intended, so just silently
16975 consume the initializer. */
16976 if (decl != error_mark_node)
16977 error_at (initializer_start_token->location,
16978 "initializer provided for function");
16979 cp_parser_skip_to_closing_parenthesis (parser,
16980 /*recovering=*/true,
16981 /*or_comma=*/false,
16982 /*consume_paren=*/true);
16985 else
16987 /* We want to record the extra mangling scope for in-class
16988 initializers of class members and initializers of static data
16989 member templates. The former involves deferring
16990 parsing of the initializer until end of class as with default
16991 arguments. So right here we only handle the latter. */
16992 if (!member_p && processing_template_decl)
16993 start_lambda_scope (decl);
16994 initializer = cp_parser_initializer (parser,
16995 &is_direct_init,
16996 &is_non_constant_init);
16997 if (!member_p && processing_template_decl)
16998 finish_lambda_scope ();
16999 if (initializer == error_mark_node)
17000 cp_parser_skip_to_end_of_statement (parser);
17004 /* The old parser allows attributes to appear after a parenthesized
17005 initializer. Mark Mitchell proposed removing this functionality
17006 on the GCC mailing lists on 2002-08-13. This parser accepts the
17007 attributes -- but ignores them. */
17008 if (cp_parser_allow_gnu_extensions_p (parser)
17009 && initialization_kind == CPP_OPEN_PAREN)
17010 if (cp_parser_attributes_opt (parser))
17011 warning (OPT_Wattributes,
17012 "attributes after parenthesized initializer ignored");
17014 /* And now complain about a non-function implicit template. */
17015 if (bogus_implicit_tmpl)
17016 error_at (DECL_SOURCE_LOCATION (decl),
17017 "non-function %qD declared as implicit template", decl);
17019 /* For an in-class declaration, use `grokfield' to create the
17020 declaration. */
17021 if (member_p)
17023 if (pushed_scope)
17025 pop_scope (pushed_scope);
17026 pushed_scope = NULL_TREE;
17028 decl = grokfield (declarator, decl_specifiers,
17029 initializer, !is_non_constant_init,
17030 /*asmspec=*/NULL_TREE,
17031 chainon (attributes, prefix_attributes));
17032 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17033 cp_parser_save_default_args (parser, decl);
17034 cp_finalize_omp_declare_simd (parser, decl);
17037 /* Finish processing the declaration. But, skip member
17038 declarations. */
17039 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17041 cp_finish_decl (decl,
17042 initializer, !is_non_constant_init,
17043 asm_specification,
17044 /* If the initializer is in parentheses, then this is
17045 a direct-initialization, which means that an
17046 `explicit' constructor is OK. Otherwise, an
17047 `explicit' constructor cannot be used. */
17048 ((is_direct_init || !is_initialized)
17049 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17051 else if ((cxx_dialect != cxx98) && friend_p
17052 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17053 /* Core issue #226 (C++0x only): A default template-argument
17054 shall not be specified in a friend class template
17055 declaration. */
17056 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17057 /*is_partial=*/false, /*is_friend_decl=*/1);
17059 if (!friend_p && pushed_scope)
17060 pop_scope (pushed_scope);
17062 if (function_declarator_p (declarator)
17063 && parser->fully_implicit_function_template_p)
17065 if (member_p)
17066 decl = finish_fully_implicit_template (parser, decl);
17067 else
17068 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17071 return decl;
17074 /* Parse a declarator.
17076 declarator:
17077 direct-declarator
17078 ptr-operator declarator
17080 abstract-declarator:
17081 ptr-operator abstract-declarator [opt]
17082 direct-abstract-declarator
17084 GNU Extensions:
17086 declarator:
17087 attributes [opt] direct-declarator
17088 attributes [opt] ptr-operator declarator
17090 abstract-declarator:
17091 attributes [opt] ptr-operator abstract-declarator [opt]
17092 attributes [opt] direct-abstract-declarator
17094 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17095 detect constructor, destructor or conversion operators. It is set
17096 to -1 if the declarator is a name, and +1 if it is a
17097 function. Otherwise it is set to zero. Usually you just want to
17098 test for >0, but internally the negative value is used.
17100 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17101 a decl-specifier-seq unless it declares a constructor, destructor,
17102 or conversion. It might seem that we could check this condition in
17103 semantic analysis, rather than parsing, but that makes it difficult
17104 to handle something like `f()'. We want to notice that there are
17105 no decl-specifiers, and therefore realize that this is an
17106 expression, not a declaration.)
17108 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17109 the declarator is a direct-declarator of the form "(...)".
17111 MEMBER_P is true iff this declarator is a member-declarator.
17113 FRIEND_P is true iff this declarator is a friend. */
17115 static cp_declarator *
17116 cp_parser_declarator (cp_parser* parser,
17117 cp_parser_declarator_kind dcl_kind,
17118 int* ctor_dtor_or_conv_p,
17119 bool* parenthesized_p,
17120 bool member_p, bool friend_p)
17122 cp_declarator *declarator;
17123 enum tree_code code;
17124 cp_cv_quals cv_quals;
17125 tree class_type;
17126 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17128 /* Assume this is not a constructor, destructor, or type-conversion
17129 operator. */
17130 if (ctor_dtor_or_conv_p)
17131 *ctor_dtor_or_conv_p = 0;
17133 if (cp_parser_allow_gnu_extensions_p (parser))
17134 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17136 /* Check for the ptr-operator production. */
17137 cp_parser_parse_tentatively (parser);
17138 /* Parse the ptr-operator. */
17139 code = cp_parser_ptr_operator (parser,
17140 &class_type,
17141 &cv_quals,
17142 &std_attributes);
17144 /* If that worked, then we have a ptr-operator. */
17145 if (cp_parser_parse_definitely (parser))
17147 /* If a ptr-operator was found, then this declarator was not
17148 parenthesized. */
17149 if (parenthesized_p)
17150 *parenthesized_p = true;
17151 /* The dependent declarator is optional if we are parsing an
17152 abstract-declarator. */
17153 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17154 cp_parser_parse_tentatively (parser);
17156 /* Parse the dependent declarator. */
17157 declarator = cp_parser_declarator (parser, dcl_kind,
17158 /*ctor_dtor_or_conv_p=*/NULL,
17159 /*parenthesized_p=*/NULL,
17160 /*member_p=*/false,
17161 friend_p);
17163 /* If we are parsing an abstract-declarator, we must handle the
17164 case where the dependent declarator is absent. */
17165 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17166 && !cp_parser_parse_definitely (parser))
17167 declarator = NULL;
17169 declarator = cp_parser_make_indirect_declarator
17170 (code, class_type, cv_quals, declarator, std_attributes);
17172 /* Everything else is a direct-declarator. */
17173 else
17175 if (parenthesized_p)
17176 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17177 CPP_OPEN_PAREN);
17178 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17179 ctor_dtor_or_conv_p,
17180 member_p, friend_p);
17183 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17184 declarator->attributes = gnu_attributes;
17185 return declarator;
17188 /* Parse a direct-declarator or direct-abstract-declarator.
17190 direct-declarator:
17191 declarator-id
17192 direct-declarator ( parameter-declaration-clause )
17193 cv-qualifier-seq [opt]
17194 ref-qualifier [opt]
17195 exception-specification [opt]
17196 direct-declarator [ constant-expression [opt] ]
17197 ( declarator )
17199 direct-abstract-declarator:
17200 direct-abstract-declarator [opt]
17201 ( parameter-declaration-clause )
17202 cv-qualifier-seq [opt]
17203 ref-qualifier [opt]
17204 exception-specification [opt]
17205 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17206 ( abstract-declarator )
17208 Returns a representation of the declarator. DCL_KIND is
17209 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17210 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17211 we are parsing a direct-declarator. It is
17212 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17213 of ambiguity we prefer an abstract declarator, as per
17214 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17215 as for cp_parser_declarator. */
17217 static cp_declarator *
17218 cp_parser_direct_declarator (cp_parser* parser,
17219 cp_parser_declarator_kind dcl_kind,
17220 int* ctor_dtor_or_conv_p,
17221 bool member_p, bool friend_p)
17223 cp_token *token;
17224 cp_declarator *declarator = NULL;
17225 tree scope = NULL_TREE;
17226 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17227 bool saved_in_declarator_p = parser->in_declarator_p;
17228 bool first = true;
17229 tree pushed_scope = NULL_TREE;
17231 while (true)
17233 /* Peek at the next token. */
17234 token = cp_lexer_peek_token (parser->lexer);
17235 if (token->type == CPP_OPEN_PAREN)
17237 /* This is either a parameter-declaration-clause, or a
17238 parenthesized declarator. When we know we are parsing a
17239 named declarator, it must be a parenthesized declarator
17240 if FIRST is true. For instance, `(int)' is a
17241 parameter-declaration-clause, with an omitted
17242 direct-abstract-declarator. But `((*))', is a
17243 parenthesized abstract declarator. Finally, when T is a
17244 template parameter `(T)' is a
17245 parameter-declaration-clause, and not a parenthesized
17246 named declarator.
17248 We first try and parse a parameter-declaration-clause,
17249 and then try a nested declarator (if FIRST is true).
17251 It is not an error for it not to be a
17252 parameter-declaration-clause, even when FIRST is
17253 false. Consider,
17255 int i (int);
17256 int i (3);
17258 The first is the declaration of a function while the
17259 second is the definition of a variable, including its
17260 initializer.
17262 Having seen only the parenthesis, we cannot know which of
17263 these two alternatives should be selected. Even more
17264 complex are examples like:
17266 int i (int (a));
17267 int i (int (3));
17269 The former is a function-declaration; the latter is a
17270 variable initialization.
17272 Thus again, we try a parameter-declaration-clause, and if
17273 that fails, we back out and return. */
17275 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17277 tree params;
17278 bool is_declarator = false;
17280 /* In a member-declarator, the only valid interpretation
17281 of a parenthesis is the start of a
17282 parameter-declaration-clause. (It is invalid to
17283 initialize a static data member with a parenthesized
17284 initializer; only the "=" form of initialization is
17285 permitted.) */
17286 if (!member_p)
17287 cp_parser_parse_tentatively (parser);
17289 /* Consume the `('. */
17290 cp_lexer_consume_token (parser->lexer);
17291 if (first)
17293 /* If this is going to be an abstract declarator, we're
17294 in a declarator and we can't have default args. */
17295 parser->default_arg_ok_p = false;
17296 parser->in_declarator_p = true;
17299 begin_scope (sk_function_parms, NULL_TREE);
17301 /* Parse the parameter-declaration-clause. */
17302 params = cp_parser_parameter_declaration_clause (parser);
17304 /* Consume the `)'. */
17305 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17307 /* If all went well, parse the cv-qualifier-seq,
17308 ref-qualifier and the exception-specification. */
17309 if (member_p || cp_parser_parse_definitely (parser))
17311 cp_cv_quals cv_quals;
17312 cp_virt_specifiers virt_specifiers;
17313 cp_ref_qualifier ref_qual;
17314 tree exception_specification;
17315 tree late_return;
17316 tree attrs;
17317 bool memfn = (member_p || (pushed_scope
17318 && CLASS_TYPE_P (pushed_scope)));
17320 is_declarator = true;
17322 if (ctor_dtor_or_conv_p)
17323 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17324 first = false;
17326 /* Parse the cv-qualifier-seq. */
17327 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17328 /* Parse the ref-qualifier. */
17329 ref_qual = cp_parser_ref_qualifier_opt (parser);
17330 /* And the exception-specification. */
17331 exception_specification
17332 = cp_parser_exception_specification_opt (parser);
17334 attrs = cp_parser_std_attribute_spec_seq (parser);
17336 /* In here, we handle cases where attribute is used after
17337 the function declaration. For example:
17338 void func (int x) __attribute__((vector(..))); */
17339 if (flag_cilkplus
17340 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17342 cp_parser_parse_tentatively (parser);
17343 tree attr = cp_parser_gnu_attributes_opt (parser);
17344 if (cp_lexer_next_token_is_not (parser->lexer,
17345 CPP_SEMICOLON)
17346 && cp_lexer_next_token_is_not (parser->lexer,
17347 CPP_OPEN_BRACE))
17348 cp_parser_abort_tentative_parse (parser);
17349 else if (!cp_parser_parse_definitely (parser))
17351 else
17352 attrs = chainon (attr, attrs);
17354 late_return = (cp_parser_late_return_type_opt
17355 (parser, declarator,
17356 memfn ? cv_quals : -1));
17359 /* Parse the virt-specifier-seq. */
17360 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17362 /* Create the function-declarator. */
17363 declarator = make_call_declarator (declarator,
17364 params,
17365 cv_quals,
17366 virt_specifiers,
17367 ref_qual,
17368 exception_specification,
17369 late_return);
17370 declarator->std_attributes = attrs;
17371 /* Any subsequent parameter lists are to do with
17372 return type, so are not those of the declared
17373 function. */
17374 parser->default_arg_ok_p = false;
17377 /* Remove the function parms from scope. */
17378 pop_bindings_and_leave_scope ();
17380 if (is_declarator)
17381 /* Repeat the main loop. */
17382 continue;
17385 /* If this is the first, we can try a parenthesized
17386 declarator. */
17387 if (first)
17389 bool saved_in_type_id_in_expr_p;
17391 parser->default_arg_ok_p = saved_default_arg_ok_p;
17392 parser->in_declarator_p = saved_in_declarator_p;
17394 /* Consume the `('. */
17395 cp_lexer_consume_token (parser->lexer);
17396 /* Parse the nested declarator. */
17397 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17398 parser->in_type_id_in_expr_p = true;
17399 declarator
17400 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17401 /*parenthesized_p=*/NULL,
17402 member_p, friend_p);
17403 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17404 first = false;
17405 /* Expect a `)'. */
17406 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17407 declarator = cp_error_declarator;
17408 if (declarator == cp_error_declarator)
17409 break;
17411 goto handle_declarator;
17413 /* Otherwise, we must be done. */
17414 else
17415 break;
17417 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17418 && token->type == CPP_OPEN_SQUARE
17419 && !cp_next_tokens_can_be_attribute_p (parser))
17421 /* Parse an array-declarator. */
17422 tree bounds, attrs;
17424 if (ctor_dtor_or_conv_p)
17425 *ctor_dtor_or_conv_p = 0;
17427 first = false;
17428 parser->default_arg_ok_p = false;
17429 parser->in_declarator_p = true;
17430 /* Consume the `['. */
17431 cp_lexer_consume_token (parser->lexer);
17432 /* Peek at the next token. */
17433 token = cp_lexer_peek_token (parser->lexer);
17434 /* If the next token is `]', then there is no
17435 constant-expression. */
17436 if (token->type != CPP_CLOSE_SQUARE)
17438 bool non_constant_p;
17439 bounds
17440 = cp_parser_constant_expression (parser,
17441 /*allow_non_constant=*/true,
17442 &non_constant_p);
17443 if (!non_constant_p)
17444 /* OK */;
17445 else if (error_operand_p (bounds))
17446 /* Already gave an error. */;
17447 else if (!parser->in_function_body
17448 || current_binding_level->kind == sk_function_parms)
17450 /* Normally, the array bound must be an integral constant
17451 expression. However, as an extension, we allow VLAs
17452 in function scopes as long as they aren't part of a
17453 parameter declaration. */
17454 cp_parser_error (parser,
17455 "array bound is not an integer constant");
17456 bounds = error_mark_node;
17458 else if (processing_template_decl
17459 && !type_dependent_expression_p (bounds))
17461 /* Remember this wasn't a constant-expression. */
17462 bounds = build_nop (TREE_TYPE (bounds), bounds);
17463 TREE_SIDE_EFFECTS (bounds) = 1;
17466 else
17467 bounds = NULL_TREE;
17468 /* Look for the closing `]'. */
17469 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17471 declarator = cp_error_declarator;
17472 break;
17475 attrs = cp_parser_std_attribute_spec_seq (parser);
17476 declarator = make_array_declarator (declarator, bounds);
17477 declarator->std_attributes = attrs;
17479 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17482 tree qualifying_scope;
17483 tree unqualified_name;
17484 tree attrs;
17485 special_function_kind sfk;
17486 bool abstract_ok;
17487 bool pack_expansion_p = false;
17488 cp_token *declarator_id_start_token;
17490 /* Parse a declarator-id */
17491 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17492 if (abstract_ok)
17494 cp_parser_parse_tentatively (parser);
17496 /* If we see an ellipsis, we should be looking at a
17497 parameter pack. */
17498 if (token->type == CPP_ELLIPSIS)
17500 /* Consume the `...' */
17501 cp_lexer_consume_token (parser->lexer);
17503 pack_expansion_p = true;
17507 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17508 unqualified_name
17509 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17510 qualifying_scope = parser->scope;
17511 if (abstract_ok)
17513 bool okay = false;
17515 if (!unqualified_name && pack_expansion_p)
17517 /* Check whether an error occurred. */
17518 okay = !cp_parser_error_occurred (parser);
17520 /* We already consumed the ellipsis to mark a
17521 parameter pack, but we have no way to report it,
17522 so abort the tentative parse. We will be exiting
17523 immediately anyway. */
17524 cp_parser_abort_tentative_parse (parser);
17526 else
17527 okay = cp_parser_parse_definitely (parser);
17529 if (!okay)
17530 unqualified_name = error_mark_node;
17531 else if (unqualified_name
17532 && (qualifying_scope
17533 || (!identifier_p (unqualified_name))))
17535 cp_parser_error (parser, "expected unqualified-id");
17536 unqualified_name = error_mark_node;
17540 if (!unqualified_name)
17541 return NULL;
17542 if (unqualified_name == error_mark_node)
17544 declarator = cp_error_declarator;
17545 pack_expansion_p = false;
17546 declarator->parameter_pack_p = false;
17547 break;
17550 attrs = cp_parser_std_attribute_spec_seq (parser);
17552 if (qualifying_scope && at_namespace_scope_p ()
17553 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17555 /* In the declaration of a member of a template class
17556 outside of the class itself, the SCOPE will sometimes
17557 be a TYPENAME_TYPE. For example, given:
17559 template <typename T>
17560 int S<T>::R::i = 3;
17562 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17563 this context, we must resolve S<T>::R to an ordinary
17564 type, rather than a typename type.
17566 The reason we normally avoid resolving TYPENAME_TYPEs
17567 is that a specialization of `S' might render
17568 `S<T>::R' not a type. However, if `S' is
17569 specialized, then this `i' will not be used, so there
17570 is no harm in resolving the types here. */
17571 tree type;
17573 /* Resolve the TYPENAME_TYPE. */
17574 type = resolve_typename_type (qualifying_scope,
17575 /*only_current_p=*/false);
17576 /* If that failed, the declarator is invalid. */
17577 if (TREE_CODE (type) == TYPENAME_TYPE)
17579 if (typedef_variant_p (type))
17580 error_at (declarator_id_start_token->location,
17581 "cannot define member of dependent typedef "
17582 "%qT", type);
17583 else
17584 error_at (declarator_id_start_token->location,
17585 "%<%T::%E%> is not a type",
17586 TYPE_CONTEXT (qualifying_scope),
17587 TYPE_IDENTIFIER (qualifying_scope));
17589 qualifying_scope = type;
17592 sfk = sfk_none;
17594 if (unqualified_name)
17596 tree class_type;
17598 if (qualifying_scope
17599 && CLASS_TYPE_P (qualifying_scope))
17600 class_type = qualifying_scope;
17601 else
17602 class_type = current_class_type;
17604 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17606 tree name_type = TREE_TYPE (unqualified_name);
17607 if (class_type && same_type_p (name_type, class_type))
17609 if (qualifying_scope
17610 && CLASSTYPE_USE_TEMPLATE (name_type))
17612 error_at (declarator_id_start_token->location,
17613 "invalid use of constructor as a template");
17614 inform (declarator_id_start_token->location,
17615 "use %<%T::%D%> instead of %<%T::%D%> to "
17616 "name the constructor in a qualified name",
17617 class_type,
17618 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17619 class_type, name_type);
17620 declarator = cp_error_declarator;
17621 break;
17623 else
17624 unqualified_name = constructor_name (class_type);
17626 else
17628 /* We do not attempt to print the declarator
17629 here because we do not have enough
17630 information about its original syntactic
17631 form. */
17632 cp_parser_error (parser, "invalid declarator");
17633 declarator = cp_error_declarator;
17634 break;
17638 if (class_type)
17640 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17641 sfk = sfk_destructor;
17642 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17643 sfk = sfk_conversion;
17644 else if (/* There's no way to declare a constructor
17645 for an anonymous type, even if the type
17646 got a name for linkage purposes. */
17647 !TYPE_WAS_ANONYMOUS (class_type)
17648 /* Handle correctly (c++/19200):
17650 struct S {
17651 struct T{};
17652 friend void S(T);
17655 and also:
17657 namespace N {
17658 void S();
17661 struct S {
17662 friend void N::S();
17663 }; */
17664 && !(friend_p
17665 && class_type != qualifying_scope)
17666 && constructor_name_p (unqualified_name,
17667 class_type))
17669 unqualified_name = constructor_name (class_type);
17670 sfk = sfk_constructor;
17672 else if (is_overloaded_fn (unqualified_name)
17673 && DECL_CONSTRUCTOR_P (get_first_fn
17674 (unqualified_name)))
17675 sfk = sfk_constructor;
17677 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17678 *ctor_dtor_or_conv_p = -1;
17681 declarator = make_id_declarator (qualifying_scope,
17682 unqualified_name,
17683 sfk);
17684 declarator->std_attributes = attrs;
17685 declarator->id_loc = token->location;
17686 declarator->parameter_pack_p = pack_expansion_p;
17688 if (pack_expansion_p)
17689 maybe_warn_variadic_templates ();
17692 handle_declarator:;
17693 scope = get_scope_of_declarator (declarator);
17694 if (scope)
17696 /* Any names that appear after the declarator-id for a
17697 member are looked up in the containing scope. */
17698 if (at_function_scope_p ())
17700 /* But declarations with qualified-ids can't appear in a
17701 function. */
17702 cp_parser_error (parser, "qualified-id in declaration");
17703 declarator = cp_error_declarator;
17704 break;
17706 pushed_scope = push_scope (scope);
17708 parser->in_declarator_p = true;
17709 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17710 || (declarator && declarator->kind == cdk_id))
17711 /* Default args are only allowed on function
17712 declarations. */
17713 parser->default_arg_ok_p = saved_default_arg_ok_p;
17714 else
17715 parser->default_arg_ok_p = false;
17717 first = false;
17719 /* We're done. */
17720 else
17721 break;
17724 /* For an abstract declarator, we might wind up with nothing at this
17725 point. That's an error; the declarator is not optional. */
17726 if (!declarator)
17727 cp_parser_error (parser, "expected declarator");
17729 /* If we entered a scope, we must exit it now. */
17730 if (pushed_scope)
17731 pop_scope (pushed_scope);
17733 parser->default_arg_ok_p = saved_default_arg_ok_p;
17734 parser->in_declarator_p = saved_in_declarator_p;
17736 return declarator;
17739 /* Parse a ptr-operator.
17741 ptr-operator:
17742 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17743 * cv-qualifier-seq [opt]
17745 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17746 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17748 GNU Extension:
17750 ptr-operator:
17751 & cv-qualifier-seq [opt]
17753 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17754 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17755 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17756 filled in with the TYPE containing the member. *CV_QUALS is
17757 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17758 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17759 Note that the tree codes returned by this function have nothing
17760 to do with the types of trees that will be eventually be created
17761 to represent the pointer or reference type being parsed. They are
17762 just constants with suggestive names. */
17763 static enum tree_code
17764 cp_parser_ptr_operator (cp_parser* parser,
17765 tree* type,
17766 cp_cv_quals *cv_quals,
17767 tree *attributes)
17769 enum tree_code code = ERROR_MARK;
17770 cp_token *token;
17771 tree attrs = NULL_TREE;
17773 /* Assume that it's not a pointer-to-member. */
17774 *type = NULL_TREE;
17775 /* And that there are no cv-qualifiers. */
17776 *cv_quals = TYPE_UNQUALIFIED;
17778 /* Peek at the next token. */
17779 token = cp_lexer_peek_token (parser->lexer);
17781 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17782 if (token->type == CPP_MULT)
17783 code = INDIRECT_REF;
17784 else if (token->type == CPP_AND)
17785 code = ADDR_EXPR;
17786 else if ((cxx_dialect != cxx98) &&
17787 token->type == CPP_AND_AND) /* C++0x only */
17788 code = NON_LVALUE_EXPR;
17790 if (code != ERROR_MARK)
17792 /* Consume the `*', `&' or `&&'. */
17793 cp_lexer_consume_token (parser->lexer);
17795 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17796 `&', if we are allowing GNU extensions. (The only qualifier
17797 that can legally appear after `&' is `restrict', but that is
17798 enforced during semantic analysis. */
17799 if (code == INDIRECT_REF
17800 || cp_parser_allow_gnu_extensions_p (parser))
17801 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17803 attrs = cp_parser_std_attribute_spec_seq (parser);
17804 if (attributes != NULL)
17805 *attributes = attrs;
17807 else
17809 /* Try the pointer-to-member case. */
17810 cp_parser_parse_tentatively (parser);
17811 /* Look for the optional `::' operator. */
17812 cp_parser_global_scope_opt (parser,
17813 /*current_scope_valid_p=*/false);
17814 /* Look for the nested-name specifier. */
17815 token = cp_lexer_peek_token (parser->lexer);
17816 cp_parser_nested_name_specifier (parser,
17817 /*typename_keyword_p=*/false,
17818 /*check_dependency_p=*/true,
17819 /*type_p=*/false,
17820 /*is_declaration=*/false);
17821 /* If we found it, and the next token is a `*', then we are
17822 indeed looking at a pointer-to-member operator. */
17823 if (!cp_parser_error_occurred (parser)
17824 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17826 /* Indicate that the `*' operator was used. */
17827 code = INDIRECT_REF;
17829 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17830 error_at (token->location, "%qD is a namespace", parser->scope);
17831 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17832 error_at (token->location, "cannot form pointer to member of "
17833 "non-class %q#T", parser->scope);
17834 else
17836 /* The type of which the member is a member is given by the
17837 current SCOPE. */
17838 *type = parser->scope;
17839 /* The next name will not be qualified. */
17840 parser->scope = NULL_TREE;
17841 parser->qualifying_scope = NULL_TREE;
17842 parser->object_scope = NULL_TREE;
17843 /* Look for optional c++11 attributes. */
17844 attrs = cp_parser_std_attribute_spec_seq (parser);
17845 if (attributes != NULL)
17846 *attributes = attrs;
17847 /* Look for the optional cv-qualifier-seq. */
17848 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17851 /* If that didn't work we don't have a ptr-operator. */
17852 if (!cp_parser_parse_definitely (parser))
17853 cp_parser_error (parser, "expected ptr-operator");
17856 return code;
17859 /* Parse an (optional) cv-qualifier-seq.
17861 cv-qualifier-seq:
17862 cv-qualifier cv-qualifier-seq [opt]
17864 cv-qualifier:
17865 const
17866 volatile
17868 GNU Extension:
17870 cv-qualifier:
17871 __restrict__
17873 Returns a bitmask representing the cv-qualifiers. */
17875 static cp_cv_quals
17876 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17878 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17880 while (true)
17882 cp_token *token;
17883 cp_cv_quals cv_qualifier;
17885 /* Peek at the next token. */
17886 token = cp_lexer_peek_token (parser->lexer);
17887 /* See if it's a cv-qualifier. */
17888 switch (token->keyword)
17890 case RID_CONST:
17891 cv_qualifier = TYPE_QUAL_CONST;
17892 break;
17894 case RID_VOLATILE:
17895 cv_qualifier = TYPE_QUAL_VOLATILE;
17896 break;
17898 case RID_RESTRICT:
17899 cv_qualifier = TYPE_QUAL_RESTRICT;
17900 break;
17902 default:
17903 cv_qualifier = TYPE_UNQUALIFIED;
17904 break;
17907 if (!cv_qualifier)
17908 break;
17910 if (cv_quals & cv_qualifier)
17912 error_at (token->location, "duplicate cv-qualifier");
17913 cp_lexer_purge_token (parser->lexer);
17915 else
17917 cp_lexer_consume_token (parser->lexer);
17918 cv_quals |= cv_qualifier;
17922 return cv_quals;
17925 /* Parse an (optional) ref-qualifier
17927 ref-qualifier:
17931 Returns cp_ref_qualifier representing ref-qualifier. */
17933 static cp_ref_qualifier
17934 cp_parser_ref_qualifier_opt (cp_parser* parser)
17936 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17938 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17939 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17940 return ref_qual;
17942 while (true)
17944 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17945 cp_token *token = cp_lexer_peek_token (parser->lexer);
17947 switch (token->type)
17949 case CPP_AND:
17950 curr_ref_qual = REF_QUAL_LVALUE;
17951 break;
17953 case CPP_AND_AND:
17954 curr_ref_qual = REF_QUAL_RVALUE;
17955 break;
17957 default:
17958 curr_ref_qual = REF_QUAL_NONE;
17959 break;
17962 if (!curr_ref_qual)
17963 break;
17964 else if (ref_qual)
17966 error_at (token->location, "multiple ref-qualifiers");
17967 cp_lexer_purge_token (parser->lexer);
17969 else
17971 ref_qual = curr_ref_qual;
17972 cp_lexer_consume_token (parser->lexer);
17976 return ref_qual;
17979 /* Parse an (optional) virt-specifier-seq.
17981 virt-specifier-seq:
17982 virt-specifier virt-specifier-seq [opt]
17984 virt-specifier:
17985 override
17986 final
17988 Returns a bitmask representing the virt-specifiers. */
17990 static cp_virt_specifiers
17991 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17993 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17995 while (true)
17997 cp_token *token;
17998 cp_virt_specifiers virt_specifier;
18000 /* Peek at the next token. */
18001 token = cp_lexer_peek_token (parser->lexer);
18002 /* See if it's a virt-specifier-qualifier. */
18003 if (token->type != CPP_NAME)
18004 break;
18005 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18007 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18008 virt_specifier = VIRT_SPEC_OVERRIDE;
18010 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18012 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18013 virt_specifier = VIRT_SPEC_FINAL;
18015 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18017 virt_specifier = VIRT_SPEC_FINAL;
18019 else
18020 break;
18022 if (virt_specifiers & virt_specifier)
18024 error_at (token->location, "duplicate virt-specifier");
18025 cp_lexer_purge_token (parser->lexer);
18027 else
18029 cp_lexer_consume_token (parser->lexer);
18030 virt_specifiers |= virt_specifier;
18033 return virt_specifiers;
18036 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18037 is in scope even though it isn't real. */
18039 void
18040 inject_this_parameter (tree ctype, cp_cv_quals quals)
18042 tree this_parm;
18044 if (current_class_ptr)
18046 /* We don't clear this between NSDMIs. Is it already what we want? */
18047 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18048 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18049 && cp_type_quals (type) == quals)
18050 return;
18053 this_parm = build_this_parm (ctype, quals);
18054 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18055 current_class_ptr = NULL_TREE;
18056 current_class_ref
18057 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18058 current_class_ptr = this_parm;
18061 /* Return true iff our current scope is a non-static data member
18062 initializer. */
18064 bool
18065 parsing_nsdmi (void)
18067 /* We recognize NSDMI context by the context-less 'this' pointer set up
18068 by the function above. */
18069 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18070 return true;
18071 return false;
18074 /* Parse a late-specified return type, if any. This is not a separate
18075 non-terminal, but part of a function declarator, which looks like
18077 -> trailing-type-specifier-seq abstract-declarator(opt)
18079 Returns the type indicated by the type-id.
18081 In addition to this this parses any queued up omp declare simd
18082 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18084 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18085 function. */
18087 static tree
18088 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18089 cp_cv_quals quals)
18091 cp_token *token;
18092 tree type = NULL_TREE;
18093 bool declare_simd_p = (parser->omp_declare_simd
18094 && declarator
18095 && declarator->kind == cdk_id);
18097 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18098 && declarator && declarator->kind == cdk_id);
18100 /* Peek at the next token. */
18101 token = cp_lexer_peek_token (parser->lexer);
18102 /* A late-specified return type is indicated by an initial '->'. */
18103 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18104 return NULL_TREE;
18106 tree save_ccp = current_class_ptr;
18107 tree save_ccr = current_class_ref;
18108 if (quals >= 0)
18110 /* DR 1207: 'this' is in scope in the trailing return type. */
18111 inject_this_parameter (current_class_type, quals);
18114 if (token->type == CPP_DEREF)
18116 /* Consume the ->. */
18117 cp_lexer_consume_token (parser->lexer);
18119 type = cp_parser_trailing_type_id (parser);
18122 if (cilk_simd_fn_vector_p)
18123 declarator->std_attributes
18124 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18125 declarator->std_attributes);
18126 if (declare_simd_p)
18127 declarator->std_attributes
18128 = cp_parser_late_parsing_omp_declare_simd (parser,
18129 declarator->std_attributes);
18131 if (quals >= 0)
18133 current_class_ptr = save_ccp;
18134 current_class_ref = save_ccr;
18137 return type;
18140 /* Parse a declarator-id.
18142 declarator-id:
18143 id-expression
18144 :: [opt] nested-name-specifier [opt] type-name
18146 In the `id-expression' case, the value returned is as for
18147 cp_parser_id_expression if the id-expression was an unqualified-id.
18148 If the id-expression was a qualified-id, then a SCOPE_REF is
18149 returned. The first operand is the scope (either a NAMESPACE_DECL
18150 or TREE_TYPE), but the second is still just a representation of an
18151 unqualified-id. */
18153 static tree
18154 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18156 tree id;
18157 /* The expression must be an id-expression. Assume that qualified
18158 names are the names of types so that:
18160 template <class T>
18161 int S<T>::R::i = 3;
18163 will work; we must treat `S<T>::R' as the name of a type.
18164 Similarly, assume that qualified names are templates, where
18165 required, so that:
18167 template <class T>
18168 int S<T>::R<T>::i = 3;
18170 will work, too. */
18171 id = cp_parser_id_expression (parser,
18172 /*template_keyword_p=*/false,
18173 /*check_dependency_p=*/false,
18174 /*template_p=*/NULL,
18175 /*declarator_p=*/true,
18176 optional_p);
18177 if (id && BASELINK_P (id))
18178 id = BASELINK_FUNCTIONS (id);
18179 return id;
18182 /* Parse a type-id.
18184 type-id:
18185 type-specifier-seq abstract-declarator [opt]
18187 Returns the TYPE specified. */
18189 static tree
18190 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18191 bool is_trailing_return)
18193 cp_decl_specifier_seq type_specifier_seq;
18194 cp_declarator *abstract_declarator;
18196 /* Parse the type-specifier-seq. */
18197 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18198 is_trailing_return,
18199 &type_specifier_seq);
18200 if (type_specifier_seq.type == error_mark_node)
18201 return error_mark_node;
18203 /* There might or might not be an abstract declarator. */
18204 cp_parser_parse_tentatively (parser);
18205 /* Look for the declarator. */
18206 abstract_declarator
18207 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18208 /*parenthesized_p=*/NULL,
18209 /*member_p=*/false,
18210 /*friend_p=*/false);
18211 /* Check to see if there really was a declarator. */
18212 if (!cp_parser_parse_definitely (parser))
18213 abstract_declarator = NULL;
18215 if (type_specifier_seq.type
18216 /* None of the valid uses of 'auto' in C++14 involve the type-id
18217 nonterminal, but it is valid in a trailing-return-type. */
18218 && !(cxx_dialect >= cxx14 && is_trailing_return)
18219 && type_uses_auto (type_specifier_seq.type))
18221 /* A type-id with type 'auto' is only ok if the abstract declarator
18222 is a function declarator with a late-specified return type. */
18223 if (abstract_declarator
18224 && abstract_declarator->kind == cdk_function
18225 && abstract_declarator->u.function.late_return_type)
18226 /* OK */;
18227 else
18229 error ("invalid use of %<auto%>");
18230 return error_mark_node;
18234 return groktypename (&type_specifier_seq, abstract_declarator,
18235 is_template_arg);
18238 static tree cp_parser_type_id (cp_parser *parser)
18240 return cp_parser_type_id_1 (parser, false, false);
18243 static tree cp_parser_template_type_arg (cp_parser *parser)
18245 tree r;
18246 const char *saved_message = parser->type_definition_forbidden_message;
18247 parser->type_definition_forbidden_message
18248 = G_("types may not be defined in template arguments");
18249 r = cp_parser_type_id_1 (parser, true, false);
18250 parser->type_definition_forbidden_message = saved_message;
18251 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18253 error ("invalid use of %<auto%> in template argument");
18254 r = error_mark_node;
18256 return r;
18259 static tree cp_parser_trailing_type_id (cp_parser *parser)
18261 return cp_parser_type_id_1 (parser, false, true);
18264 /* Parse a type-specifier-seq.
18266 type-specifier-seq:
18267 type-specifier type-specifier-seq [opt]
18269 GNU extension:
18271 type-specifier-seq:
18272 attributes type-specifier-seq [opt]
18274 If IS_DECLARATION is true, we are at the start of a "condition" or
18275 exception-declaration, so we might be followed by a declarator-id.
18277 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18278 i.e. we've just seen "->".
18280 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18282 static void
18283 cp_parser_type_specifier_seq (cp_parser* parser,
18284 bool is_declaration,
18285 bool is_trailing_return,
18286 cp_decl_specifier_seq *type_specifier_seq)
18288 bool seen_type_specifier = false;
18289 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18290 cp_token *start_token = NULL;
18292 /* Clear the TYPE_SPECIFIER_SEQ. */
18293 clear_decl_specs (type_specifier_seq);
18295 /* In the context of a trailing return type, enum E { } is an
18296 elaborated-type-specifier followed by a function-body, not an
18297 enum-specifier. */
18298 if (is_trailing_return)
18299 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18301 /* Parse the type-specifiers and attributes. */
18302 while (true)
18304 tree type_specifier;
18305 bool is_cv_qualifier;
18307 /* Check for attributes first. */
18308 if (cp_next_tokens_can_be_attribute_p (parser))
18310 type_specifier_seq->attributes =
18311 chainon (type_specifier_seq->attributes,
18312 cp_parser_attributes_opt (parser));
18313 continue;
18316 /* record the token of the beginning of the type specifier seq,
18317 for error reporting purposes*/
18318 if (!start_token)
18319 start_token = cp_lexer_peek_token (parser->lexer);
18321 /* Look for the type-specifier. */
18322 type_specifier = cp_parser_type_specifier (parser,
18323 flags,
18324 type_specifier_seq,
18325 /*is_declaration=*/false,
18326 NULL,
18327 &is_cv_qualifier);
18328 if (!type_specifier)
18330 /* If the first type-specifier could not be found, this is not a
18331 type-specifier-seq at all. */
18332 if (!seen_type_specifier)
18334 /* Set in_declarator_p to avoid skipping to the semicolon. */
18335 int in_decl = parser->in_declarator_p;
18336 parser->in_declarator_p = true;
18338 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18339 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18340 cp_parser_error (parser, "expected type-specifier");
18342 parser->in_declarator_p = in_decl;
18344 type_specifier_seq->type = error_mark_node;
18345 return;
18347 /* If subsequent type-specifiers could not be found, the
18348 type-specifier-seq is complete. */
18349 break;
18352 seen_type_specifier = true;
18353 /* The standard says that a condition can be:
18355 type-specifier-seq declarator = assignment-expression
18357 However, given:
18359 struct S {};
18360 if (int S = ...)
18362 we should treat the "S" as a declarator, not as a
18363 type-specifier. The standard doesn't say that explicitly for
18364 type-specifier-seq, but it does say that for
18365 decl-specifier-seq in an ordinary declaration. Perhaps it
18366 would be clearer just to allow a decl-specifier-seq here, and
18367 then add a semantic restriction that if any decl-specifiers
18368 that are not type-specifiers appear, the program is invalid. */
18369 if (is_declaration && !is_cv_qualifier)
18370 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18374 /* Return whether the function currently being declared has an associated
18375 template parameter list. */
18377 static bool
18378 function_being_declared_is_template_p (cp_parser* parser)
18380 if (!current_template_parms || processing_template_parmlist)
18381 return false;
18383 if (parser->implicit_template_scope)
18384 return true;
18386 if (at_class_scope_p ()
18387 && TYPE_BEING_DEFINED (current_class_type))
18388 return parser->num_template_parameter_lists != 0;
18390 return ((int) parser->num_template_parameter_lists > template_class_depth
18391 (current_class_type));
18394 /* Parse a parameter-declaration-clause.
18396 parameter-declaration-clause:
18397 parameter-declaration-list [opt] ... [opt]
18398 parameter-declaration-list , ...
18400 Returns a representation for the parameter declarations. A return
18401 value of NULL indicates a parameter-declaration-clause consisting
18402 only of an ellipsis. */
18404 static tree
18405 cp_parser_parameter_declaration_clause (cp_parser* parser)
18407 tree parameters;
18408 cp_token *token;
18409 bool ellipsis_p;
18410 bool is_error;
18412 struct cleanup {
18413 cp_parser* parser;
18414 int auto_is_implicit_function_template_parm_p;
18415 ~cleanup() {
18416 parser->auto_is_implicit_function_template_parm_p
18417 = auto_is_implicit_function_template_parm_p;
18419 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18421 (void) cleanup;
18423 if (!processing_specialization
18424 && !processing_template_parmlist
18425 && !processing_explicit_instantiation)
18426 if (!current_function_decl
18427 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18428 parser->auto_is_implicit_function_template_parm_p = true;
18430 /* Peek at the next token. */
18431 token = cp_lexer_peek_token (parser->lexer);
18432 /* Check for trivial parameter-declaration-clauses. */
18433 if (token->type == CPP_ELLIPSIS)
18435 /* Consume the `...' token. */
18436 cp_lexer_consume_token (parser->lexer);
18437 return NULL_TREE;
18439 else if (token->type == CPP_CLOSE_PAREN)
18440 /* There are no parameters. */
18442 #ifndef NO_IMPLICIT_EXTERN_C
18443 if (in_system_header_at (input_location)
18444 && current_class_type == NULL
18445 && current_lang_name == lang_name_c)
18446 return NULL_TREE;
18447 else
18448 #endif
18449 return void_list_node;
18451 /* Check for `(void)', too, which is a special case. */
18452 else if (token->keyword == RID_VOID
18453 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18454 == CPP_CLOSE_PAREN))
18456 /* Consume the `void' token. */
18457 cp_lexer_consume_token (parser->lexer);
18458 /* There are no parameters. */
18459 return void_list_node;
18462 /* Parse the parameter-declaration-list. */
18463 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18464 /* If a parse error occurred while parsing the
18465 parameter-declaration-list, then the entire
18466 parameter-declaration-clause is erroneous. */
18467 if (is_error)
18468 return NULL;
18470 /* Peek at the next token. */
18471 token = cp_lexer_peek_token (parser->lexer);
18472 /* If it's a `,', the clause should terminate with an ellipsis. */
18473 if (token->type == CPP_COMMA)
18475 /* Consume the `,'. */
18476 cp_lexer_consume_token (parser->lexer);
18477 /* Expect an ellipsis. */
18478 ellipsis_p
18479 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18481 /* It might also be `...' if the optional trailing `,' was
18482 omitted. */
18483 else if (token->type == CPP_ELLIPSIS)
18485 /* Consume the `...' token. */
18486 cp_lexer_consume_token (parser->lexer);
18487 /* And remember that we saw it. */
18488 ellipsis_p = true;
18490 else
18491 ellipsis_p = false;
18493 /* Finish the parameter list. */
18494 if (!ellipsis_p)
18495 parameters = chainon (parameters, void_list_node);
18497 return parameters;
18500 /* Parse a parameter-declaration-list.
18502 parameter-declaration-list:
18503 parameter-declaration
18504 parameter-declaration-list , parameter-declaration
18506 Returns a representation of the parameter-declaration-list, as for
18507 cp_parser_parameter_declaration_clause. However, the
18508 `void_list_node' is never appended to the list. Upon return,
18509 *IS_ERROR will be true iff an error occurred. */
18511 static tree
18512 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18514 tree parameters = NULL_TREE;
18515 tree *tail = &parameters;
18516 bool saved_in_unbraced_linkage_specification_p;
18517 int index = 0;
18519 /* Assume all will go well. */
18520 *is_error = false;
18521 /* The special considerations that apply to a function within an
18522 unbraced linkage specifications do not apply to the parameters
18523 to the function. */
18524 saved_in_unbraced_linkage_specification_p
18525 = parser->in_unbraced_linkage_specification_p;
18526 parser->in_unbraced_linkage_specification_p = false;
18528 /* Look for more parameters. */
18529 while (true)
18531 cp_parameter_declarator *parameter;
18532 tree decl = error_mark_node;
18533 bool parenthesized_p = false;
18534 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18535 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18536 (current_template_parms)) : 0);
18538 /* Parse the parameter. */
18539 parameter
18540 = cp_parser_parameter_declaration (parser,
18541 /*template_parm_p=*/false,
18542 &parenthesized_p);
18544 /* We don't know yet if the enclosing context is deprecated, so wait
18545 and warn in grokparms if appropriate. */
18546 deprecated_state = DEPRECATED_SUPPRESS;
18548 if (parameter)
18550 /* If a function parameter pack was specified and an implicit template
18551 parameter was introduced during cp_parser_parameter_declaration,
18552 change any implicit parameters introduced into packs. */
18553 if (parser->implicit_template_parms
18554 && parameter->declarator
18555 && parameter->declarator->parameter_pack_p)
18557 int latest_template_parm_idx = TREE_VEC_LENGTH
18558 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18560 if (latest_template_parm_idx != template_parm_idx)
18561 parameter->decl_specifiers.type = convert_generic_types_to_packs
18562 (parameter->decl_specifiers.type,
18563 template_parm_idx, latest_template_parm_idx);
18566 decl = grokdeclarator (parameter->declarator,
18567 &parameter->decl_specifiers,
18568 PARM,
18569 parameter->default_argument != NULL_TREE,
18570 &parameter->decl_specifiers.attributes);
18573 deprecated_state = DEPRECATED_NORMAL;
18575 /* If a parse error occurred parsing the parameter declaration,
18576 then the entire parameter-declaration-list is erroneous. */
18577 if (decl == error_mark_node)
18579 *is_error = true;
18580 parameters = error_mark_node;
18581 break;
18584 if (parameter->decl_specifiers.attributes)
18585 cplus_decl_attributes (&decl,
18586 parameter->decl_specifiers.attributes,
18588 if (DECL_NAME (decl))
18589 decl = pushdecl (decl);
18591 if (decl != error_mark_node)
18593 retrofit_lang_decl (decl);
18594 DECL_PARM_INDEX (decl) = ++index;
18595 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18598 /* Add the new parameter to the list. */
18599 *tail = build_tree_list (parameter->default_argument, decl);
18600 tail = &TREE_CHAIN (*tail);
18602 /* Peek at the next token. */
18603 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18604 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18605 /* These are for Objective-C++ */
18606 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18607 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18608 /* The parameter-declaration-list is complete. */
18609 break;
18610 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18612 cp_token *token;
18614 /* Peek at the next token. */
18615 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18616 /* If it's an ellipsis, then the list is complete. */
18617 if (token->type == CPP_ELLIPSIS)
18618 break;
18619 /* Otherwise, there must be more parameters. Consume the
18620 `,'. */
18621 cp_lexer_consume_token (parser->lexer);
18622 /* When parsing something like:
18624 int i(float f, double d)
18626 we can tell after seeing the declaration for "f" that we
18627 are not looking at an initialization of a variable "i",
18628 but rather at the declaration of a function "i".
18630 Due to the fact that the parsing of template arguments
18631 (as specified to a template-id) requires backtracking we
18632 cannot use this technique when inside a template argument
18633 list. */
18634 if (!parser->in_template_argument_list_p
18635 && !parser->in_type_id_in_expr_p
18636 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18637 /* However, a parameter-declaration of the form
18638 "float(f)" (which is a valid declaration of a
18639 parameter "f") can also be interpreted as an
18640 expression (the conversion of "f" to "float"). */
18641 && !parenthesized_p)
18642 cp_parser_commit_to_tentative_parse (parser);
18644 else
18646 cp_parser_error (parser, "expected %<,%> or %<...%>");
18647 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18648 cp_parser_skip_to_closing_parenthesis (parser,
18649 /*recovering=*/true,
18650 /*or_comma=*/false,
18651 /*consume_paren=*/false);
18652 break;
18656 parser->in_unbraced_linkage_specification_p
18657 = saved_in_unbraced_linkage_specification_p;
18659 /* Reset implicit_template_scope if we are about to leave the function
18660 parameter list that introduced it. Note that for out-of-line member
18661 definitions, there will be one or more class scopes before we get to
18662 the template parameter scope. */
18664 if (cp_binding_level *its = parser->implicit_template_scope)
18665 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18667 while (maybe_its->kind == sk_class)
18668 maybe_its = maybe_its->level_chain;
18669 if (maybe_its == its)
18671 parser->implicit_template_parms = 0;
18672 parser->implicit_template_scope = 0;
18676 return parameters;
18679 /* Parse a parameter declaration.
18681 parameter-declaration:
18682 decl-specifier-seq ... [opt] declarator
18683 decl-specifier-seq declarator = assignment-expression
18684 decl-specifier-seq ... [opt] abstract-declarator [opt]
18685 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18687 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18688 declares a template parameter. (In that case, a non-nested `>'
18689 token encountered during the parsing of the assignment-expression
18690 is not interpreted as a greater-than operator.)
18692 Returns a representation of the parameter, or NULL if an error
18693 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18694 true iff the declarator is of the form "(p)". */
18696 static cp_parameter_declarator *
18697 cp_parser_parameter_declaration (cp_parser *parser,
18698 bool template_parm_p,
18699 bool *parenthesized_p)
18701 int declares_class_or_enum;
18702 cp_decl_specifier_seq decl_specifiers;
18703 cp_declarator *declarator;
18704 tree default_argument;
18705 cp_token *token = NULL, *declarator_token_start = NULL;
18706 const char *saved_message;
18708 /* In a template parameter, `>' is not an operator.
18710 [temp.param]
18712 When parsing a default template-argument for a non-type
18713 template-parameter, the first non-nested `>' is taken as the end
18714 of the template parameter-list rather than a greater-than
18715 operator. */
18717 /* Type definitions may not appear in parameter types. */
18718 saved_message = parser->type_definition_forbidden_message;
18719 parser->type_definition_forbidden_message
18720 = G_("types may not be defined in parameter types");
18722 /* Parse the declaration-specifiers. */
18723 cp_parser_decl_specifier_seq (parser,
18724 CP_PARSER_FLAGS_NONE,
18725 &decl_specifiers,
18726 &declares_class_or_enum);
18728 /* Complain about missing 'typename' or other invalid type names. */
18729 if (!decl_specifiers.any_type_specifiers_p
18730 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18731 decl_specifiers.type = error_mark_node;
18733 /* If an error occurred, there's no reason to attempt to parse the
18734 rest of the declaration. */
18735 if (cp_parser_error_occurred (parser))
18737 parser->type_definition_forbidden_message = saved_message;
18738 return NULL;
18741 /* Peek at the next token. */
18742 token = cp_lexer_peek_token (parser->lexer);
18744 /* If the next token is a `)', `,', `=', `>', or `...', then there
18745 is no declarator. However, when variadic templates are enabled,
18746 there may be a declarator following `...'. */
18747 if (token->type == CPP_CLOSE_PAREN
18748 || token->type == CPP_COMMA
18749 || token->type == CPP_EQ
18750 || token->type == CPP_GREATER)
18752 declarator = NULL;
18753 if (parenthesized_p)
18754 *parenthesized_p = false;
18756 /* Otherwise, there should be a declarator. */
18757 else
18759 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18760 parser->default_arg_ok_p = false;
18762 /* After seeing a decl-specifier-seq, if the next token is not a
18763 "(", there is no possibility that the code is a valid
18764 expression. Therefore, if parsing tentatively, we commit at
18765 this point. */
18766 if (!parser->in_template_argument_list_p
18767 /* In an expression context, having seen:
18769 (int((char ...
18771 we cannot be sure whether we are looking at a
18772 function-type (taking a "char" as a parameter) or a cast
18773 of some object of type "char" to "int". */
18774 && !parser->in_type_id_in_expr_p
18775 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18776 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18777 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18778 cp_parser_commit_to_tentative_parse (parser);
18779 /* Parse the declarator. */
18780 declarator_token_start = token;
18781 declarator = cp_parser_declarator (parser,
18782 CP_PARSER_DECLARATOR_EITHER,
18783 /*ctor_dtor_or_conv_p=*/NULL,
18784 parenthesized_p,
18785 /*member_p=*/false,
18786 /*friend_p=*/false);
18787 parser->default_arg_ok_p = saved_default_arg_ok_p;
18788 /* After the declarator, allow more attributes. */
18789 decl_specifiers.attributes
18790 = chainon (decl_specifiers.attributes,
18791 cp_parser_attributes_opt (parser));
18794 /* If the next token is an ellipsis, and we have not seen a
18795 declarator name, and the type of the declarator contains parameter
18796 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18797 a parameter pack expansion expression. Otherwise, leave the
18798 ellipsis for a C-style variadic function. */
18799 token = cp_lexer_peek_token (parser->lexer);
18800 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18802 tree type = decl_specifiers.type;
18804 if (type && DECL_P (type))
18805 type = TREE_TYPE (type);
18807 if (type
18808 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18809 && declarator_can_be_parameter_pack (declarator)
18810 && (!declarator || !declarator->parameter_pack_p)
18811 && uses_parameter_packs (type))
18813 /* Consume the `...'. */
18814 cp_lexer_consume_token (parser->lexer);
18815 maybe_warn_variadic_templates ();
18817 /* Build a pack expansion type */
18818 if (declarator)
18819 declarator->parameter_pack_p = true;
18820 else
18821 decl_specifiers.type = make_pack_expansion (type);
18825 /* The restriction on defining new types applies only to the type
18826 of the parameter, not to the default argument. */
18827 parser->type_definition_forbidden_message = saved_message;
18829 /* If the next token is `=', then process a default argument. */
18830 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18832 token = cp_lexer_peek_token (parser->lexer);
18833 /* If we are defining a class, then the tokens that make up the
18834 default argument must be saved and processed later. */
18835 if (!template_parm_p && at_class_scope_p ()
18836 && TYPE_BEING_DEFINED (current_class_type)
18837 && !LAMBDA_TYPE_P (current_class_type))
18838 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18839 /* Outside of a class definition, we can just parse the
18840 assignment-expression. */
18841 else
18842 default_argument
18843 = cp_parser_default_argument (parser, template_parm_p);
18845 if (!parser->default_arg_ok_p)
18847 if (flag_permissive)
18848 warning (0, "deprecated use of default argument for parameter of non-function");
18849 else
18851 error_at (token->location,
18852 "default arguments are only "
18853 "permitted for function parameters");
18854 default_argument = NULL_TREE;
18857 else if ((declarator && declarator->parameter_pack_p)
18858 || (decl_specifiers.type
18859 && PACK_EXPANSION_P (decl_specifiers.type)))
18861 /* Find the name of the parameter pack. */
18862 cp_declarator *id_declarator = declarator;
18863 while (id_declarator && id_declarator->kind != cdk_id)
18864 id_declarator = id_declarator->declarator;
18866 if (id_declarator && id_declarator->kind == cdk_id)
18867 error_at (declarator_token_start->location,
18868 template_parm_p
18869 ? G_("template parameter pack %qD "
18870 "cannot have a default argument")
18871 : G_("parameter pack %qD cannot have "
18872 "a default argument"),
18873 id_declarator->u.id.unqualified_name);
18874 else
18875 error_at (declarator_token_start->location,
18876 template_parm_p
18877 ? G_("template parameter pack cannot have "
18878 "a default argument")
18879 : G_("parameter pack cannot have a "
18880 "default argument"));
18882 default_argument = NULL_TREE;
18885 else
18886 default_argument = NULL_TREE;
18888 return make_parameter_declarator (&decl_specifiers,
18889 declarator,
18890 default_argument);
18893 /* Parse a default argument and return it.
18895 TEMPLATE_PARM_P is true if this is a default argument for a
18896 non-type template parameter. */
18897 static tree
18898 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18900 tree default_argument = NULL_TREE;
18901 bool saved_greater_than_is_operator_p;
18902 bool saved_local_variables_forbidden_p;
18903 bool non_constant_p, is_direct_init;
18905 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18906 set correctly. */
18907 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18908 parser->greater_than_is_operator_p = !template_parm_p;
18909 /* Local variable names (and the `this' keyword) may not
18910 appear in a default argument. */
18911 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18912 parser->local_variables_forbidden_p = true;
18913 /* Parse the assignment-expression. */
18914 if (template_parm_p)
18915 push_deferring_access_checks (dk_no_deferred);
18916 tree saved_class_ptr = NULL_TREE;
18917 tree saved_class_ref = NULL_TREE;
18918 /* The "this" pointer is not valid in a default argument. */
18919 if (cfun)
18921 saved_class_ptr = current_class_ptr;
18922 cp_function_chain->x_current_class_ptr = NULL_TREE;
18923 saved_class_ref = current_class_ref;
18924 cp_function_chain->x_current_class_ref = NULL_TREE;
18926 default_argument
18927 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18928 /* Restore the "this" pointer. */
18929 if (cfun)
18931 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18932 cp_function_chain->x_current_class_ref = saved_class_ref;
18934 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18935 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18936 if (template_parm_p)
18937 pop_deferring_access_checks ();
18938 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18939 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18941 return default_argument;
18944 /* Parse a function-body.
18946 function-body:
18947 compound_statement */
18949 static void
18950 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18952 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18955 /* Parse a ctor-initializer-opt followed by a function-body. Return
18956 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18957 is true we are parsing a function-try-block. */
18959 static bool
18960 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18961 bool in_function_try_block)
18963 tree body, list;
18964 bool ctor_initializer_p;
18965 const bool check_body_p =
18966 DECL_CONSTRUCTOR_P (current_function_decl)
18967 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18968 tree last = NULL;
18970 /* Begin the function body. */
18971 body = begin_function_body ();
18972 /* Parse the optional ctor-initializer. */
18973 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18975 /* If we're parsing a constexpr constructor definition, we need
18976 to check that the constructor body is indeed empty. However,
18977 before we get to cp_parser_function_body lot of junk has been
18978 generated, so we can't just check that we have an empty block.
18979 Rather we take a snapshot of the outermost block, and check whether
18980 cp_parser_function_body changed its state. */
18981 if (check_body_p)
18983 list = cur_stmt_list;
18984 if (STATEMENT_LIST_TAIL (list))
18985 last = STATEMENT_LIST_TAIL (list)->stmt;
18987 /* Parse the function-body. */
18988 cp_parser_function_body (parser, in_function_try_block);
18989 if (check_body_p)
18990 check_constexpr_ctor_body (last, list);
18991 /* Finish the function body. */
18992 finish_function_body (body);
18994 return ctor_initializer_p;
18997 /* Parse an initializer.
18999 initializer:
19000 = initializer-clause
19001 ( expression-list )
19003 Returns an expression representing the initializer. If no
19004 initializer is present, NULL_TREE is returned.
19006 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19007 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19008 set to TRUE if there is no initializer present. If there is an
19009 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19010 is set to true; otherwise it is set to false. */
19012 static tree
19013 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19014 bool* non_constant_p)
19016 cp_token *token;
19017 tree init;
19019 /* Peek at the next token. */
19020 token = cp_lexer_peek_token (parser->lexer);
19022 /* Let our caller know whether or not this initializer was
19023 parenthesized. */
19024 *is_direct_init = (token->type != CPP_EQ);
19025 /* Assume that the initializer is constant. */
19026 *non_constant_p = false;
19028 if (token->type == CPP_EQ)
19030 /* Consume the `='. */
19031 cp_lexer_consume_token (parser->lexer);
19032 /* Parse the initializer-clause. */
19033 init = cp_parser_initializer_clause (parser, non_constant_p);
19035 else if (token->type == CPP_OPEN_PAREN)
19037 vec<tree, va_gc> *vec;
19038 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19039 /*cast_p=*/false,
19040 /*allow_expansion_p=*/true,
19041 non_constant_p);
19042 if (vec == NULL)
19043 return error_mark_node;
19044 init = build_tree_list_vec (vec);
19045 release_tree_vector (vec);
19047 else if (token->type == CPP_OPEN_BRACE)
19049 cp_lexer_set_source_position (parser->lexer);
19050 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19051 init = cp_parser_braced_list (parser, non_constant_p);
19052 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19054 else
19056 /* Anything else is an error. */
19057 cp_parser_error (parser, "expected initializer");
19058 init = error_mark_node;
19061 return init;
19064 /* Parse an initializer-clause.
19066 initializer-clause:
19067 assignment-expression
19068 braced-init-list
19070 Returns an expression representing the initializer.
19072 If the `assignment-expression' production is used the value
19073 returned is simply a representation for the expression.
19075 Otherwise, calls cp_parser_braced_list. */
19077 static tree
19078 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19080 tree initializer;
19082 /* Assume the expression is constant. */
19083 *non_constant_p = false;
19085 /* If it is not a `{', then we are looking at an
19086 assignment-expression. */
19087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19089 initializer
19090 = cp_parser_constant_expression (parser,
19091 /*allow_non_constant_p=*/true,
19092 non_constant_p);
19094 else
19095 initializer = cp_parser_braced_list (parser, non_constant_p);
19097 return initializer;
19100 /* Parse a brace-enclosed initializer list.
19102 braced-init-list:
19103 { initializer-list , [opt] }
19106 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19107 the elements of the initializer-list (or NULL, if the last
19108 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19109 NULL_TREE. There is no way to detect whether or not the optional
19110 trailing `,' was provided. NON_CONSTANT_P is as for
19111 cp_parser_initializer. */
19113 static tree
19114 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19116 tree initializer;
19118 /* Consume the `{' token. */
19119 cp_lexer_consume_token (parser->lexer);
19120 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19121 initializer = make_node (CONSTRUCTOR);
19122 /* If it's not a `}', then there is a non-trivial initializer. */
19123 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19125 /* Parse the initializer list. */
19126 CONSTRUCTOR_ELTS (initializer)
19127 = cp_parser_initializer_list (parser, non_constant_p);
19128 /* A trailing `,' token is allowed. */
19129 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19130 cp_lexer_consume_token (parser->lexer);
19132 else
19133 *non_constant_p = false;
19134 /* Now, there should be a trailing `}'. */
19135 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19136 TREE_TYPE (initializer) = init_list_type_node;
19137 return initializer;
19140 /* Parse an initializer-list.
19142 initializer-list:
19143 initializer-clause ... [opt]
19144 initializer-list , initializer-clause ... [opt]
19146 GNU Extension:
19148 initializer-list:
19149 designation initializer-clause ...[opt]
19150 initializer-list , designation initializer-clause ...[opt]
19152 designation:
19153 . identifier =
19154 identifier :
19155 [ constant-expression ] =
19157 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19158 for the initializer. If the INDEX of the elt is non-NULL, it is the
19159 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19160 as for cp_parser_initializer. */
19162 static vec<constructor_elt, va_gc> *
19163 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19165 vec<constructor_elt, va_gc> *v = NULL;
19167 /* Assume all of the expressions are constant. */
19168 *non_constant_p = false;
19170 /* Parse the rest of the list. */
19171 while (true)
19173 cp_token *token;
19174 tree designator;
19175 tree initializer;
19176 bool clause_non_constant_p;
19178 /* If the next token is an identifier and the following one is a
19179 colon, we are looking at the GNU designated-initializer
19180 syntax. */
19181 if (cp_parser_allow_gnu_extensions_p (parser)
19182 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19183 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19185 /* Warn the user that they are using an extension. */
19186 pedwarn (input_location, OPT_Wpedantic,
19187 "ISO C++ does not allow designated initializers");
19188 /* Consume the identifier. */
19189 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19190 /* Consume the `:'. */
19191 cp_lexer_consume_token (parser->lexer);
19193 /* Also handle the C99 syntax, '. id ='. */
19194 else if (cp_parser_allow_gnu_extensions_p (parser)
19195 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19196 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19197 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19199 /* Warn the user that they are using an extension. */
19200 pedwarn (input_location, OPT_Wpedantic,
19201 "ISO C++ does not allow C99 designated initializers");
19202 /* Consume the `.'. */
19203 cp_lexer_consume_token (parser->lexer);
19204 /* Consume the identifier. */
19205 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19206 /* Consume the `='. */
19207 cp_lexer_consume_token (parser->lexer);
19209 /* Also handle C99 array designators, '[ const ] ='. */
19210 else if (cp_parser_allow_gnu_extensions_p (parser)
19211 && !c_dialect_objc ()
19212 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19214 /* In C++11, [ could start a lambda-introducer. */
19215 bool non_const = false;
19217 cp_parser_parse_tentatively (parser);
19218 cp_lexer_consume_token (parser->lexer);
19219 designator = cp_parser_constant_expression (parser, true, &non_const);
19220 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19221 cp_parser_require (parser, CPP_EQ, RT_EQ);
19222 if (!cp_parser_parse_definitely (parser))
19223 designator = NULL_TREE;
19224 else if (non_const)
19225 require_potential_rvalue_constant_expression (designator);
19227 else
19228 designator = NULL_TREE;
19230 /* Parse the initializer. */
19231 initializer = cp_parser_initializer_clause (parser,
19232 &clause_non_constant_p);
19233 /* If any clause is non-constant, so is the entire initializer. */
19234 if (clause_non_constant_p)
19235 *non_constant_p = true;
19237 /* If we have an ellipsis, this is an initializer pack
19238 expansion. */
19239 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19241 /* Consume the `...'. */
19242 cp_lexer_consume_token (parser->lexer);
19244 /* Turn the initializer into an initializer expansion. */
19245 initializer = make_pack_expansion (initializer);
19248 /* Add it to the vector. */
19249 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19251 /* If the next token is not a comma, we have reached the end of
19252 the list. */
19253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19254 break;
19256 /* Peek at the next token. */
19257 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19258 /* If the next token is a `}', then we're still done. An
19259 initializer-clause can have a trailing `,' after the
19260 initializer-list and before the closing `}'. */
19261 if (token->type == CPP_CLOSE_BRACE)
19262 break;
19264 /* Consume the `,' token. */
19265 cp_lexer_consume_token (parser->lexer);
19268 return v;
19271 /* Classes [gram.class] */
19273 /* Parse a class-name.
19275 class-name:
19276 identifier
19277 template-id
19279 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19280 to indicate that names looked up in dependent types should be
19281 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19282 keyword has been used to indicate that the name that appears next
19283 is a template. TAG_TYPE indicates the explicit tag given before
19284 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19285 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19286 is the class being defined in a class-head.
19288 Returns the TYPE_DECL representing the class. */
19290 static tree
19291 cp_parser_class_name (cp_parser *parser,
19292 bool typename_keyword_p,
19293 bool template_keyword_p,
19294 enum tag_types tag_type,
19295 bool check_dependency_p,
19296 bool class_head_p,
19297 bool is_declaration)
19299 tree decl;
19300 tree scope;
19301 bool typename_p;
19302 cp_token *token;
19303 tree identifier = NULL_TREE;
19305 /* All class-names start with an identifier. */
19306 token = cp_lexer_peek_token (parser->lexer);
19307 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19309 cp_parser_error (parser, "expected class-name");
19310 return error_mark_node;
19313 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19314 to a template-id, so we save it here. */
19315 scope = parser->scope;
19316 if (scope == error_mark_node)
19317 return error_mark_node;
19319 /* Any name names a type if we're following the `typename' keyword
19320 in a qualified name where the enclosing scope is type-dependent. */
19321 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19322 && dependent_type_p (scope));
19323 /* Handle the common case (an identifier, but not a template-id)
19324 efficiently. */
19325 if (token->type == CPP_NAME
19326 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19328 cp_token *identifier_token;
19329 bool ambiguous_p;
19331 /* Look for the identifier. */
19332 identifier_token = cp_lexer_peek_token (parser->lexer);
19333 ambiguous_p = identifier_token->error_reported;
19334 identifier = cp_parser_identifier (parser);
19335 /* If the next token isn't an identifier, we are certainly not
19336 looking at a class-name. */
19337 if (identifier == error_mark_node)
19338 decl = error_mark_node;
19339 /* If we know this is a type-name, there's no need to look it
19340 up. */
19341 else if (typename_p)
19342 decl = identifier;
19343 else
19345 tree ambiguous_decls;
19346 /* If we already know that this lookup is ambiguous, then
19347 we've already issued an error message; there's no reason
19348 to check again. */
19349 if (ambiguous_p)
19351 cp_parser_simulate_error (parser);
19352 return error_mark_node;
19354 /* If the next token is a `::', then the name must be a type
19355 name.
19357 [basic.lookup.qual]
19359 During the lookup for a name preceding the :: scope
19360 resolution operator, object, function, and enumerator
19361 names are ignored. */
19362 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19363 tag_type = typename_type;
19364 /* Look up the name. */
19365 decl = cp_parser_lookup_name (parser, identifier,
19366 tag_type,
19367 /*is_template=*/false,
19368 /*is_namespace=*/false,
19369 check_dependency_p,
19370 &ambiguous_decls,
19371 identifier_token->location);
19372 if (ambiguous_decls)
19374 if (cp_parser_parsing_tentatively (parser))
19375 cp_parser_simulate_error (parser);
19376 return error_mark_node;
19380 else
19382 /* Try a template-id. */
19383 decl = cp_parser_template_id (parser, template_keyword_p,
19384 check_dependency_p,
19385 tag_type,
19386 is_declaration);
19387 if (decl == error_mark_node)
19388 return error_mark_node;
19391 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19393 /* If this is a typename, create a TYPENAME_TYPE. */
19394 if (typename_p && decl != error_mark_node)
19396 decl = make_typename_type (scope, decl, typename_type,
19397 /*complain=*/tf_error);
19398 if (decl != error_mark_node)
19399 decl = TYPE_NAME (decl);
19402 decl = strip_using_decl (decl);
19404 /* Check to see that it is really the name of a class. */
19405 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19406 && identifier_p (TREE_OPERAND (decl, 0))
19407 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19408 /* Situations like this:
19410 template <typename T> struct A {
19411 typename T::template X<int>::I i;
19414 are problematic. Is `T::template X<int>' a class-name? The
19415 standard does not seem to be definitive, but there is no other
19416 valid interpretation of the following `::'. Therefore, those
19417 names are considered class-names. */
19419 decl = make_typename_type (scope, decl, tag_type, tf_error);
19420 if (decl != error_mark_node)
19421 decl = TYPE_NAME (decl);
19423 else if (TREE_CODE (decl) != TYPE_DECL
19424 || TREE_TYPE (decl) == error_mark_node
19425 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19426 /* In Objective-C 2.0, a classname followed by '.' starts a
19427 dot-syntax expression, and it's not a type-name. */
19428 || (c_dialect_objc ()
19429 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19430 && objc_is_class_name (decl)))
19431 decl = error_mark_node;
19433 if (decl == error_mark_node)
19434 cp_parser_error (parser, "expected class-name");
19435 else if (identifier && !parser->scope)
19436 maybe_note_name_used_in_class (identifier, decl);
19438 return decl;
19441 /* Parse a class-specifier.
19443 class-specifier:
19444 class-head { member-specification [opt] }
19446 Returns the TREE_TYPE representing the class. */
19448 static tree
19449 cp_parser_class_specifier_1 (cp_parser* parser)
19451 tree type;
19452 tree attributes = NULL_TREE;
19453 bool nested_name_specifier_p;
19454 unsigned saved_num_template_parameter_lists;
19455 bool saved_in_function_body;
19456 unsigned char in_statement;
19457 bool in_switch_statement_p;
19458 bool saved_in_unbraced_linkage_specification_p;
19459 tree old_scope = NULL_TREE;
19460 tree scope = NULL_TREE;
19461 cp_token *closing_brace;
19463 push_deferring_access_checks (dk_no_deferred);
19465 /* Parse the class-head. */
19466 type = cp_parser_class_head (parser,
19467 &nested_name_specifier_p);
19468 /* If the class-head was a semantic disaster, skip the entire body
19469 of the class. */
19470 if (!type)
19472 cp_parser_skip_to_end_of_block_or_statement (parser);
19473 pop_deferring_access_checks ();
19474 return error_mark_node;
19477 /* Look for the `{'. */
19478 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19480 pop_deferring_access_checks ();
19481 return error_mark_node;
19484 cp_ensure_no_omp_declare_simd (parser);
19486 /* Issue an error message if type-definitions are forbidden here. */
19487 cp_parser_check_type_definition (parser);
19488 /* Remember that we are defining one more class. */
19489 ++parser->num_classes_being_defined;
19490 /* Inside the class, surrounding template-parameter-lists do not
19491 apply. */
19492 saved_num_template_parameter_lists
19493 = parser->num_template_parameter_lists;
19494 parser->num_template_parameter_lists = 0;
19495 /* We are not in a function body. */
19496 saved_in_function_body = parser->in_function_body;
19497 parser->in_function_body = false;
19498 /* Or in a loop. */
19499 in_statement = parser->in_statement;
19500 parser->in_statement = 0;
19501 /* Or in a switch. */
19502 in_switch_statement_p = parser->in_switch_statement_p;
19503 parser->in_switch_statement_p = false;
19504 /* We are not immediately inside an extern "lang" block. */
19505 saved_in_unbraced_linkage_specification_p
19506 = parser->in_unbraced_linkage_specification_p;
19507 parser->in_unbraced_linkage_specification_p = false;
19509 /* Start the class. */
19510 if (nested_name_specifier_p)
19512 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19513 old_scope = push_inner_scope (scope);
19515 type = begin_class_definition (type);
19517 if (type == error_mark_node)
19518 /* If the type is erroneous, skip the entire body of the class. */
19519 cp_parser_skip_to_closing_brace (parser);
19520 else
19521 /* Parse the member-specification. */
19522 cp_parser_member_specification_opt (parser);
19524 /* Look for the trailing `}'. */
19525 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19526 /* Look for trailing attributes to apply to this class. */
19527 if (cp_parser_allow_gnu_extensions_p (parser))
19528 attributes = cp_parser_gnu_attributes_opt (parser);
19529 if (type != error_mark_node)
19530 type = finish_struct (type, attributes);
19531 if (nested_name_specifier_p)
19532 pop_inner_scope (old_scope, scope);
19534 /* We've finished a type definition. Check for the common syntax
19535 error of forgetting a semicolon after the definition. We need to
19536 be careful, as we can't just check for not-a-semicolon and be done
19537 with it; the user might have typed:
19539 class X { } c = ...;
19540 class X { } *p = ...;
19542 and so forth. Instead, enumerate all the possible tokens that
19543 might follow this production; if we don't see one of them, then
19544 complain and silently insert the semicolon. */
19546 cp_token *token = cp_lexer_peek_token (parser->lexer);
19547 bool want_semicolon = true;
19549 if (cp_next_tokens_can_be_std_attribute_p (parser))
19550 /* Don't try to parse c++11 attributes here. As per the
19551 grammar, that should be a task for
19552 cp_parser_decl_specifier_seq. */
19553 want_semicolon = false;
19555 switch (token->type)
19557 case CPP_NAME:
19558 case CPP_SEMICOLON:
19559 case CPP_MULT:
19560 case CPP_AND:
19561 case CPP_OPEN_PAREN:
19562 case CPP_CLOSE_PAREN:
19563 case CPP_COMMA:
19564 want_semicolon = false;
19565 break;
19567 /* While it's legal for type qualifiers and storage class
19568 specifiers to follow type definitions in the grammar, only
19569 compiler testsuites contain code like that. Assume that if
19570 we see such code, then what we're really seeing is a case
19571 like:
19573 class X { }
19574 const <type> var = ...;
19578 class Y { }
19579 static <type> func (...) ...
19581 i.e. the qualifier or specifier applies to the next
19582 declaration. To do so, however, we need to look ahead one
19583 more token to see if *that* token is a type specifier.
19585 This code could be improved to handle:
19587 class Z { }
19588 static const <type> var = ...; */
19589 case CPP_KEYWORD:
19590 if (keyword_is_decl_specifier (token->keyword))
19592 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19594 /* Handling user-defined types here would be nice, but very
19595 tricky. */
19596 want_semicolon
19597 = (lookahead->type == CPP_KEYWORD
19598 && keyword_begins_type_specifier (lookahead->keyword));
19600 break;
19601 default:
19602 break;
19605 /* If we don't have a type, then something is very wrong and we
19606 shouldn't try to do anything clever. Likewise for not seeing the
19607 closing brace. */
19608 if (closing_brace && TYPE_P (type) && want_semicolon)
19610 cp_token_position prev
19611 = cp_lexer_previous_token_position (parser->lexer);
19612 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19613 location_t loc = prev_token->location;
19615 if (CLASSTYPE_DECLARED_CLASS (type))
19616 error_at (loc, "expected %<;%> after class definition");
19617 else if (TREE_CODE (type) == RECORD_TYPE)
19618 error_at (loc, "expected %<;%> after struct definition");
19619 else if (TREE_CODE (type) == UNION_TYPE)
19620 error_at (loc, "expected %<;%> after union definition");
19621 else
19622 gcc_unreachable ();
19624 /* Unget one token and smash it to look as though we encountered
19625 a semicolon in the input stream. */
19626 cp_lexer_set_token_position (parser->lexer, prev);
19627 token = cp_lexer_peek_token (parser->lexer);
19628 token->type = CPP_SEMICOLON;
19629 token->keyword = RID_MAX;
19633 /* If this class is not itself within the scope of another class,
19634 then we need to parse the bodies of all of the queued function
19635 definitions. Note that the queued functions defined in a class
19636 are not always processed immediately following the
19637 class-specifier for that class. Consider:
19639 struct A {
19640 struct B { void f() { sizeof (A); } };
19643 If `f' were processed before the processing of `A' were
19644 completed, there would be no way to compute the size of `A'.
19645 Note that the nesting we are interested in here is lexical --
19646 not the semantic nesting given by TYPE_CONTEXT. In particular,
19647 for:
19649 struct A { struct B; };
19650 struct A::B { void f() { } };
19652 there is no need to delay the parsing of `A::B::f'. */
19653 if (--parser->num_classes_being_defined == 0)
19655 tree decl;
19656 tree class_type = NULL_TREE;
19657 tree pushed_scope = NULL_TREE;
19658 unsigned ix;
19659 cp_default_arg_entry *e;
19660 tree save_ccp, save_ccr;
19662 /* In a first pass, parse default arguments to the functions.
19663 Then, in a second pass, parse the bodies of the functions.
19664 This two-phased approach handles cases like:
19666 struct S {
19667 void f() { g(); }
19668 void g(int i = 3);
19672 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19674 decl = e->decl;
19675 /* If there are default arguments that have not yet been processed,
19676 take care of them now. */
19677 if (class_type != e->class_type)
19679 if (pushed_scope)
19680 pop_scope (pushed_scope);
19681 class_type = e->class_type;
19682 pushed_scope = push_scope (class_type);
19684 /* Make sure that any template parameters are in scope. */
19685 maybe_begin_member_template_processing (decl);
19686 /* Parse the default argument expressions. */
19687 cp_parser_late_parsing_default_args (parser, decl);
19688 /* Remove any template parameters from the symbol table. */
19689 maybe_end_member_template_processing ();
19691 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19692 /* Now parse any NSDMIs. */
19693 save_ccp = current_class_ptr;
19694 save_ccr = current_class_ref;
19695 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19697 if (class_type != DECL_CONTEXT (decl))
19699 if (pushed_scope)
19700 pop_scope (pushed_scope);
19701 class_type = DECL_CONTEXT (decl);
19702 pushed_scope = push_scope (class_type);
19704 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19705 cp_parser_late_parsing_nsdmi (parser, decl);
19707 vec_safe_truncate (unparsed_nsdmis, 0);
19708 current_class_ptr = save_ccp;
19709 current_class_ref = save_ccr;
19710 if (pushed_scope)
19711 pop_scope (pushed_scope);
19713 /* Now do some post-NSDMI bookkeeping. */
19714 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19715 after_nsdmi_defaulted_late_checks (class_type);
19716 vec_safe_truncate (unparsed_classes, 0);
19717 after_nsdmi_defaulted_late_checks (type);
19719 /* Now parse the body of the functions. */
19720 if (flag_openmp)
19722 /* OpenMP UDRs need to be parsed before all other functions. */
19723 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19724 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19725 cp_parser_late_parsing_for_member (parser, decl);
19726 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19727 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19728 cp_parser_late_parsing_for_member (parser, decl);
19730 else
19731 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19732 cp_parser_late_parsing_for_member (parser, decl);
19733 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19735 else
19736 vec_safe_push (unparsed_classes, type);
19738 /* Put back any saved access checks. */
19739 pop_deferring_access_checks ();
19741 /* Restore saved state. */
19742 parser->in_switch_statement_p = in_switch_statement_p;
19743 parser->in_statement = in_statement;
19744 parser->in_function_body = saved_in_function_body;
19745 parser->num_template_parameter_lists
19746 = saved_num_template_parameter_lists;
19747 parser->in_unbraced_linkage_specification_p
19748 = saved_in_unbraced_linkage_specification_p;
19750 return type;
19753 static tree
19754 cp_parser_class_specifier (cp_parser* parser)
19756 tree ret;
19757 timevar_push (TV_PARSE_STRUCT);
19758 ret = cp_parser_class_specifier_1 (parser);
19759 timevar_pop (TV_PARSE_STRUCT);
19760 return ret;
19763 /* Parse a class-head.
19765 class-head:
19766 class-key identifier [opt] base-clause [opt]
19767 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19768 class-key nested-name-specifier [opt] template-id
19769 base-clause [opt]
19771 class-virt-specifier:
19772 final
19774 GNU Extensions:
19775 class-key attributes identifier [opt] base-clause [opt]
19776 class-key attributes nested-name-specifier identifier base-clause [opt]
19777 class-key attributes nested-name-specifier [opt] template-id
19778 base-clause [opt]
19780 Upon return BASES is initialized to the list of base classes (or
19781 NULL, if there are none) in the same form returned by
19782 cp_parser_base_clause.
19784 Returns the TYPE of the indicated class. Sets
19785 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19786 involving a nested-name-specifier was used, and FALSE otherwise.
19788 Returns error_mark_node if this is not a class-head.
19790 Returns NULL_TREE if the class-head is syntactically valid, but
19791 semantically invalid in a way that means we should skip the entire
19792 body of the class. */
19794 static tree
19795 cp_parser_class_head (cp_parser* parser,
19796 bool* nested_name_specifier_p)
19798 tree nested_name_specifier;
19799 enum tag_types class_key;
19800 tree id = NULL_TREE;
19801 tree type = NULL_TREE;
19802 tree attributes;
19803 tree bases;
19804 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19805 bool template_id_p = false;
19806 bool qualified_p = false;
19807 bool invalid_nested_name_p = false;
19808 bool invalid_explicit_specialization_p = false;
19809 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19810 tree pushed_scope = NULL_TREE;
19811 unsigned num_templates;
19812 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19813 /* Assume no nested-name-specifier will be present. */
19814 *nested_name_specifier_p = false;
19815 /* Assume no template parameter lists will be used in defining the
19816 type. */
19817 num_templates = 0;
19818 parser->colon_corrects_to_scope_p = false;
19820 /* Look for the class-key. */
19821 class_key = cp_parser_class_key (parser);
19822 if (class_key == none_type)
19823 return error_mark_node;
19825 /* Parse the attributes. */
19826 attributes = cp_parser_attributes_opt (parser);
19828 /* If the next token is `::', that is invalid -- but sometimes
19829 people do try to write:
19831 struct ::S {};
19833 Handle this gracefully by accepting the extra qualifier, and then
19834 issuing an error about it later if this really is a
19835 class-head. If it turns out just to be an elaborated type
19836 specifier, remain silent. */
19837 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19838 qualified_p = true;
19840 push_deferring_access_checks (dk_no_check);
19842 /* Determine the name of the class. Begin by looking for an
19843 optional nested-name-specifier. */
19844 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19845 nested_name_specifier
19846 = cp_parser_nested_name_specifier_opt (parser,
19847 /*typename_keyword_p=*/false,
19848 /*check_dependency_p=*/false,
19849 /*type_p=*/true,
19850 /*is_declaration=*/false);
19851 /* If there was a nested-name-specifier, then there *must* be an
19852 identifier. */
19853 if (nested_name_specifier)
19855 type_start_token = cp_lexer_peek_token (parser->lexer);
19856 /* Although the grammar says `identifier', it really means
19857 `class-name' or `template-name'. You are only allowed to
19858 define a class that has already been declared with this
19859 syntax.
19861 The proposed resolution for Core Issue 180 says that wherever
19862 you see `class T::X' you should treat `X' as a type-name.
19864 It is OK to define an inaccessible class; for example:
19866 class A { class B; };
19867 class A::B {};
19869 We do not know if we will see a class-name, or a
19870 template-name. We look for a class-name first, in case the
19871 class-name is a template-id; if we looked for the
19872 template-name first we would stop after the template-name. */
19873 cp_parser_parse_tentatively (parser);
19874 type = cp_parser_class_name (parser,
19875 /*typename_keyword_p=*/false,
19876 /*template_keyword_p=*/false,
19877 class_type,
19878 /*check_dependency_p=*/false,
19879 /*class_head_p=*/true,
19880 /*is_declaration=*/false);
19881 /* If that didn't work, ignore the nested-name-specifier. */
19882 if (!cp_parser_parse_definitely (parser))
19884 invalid_nested_name_p = true;
19885 type_start_token = cp_lexer_peek_token (parser->lexer);
19886 id = cp_parser_identifier (parser);
19887 if (id == error_mark_node)
19888 id = NULL_TREE;
19890 /* If we could not find a corresponding TYPE, treat this
19891 declaration like an unqualified declaration. */
19892 if (type == error_mark_node)
19893 nested_name_specifier = NULL_TREE;
19894 /* Otherwise, count the number of templates used in TYPE and its
19895 containing scopes. */
19896 else
19898 tree scope;
19900 for (scope = TREE_TYPE (type);
19901 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19902 scope = get_containing_scope (scope))
19903 if (TYPE_P (scope)
19904 && CLASS_TYPE_P (scope)
19905 && CLASSTYPE_TEMPLATE_INFO (scope)
19906 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19907 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19908 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19909 ++num_templates;
19912 /* Otherwise, the identifier is optional. */
19913 else
19915 /* We don't know whether what comes next is a template-id,
19916 an identifier, or nothing at all. */
19917 cp_parser_parse_tentatively (parser);
19918 /* Check for a template-id. */
19919 type_start_token = cp_lexer_peek_token (parser->lexer);
19920 id = cp_parser_template_id (parser,
19921 /*template_keyword_p=*/false,
19922 /*check_dependency_p=*/true,
19923 class_key,
19924 /*is_declaration=*/true);
19925 /* If that didn't work, it could still be an identifier. */
19926 if (!cp_parser_parse_definitely (parser))
19928 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19930 type_start_token = cp_lexer_peek_token (parser->lexer);
19931 id = cp_parser_identifier (parser);
19933 else
19934 id = NULL_TREE;
19936 else
19938 template_id_p = true;
19939 ++num_templates;
19943 pop_deferring_access_checks ();
19945 if (id)
19947 cp_parser_check_for_invalid_template_id (parser, id,
19948 class_key,
19949 type_start_token->location);
19951 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19953 /* If it's not a `:' or a `{' then we can't really be looking at a
19954 class-head, since a class-head only appears as part of a
19955 class-specifier. We have to detect this situation before calling
19956 xref_tag, since that has irreversible side-effects. */
19957 if (!cp_parser_next_token_starts_class_definition_p (parser))
19959 cp_parser_error (parser, "expected %<{%> or %<:%>");
19960 type = error_mark_node;
19961 goto out;
19964 /* At this point, we're going ahead with the class-specifier, even
19965 if some other problem occurs. */
19966 cp_parser_commit_to_tentative_parse (parser);
19967 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19969 cp_parser_error (parser,
19970 "cannot specify %<override%> for a class");
19971 type = error_mark_node;
19972 goto out;
19974 /* Issue the error about the overly-qualified name now. */
19975 if (qualified_p)
19977 cp_parser_error (parser,
19978 "global qualification of class name is invalid");
19979 type = error_mark_node;
19980 goto out;
19982 else if (invalid_nested_name_p)
19984 cp_parser_error (parser,
19985 "qualified name does not name a class");
19986 type = error_mark_node;
19987 goto out;
19989 else if (nested_name_specifier)
19991 tree scope;
19993 /* Reject typedef-names in class heads. */
19994 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19996 error_at (type_start_token->location,
19997 "invalid class name in declaration of %qD",
19998 type);
19999 type = NULL_TREE;
20000 goto done;
20003 /* Figure out in what scope the declaration is being placed. */
20004 scope = current_scope ();
20005 /* If that scope does not contain the scope in which the
20006 class was originally declared, the program is invalid. */
20007 if (scope && !is_ancestor (scope, nested_name_specifier))
20009 if (at_namespace_scope_p ())
20010 error_at (type_start_token->location,
20011 "declaration of %qD in namespace %qD which does not "
20012 "enclose %qD",
20013 type, scope, nested_name_specifier);
20014 else
20015 error_at (type_start_token->location,
20016 "declaration of %qD in %qD which does not enclose %qD",
20017 type, scope, nested_name_specifier);
20018 type = NULL_TREE;
20019 goto done;
20021 /* [dcl.meaning]
20023 A declarator-id shall not be qualified except for the
20024 definition of a ... nested class outside of its class
20025 ... [or] the definition or explicit instantiation of a
20026 class member of a namespace outside of its namespace. */
20027 if (scope == nested_name_specifier)
20029 permerror (nested_name_specifier_token_start->location,
20030 "extra qualification not allowed");
20031 nested_name_specifier = NULL_TREE;
20032 num_templates = 0;
20035 /* An explicit-specialization must be preceded by "template <>". If
20036 it is not, try to recover gracefully. */
20037 if (at_namespace_scope_p ()
20038 && parser->num_template_parameter_lists == 0
20039 && template_id_p)
20041 error_at (type_start_token->location,
20042 "an explicit specialization must be preceded by %<template <>%>");
20043 invalid_explicit_specialization_p = true;
20044 /* Take the same action that would have been taken by
20045 cp_parser_explicit_specialization. */
20046 ++parser->num_template_parameter_lists;
20047 begin_specialization ();
20049 /* There must be no "return" statements between this point and the
20050 end of this function; set "type "to the correct return value and
20051 use "goto done;" to return. */
20052 /* Make sure that the right number of template parameters were
20053 present. */
20054 if (!cp_parser_check_template_parameters (parser, num_templates,
20055 type_start_token->location,
20056 /*declarator=*/NULL))
20058 /* If something went wrong, there is no point in even trying to
20059 process the class-definition. */
20060 type = NULL_TREE;
20061 goto done;
20064 /* Look up the type. */
20065 if (template_id_p)
20067 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20068 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20069 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20071 error_at (type_start_token->location,
20072 "function template %qD redeclared as a class template", id);
20073 type = error_mark_node;
20075 else
20077 type = TREE_TYPE (id);
20078 type = maybe_process_partial_specialization (type);
20080 if (nested_name_specifier)
20081 pushed_scope = push_scope (nested_name_specifier);
20083 else if (nested_name_specifier)
20085 tree class_type;
20087 /* Given:
20089 template <typename T> struct S { struct T };
20090 template <typename T> struct S<T>::T { };
20092 we will get a TYPENAME_TYPE when processing the definition of
20093 `S::T'. We need to resolve it to the actual type before we
20094 try to define it. */
20095 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20097 class_type = resolve_typename_type (TREE_TYPE (type),
20098 /*only_current_p=*/false);
20099 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20100 type = TYPE_NAME (class_type);
20101 else
20103 cp_parser_error (parser, "could not resolve typename type");
20104 type = error_mark_node;
20108 if (maybe_process_partial_specialization (TREE_TYPE (type))
20109 == error_mark_node)
20111 type = NULL_TREE;
20112 goto done;
20115 class_type = current_class_type;
20116 /* Enter the scope indicated by the nested-name-specifier. */
20117 pushed_scope = push_scope (nested_name_specifier);
20118 /* Get the canonical version of this type. */
20119 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20120 /* Call push_template_decl if it seems like we should be defining a
20121 template either from the template headers or the type we're
20122 defining, so that we diagnose both extra and missing headers. */
20123 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20124 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
20125 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
20126 (TREE_TYPE (type)))))
20127 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20129 type = push_template_decl (type);
20130 if (type == error_mark_node)
20132 type = NULL_TREE;
20133 goto done;
20137 type = TREE_TYPE (type);
20138 *nested_name_specifier_p = true;
20140 else /* The name is not a nested name. */
20142 /* If the class was unnamed, create a dummy name. */
20143 if (!id)
20144 id = make_anon_name ();
20145 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20146 parser->num_template_parameter_lists);
20149 /* Indicate whether this class was declared as a `class' or as a
20150 `struct'. */
20151 if (TREE_CODE (type) == RECORD_TYPE)
20152 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20153 cp_parser_check_class_key (class_key, type);
20155 /* If this type was already complete, and we see another definition,
20156 that's an error. */
20157 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20159 error_at (type_start_token->location, "redefinition of %q#T",
20160 type);
20161 error_at (type_start_token->location, "previous definition of %q+#T",
20162 type);
20163 type = NULL_TREE;
20164 goto done;
20166 else if (type == error_mark_node)
20167 type = NULL_TREE;
20169 if (type)
20171 /* Apply attributes now, before any use of the class as a template
20172 argument in its base list. */
20173 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20174 fixup_attribute_variants (type);
20177 /* We will have entered the scope containing the class; the names of
20178 base classes should be looked up in that context. For example:
20180 struct A { struct B {}; struct C; };
20181 struct A::C : B {};
20183 is valid. */
20185 /* Get the list of base-classes, if there is one. */
20186 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20188 /* PR59482: enter the class scope so that base-specifiers are looked
20189 up correctly. */
20190 if (type)
20191 pushclass (type);
20192 bases = cp_parser_base_clause (parser);
20193 /* PR59482: get out of the previously pushed class scope so that the
20194 subsequent pops pop the right thing. */
20195 if (type)
20196 popclass ();
20198 else
20199 bases = NULL_TREE;
20201 /* If we're really defining a class, process the base classes.
20202 If they're invalid, fail. */
20203 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20204 && !xref_basetypes (type, bases))
20205 type = NULL_TREE;
20207 done:
20208 /* Leave the scope given by the nested-name-specifier. We will
20209 enter the class scope itself while processing the members. */
20210 if (pushed_scope)
20211 pop_scope (pushed_scope);
20213 if (invalid_explicit_specialization_p)
20215 end_specialization ();
20216 --parser->num_template_parameter_lists;
20219 if (type)
20220 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20221 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20222 CLASSTYPE_FINAL (type) = 1;
20223 out:
20224 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20225 return type;
20228 /* Parse a class-key.
20230 class-key:
20231 class
20232 struct
20233 union
20235 Returns the kind of class-key specified, or none_type to indicate
20236 error. */
20238 static enum tag_types
20239 cp_parser_class_key (cp_parser* parser)
20241 cp_token *token;
20242 enum tag_types tag_type;
20244 /* Look for the class-key. */
20245 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20246 if (!token)
20247 return none_type;
20249 /* Check to see if the TOKEN is a class-key. */
20250 tag_type = cp_parser_token_is_class_key (token);
20251 if (!tag_type)
20252 cp_parser_error (parser, "expected class-key");
20253 return tag_type;
20256 /* Parse a type-parameter-key.
20258 type-parameter-key:
20259 class
20260 typename
20263 static void
20264 cp_parser_type_parameter_key (cp_parser* parser)
20266 /* Look for the type-parameter-key. */
20267 enum tag_types tag_type = none_type;
20268 cp_token *token = cp_lexer_peek_token (parser->lexer);
20269 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20271 cp_lexer_consume_token (parser->lexer);
20272 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20273 /* typename is not allowed in a template template parameter
20274 by the standard until C++1Z. */
20275 pedwarn (token->location, OPT_Wpedantic,
20276 "ISO C++ forbids typename key in template template parameter;"
20277 " use -std=c++1z or -std=gnu++1z");
20279 else
20280 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20282 return;
20285 /* Parse an (optional) member-specification.
20287 member-specification:
20288 member-declaration member-specification [opt]
20289 access-specifier : member-specification [opt] */
20291 static void
20292 cp_parser_member_specification_opt (cp_parser* parser)
20294 while (true)
20296 cp_token *token;
20297 enum rid keyword;
20299 /* Peek at the next token. */
20300 token = cp_lexer_peek_token (parser->lexer);
20301 /* If it's a `}', or EOF then we've seen all the members. */
20302 if (token->type == CPP_CLOSE_BRACE
20303 || token->type == CPP_EOF
20304 || token->type == CPP_PRAGMA_EOL)
20305 break;
20307 /* See if this token is a keyword. */
20308 keyword = token->keyword;
20309 switch (keyword)
20311 case RID_PUBLIC:
20312 case RID_PROTECTED:
20313 case RID_PRIVATE:
20314 /* Consume the access-specifier. */
20315 cp_lexer_consume_token (parser->lexer);
20316 /* Remember which access-specifier is active. */
20317 current_access_specifier = token->u.value;
20318 /* Look for the `:'. */
20319 cp_parser_require (parser, CPP_COLON, RT_COLON);
20320 break;
20322 default:
20323 /* Accept #pragmas at class scope. */
20324 if (token->type == CPP_PRAGMA)
20326 cp_parser_pragma (parser, pragma_member);
20327 break;
20330 /* Otherwise, the next construction must be a
20331 member-declaration. */
20332 cp_parser_member_declaration (parser);
20337 /* Parse a member-declaration.
20339 member-declaration:
20340 decl-specifier-seq [opt] member-declarator-list [opt] ;
20341 function-definition ; [opt]
20342 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20343 using-declaration
20344 template-declaration
20345 alias-declaration
20347 member-declarator-list:
20348 member-declarator
20349 member-declarator-list , member-declarator
20351 member-declarator:
20352 declarator pure-specifier [opt]
20353 declarator constant-initializer [opt]
20354 identifier [opt] : constant-expression
20356 GNU Extensions:
20358 member-declaration:
20359 __extension__ member-declaration
20361 member-declarator:
20362 declarator attributes [opt] pure-specifier [opt]
20363 declarator attributes [opt] constant-initializer [opt]
20364 identifier [opt] attributes [opt] : constant-expression
20366 C++0x Extensions:
20368 member-declaration:
20369 static_assert-declaration */
20371 static void
20372 cp_parser_member_declaration (cp_parser* parser)
20374 cp_decl_specifier_seq decl_specifiers;
20375 tree prefix_attributes;
20376 tree decl;
20377 int declares_class_or_enum;
20378 bool friend_p;
20379 cp_token *token = NULL;
20380 cp_token *decl_spec_token_start = NULL;
20381 cp_token *initializer_token_start = NULL;
20382 int saved_pedantic;
20383 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20385 /* Check for the `__extension__' keyword. */
20386 if (cp_parser_extension_opt (parser, &saved_pedantic))
20388 /* Recurse. */
20389 cp_parser_member_declaration (parser);
20390 /* Restore the old value of the PEDANTIC flag. */
20391 pedantic = saved_pedantic;
20393 return;
20396 /* Check for a template-declaration. */
20397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20399 /* An explicit specialization here is an error condition, and we
20400 expect the specialization handler to detect and report this. */
20401 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20402 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20403 cp_parser_explicit_specialization (parser);
20404 else
20405 cp_parser_template_declaration (parser, /*member_p=*/true);
20407 return;
20410 /* Check for a using-declaration. */
20411 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20413 if (cxx_dialect < cxx11)
20415 /* Parse the using-declaration. */
20416 cp_parser_using_declaration (parser,
20417 /*access_declaration_p=*/false);
20418 return;
20420 else
20422 tree decl;
20423 bool alias_decl_expected;
20424 cp_parser_parse_tentatively (parser);
20425 decl = cp_parser_alias_declaration (parser);
20426 /* Note that if we actually see the '=' token after the
20427 identifier, cp_parser_alias_declaration commits the
20428 tentative parse. In that case, we really expects an
20429 alias-declaration. Otherwise, we expect a using
20430 declaration. */
20431 alias_decl_expected =
20432 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20433 cp_parser_parse_definitely (parser);
20435 if (alias_decl_expected)
20436 finish_member_declaration (decl);
20437 else
20438 cp_parser_using_declaration (parser,
20439 /*access_declaration_p=*/false);
20440 return;
20444 /* Check for @defs. */
20445 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20447 tree ivar, member;
20448 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20449 ivar = ivar_chains;
20450 while (ivar)
20452 member = ivar;
20453 ivar = TREE_CHAIN (member);
20454 TREE_CHAIN (member) = NULL_TREE;
20455 finish_member_declaration (member);
20457 return;
20460 /* If the next token is `static_assert' we have a static assertion. */
20461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20463 cp_parser_static_assert (parser, /*member_p=*/true);
20464 return;
20467 parser->colon_corrects_to_scope_p = false;
20469 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20470 goto out;
20472 /* Parse the decl-specifier-seq. */
20473 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20474 cp_parser_decl_specifier_seq (parser,
20475 CP_PARSER_FLAGS_OPTIONAL,
20476 &decl_specifiers,
20477 &declares_class_or_enum);
20478 /* Check for an invalid type-name. */
20479 if (!decl_specifiers.any_type_specifiers_p
20480 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20481 goto out;
20482 /* If there is no declarator, then the decl-specifier-seq should
20483 specify a type. */
20484 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20486 /* If there was no decl-specifier-seq, and the next token is a
20487 `;', then we have something like:
20489 struct S { ; };
20491 [class.mem]
20493 Each member-declaration shall declare at least one member
20494 name of the class. */
20495 if (!decl_specifiers.any_specifiers_p)
20497 cp_token *token = cp_lexer_peek_token (parser->lexer);
20498 if (!in_system_header_at (token->location))
20499 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20501 else
20503 tree type;
20505 /* See if this declaration is a friend. */
20506 friend_p = cp_parser_friend_p (&decl_specifiers);
20507 /* If there were decl-specifiers, check to see if there was
20508 a class-declaration. */
20509 type = check_tag_decl (&decl_specifiers,
20510 /*explicit_type_instantiation_p=*/false);
20511 /* Nested classes have already been added to the class, but
20512 a `friend' needs to be explicitly registered. */
20513 if (friend_p)
20515 /* If the `friend' keyword was present, the friend must
20516 be introduced with a class-key. */
20517 if (!declares_class_or_enum && cxx_dialect < cxx11)
20518 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20519 "in C++03 a class-key must be used "
20520 "when declaring a friend");
20521 /* In this case:
20523 template <typename T> struct A {
20524 friend struct A<T>::B;
20527 A<T>::B will be represented by a TYPENAME_TYPE, and
20528 therefore not recognized by check_tag_decl. */
20529 if (!type)
20531 type = decl_specifiers.type;
20532 if (type && TREE_CODE (type) == TYPE_DECL)
20533 type = TREE_TYPE (type);
20535 if (!type || !TYPE_P (type))
20536 error_at (decl_spec_token_start->location,
20537 "friend declaration does not name a class or "
20538 "function");
20539 else
20540 make_friend_class (current_class_type, type,
20541 /*complain=*/true);
20543 /* If there is no TYPE, an error message will already have
20544 been issued. */
20545 else if (!type || type == error_mark_node)
20547 /* An anonymous aggregate has to be handled specially; such
20548 a declaration really declares a data member (with a
20549 particular type), as opposed to a nested class. */
20550 else if (ANON_AGGR_TYPE_P (type))
20552 /* C++11 9.5/6. */
20553 if (decl_specifiers.storage_class != sc_none)
20554 error_at (decl_spec_token_start->location,
20555 "a storage class on an anonymous aggregate "
20556 "in class scope is not allowed");
20558 /* Remove constructors and such from TYPE, now that we
20559 know it is an anonymous aggregate. */
20560 fixup_anonymous_aggr (type);
20561 /* And make the corresponding data member. */
20562 decl = build_decl (decl_spec_token_start->location,
20563 FIELD_DECL, NULL_TREE, type);
20564 /* Add it to the class. */
20565 finish_member_declaration (decl);
20567 else
20568 cp_parser_check_access_in_redeclaration
20569 (TYPE_NAME (type),
20570 decl_spec_token_start->location);
20573 else
20575 bool assume_semicolon = false;
20577 /* Clear attributes from the decl_specifiers but keep them
20578 around as prefix attributes that apply them to the entity
20579 being declared. */
20580 prefix_attributes = decl_specifiers.attributes;
20581 decl_specifiers.attributes = NULL_TREE;
20583 /* See if these declarations will be friends. */
20584 friend_p = cp_parser_friend_p (&decl_specifiers);
20586 /* Keep going until we hit the `;' at the end of the
20587 declaration. */
20588 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20590 tree attributes = NULL_TREE;
20591 tree first_attribute;
20593 /* Peek at the next token. */
20594 token = cp_lexer_peek_token (parser->lexer);
20596 /* Check for a bitfield declaration. */
20597 if (token->type == CPP_COLON
20598 || (token->type == CPP_NAME
20599 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20600 == CPP_COLON))
20602 tree identifier;
20603 tree width;
20605 /* Get the name of the bitfield. Note that we cannot just
20606 check TOKEN here because it may have been invalidated by
20607 the call to cp_lexer_peek_nth_token above. */
20608 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20609 identifier = cp_parser_identifier (parser);
20610 else
20611 identifier = NULL_TREE;
20613 /* Consume the `:' token. */
20614 cp_lexer_consume_token (parser->lexer);
20615 /* Get the width of the bitfield. */
20616 width
20617 = cp_parser_constant_expression (parser,
20618 /*allow_non_constant=*/false,
20619 NULL);
20621 /* Look for attributes that apply to the bitfield. */
20622 attributes = cp_parser_attributes_opt (parser);
20623 /* Remember which attributes are prefix attributes and
20624 which are not. */
20625 first_attribute = attributes;
20626 /* Combine the attributes. */
20627 attributes = chainon (prefix_attributes, attributes);
20629 /* Create the bitfield declaration. */
20630 decl = grokbitfield (identifier
20631 ? make_id_declarator (NULL_TREE,
20632 identifier,
20633 sfk_none)
20634 : NULL,
20635 &decl_specifiers,
20636 width,
20637 attributes);
20639 else
20641 cp_declarator *declarator;
20642 tree initializer;
20643 tree asm_specification;
20644 int ctor_dtor_or_conv_p;
20646 /* Parse the declarator. */
20647 declarator
20648 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20649 &ctor_dtor_or_conv_p,
20650 /*parenthesized_p=*/NULL,
20651 /*member_p=*/true,
20652 friend_p);
20654 /* If something went wrong parsing the declarator, make sure
20655 that we at least consume some tokens. */
20656 if (declarator == cp_error_declarator)
20658 /* Skip to the end of the statement. */
20659 cp_parser_skip_to_end_of_statement (parser);
20660 /* If the next token is not a semicolon, that is
20661 probably because we just skipped over the body of
20662 a function. So, we consume a semicolon if
20663 present, but do not issue an error message if it
20664 is not present. */
20665 if (cp_lexer_next_token_is (parser->lexer,
20666 CPP_SEMICOLON))
20667 cp_lexer_consume_token (parser->lexer);
20668 goto out;
20671 if (declares_class_or_enum & 2)
20672 cp_parser_check_for_definition_in_return_type
20673 (declarator, decl_specifiers.type,
20674 decl_specifiers.locations[ds_type_spec]);
20676 /* Look for an asm-specification. */
20677 asm_specification = cp_parser_asm_specification_opt (parser);
20678 /* Look for attributes that apply to the declaration. */
20679 attributes = cp_parser_attributes_opt (parser);
20680 /* Remember which attributes are prefix attributes and
20681 which are not. */
20682 first_attribute = attributes;
20683 /* Combine the attributes. */
20684 attributes = chainon (prefix_attributes, attributes);
20686 /* If it's an `=', then we have a constant-initializer or a
20687 pure-specifier. It is not correct to parse the
20688 initializer before registering the member declaration
20689 since the member declaration should be in scope while
20690 its initializer is processed. However, the rest of the
20691 front end does not yet provide an interface that allows
20692 us to handle this correctly. */
20693 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20695 /* In [class.mem]:
20697 A pure-specifier shall be used only in the declaration of
20698 a virtual function.
20700 A member-declarator can contain a constant-initializer
20701 only if it declares a static member of integral or
20702 enumeration type.
20704 Therefore, if the DECLARATOR is for a function, we look
20705 for a pure-specifier; otherwise, we look for a
20706 constant-initializer. When we call `grokfield', it will
20707 perform more stringent semantics checks. */
20708 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20709 if (function_declarator_p (declarator)
20710 || (decl_specifiers.type
20711 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20712 && declarator->kind == cdk_id
20713 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20714 == FUNCTION_TYPE)))
20715 initializer = cp_parser_pure_specifier (parser);
20716 else if (decl_specifiers.storage_class != sc_static)
20717 initializer = cp_parser_save_nsdmi (parser);
20718 else if (cxx_dialect >= cxx11)
20720 bool nonconst;
20721 /* Don't require a constant rvalue in C++11, since we
20722 might want a reference constant. We'll enforce
20723 constancy later. */
20724 cp_lexer_consume_token (parser->lexer);
20725 /* Parse the initializer. */
20726 initializer = cp_parser_initializer_clause (parser,
20727 &nonconst);
20729 else
20730 /* Parse the initializer. */
20731 initializer = cp_parser_constant_initializer (parser);
20733 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20734 && !function_declarator_p (declarator))
20736 bool x;
20737 if (decl_specifiers.storage_class != sc_static)
20738 initializer = cp_parser_save_nsdmi (parser);
20739 else
20740 initializer = cp_parser_initializer (parser, &x, &x);
20742 /* Otherwise, there is no initializer. */
20743 else
20744 initializer = NULL_TREE;
20746 /* See if we are probably looking at a function
20747 definition. We are certainly not looking at a
20748 member-declarator. Calling `grokfield' has
20749 side-effects, so we must not do it unless we are sure
20750 that we are looking at a member-declarator. */
20751 if (cp_parser_token_starts_function_definition_p
20752 (cp_lexer_peek_token (parser->lexer)))
20754 /* The grammar does not allow a pure-specifier to be
20755 used when a member function is defined. (It is
20756 possible that this fact is an oversight in the
20757 standard, since a pure function may be defined
20758 outside of the class-specifier. */
20759 if (initializer && initializer_token_start)
20760 error_at (initializer_token_start->location,
20761 "pure-specifier on function-definition");
20762 decl = cp_parser_save_member_function_body (parser,
20763 &decl_specifiers,
20764 declarator,
20765 attributes);
20766 if (parser->fully_implicit_function_template_p)
20767 decl = finish_fully_implicit_template (parser, decl);
20768 /* If the member was not a friend, declare it here. */
20769 if (!friend_p)
20770 finish_member_declaration (decl);
20771 /* Peek at the next token. */
20772 token = cp_lexer_peek_token (parser->lexer);
20773 /* If the next token is a semicolon, consume it. */
20774 if (token->type == CPP_SEMICOLON)
20775 cp_lexer_consume_token (parser->lexer);
20776 goto out;
20778 else
20779 if (declarator->kind == cdk_function)
20780 declarator->id_loc = token->location;
20781 /* Create the declaration. */
20782 decl = grokfield (declarator, &decl_specifiers,
20783 initializer, /*init_const_expr_p=*/true,
20784 asm_specification, attributes);
20785 if (parser->fully_implicit_function_template_p)
20787 if (friend_p)
20788 finish_fully_implicit_template (parser, 0);
20789 else
20790 decl = finish_fully_implicit_template (parser, decl);
20794 cp_finalize_omp_declare_simd (parser, decl);
20796 /* Reset PREFIX_ATTRIBUTES. */
20797 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20798 attributes = TREE_CHAIN (attributes);
20799 if (attributes)
20800 TREE_CHAIN (attributes) = NULL_TREE;
20802 /* If there is any qualification still in effect, clear it
20803 now; we will be starting fresh with the next declarator. */
20804 parser->scope = NULL_TREE;
20805 parser->qualifying_scope = NULL_TREE;
20806 parser->object_scope = NULL_TREE;
20807 /* If it's a `,', then there are more declarators. */
20808 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20810 cp_lexer_consume_token (parser->lexer);
20811 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20813 cp_token *token = cp_lexer_previous_token (parser->lexer);
20814 error_at (token->location,
20815 "stray %<,%> at end of member declaration");
20818 /* If the next token isn't a `;', then we have a parse error. */
20819 else if (cp_lexer_next_token_is_not (parser->lexer,
20820 CPP_SEMICOLON))
20822 /* The next token might be a ways away from where the
20823 actual semicolon is missing. Find the previous token
20824 and use that for our error position. */
20825 cp_token *token = cp_lexer_previous_token (parser->lexer);
20826 error_at (token->location,
20827 "expected %<;%> at end of member declaration");
20829 /* Assume that the user meant to provide a semicolon. If
20830 we were to cp_parser_skip_to_end_of_statement, we might
20831 skip to a semicolon inside a member function definition
20832 and issue nonsensical error messages. */
20833 assume_semicolon = true;
20836 if (decl)
20838 /* Add DECL to the list of members. */
20839 if (!friend_p)
20840 finish_member_declaration (decl);
20842 if (TREE_CODE (decl) == FUNCTION_DECL)
20843 cp_parser_save_default_args (parser, decl);
20844 else if (TREE_CODE (decl) == FIELD_DECL
20845 && !DECL_C_BIT_FIELD (decl)
20846 && DECL_INITIAL (decl))
20847 /* Add DECL to the queue of NSDMI to be parsed later. */
20848 vec_safe_push (unparsed_nsdmis, decl);
20851 if (assume_semicolon)
20852 goto out;
20856 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20857 out:
20858 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20861 /* Parse a pure-specifier.
20863 pure-specifier:
20866 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20867 Otherwise, ERROR_MARK_NODE is returned. */
20869 static tree
20870 cp_parser_pure_specifier (cp_parser* parser)
20872 cp_token *token;
20874 /* Look for the `=' token. */
20875 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20876 return error_mark_node;
20877 /* Look for the `0' token. */
20878 token = cp_lexer_peek_token (parser->lexer);
20880 if (token->type == CPP_EOF
20881 || token->type == CPP_PRAGMA_EOL)
20882 return error_mark_node;
20884 cp_lexer_consume_token (parser->lexer);
20886 /* Accept = default or = delete in c++0x mode. */
20887 if (token->keyword == RID_DEFAULT
20888 || token->keyword == RID_DELETE)
20890 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20891 return token->u.value;
20894 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20895 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20897 cp_parser_error (parser,
20898 "invalid pure specifier (only %<= 0%> is allowed)");
20899 cp_parser_skip_to_end_of_statement (parser);
20900 return error_mark_node;
20902 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20904 error_at (token->location, "templates may not be %<virtual%>");
20905 return error_mark_node;
20908 return integer_zero_node;
20911 /* Parse a constant-initializer.
20913 constant-initializer:
20914 = constant-expression
20916 Returns a representation of the constant-expression. */
20918 static tree
20919 cp_parser_constant_initializer (cp_parser* parser)
20921 /* Look for the `=' token. */
20922 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20923 return error_mark_node;
20925 /* It is invalid to write:
20927 struct S { static const int i = { 7 }; };
20930 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20932 cp_parser_error (parser,
20933 "a brace-enclosed initializer is not allowed here");
20934 /* Consume the opening brace. */
20935 cp_lexer_consume_token (parser->lexer);
20936 /* Skip the initializer. */
20937 cp_parser_skip_to_closing_brace (parser);
20938 /* Look for the trailing `}'. */
20939 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20941 return error_mark_node;
20944 return cp_parser_constant_expression (parser,
20945 /*allow_non_constant=*/false,
20946 NULL);
20949 /* Derived classes [gram.class.derived] */
20951 /* Parse a base-clause.
20953 base-clause:
20954 : base-specifier-list
20956 base-specifier-list:
20957 base-specifier ... [opt]
20958 base-specifier-list , base-specifier ... [opt]
20960 Returns a TREE_LIST representing the base-classes, in the order in
20961 which they were declared. The representation of each node is as
20962 described by cp_parser_base_specifier.
20964 In the case that no bases are specified, this function will return
20965 NULL_TREE, not ERROR_MARK_NODE. */
20967 static tree
20968 cp_parser_base_clause (cp_parser* parser)
20970 tree bases = NULL_TREE;
20972 /* Look for the `:' that begins the list. */
20973 cp_parser_require (parser, CPP_COLON, RT_COLON);
20975 /* Scan the base-specifier-list. */
20976 while (true)
20978 cp_token *token;
20979 tree base;
20980 bool pack_expansion_p = false;
20982 /* Look for the base-specifier. */
20983 base = cp_parser_base_specifier (parser);
20984 /* Look for the (optional) ellipsis. */
20985 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20987 /* Consume the `...'. */
20988 cp_lexer_consume_token (parser->lexer);
20990 pack_expansion_p = true;
20993 /* Add BASE to the front of the list. */
20994 if (base && base != error_mark_node)
20996 if (pack_expansion_p)
20997 /* Make this a pack expansion type. */
20998 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21000 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21002 TREE_CHAIN (base) = bases;
21003 bases = base;
21006 /* Peek at the next token. */
21007 token = cp_lexer_peek_token (parser->lexer);
21008 /* If it's not a comma, then the list is complete. */
21009 if (token->type != CPP_COMMA)
21010 break;
21011 /* Consume the `,'. */
21012 cp_lexer_consume_token (parser->lexer);
21015 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21016 base class had a qualified name. However, the next name that
21017 appears is certainly not qualified. */
21018 parser->scope = NULL_TREE;
21019 parser->qualifying_scope = NULL_TREE;
21020 parser->object_scope = NULL_TREE;
21022 return nreverse (bases);
21025 /* Parse a base-specifier.
21027 base-specifier:
21028 :: [opt] nested-name-specifier [opt] class-name
21029 virtual access-specifier [opt] :: [opt] nested-name-specifier
21030 [opt] class-name
21031 access-specifier virtual [opt] :: [opt] nested-name-specifier
21032 [opt] class-name
21034 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21035 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21036 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21037 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21039 static tree
21040 cp_parser_base_specifier (cp_parser* parser)
21042 cp_token *token;
21043 bool done = false;
21044 bool virtual_p = false;
21045 bool duplicate_virtual_error_issued_p = false;
21046 bool duplicate_access_error_issued_p = false;
21047 bool class_scope_p, template_p;
21048 tree access = access_default_node;
21049 tree type;
21051 /* Process the optional `virtual' and `access-specifier'. */
21052 while (!done)
21054 /* Peek at the next token. */
21055 token = cp_lexer_peek_token (parser->lexer);
21056 /* Process `virtual'. */
21057 switch (token->keyword)
21059 case RID_VIRTUAL:
21060 /* If `virtual' appears more than once, issue an error. */
21061 if (virtual_p && !duplicate_virtual_error_issued_p)
21063 cp_parser_error (parser,
21064 "%<virtual%> specified more than once in base-specified");
21065 duplicate_virtual_error_issued_p = true;
21068 virtual_p = true;
21070 /* Consume the `virtual' token. */
21071 cp_lexer_consume_token (parser->lexer);
21073 break;
21075 case RID_PUBLIC:
21076 case RID_PROTECTED:
21077 case RID_PRIVATE:
21078 /* If more than one access specifier appears, issue an
21079 error. */
21080 if (access != access_default_node
21081 && !duplicate_access_error_issued_p)
21083 cp_parser_error (parser,
21084 "more than one access specifier in base-specified");
21085 duplicate_access_error_issued_p = true;
21088 access = ridpointers[(int) token->keyword];
21090 /* Consume the access-specifier. */
21091 cp_lexer_consume_token (parser->lexer);
21093 break;
21095 default:
21096 done = true;
21097 break;
21100 /* It is not uncommon to see programs mechanically, erroneously, use
21101 the 'typename' keyword to denote (dependent) qualified types
21102 as base classes. */
21103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21105 token = cp_lexer_peek_token (parser->lexer);
21106 if (!processing_template_decl)
21107 error_at (token->location,
21108 "keyword %<typename%> not allowed outside of templates");
21109 else
21110 error_at (token->location,
21111 "keyword %<typename%> not allowed in this context "
21112 "(the base class is implicitly a type)");
21113 cp_lexer_consume_token (parser->lexer);
21116 /* Look for the optional `::' operator. */
21117 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21118 /* Look for the nested-name-specifier. The simplest way to
21119 implement:
21121 [temp.res]
21123 The keyword `typename' is not permitted in a base-specifier or
21124 mem-initializer; in these contexts a qualified name that
21125 depends on a template-parameter is implicitly assumed to be a
21126 type name.
21128 is to pretend that we have seen the `typename' keyword at this
21129 point. */
21130 cp_parser_nested_name_specifier_opt (parser,
21131 /*typename_keyword_p=*/true,
21132 /*check_dependency_p=*/true,
21133 typename_type,
21134 /*is_declaration=*/true);
21135 /* If the base class is given by a qualified name, assume that names
21136 we see are type names or templates, as appropriate. */
21137 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21138 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21140 if (!parser->scope
21141 && cp_lexer_next_token_is_decltype (parser->lexer))
21142 /* DR 950 allows decltype as a base-specifier. */
21143 type = cp_parser_decltype (parser);
21144 else
21146 /* Otherwise, look for the class-name. */
21147 type = cp_parser_class_name (parser,
21148 class_scope_p,
21149 template_p,
21150 typename_type,
21151 /*check_dependency_p=*/true,
21152 /*class_head_p=*/false,
21153 /*is_declaration=*/true);
21154 type = TREE_TYPE (type);
21157 if (type == error_mark_node)
21158 return error_mark_node;
21160 return finish_base_specifier (type, access, virtual_p);
21163 /* Exception handling [gram.exception] */
21165 /* Parse an (optional) noexcept-specification.
21167 noexcept-specification:
21168 noexcept ( constant-expression ) [opt]
21170 If no noexcept-specification is present, returns NULL_TREE.
21171 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21172 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21173 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21174 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21175 in which case a boolean condition is returned instead. */
21177 static tree
21178 cp_parser_noexcept_specification_opt (cp_parser* parser,
21179 bool require_constexpr,
21180 bool* consumed_expr,
21181 bool return_cond)
21183 cp_token *token;
21184 const char *saved_message;
21186 /* Peek at the next token. */
21187 token = cp_lexer_peek_token (parser->lexer);
21189 /* Is it a noexcept-specification? */
21190 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21192 tree expr;
21193 cp_lexer_consume_token (parser->lexer);
21195 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21197 cp_lexer_consume_token (parser->lexer);
21199 if (require_constexpr)
21201 /* Types may not be defined in an exception-specification. */
21202 saved_message = parser->type_definition_forbidden_message;
21203 parser->type_definition_forbidden_message
21204 = G_("types may not be defined in an exception-specification");
21206 expr = cp_parser_constant_expression (parser, false, NULL);
21208 /* Restore the saved message. */
21209 parser->type_definition_forbidden_message = saved_message;
21211 else
21213 expr = cp_parser_expression (parser);
21214 *consumed_expr = true;
21217 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21219 else
21221 expr = boolean_true_node;
21222 if (!require_constexpr)
21223 *consumed_expr = false;
21226 /* We cannot build a noexcept-spec right away because this will check
21227 that expr is a constexpr. */
21228 if (!return_cond)
21229 return build_noexcept_spec (expr, tf_warning_or_error);
21230 else
21231 return expr;
21233 else
21234 return NULL_TREE;
21237 /* Parse an (optional) exception-specification.
21239 exception-specification:
21240 throw ( type-id-list [opt] )
21242 Returns a TREE_LIST representing the exception-specification. The
21243 TREE_VALUE of each node is a type. */
21245 static tree
21246 cp_parser_exception_specification_opt (cp_parser* parser)
21248 cp_token *token;
21249 tree type_id_list;
21250 const char *saved_message;
21252 /* Peek at the next token. */
21253 token = cp_lexer_peek_token (parser->lexer);
21255 /* Is it a noexcept-specification? */
21256 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21257 false);
21258 if (type_id_list != NULL_TREE)
21259 return type_id_list;
21261 /* If it's not `throw', then there's no exception-specification. */
21262 if (!cp_parser_is_keyword (token, RID_THROW))
21263 return NULL_TREE;
21265 #if 0
21266 /* Enable this once a lot of code has transitioned to noexcept? */
21267 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21268 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21269 "deprecated in C++0x; use %<noexcept%> instead");
21270 #endif
21272 /* Consume the `throw'. */
21273 cp_lexer_consume_token (parser->lexer);
21275 /* Look for the `('. */
21276 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21278 /* Peek at the next token. */
21279 token = cp_lexer_peek_token (parser->lexer);
21280 /* If it's not a `)', then there is a type-id-list. */
21281 if (token->type != CPP_CLOSE_PAREN)
21283 /* Types may not be defined in an exception-specification. */
21284 saved_message = parser->type_definition_forbidden_message;
21285 parser->type_definition_forbidden_message
21286 = G_("types may not be defined in an exception-specification");
21287 /* Parse the type-id-list. */
21288 type_id_list = cp_parser_type_id_list (parser);
21289 /* Restore the saved message. */
21290 parser->type_definition_forbidden_message = saved_message;
21292 else
21293 type_id_list = empty_except_spec;
21295 /* Look for the `)'. */
21296 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21298 return type_id_list;
21301 /* Parse an (optional) type-id-list.
21303 type-id-list:
21304 type-id ... [opt]
21305 type-id-list , type-id ... [opt]
21307 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21308 in the order that the types were presented. */
21310 static tree
21311 cp_parser_type_id_list (cp_parser* parser)
21313 tree types = NULL_TREE;
21315 while (true)
21317 cp_token *token;
21318 tree type;
21320 /* Get the next type-id. */
21321 type = cp_parser_type_id (parser);
21322 /* Parse the optional ellipsis. */
21323 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21325 /* Consume the `...'. */
21326 cp_lexer_consume_token (parser->lexer);
21328 /* Turn the type into a pack expansion expression. */
21329 type = make_pack_expansion (type);
21331 /* Add it to the list. */
21332 types = add_exception_specifier (types, type, /*complain=*/1);
21333 /* Peek at the next token. */
21334 token = cp_lexer_peek_token (parser->lexer);
21335 /* If it is not a `,', we are done. */
21336 if (token->type != CPP_COMMA)
21337 break;
21338 /* Consume the `,'. */
21339 cp_lexer_consume_token (parser->lexer);
21342 return nreverse (types);
21345 /* Parse a try-block.
21347 try-block:
21348 try compound-statement handler-seq */
21350 static tree
21351 cp_parser_try_block (cp_parser* parser)
21353 tree try_block;
21355 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21356 try_block = begin_try_block ();
21357 cp_parser_compound_statement (parser, NULL, true, false);
21358 finish_try_block (try_block);
21359 cp_parser_handler_seq (parser);
21360 finish_handler_sequence (try_block);
21362 return try_block;
21365 /* Parse a function-try-block.
21367 function-try-block:
21368 try ctor-initializer [opt] function-body handler-seq */
21370 static bool
21371 cp_parser_function_try_block (cp_parser* parser)
21373 tree compound_stmt;
21374 tree try_block;
21375 bool ctor_initializer_p;
21377 /* Look for the `try' keyword. */
21378 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21379 return false;
21380 /* Let the rest of the front end know where we are. */
21381 try_block = begin_function_try_block (&compound_stmt);
21382 /* Parse the function-body. */
21383 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21384 (parser, /*in_function_try_block=*/true);
21385 /* We're done with the `try' part. */
21386 finish_function_try_block (try_block);
21387 /* Parse the handlers. */
21388 cp_parser_handler_seq (parser);
21389 /* We're done with the handlers. */
21390 finish_function_handler_sequence (try_block, compound_stmt);
21392 return ctor_initializer_p;
21395 /* Parse a handler-seq.
21397 handler-seq:
21398 handler handler-seq [opt] */
21400 static void
21401 cp_parser_handler_seq (cp_parser* parser)
21403 while (true)
21405 cp_token *token;
21407 /* Parse the handler. */
21408 cp_parser_handler (parser);
21409 /* Peek at the next token. */
21410 token = cp_lexer_peek_token (parser->lexer);
21411 /* If it's not `catch' then there are no more handlers. */
21412 if (!cp_parser_is_keyword (token, RID_CATCH))
21413 break;
21417 /* Parse a handler.
21419 handler:
21420 catch ( exception-declaration ) compound-statement */
21422 static void
21423 cp_parser_handler (cp_parser* parser)
21425 tree handler;
21426 tree declaration;
21428 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21429 handler = begin_handler ();
21430 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21431 declaration = cp_parser_exception_declaration (parser);
21432 finish_handler_parms (declaration, handler);
21433 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21434 cp_parser_compound_statement (parser, NULL, false, false);
21435 finish_handler (handler);
21438 /* Parse an exception-declaration.
21440 exception-declaration:
21441 type-specifier-seq declarator
21442 type-specifier-seq abstract-declarator
21443 type-specifier-seq
21446 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21447 ellipsis variant is used. */
21449 static tree
21450 cp_parser_exception_declaration (cp_parser* parser)
21452 cp_decl_specifier_seq type_specifiers;
21453 cp_declarator *declarator;
21454 const char *saved_message;
21456 /* If it's an ellipsis, it's easy to handle. */
21457 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21459 /* Consume the `...' token. */
21460 cp_lexer_consume_token (parser->lexer);
21461 return NULL_TREE;
21464 /* Types may not be defined in exception-declarations. */
21465 saved_message = parser->type_definition_forbidden_message;
21466 parser->type_definition_forbidden_message
21467 = G_("types may not be defined in exception-declarations");
21469 /* Parse the type-specifier-seq. */
21470 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21471 /*is_trailing_return=*/false,
21472 &type_specifiers);
21473 /* If it's a `)', then there is no declarator. */
21474 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21475 declarator = NULL;
21476 else
21477 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21478 /*ctor_dtor_or_conv_p=*/NULL,
21479 /*parenthesized_p=*/NULL,
21480 /*member_p=*/false,
21481 /*friend_p=*/false);
21483 /* Restore the saved message. */
21484 parser->type_definition_forbidden_message = saved_message;
21486 if (!type_specifiers.any_specifiers_p)
21487 return error_mark_node;
21489 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21492 /* Parse a throw-expression.
21494 throw-expression:
21495 throw assignment-expression [opt]
21497 Returns a THROW_EXPR representing the throw-expression. */
21499 static tree
21500 cp_parser_throw_expression (cp_parser* parser)
21502 tree expression;
21503 cp_token* token;
21505 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21506 token = cp_lexer_peek_token (parser->lexer);
21507 /* Figure out whether or not there is an assignment-expression
21508 following the "throw" keyword. */
21509 if (token->type == CPP_COMMA
21510 || token->type == CPP_SEMICOLON
21511 || token->type == CPP_CLOSE_PAREN
21512 || token->type == CPP_CLOSE_SQUARE
21513 || token->type == CPP_CLOSE_BRACE
21514 || token->type == CPP_COLON)
21515 expression = NULL_TREE;
21516 else
21517 expression = cp_parser_assignment_expression (parser,
21518 /*cast_p=*/false, NULL);
21520 return build_throw (expression);
21523 /* GNU Extensions */
21525 /* Parse an (optional) asm-specification.
21527 asm-specification:
21528 asm ( string-literal )
21530 If the asm-specification is present, returns a STRING_CST
21531 corresponding to the string-literal. Otherwise, returns
21532 NULL_TREE. */
21534 static tree
21535 cp_parser_asm_specification_opt (cp_parser* parser)
21537 cp_token *token;
21538 tree asm_specification;
21540 /* Peek at the next token. */
21541 token = cp_lexer_peek_token (parser->lexer);
21542 /* If the next token isn't the `asm' keyword, then there's no
21543 asm-specification. */
21544 if (!cp_parser_is_keyword (token, RID_ASM))
21545 return NULL_TREE;
21547 /* Consume the `asm' token. */
21548 cp_lexer_consume_token (parser->lexer);
21549 /* Look for the `('. */
21550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21552 /* Look for the string-literal. */
21553 asm_specification = cp_parser_string_literal (parser, false, false);
21555 /* Look for the `)'. */
21556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21558 return asm_specification;
21561 /* Parse an asm-operand-list.
21563 asm-operand-list:
21564 asm-operand
21565 asm-operand-list , asm-operand
21567 asm-operand:
21568 string-literal ( expression )
21569 [ string-literal ] string-literal ( expression )
21571 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21572 each node is the expression. The TREE_PURPOSE is itself a
21573 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21574 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21575 is a STRING_CST for the string literal before the parenthesis. Returns
21576 ERROR_MARK_NODE if any of the operands are invalid. */
21578 static tree
21579 cp_parser_asm_operand_list (cp_parser* parser)
21581 tree asm_operands = NULL_TREE;
21582 bool invalid_operands = false;
21584 while (true)
21586 tree string_literal;
21587 tree expression;
21588 tree name;
21590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21592 /* Consume the `[' token. */
21593 cp_lexer_consume_token (parser->lexer);
21594 /* Read the operand name. */
21595 name = cp_parser_identifier (parser);
21596 if (name != error_mark_node)
21597 name = build_string (IDENTIFIER_LENGTH (name),
21598 IDENTIFIER_POINTER (name));
21599 /* Look for the closing `]'. */
21600 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21602 else
21603 name = NULL_TREE;
21604 /* Look for the string-literal. */
21605 string_literal = cp_parser_string_literal (parser, false, false);
21607 /* Look for the `('. */
21608 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21609 /* Parse the expression. */
21610 expression = cp_parser_expression (parser);
21611 /* Look for the `)'. */
21612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21614 if (name == error_mark_node
21615 || string_literal == error_mark_node
21616 || expression == error_mark_node)
21617 invalid_operands = true;
21619 /* Add this operand to the list. */
21620 asm_operands = tree_cons (build_tree_list (name, string_literal),
21621 expression,
21622 asm_operands);
21623 /* If the next token is not a `,', there are no more
21624 operands. */
21625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21626 break;
21627 /* Consume the `,'. */
21628 cp_lexer_consume_token (parser->lexer);
21631 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21634 /* Parse an asm-clobber-list.
21636 asm-clobber-list:
21637 string-literal
21638 asm-clobber-list , string-literal
21640 Returns a TREE_LIST, indicating the clobbers in the order that they
21641 appeared. The TREE_VALUE of each node is a STRING_CST. */
21643 static tree
21644 cp_parser_asm_clobber_list (cp_parser* parser)
21646 tree clobbers = NULL_TREE;
21648 while (true)
21650 tree string_literal;
21652 /* Look for the string literal. */
21653 string_literal = cp_parser_string_literal (parser, false, false);
21654 /* Add it to the list. */
21655 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21656 /* If the next token is not a `,', then the list is
21657 complete. */
21658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21659 break;
21660 /* Consume the `,' token. */
21661 cp_lexer_consume_token (parser->lexer);
21664 return clobbers;
21667 /* Parse an asm-label-list.
21669 asm-label-list:
21670 identifier
21671 asm-label-list , identifier
21673 Returns a TREE_LIST, indicating the labels in the order that they
21674 appeared. The TREE_VALUE of each node is a label. */
21676 static tree
21677 cp_parser_asm_label_list (cp_parser* parser)
21679 tree labels = NULL_TREE;
21681 while (true)
21683 tree identifier, label, name;
21685 /* Look for the identifier. */
21686 identifier = cp_parser_identifier (parser);
21687 if (!error_operand_p (identifier))
21689 label = lookup_label (identifier);
21690 if (TREE_CODE (label) == LABEL_DECL)
21692 TREE_USED (label) = 1;
21693 check_goto (label);
21694 name = build_string (IDENTIFIER_LENGTH (identifier),
21695 IDENTIFIER_POINTER (identifier));
21696 labels = tree_cons (name, label, labels);
21699 /* If the next token is not a `,', then the list is
21700 complete. */
21701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21702 break;
21703 /* Consume the `,' token. */
21704 cp_lexer_consume_token (parser->lexer);
21707 return nreverse (labels);
21710 /* Return TRUE iff the next tokens in the stream are possibly the
21711 beginning of a GNU extension attribute. */
21713 static bool
21714 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21716 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21719 /* Return TRUE iff the next tokens in the stream are possibly the
21720 beginning of a standard C++-11 attribute specifier. */
21722 static bool
21723 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21725 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21728 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21729 beginning of a standard C++-11 attribute specifier. */
21731 static bool
21732 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21734 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21736 return (cxx_dialect >= cxx11
21737 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21738 || (token->type == CPP_OPEN_SQUARE
21739 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21740 && token->type == CPP_OPEN_SQUARE)));
21743 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21744 beginning of a GNU extension attribute. */
21746 static bool
21747 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21749 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21751 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21754 /* Return true iff the next tokens can be the beginning of either a
21755 GNU attribute list, or a standard C++11 attribute sequence. */
21757 static bool
21758 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21760 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21761 || cp_next_tokens_can_be_std_attribute_p (parser));
21764 /* Return true iff the next Nth tokens can be the beginning of either
21765 a GNU attribute list, or a standard C++11 attribute sequence. */
21767 static bool
21768 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21770 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21771 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21774 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21775 of GNU attributes, or return NULL. */
21777 static tree
21778 cp_parser_attributes_opt (cp_parser *parser)
21780 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21781 return cp_parser_gnu_attributes_opt (parser);
21782 return cp_parser_std_attribute_spec_seq (parser);
21785 #define CILK_SIMD_FN_CLAUSE_MASK \
21786 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21787 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21788 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21789 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21790 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21792 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21793 vector [(<clauses>)] */
21795 static void
21796 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21798 bool first_p = parser->cilk_simd_fn_info == NULL;
21799 cp_token *token = v_token;
21800 if (first_p)
21802 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21803 parser->cilk_simd_fn_info->error_seen = false;
21804 parser->cilk_simd_fn_info->fndecl_seen = false;
21805 parser->cilk_simd_fn_info->tokens = vNULL;
21807 int paren_scope = 0;
21808 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21810 cp_lexer_consume_token (parser->lexer);
21811 v_token = cp_lexer_peek_token (parser->lexer);
21812 paren_scope++;
21814 while (paren_scope > 0)
21816 token = cp_lexer_peek_token (parser->lexer);
21817 if (token->type == CPP_OPEN_PAREN)
21818 paren_scope++;
21819 else if (token->type == CPP_CLOSE_PAREN)
21820 paren_scope--;
21821 /* Do not push the last ')' */
21822 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21823 cp_lexer_consume_token (parser->lexer);
21826 token->type = CPP_PRAGMA_EOL;
21827 parser->lexer->next_token = token;
21828 cp_lexer_consume_token (parser->lexer);
21830 struct cp_token_cache *cp
21831 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21832 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21835 /* Parse an (optional) series of attributes.
21837 attributes:
21838 attributes attribute
21840 attribute:
21841 __attribute__ (( attribute-list [opt] ))
21843 The return value is as for cp_parser_gnu_attribute_list. */
21845 static tree
21846 cp_parser_gnu_attributes_opt (cp_parser* parser)
21848 tree attributes = NULL_TREE;
21850 while (true)
21852 cp_token *token;
21853 tree attribute_list;
21854 bool ok = true;
21856 /* Peek at the next token. */
21857 token = cp_lexer_peek_token (parser->lexer);
21858 /* If it's not `__attribute__', then we're done. */
21859 if (token->keyword != RID_ATTRIBUTE)
21860 break;
21862 /* Consume the `__attribute__' keyword. */
21863 cp_lexer_consume_token (parser->lexer);
21864 /* Look for the two `(' tokens. */
21865 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21868 /* Peek at the next token. */
21869 token = cp_lexer_peek_token (parser->lexer);
21870 if (token->type != CPP_CLOSE_PAREN)
21871 /* Parse the attribute-list. */
21872 attribute_list = cp_parser_gnu_attribute_list (parser);
21873 else
21874 /* If the next token is a `)', then there is no attribute
21875 list. */
21876 attribute_list = NULL;
21878 /* Look for the two `)' tokens. */
21879 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21880 ok = false;
21881 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21882 ok = false;
21883 if (!ok)
21884 cp_parser_skip_to_end_of_statement (parser);
21886 /* Add these new attributes to the list. */
21887 attributes = chainon (attributes, attribute_list);
21890 return attributes;
21893 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21894 "__vector" or "__vector__." */
21896 static inline bool
21897 is_cilkplus_vector_p (tree name)
21899 if (flag_cilkplus && is_attribute_p ("vector", name))
21900 return true;
21901 return false;
21904 /* Parse a GNU attribute-list.
21906 attribute-list:
21907 attribute
21908 attribute-list , attribute
21910 attribute:
21911 identifier
21912 identifier ( identifier )
21913 identifier ( identifier , expression-list )
21914 identifier ( expression-list )
21916 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21917 to an attribute. The TREE_PURPOSE of each node is the identifier
21918 indicating which attribute is in use. The TREE_VALUE represents
21919 the arguments, if any. */
21921 static tree
21922 cp_parser_gnu_attribute_list (cp_parser* parser)
21924 tree attribute_list = NULL_TREE;
21925 bool save_translate_strings_p = parser->translate_strings_p;
21927 parser->translate_strings_p = false;
21928 while (true)
21930 cp_token *token;
21931 tree identifier;
21932 tree attribute;
21934 /* Look for the identifier. We also allow keywords here; for
21935 example `__attribute__ ((const))' is legal. */
21936 token = cp_lexer_peek_token (parser->lexer);
21937 if (token->type == CPP_NAME
21938 || token->type == CPP_KEYWORD)
21940 tree arguments = NULL_TREE;
21942 /* Consume the token, but save it since we need it for the
21943 SIMD enabled function parsing. */
21944 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21946 /* Save away the identifier that indicates which attribute
21947 this is. */
21948 identifier = (token->type == CPP_KEYWORD)
21949 /* For keywords, use the canonical spelling, not the
21950 parsed identifier. */
21951 ? ridpointers[(int) token->keyword]
21952 : id_token->u.value;
21954 attribute = build_tree_list (identifier, NULL_TREE);
21956 /* Peek at the next token. */
21957 token = cp_lexer_peek_token (parser->lexer);
21958 /* If it's an `(', then parse the attribute arguments. */
21959 if (token->type == CPP_OPEN_PAREN)
21961 vec<tree, va_gc> *vec;
21962 int attr_flag = (attribute_takes_identifier_p (identifier)
21963 ? id_attr : normal_attr);
21964 if (is_cilkplus_vector_p (identifier))
21966 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21967 continue;
21969 else
21970 vec = cp_parser_parenthesized_expression_list
21971 (parser, attr_flag, /*cast_p=*/false,
21972 /*allow_expansion_p=*/false,
21973 /*non_constant_p=*/NULL);
21974 if (vec == NULL)
21975 arguments = error_mark_node;
21976 else
21978 arguments = build_tree_list_vec (vec);
21979 release_tree_vector (vec);
21981 /* Save the arguments away. */
21982 TREE_VALUE (attribute) = arguments;
21984 else if (is_cilkplus_vector_p (identifier))
21986 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21987 continue;
21990 if (arguments != error_mark_node)
21992 /* Add this attribute to the list. */
21993 TREE_CHAIN (attribute) = attribute_list;
21994 attribute_list = attribute;
21997 token = cp_lexer_peek_token (parser->lexer);
21999 /* Now, look for more attributes. If the next token isn't a
22000 `,', we're done. */
22001 if (token->type != CPP_COMMA)
22002 break;
22004 /* Consume the comma and keep going. */
22005 cp_lexer_consume_token (parser->lexer);
22007 parser->translate_strings_p = save_translate_strings_p;
22009 /* We built up the list in reverse order. */
22010 return nreverse (attribute_list);
22013 /* Parse a standard C++11 attribute.
22015 The returned representation is a TREE_LIST which TREE_PURPOSE is
22016 the scoped name of the attribute, and the TREE_VALUE is its
22017 arguments list.
22019 Note that the scoped name of the attribute is itself a TREE_LIST
22020 which TREE_PURPOSE is the namespace of the attribute, and
22021 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22022 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22023 and which TREE_PURPOSE is directly the attribute name.
22025 Clients of the attribute code should use get_attribute_namespace
22026 and get_attribute_name to get the actual namespace and name of
22027 attributes, regardless of their being GNU or C++11 attributes.
22029 attribute:
22030 attribute-token attribute-argument-clause [opt]
22032 attribute-token:
22033 identifier
22034 attribute-scoped-token
22036 attribute-scoped-token:
22037 attribute-namespace :: identifier
22039 attribute-namespace:
22040 identifier
22042 attribute-argument-clause:
22043 ( balanced-token-seq )
22045 balanced-token-seq:
22046 balanced-token [opt]
22047 balanced-token-seq balanced-token
22049 balanced-token:
22050 ( balanced-token-seq )
22051 [ balanced-token-seq ]
22052 { balanced-token-seq }. */
22054 static tree
22055 cp_parser_std_attribute (cp_parser *parser)
22057 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22058 cp_token *token;
22060 /* First, parse name of the the attribute, a.k.a
22061 attribute-token. */
22063 token = cp_lexer_peek_token (parser->lexer);
22064 if (token->type == CPP_NAME)
22065 attr_id = token->u.value;
22066 else if (token->type == CPP_KEYWORD)
22067 attr_id = ridpointers[(int) token->keyword];
22068 else if (token->flags & NAMED_OP)
22069 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22071 if (attr_id == NULL_TREE)
22072 return NULL_TREE;
22074 cp_lexer_consume_token (parser->lexer);
22076 token = cp_lexer_peek_token (parser->lexer);
22077 if (token->type == CPP_SCOPE)
22079 /* We are seeing a scoped attribute token. */
22081 cp_lexer_consume_token (parser->lexer);
22082 attr_ns = attr_id;
22084 token = cp_lexer_consume_token (parser->lexer);
22085 if (token->type == CPP_NAME)
22086 attr_id = token->u.value;
22087 else if (token->type == CPP_KEYWORD)
22088 attr_id = ridpointers[(int) token->keyword];
22089 else
22091 error_at (token->location,
22092 "expected an identifier for the attribute name");
22093 return error_mark_node;
22095 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22096 NULL_TREE);
22097 token = cp_lexer_peek_token (parser->lexer);
22099 else
22101 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22102 NULL_TREE);
22103 /* C++11 noreturn attribute is equivalent to GNU's. */
22104 if (is_attribute_p ("noreturn", attr_id))
22105 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22106 /* C++14 deprecated attribute is equivalent to GNU's. */
22107 else if (cxx_dialect >= cxx14 && is_attribute_p ("deprecated", attr_id))
22108 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22111 /* Now parse the optional argument clause of the attribute. */
22113 if (token->type != CPP_OPEN_PAREN)
22114 return attribute;
22117 vec<tree, va_gc> *vec;
22118 int attr_flag = normal_attr;
22120 if (attr_ns == get_identifier ("gnu")
22121 && attribute_takes_identifier_p (attr_id))
22122 /* A GNU attribute that takes an identifier in parameter. */
22123 attr_flag = id_attr;
22125 vec = cp_parser_parenthesized_expression_list
22126 (parser, attr_flag, /*cast_p=*/false,
22127 /*allow_expansion_p=*/true,
22128 /*non_constant_p=*/NULL);
22129 if (vec == NULL)
22130 arguments = error_mark_node;
22131 else
22133 arguments = build_tree_list_vec (vec);
22134 release_tree_vector (vec);
22137 if (arguments == error_mark_node)
22138 attribute = error_mark_node;
22139 else
22140 TREE_VALUE (attribute) = arguments;
22143 return attribute;
22146 /* Parse a list of standard C++-11 attributes.
22148 attribute-list:
22149 attribute [opt]
22150 attribute-list , attribute[opt]
22151 attribute ...
22152 attribute-list , attribute ...
22155 static tree
22156 cp_parser_std_attribute_list (cp_parser *parser)
22158 tree attributes = NULL_TREE, attribute = NULL_TREE;
22159 cp_token *token = NULL;
22161 while (true)
22163 attribute = cp_parser_std_attribute (parser);
22164 if (attribute == error_mark_node)
22165 break;
22166 if (attribute != NULL_TREE)
22168 TREE_CHAIN (attribute) = attributes;
22169 attributes = attribute;
22171 token = cp_lexer_peek_token (parser->lexer);
22172 if (token->type != CPP_COMMA)
22173 break;
22174 cp_lexer_consume_token (parser->lexer);
22176 attributes = nreverse (attributes);
22177 return attributes;
22180 /* Parse a standard C++-11 attribute specifier.
22182 attribute-specifier:
22183 [ [ attribute-list ] ]
22184 alignment-specifier
22186 alignment-specifier:
22187 alignas ( type-id ... [opt] )
22188 alignas ( alignment-expression ... [opt] ). */
22190 static tree
22191 cp_parser_std_attribute_spec (cp_parser *parser)
22193 tree attributes = NULL_TREE;
22194 cp_token *token = cp_lexer_peek_token (parser->lexer);
22196 if (token->type == CPP_OPEN_SQUARE
22197 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22199 cp_lexer_consume_token (parser->lexer);
22200 cp_lexer_consume_token (parser->lexer);
22202 attributes = cp_parser_std_attribute_list (parser);
22204 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22205 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22206 cp_parser_skip_to_end_of_statement (parser);
22207 else
22208 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22209 when we are sure that we have actually parsed them. */
22210 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22212 else
22214 tree alignas_expr;
22216 /* Look for an alignment-specifier. */
22218 token = cp_lexer_peek_token (parser->lexer);
22220 if (token->type != CPP_KEYWORD
22221 || token->keyword != RID_ALIGNAS)
22222 return NULL_TREE;
22224 cp_lexer_consume_token (parser->lexer);
22225 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22227 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22229 cp_parser_error (parser, "expected %<(%>");
22230 return error_mark_node;
22233 cp_parser_parse_tentatively (parser);
22234 alignas_expr = cp_parser_type_id (parser);
22236 if (!cp_parser_parse_definitely (parser))
22238 gcc_assert (alignas_expr == error_mark_node
22239 || alignas_expr == NULL_TREE);
22241 alignas_expr =
22242 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22243 /**cp_id_kind=*/NULL);
22244 if (alignas_expr == error_mark_node)
22245 cp_parser_skip_to_end_of_statement (parser);
22246 if (alignas_expr == NULL_TREE
22247 || alignas_expr == error_mark_node)
22248 return alignas_expr;
22251 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22253 cp_parser_error (parser, "expected %<)%>");
22254 return error_mark_node;
22257 alignas_expr = cxx_alignas_expr (alignas_expr);
22259 /* Build the C++-11 representation of an 'aligned'
22260 attribute. */
22261 attributes =
22262 build_tree_list (build_tree_list (get_identifier ("gnu"),
22263 get_identifier ("aligned")),
22264 build_tree_list (NULL_TREE, alignas_expr));
22267 return attributes;
22270 /* Parse a standard C++-11 attribute-specifier-seq.
22272 attribute-specifier-seq:
22273 attribute-specifier-seq [opt] attribute-specifier
22276 static tree
22277 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22279 tree attr_specs = NULL;
22281 while (true)
22283 tree attr_spec = cp_parser_std_attribute_spec (parser);
22284 if (attr_spec == NULL_TREE)
22285 break;
22286 if (attr_spec == error_mark_node)
22287 return error_mark_node;
22289 TREE_CHAIN (attr_spec) = attr_specs;
22290 attr_specs = attr_spec;
22293 attr_specs = nreverse (attr_specs);
22294 return attr_specs;
22297 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22298 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22299 current value of the PEDANTIC flag, regardless of whether or not
22300 the `__extension__' keyword is present. The caller is responsible
22301 for restoring the value of the PEDANTIC flag. */
22303 static bool
22304 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22306 /* Save the old value of the PEDANTIC flag. */
22307 *saved_pedantic = pedantic;
22309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22311 /* Consume the `__extension__' token. */
22312 cp_lexer_consume_token (parser->lexer);
22313 /* We're not being pedantic while the `__extension__' keyword is
22314 in effect. */
22315 pedantic = 0;
22317 return true;
22320 return false;
22323 /* Parse a label declaration.
22325 label-declaration:
22326 __label__ label-declarator-seq ;
22328 label-declarator-seq:
22329 identifier , label-declarator-seq
22330 identifier */
22332 static void
22333 cp_parser_label_declaration (cp_parser* parser)
22335 /* Look for the `__label__' keyword. */
22336 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22338 while (true)
22340 tree identifier;
22342 /* Look for an identifier. */
22343 identifier = cp_parser_identifier (parser);
22344 /* If we failed, stop. */
22345 if (identifier == error_mark_node)
22346 break;
22347 /* Declare it as a label. */
22348 finish_label_decl (identifier);
22349 /* If the next token is a `;', stop. */
22350 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22351 break;
22352 /* Look for the `,' separating the label declarations. */
22353 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22356 /* Look for the final `;'. */
22357 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22360 /* Support Functions */
22362 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22363 NAME should have one of the representations used for an
22364 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22365 is returned. If PARSER->SCOPE is a dependent type, then a
22366 SCOPE_REF is returned.
22368 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22369 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22370 was formed. Abstractly, such entities should not be passed to this
22371 function, because they do not need to be looked up, but it is
22372 simpler to check for this special case here, rather than at the
22373 call-sites.
22375 In cases not explicitly covered above, this function returns a
22376 DECL, OVERLOAD, or baselink representing the result of the lookup.
22377 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22378 is returned.
22380 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22381 (e.g., "struct") that was used. In that case bindings that do not
22382 refer to types are ignored.
22384 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22385 ignored.
22387 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22388 are ignored.
22390 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22391 types.
22393 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22394 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22395 NULL_TREE otherwise. */
22397 static tree
22398 cp_parser_lookup_name (cp_parser *parser, tree name,
22399 enum tag_types tag_type,
22400 bool is_template,
22401 bool is_namespace,
22402 bool check_dependency,
22403 tree *ambiguous_decls,
22404 location_t name_location)
22406 tree decl;
22407 tree object_type = parser->context->object_type;
22409 /* Assume that the lookup will be unambiguous. */
22410 if (ambiguous_decls)
22411 *ambiguous_decls = NULL_TREE;
22413 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22414 no longer valid. Note that if we are parsing tentatively, and
22415 the parse fails, OBJECT_TYPE will be automatically restored. */
22416 parser->context->object_type = NULL_TREE;
22418 if (name == error_mark_node)
22419 return error_mark_node;
22421 /* A template-id has already been resolved; there is no lookup to
22422 do. */
22423 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22424 return name;
22425 if (BASELINK_P (name))
22427 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22428 == TEMPLATE_ID_EXPR);
22429 return name;
22432 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22433 it should already have been checked to make sure that the name
22434 used matches the type being destroyed. */
22435 if (TREE_CODE (name) == BIT_NOT_EXPR)
22437 tree type;
22439 /* Figure out to which type this destructor applies. */
22440 if (parser->scope)
22441 type = parser->scope;
22442 else if (object_type)
22443 type = object_type;
22444 else
22445 type = current_class_type;
22446 /* If that's not a class type, there is no destructor. */
22447 if (!type || !CLASS_TYPE_P (type))
22448 return error_mark_node;
22449 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22450 lazily_declare_fn (sfk_destructor, type);
22451 if (!CLASSTYPE_DESTRUCTORS (type))
22452 return error_mark_node;
22453 /* If it was a class type, return the destructor. */
22454 return CLASSTYPE_DESTRUCTORS (type);
22457 /* By this point, the NAME should be an ordinary identifier. If
22458 the id-expression was a qualified name, the qualifying scope is
22459 stored in PARSER->SCOPE at this point. */
22460 gcc_assert (identifier_p (name));
22462 /* Perform the lookup. */
22463 if (parser->scope)
22465 bool dependent_p;
22467 if (parser->scope == error_mark_node)
22468 return error_mark_node;
22470 /* If the SCOPE is dependent, the lookup must be deferred until
22471 the template is instantiated -- unless we are explicitly
22472 looking up names in uninstantiated templates. Even then, we
22473 cannot look up the name if the scope is not a class type; it
22474 might, for example, be a template type parameter. */
22475 dependent_p = (TYPE_P (parser->scope)
22476 && dependent_scope_p (parser->scope));
22477 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22478 && dependent_p)
22479 /* Defer lookup. */
22480 decl = error_mark_node;
22481 else
22483 tree pushed_scope = NULL_TREE;
22485 /* If PARSER->SCOPE is a dependent type, then it must be a
22486 class type, and we must not be checking dependencies;
22487 otherwise, we would have processed this lookup above. So
22488 that PARSER->SCOPE is not considered a dependent base by
22489 lookup_member, we must enter the scope here. */
22490 if (dependent_p)
22491 pushed_scope = push_scope (parser->scope);
22493 /* If the PARSER->SCOPE is a template specialization, it
22494 may be instantiated during name lookup. In that case,
22495 errors may be issued. Even if we rollback the current
22496 tentative parse, those errors are valid. */
22497 decl = lookup_qualified_name (parser->scope, name,
22498 tag_type != none_type,
22499 /*complain=*/true);
22501 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22502 lookup result and the nested-name-specifier nominates a class C:
22503 * if the name specified after the nested-name-specifier, when
22504 looked up in C, is the injected-class-name of C (Clause 9), or
22505 * if the name specified after the nested-name-specifier is the
22506 same as the identifier or the simple-template-id's template-
22507 name in the last component of the nested-name-specifier,
22508 the name is instead considered to name the constructor of
22509 class C. [ Note: for example, the constructor is not an
22510 acceptable lookup result in an elaborated-type-specifier so
22511 the constructor would not be used in place of the
22512 injected-class-name. --end note ] Such a constructor name
22513 shall be used only in the declarator-id of a declaration that
22514 names a constructor or in a using-declaration. */
22515 if (tag_type == none_type
22516 && DECL_SELF_REFERENCE_P (decl)
22517 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22518 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22519 tag_type != none_type,
22520 /*complain=*/true);
22522 /* If we have a single function from a using decl, pull it out. */
22523 if (TREE_CODE (decl) == OVERLOAD
22524 && !really_overloaded_fn (decl))
22525 decl = OVL_FUNCTION (decl);
22527 if (pushed_scope)
22528 pop_scope (pushed_scope);
22531 /* If the scope is a dependent type and either we deferred lookup or
22532 we did lookup but didn't find the name, rememeber the name. */
22533 if (decl == error_mark_node && TYPE_P (parser->scope)
22534 && dependent_type_p (parser->scope))
22536 if (tag_type)
22538 tree type;
22540 /* The resolution to Core Issue 180 says that `struct
22541 A::B' should be considered a type-name, even if `A'
22542 is dependent. */
22543 type = make_typename_type (parser->scope, name, tag_type,
22544 /*complain=*/tf_error);
22545 if (type != error_mark_node)
22546 decl = TYPE_NAME (type);
22548 else if (is_template
22549 && (cp_parser_next_token_ends_template_argument_p (parser)
22550 || cp_lexer_next_token_is (parser->lexer,
22551 CPP_CLOSE_PAREN)))
22552 decl = make_unbound_class_template (parser->scope,
22553 name, NULL_TREE,
22554 /*complain=*/tf_error);
22555 else
22556 decl = build_qualified_name (/*type=*/NULL_TREE,
22557 parser->scope, name,
22558 is_template);
22560 parser->qualifying_scope = parser->scope;
22561 parser->object_scope = NULL_TREE;
22563 else if (object_type)
22565 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22566 OBJECT_TYPE is not a class. */
22567 if (CLASS_TYPE_P (object_type))
22568 /* If the OBJECT_TYPE is a template specialization, it may
22569 be instantiated during name lookup. In that case, errors
22570 may be issued. Even if we rollback the current tentative
22571 parse, those errors are valid. */
22572 decl = lookup_member (object_type,
22573 name,
22574 /*protect=*/0,
22575 tag_type != none_type,
22576 tf_warning_or_error);
22577 else
22578 decl = NULL_TREE;
22580 if (!decl)
22581 /* Look it up in the enclosing context. */
22582 decl = lookup_name_real (name, tag_type != none_type,
22583 /*nonclass=*/0,
22584 /*block_p=*/true, is_namespace, 0);
22585 parser->object_scope = object_type;
22586 parser->qualifying_scope = NULL_TREE;
22588 else
22590 decl = lookup_name_real (name, tag_type != none_type,
22591 /*nonclass=*/0,
22592 /*block_p=*/true, is_namespace, 0);
22593 parser->qualifying_scope = NULL_TREE;
22594 parser->object_scope = NULL_TREE;
22597 /* If the lookup failed, let our caller know. */
22598 if (!decl || decl == error_mark_node)
22599 return error_mark_node;
22601 /* Pull out the template from an injected-class-name (or multiple). */
22602 if (is_template)
22603 decl = maybe_get_template_decl_from_type_decl (decl);
22605 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22606 if (TREE_CODE (decl) == TREE_LIST)
22608 if (ambiguous_decls)
22609 *ambiguous_decls = decl;
22610 /* The error message we have to print is too complicated for
22611 cp_parser_error, so we incorporate its actions directly. */
22612 if (!cp_parser_simulate_error (parser))
22614 error_at (name_location, "reference to %qD is ambiguous",
22615 name);
22616 print_candidates (decl);
22618 return error_mark_node;
22621 gcc_assert (DECL_P (decl)
22622 || TREE_CODE (decl) == OVERLOAD
22623 || TREE_CODE (decl) == SCOPE_REF
22624 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22625 || BASELINK_P (decl));
22627 /* If we have resolved the name of a member declaration, check to
22628 see if the declaration is accessible. When the name resolves to
22629 set of overloaded functions, accessibility is checked when
22630 overload resolution is done.
22632 During an explicit instantiation, access is not checked at all,
22633 as per [temp.explicit]. */
22634 if (DECL_P (decl))
22635 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22637 maybe_record_typedef_use (decl);
22639 return decl;
22642 /* Like cp_parser_lookup_name, but for use in the typical case where
22643 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22644 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22646 static tree
22647 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22649 return cp_parser_lookup_name (parser, name,
22650 none_type,
22651 /*is_template=*/false,
22652 /*is_namespace=*/false,
22653 /*check_dependency=*/true,
22654 /*ambiguous_decls=*/NULL,
22655 location);
22658 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22659 the current context, return the TYPE_DECL. If TAG_NAME_P is
22660 true, the DECL indicates the class being defined in a class-head,
22661 or declared in an elaborated-type-specifier.
22663 Otherwise, return DECL. */
22665 static tree
22666 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22668 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22669 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22671 struct A {
22672 template <typename T> struct B;
22675 template <typename T> struct A::B {};
22677 Similarly, in an elaborated-type-specifier:
22679 namespace N { struct X{}; }
22681 struct A {
22682 template <typename T> friend struct N::X;
22685 However, if the DECL refers to a class type, and we are in
22686 the scope of the class, then the name lookup automatically
22687 finds the TYPE_DECL created by build_self_reference rather
22688 than a TEMPLATE_DECL. For example, in:
22690 template <class T> struct S {
22691 S s;
22694 there is no need to handle such case. */
22696 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22697 return DECL_TEMPLATE_RESULT (decl);
22699 return decl;
22702 /* If too many, or too few, template-parameter lists apply to the
22703 declarator, issue an error message. Returns TRUE if all went well,
22704 and FALSE otherwise. */
22706 static bool
22707 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22708 cp_declarator *declarator,
22709 location_t declarator_location)
22711 switch (declarator->kind)
22713 case cdk_id:
22715 unsigned num_templates = 0;
22716 tree scope = declarator->u.id.qualifying_scope;
22718 if (scope)
22719 num_templates = num_template_headers_for_class (scope);
22720 else if (TREE_CODE (declarator->u.id.unqualified_name)
22721 == TEMPLATE_ID_EXPR)
22722 /* If the DECLARATOR has the form `X<y>' then it uses one
22723 additional level of template parameters. */
22724 ++num_templates;
22726 return cp_parser_check_template_parameters
22727 (parser, num_templates, declarator_location, declarator);
22730 case cdk_function:
22731 case cdk_array:
22732 case cdk_pointer:
22733 case cdk_reference:
22734 case cdk_ptrmem:
22735 return (cp_parser_check_declarator_template_parameters
22736 (parser, declarator->declarator, declarator_location));
22738 case cdk_error:
22739 return true;
22741 default:
22742 gcc_unreachable ();
22744 return false;
22747 /* NUM_TEMPLATES were used in the current declaration. If that is
22748 invalid, return FALSE and issue an error messages. Otherwise,
22749 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22750 declarator and we can print more accurate diagnostics. */
22752 static bool
22753 cp_parser_check_template_parameters (cp_parser* parser,
22754 unsigned num_templates,
22755 location_t location,
22756 cp_declarator *declarator)
22758 /* If there are the same number of template classes and parameter
22759 lists, that's OK. */
22760 if (parser->num_template_parameter_lists == num_templates)
22761 return true;
22762 /* If there are more, but only one more, then we are referring to a
22763 member template. That's OK too. */
22764 if (parser->num_template_parameter_lists == num_templates + 1)
22765 return true;
22766 /* If there are more template classes than parameter lists, we have
22767 something like:
22769 template <class T> void S<T>::R<T>::f (); */
22770 if (parser->num_template_parameter_lists < num_templates)
22772 if (declarator && !current_function_decl)
22773 error_at (location, "specializing member %<%T::%E%> "
22774 "requires %<template<>%> syntax",
22775 declarator->u.id.qualifying_scope,
22776 declarator->u.id.unqualified_name);
22777 else if (declarator)
22778 error_at (location, "invalid declaration of %<%T::%E%>",
22779 declarator->u.id.qualifying_scope,
22780 declarator->u.id.unqualified_name);
22781 else
22782 error_at (location, "too few template-parameter-lists");
22783 return false;
22785 /* Otherwise, there are too many template parameter lists. We have
22786 something like:
22788 template <class T> template <class U> void S::f(); */
22789 error_at (location, "too many template-parameter-lists");
22790 return false;
22793 /* Parse an optional `::' token indicating that the following name is
22794 from the global namespace. If so, PARSER->SCOPE is set to the
22795 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22796 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22797 Returns the new value of PARSER->SCOPE, if the `::' token is
22798 present, and NULL_TREE otherwise. */
22800 static tree
22801 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22803 cp_token *token;
22805 /* Peek at the next token. */
22806 token = cp_lexer_peek_token (parser->lexer);
22807 /* If we're looking at a `::' token then we're starting from the
22808 global namespace, not our current location. */
22809 if (token->type == CPP_SCOPE)
22811 /* Consume the `::' token. */
22812 cp_lexer_consume_token (parser->lexer);
22813 /* Set the SCOPE so that we know where to start the lookup. */
22814 parser->scope = global_namespace;
22815 parser->qualifying_scope = global_namespace;
22816 parser->object_scope = NULL_TREE;
22818 return parser->scope;
22820 else if (!current_scope_valid_p)
22822 parser->scope = NULL_TREE;
22823 parser->qualifying_scope = NULL_TREE;
22824 parser->object_scope = NULL_TREE;
22827 return NULL_TREE;
22830 /* Returns TRUE if the upcoming token sequence is the start of a
22831 constructor declarator. If FRIEND_P is true, the declarator is
22832 preceded by the `friend' specifier. */
22834 static bool
22835 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22837 bool constructor_p;
22838 bool outside_class_specifier_p;
22839 tree nested_name_specifier;
22840 cp_token *next_token;
22842 /* The common case is that this is not a constructor declarator, so
22843 try to avoid doing lots of work if at all possible. It's not
22844 valid declare a constructor at function scope. */
22845 if (parser->in_function_body)
22846 return false;
22847 /* And only certain tokens can begin a constructor declarator. */
22848 next_token = cp_lexer_peek_token (parser->lexer);
22849 if (next_token->type != CPP_NAME
22850 && next_token->type != CPP_SCOPE
22851 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22852 && next_token->type != CPP_TEMPLATE_ID)
22853 return false;
22855 /* Parse tentatively; we are going to roll back all of the tokens
22856 consumed here. */
22857 cp_parser_parse_tentatively (parser);
22858 /* Assume that we are looking at a constructor declarator. */
22859 constructor_p = true;
22861 /* Look for the optional `::' operator. */
22862 cp_parser_global_scope_opt (parser,
22863 /*current_scope_valid_p=*/false);
22864 /* Look for the nested-name-specifier. */
22865 nested_name_specifier
22866 = (cp_parser_nested_name_specifier_opt (parser,
22867 /*typename_keyword_p=*/false,
22868 /*check_dependency_p=*/false,
22869 /*type_p=*/false,
22870 /*is_declaration=*/false));
22872 outside_class_specifier_p = (!at_class_scope_p ()
22873 || !TYPE_BEING_DEFINED (current_class_type)
22874 || friend_p);
22876 /* Outside of a class-specifier, there must be a
22877 nested-name-specifier. */
22878 if (!nested_name_specifier && outside_class_specifier_p)
22879 constructor_p = false;
22880 else if (nested_name_specifier == error_mark_node)
22881 constructor_p = false;
22883 /* If we have a class scope, this is easy; DR 147 says that S::S always
22884 names the constructor, and no other qualified name could. */
22885 if (constructor_p && nested_name_specifier
22886 && CLASS_TYPE_P (nested_name_specifier))
22888 tree id = cp_parser_unqualified_id (parser,
22889 /*template_keyword_p=*/false,
22890 /*check_dependency_p=*/false,
22891 /*declarator_p=*/true,
22892 /*optional_p=*/false);
22893 if (is_overloaded_fn (id))
22894 id = DECL_NAME (get_first_fn (id));
22895 if (!constructor_name_p (id, nested_name_specifier))
22896 constructor_p = false;
22898 /* If we still think that this might be a constructor-declarator,
22899 look for a class-name. */
22900 else if (constructor_p)
22902 /* If we have:
22904 template <typename T> struct S {
22905 S();
22908 we must recognize that the nested `S' names a class. */
22909 tree type_decl;
22910 type_decl = cp_parser_class_name (parser,
22911 /*typename_keyword_p=*/false,
22912 /*template_keyword_p=*/false,
22913 none_type,
22914 /*check_dependency_p=*/false,
22915 /*class_head_p=*/false,
22916 /*is_declaration=*/false);
22917 /* If there was no class-name, then this is not a constructor.
22918 Otherwise, if we are in a class-specifier and we aren't
22919 handling a friend declaration, check that its type matches
22920 current_class_type (c++/38313). Note: error_mark_node
22921 is left alone for error recovery purposes. */
22922 constructor_p = (!cp_parser_error_occurred (parser)
22923 && (outside_class_specifier_p
22924 || type_decl == error_mark_node
22925 || same_type_p (current_class_type,
22926 TREE_TYPE (type_decl))));
22928 /* If we're still considering a constructor, we have to see a `(',
22929 to begin the parameter-declaration-clause, followed by either a
22930 `)', an `...', or a decl-specifier. We need to check for a
22931 type-specifier to avoid being fooled into thinking that:
22933 S (f) (int);
22935 is a constructor. (It is actually a function named `f' that
22936 takes one parameter (of type `int') and returns a value of type
22937 `S'. */
22938 if (constructor_p
22939 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22940 constructor_p = false;
22942 if (constructor_p
22943 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22944 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22945 /* A parameter declaration begins with a decl-specifier,
22946 which is either the "attribute" keyword, a storage class
22947 specifier, or (usually) a type-specifier. */
22948 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22950 tree type;
22951 tree pushed_scope = NULL_TREE;
22952 unsigned saved_num_template_parameter_lists;
22954 /* Names appearing in the type-specifier should be looked up
22955 in the scope of the class. */
22956 if (current_class_type)
22957 type = NULL_TREE;
22958 else
22960 type = TREE_TYPE (type_decl);
22961 if (TREE_CODE (type) == TYPENAME_TYPE)
22963 type = resolve_typename_type (type,
22964 /*only_current_p=*/false);
22965 if (TREE_CODE (type) == TYPENAME_TYPE)
22967 cp_parser_abort_tentative_parse (parser);
22968 return false;
22971 pushed_scope = push_scope (type);
22974 /* Inside the constructor parameter list, surrounding
22975 template-parameter-lists do not apply. */
22976 saved_num_template_parameter_lists
22977 = parser->num_template_parameter_lists;
22978 parser->num_template_parameter_lists = 0;
22980 /* Look for the type-specifier. */
22981 cp_parser_type_specifier (parser,
22982 CP_PARSER_FLAGS_NONE,
22983 /*decl_specs=*/NULL,
22984 /*is_declarator=*/true,
22985 /*declares_class_or_enum=*/NULL,
22986 /*is_cv_qualifier=*/NULL);
22988 parser->num_template_parameter_lists
22989 = saved_num_template_parameter_lists;
22991 /* Leave the scope of the class. */
22992 if (pushed_scope)
22993 pop_scope (pushed_scope);
22995 constructor_p = !cp_parser_error_occurred (parser);
22999 /* We did not really want to consume any tokens. */
23000 cp_parser_abort_tentative_parse (parser);
23002 return constructor_p;
23005 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23006 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23007 they must be performed once we are in the scope of the function.
23009 Returns the function defined. */
23011 static tree
23012 cp_parser_function_definition_from_specifiers_and_declarator
23013 (cp_parser* parser,
23014 cp_decl_specifier_seq *decl_specifiers,
23015 tree attributes,
23016 const cp_declarator *declarator)
23018 tree fn;
23019 bool success_p;
23021 /* Begin the function-definition. */
23022 success_p = start_function (decl_specifiers, declarator, attributes);
23024 /* The things we're about to see are not directly qualified by any
23025 template headers we've seen thus far. */
23026 reset_specialization ();
23028 /* If there were names looked up in the decl-specifier-seq that we
23029 did not check, check them now. We must wait until we are in the
23030 scope of the function to perform the checks, since the function
23031 might be a friend. */
23032 perform_deferred_access_checks (tf_warning_or_error);
23034 if (success_p)
23036 cp_finalize_omp_declare_simd (parser, current_function_decl);
23037 parser->omp_declare_simd = NULL;
23040 if (!success_p)
23042 /* Skip the entire function. */
23043 cp_parser_skip_to_end_of_block_or_statement (parser);
23044 fn = error_mark_node;
23046 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23048 /* Seen already, skip it. An error message has already been output. */
23049 cp_parser_skip_to_end_of_block_or_statement (parser);
23050 fn = current_function_decl;
23051 current_function_decl = NULL_TREE;
23052 /* If this is a function from a class, pop the nested class. */
23053 if (current_class_name)
23054 pop_nested_class ();
23056 else
23058 timevar_id_t tv;
23059 if (DECL_DECLARED_INLINE_P (current_function_decl))
23060 tv = TV_PARSE_INLINE;
23061 else
23062 tv = TV_PARSE_FUNC;
23063 timevar_push (tv);
23064 fn = cp_parser_function_definition_after_declarator (parser,
23065 /*inline_p=*/false);
23066 timevar_pop (tv);
23069 return fn;
23072 /* Parse the part of a function-definition that follows the
23073 declarator. INLINE_P is TRUE iff this function is an inline
23074 function defined within a class-specifier.
23076 Returns the function defined. */
23078 static tree
23079 cp_parser_function_definition_after_declarator (cp_parser* parser,
23080 bool inline_p)
23082 tree fn;
23083 bool ctor_initializer_p = false;
23084 bool saved_in_unbraced_linkage_specification_p;
23085 bool saved_in_function_body;
23086 unsigned saved_num_template_parameter_lists;
23087 cp_token *token;
23088 bool fully_implicit_function_template_p
23089 = parser->fully_implicit_function_template_p;
23090 parser->fully_implicit_function_template_p = false;
23091 tree implicit_template_parms
23092 = parser->implicit_template_parms;
23093 parser->implicit_template_parms = 0;
23094 cp_binding_level* implicit_template_scope
23095 = parser->implicit_template_scope;
23096 parser->implicit_template_scope = 0;
23098 saved_in_function_body = parser->in_function_body;
23099 parser->in_function_body = true;
23100 /* If the next token is `return', then the code may be trying to
23101 make use of the "named return value" extension that G++ used to
23102 support. */
23103 token = cp_lexer_peek_token (parser->lexer);
23104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23106 /* Consume the `return' keyword. */
23107 cp_lexer_consume_token (parser->lexer);
23108 /* Look for the identifier that indicates what value is to be
23109 returned. */
23110 cp_parser_identifier (parser);
23111 /* Issue an error message. */
23112 error_at (token->location,
23113 "named return values are no longer supported");
23114 /* Skip tokens until we reach the start of the function body. */
23115 while (true)
23117 cp_token *token = cp_lexer_peek_token (parser->lexer);
23118 if (token->type == CPP_OPEN_BRACE
23119 || token->type == CPP_EOF
23120 || token->type == CPP_PRAGMA_EOL)
23121 break;
23122 cp_lexer_consume_token (parser->lexer);
23125 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23126 anything declared inside `f'. */
23127 saved_in_unbraced_linkage_specification_p
23128 = parser->in_unbraced_linkage_specification_p;
23129 parser->in_unbraced_linkage_specification_p = false;
23130 /* Inside the function, surrounding template-parameter-lists do not
23131 apply. */
23132 saved_num_template_parameter_lists
23133 = parser->num_template_parameter_lists;
23134 parser->num_template_parameter_lists = 0;
23136 start_lambda_scope (current_function_decl);
23138 /* If the next token is `try', `__transaction_atomic', or
23139 `__transaction_relaxed`, then we are looking at either function-try-block
23140 or function-transaction-block. Note that all of these include the
23141 function-body. */
23142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23143 ctor_initializer_p = cp_parser_function_transaction (parser,
23144 RID_TRANSACTION_ATOMIC);
23145 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23146 RID_TRANSACTION_RELAXED))
23147 ctor_initializer_p = cp_parser_function_transaction (parser,
23148 RID_TRANSACTION_RELAXED);
23149 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23150 ctor_initializer_p = cp_parser_function_try_block (parser);
23151 else
23152 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23153 (parser, /*in_function_try_block=*/false);
23155 finish_lambda_scope ();
23157 /* Finish the function. */
23158 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23159 (inline_p ? 2 : 0));
23160 /* Generate code for it, if necessary. */
23161 expand_or_defer_fn (fn);
23162 /* Restore the saved values. */
23163 parser->in_unbraced_linkage_specification_p
23164 = saved_in_unbraced_linkage_specification_p;
23165 parser->num_template_parameter_lists
23166 = saved_num_template_parameter_lists;
23167 parser->in_function_body = saved_in_function_body;
23169 parser->fully_implicit_function_template_p
23170 = fully_implicit_function_template_p;
23171 parser->implicit_template_parms
23172 = implicit_template_parms;
23173 parser->implicit_template_scope
23174 = implicit_template_scope;
23176 if (parser->fully_implicit_function_template_p)
23177 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23179 return fn;
23182 /* Parse a template-declaration, assuming that the `export' (and
23183 `extern') keywords, if present, has already been scanned. MEMBER_P
23184 is as for cp_parser_template_declaration. */
23186 static void
23187 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23189 tree decl = NULL_TREE;
23190 vec<deferred_access_check, va_gc> *checks;
23191 tree parameter_list;
23192 bool friend_p = false;
23193 bool need_lang_pop;
23194 cp_token *token;
23196 /* Look for the `template' keyword. */
23197 token = cp_lexer_peek_token (parser->lexer);
23198 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23199 return;
23201 /* And the `<'. */
23202 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23203 return;
23204 if (at_class_scope_p () && current_function_decl)
23206 /* 14.5.2.2 [temp.mem]
23208 A local class shall not have member templates. */
23209 error_at (token->location,
23210 "invalid declaration of member template in local class");
23211 cp_parser_skip_to_end_of_block_or_statement (parser);
23212 return;
23214 /* [temp]
23216 A template ... shall not have C linkage. */
23217 if (current_lang_name == lang_name_c)
23219 error_at (token->location, "template with C linkage");
23220 /* Give it C++ linkage to avoid confusing other parts of the
23221 front end. */
23222 push_lang_context (lang_name_cplusplus);
23223 need_lang_pop = true;
23225 else
23226 need_lang_pop = false;
23228 /* We cannot perform access checks on the template parameter
23229 declarations until we know what is being declared, just as we
23230 cannot check the decl-specifier list. */
23231 push_deferring_access_checks (dk_deferred);
23233 /* If the next token is `>', then we have an invalid
23234 specialization. Rather than complain about an invalid template
23235 parameter, issue an error message here. */
23236 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23238 cp_parser_error (parser, "invalid explicit specialization");
23239 begin_specialization ();
23240 parameter_list = NULL_TREE;
23242 else
23244 /* Parse the template parameters. */
23245 parameter_list = cp_parser_template_parameter_list (parser);
23248 /* Get the deferred access checks from the parameter list. These
23249 will be checked once we know what is being declared, as for a
23250 member template the checks must be performed in the scope of the
23251 class containing the member. */
23252 checks = get_deferred_access_checks ();
23254 /* Look for the `>'. */
23255 cp_parser_skip_to_end_of_template_parameter_list (parser);
23256 /* We just processed one more parameter list. */
23257 ++parser->num_template_parameter_lists;
23258 /* If the next token is `template', there are more template
23259 parameters. */
23260 if (cp_lexer_next_token_is_keyword (parser->lexer,
23261 RID_TEMPLATE))
23262 cp_parser_template_declaration_after_export (parser, member_p);
23263 else if (cxx_dialect >= cxx11
23264 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23265 decl = cp_parser_alias_declaration (parser);
23266 else
23268 /* There are no access checks when parsing a template, as we do not
23269 know if a specialization will be a friend. */
23270 push_deferring_access_checks (dk_no_check);
23271 token = cp_lexer_peek_token (parser->lexer);
23272 decl = cp_parser_single_declaration (parser,
23273 checks,
23274 member_p,
23275 /*explicit_specialization_p=*/false,
23276 &friend_p);
23277 pop_deferring_access_checks ();
23279 /* If this is a member template declaration, let the front
23280 end know. */
23281 if (member_p && !friend_p && decl)
23283 if (TREE_CODE (decl) == TYPE_DECL)
23284 cp_parser_check_access_in_redeclaration (decl, token->location);
23286 decl = finish_member_template_decl (decl);
23288 else if (friend_p && decl
23289 && DECL_DECLARES_TYPE_P (decl))
23290 make_friend_class (current_class_type, TREE_TYPE (decl),
23291 /*complain=*/true);
23293 /* We are done with the current parameter list. */
23294 --parser->num_template_parameter_lists;
23296 pop_deferring_access_checks ();
23298 /* Finish up. */
23299 finish_template_decl (parameter_list);
23301 /* Check the template arguments for a literal operator template. */
23302 if (decl
23303 && DECL_DECLARES_FUNCTION_P (decl)
23304 && UDLIT_OPER_P (DECL_NAME (decl)))
23306 bool ok = true;
23307 if (parameter_list == NULL_TREE)
23308 ok = false;
23309 else
23311 int num_parms = TREE_VEC_LENGTH (parameter_list);
23312 if (num_parms == 1)
23314 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23315 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23316 if (TREE_TYPE (parm) != char_type_node
23317 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23318 ok = false;
23320 else if (num_parms == 2 && cxx_dialect >= cxx14)
23322 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23323 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23324 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23325 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23326 if (TREE_TYPE (parm) != TREE_TYPE (type)
23327 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23328 ok = false;
23330 else
23331 ok = false;
23333 if (!ok)
23335 if (cxx_dialect >= cxx14)
23336 error ("literal operator template %qD has invalid parameter list."
23337 " Expected non-type template argument pack <char...>"
23338 " or <typename CharT, CharT...>",
23339 decl);
23340 else
23341 error ("literal operator template %qD has invalid parameter list."
23342 " Expected non-type template argument pack <char...>",
23343 decl);
23346 /* Register member declarations. */
23347 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23348 finish_member_declaration (decl);
23349 /* For the erroneous case of a template with C linkage, we pushed an
23350 implicit C++ linkage scope; exit that scope now. */
23351 if (need_lang_pop)
23352 pop_lang_context ();
23353 /* If DECL is a function template, we must return to parse it later.
23354 (Even though there is no definition, there might be default
23355 arguments that need handling.) */
23356 if (member_p && decl
23357 && DECL_DECLARES_FUNCTION_P (decl))
23358 vec_safe_push (unparsed_funs_with_definitions, decl);
23361 /* Perform the deferred access checks from a template-parameter-list.
23362 CHECKS is a TREE_LIST of access checks, as returned by
23363 get_deferred_access_checks. */
23365 static void
23366 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23368 ++processing_template_parmlist;
23369 perform_access_checks (checks, tf_warning_or_error);
23370 --processing_template_parmlist;
23373 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23374 `function-definition' sequence that follows a template header.
23375 If MEMBER_P is true, this declaration appears in a class scope.
23377 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23378 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23380 static tree
23381 cp_parser_single_declaration (cp_parser* parser,
23382 vec<deferred_access_check, va_gc> *checks,
23383 bool member_p,
23384 bool explicit_specialization_p,
23385 bool* friend_p)
23387 int declares_class_or_enum;
23388 tree decl = NULL_TREE;
23389 cp_decl_specifier_seq decl_specifiers;
23390 bool function_definition_p = false;
23391 cp_token *decl_spec_token_start;
23393 /* This function is only used when processing a template
23394 declaration. */
23395 gcc_assert (innermost_scope_kind () == sk_template_parms
23396 || innermost_scope_kind () == sk_template_spec);
23398 /* Defer access checks until we know what is being declared. */
23399 push_deferring_access_checks (dk_deferred);
23401 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23402 alternative. */
23403 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23404 cp_parser_decl_specifier_seq (parser,
23405 CP_PARSER_FLAGS_OPTIONAL,
23406 &decl_specifiers,
23407 &declares_class_or_enum);
23408 if (friend_p)
23409 *friend_p = cp_parser_friend_p (&decl_specifiers);
23411 /* There are no template typedefs. */
23412 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23414 error_at (decl_spec_token_start->location,
23415 "template declaration of %<typedef%>");
23416 decl = error_mark_node;
23419 /* Gather up the access checks that occurred the
23420 decl-specifier-seq. */
23421 stop_deferring_access_checks ();
23423 /* Check for the declaration of a template class. */
23424 if (declares_class_or_enum)
23426 if (cp_parser_declares_only_class_p (parser))
23428 decl = shadow_tag (&decl_specifiers);
23430 /* In this case:
23432 struct C {
23433 friend template <typename T> struct A<T>::B;
23436 A<T>::B will be represented by a TYPENAME_TYPE, and
23437 therefore not recognized by shadow_tag. */
23438 if (friend_p && *friend_p
23439 && !decl
23440 && decl_specifiers.type
23441 && TYPE_P (decl_specifiers.type))
23442 decl = decl_specifiers.type;
23444 if (decl && decl != error_mark_node)
23445 decl = TYPE_NAME (decl);
23446 else
23447 decl = error_mark_node;
23449 /* Perform access checks for template parameters. */
23450 cp_parser_perform_template_parameter_access_checks (checks);
23454 /* Complain about missing 'typename' or other invalid type names. */
23455 if (!decl_specifiers.any_type_specifiers_p
23456 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23458 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23459 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23460 the rest of this declaration. */
23461 decl = error_mark_node;
23462 goto out;
23465 /* If it's not a template class, try for a template function. If
23466 the next token is a `;', then this declaration does not declare
23467 anything. But, if there were errors in the decl-specifiers, then
23468 the error might well have come from an attempted class-specifier.
23469 In that case, there's no need to warn about a missing declarator. */
23470 if (!decl
23471 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23472 || decl_specifiers.type != error_mark_node))
23474 decl = cp_parser_init_declarator (parser,
23475 &decl_specifiers,
23476 checks,
23477 /*function_definition_allowed_p=*/true,
23478 member_p,
23479 declares_class_or_enum,
23480 &function_definition_p,
23481 NULL);
23483 /* 7.1.1-1 [dcl.stc]
23485 A storage-class-specifier shall not be specified in an explicit
23486 specialization... */
23487 if (decl
23488 && explicit_specialization_p
23489 && decl_specifiers.storage_class != sc_none)
23491 error_at (decl_spec_token_start->location,
23492 "explicit template specialization cannot have a storage class");
23493 decl = error_mark_node;
23496 if (decl && VAR_P (decl))
23497 check_template_variable (decl);
23500 /* Look for a trailing `;' after the declaration. */
23501 if (!function_definition_p
23502 && (decl == error_mark_node
23503 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23504 cp_parser_skip_to_end_of_block_or_statement (parser);
23506 out:
23507 pop_deferring_access_checks ();
23509 /* Clear any current qualification; whatever comes next is the start
23510 of something new. */
23511 parser->scope = NULL_TREE;
23512 parser->qualifying_scope = NULL_TREE;
23513 parser->object_scope = NULL_TREE;
23515 return decl;
23518 /* Parse a cast-expression that is not the operand of a unary "&". */
23520 static tree
23521 cp_parser_simple_cast_expression (cp_parser *parser)
23523 return cp_parser_cast_expression (parser, /*address_p=*/false,
23524 /*cast_p=*/false, /*decltype*/false, NULL);
23527 /* Parse a functional cast to TYPE. Returns an expression
23528 representing the cast. */
23530 static tree
23531 cp_parser_functional_cast (cp_parser* parser, tree type)
23533 vec<tree, va_gc> *vec;
23534 tree expression_list;
23535 tree cast;
23536 bool nonconst_p;
23538 if (!type)
23539 type = error_mark_node;
23541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23543 cp_lexer_set_source_position (parser->lexer);
23544 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23545 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23546 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23547 if (TREE_CODE (type) == TYPE_DECL)
23548 type = TREE_TYPE (type);
23549 return finish_compound_literal (type, expression_list,
23550 tf_warning_or_error);
23554 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23555 /*cast_p=*/true,
23556 /*allow_expansion_p=*/true,
23557 /*non_constant_p=*/NULL);
23558 if (vec == NULL)
23559 expression_list = error_mark_node;
23560 else
23562 expression_list = build_tree_list_vec (vec);
23563 release_tree_vector (vec);
23566 cast = build_functional_cast (type, expression_list,
23567 tf_warning_or_error);
23568 /* [expr.const]/1: In an integral constant expression "only type
23569 conversions to integral or enumeration type can be used". */
23570 if (TREE_CODE (type) == TYPE_DECL)
23571 type = TREE_TYPE (type);
23572 if (cast != error_mark_node
23573 && !cast_valid_in_integral_constant_expression_p (type)
23574 && cp_parser_non_integral_constant_expression (parser,
23575 NIC_CONSTRUCTOR))
23576 return error_mark_node;
23577 return cast;
23580 /* Save the tokens that make up the body of a member function defined
23581 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23582 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23583 specifiers applied to the declaration. Returns the FUNCTION_DECL
23584 for the member function. */
23586 static tree
23587 cp_parser_save_member_function_body (cp_parser* parser,
23588 cp_decl_specifier_seq *decl_specifiers,
23589 cp_declarator *declarator,
23590 tree attributes)
23592 cp_token *first;
23593 cp_token *last;
23594 tree fn;
23596 /* Create the FUNCTION_DECL. */
23597 fn = grokmethod (decl_specifiers, declarator, attributes);
23598 cp_finalize_omp_declare_simd (parser, fn);
23599 /* If something went badly wrong, bail out now. */
23600 if (fn == error_mark_node)
23602 /* If there's a function-body, skip it. */
23603 if (cp_parser_token_starts_function_definition_p
23604 (cp_lexer_peek_token (parser->lexer)))
23605 cp_parser_skip_to_end_of_block_or_statement (parser);
23606 return error_mark_node;
23609 /* Remember it, if there default args to post process. */
23610 cp_parser_save_default_args (parser, fn);
23612 /* Save away the tokens that make up the body of the
23613 function. */
23614 first = parser->lexer->next_token;
23615 /* Handle function try blocks. */
23616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23617 cp_lexer_consume_token (parser->lexer);
23618 /* We can have braced-init-list mem-initializers before the fn body. */
23619 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23621 cp_lexer_consume_token (parser->lexer);
23622 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23624 /* cache_group will stop after an un-nested { } pair, too. */
23625 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23626 break;
23628 /* variadic mem-inits have ... after the ')'. */
23629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23630 cp_lexer_consume_token (parser->lexer);
23633 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23634 /* Handle function try blocks. */
23635 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23636 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23637 last = parser->lexer->next_token;
23639 /* Save away the inline definition; we will process it when the
23640 class is complete. */
23641 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23642 DECL_PENDING_INLINE_P (fn) = 1;
23644 /* We need to know that this was defined in the class, so that
23645 friend templates are handled correctly. */
23646 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23648 /* Add FN to the queue of functions to be parsed later. */
23649 vec_safe_push (unparsed_funs_with_definitions, fn);
23651 return fn;
23654 /* Save the tokens that make up the in-class initializer for a non-static
23655 data member. Returns a DEFAULT_ARG. */
23657 static tree
23658 cp_parser_save_nsdmi (cp_parser* parser)
23660 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23663 /* Parse a template-argument-list, as well as the trailing ">" (but
23664 not the opening "<"). See cp_parser_template_argument_list for the
23665 return value. */
23667 static tree
23668 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23670 tree arguments;
23671 tree saved_scope;
23672 tree saved_qualifying_scope;
23673 tree saved_object_scope;
23674 bool saved_greater_than_is_operator_p;
23675 int saved_unevaluated_operand;
23676 int saved_inhibit_evaluation_warnings;
23678 /* [temp.names]
23680 When parsing a template-id, the first non-nested `>' is taken as
23681 the end of the template-argument-list rather than a greater-than
23682 operator. */
23683 saved_greater_than_is_operator_p
23684 = parser->greater_than_is_operator_p;
23685 parser->greater_than_is_operator_p = false;
23686 /* Parsing the argument list may modify SCOPE, so we save it
23687 here. */
23688 saved_scope = parser->scope;
23689 saved_qualifying_scope = parser->qualifying_scope;
23690 saved_object_scope = parser->object_scope;
23691 /* We need to evaluate the template arguments, even though this
23692 template-id may be nested within a "sizeof". */
23693 saved_unevaluated_operand = cp_unevaluated_operand;
23694 cp_unevaluated_operand = 0;
23695 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23696 c_inhibit_evaluation_warnings = 0;
23697 /* Parse the template-argument-list itself. */
23698 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23699 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23700 arguments = NULL_TREE;
23701 else
23702 arguments = cp_parser_template_argument_list (parser);
23703 /* Look for the `>' that ends the template-argument-list. If we find
23704 a '>>' instead, it's probably just a typo. */
23705 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23707 if (cxx_dialect != cxx98)
23709 /* In C++0x, a `>>' in a template argument list or cast
23710 expression is considered to be two separate `>'
23711 tokens. So, change the current token to a `>', but don't
23712 consume it: it will be consumed later when the outer
23713 template argument list (or cast expression) is parsed.
23714 Note that this replacement of `>' for `>>' is necessary
23715 even if we are parsing tentatively: in the tentative
23716 case, after calling
23717 cp_parser_enclosed_template_argument_list we will always
23718 throw away all of the template arguments and the first
23719 closing `>', either because the template argument list
23720 was erroneous or because we are replacing those tokens
23721 with a CPP_TEMPLATE_ID token. The second `>' (which will
23722 not have been thrown away) is needed either to close an
23723 outer template argument list or to complete a new-style
23724 cast. */
23725 cp_token *token = cp_lexer_peek_token (parser->lexer);
23726 token->type = CPP_GREATER;
23728 else if (!saved_greater_than_is_operator_p)
23730 /* If we're in a nested template argument list, the '>>' has
23731 to be a typo for '> >'. We emit the error message, but we
23732 continue parsing and we push a '>' as next token, so that
23733 the argument list will be parsed correctly. Note that the
23734 global source location is still on the token before the
23735 '>>', so we need to say explicitly where we want it. */
23736 cp_token *token = cp_lexer_peek_token (parser->lexer);
23737 error_at (token->location, "%<>>%> should be %<> >%> "
23738 "within a nested template argument list");
23740 token->type = CPP_GREATER;
23742 else
23744 /* If this is not a nested template argument list, the '>>'
23745 is a typo for '>'. Emit an error message and continue.
23746 Same deal about the token location, but here we can get it
23747 right by consuming the '>>' before issuing the diagnostic. */
23748 cp_token *token = cp_lexer_consume_token (parser->lexer);
23749 error_at (token->location,
23750 "spurious %<>>%>, use %<>%> to terminate "
23751 "a template argument list");
23754 else
23755 cp_parser_skip_to_end_of_template_parameter_list (parser);
23756 /* The `>' token might be a greater-than operator again now. */
23757 parser->greater_than_is_operator_p
23758 = saved_greater_than_is_operator_p;
23759 /* Restore the SAVED_SCOPE. */
23760 parser->scope = saved_scope;
23761 parser->qualifying_scope = saved_qualifying_scope;
23762 parser->object_scope = saved_object_scope;
23763 cp_unevaluated_operand = saved_unevaluated_operand;
23764 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23766 return arguments;
23769 /* MEMBER_FUNCTION is a member function, or a friend. If default
23770 arguments, or the body of the function have not yet been parsed,
23771 parse them now. */
23773 static void
23774 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23776 timevar_push (TV_PARSE_INMETH);
23777 /* If this member is a template, get the underlying
23778 FUNCTION_DECL. */
23779 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23780 member_function = DECL_TEMPLATE_RESULT (member_function);
23782 /* There should not be any class definitions in progress at this
23783 point; the bodies of members are only parsed outside of all class
23784 definitions. */
23785 gcc_assert (parser->num_classes_being_defined == 0);
23786 /* While we're parsing the member functions we might encounter more
23787 classes. We want to handle them right away, but we don't want
23788 them getting mixed up with functions that are currently in the
23789 queue. */
23790 push_unparsed_function_queues (parser);
23792 /* Make sure that any template parameters are in scope. */
23793 maybe_begin_member_template_processing (member_function);
23795 /* If the body of the function has not yet been parsed, parse it
23796 now. */
23797 if (DECL_PENDING_INLINE_P (member_function))
23799 tree function_scope;
23800 cp_token_cache *tokens;
23802 /* The function is no longer pending; we are processing it. */
23803 tokens = DECL_PENDING_INLINE_INFO (member_function);
23804 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23805 DECL_PENDING_INLINE_P (member_function) = 0;
23807 /* If this is a local class, enter the scope of the containing
23808 function. */
23809 function_scope = current_function_decl;
23810 if (function_scope)
23811 push_function_context ();
23813 /* Push the body of the function onto the lexer stack. */
23814 cp_parser_push_lexer_for_tokens (parser, tokens);
23816 /* Let the front end know that we going to be defining this
23817 function. */
23818 start_preparsed_function (member_function, NULL_TREE,
23819 SF_PRE_PARSED | SF_INCLASS_INLINE);
23821 /* Don't do access checking if it is a templated function. */
23822 if (processing_template_decl)
23823 push_deferring_access_checks (dk_no_check);
23825 /* #pragma omp declare reduction needs special parsing. */
23826 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23828 parser->lexer->in_pragma = true;
23829 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23830 finish_function (/*inline*/2);
23831 cp_check_omp_declare_reduction (member_function);
23833 else
23834 /* Now, parse the body of the function. */
23835 cp_parser_function_definition_after_declarator (parser,
23836 /*inline_p=*/true);
23838 if (processing_template_decl)
23839 pop_deferring_access_checks ();
23841 /* Leave the scope of the containing function. */
23842 if (function_scope)
23843 pop_function_context ();
23844 cp_parser_pop_lexer (parser);
23847 /* Remove any template parameters from the symbol table. */
23848 maybe_end_member_template_processing ();
23850 /* Restore the queue. */
23851 pop_unparsed_function_queues (parser);
23852 timevar_pop (TV_PARSE_INMETH);
23855 /* If DECL contains any default args, remember it on the unparsed
23856 functions queue. */
23858 static void
23859 cp_parser_save_default_args (cp_parser* parser, tree decl)
23861 tree probe;
23863 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23864 probe;
23865 probe = TREE_CHAIN (probe))
23866 if (TREE_PURPOSE (probe))
23868 cp_default_arg_entry entry = {current_class_type, decl};
23869 vec_safe_push (unparsed_funs_with_default_args, entry);
23870 break;
23874 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23875 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23876 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23877 from the parameter-type-list. */
23879 static tree
23880 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23881 tree default_arg, tree parmtype)
23883 cp_token_cache *tokens;
23884 tree parsed_arg;
23885 bool dummy;
23887 if (default_arg == error_mark_node)
23888 return error_mark_node;
23890 /* Push the saved tokens for the default argument onto the parser's
23891 lexer stack. */
23892 tokens = DEFARG_TOKENS (default_arg);
23893 cp_parser_push_lexer_for_tokens (parser, tokens);
23895 start_lambda_scope (decl);
23897 /* Parse the default argument. */
23898 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23899 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23900 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23902 finish_lambda_scope ();
23904 if (parsed_arg == error_mark_node)
23905 cp_parser_skip_to_end_of_statement (parser);
23907 if (!processing_template_decl)
23909 /* In a non-template class, check conversions now. In a template,
23910 we'll wait and instantiate these as needed. */
23911 if (TREE_CODE (decl) == PARM_DECL)
23912 parsed_arg = check_default_argument (parmtype, parsed_arg,
23913 tf_warning_or_error);
23914 else
23915 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
23918 /* If the token stream has not been completely used up, then
23919 there was extra junk after the end of the default
23920 argument. */
23921 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23923 if (TREE_CODE (decl) == PARM_DECL)
23924 cp_parser_error (parser, "expected %<,%>");
23925 else
23926 cp_parser_error (parser, "expected %<;%>");
23929 /* Revert to the main lexer. */
23930 cp_parser_pop_lexer (parser);
23932 return parsed_arg;
23935 /* FIELD is a non-static data member with an initializer which we saved for
23936 later; parse it now. */
23938 static void
23939 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23941 tree def;
23943 maybe_begin_member_template_processing (field);
23945 push_unparsed_function_queues (parser);
23946 def = cp_parser_late_parse_one_default_arg (parser, field,
23947 DECL_INITIAL (field),
23948 NULL_TREE);
23949 pop_unparsed_function_queues (parser);
23951 maybe_end_member_template_processing ();
23953 DECL_INITIAL (field) = def;
23956 /* FN is a FUNCTION_DECL which may contains a parameter with an
23957 unparsed DEFAULT_ARG. Parse the default args now. This function
23958 assumes that the current scope is the scope in which the default
23959 argument should be processed. */
23961 static void
23962 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23964 bool saved_local_variables_forbidden_p;
23965 tree parm, parmdecl;
23967 /* While we're parsing the default args, we might (due to the
23968 statement expression extension) encounter more classes. We want
23969 to handle them right away, but we don't want them getting mixed
23970 up with default args that are currently in the queue. */
23971 push_unparsed_function_queues (parser);
23973 /* Local variable names (and the `this' keyword) may not appear
23974 in a default argument. */
23975 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23976 parser->local_variables_forbidden_p = true;
23978 push_defarg_context (fn);
23980 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23981 parmdecl = DECL_ARGUMENTS (fn);
23982 parm && parm != void_list_node;
23983 parm = TREE_CHAIN (parm),
23984 parmdecl = DECL_CHAIN (parmdecl))
23986 tree default_arg = TREE_PURPOSE (parm);
23987 tree parsed_arg;
23988 vec<tree, va_gc> *insts;
23989 tree copy;
23990 unsigned ix;
23992 if (!default_arg)
23993 continue;
23995 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23996 /* This can happen for a friend declaration for a function
23997 already declared with default arguments. */
23998 continue;
24000 parsed_arg
24001 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24002 default_arg,
24003 TREE_VALUE (parm));
24004 if (parsed_arg == error_mark_node)
24006 continue;
24009 TREE_PURPOSE (parm) = parsed_arg;
24011 /* Update any instantiations we've already created. */
24012 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24013 vec_safe_iterate (insts, ix, &copy); ix++)
24014 TREE_PURPOSE (copy) = parsed_arg;
24017 pop_defarg_context ();
24019 /* Make sure no default arg is missing. */
24020 check_default_args (fn);
24022 /* Restore the state of local_variables_forbidden_p. */
24023 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24025 /* Restore the queue. */
24026 pop_unparsed_function_queues (parser);
24029 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24031 sizeof ... ( identifier )
24033 where the 'sizeof' token has already been consumed. */
24035 static tree
24036 cp_parser_sizeof_pack (cp_parser *parser)
24038 /* Consume the `...'. */
24039 cp_lexer_consume_token (parser->lexer);
24040 maybe_warn_variadic_templates ();
24042 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24043 if (paren)
24044 cp_lexer_consume_token (parser->lexer);
24045 else
24046 permerror (cp_lexer_peek_token (parser->lexer)->location,
24047 "%<sizeof...%> argument must be surrounded by parentheses");
24049 cp_token *token = cp_lexer_peek_token (parser->lexer);
24050 tree name = cp_parser_identifier (parser);
24051 if (name == error_mark_node)
24052 return error_mark_node;
24053 /* The name is not qualified. */
24054 parser->scope = NULL_TREE;
24055 parser->qualifying_scope = NULL_TREE;
24056 parser->object_scope = NULL_TREE;
24057 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24058 if (expr == error_mark_node)
24059 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24060 token->location);
24061 if (TREE_CODE (expr) == TYPE_DECL)
24062 expr = TREE_TYPE (expr);
24063 else if (TREE_CODE (expr) == CONST_DECL)
24064 expr = DECL_INITIAL (expr);
24065 expr = make_pack_expansion (expr);
24067 if (paren)
24068 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24070 return expr;
24073 /* Parse the operand of `sizeof' (or a similar operator). Returns
24074 either a TYPE or an expression, depending on the form of the
24075 input. The KEYWORD indicates which kind of expression we have
24076 encountered. */
24078 static tree
24079 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24081 tree expr = NULL_TREE;
24082 const char *saved_message;
24083 char *tmp;
24084 bool saved_integral_constant_expression_p;
24085 bool saved_non_integral_constant_expression_p;
24087 /* If it's a `...', then we are computing the length of a parameter
24088 pack. */
24089 if (keyword == RID_SIZEOF
24090 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24091 return cp_parser_sizeof_pack (parser);
24093 /* Types cannot be defined in a `sizeof' expression. Save away the
24094 old message. */
24095 saved_message = parser->type_definition_forbidden_message;
24096 /* And create the new one. */
24097 tmp = concat ("types may not be defined in %<",
24098 IDENTIFIER_POINTER (ridpointers[keyword]),
24099 "%> expressions", NULL);
24100 parser->type_definition_forbidden_message = tmp;
24102 /* The restrictions on constant-expressions do not apply inside
24103 sizeof expressions. */
24104 saved_integral_constant_expression_p
24105 = parser->integral_constant_expression_p;
24106 saved_non_integral_constant_expression_p
24107 = parser->non_integral_constant_expression_p;
24108 parser->integral_constant_expression_p = false;
24110 /* Do not actually evaluate the expression. */
24111 ++cp_unevaluated_operand;
24112 ++c_inhibit_evaluation_warnings;
24113 /* If it's a `(', then we might be looking at the type-id
24114 construction. */
24115 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24117 tree type = NULL_TREE;
24119 /* We can't be sure yet whether we're looking at a type-id or an
24120 expression. */
24121 cp_parser_parse_tentatively (parser);
24122 /* Note: as a GNU Extension, compound literals are considered
24123 postfix-expressions as they are in C99, so they are valid
24124 arguments to sizeof. See comment in cp_parser_cast_expression
24125 for details. */
24126 if (cp_parser_compound_literal_p (parser))
24127 cp_parser_simulate_error (parser);
24128 else
24130 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24131 parser->in_type_id_in_expr_p = true;
24132 /* Look for the type-id. */
24133 type = cp_parser_type_id (parser);
24134 /* Look for the closing `)'. */
24135 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24136 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24139 /* If all went well, then we're done. */
24140 if (cp_parser_parse_definitely (parser))
24142 cp_decl_specifier_seq decl_specs;
24144 /* Build a trivial decl-specifier-seq. */
24145 clear_decl_specs (&decl_specs);
24146 decl_specs.type = type;
24148 /* Call grokdeclarator to figure out what type this is. */
24149 expr = grokdeclarator (NULL,
24150 &decl_specs,
24151 TYPENAME,
24152 /*initialized=*/0,
24153 /*attrlist=*/NULL);
24157 /* If the type-id production did not work out, then we must be
24158 looking at the unary-expression production. */
24159 if (!expr)
24160 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24161 /*cast_p=*/false, NULL);
24163 /* Go back to evaluating expressions. */
24164 --cp_unevaluated_operand;
24165 --c_inhibit_evaluation_warnings;
24167 /* Free the message we created. */
24168 free (tmp);
24169 /* And restore the old one. */
24170 parser->type_definition_forbidden_message = saved_message;
24171 parser->integral_constant_expression_p
24172 = saved_integral_constant_expression_p;
24173 parser->non_integral_constant_expression_p
24174 = saved_non_integral_constant_expression_p;
24176 return expr;
24179 /* If the current declaration has no declarator, return true. */
24181 static bool
24182 cp_parser_declares_only_class_p (cp_parser *parser)
24184 /* If the next token is a `;' or a `,' then there is no
24185 declarator. */
24186 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24187 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24190 /* Update the DECL_SPECS to reflect the storage class indicated by
24191 KEYWORD. */
24193 static void
24194 cp_parser_set_storage_class (cp_parser *parser,
24195 cp_decl_specifier_seq *decl_specs,
24196 enum rid keyword,
24197 cp_token *token)
24199 cp_storage_class storage_class;
24201 if (parser->in_unbraced_linkage_specification_p)
24203 error_at (token->location, "invalid use of %qD in linkage specification",
24204 ridpointers[keyword]);
24205 return;
24207 else if (decl_specs->storage_class != sc_none)
24209 decl_specs->conflicting_specifiers_p = true;
24210 return;
24213 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24214 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24215 && decl_specs->gnu_thread_keyword_p)
24217 pedwarn (decl_specs->locations[ds_thread], 0,
24218 "%<__thread%> before %qD", ridpointers[keyword]);
24221 switch (keyword)
24223 case RID_AUTO:
24224 storage_class = sc_auto;
24225 break;
24226 case RID_REGISTER:
24227 storage_class = sc_register;
24228 break;
24229 case RID_STATIC:
24230 storage_class = sc_static;
24231 break;
24232 case RID_EXTERN:
24233 storage_class = sc_extern;
24234 break;
24235 case RID_MUTABLE:
24236 storage_class = sc_mutable;
24237 break;
24238 default:
24239 gcc_unreachable ();
24241 decl_specs->storage_class = storage_class;
24242 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24244 /* A storage class specifier cannot be applied alongside a typedef
24245 specifier. If there is a typedef specifier present then set
24246 conflicting_specifiers_p which will trigger an error later
24247 on in grokdeclarator. */
24248 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24249 decl_specs->conflicting_specifiers_p = true;
24252 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24253 is true, the type is a class or enum definition. */
24255 static void
24256 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24257 tree type_spec,
24258 cp_token *token,
24259 bool type_definition_p)
24261 decl_specs->any_specifiers_p = true;
24263 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24264 (with, for example, in "typedef int wchar_t;") we remember that
24265 this is what happened. In system headers, we ignore these
24266 declarations so that G++ can work with system headers that are not
24267 C++-safe. */
24268 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24269 && !type_definition_p
24270 && (type_spec == boolean_type_node
24271 || type_spec == char16_type_node
24272 || type_spec == char32_type_node
24273 || type_spec == wchar_type_node)
24274 && (decl_specs->type
24275 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24276 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24277 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24278 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24280 decl_specs->redefined_builtin_type = type_spec;
24281 set_and_check_decl_spec_loc (decl_specs,
24282 ds_redefined_builtin_type_spec,
24283 token);
24284 if (!decl_specs->type)
24286 decl_specs->type = type_spec;
24287 decl_specs->type_definition_p = false;
24288 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24291 else if (decl_specs->type)
24292 decl_specs->multiple_types_p = true;
24293 else
24295 decl_specs->type = type_spec;
24296 decl_specs->type_definition_p = type_definition_p;
24297 decl_specs->redefined_builtin_type = NULL_TREE;
24298 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24302 /* True iff TOKEN is the GNU keyword __thread. */
24304 static bool
24305 token_is__thread (cp_token *token)
24307 gcc_assert (token->keyword == RID_THREAD);
24308 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24311 /* Set the location for a declarator specifier and check if it is
24312 duplicated.
24314 DECL_SPECS is the sequence of declarator specifiers onto which to
24315 set the location.
24317 DS is the single declarator specifier to set which location is to
24318 be set onto the existing sequence of declarators.
24320 LOCATION is the location for the declarator specifier to
24321 consider. */
24323 static void
24324 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24325 cp_decl_spec ds, cp_token *token)
24327 gcc_assert (ds < ds_last);
24329 if (decl_specs == NULL)
24330 return;
24332 source_location location = token->location;
24334 if (decl_specs->locations[ds] == 0)
24336 decl_specs->locations[ds] = location;
24337 if (ds == ds_thread)
24338 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24340 else
24342 if (ds == ds_long)
24344 if (decl_specs->locations[ds_long_long] != 0)
24345 error_at (location,
24346 "%<long long long%> is too long for GCC");
24347 else
24349 decl_specs->locations[ds_long_long] = location;
24350 pedwarn_cxx98 (location,
24351 OPT_Wlong_long,
24352 "ISO C++ 1998 does not support %<long long%>");
24355 else if (ds == ds_thread)
24357 bool gnu = token_is__thread (token);
24358 if (gnu != decl_specs->gnu_thread_keyword_p)
24359 error_at (location,
24360 "both %<__thread%> and %<thread_local%> specified");
24361 else
24362 error_at (location, "duplicate %qD", token->u.value);
24364 else
24366 static const char *const decl_spec_names[] = {
24367 "signed",
24368 "unsigned",
24369 "short",
24370 "long",
24371 "const",
24372 "volatile",
24373 "restrict",
24374 "inline",
24375 "virtual",
24376 "explicit",
24377 "friend",
24378 "typedef",
24379 "using",
24380 "constexpr",
24381 "__complex"
24383 error_at (location,
24384 "duplicate %qs", decl_spec_names[ds]);
24389 /* Return true iff the declarator specifier DS is present in the
24390 sequence of declarator specifiers DECL_SPECS. */
24392 bool
24393 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24394 cp_decl_spec ds)
24396 gcc_assert (ds < ds_last);
24398 if (decl_specs == NULL)
24399 return false;
24401 return decl_specs->locations[ds] != 0;
24404 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24405 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24407 static bool
24408 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24410 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24413 /* Issue an error message indicating that TOKEN_DESC was expected.
24414 If KEYWORD is true, it indicated this function is called by
24415 cp_parser_require_keword and the required token can only be
24416 a indicated keyword. */
24418 static void
24419 cp_parser_required_error (cp_parser *parser,
24420 required_token token_desc,
24421 bool keyword)
24423 switch (token_desc)
24425 case RT_NEW:
24426 cp_parser_error (parser, "expected %<new%>");
24427 return;
24428 case RT_DELETE:
24429 cp_parser_error (parser, "expected %<delete%>");
24430 return;
24431 case RT_RETURN:
24432 cp_parser_error (parser, "expected %<return%>");
24433 return;
24434 case RT_WHILE:
24435 cp_parser_error (parser, "expected %<while%>");
24436 return;
24437 case RT_EXTERN:
24438 cp_parser_error (parser, "expected %<extern%>");
24439 return;
24440 case RT_STATIC_ASSERT:
24441 cp_parser_error (parser, "expected %<static_assert%>");
24442 return;
24443 case RT_DECLTYPE:
24444 cp_parser_error (parser, "expected %<decltype%>");
24445 return;
24446 case RT_OPERATOR:
24447 cp_parser_error (parser, "expected %<operator%>");
24448 return;
24449 case RT_CLASS:
24450 cp_parser_error (parser, "expected %<class%>");
24451 return;
24452 case RT_TEMPLATE:
24453 cp_parser_error (parser, "expected %<template%>");
24454 return;
24455 case RT_NAMESPACE:
24456 cp_parser_error (parser, "expected %<namespace%>");
24457 return;
24458 case RT_USING:
24459 cp_parser_error (parser, "expected %<using%>");
24460 return;
24461 case RT_ASM:
24462 cp_parser_error (parser, "expected %<asm%>");
24463 return;
24464 case RT_TRY:
24465 cp_parser_error (parser, "expected %<try%>");
24466 return;
24467 case RT_CATCH:
24468 cp_parser_error (parser, "expected %<catch%>");
24469 return;
24470 case RT_THROW:
24471 cp_parser_error (parser, "expected %<throw%>");
24472 return;
24473 case RT_LABEL:
24474 cp_parser_error (parser, "expected %<__label__%>");
24475 return;
24476 case RT_AT_TRY:
24477 cp_parser_error (parser, "expected %<@try%>");
24478 return;
24479 case RT_AT_SYNCHRONIZED:
24480 cp_parser_error (parser, "expected %<@synchronized%>");
24481 return;
24482 case RT_AT_THROW:
24483 cp_parser_error (parser, "expected %<@throw%>");
24484 return;
24485 case RT_TRANSACTION_ATOMIC:
24486 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24487 return;
24488 case RT_TRANSACTION_RELAXED:
24489 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24490 return;
24491 default:
24492 break;
24494 if (!keyword)
24496 switch (token_desc)
24498 case RT_SEMICOLON:
24499 cp_parser_error (parser, "expected %<;%>");
24500 return;
24501 case RT_OPEN_PAREN:
24502 cp_parser_error (parser, "expected %<(%>");
24503 return;
24504 case RT_CLOSE_BRACE:
24505 cp_parser_error (parser, "expected %<}%>");
24506 return;
24507 case RT_OPEN_BRACE:
24508 cp_parser_error (parser, "expected %<{%>");
24509 return;
24510 case RT_CLOSE_SQUARE:
24511 cp_parser_error (parser, "expected %<]%>");
24512 return;
24513 case RT_OPEN_SQUARE:
24514 cp_parser_error (parser, "expected %<[%>");
24515 return;
24516 case RT_COMMA:
24517 cp_parser_error (parser, "expected %<,%>");
24518 return;
24519 case RT_SCOPE:
24520 cp_parser_error (parser, "expected %<::%>");
24521 return;
24522 case RT_LESS:
24523 cp_parser_error (parser, "expected %<<%>");
24524 return;
24525 case RT_GREATER:
24526 cp_parser_error (parser, "expected %<>%>");
24527 return;
24528 case RT_EQ:
24529 cp_parser_error (parser, "expected %<=%>");
24530 return;
24531 case RT_ELLIPSIS:
24532 cp_parser_error (parser, "expected %<...%>");
24533 return;
24534 case RT_MULT:
24535 cp_parser_error (parser, "expected %<*%>");
24536 return;
24537 case RT_COMPL:
24538 cp_parser_error (parser, "expected %<~%>");
24539 return;
24540 case RT_COLON:
24541 cp_parser_error (parser, "expected %<:%>");
24542 return;
24543 case RT_COLON_SCOPE:
24544 cp_parser_error (parser, "expected %<:%> or %<::%>");
24545 return;
24546 case RT_CLOSE_PAREN:
24547 cp_parser_error (parser, "expected %<)%>");
24548 return;
24549 case RT_COMMA_CLOSE_PAREN:
24550 cp_parser_error (parser, "expected %<,%> or %<)%>");
24551 return;
24552 case RT_PRAGMA_EOL:
24553 cp_parser_error (parser, "expected end of line");
24554 return;
24555 case RT_NAME:
24556 cp_parser_error (parser, "expected identifier");
24557 return;
24558 case RT_SELECT:
24559 cp_parser_error (parser, "expected selection-statement");
24560 return;
24561 case RT_INTERATION:
24562 cp_parser_error (parser, "expected iteration-statement");
24563 return;
24564 case RT_JUMP:
24565 cp_parser_error (parser, "expected jump-statement");
24566 return;
24567 case RT_CLASS_KEY:
24568 cp_parser_error (parser, "expected class-key");
24569 return;
24570 case RT_CLASS_TYPENAME_TEMPLATE:
24571 cp_parser_error (parser,
24572 "expected %<class%>, %<typename%>, or %<template%>");
24573 return;
24574 default:
24575 gcc_unreachable ();
24578 else
24579 gcc_unreachable ();
24584 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24585 issue an error message indicating that TOKEN_DESC was expected.
24587 Returns the token consumed, if the token had the appropriate type.
24588 Otherwise, returns NULL. */
24590 static cp_token *
24591 cp_parser_require (cp_parser* parser,
24592 enum cpp_ttype type,
24593 required_token token_desc)
24595 if (cp_lexer_next_token_is (parser->lexer, type))
24596 return cp_lexer_consume_token (parser->lexer);
24597 else
24599 /* Output the MESSAGE -- unless we're parsing tentatively. */
24600 if (!cp_parser_simulate_error (parser))
24601 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24602 return NULL;
24606 /* An error message is produced if the next token is not '>'.
24607 All further tokens are skipped until the desired token is
24608 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24610 static void
24611 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24613 /* Current level of '< ... >'. */
24614 unsigned level = 0;
24615 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24616 unsigned nesting_depth = 0;
24618 /* Are we ready, yet? If not, issue error message. */
24619 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24620 return;
24622 /* Skip tokens until the desired token is found. */
24623 while (true)
24625 /* Peek at the next token. */
24626 switch (cp_lexer_peek_token (parser->lexer)->type)
24628 case CPP_LESS:
24629 if (!nesting_depth)
24630 ++level;
24631 break;
24633 case CPP_RSHIFT:
24634 if (cxx_dialect == cxx98)
24635 /* C++0x views the `>>' operator as two `>' tokens, but
24636 C++98 does not. */
24637 break;
24638 else if (!nesting_depth && level-- == 0)
24640 /* We've hit a `>>' where the first `>' closes the
24641 template argument list, and the second `>' is
24642 spurious. Just consume the `>>' and stop; we've
24643 already produced at least one error. */
24644 cp_lexer_consume_token (parser->lexer);
24645 return;
24647 /* Fall through for C++0x, so we handle the second `>' in
24648 the `>>'. */
24650 case CPP_GREATER:
24651 if (!nesting_depth && level-- == 0)
24653 /* We've reached the token we want, consume it and stop. */
24654 cp_lexer_consume_token (parser->lexer);
24655 return;
24657 break;
24659 case CPP_OPEN_PAREN:
24660 case CPP_OPEN_SQUARE:
24661 ++nesting_depth;
24662 break;
24664 case CPP_CLOSE_PAREN:
24665 case CPP_CLOSE_SQUARE:
24666 if (nesting_depth-- == 0)
24667 return;
24668 break;
24670 case CPP_EOF:
24671 case CPP_PRAGMA_EOL:
24672 case CPP_SEMICOLON:
24673 case CPP_OPEN_BRACE:
24674 case CPP_CLOSE_BRACE:
24675 /* The '>' was probably forgotten, don't look further. */
24676 return;
24678 default:
24679 break;
24682 /* Consume this token. */
24683 cp_lexer_consume_token (parser->lexer);
24687 /* If the next token is the indicated keyword, consume it. Otherwise,
24688 issue an error message indicating that TOKEN_DESC was expected.
24690 Returns the token consumed, if the token had the appropriate type.
24691 Otherwise, returns NULL. */
24693 static cp_token *
24694 cp_parser_require_keyword (cp_parser* parser,
24695 enum rid keyword,
24696 required_token token_desc)
24698 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24700 if (token && token->keyword != keyword)
24702 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24703 return NULL;
24706 return token;
24709 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24710 function-definition. */
24712 static bool
24713 cp_parser_token_starts_function_definition_p (cp_token* token)
24715 return (/* An ordinary function-body begins with an `{'. */
24716 token->type == CPP_OPEN_BRACE
24717 /* A ctor-initializer begins with a `:'. */
24718 || token->type == CPP_COLON
24719 /* A function-try-block begins with `try'. */
24720 || token->keyword == RID_TRY
24721 /* A function-transaction-block begins with `__transaction_atomic'
24722 or `__transaction_relaxed'. */
24723 || token->keyword == RID_TRANSACTION_ATOMIC
24724 || token->keyword == RID_TRANSACTION_RELAXED
24725 /* The named return value extension begins with `return'. */
24726 || token->keyword == RID_RETURN);
24729 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24730 definition. */
24732 static bool
24733 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24735 cp_token *token;
24737 token = cp_lexer_peek_token (parser->lexer);
24738 return (token->type == CPP_OPEN_BRACE
24739 || (token->type == CPP_COLON
24740 && !parser->colon_doesnt_start_class_def_p));
24743 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24744 C++0x) ending a template-argument. */
24746 static bool
24747 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24749 cp_token *token;
24751 token = cp_lexer_peek_token (parser->lexer);
24752 return (token->type == CPP_COMMA
24753 || token->type == CPP_GREATER
24754 || token->type == CPP_ELLIPSIS
24755 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24758 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24759 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24761 static bool
24762 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24763 size_t n)
24765 cp_token *token;
24767 token = cp_lexer_peek_nth_token (parser->lexer, n);
24768 if (token->type == CPP_LESS)
24769 return true;
24770 /* Check for the sequence `<::' in the original code. It would be lexed as
24771 `[:', where `[' is a digraph, and there is no whitespace before
24772 `:'. */
24773 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24775 cp_token *token2;
24776 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24777 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24778 return true;
24780 return false;
24783 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24784 or none_type otherwise. */
24786 static enum tag_types
24787 cp_parser_token_is_class_key (cp_token* token)
24789 switch (token->keyword)
24791 case RID_CLASS:
24792 return class_type;
24793 case RID_STRUCT:
24794 return record_type;
24795 case RID_UNION:
24796 return union_type;
24798 default:
24799 return none_type;
24803 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
24804 or none_type otherwise or if the token is null. */
24806 static enum tag_types
24807 cp_parser_token_is_type_parameter_key (cp_token* token)
24809 if (!token)
24810 return none_type;
24812 switch (token->keyword)
24814 case RID_CLASS:
24815 return class_type;
24816 case RID_TYPENAME:
24817 return typename_type;
24819 default:
24820 return none_type;
24824 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24826 static void
24827 cp_parser_check_class_key (enum tag_types class_key, tree type)
24829 if (type == error_mark_node)
24830 return;
24831 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24833 if (permerror (input_location, "%qs tag used in naming %q#T",
24834 class_key == union_type ? "union"
24835 : class_key == record_type ? "struct" : "class",
24836 type))
24837 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24838 "%q#T was previously declared here", type);
24842 /* Issue an error message if DECL is redeclared with different
24843 access than its original declaration [class.access.spec/3].
24844 This applies to nested classes and nested class templates.
24845 [class.mem/1]. */
24847 static void
24848 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24850 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24851 return;
24853 if ((TREE_PRIVATE (decl)
24854 != (current_access_specifier == access_private_node))
24855 || (TREE_PROTECTED (decl)
24856 != (current_access_specifier == access_protected_node)))
24857 error_at (location, "%qD redeclared with different access", decl);
24860 /* Look for the `template' keyword, as a syntactic disambiguator.
24861 Return TRUE iff it is present, in which case it will be
24862 consumed. */
24864 static bool
24865 cp_parser_optional_template_keyword (cp_parser *parser)
24867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24869 /* In C++98 the `template' keyword can only be used within templates;
24870 outside templates the parser can always figure out what is a
24871 template and what is not. In C++11, per the resolution of DR 468,
24872 `template' is allowed in cases where it is not strictly necessary. */
24873 if (!processing_template_decl
24874 && pedantic && cxx_dialect == cxx98)
24876 cp_token *token = cp_lexer_peek_token (parser->lexer);
24877 pedwarn (token->location, OPT_Wpedantic,
24878 "in C++98 %<template%> (as a disambiguator) is only "
24879 "allowed within templates");
24880 /* If this part of the token stream is rescanned, the same
24881 error message would be generated. So, we purge the token
24882 from the stream. */
24883 cp_lexer_purge_token (parser->lexer);
24884 return false;
24886 else
24888 /* Consume the `template' keyword. */
24889 cp_lexer_consume_token (parser->lexer);
24890 return true;
24893 return false;
24896 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24897 set PARSER->SCOPE, and perform other related actions. */
24899 static void
24900 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24902 int i;
24903 struct tree_check *check_value;
24904 deferred_access_check *chk;
24905 vec<deferred_access_check, va_gc> *checks;
24907 /* Get the stored value. */
24908 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24909 /* Perform any access checks that were deferred. */
24910 checks = check_value->checks;
24911 if (checks)
24913 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24914 perform_or_defer_access_check (chk->binfo,
24915 chk->decl,
24916 chk->diag_decl, tf_warning_or_error);
24918 /* Set the scope from the stored value. */
24919 parser->scope = check_value->value;
24920 parser->qualifying_scope = check_value->qualifying_scope;
24921 parser->object_scope = NULL_TREE;
24924 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24925 encounter the end of a block before what we were looking for. */
24927 static bool
24928 cp_parser_cache_group (cp_parser *parser,
24929 enum cpp_ttype end,
24930 unsigned depth)
24932 while (true)
24934 cp_token *token = cp_lexer_peek_token (parser->lexer);
24936 /* Abort a parenthesized expression if we encounter a semicolon. */
24937 if ((end == CPP_CLOSE_PAREN || depth == 0)
24938 && token->type == CPP_SEMICOLON)
24939 return true;
24940 /* If we've reached the end of the file, stop. */
24941 if (token->type == CPP_EOF
24942 || (end != CPP_PRAGMA_EOL
24943 && token->type == CPP_PRAGMA_EOL))
24944 return true;
24945 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24946 /* We've hit the end of an enclosing block, so there's been some
24947 kind of syntax error. */
24948 return true;
24950 /* Consume the token. */
24951 cp_lexer_consume_token (parser->lexer);
24952 /* See if it starts a new group. */
24953 if (token->type == CPP_OPEN_BRACE)
24955 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24956 /* In theory this should probably check end == '}', but
24957 cp_parser_save_member_function_body needs it to exit
24958 after either '}' or ')' when called with ')'. */
24959 if (depth == 0)
24960 return false;
24962 else if (token->type == CPP_OPEN_PAREN)
24964 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24965 if (depth == 0 && end == CPP_CLOSE_PAREN)
24966 return false;
24968 else if (token->type == CPP_PRAGMA)
24969 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24970 else if (token->type == end)
24971 return false;
24975 /* Like above, for caching a default argument or NSDMI. Both of these are
24976 terminated by a non-nested comma, but it can be unclear whether or not a
24977 comma is nested in a template argument list unless we do more parsing.
24978 In order to handle this ambiguity, when we encounter a ',' after a '<'
24979 we try to parse what follows as a parameter-declaration-list (in the
24980 case of a default argument) or a member-declarator (in the case of an
24981 NSDMI). If that succeeds, then we stop caching. */
24983 static tree
24984 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24986 unsigned depth = 0;
24987 int maybe_template_id = 0;
24988 cp_token *first_token;
24989 cp_token *token;
24990 tree default_argument;
24992 /* Add tokens until we have processed the entire default
24993 argument. We add the range [first_token, token). */
24994 first_token = cp_lexer_peek_token (parser->lexer);
24995 if (first_token->type == CPP_OPEN_BRACE)
24997 /* For list-initialization, this is straightforward. */
24998 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24999 token = cp_lexer_peek_token (parser->lexer);
25001 else while (true)
25003 bool done = false;
25005 /* Peek at the next token. */
25006 token = cp_lexer_peek_token (parser->lexer);
25007 /* What we do depends on what token we have. */
25008 switch (token->type)
25010 /* In valid code, a default argument must be
25011 immediately followed by a `,' `)', or `...'. */
25012 case CPP_COMMA:
25013 if (depth == 0 && maybe_template_id)
25015 /* If we've seen a '<', we might be in a
25016 template-argument-list. Until Core issue 325 is
25017 resolved, we don't know how this situation ought
25018 to be handled, so try to DTRT. We check whether
25019 what comes after the comma is a valid parameter
25020 declaration list. If it is, then the comma ends
25021 the default argument; otherwise the default
25022 argument continues. */
25023 bool error = false;
25025 /* Set ITALP so cp_parser_parameter_declaration_list
25026 doesn't decide to commit to this parse. */
25027 bool saved_italp = parser->in_template_argument_list_p;
25028 parser->in_template_argument_list_p = true;
25030 cp_parser_parse_tentatively (parser);
25031 cp_lexer_consume_token (parser->lexer);
25033 if (nsdmi)
25035 int ctor_dtor_or_conv_p;
25036 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25037 &ctor_dtor_or_conv_p,
25038 /*parenthesized_p=*/NULL,
25039 /*member_p=*/true,
25040 /*friend_p=*/false);
25042 else
25044 begin_scope (sk_function_parms, NULL_TREE);
25045 cp_parser_parameter_declaration_list (parser, &error);
25046 pop_bindings_and_leave_scope ();
25048 if (!cp_parser_error_occurred (parser) && !error)
25049 done = true;
25050 cp_parser_abort_tentative_parse (parser);
25052 parser->in_template_argument_list_p = saved_italp;
25053 break;
25055 case CPP_CLOSE_PAREN:
25056 case CPP_ELLIPSIS:
25057 /* If we run into a non-nested `;', `}', or `]',
25058 then the code is invalid -- but the default
25059 argument is certainly over. */
25060 case CPP_SEMICOLON:
25061 case CPP_CLOSE_BRACE:
25062 case CPP_CLOSE_SQUARE:
25063 if (depth == 0
25064 /* Handle correctly int n = sizeof ... ( p ); */
25065 && token->type != CPP_ELLIPSIS)
25066 done = true;
25067 /* Update DEPTH, if necessary. */
25068 else if (token->type == CPP_CLOSE_PAREN
25069 || token->type == CPP_CLOSE_BRACE
25070 || token->type == CPP_CLOSE_SQUARE)
25071 --depth;
25072 break;
25074 case CPP_OPEN_PAREN:
25075 case CPP_OPEN_SQUARE:
25076 case CPP_OPEN_BRACE:
25077 ++depth;
25078 break;
25080 case CPP_LESS:
25081 if (depth == 0)
25082 /* This might be the comparison operator, or it might
25083 start a template argument list. */
25084 ++maybe_template_id;
25085 break;
25087 case CPP_RSHIFT:
25088 if (cxx_dialect == cxx98)
25089 break;
25090 /* Fall through for C++0x, which treats the `>>'
25091 operator like two `>' tokens in certain
25092 cases. */
25094 case CPP_GREATER:
25095 if (depth == 0)
25097 /* This might be an operator, or it might close a
25098 template argument list. But if a previous '<'
25099 started a template argument list, this will have
25100 closed it, so we can't be in one anymore. */
25101 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25102 if (maybe_template_id < 0)
25103 maybe_template_id = 0;
25105 break;
25107 /* If we run out of tokens, issue an error message. */
25108 case CPP_EOF:
25109 case CPP_PRAGMA_EOL:
25110 error_at (token->location, "file ends in default argument");
25111 done = true;
25112 break;
25114 case CPP_NAME:
25115 case CPP_SCOPE:
25116 /* In these cases, we should look for template-ids.
25117 For example, if the default argument is
25118 `X<int, double>()', we need to do name lookup to
25119 figure out whether or not `X' is a template; if
25120 so, the `,' does not end the default argument.
25122 That is not yet done. */
25123 break;
25125 default:
25126 break;
25129 /* If we've reached the end, stop. */
25130 if (done)
25131 break;
25133 /* Add the token to the token block. */
25134 token = cp_lexer_consume_token (parser->lexer);
25137 /* Create a DEFAULT_ARG to represent the unparsed default
25138 argument. */
25139 default_argument = make_node (DEFAULT_ARG);
25140 DEFARG_TOKENS (default_argument)
25141 = cp_token_cache_new (first_token, token);
25142 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25144 return default_argument;
25147 /* Begin parsing tentatively. We always save tokens while parsing
25148 tentatively so that if the tentative parsing fails we can restore the
25149 tokens. */
25151 static void
25152 cp_parser_parse_tentatively (cp_parser* parser)
25154 /* Enter a new parsing context. */
25155 parser->context = cp_parser_context_new (parser->context);
25156 /* Begin saving tokens. */
25157 cp_lexer_save_tokens (parser->lexer);
25158 /* In order to avoid repetitive access control error messages,
25159 access checks are queued up until we are no longer parsing
25160 tentatively. */
25161 push_deferring_access_checks (dk_deferred);
25164 /* Commit to the currently active tentative parse. */
25166 static void
25167 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25169 cp_parser_context *context;
25170 cp_lexer *lexer;
25172 /* Mark all of the levels as committed. */
25173 lexer = parser->lexer;
25174 for (context = parser->context; context->next; context = context->next)
25176 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25177 break;
25178 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25179 while (!cp_lexer_saving_tokens (lexer))
25180 lexer = lexer->next;
25181 cp_lexer_commit_tokens (lexer);
25185 /* Commit to the topmost currently active tentative parse.
25187 Note that this function shouldn't be called when there are
25188 irreversible side-effects while in a tentative state. For
25189 example, we shouldn't create a permanent entry in the symbol
25190 table, or issue an error message that might not apply if the
25191 tentative parse is aborted. */
25193 static void
25194 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25196 cp_parser_context *context = parser->context;
25197 cp_lexer *lexer = parser->lexer;
25199 if (context)
25201 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25202 return;
25203 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25205 while (!cp_lexer_saving_tokens (lexer))
25206 lexer = lexer->next;
25207 cp_lexer_commit_tokens (lexer);
25211 /* Abort the currently active tentative parse. All consumed tokens
25212 will be rolled back, and no diagnostics will be issued. */
25214 static void
25215 cp_parser_abort_tentative_parse (cp_parser* parser)
25217 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25218 || errorcount > 0);
25219 cp_parser_simulate_error (parser);
25220 /* Now, pretend that we want to see if the construct was
25221 successfully parsed. */
25222 cp_parser_parse_definitely (parser);
25225 /* Stop parsing tentatively. If a parse error has occurred, restore the
25226 token stream. Otherwise, commit to the tokens we have consumed.
25227 Returns true if no error occurred; false otherwise. */
25229 static bool
25230 cp_parser_parse_definitely (cp_parser* parser)
25232 bool error_occurred;
25233 cp_parser_context *context;
25235 /* Remember whether or not an error occurred, since we are about to
25236 destroy that information. */
25237 error_occurred = cp_parser_error_occurred (parser);
25238 /* Remove the topmost context from the stack. */
25239 context = parser->context;
25240 parser->context = context->next;
25241 /* If no parse errors occurred, commit to the tentative parse. */
25242 if (!error_occurred)
25244 /* Commit to the tokens read tentatively, unless that was
25245 already done. */
25246 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25247 cp_lexer_commit_tokens (parser->lexer);
25249 pop_to_parent_deferring_access_checks ();
25251 /* Otherwise, if errors occurred, roll back our state so that things
25252 are just as they were before we began the tentative parse. */
25253 else
25255 cp_lexer_rollback_tokens (parser->lexer);
25256 pop_deferring_access_checks ();
25258 /* Add the context to the front of the free list. */
25259 context->next = cp_parser_context_free_list;
25260 cp_parser_context_free_list = context;
25262 return !error_occurred;
25265 /* Returns true if we are parsing tentatively and are not committed to
25266 this tentative parse. */
25268 static bool
25269 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25271 return (cp_parser_parsing_tentatively (parser)
25272 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25275 /* Returns nonzero iff an error has occurred during the most recent
25276 tentative parse. */
25278 static bool
25279 cp_parser_error_occurred (cp_parser* parser)
25281 return (cp_parser_parsing_tentatively (parser)
25282 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25285 /* Returns nonzero if GNU extensions are allowed. */
25287 static bool
25288 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25290 return parser->allow_gnu_extensions_p;
25293 /* Objective-C++ Productions */
25296 /* Parse an Objective-C expression, which feeds into a primary-expression
25297 above.
25299 objc-expression:
25300 objc-message-expression
25301 objc-string-literal
25302 objc-encode-expression
25303 objc-protocol-expression
25304 objc-selector-expression
25306 Returns a tree representation of the expression. */
25308 static tree
25309 cp_parser_objc_expression (cp_parser* parser)
25311 /* Try to figure out what kind of declaration is present. */
25312 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25314 switch (kwd->type)
25316 case CPP_OPEN_SQUARE:
25317 return cp_parser_objc_message_expression (parser);
25319 case CPP_OBJC_STRING:
25320 kwd = cp_lexer_consume_token (parser->lexer);
25321 return objc_build_string_object (kwd->u.value);
25323 case CPP_KEYWORD:
25324 switch (kwd->keyword)
25326 case RID_AT_ENCODE:
25327 return cp_parser_objc_encode_expression (parser);
25329 case RID_AT_PROTOCOL:
25330 return cp_parser_objc_protocol_expression (parser);
25332 case RID_AT_SELECTOR:
25333 return cp_parser_objc_selector_expression (parser);
25335 default:
25336 break;
25338 default:
25339 error_at (kwd->location,
25340 "misplaced %<@%D%> Objective-C++ construct",
25341 kwd->u.value);
25342 cp_parser_skip_to_end_of_block_or_statement (parser);
25345 return error_mark_node;
25348 /* Parse an Objective-C message expression.
25350 objc-message-expression:
25351 [ objc-message-receiver objc-message-args ]
25353 Returns a representation of an Objective-C message. */
25355 static tree
25356 cp_parser_objc_message_expression (cp_parser* parser)
25358 tree receiver, messageargs;
25360 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25361 receiver = cp_parser_objc_message_receiver (parser);
25362 messageargs = cp_parser_objc_message_args (parser);
25363 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25365 return objc_build_message_expr (receiver, messageargs);
25368 /* Parse an objc-message-receiver.
25370 objc-message-receiver:
25371 expression
25372 simple-type-specifier
25374 Returns a representation of the type or expression. */
25376 static tree
25377 cp_parser_objc_message_receiver (cp_parser* parser)
25379 tree rcv;
25381 /* An Objective-C message receiver may be either (1) a type
25382 or (2) an expression. */
25383 cp_parser_parse_tentatively (parser);
25384 rcv = cp_parser_expression (parser);
25386 if (cp_parser_parse_definitely (parser))
25387 return rcv;
25389 rcv = cp_parser_simple_type_specifier (parser,
25390 /*decl_specs=*/NULL,
25391 CP_PARSER_FLAGS_NONE);
25393 return objc_get_class_reference (rcv);
25396 /* Parse the arguments and selectors comprising an Objective-C message.
25398 objc-message-args:
25399 objc-selector
25400 objc-selector-args
25401 objc-selector-args , objc-comma-args
25403 objc-selector-args:
25404 objc-selector [opt] : assignment-expression
25405 objc-selector-args objc-selector [opt] : assignment-expression
25407 objc-comma-args:
25408 assignment-expression
25409 objc-comma-args , assignment-expression
25411 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25412 selector arguments and TREE_VALUE containing a list of comma
25413 arguments. */
25415 static tree
25416 cp_parser_objc_message_args (cp_parser* parser)
25418 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25419 bool maybe_unary_selector_p = true;
25420 cp_token *token = cp_lexer_peek_token (parser->lexer);
25422 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25424 tree selector = NULL_TREE, arg;
25426 if (token->type != CPP_COLON)
25427 selector = cp_parser_objc_selector (parser);
25429 /* Detect if we have a unary selector. */
25430 if (maybe_unary_selector_p
25431 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25432 return build_tree_list (selector, NULL_TREE);
25434 maybe_unary_selector_p = false;
25435 cp_parser_require (parser, CPP_COLON, RT_COLON);
25436 arg = cp_parser_assignment_expression (parser, false, NULL);
25438 sel_args
25439 = chainon (sel_args,
25440 build_tree_list (selector, arg));
25442 token = cp_lexer_peek_token (parser->lexer);
25445 /* Handle non-selector arguments, if any. */
25446 while (token->type == CPP_COMMA)
25448 tree arg;
25450 cp_lexer_consume_token (parser->lexer);
25451 arg = cp_parser_assignment_expression (parser, false, NULL);
25453 addl_args
25454 = chainon (addl_args,
25455 build_tree_list (NULL_TREE, arg));
25457 token = cp_lexer_peek_token (parser->lexer);
25460 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25462 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25463 return build_tree_list (error_mark_node, error_mark_node);
25466 return build_tree_list (sel_args, addl_args);
25469 /* Parse an Objective-C encode expression.
25471 objc-encode-expression:
25472 @encode objc-typename
25474 Returns an encoded representation of the type argument. */
25476 static tree
25477 cp_parser_objc_encode_expression (cp_parser* parser)
25479 tree type;
25480 cp_token *token;
25482 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25483 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25484 token = cp_lexer_peek_token (parser->lexer);
25485 type = complete_type (cp_parser_type_id (parser));
25486 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25488 if (!type)
25490 error_at (token->location,
25491 "%<@encode%> must specify a type as an argument");
25492 return error_mark_node;
25495 /* This happens if we find @encode(T) (where T is a template
25496 typename or something dependent on a template typename) when
25497 parsing a template. In that case, we can't compile it
25498 immediately, but we rather create an AT_ENCODE_EXPR which will
25499 need to be instantiated when the template is used.
25501 if (dependent_type_p (type))
25503 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25504 TREE_READONLY (value) = 1;
25505 return value;
25508 return objc_build_encode_expr (type);
25511 /* Parse an Objective-C @defs expression. */
25513 static tree
25514 cp_parser_objc_defs_expression (cp_parser *parser)
25516 tree name;
25518 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25519 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25520 name = cp_parser_identifier (parser);
25521 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25523 return objc_get_class_ivars (name);
25526 /* Parse an Objective-C protocol expression.
25528 objc-protocol-expression:
25529 @protocol ( identifier )
25531 Returns a representation of the protocol expression. */
25533 static tree
25534 cp_parser_objc_protocol_expression (cp_parser* parser)
25536 tree proto;
25538 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25539 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25540 proto = cp_parser_identifier (parser);
25541 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25543 return objc_build_protocol_expr (proto);
25546 /* Parse an Objective-C selector expression.
25548 objc-selector-expression:
25549 @selector ( objc-method-signature )
25551 objc-method-signature:
25552 objc-selector
25553 objc-selector-seq
25555 objc-selector-seq:
25556 objc-selector :
25557 objc-selector-seq objc-selector :
25559 Returns a representation of the method selector. */
25561 static tree
25562 cp_parser_objc_selector_expression (cp_parser* parser)
25564 tree sel_seq = NULL_TREE;
25565 bool maybe_unary_selector_p = true;
25566 cp_token *token;
25567 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25569 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25570 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25571 token = cp_lexer_peek_token (parser->lexer);
25573 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25574 || token->type == CPP_SCOPE)
25576 tree selector = NULL_TREE;
25578 if (token->type != CPP_COLON
25579 || token->type == CPP_SCOPE)
25580 selector = cp_parser_objc_selector (parser);
25582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25583 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25585 /* Detect if we have a unary selector. */
25586 if (maybe_unary_selector_p)
25588 sel_seq = selector;
25589 goto finish_selector;
25591 else
25593 cp_parser_error (parser, "expected %<:%>");
25596 maybe_unary_selector_p = false;
25597 token = cp_lexer_consume_token (parser->lexer);
25599 if (token->type == CPP_SCOPE)
25601 sel_seq
25602 = chainon (sel_seq,
25603 build_tree_list (selector, NULL_TREE));
25604 sel_seq
25605 = chainon (sel_seq,
25606 build_tree_list (NULL_TREE, NULL_TREE));
25608 else
25609 sel_seq
25610 = chainon (sel_seq,
25611 build_tree_list (selector, NULL_TREE));
25613 token = cp_lexer_peek_token (parser->lexer);
25616 finish_selector:
25617 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25619 return objc_build_selector_expr (loc, sel_seq);
25622 /* Parse a list of identifiers.
25624 objc-identifier-list:
25625 identifier
25626 objc-identifier-list , identifier
25628 Returns a TREE_LIST of identifier nodes. */
25630 static tree
25631 cp_parser_objc_identifier_list (cp_parser* parser)
25633 tree identifier;
25634 tree list;
25635 cp_token *sep;
25637 identifier = cp_parser_identifier (parser);
25638 if (identifier == error_mark_node)
25639 return error_mark_node;
25641 list = build_tree_list (NULL_TREE, identifier);
25642 sep = cp_lexer_peek_token (parser->lexer);
25644 while (sep->type == CPP_COMMA)
25646 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25647 identifier = cp_parser_identifier (parser);
25648 if (identifier == error_mark_node)
25649 return list;
25651 list = chainon (list, build_tree_list (NULL_TREE,
25652 identifier));
25653 sep = cp_lexer_peek_token (parser->lexer);
25656 return list;
25659 /* Parse an Objective-C alias declaration.
25661 objc-alias-declaration:
25662 @compatibility_alias identifier identifier ;
25664 This function registers the alias mapping with the Objective-C front end.
25665 It returns nothing. */
25667 static void
25668 cp_parser_objc_alias_declaration (cp_parser* parser)
25670 tree alias, orig;
25672 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25673 alias = cp_parser_identifier (parser);
25674 orig = cp_parser_identifier (parser);
25675 objc_declare_alias (alias, orig);
25676 cp_parser_consume_semicolon_at_end_of_statement (parser);
25679 /* Parse an Objective-C class forward-declaration.
25681 objc-class-declaration:
25682 @class objc-identifier-list ;
25684 The function registers the forward declarations with the Objective-C
25685 front end. It returns nothing. */
25687 static void
25688 cp_parser_objc_class_declaration (cp_parser* parser)
25690 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25691 while (true)
25693 tree id;
25695 id = cp_parser_identifier (parser);
25696 if (id == error_mark_node)
25697 break;
25699 objc_declare_class (id);
25701 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25702 cp_lexer_consume_token (parser->lexer);
25703 else
25704 break;
25706 cp_parser_consume_semicolon_at_end_of_statement (parser);
25709 /* Parse a list of Objective-C protocol references.
25711 objc-protocol-refs-opt:
25712 objc-protocol-refs [opt]
25714 objc-protocol-refs:
25715 < objc-identifier-list >
25717 Returns a TREE_LIST of identifiers, if any. */
25719 static tree
25720 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25722 tree protorefs = NULL_TREE;
25724 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25726 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25727 protorefs = cp_parser_objc_identifier_list (parser);
25728 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25731 return protorefs;
25734 /* Parse a Objective-C visibility specification. */
25736 static void
25737 cp_parser_objc_visibility_spec (cp_parser* parser)
25739 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25741 switch (vis->keyword)
25743 case RID_AT_PRIVATE:
25744 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25745 break;
25746 case RID_AT_PROTECTED:
25747 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25748 break;
25749 case RID_AT_PUBLIC:
25750 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25751 break;
25752 case RID_AT_PACKAGE:
25753 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25754 break;
25755 default:
25756 return;
25759 /* Eat '@private'/'@protected'/'@public'. */
25760 cp_lexer_consume_token (parser->lexer);
25763 /* Parse an Objective-C method type. Return 'true' if it is a class
25764 (+) method, and 'false' if it is an instance (-) method. */
25766 static inline bool
25767 cp_parser_objc_method_type (cp_parser* parser)
25769 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25770 return true;
25771 else
25772 return false;
25775 /* Parse an Objective-C protocol qualifier. */
25777 static tree
25778 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25780 tree quals = NULL_TREE, node;
25781 cp_token *token = cp_lexer_peek_token (parser->lexer);
25783 node = token->u.value;
25785 while (node && identifier_p (node)
25786 && (node == ridpointers [(int) RID_IN]
25787 || node == ridpointers [(int) RID_OUT]
25788 || node == ridpointers [(int) RID_INOUT]
25789 || node == ridpointers [(int) RID_BYCOPY]
25790 || node == ridpointers [(int) RID_BYREF]
25791 || node == ridpointers [(int) RID_ONEWAY]))
25793 quals = tree_cons (NULL_TREE, node, quals);
25794 cp_lexer_consume_token (parser->lexer);
25795 token = cp_lexer_peek_token (parser->lexer);
25796 node = token->u.value;
25799 return quals;
25802 /* Parse an Objective-C typename. */
25804 static tree
25805 cp_parser_objc_typename (cp_parser* parser)
25807 tree type_name = NULL_TREE;
25809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25811 tree proto_quals, cp_type = NULL_TREE;
25813 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25814 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25816 /* An ObjC type name may consist of just protocol qualifiers, in which
25817 case the type shall default to 'id'. */
25818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25820 cp_type = cp_parser_type_id (parser);
25822 /* If the type could not be parsed, an error has already
25823 been produced. For error recovery, behave as if it had
25824 not been specified, which will use the default type
25825 'id'. */
25826 if (cp_type == error_mark_node)
25828 cp_type = NULL_TREE;
25829 /* We need to skip to the closing parenthesis as
25830 cp_parser_type_id() does not seem to do it for
25831 us. */
25832 cp_parser_skip_to_closing_parenthesis (parser,
25833 /*recovering=*/true,
25834 /*or_comma=*/false,
25835 /*consume_paren=*/false);
25839 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25840 type_name = build_tree_list (proto_quals, cp_type);
25843 return type_name;
25846 /* Check to see if TYPE refers to an Objective-C selector name. */
25848 static bool
25849 cp_parser_objc_selector_p (enum cpp_ttype type)
25851 return (type == CPP_NAME || type == CPP_KEYWORD
25852 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25853 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25854 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25855 || type == CPP_XOR || type == CPP_XOR_EQ);
25858 /* Parse an Objective-C selector. */
25860 static tree
25861 cp_parser_objc_selector (cp_parser* parser)
25863 cp_token *token = cp_lexer_consume_token (parser->lexer);
25865 if (!cp_parser_objc_selector_p (token->type))
25867 error_at (token->location, "invalid Objective-C++ selector name");
25868 return error_mark_node;
25871 /* C++ operator names are allowed to appear in ObjC selectors. */
25872 switch (token->type)
25874 case CPP_AND_AND: return get_identifier ("and");
25875 case CPP_AND_EQ: return get_identifier ("and_eq");
25876 case CPP_AND: return get_identifier ("bitand");
25877 case CPP_OR: return get_identifier ("bitor");
25878 case CPP_COMPL: return get_identifier ("compl");
25879 case CPP_NOT: return get_identifier ("not");
25880 case CPP_NOT_EQ: return get_identifier ("not_eq");
25881 case CPP_OR_OR: return get_identifier ("or");
25882 case CPP_OR_EQ: return get_identifier ("or_eq");
25883 case CPP_XOR: return get_identifier ("xor");
25884 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25885 default: return token->u.value;
25889 /* Parse an Objective-C params list. */
25891 static tree
25892 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25894 tree params = NULL_TREE;
25895 bool maybe_unary_selector_p = true;
25896 cp_token *token = cp_lexer_peek_token (parser->lexer);
25898 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25900 tree selector = NULL_TREE, type_name, identifier;
25901 tree parm_attr = NULL_TREE;
25903 if (token->keyword == RID_ATTRIBUTE)
25904 break;
25906 if (token->type != CPP_COLON)
25907 selector = cp_parser_objc_selector (parser);
25909 /* Detect if we have a unary selector. */
25910 if (maybe_unary_selector_p
25911 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25913 params = selector; /* Might be followed by attributes. */
25914 break;
25917 maybe_unary_selector_p = false;
25918 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25920 /* Something went quite wrong. There should be a colon
25921 here, but there is not. Stop parsing parameters. */
25922 break;
25924 type_name = cp_parser_objc_typename (parser);
25925 /* New ObjC allows attributes on parameters too. */
25926 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25927 parm_attr = cp_parser_attributes_opt (parser);
25928 identifier = cp_parser_identifier (parser);
25930 params
25931 = chainon (params,
25932 objc_build_keyword_decl (selector,
25933 type_name,
25934 identifier,
25935 parm_attr));
25937 token = cp_lexer_peek_token (parser->lexer);
25940 if (params == NULL_TREE)
25942 cp_parser_error (parser, "objective-c++ method declaration is expected");
25943 return error_mark_node;
25946 /* We allow tail attributes for the method. */
25947 if (token->keyword == RID_ATTRIBUTE)
25949 *attributes = cp_parser_attributes_opt (parser);
25950 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25951 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25952 return params;
25953 cp_parser_error (parser,
25954 "method attributes must be specified at the end");
25955 return error_mark_node;
25958 if (params == NULL_TREE)
25960 cp_parser_error (parser, "objective-c++ method declaration is expected");
25961 return error_mark_node;
25963 return params;
25966 /* Parse the non-keyword Objective-C params. */
25968 static tree
25969 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25970 tree* attributes)
25972 tree params = make_node (TREE_LIST);
25973 cp_token *token = cp_lexer_peek_token (parser->lexer);
25974 *ellipsisp = false; /* Initially, assume no ellipsis. */
25976 while (token->type == CPP_COMMA)
25978 cp_parameter_declarator *parmdecl;
25979 tree parm;
25981 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25982 token = cp_lexer_peek_token (parser->lexer);
25984 if (token->type == CPP_ELLIPSIS)
25986 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25987 *ellipsisp = true;
25988 token = cp_lexer_peek_token (parser->lexer);
25989 break;
25992 /* TODO: parse attributes for tail parameters. */
25993 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25994 parm = grokdeclarator (parmdecl->declarator,
25995 &parmdecl->decl_specifiers,
25996 PARM, /*initialized=*/0,
25997 /*attrlist=*/NULL);
25999 chainon (params, build_tree_list (NULL_TREE, parm));
26000 token = cp_lexer_peek_token (parser->lexer);
26003 /* We allow tail attributes for the method. */
26004 if (token->keyword == RID_ATTRIBUTE)
26006 if (*attributes == NULL_TREE)
26008 *attributes = cp_parser_attributes_opt (parser);
26009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26010 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26011 return params;
26013 else
26014 /* We have an error, but parse the attributes, so that we can
26015 carry on. */
26016 *attributes = cp_parser_attributes_opt (parser);
26018 cp_parser_error (parser,
26019 "method attributes must be specified at the end");
26020 return error_mark_node;
26023 return params;
26026 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26028 static void
26029 cp_parser_objc_interstitial_code (cp_parser* parser)
26031 cp_token *token = cp_lexer_peek_token (parser->lexer);
26033 /* If the next token is `extern' and the following token is a string
26034 literal, then we have a linkage specification. */
26035 if (token->keyword == RID_EXTERN
26036 && cp_parser_is_pure_string_literal
26037 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26038 cp_parser_linkage_specification (parser);
26039 /* Handle #pragma, if any. */
26040 else if (token->type == CPP_PRAGMA)
26041 cp_parser_pragma (parser, pragma_objc_icode);
26042 /* Allow stray semicolons. */
26043 else if (token->type == CPP_SEMICOLON)
26044 cp_lexer_consume_token (parser->lexer);
26045 /* Mark methods as optional or required, when building protocols. */
26046 else if (token->keyword == RID_AT_OPTIONAL)
26048 cp_lexer_consume_token (parser->lexer);
26049 objc_set_method_opt (true);
26051 else if (token->keyword == RID_AT_REQUIRED)
26053 cp_lexer_consume_token (parser->lexer);
26054 objc_set_method_opt (false);
26056 else if (token->keyword == RID_NAMESPACE)
26057 cp_parser_namespace_definition (parser);
26058 /* Other stray characters must generate errors. */
26059 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26061 cp_lexer_consume_token (parser->lexer);
26062 error ("stray %qs between Objective-C++ methods",
26063 token->type == CPP_OPEN_BRACE ? "{" : "}");
26065 /* Finally, try to parse a block-declaration, or a function-definition. */
26066 else
26067 cp_parser_block_declaration (parser, /*statement_p=*/false);
26070 /* Parse a method signature. */
26072 static tree
26073 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26075 tree rettype, kwdparms, optparms;
26076 bool ellipsis = false;
26077 bool is_class_method;
26079 is_class_method = cp_parser_objc_method_type (parser);
26080 rettype = cp_parser_objc_typename (parser);
26081 *attributes = NULL_TREE;
26082 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26083 if (kwdparms == error_mark_node)
26084 return error_mark_node;
26085 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26086 if (optparms == error_mark_node)
26087 return error_mark_node;
26089 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26092 static bool
26093 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26095 tree tattr;
26096 cp_lexer_save_tokens (parser->lexer);
26097 tattr = cp_parser_attributes_opt (parser);
26098 gcc_assert (tattr) ;
26100 /* If the attributes are followed by a method introducer, this is not allowed.
26101 Dump the attributes and flag the situation. */
26102 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26103 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26104 return true;
26106 /* Otherwise, the attributes introduce some interstitial code, possibly so
26107 rewind to allow that check. */
26108 cp_lexer_rollback_tokens (parser->lexer);
26109 return false;
26112 /* Parse an Objective-C method prototype list. */
26114 static void
26115 cp_parser_objc_method_prototype_list (cp_parser* parser)
26117 cp_token *token = cp_lexer_peek_token (parser->lexer);
26119 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26121 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26123 tree attributes, sig;
26124 bool is_class_method;
26125 if (token->type == CPP_PLUS)
26126 is_class_method = true;
26127 else
26128 is_class_method = false;
26129 sig = cp_parser_objc_method_signature (parser, &attributes);
26130 if (sig == error_mark_node)
26132 cp_parser_skip_to_end_of_block_or_statement (parser);
26133 token = cp_lexer_peek_token (parser->lexer);
26134 continue;
26136 objc_add_method_declaration (is_class_method, sig, attributes);
26137 cp_parser_consume_semicolon_at_end_of_statement (parser);
26139 else if (token->keyword == RID_AT_PROPERTY)
26140 cp_parser_objc_at_property_declaration (parser);
26141 else if (token->keyword == RID_ATTRIBUTE
26142 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26143 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26144 OPT_Wattributes,
26145 "prefix attributes are ignored for methods");
26146 else
26147 /* Allow for interspersed non-ObjC++ code. */
26148 cp_parser_objc_interstitial_code (parser);
26150 token = cp_lexer_peek_token (parser->lexer);
26153 if (token->type != CPP_EOF)
26154 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26155 else
26156 cp_parser_error (parser, "expected %<@end%>");
26158 objc_finish_interface ();
26161 /* Parse an Objective-C method definition list. */
26163 static void
26164 cp_parser_objc_method_definition_list (cp_parser* parser)
26166 cp_token *token = cp_lexer_peek_token (parser->lexer);
26168 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26170 tree meth;
26172 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26174 cp_token *ptk;
26175 tree sig, attribute;
26176 bool is_class_method;
26177 if (token->type == CPP_PLUS)
26178 is_class_method = true;
26179 else
26180 is_class_method = false;
26181 push_deferring_access_checks (dk_deferred);
26182 sig = cp_parser_objc_method_signature (parser, &attribute);
26183 if (sig == error_mark_node)
26185 cp_parser_skip_to_end_of_block_or_statement (parser);
26186 token = cp_lexer_peek_token (parser->lexer);
26187 continue;
26189 objc_start_method_definition (is_class_method, sig, attribute,
26190 NULL_TREE);
26192 /* For historical reasons, we accept an optional semicolon. */
26193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26194 cp_lexer_consume_token (parser->lexer);
26196 ptk = cp_lexer_peek_token (parser->lexer);
26197 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26198 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26200 perform_deferred_access_checks (tf_warning_or_error);
26201 stop_deferring_access_checks ();
26202 meth = cp_parser_function_definition_after_declarator (parser,
26203 false);
26204 pop_deferring_access_checks ();
26205 objc_finish_method_definition (meth);
26208 /* The following case will be removed once @synthesize is
26209 completely implemented. */
26210 else if (token->keyword == RID_AT_PROPERTY)
26211 cp_parser_objc_at_property_declaration (parser);
26212 else if (token->keyword == RID_AT_SYNTHESIZE)
26213 cp_parser_objc_at_synthesize_declaration (parser);
26214 else if (token->keyword == RID_AT_DYNAMIC)
26215 cp_parser_objc_at_dynamic_declaration (parser);
26216 else if (token->keyword == RID_ATTRIBUTE
26217 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26218 warning_at (token->location, OPT_Wattributes,
26219 "prefix attributes are ignored for methods");
26220 else
26221 /* Allow for interspersed non-ObjC++ code. */
26222 cp_parser_objc_interstitial_code (parser);
26224 token = cp_lexer_peek_token (parser->lexer);
26227 if (token->type != CPP_EOF)
26228 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26229 else
26230 cp_parser_error (parser, "expected %<@end%>");
26232 objc_finish_implementation ();
26235 /* Parse Objective-C ivars. */
26237 static void
26238 cp_parser_objc_class_ivars (cp_parser* parser)
26240 cp_token *token = cp_lexer_peek_token (parser->lexer);
26242 if (token->type != CPP_OPEN_BRACE)
26243 return; /* No ivars specified. */
26245 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26246 token = cp_lexer_peek_token (parser->lexer);
26248 while (token->type != CPP_CLOSE_BRACE
26249 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26251 cp_decl_specifier_seq declspecs;
26252 int decl_class_or_enum_p;
26253 tree prefix_attributes;
26255 cp_parser_objc_visibility_spec (parser);
26257 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26258 break;
26260 cp_parser_decl_specifier_seq (parser,
26261 CP_PARSER_FLAGS_OPTIONAL,
26262 &declspecs,
26263 &decl_class_or_enum_p);
26265 /* auto, register, static, extern, mutable. */
26266 if (declspecs.storage_class != sc_none)
26268 cp_parser_error (parser, "invalid type for instance variable");
26269 declspecs.storage_class = sc_none;
26272 /* thread_local. */
26273 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26275 cp_parser_error (parser, "invalid type for instance variable");
26276 declspecs.locations[ds_thread] = 0;
26279 /* typedef. */
26280 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26282 cp_parser_error (parser, "invalid type for instance variable");
26283 declspecs.locations[ds_typedef] = 0;
26286 prefix_attributes = declspecs.attributes;
26287 declspecs.attributes = NULL_TREE;
26289 /* Keep going until we hit the `;' at the end of the
26290 declaration. */
26291 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26293 tree width = NULL_TREE, attributes, first_attribute, decl;
26294 cp_declarator *declarator = NULL;
26295 int ctor_dtor_or_conv_p;
26297 /* Check for a (possibly unnamed) bitfield declaration. */
26298 token = cp_lexer_peek_token (parser->lexer);
26299 if (token->type == CPP_COLON)
26300 goto eat_colon;
26302 if (token->type == CPP_NAME
26303 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26304 == CPP_COLON))
26306 /* Get the name of the bitfield. */
26307 declarator = make_id_declarator (NULL_TREE,
26308 cp_parser_identifier (parser),
26309 sfk_none);
26311 eat_colon:
26312 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26313 /* Get the width of the bitfield. */
26314 width
26315 = cp_parser_constant_expression (parser,
26316 /*allow_non_constant=*/false,
26317 NULL);
26319 else
26321 /* Parse the declarator. */
26322 declarator
26323 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26324 &ctor_dtor_or_conv_p,
26325 /*parenthesized_p=*/NULL,
26326 /*member_p=*/false,
26327 /*friend_p=*/false);
26330 /* Look for attributes that apply to the ivar. */
26331 attributes = cp_parser_attributes_opt (parser);
26332 /* Remember which attributes are prefix attributes and
26333 which are not. */
26334 first_attribute = attributes;
26335 /* Combine the attributes. */
26336 attributes = chainon (prefix_attributes, attributes);
26338 if (width)
26339 /* Create the bitfield declaration. */
26340 decl = grokbitfield (declarator, &declspecs,
26341 width,
26342 attributes);
26343 else
26344 decl = grokfield (declarator, &declspecs,
26345 NULL_TREE, /*init_const_expr_p=*/false,
26346 NULL_TREE, attributes);
26348 /* Add the instance variable. */
26349 if (decl != error_mark_node && decl != NULL_TREE)
26350 objc_add_instance_variable (decl);
26352 /* Reset PREFIX_ATTRIBUTES. */
26353 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26354 attributes = TREE_CHAIN (attributes);
26355 if (attributes)
26356 TREE_CHAIN (attributes) = NULL_TREE;
26358 token = cp_lexer_peek_token (parser->lexer);
26360 if (token->type == CPP_COMMA)
26362 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26363 continue;
26365 break;
26368 cp_parser_consume_semicolon_at_end_of_statement (parser);
26369 token = cp_lexer_peek_token (parser->lexer);
26372 if (token->keyword == RID_AT_END)
26373 cp_parser_error (parser, "expected %<}%>");
26375 /* Do not consume the RID_AT_END, so it will be read again as terminating
26376 the @interface of @implementation. */
26377 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26378 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26380 /* For historical reasons, we accept an optional semicolon. */
26381 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26382 cp_lexer_consume_token (parser->lexer);
26385 /* Parse an Objective-C protocol declaration. */
26387 static void
26388 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26390 tree proto, protorefs;
26391 cp_token *tok;
26393 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26396 tok = cp_lexer_peek_token (parser->lexer);
26397 error_at (tok->location, "identifier expected after %<@protocol%>");
26398 cp_parser_consume_semicolon_at_end_of_statement (parser);
26399 return;
26402 /* See if we have a forward declaration or a definition. */
26403 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26405 /* Try a forward declaration first. */
26406 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26408 while (true)
26410 tree id;
26412 id = cp_parser_identifier (parser);
26413 if (id == error_mark_node)
26414 break;
26416 objc_declare_protocol (id, attributes);
26418 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26419 cp_lexer_consume_token (parser->lexer);
26420 else
26421 break;
26423 cp_parser_consume_semicolon_at_end_of_statement (parser);
26426 /* Ok, we got a full-fledged definition (or at least should). */
26427 else
26429 proto = cp_parser_identifier (parser);
26430 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26431 objc_start_protocol (proto, protorefs, attributes);
26432 cp_parser_objc_method_prototype_list (parser);
26436 /* Parse an Objective-C superclass or category. */
26438 static void
26439 cp_parser_objc_superclass_or_category (cp_parser *parser,
26440 bool iface_p,
26441 tree *super,
26442 tree *categ, bool *is_class_extension)
26444 cp_token *next = cp_lexer_peek_token (parser->lexer);
26446 *super = *categ = NULL_TREE;
26447 *is_class_extension = false;
26448 if (next->type == CPP_COLON)
26450 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26451 *super = cp_parser_identifier (parser);
26453 else if (next->type == CPP_OPEN_PAREN)
26455 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26457 /* If there is no category name, and this is an @interface, we
26458 have a class extension. */
26459 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26461 *categ = NULL_TREE;
26462 *is_class_extension = true;
26464 else
26465 *categ = cp_parser_identifier (parser);
26467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26471 /* Parse an Objective-C class interface. */
26473 static void
26474 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26476 tree name, super, categ, protos;
26477 bool is_class_extension;
26479 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26480 name = cp_parser_identifier (parser);
26481 if (name == error_mark_node)
26483 /* It's hard to recover because even if valid @interface stuff
26484 is to follow, we can't compile it (or validate it) if we
26485 don't even know which class it refers to. Let's assume this
26486 was a stray '@interface' token in the stream and skip it.
26488 return;
26490 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26491 &is_class_extension);
26492 protos = cp_parser_objc_protocol_refs_opt (parser);
26494 /* We have either a class or a category on our hands. */
26495 if (categ || is_class_extension)
26496 objc_start_category_interface (name, categ, protos, attributes);
26497 else
26499 objc_start_class_interface (name, super, protos, attributes);
26500 /* Handle instance variable declarations, if any. */
26501 cp_parser_objc_class_ivars (parser);
26502 objc_continue_interface ();
26505 cp_parser_objc_method_prototype_list (parser);
26508 /* Parse an Objective-C class implementation. */
26510 static void
26511 cp_parser_objc_class_implementation (cp_parser* parser)
26513 tree name, super, categ;
26514 bool is_class_extension;
26516 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26517 name = cp_parser_identifier (parser);
26518 if (name == error_mark_node)
26520 /* It's hard to recover because even if valid @implementation
26521 stuff is to follow, we can't compile it (or validate it) if
26522 we don't even know which class it refers to. Let's assume
26523 this was a stray '@implementation' token in the stream and
26524 skip it.
26526 return;
26528 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26529 &is_class_extension);
26531 /* We have either a class or a category on our hands. */
26532 if (categ)
26533 objc_start_category_implementation (name, categ);
26534 else
26536 objc_start_class_implementation (name, super);
26537 /* Handle instance variable declarations, if any. */
26538 cp_parser_objc_class_ivars (parser);
26539 objc_continue_implementation ();
26542 cp_parser_objc_method_definition_list (parser);
26545 /* Consume the @end token and finish off the implementation. */
26547 static void
26548 cp_parser_objc_end_implementation (cp_parser* parser)
26550 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26551 objc_finish_implementation ();
26554 /* Parse an Objective-C declaration. */
26556 static void
26557 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26559 /* Try to figure out what kind of declaration is present. */
26560 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26562 if (attributes)
26563 switch (kwd->keyword)
26565 case RID_AT_ALIAS:
26566 case RID_AT_CLASS:
26567 case RID_AT_END:
26568 error_at (kwd->location, "attributes may not be specified before"
26569 " the %<@%D%> Objective-C++ keyword",
26570 kwd->u.value);
26571 attributes = NULL;
26572 break;
26573 case RID_AT_IMPLEMENTATION:
26574 warning_at (kwd->location, OPT_Wattributes,
26575 "prefix attributes are ignored before %<@%D%>",
26576 kwd->u.value);
26577 attributes = NULL;
26578 default:
26579 break;
26582 switch (kwd->keyword)
26584 case RID_AT_ALIAS:
26585 cp_parser_objc_alias_declaration (parser);
26586 break;
26587 case RID_AT_CLASS:
26588 cp_parser_objc_class_declaration (parser);
26589 break;
26590 case RID_AT_PROTOCOL:
26591 cp_parser_objc_protocol_declaration (parser, attributes);
26592 break;
26593 case RID_AT_INTERFACE:
26594 cp_parser_objc_class_interface (parser, attributes);
26595 break;
26596 case RID_AT_IMPLEMENTATION:
26597 cp_parser_objc_class_implementation (parser);
26598 break;
26599 case RID_AT_END:
26600 cp_parser_objc_end_implementation (parser);
26601 break;
26602 default:
26603 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26604 kwd->u.value);
26605 cp_parser_skip_to_end_of_block_or_statement (parser);
26609 /* Parse an Objective-C try-catch-finally statement.
26611 objc-try-catch-finally-stmt:
26612 @try compound-statement objc-catch-clause-seq [opt]
26613 objc-finally-clause [opt]
26615 objc-catch-clause-seq:
26616 objc-catch-clause objc-catch-clause-seq [opt]
26618 objc-catch-clause:
26619 @catch ( objc-exception-declaration ) compound-statement
26621 objc-finally-clause:
26622 @finally compound-statement
26624 objc-exception-declaration:
26625 parameter-declaration
26626 '...'
26628 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26630 Returns NULL_TREE.
26632 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26633 for C. Keep them in sync. */
26635 static tree
26636 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26638 location_t location;
26639 tree stmt;
26641 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26642 location = cp_lexer_peek_token (parser->lexer)->location;
26643 objc_maybe_warn_exceptions (location);
26644 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26645 node, lest it get absorbed into the surrounding block. */
26646 stmt = push_stmt_list ();
26647 cp_parser_compound_statement (parser, NULL, false, false);
26648 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26650 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26652 cp_parameter_declarator *parm;
26653 tree parameter_declaration = error_mark_node;
26654 bool seen_open_paren = false;
26656 cp_lexer_consume_token (parser->lexer);
26657 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26658 seen_open_paren = true;
26659 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26661 /* We have "@catch (...)" (where the '...' are literally
26662 what is in the code). Skip the '...'.
26663 parameter_declaration is set to NULL_TREE, and
26664 objc_being_catch_clauses() knows that that means
26665 '...'. */
26666 cp_lexer_consume_token (parser->lexer);
26667 parameter_declaration = NULL_TREE;
26669 else
26671 /* We have "@catch (NSException *exception)" or something
26672 like that. Parse the parameter declaration. */
26673 parm = cp_parser_parameter_declaration (parser, false, NULL);
26674 if (parm == NULL)
26675 parameter_declaration = error_mark_node;
26676 else
26677 parameter_declaration = grokdeclarator (parm->declarator,
26678 &parm->decl_specifiers,
26679 PARM, /*initialized=*/0,
26680 /*attrlist=*/NULL);
26682 if (seen_open_paren)
26683 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26684 else
26686 /* If there was no open parenthesis, we are recovering from
26687 an error, and we are trying to figure out what mistake
26688 the user has made. */
26690 /* If there is an immediate closing parenthesis, the user
26691 probably forgot the opening one (ie, they typed "@catch
26692 NSException *e)". Parse the closing parenthesis and keep
26693 going. */
26694 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26695 cp_lexer_consume_token (parser->lexer);
26697 /* If these is no immediate closing parenthesis, the user
26698 probably doesn't know that parenthesis are required at
26699 all (ie, they typed "@catch NSException *e"). So, just
26700 forget about the closing parenthesis and keep going. */
26702 objc_begin_catch_clause (parameter_declaration);
26703 cp_parser_compound_statement (parser, NULL, false, false);
26704 objc_finish_catch_clause ();
26706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26708 cp_lexer_consume_token (parser->lexer);
26709 location = cp_lexer_peek_token (parser->lexer)->location;
26710 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26711 node, lest it get absorbed into the surrounding block. */
26712 stmt = push_stmt_list ();
26713 cp_parser_compound_statement (parser, NULL, false, false);
26714 objc_build_finally_clause (location, pop_stmt_list (stmt));
26717 return objc_finish_try_stmt ();
26720 /* Parse an Objective-C synchronized statement.
26722 objc-synchronized-stmt:
26723 @synchronized ( expression ) compound-statement
26725 Returns NULL_TREE. */
26727 static tree
26728 cp_parser_objc_synchronized_statement (cp_parser *parser)
26730 location_t location;
26731 tree lock, stmt;
26733 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26735 location = cp_lexer_peek_token (parser->lexer)->location;
26736 objc_maybe_warn_exceptions (location);
26737 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26738 lock = cp_parser_expression (parser);
26739 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26741 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26742 node, lest it get absorbed into the surrounding block. */
26743 stmt = push_stmt_list ();
26744 cp_parser_compound_statement (parser, NULL, false, false);
26746 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26749 /* Parse an Objective-C throw statement.
26751 objc-throw-stmt:
26752 @throw assignment-expression [opt] ;
26754 Returns a constructed '@throw' statement. */
26756 static tree
26757 cp_parser_objc_throw_statement (cp_parser *parser)
26759 tree expr = NULL_TREE;
26760 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26762 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26765 expr = cp_parser_expression (parser);
26767 cp_parser_consume_semicolon_at_end_of_statement (parser);
26769 return objc_build_throw_stmt (loc, expr);
26772 /* Parse an Objective-C statement. */
26774 static tree
26775 cp_parser_objc_statement (cp_parser * parser)
26777 /* Try to figure out what kind of declaration is present. */
26778 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26780 switch (kwd->keyword)
26782 case RID_AT_TRY:
26783 return cp_parser_objc_try_catch_finally_statement (parser);
26784 case RID_AT_SYNCHRONIZED:
26785 return cp_parser_objc_synchronized_statement (parser);
26786 case RID_AT_THROW:
26787 return cp_parser_objc_throw_statement (parser);
26788 default:
26789 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26790 kwd->u.value);
26791 cp_parser_skip_to_end_of_block_or_statement (parser);
26794 return error_mark_node;
26797 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26798 look ahead to see if an objc keyword follows the attributes. This
26799 is to detect the use of prefix attributes on ObjC @interface and
26800 @protocol. */
26802 static bool
26803 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26805 cp_lexer_save_tokens (parser->lexer);
26806 *attrib = cp_parser_attributes_opt (parser);
26807 gcc_assert (*attrib);
26808 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26810 cp_lexer_commit_tokens (parser->lexer);
26811 return true;
26813 cp_lexer_rollback_tokens (parser->lexer);
26814 return false;
26817 /* This routine is a minimal replacement for
26818 c_parser_struct_declaration () used when parsing the list of
26819 types/names or ObjC++ properties. For example, when parsing the
26820 code
26822 @property (readonly) int a, b, c;
26824 this function is responsible for parsing "int a, int b, int c" and
26825 returning the declarations as CHAIN of DECLs.
26827 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26828 similar parsing. */
26829 static tree
26830 cp_parser_objc_struct_declaration (cp_parser *parser)
26832 tree decls = NULL_TREE;
26833 cp_decl_specifier_seq declspecs;
26834 int decl_class_or_enum_p;
26835 tree prefix_attributes;
26837 cp_parser_decl_specifier_seq (parser,
26838 CP_PARSER_FLAGS_NONE,
26839 &declspecs,
26840 &decl_class_or_enum_p);
26842 if (declspecs.type == error_mark_node)
26843 return error_mark_node;
26845 /* auto, register, static, extern, mutable. */
26846 if (declspecs.storage_class != sc_none)
26848 cp_parser_error (parser, "invalid type for property");
26849 declspecs.storage_class = sc_none;
26852 /* thread_local. */
26853 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26855 cp_parser_error (parser, "invalid type for property");
26856 declspecs.locations[ds_thread] = 0;
26859 /* typedef. */
26860 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26862 cp_parser_error (parser, "invalid type for property");
26863 declspecs.locations[ds_typedef] = 0;
26866 prefix_attributes = declspecs.attributes;
26867 declspecs.attributes = NULL_TREE;
26869 /* Keep going until we hit the `;' at the end of the declaration. */
26870 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26872 tree attributes, first_attribute, decl;
26873 cp_declarator *declarator;
26874 cp_token *token;
26876 /* Parse the declarator. */
26877 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26878 NULL, NULL, false, false);
26880 /* Look for attributes that apply to the ivar. */
26881 attributes = cp_parser_attributes_opt (parser);
26882 /* Remember which attributes are prefix attributes and
26883 which are not. */
26884 first_attribute = attributes;
26885 /* Combine the attributes. */
26886 attributes = chainon (prefix_attributes, attributes);
26888 decl = grokfield (declarator, &declspecs,
26889 NULL_TREE, /*init_const_expr_p=*/false,
26890 NULL_TREE, attributes);
26892 if (decl == error_mark_node || decl == NULL_TREE)
26893 return error_mark_node;
26895 /* Reset PREFIX_ATTRIBUTES. */
26896 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26897 attributes = TREE_CHAIN (attributes);
26898 if (attributes)
26899 TREE_CHAIN (attributes) = NULL_TREE;
26901 DECL_CHAIN (decl) = decls;
26902 decls = decl;
26904 token = cp_lexer_peek_token (parser->lexer);
26905 if (token->type == CPP_COMMA)
26907 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26908 continue;
26910 else
26911 break;
26913 return decls;
26916 /* Parse an Objective-C @property declaration. The syntax is:
26918 objc-property-declaration:
26919 '@property' objc-property-attributes[opt] struct-declaration ;
26921 objc-property-attributes:
26922 '(' objc-property-attribute-list ')'
26924 objc-property-attribute-list:
26925 objc-property-attribute
26926 objc-property-attribute-list, objc-property-attribute
26928 objc-property-attribute
26929 'getter' = identifier
26930 'setter' = identifier
26931 'readonly'
26932 'readwrite'
26933 'assign'
26934 'retain'
26935 'copy'
26936 'nonatomic'
26938 For example:
26939 @property NSString *name;
26940 @property (readonly) id object;
26941 @property (retain, nonatomic, getter=getTheName) id name;
26942 @property int a, b, c;
26944 PS: This function is identical to
26945 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26946 static void
26947 cp_parser_objc_at_property_declaration (cp_parser *parser)
26949 /* The following variables hold the attributes of the properties as
26950 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26951 seen. When we see an attribute, we set them to 'true' (if they
26952 are boolean properties) or to the identifier (if they have an
26953 argument, ie, for getter and setter). Note that here we only
26954 parse the list of attributes, check the syntax and accumulate the
26955 attributes that we find. objc_add_property_declaration() will
26956 then process the information. */
26957 bool property_assign = false;
26958 bool property_copy = false;
26959 tree property_getter_ident = NULL_TREE;
26960 bool property_nonatomic = false;
26961 bool property_readonly = false;
26962 bool property_readwrite = false;
26963 bool property_retain = false;
26964 tree property_setter_ident = NULL_TREE;
26966 /* 'properties' is the list of properties that we read. Usually a
26967 single one, but maybe more (eg, in "@property int a, b, c;" there
26968 are three). */
26969 tree properties;
26970 location_t loc;
26972 loc = cp_lexer_peek_token (parser->lexer)->location;
26974 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26976 /* Parse the optional attribute list... */
26977 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26979 /* Eat the '('. */
26980 cp_lexer_consume_token (parser->lexer);
26982 while (true)
26984 bool syntax_error = false;
26985 cp_token *token = cp_lexer_peek_token (parser->lexer);
26986 enum rid keyword;
26988 if (token->type != CPP_NAME)
26990 cp_parser_error (parser, "expected identifier");
26991 break;
26993 keyword = C_RID_CODE (token->u.value);
26994 cp_lexer_consume_token (parser->lexer);
26995 switch (keyword)
26997 case RID_ASSIGN: property_assign = true; break;
26998 case RID_COPY: property_copy = true; break;
26999 case RID_NONATOMIC: property_nonatomic = true; break;
27000 case RID_READONLY: property_readonly = true; break;
27001 case RID_READWRITE: property_readwrite = true; break;
27002 case RID_RETAIN: property_retain = true; break;
27004 case RID_GETTER:
27005 case RID_SETTER:
27006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27008 if (keyword == RID_GETTER)
27009 cp_parser_error (parser,
27010 "missing %<=%> (after %<getter%> attribute)");
27011 else
27012 cp_parser_error (parser,
27013 "missing %<=%> (after %<setter%> attribute)");
27014 syntax_error = true;
27015 break;
27017 cp_lexer_consume_token (parser->lexer); /* eat the = */
27018 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27020 cp_parser_error (parser, "expected identifier");
27021 syntax_error = true;
27022 break;
27024 if (keyword == RID_SETTER)
27026 if (property_setter_ident != NULL_TREE)
27028 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27029 cp_lexer_consume_token (parser->lexer);
27031 else
27032 property_setter_ident = cp_parser_objc_selector (parser);
27033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27034 cp_parser_error (parser, "setter name must terminate with %<:%>");
27035 else
27036 cp_lexer_consume_token (parser->lexer);
27038 else
27040 if (property_getter_ident != NULL_TREE)
27042 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27043 cp_lexer_consume_token (parser->lexer);
27045 else
27046 property_getter_ident = cp_parser_objc_selector (parser);
27048 break;
27049 default:
27050 cp_parser_error (parser, "unknown property attribute");
27051 syntax_error = true;
27052 break;
27055 if (syntax_error)
27056 break;
27058 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27059 cp_lexer_consume_token (parser->lexer);
27060 else
27061 break;
27064 /* FIXME: "@property (setter, assign);" will generate a spurious
27065 "error: expected ‘)’ before ‘,’ token". This is because
27066 cp_parser_require, unlike the C counterpart, will produce an
27067 error even if we are in error recovery. */
27068 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27070 cp_parser_skip_to_closing_parenthesis (parser,
27071 /*recovering=*/true,
27072 /*or_comma=*/false,
27073 /*consume_paren=*/true);
27077 /* ... and the property declaration(s). */
27078 properties = cp_parser_objc_struct_declaration (parser);
27080 if (properties == error_mark_node)
27082 cp_parser_skip_to_end_of_statement (parser);
27083 /* If the next token is now a `;', consume it. */
27084 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27085 cp_lexer_consume_token (parser->lexer);
27086 return;
27089 if (properties == NULL_TREE)
27090 cp_parser_error (parser, "expected identifier");
27091 else
27093 /* Comma-separated properties are chained together in
27094 reverse order; add them one by one. */
27095 properties = nreverse (properties);
27097 for (; properties; properties = TREE_CHAIN (properties))
27098 objc_add_property_declaration (loc, copy_node (properties),
27099 property_readonly, property_readwrite,
27100 property_assign, property_retain,
27101 property_copy, property_nonatomic,
27102 property_getter_ident, property_setter_ident);
27105 cp_parser_consume_semicolon_at_end_of_statement (parser);
27108 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27110 objc-synthesize-declaration:
27111 @synthesize objc-synthesize-identifier-list ;
27113 objc-synthesize-identifier-list:
27114 objc-synthesize-identifier
27115 objc-synthesize-identifier-list, objc-synthesize-identifier
27117 objc-synthesize-identifier
27118 identifier
27119 identifier = identifier
27121 For example:
27122 @synthesize MyProperty;
27123 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27125 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27126 for C. Keep them in sync.
27128 static void
27129 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27131 tree list = NULL_TREE;
27132 location_t loc;
27133 loc = cp_lexer_peek_token (parser->lexer)->location;
27135 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27136 while (true)
27138 tree property, ivar;
27139 property = cp_parser_identifier (parser);
27140 if (property == error_mark_node)
27142 cp_parser_consume_semicolon_at_end_of_statement (parser);
27143 return;
27145 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27147 cp_lexer_consume_token (parser->lexer);
27148 ivar = cp_parser_identifier (parser);
27149 if (ivar == error_mark_node)
27151 cp_parser_consume_semicolon_at_end_of_statement (parser);
27152 return;
27155 else
27156 ivar = NULL_TREE;
27157 list = chainon (list, build_tree_list (ivar, property));
27158 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27159 cp_lexer_consume_token (parser->lexer);
27160 else
27161 break;
27163 cp_parser_consume_semicolon_at_end_of_statement (parser);
27164 objc_add_synthesize_declaration (loc, list);
27167 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27169 objc-dynamic-declaration:
27170 @dynamic identifier-list ;
27172 For example:
27173 @dynamic MyProperty;
27174 @dynamic MyProperty, AnotherProperty;
27176 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27177 for C. Keep them in sync.
27179 static void
27180 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27182 tree list = NULL_TREE;
27183 location_t loc;
27184 loc = cp_lexer_peek_token (parser->lexer)->location;
27186 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27187 while (true)
27189 tree property;
27190 property = cp_parser_identifier (parser);
27191 if (property == error_mark_node)
27193 cp_parser_consume_semicolon_at_end_of_statement (parser);
27194 return;
27196 list = chainon (list, build_tree_list (NULL, property));
27197 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27198 cp_lexer_consume_token (parser->lexer);
27199 else
27200 break;
27202 cp_parser_consume_semicolon_at_end_of_statement (parser);
27203 objc_add_dynamic_declaration (loc, list);
27207 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27209 /* Returns name of the next clause.
27210 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27211 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27212 returned and the token is consumed. */
27214 static pragma_omp_clause
27215 cp_parser_omp_clause_name (cp_parser *parser)
27217 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27220 result = PRAGMA_OMP_CLAUSE_IF;
27221 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27222 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27223 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27224 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27225 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27226 result = PRAGMA_OMP_CLAUSE_FOR;
27227 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27230 const char *p = IDENTIFIER_POINTER (id);
27232 switch (p[0])
27234 case 'a':
27235 if (!strcmp ("aligned", p))
27236 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27237 break;
27238 case 'c':
27239 if (!strcmp ("collapse", p))
27240 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27241 else if (!strcmp ("copyin", p))
27242 result = PRAGMA_OMP_CLAUSE_COPYIN;
27243 else if (!strcmp ("copyprivate", p))
27244 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27245 break;
27246 case 'd':
27247 if (!strcmp ("depend", p))
27248 result = PRAGMA_OMP_CLAUSE_DEPEND;
27249 else if (!strcmp ("device", p))
27250 result = PRAGMA_OMP_CLAUSE_DEVICE;
27251 else if (!strcmp ("dist_schedule", p))
27252 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27253 break;
27254 case 'f':
27255 if (!strcmp ("final", p))
27256 result = PRAGMA_OMP_CLAUSE_FINAL;
27257 else if (!strcmp ("firstprivate", p))
27258 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27259 else if (!strcmp ("from", p))
27260 result = PRAGMA_OMP_CLAUSE_FROM;
27261 break;
27262 case 'i':
27263 if (!strcmp ("inbranch", p))
27264 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27265 break;
27266 case 'l':
27267 if (!strcmp ("lastprivate", p))
27268 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27269 else if (!strcmp ("linear", p))
27270 result = PRAGMA_OMP_CLAUSE_LINEAR;
27271 break;
27272 case 'm':
27273 if (!strcmp ("map", p))
27274 result = PRAGMA_OMP_CLAUSE_MAP;
27275 else if (!strcmp ("mergeable", p))
27276 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27277 else if (flag_cilkplus && !strcmp ("mask", p))
27278 result = PRAGMA_CILK_CLAUSE_MASK;
27279 break;
27280 case 'n':
27281 if (!strcmp ("notinbranch", p))
27282 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27283 else if (!strcmp ("nowait", p))
27284 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27285 else if (flag_cilkplus && !strcmp ("nomask", p))
27286 result = PRAGMA_CILK_CLAUSE_NOMASK;
27287 else if (!strcmp ("num_teams", p))
27288 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27289 else if (!strcmp ("num_threads", p))
27290 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27291 break;
27292 case 'o':
27293 if (!strcmp ("ordered", p))
27294 result = PRAGMA_OMP_CLAUSE_ORDERED;
27295 break;
27296 case 'p':
27297 if (!strcmp ("parallel", p))
27298 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27299 else if (!strcmp ("proc_bind", p))
27300 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27301 break;
27302 case 'r':
27303 if (!strcmp ("reduction", p))
27304 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27305 break;
27306 case 's':
27307 if (!strcmp ("safelen", p))
27308 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27309 else if (!strcmp ("schedule", p))
27310 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27311 else if (!strcmp ("sections", p))
27312 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27313 else if (!strcmp ("shared", p))
27314 result = PRAGMA_OMP_CLAUSE_SHARED;
27315 else if (!strcmp ("simdlen", p))
27316 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27317 break;
27318 case 't':
27319 if (!strcmp ("taskgroup", p))
27320 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27321 else if (!strcmp ("thread_limit", p))
27322 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27323 else if (!strcmp ("to", p))
27324 result = PRAGMA_OMP_CLAUSE_TO;
27325 break;
27326 case 'u':
27327 if (!strcmp ("uniform", p))
27328 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27329 else if (!strcmp ("untied", p))
27330 result = PRAGMA_OMP_CLAUSE_UNTIED;
27331 break;
27332 case 'v':
27333 if (flag_cilkplus && !strcmp ("vectorlength", p))
27334 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27335 break;
27339 if (result != PRAGMA_OMP_CLAUSE_NONE)
27340 cp_lexer_consume_token (parser->lexer);
27342 return result;
27345 /* Validate that a clause of the given type does not already exist. */
27347 static void
27348 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27349 const char *name, location_t location)
27351 tree c;
27353 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27354 if (OMP_CLAUSE_CODE (c) == code)
27356 error_at (location, "too many %qs clauses", name);
27357 break;
27361 /* OpenMP 2.5:
27362 variable-list:
27363 identifier
27364 variable-list , identifier
27366 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27367 colon). An opening parenthesis will have been consumed by the caller.
27369 If KIND is nonzero, create the appropriate node and install the decl
27370 in OMP_CLAUSE_DECL and add the node to the head of the list.
27372 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27373 return the list created.
27375 COLON can be NULL if only closing parenthesis should end the list,
27376 or pointer to bool which will receive false if the list is terminated
27377 by closing parenthesis or true if the list is terminated by colon. */
27379 static tree
27380 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27381 tree list, bool *colon)
27383 cp_token *token;
27384 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27385 if (colon)
27387 parser->colon_corrects_to_scope_p = false;
27388 *colon = false;
27390 while (1)
27392 tree name, decl;
27394 token = cp_lexer_peek_token (parser->lexer);
27395 name = cp_parser_id_expression (parser, /*template_p=*/false,
27396 /*check_dependency_p=*/true,
27397 /*template_p=*/NULL,
27398 /*declarator_p=*/false,
27399 /*optional_p=*/false);
27400 if (name == error_mark_node)
27401 goto skip_comma;
27403 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27404 if (decl == error_mark_node)
27405 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27406 token->location);
27407 else if (kind != 0)
27409 switch (kind)
27411 case OMP_CLAUSE_MAP:
27412 case OMP_CLAUSE_FROM:
27413 case OMP_CLAUSE_TO:
27414 case OMP_CLAUSE_DEPEND:
27415 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27417 tree low_bound = NULL_TREE, length = NULL_TREE;
27419 parser->colon_corrects_to_scope_p = false;
27420 cp_lexer_consume_token (parser->lexer);
27421 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27422 low_bound = cp_parser_expression (parser);
27423 if (!colon)
27424 parser->colon_corrects_to_scope_p
27425 = saved_colon_corrects_to_scope_p;
27426 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27427 length = integer_one_node;
27428 else
27430 /* Look for `:'. */
27431 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27432 goto skip_comma;
27433 if (!cp_lexer_next_token_is (parser->lexer,
27434 CPP_CLOSE_SQUARE))
27435 length = cp_parser_expression (parser);
27437 /* Look for the closing `]'. */
27438 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27439 RT_CLOSE_SQUARE))
27440 goto skip_comma;
27441 decl = tree_cons (low_bound, length, decl);
27443 break;
27444 default:
27445 break;
27448 tree u = build_omp_clause (token->location, kind);
27449 OMP_CLAUSE_DECL (u) = decl;
27450 OMP_CLAUSE_CHAIN (u) = list;
27451 list = u;
27453 else
27454 list = tree_cons (decl, NULL_TREE, list);
27456 get_comma:
27457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27458 break;
27459 cp_lexer_consume_token (parser->lexer);
27462 if (colon)
27463 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27465 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27467 *colon = true;
27468 cp_parser_require (parser, CPP_COLON, RT_COLON);
27469 return list;
27472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27474 int ending;
27476 /* Try to resync to an unnested comma. Copied from
27477 cp_parser_parenthesized_expression_list. */
27478 skip_comma:
27479 if (colon)
27480 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27481 ending = cp_parser_skip_to_closing_parenthesis (parser,
27482 /*recovering=*/true,
27483 /*or_comma=*/true,
27484 /*consume_paren=*/true);
27485 if (ending < 0)
27486 goto get_comma;
27489 return list;
27492 /* Similarly, but expect leading and trailing parenthesis. This is a very
27493 common case for omp clauses. */
27495 static tree
27496 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27498 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27499 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27500 return list;
27503 /* OpenMP 3.0:
27504 collapse ( constant-expression ) */
27506 static tree
27507 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27509 tree c, num;
27510 location_t loc;
27511 HOST_WIDE_INT n;
27513 loc = cp_lexer_peek_token (parser->lexer)->location;
27514 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27515 return list;
27517 num = cp_parser_constant_expression (parser, false, NULL);
27519 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27520 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27521 /*or_comma=*/false,
27522 /*consume_paren=*/true);
27524 if (num == error_mark_node)
27525 return list;
27526 num = fold_non_dependent_expr (num);
27527 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27528 || !tree_fits_shwi_p (num)
27529 || (n = tree_to_shwi (num)) <= 0
27530 || (int) n != n)
27532 error_at (loc, "collapse argument needs positive constant integer expression");
27533 return list;
27536 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27537 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27538 OMP_CLAUSE_CHAIN (c) = list;
27539 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27541 return c;
27544 /* OpenMP 2.5:
27545 default ( shared | none ) */
27547 static tree
27548 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27550 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27551 tree c;
27553 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27554 return list;
27555 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27557 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27558 const char *p = IDENTIFIER_POINTER (id);
27560 switch (p[0])
27562 case 'n':
27563 if (strcmp ("none", p) != 0)
27564 goto invalid_kind;
27565 kind = OMP_CLAUSE_DEFAULT_NONE;
27566 break;
27568 case 's':
27569 if (strcmp ("shared", p) != 0)
27570 goto invalid_kind;
27571 kind = OMP_CLAUSE_DEFAULT_SHARED;
27572 break;
27574 default:
27575 goto invalid_kind;
27578 cp_lexer_consume_token (parser->lexer);
27580 else
27582 invalid_kind:
27583 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27586 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27587 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27588 /*or_comma=*/false,
27589 /*consume_paren=*/true);
27591 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27592 return list;
27594 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27595 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27596 OMP_CLAUSE_CHAIN (c) = list;
27597 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27599 return c;
27602 /* OpenMP 3.1:
27603 final ( expression ) */
27605 static tree
27606 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27608 tree t, c;
27610 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27611 return list;
27613 t = cp_parser_condition (parser);
27615 if (t == error_mark_node
27616 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27618 /*or_comma=*/false,
27619 /*consume_paren=*/true);
27621 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27623 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27624 OMP_CLAUSE_FINAL_EXPR (c) = t;
27625 OMP_CLAUSE_CHAIN (c) = list;
27627 return c;
27630 /* OpenMP 2.5:
27631 if ( expression ) */
27633 static tree
27634 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27636 tree t, c;
27638 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27639 return list;
27641 t = cp_parser_condition (parser);
27643 if (t == error_mark_node
27644 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27645 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27646 /*or_comma=*/false,
27647 /*consume_paren=*/true);
27649 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27651 c = build_omp_clause (location, OMP_CLAUSE_IF);
27652 OMP_CLAUSE_IF_EXPR (c) = t;
27653 OMP_CLAUSE_CHAIN (c) = list;
27655 return c;
27658 /* OpenMP 3.1:
27659 mergeable */
27661 static tree
27662 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27663 tree list, location_t location)
27665 tree c;
27667 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27668 location);
27670 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27671 OMP_CLAUSE_CHAIN (c) = list;
27672 return c;
27675 /* OpenMP 2.5:
27676 nowait */
27678 static tree
27679 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27680 tree list, location_t location)
27682 tree c;
27684 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27686 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27687 OMP_CLAUSE_CHAIN (c) = list;
27688 return c;
27691 /* OpenMP 2.5:
27692 num_threads ( expression ) */
27694 static tree
27695 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27696 location_t location)
27698 tree t, c;
27700 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27701 return list;
27703 t = cp_parser_expression (parser);
27705 if (t == error_mark_node
27706 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27708 /*or_comma=*/false,
27709 /*consume_paren=*/true);
27711 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27712 "num_threads", location);
27714 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27715 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27716 OMP_CLAUSE_CHAIN (c) = list;
27718 return c;
27721 /* OpenMP 2.5:
27722 ordered */
27724 static tree
27725 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27726 tree list, location_t location)
27728 tree c;
27730 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27731 "ordered", location);
27733 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27734 OMP_CLAUSE_CHAIN (c) = list;
27735 return c;
27738 /* OpenMP 2.5:
27739 reduction ( reduction-operator : variable-list )
27741 reduction-operator:
27742 One of: + * - & ^ | && ||
27744 OpenMP 3.1:
27746 reduction-operator:
27747 One of: + * - & ^ | && || min max
27749 OpenMP 4.0:
27751 reduction-operator:
27752 One of: + * - & ^ | && ||
27753 id-expression */
27755 static tree
27756 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27758 enum tree_code code = ERROR_MARK;
27759 tree nlist, c, id = NULL_TREE;
27761 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27762 return list;
27764 switch (cp_lexer_peek_token (parser->lexer)->type)
27766 case CPP_PLUS: code = PLUS_EXPR; break;
27767 case CPP_MULT: code = MULT_EXPR; break;
27768 case CPP_MINUS: code = MINUS_EXPR; break;
27769 case CPP_AND: code = BIT_AND_EXPR; break;
27770 case CPP_XOR: code = BIT_XOR_EXPR; break;
27771 case CPP_OR: code = BIT_IOR_EXPR; break;
27772 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27773 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27774 default: break;
27777 if (code != ERROR_MARK)
27778 cp_lexer_consume_token (parser->lexer);
27779 else
27781 bool saved_colon_corrects_to_scope_p;
27782 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27783 parser->colon_corrects_to_scope_p = false;
27784 id = cp_parser_id_expression (parser, /*template_p=*/false,
27785 /*check_dependency_p=*/true,
27786 /*template_p=*/NULL,
27787 /*declarator_p=*/false,
27788 /*optional_p=*/false);
27789 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27790 if (identifier_p (id))
27792 const char *p = IDENTIFIER_POINTER (id);
27794 if (strcmp (p, "min") == 0)
27795 code = MIN_EXPR;
27796 else if (strcmp (p, "max") == 0)
27797 code = MAX_EXPR;
27798 else if (id == ansi_opname (PLUS_EXPR))
27799 code = PLUS_EXPR;
27800 else if (id == ansi_opname (MULT_EXPR))
27801 code = MULT_EXPR;
27802 else if (id == ansi_opname (MINUS_EXPR))
27803 code = MINUS_EXPR;
27804 else if (id == ansi_opname (BIT_AND_EXPR))
27805 code = BIT_AND_EXPR;
27806 else if (id == ansi_opname (BIT_IOR_EXPR))
27807 code = BIT_IOR_EXPR;
27808 else if (id == ansi_opname (BIT_XOR_EXPR))
27809 code = BIT_XOR_EXPR;
27810 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27811 code = TRUTH_ANDIF_EXPR;
27812 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27813 code = TRUTH_ORIF_EXPR;
27814 id = omp_reduction_id (code, id, NULL_TREE);
27815 tree scope = parser->scope;
27816 if (scope)
27817 id = build_qualified_name (NULL_TREE, scope, id, false);
27818 parser->scope = NULL_TREE;
27819 parser->qualifying_scope = NULL_TREE;
27820 parser->object_scope = NULL_TREE;
27822 else
27824 error ("invalid reduction-identifier");
27825 resync_fail:
27826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27827 /*or_comma=*/false,
27828 /*consume_paren=*/true);
27829 return list;
27833 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27834 goto resync_fail;
27836 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27837 NULL);
27838 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27840 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27841 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27844 return nlist;
27847 /* OpenMP 2.5:
27848 schedule ( schedule-kind )
27849 schedule ( schedule-kind , expression )
27851 schedule-kind:
27852 static | dynamic | guided | runtime | auto */
27854 static tree
27855 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27857 tree c, t;
27859 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27860 return list;
27862 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27866 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27867 const char *p = IDENTIFIER_POINTER (id);
27869 switch (p[0])
27871 case 'd':
27872 if (strcmp ("dynamic", p) != 0)
27873 goto invalid_kind;
27874 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27875 break;
27877 case 'g':
27878 if (strcmp ("guided", p) != 0)
27879 goto invalid_kind;
27880 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27881 break;
27883 case 'r':
27884 if (strcmp ("runtime", p) != 0)
27885 goto invalid_kind;
27886 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27887 break;
27889 default:
27890 goto invalid_kind;
27893 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27894 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27895 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27896 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27897 else
27898 goto invalid_kind;
27899 cp_lexer_consume_token (parser->lexer);
27901 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27903 cp_token *token;
27904 cp_lexer_consume_token (parser->lexer);
27906 token = cp_lexer_peek_token (parser->lexer);
27907 t = cp_parser_assignment_expression (parser, false, NULL);
27909 if (t == error_mark_node)
27910 goto resync_fail;
27911 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27912 error_at (token->location, "schedule %<runtime%> does not take "
27913 "a %<chunk_size%> parameter");
27914 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27915 error_at (token->location, "schedule %<auto%> does not take "
27916 "a %<chunk_size%> parameter");
27917 else
27918 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27920 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27921 goto resync_fail;
27923 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27924 goto resync_fail;
27926 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27927 OMP_CLAUSE_CHAIN (c) = list;
27928 return c;
27930 invalid_kind:
27931 cp_parser_error (parser, "invalid schedule kind");
27932 resync_fail:
27933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27934 /*or_comma=*/false,
27935 /*consume_paren=*/true);
27936 return list;
27939 /* OpenMP 3.0:
27940 untied */
27942 static tree
27943 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27944 tree list, location_t location)
27946 tree c;
27948 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27950 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27951 OMP_CLAUSE_CHAIN (c) = list;
27952 return c;
27955 /* OpenMP 4.0:
27956 inbranch
27957 notinbranch */
27959 static tree
27960 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27961 tree list, location_t location)
27963 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27964 tree c = build_omp_clause (location, code);
27965 OMP_CLAUSE_CHAIN (c) = list;
27966 return c;
27969 /* OpenMP 4.0:
27970 parallel
27972 sections
27973 taskgroup */
27975 static tree
27976 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27977 enum omp_clause_code code,
27978 tree list, location_t location)
27980 tree c = build_omp_clause (location, code);
27981 OMP_CLAUSE_CHAIN (c) = list;
27982 return c;
27985 /* OpenMP 4.0:
27986 num_teams ( expression ) */
27988 static tree
27989 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27990 location_t location)
27992 tree t, c;
27994 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27995 return list;
27997 t = cp_parser_expression (parser);
27999 if (t == error_mark_node
28000 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28001 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28002 /*or_comma=*/false,
28003 /*consume_paren=*/true);
28005 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28006 "num_teams", location);
28008 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28009 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28010 OMP_CLAUSE_CHAIN (c) = list;
28012 return c;
28015 /* OpenMP 4.0:
28016 thread_limit ( expression ) */
28018 static tree
28019 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28020 location_t location)
28022 tree t, c;
28024 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28025 return list;
28027 t = cp_parser_expression (parser);
28029 if (t == error_mark_node
28030 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28031 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28032 /*or_comma=*/false,
28033 /*consume_paren=*/true);
28035 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28036 "thread_limit", location);
28038 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28039 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28040 OMP_CLAUSE_CHAIN (c) = list;
28042 return c;
28045 /* OpenMP 4.0:
28046 aligned ( variable-list )
28047 aligned ( variable-list : constant-expression ) */
28049 static tree
28050 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28052 tree nlist, c, alignment = NULL_TREE;
28053 bool colon;
28055 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28056 return list;
28058 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28059 &colon);
28061 if (colon)
28063 alignment = cp_parser_constant_expression (parser, false, NULL);
28065 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28066 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28067 /*or_comma=*/false,
28068 /*consume_paren=*/true);
28070 if (alignment == error_mark_node)
28071 alignment = NULL_TREE;
28074 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28075 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28077 return nlist;
28080 /* OpenMP 4.0:
28081 linear ( variable-list )
28082 linear ( variable-list : expression ) */
28084 static tree
28085 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28086 bool is_cilk_simd_fn)
28088 tree nlist, c, step = integer_one_node;
28089 bool colon;
28091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28092 return list;
28094 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28095 &colon);
28097 if (colon)
28099 step = cp_parser_expression (parser);
28101 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28103 sorry ("using parameters for %<linear%> step is not supported yet");
28104 step = integer_one_node;
28106 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28107 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28108 /*or_comma=*/false,
28109 /*consume_paren=*/true);
28111 if (step == error_mark_node)
28112 return list;
28115 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28116 OMP_CLAUSE_LINEAR_STEP (c) = step;
28118 return nlist;
28121 /* OpenMP 4.0:
28122 safelen ( constant-expression ) */
28124 static tree
28125 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28126 location_t location)
28128 tree t, c;
28130 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28131 return list;
28133 t = cp_parser_constant_expression (parser, false, NULL);
28135 if (t == error_mark_node
28136 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28137 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28138 /*or_comma=*/false,
28139 /*consume_paren=*/true);
28141 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28143 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28144 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28145 OMP_CLAUSE_CHAIN (c) = list;
28147 return c;
28150 /* OpenMP 4.0:
28151 simdlen ( constant-expression ) */
28153 static tree
28154 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28155 location_t location)
28157 tree t, c;
28159 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28160 return list;
28162 t = cp_parser_constant_expression (parser, false, NULL);
28164 if (t == error_mark_node
28165 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28166 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28167 /*or_comma=*/false,
28168 /*consume_paren=*/true);
28170 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28172 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28173 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28174 OMP_CLAUSE_CHAIN (c) = list;
28176 return c;
28179 /* OpenMP 4.0:
28180 depend ( depend-kind : variable-list )
28182 depend-kind:
28183 in | out | inout */
28185 static tree
28186 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28188 tree nlist, c;
28189 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28191 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28192 return list;
28194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28196 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28197 const char *p = IDENTIFIER_POINTER (id);
28199 if (strcmp ("in", p) == 0)
28200 kind = OMP_CLAUSE_DEPEND_IN;
28201 else if (strcmp ("inout", p) == 0)
28202 kind = OMP_CLAUSE_DEPEND_INOUT;
28203 else if (strcmp ("out", p) == 0)
28204 kind = OMP_CLAUSE_DEPEND_OUT;
28205 else
28206 goto invalid_kind;
28208 else
28209 goto invalid_kind;
28211 cp_lexer_consume_token (parser->lexer);
28212 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28213 goto resync_fail;
28215 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28216 NULL);
28218 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28219 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28221 return nlist;
28223 invalid_kind:
28224 cp_parser_error (parser, "invalid depend kind");
28225 resync_fail:
28226 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28227 /*or_comma=*/false,
28228 /*consume_paren=*/true);
28229 return list;
28232 /* OpenMP 4.0:
28233 map ( map-kind : variable-list )
28234 map ( variable-list )
28236 map-kind:
28237 alloc | to | from | tofrom */
28239 static tree
28240 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28242 tree nlist, c;
28243 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28245 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28246 return list;
28248 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28249 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28252 const char *p = IDENTIFIER_POINTER (id);
28254 if (strcmp ("alloc", p) == 0)
28255 kind = OMP_CLAUSE_MAP_ALLOC;
28256 else if (strcmp ("to", p) == 0)
28257 kind = OMP_CLAUSE_MAP_TO;
28258 else if (strcmp ("from", p) == 0)
28259 kind = OMP_CLAUSE_MAP_FROM;
28260 else if (strcmp ("tofrom", p) == 0)
28261 kind = OMP_CLAUSE_MAP_TOFROM;
28262 else
28264 cp_parser_error (parser, "invalid map kind");
28265 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28266 /*or_comma=*/false,
28267 /*consume_paren=*/true);
28268 return list;
28270 cp_lexer_consume_token (parser->lexer);
28271 cp_lexer_consume_token (parser->lexer);
28274 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28275 NULL);
28277 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28278 OMP_CLAUSE_MAP_KIND (c) = kind;
28280 return nlist;
28283 /* OpenMP 4.0:
28284 device ( expression ) */
28286 static tree
28287 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28288 location_t location)
28290 tree t, c;
28292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28293 return list;
28295 t = cp_parser_expression (parser);
28297 if (t == error_mark_node
28298 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28299 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28300 /*or_comma=*/false,
28301 /*consume_paren=*/true);
28303 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28304 "device", location);
28306 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28307 OMP_CLAUSE_DEVICE_ID (c) = t;
28308 OMP_CLAUSE_CHAIN (c) = list;
28310 return c;
28313 /* OpenMP 4.0:
28314 dist_schedule ( static )
28315 dist_schedule ( static , expression ) */
28317 static tree
28318 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28319 location_t location)
28321 tree c, t;
28323 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28324 return list;
28326 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28328 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28329 goto invalid_kind;
28330 cp_lexer_consume_token (parser->lexer);
28332 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28334 cp_lexer_consume_token (parser->lexer);
28336 t = cp_parser_assignment_expression (parser, false, NULL);
28338 if (t == error_mark_node)
28339 goto resync_fail;
28340 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28343 goto resync_fail;
28345 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28346 goto resync_fail;
28348 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28349 location);
28350 OMP_CLAUSE_CHAIN (c) = list;
28351 return c;
28353 invalid_kind:
28354 cp_parser_error (parser, "invalid dist_schedule kind");
28355 resync_fail:
28356 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28357 /*or_comma=*/false,
28358 /*consume_paren=*/true);
28359 return list;
28362 /* OpenMP 4.0:
28363 proc_bind ( proc-bind-kind )
28365 proc-bind-kind:
28366 master | close | spread */
28368 static tree
28369 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28370 location_t location)
28372 tree c;
28373 enum omp_clause_proc_bind_kind kind;
28375 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28376 return list;
28378 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28380 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28381 const char *p = IDENTIFIER_POINTER (id);
28383 if (strcmp ("master", p) == 0)
28384 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28385 else if (strcmp ("close", p) == 0)
28386 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28387 else if (strcmp ("spread", p) == 0)
28388 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28389 else
28390 goto invalid_kind;
28392 else
28393 goto invalid_kind;
28395 cp_lexer_consume_token (parser->lexer);
28396 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28397 goto resync_fail;
28399 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28400 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28401 location);
28402 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28403 OMP_CLAUSE_CHAIN (c) = list;
28404 return c;
28406 invalid_kind:
28407 cp_parser_error (parser, "invalid depend kind");
28408 resync_fail:
28409 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28410 /*or_comma=*/false,
28411 /*consume_paren=*/true);
28412 return list;
28415 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28416 is a bitmask in MASK. Return the list of clauses found; the result
28417 of clause default goes in *pdefault. */
28419 static tree
28420 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28421 const char *where, cp_token *pragma_tok,
28422 bool finish_p = true)
28424 tree clauses = NULL;
28425 bool first = true;
28426 cp_token *token = NULL;
28427 bool cilk_simd_fn = false;
28429 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28431 pragma_omp_clause c_kind;
28432 const char *c_name;
28433 tree prev = clauses;
28435 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28436 cp_lexer_consume_token (parser->lexer);
28438 token = cp_lexer_peek_token (parser->lexer);
28439 c_kind = cp_parser_omp_clause_name (parser);
28441 switch (c_kind)
28443 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28444 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28445 token->location);
28446 c_name = "collapse";
28447 break;
28448 case PRAGMA_OMP_CLAUSE_COPYIN:
28449 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28450 c_name = "copyin";
28451 break;
28452 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28453 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28454 clauses);
28455 c_name = "copyprivate";
28456 break;
28457 case PRAGMA_OMP_CLAUSE_DEFAULT:
28458 clauses = cp_parser_omp_clause_default (parser, clauses,
28459 token->location);
28460 c_name = "default";
28461 break;
28462 case PRAGMA_OMP_CLAUSE_FINAL:
28463 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28464 c_name = "final";
28465 break;
28466 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28467 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28468 clauses);
28469 c_name = "firstprivate";
28470 break;
28471 case PRAGMA_OMP_CLAUSE_IF:
28472 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28473 c_name = "if";
28474 break;
28475 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28476 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28477 clauses);
28478 c_name = "lastprivate";
28479 break;
28480 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28481 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28482 token->location);
28483 c_name = "mergeable";
28484 break;
28485 case PRAGMA_OMP_CLAUSE_NOWAIT:
28486 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28487 c_name = "nowait";
28488 break;
28489 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28490 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28491 token->location);
28492 c_name = "num_threads";
28493 break;
28494 case PRAGMA_OMP_CLAUSE_ORDERED:
28495 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28496 token->location);
28497 c_name = "ordered";
28498 break;
28499 case PRAGMA_OMP_CLAUSE_PRIVATE:
28500 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28501 clauses);
28502 c_name = "private";
28503 break;
28504 case PRAGMA_OMP_CLAUSE_REDUCTION:
28505 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28506 c_name = "reduction";
28507 break;
28508 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28509 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28510 token->location);
28511 c_name = "schedule";
28512 break;
28513 case PRAGMA_OMP_CLAUSE_SHARED:
28514 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28515 clauses);
28516 c_name = "shared";
28517 break;
28518 case PRAGMA_OMP_CLAUSE_UNTIED:
28519 clauses = cp_parser_omp_clause_untied (parser, clauses,
28520 token->location);
28521 c_name = "untied";
28522 break;
28523 case PRAGMA_OMP_CLAUSE_INBRANCH:
28524 case PRAGMA_CILK_CLAUSE_MASK:
28525 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28526 clauses, token->location);
28527 c_name = "inbranch";
28528 break;
28529 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28530 case PRAGMA_CILK_CLAUSE_NOMASK:
28531 clauses = cp_parser_omp_clause_branch (parser,
28532 OMP_CLAUSE_NOTINBRANCH,
28533 clauses, token->location);
28534 c_name = "notinbranch";
28535 break;
28536 case PRAGMA_OMP_CLAUSE_PARALLEL:
28537 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28538 clauses, token->location);
28539 c_name = "parallel";
28540 if (!first)
28542 clause_not_first:
28543 error_at (token->location, "%qs must be the first clause of %qs",
28544 c_name, where);
28545 clauses = prev;
28547 break;
28548 case PRAGMA_OMP_CLAUSE_FOR:
28549 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28550 clauses, token->location);
28551 c_name = "for";
28552 if (!first)
28553 goto clause_not_first;
28554 break;
28555 case PRAGMA_OMP_CLAUSE_SECTIONS:
28556 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28557 clauses, token->location);
28558 c_name = "sections";
28559 if (!first)
28560 goto clause_not_first;
28561 break;
28562 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28563 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28564 clauses, token->location);
28565 c_name = "taskgroup";
28566 if (!first)
28567 goto clause_not_first;
28568 break;
28569 case PRAGMA_OMP_CLAUSE_TO:
28570 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28571 clauses);
28572 c_name = "to";
28573 break;
28574 case PRAGMA_OMP_CLAUSE_FROM:
28575 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28576 clauses);
28577 c_name = "from";
28578 break;
28579 case PRAGMA_OMP_CLAUSE_UNIFORM:
28580 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28581 clauses);
28582 c_name = "uniform";
28583 break;
28584 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28585 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28586 token->location);
28587 c_name = "num_teams";
28588 break;
28589 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28590 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28591 token->location);
28592 c_name = "thread_limit";
28593 break;
28594 case PRAGMA_OMP_CLAUSE_ALIGNED:
28595 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28596 c_name = "aligned";
28597 break;
28598 case PRAGMA_OMP_CLAUSE_LINEAR:
28599 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28600 cilk_simd_fn = true;
28601 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28602 c_name = "linear";
28603 break;
28604 case PRAGMA_OMP_CLAUSE_DEPEND:
28605 clauses = cp_parser_omp_clause_depend (parser, clauses);
28606 c_name = "depend";
28607 break;
28608 case PRAGMA_OMP_CLAUSE_MAP:
28609 clauses = cp_parser_omp_clause_map (parser, clauses);
28610 c_name = "map";
28611 break;
28612 case PRAGMA_OMP_CLAUSE_DEVICE:
28613 clauses = cp_parser_omp_clause_device (parser, clauses,
28614 token->location);
28615 c_name = "device";
28616 break;
28617 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28618 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28619 token->location);
28620 c_name = "dist_schedule";
28621 break;
28622 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28623 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28624 token->location);
28625 c_name = "proc_bind";
28626 break;
28627 case PRAGMA_OMP_CLAUSE_SAFELEN:
28628 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28629 token->location);
28630 c_name = "safelen";
28631 break;
28632 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28633 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28634 token->location);
28635 c_name = "simdlen";
28636 break;
28637 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28638 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28639 c_name = "simdlen";
28640 break;
28641 default:
28642 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28643 goto saw_error;
28646 first = false;
28648 if (((mask >> c_kind) & 1) == 0)
28650 /* Remove the invalid clause(s) from the list to avoid
28651 confusing the rest of the compiler. */
28652 clauses = prev;
28653 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28656 saw_error:
28657 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28658 no reason to skip to the end. */
28659 if (!(flag_cilkplus && pragma_tok == NULL))
28660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28661 if (finish_p)
28662 return finish_omp_clauses (clauses);
28663 return clauses;
28666 /* OpenMP 2.5:
28667 structured-block:
28668 statement
28670 In practice, we're also interested in adding the statement to an
28671 outer node. So it is convenient if we work around the fact that
28672 cp_parser_statement calls add_stmt. */
28674 static unsigned
28675 cp_parser_begin_omp_structured_block (cp_parser *parser)
28677 unsigned save = parser->in_statement;
28679 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28680 This preserves the "not within loop or switch" style error messages
28681 for nonsense cases like
28682 void foo() {
28683 #pragma omp single
28684 break;
28687 if (parser->in_statement)
28688 parser->in_statement = IN_OMP_BLOCK;
28690 return save;
28693 static void
28694 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28696 parser->in_statement = save;
28699 static tree
28700 cp_parser_omp_structured_block (cp_parser *parser)
28702 tree stmt = begin_omp_structured_block ();
28703 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28705 cp_parser_statement (parser, NULL_TREE, false, NULL);
28707 cp_parser_end_omp_structured_block (parser, save);
28708 return finish_omp_structured_block (stmt);
28711 /* OpenMP 2.5:
28712 # pragma omp atomic new-line
28713 expression-stmt
28715 expression-stmt:
28716 x binop= expr | x++ | ++x | x-- | --x
28717 binop:
28718 +, *, -, /, &, ^, |, <<, >>
28720 where x is an lvalue expression with scalar type.
28722 OpenMP 3.1:
28723 # pragma omp atomic new-line
28724 update-stmt
28726 # pragma omp atomic read new-line
28727 read-stmt
28729 # pragma omp atomic write new-line
28730 write-stmt
28732 # pragma omp atomic update new-line
28733 update-stmt
28735 # pragma omp atomic capture new-line
28736 capture-stmt
28738 # pragma omp atomic capture new-line
28739 capture-block
28741 read-stmt:
28742 v = x
28743 write-stmt:
28744 x = expr
28745 update-stmt:
28746 expression-stmt | x = x binop expr
28747 capture-stmt:
28748 v = expression-stmt
28749 capture-block:
28750 { v = x; update-stmt; } | { update-stmt; v = x; }
28752 OpenMP 4.0:
28753 update-stmt:
28754 expression-stmt | x = x binop expr | x = expr binop x
28755 capture-stmt:
28756 v = update-stmt
28757 capture-block:
28758 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28760 where x and v are lvalue expressions with scalar type. */
28762 static void
28763 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28765 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28766 tree rhs1 = NULL_TREE, orig_lhs;
28767 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28768 bool structured_block = false;
28769 bool seq_cst = false;
28771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28773 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28774 const char *p = IDENTIFIER_POINTER (id);
28776 if (!strcmp (p, "seq_cst"))
28778 seq_cst = true;
28779 cp_lexer_consume_token (parser->lexer);
28780 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28781 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28782 cp_lexer_consume_token (parser->lexer);
28785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28788 const char *p = IDENTIFIER_POINTER (id);
28790 if (!strcmp (p, "read"))
28791 code = OMP_ATOMIC_READ;
28792 else if (!strcmp (p, "write"))
28793 code = NOP_EXPR;
28794 else if (!strcmp (p, "update"))
28795 code = OMP_ATOMIC;
28796 else if (!strcmp (p, "capture"))
28797 code = OMP_ATOMIC_CAPTURE_NEW;
28798 else
28799 p = NULL;
28800 if (p)
28801 cp_lexer_consume_token (parser->lexer);
28803 if (!seq_cst)
28805 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28806 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28807 cp_lexer_consume_token (parser->lexer);
28809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28811 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28812 const char *p = IDENTIFIER_POINTER (id);
28814 if (!strcmp (p, "seq_cst"))
28816 seq_cst = true;
28817 cp_lexer_consume_token (parser->lexer);
28821 cp_parser_require_pragma_eol (parser, pragma_tok);
28823 switch (code)
28825 case OMP_ATOMIC_READ:
28826 case NOP_EXPR: /* atomic write */
28827 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28828 /*cast_p=*/false, NULL);
28829 if (v == error_mark_node)
28830 goto saw_error;
28831 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28832 goto saw_error;
28833 if (code == NOP_EXPR)
28834 lhs = cp_parser_expression (parser);
28835 else
28836 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28837 /*cast_p=*/false, NULL);
28838 if (lhs == error_mark_node)
28839 goto saw_error;
28840 if (code == NOP_EXPR)
28842 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28843 opcode. */
28844 code = OMP_ATOMIC;
28845 rhs = lhs;
28846 lhs = v;
28847 v = NULL_TREE;
28849 goto done;
28850 case OMP_ATOMIC_CAPTURE_NEW:
28851 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28853 cp_lexer_consume_token (parser->lexer);
28854 structured_block = true;
28856 else
28858 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28859 /*cast_p=*/false, NULL);
28860 if (v == error_mark_node)
28861 goto saw_error;
28862 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28863 goto saw_error;
28865 default:
28866 break;
28869 restart:
28870 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28871 /*cast_p=*/false, NULL);
28872 orig_lhs = lhs;
28873 switch (TREE_CODE (lhs))
28875 case ERROR_MARK:
28876 goto saw_error;
28878 case POSTINCREMENT_EXPR:
28879 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28880 code = OMP_ATOMIC_CAPTURE_OLD;
28881 /* FALLTHROUGH */
28882 case PREINCREMENT_EXPR:
28883 lhs = TREE_OPERAND (lhs, 0);
28884 opcode = PLUS_EXPR;
28885 rhs = integer_one_node;
28886 break;
28888 case POSTDECREMENT_EXPR:
28889 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28890 code = OMP_ATOMIC_CAPTURE_OLD;
28891 /* FALLTHROUGH */
28892 case PREDECREMENT_EXPR:
28893 lhs = TREE_OPERAND (lhs, 0);
28894 opcode = MINUS_EXPR;
28895 rhs = integer_one_node;
28896 break;
28898 case COMPOUND_EXPR:
28899 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28900 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28901 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28902 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28903 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28904 (TREE_OPERAND (lhs, 1), 0), 0)))
28905 == BOOLEAN_TYPE)
28906 /* Undo effects of boolean_increment for post {in,de}crement. */
28907 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28908 /* FALLTHRU */
28909 case MODIFY_EXPR:
28910 if (TREE_CODE (lhs) == MODIFY_EXPR
28911 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28913 /* Undo effects of boolean_increment. */
28914 if (integer_onep (TREE_OPERAND (lhs, 1)))
28916 /* This is pre or post increment. */
28917 rhs = TREE_OPERAND (lhs, 1);
28918 lhs = TREE_OPERAND (lhs, 0);
28919 opcode = NOP_EXPR;
28920 if (code == OMP_ATOMIC_CAPTURE_NEW
28921 && !structured_block
28922 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28923 code = OMP_ATOMIC_CAPTURE_OLD;
28924 break;
28927 /* FALLTHRU */
28928 default:
28929 switch (cp_lexer_peek_token (parser->lexer)->type)
28931 case CPP_MULT_EQ:
28932 opcode = MULT_EXPR;
28933 break;
28934 case CPP_DIV_EQ:
28935 opcode = TRUNC_DIV_EXPR;
28936 break;
28937 case CPP_PLUS_EQ:
28938 opcode = PLUS_EXPR;
28939 break;
28940 case CPP_MINUS_EQ:
28941 opcode = MINUS_EXPR;
28942 break;
28943 case CPP_LSHIFT_EQ:
28944 opcode = LSHIFT_EXPR;
28945 break;
28946 case CPP_RSHIFT_EQ:
28947 opcode = RSHIFT_EXPR;
28948 break;
28949 case CPP_AND_EQ:
28950 opcode = BIT_AND_EXPR;
28951 break;
28952 case CPP_OR_EQ:
28953 opcode = BIT_IOR_EXPR;
28954 break;
28955 case CPP_XOR_EQ:
28956 opcode = BIT_XOR_EXPR;
28957 break;
28958 case CPP_EQ:
28959 enum cp_parser_prec oprec;
28960 cp_token *token;
28961 cp_lexer_consume_token (parser->lexer);
28962 cp_parser_parse_tentatively (parser);
28963 rhs1 = cp_parser_simple_cast_expression (parser);
28964 if (rhs1 == error_mark_node)
28966 cp_parser_abort_tentative_parse (parser);
28967 cp_parser_simple_cast_expression (parser);
28968 goto saw_error;
28970 token = cp_lexer_peek_token (parser->lexer);
28971 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28973 cp_parser_abort_tentative_parse (parser);
28974 cp_parser_parse_tentatively (parser);
28975 rhs = cp_parser_binary_expression (parser, false, true,
28976 PREC_NOT_OPERATOR, NULL);
28977 if (rhs == error_mark_node)
28979 cp_parser_abort_tentative_parse (parser);
28980 cp_parser_binary_expression (parser, false, true,
28981 PREC_NOT_OPERATOR, NULL);
28982 goto saw_error;
28984 switch (TREE_CODE (rhs))
28986 case MULT_EXPR:
28987 case TRUNC_DIV_EXPR:
28988 case PLUS_EXPR:
28989 case MINUS_EXPR:
28990 case LSHIFT_EXPR:
28991 case RSHIFT_EXPR:
28992 case BIT_AND_EXPR:
28993 case BIT_IOR_EXPR:
28994 case BIT_XOR_EXPR:
28995 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28997 if (cp_parser_parse_definitely (parser))
28999 opcode = TREE_CODE (rhs);
29000 rhs1 = TREE_OPERAND (rhs, 0);
29001 rhs = TREE_OPERAND (rhs, 1);
29002 goto stmt_done;
29004 else
29005 goto saw_error;
29007 break;
29008 default:
29009 break;
29011 cp_parser_abort_tentative_parse (parser);
29012 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29014 rhs = cp_parser_expression (parser);
29015 if (rhs == error_mark_node)
29016 goto saw_error;
29017 opcode = NOP_EXPR;
29018 rhs1 = NULL_TREE;
29019 goto stmt_done;
29021 cp_parser_error (parser,
29022 "invalid form of %<#pragma omp atomic%>");
29023 goto saw_error;
29025 if (!cp_parser_parse_definitely (parser))
29026 goto saw_error;
29027 switch (token->type)
29029 case CPP_SEMICOLON:
29030 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29032 code = OMP_ATOMIC_CAPTURE_OLD;
29033 v = lhs;
29034 lhs = NULL_TREE;
29035 lhs1 = rhs1;
29036 rhs1 = NULL_TREE;
29037 cp_lexer_consume_token (parser->lexer);
29038 goto restart;
29040 else if (structured_block)
29042 opcode = NOP_EXPR;
29043 rhs = rhs1;
29044 rhs1 = NULL_TREE;
29045 goto stmt_done;
29047 cp_parser_error (parser,
29048 "invalid form of %<#pragma omp atomic%>");
29049 goto saw_error;
29050 case CPP_MULT:
29051 opcode = MULT_EXPR;
29052 break;
29053 case CPP_DIV:
29054 opcode = TRUNC_DIV_EXPR;
29055 break;
29056 case CPP_PLUS:
29057 opcode = PLUS_EXPR;
29058 break;
29059 case CPP_MINUS:
29060 opcode = MINUS_EXPR;
29061 break;
29062 case CPP_LSHIFT:
29063 opcode = LSHIFT_EXPR;
29064 break;
29065 case CPP_RSHIFT:
29066 opcode = RSHIFT_EXPR;
29067 break;
29068 case CPP_AND:
29069 opcode = BIT_AND_EXPR;
29070 break;
29071 case CPP_OR:
29072 opcode = BIT_IOR_EXPR;
29073 break;
29074 case CPP_XOR:
29075 opcode = BIT_XOR_EXPR;
29076 break;
29077 default:
29078 cp_parser_error (parser,
29079 "invalid operator for %<#pragma omp atomic%>");
29080 goto saw_error;
29082 oprec = TOKEN_PRECEDENCE (token);
29083 gcc_assert (oprec != PREC_NOT_OPERATOR);
29084 if (commutative_tree_code (opcode))
29085 oprec = (enum cp_parser_prec) (oprec - 1);
29086 cp_lexer_consume_token (parser->lexer);
29087 rhs = cp_parser_binary_expression (parser, false, false,
29088 oprec, NULL);
29089 if (rhs == error_mark_node)
29090 goto saw_error;
29091 goto stmt_done;
29092 /* FALLTHROUGH */
29093 default:
29094 cp_parser_error (parser,
29095 "invalid operator for %<#pragma omp atomic%>");
29096 goto saw_error;
29098 cp_lexer_consume_token (parser->lexer);
29100 rhs = cp_parser_expression (parser);
29101 if (rhs == error_mark_node)
29102 goto saw_error;
29103 break;
29105 stmt_done:
29106 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29108 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29109 goto saw_error;
29110 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29111 /*cast_p=*/false, NULL);
29112 if (v == error_mark_node)
29113 goto saw_error;
29114 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29115 goto saw_error;
29116 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29117 /*cast_p=*/false, NULL);
29118 if (lhs1 == error_mark_node)
29119 goto saw_error;
29121 if (structured_block)
29123 cp_parser_consume_semicolon_at_end_of_statement (parser);
29124 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29126 done:
29127 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29128 if (!structured_block)
29129 cp_parser_consume_semicolon_at_end_of_statement (parser);
29130 return;
29132 saw_error:
29133 cp_parser_skip_to_end_of_block_or_statement (parser);
29134 if (structured_block)
29136 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29137 cp_lexer_consume_token (parser->lexer);
29138 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29140 cp_parser_skip_to_end_of_block_or_statement (parser);
29141 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29142 cp_lexer_consume_token (parser->lexer);
29148 /* OpenMP 2.5:
29149 # pragma omp barrier new-line */
29151 static void
29152 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29154 cp_parser_require_pragma_eol (parser, pragma_tok);
29155 finish_omp_barrier ();
29158 /* OpenMP 2.5:
29159 # pragma omp critical [(name)] new-line
29160 structured-block */
29162 static tree
29163 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29165 tree stmt, name = NULL;
29167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29169 cp_lexer_consume_token (parser->lexer);
29171 name = cp_parser_identifier (parser);
29173 if (name == error_mark_node
29174 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29175 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29176 /*or_comma=*/false,
29177 /*consume_paren=*/true);
29178 if (name == error_mark_node)
29179 name = NULL;
29181 cp_parser_require_pragma_eol (parser, pragma_tok);
29183 stmt = cp_parser_omp_structured_block (parser);
29184 return c_finish_omp_critical (input_location, stmt, name);
29187 /* OpenMP 2.5:
29188 # pragma omp flush flush-vars[opt] new-line
29190 flush-vars:
29191 ( variable-list ) */
29193 static void
29194 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29197 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29198 cp_parser_require_pragma_eol (parser, pragma_tok);
29200 finish_omp_flush ();
29203 /* Helper function, to parse omp for increment expression. */
29205 static tree
29206 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29208 tree cond = cp_parser_binary_expression (parser, false, true,
29209 PREC_NOT_OPERATOR, NULL);
29210 if (cond == error_mark_node
29211 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29213 cp_parser_skip_to_end_of_statement (parser);
29214 return error_mark_node;
29217 switch (TREE_CODE (cond))
29219 case GT_EXPR:
29220 case GE_EXPR:
29221 case LT_EXPR:
29222 case LE_EXPR:
29223 break;
29224 case NE_EXPR:
29225 if (code == CILK_SIMD)
29226 break;
29227 /* Fall through: OpenMP disallows NE_EXPR. */
29228 default:
29229 return error_mark_node;
29232 /* If decl is an iterator, preserve LHS and RHS of the relational
29233 expr until finish_omp_for. */
29234 if (decl
29235 && (type_dependent_expression_p (decl)
29236 || CLASS_TYPE_P (TREE_TYPE (decl))))
29237 return cond;
29239 return build_x_binary_op (input_location, TREE_CODE (cond),
29240 TREE_OPERAND (cond, 0), ERROR_MARK,
29241 TREE_OPERAND (cond, 1), ERROR_MARK,
29242 /*overload=*/NULL, tf_warning_or_error);
29245 /* Helper function, to parse omp for increment expression. */
29247 static tree
29248 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29250 cp_token *token = cp_lexer_peek_token (parser->lexer);
29251 enum tree_code op;
29252 tree lhs, rhs;
29253 cp_id_kind idk;
29254 bool decl_first;
29256 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29258 op = (token->type == CPP_PLUS_PLUS
29259 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29260 cp_lexer_consume_token (parser->lexer);
29261 lhs = cp_parser_simple_cast_expression (parser);
29262 if (lhs != decl)
29263 return error_mark_node;
29264 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29267 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29268 if (lhs != decl)
29269 return error_mark_node;
29271 token = cp_lexer_peek_token (parser->lexer);
29272 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29274 op = (token->type == CPP_PLUS_PLUS
29275 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29276 cp_lexer_consume_token (parser->lexer);
29277 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29280 op = cp_parser_assignment_operator_opt (parser);
29281 if (op == ERROR_MARK)
29282 return error_mark_node;
29284 if (op != NOP_EXPR)
29286 rhs = cp_parser_assignment_expression (parser, false, NULL);
29287 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29288 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29291 lhs = cp_parser_binary_expression (parser, false, false,
29292 PREC_ADDITIVE_EXPRESSION, NULL);
29293 token = cp_lexer_peek_token (parser->lexer);
29294 decl_first = lhs == decl;
29295 if (decl_first)
29296 lhs = NULL_TREE;
29297 if (token->type != CPP_PLUS
29298 && token->type != CPP_MINUS)
29299 return error_mark_node;
29303 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29304 cp_lexer_consume_token (parser->lexer);
29305 rhs = cp_parser_binary_expression (parser, false, false,
29306 PREC_ADDITIVE_EXPRESSION, NULL);
29307 token = cp_lexer_peek_token (parser->lexer);
29308 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29310 if (lhs == NULL_TREE)
29312 if (op == PLUS_EXPR)
29313 lhs = rhs;
29314 else
29315 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29316 tf_warning_or_error);
29318 else
29319 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29320 ERROR_MARK, NULL, tf_warning_or_error);
29323 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29325 if (!decl_first)
29327 if (rhs != decl || op == MINUS_EXPR)
29328 return error_mark_node;
29329 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29331 else
29332 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29334 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29337 /* Parse the initialization statement of either an OpenMP for loop or
29338 a Cilk Plus for loop.
29340 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29341 Plus.
29343 Return true if the resulting construct should have an
29344 OMP_CLAUSE_PRIVATE added to it. */
29346 static bool
29347 cp_parser_omp_for_loop_init (cp_parser *parser,
29348 bool parsing_openmp,
29349 tree &this_pre_body,
29350 vec<tree, va_gc> *for_block,
29351 tree &init,
29352 tree &decl,
29353 tree &real_decl)
29355 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29356 return false;
29358 bool add_private_clause = false;
29360 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29362 init-expr:
29363 var = lb
29364 integer-type var = lb
29365 random-access-iterator-type var = lb
29366 pointer-type var = lb
29368 cp_decl_specifier_seq type_specifiers;
29370 /* First, try to parse as an initialized declaration. See
29371 cp_parser_condition, from whence the bulk of this is copied. */
29373 cp_parser_parse_tentatively (parser);
29374 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29375 /*is_trailing_return=*/false,
29376 &type_specifiers);
29377 if (cp_parser_parse_definitely (parser))
29379 /* If parsing a type specifier seq succeeded, then this
29380 MUST be a initialized declaration. */
29381 tree asm_specification, attributes;
29382 cp_declarator *declarator;
29384 declarator = cp_parser_declarator (parser,
29385 CP_PARSER_DECLARATOR_NAMED,
29386 /*ctor_dtor_or_conv_p=*/NULL,
29387 /*parenthesized_p=*/NULL,
29388 /*member_p=*/false,
29389 /*friend_p=*/false);
29390 attributes = cp_parser_attributes_opt (parser);
29391 asm_specification = cp_parser_asm_specification_opt (parser);
29393 if (declarator == cp_error_declarator)
29394 cp_parser_skip_to_end_of_statement (parser);
29396 else
29398 tree pushed_scope, auto_node;
29400 decl = start_decl (declarator, &type_specifiers,
29401 SD_INITIALIZED, attributes,
29402 /*prefix_attributes=*/NULL_TREE,
29403 &pushed_scope);
29405 auto_node = type_uses_auto (TREE_TYPE (decl));
29406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29408 if (cp_lexer_next_token_is (parser->lexer,
29409 CPP_OPEN_PAREN))
29411 if (parsing_openmp)
29412 error ("parenthesized initialization is not allowed in "
29413 "OpenMP %<for%> loop");
29414 else
29415 error ("parenthesized initialization is "
29416 "not allowed in for-loop");
29418 else
29419 /* Trigger an error. */
29420 cp_parser_require (parser, CPP_EQ, RT_EQ);
29422 init = error_mark_node;
29423 cp_parser_skip_to_end_of_statement (parser);
29425 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29426 || type_dependent_expression_p (decl)
29427 || auto_node)
29429 bool is_direct_init, is_non_constant_init;
29431 init = cp_parser_initializer (parser,
29432 &is_direct_init,
29433 &is_non_constant_init);
29435 if (auto_node)
29437 TREE_TYPE (decl)
29438 = do_auto_deduction (TREE_TYPE (decl), init,
29439 auto_node);
29441 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29442 && !type_dependent_expression_p (decl))
29443 goto non_class;
29446 cp_finish_decl (decl, init, !is_non_constant_init,
29447 asm_specification,
29448 LOOKUP_ONLYCONVERTING);
29449 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29451 vec_safe_push (for_block, this_pre_body);
29452 init = NULL_TREE;
29454 else
29455 init = pop_stmt_list (this_pre_body);
29456 this_pre_body = NULL_TREE;
29458 else
29460 /* Consume '='. */
29461 cp_lexer_consume_token (parser->lexer);
29462 init = cp_parser_assignment_expression (parser, false, NULL);
29464 non_class:
29465 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29466 init = error_mark_node;
29467 else
29468 cp_finish_decl (decl, NULL_TREE,
29469 /*init_const_expr_p=*/false,
29470 asm_specification,
29471 LOOKUP_ONLYCONVERTING);
29474 if (pushed_scope)
29475 pop_scope (pushed_scope);
29478 else
29480 cp_id_kind idk;
29481 /* If parsing a type specifier sequence failed, then
29482 this MUST be a simple expression. */
29483 cp_parser_parse_tentatively (parser);
29484 decl = cp_parser_primary_expression (parser, false, false,
29485 false, &idk);
29486 if (!cp_parser_error_occurred (parser)
29487 && decl
29488 && DECL_P (decl)
29489 && CLASS_TYPE_P (TREE_TYPE (decl)))
29491 tree rhs;
29493 cp_parser_parse_definitely (parser);
29494 cp_parser_require (parser, CPP_EQ, RT_EQ);
29495 rhs = cp_parser_assignment_expression (parser, false, NULL);
29496 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29497 decl, NOP_EXPR,
29498 rhs,
29499 tf_warning_or_error));
29500 add_private_clause = true;
29502 else
29504 decl = NULL;
29505 cp_parser_abort_tentative_parse (parser);
29506 init = cp_parser_expression (parser);
29507 if (init)
29509 if (TREE_CODE (init) == MODIFY_EXPR
29510 || TREE_CODE (init) == MODOP_EXPR)
29511 real_decl = TREE_OPERAND (init, 0);
29515 return add_private_clause;
29518 /* Parse the restricted form of the for statement allowed by OpenMP. */
29520 static tree
29521 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29522 tree *cclauses)
29524 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29525 tree real_decl, initv, condv, incrv, declv;
29526 tree this_pre_body, cl;
29527 location_t loc_first;
29528 bool collapse_err = false;
29529 int i, collapse = 1, nbraces = 0;
29530 vec<tree, va_gc> *for_block = make_tree_vector ();
29532 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29533 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29534 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29536 gcc_assert (collapse >= 1);
29538 declv = make_tree_vec (collapse);
29539 initv = make_tree_vec (collapse);
29540 condv = make_tree_vec (collapse);
29541 incrv = make_tree_vec (collapse);
29543 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29545 for (i = 0; i < collapse; i++)
29547 int bracecount = 0;
29548 bool add_private_clause = false;
29549 location_t loc;
29551 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29553 cp_parser_error (parser, "for statement expected");
29554 return NULL;
29556 loc = cp_lexer_consume_token (parser->lexer)->location;
29558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29559 return NULL;
29561 init = decl = real_decl = NULL;
29562 this_pre_body = push_stmt_list ();
29564 add_private_clause
29565 |= cp_parser_omp_for_loop_init (parser,
29566 /*parsing_openmp=*/code != CILK_SIMD,
29567 this_pre_body, for_block,
29568 init, decl, real_decl);
29570 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29571 if (this_pre_body)
29573 this_pre_body = pop_stmt_list (this_pre_body);
29574 if (pre_body)
29576 tree t = pre_body;
29577 pre_body = push_stmt_list ();
29578 add_stmt (t);
29579 add_stmt (this_pre_body);
29580 pre_body = pop_stmt_list (pre_body);
29582 else
29583 pre_body = this_pre_body;
29586 if (decl)
29587 real_decl = decl;
29588 if (cclauses != NULL
29589 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29590 && real_decl != NULL_TREE)
29592 tree *c;
29593 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29594 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29595 && OMP_CLAUSE_DECL (*c) == real_decl)
29597 error_at (loc, "iteration variable %qD"
29598 " should not be firstprivate", real_decl);
29599 *c = OMP_CLAUSE_CHAIN (*c);
29601 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29602 && OMP_CLAUSE_DECL (*c) == real_decl)
29604 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29605 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29606 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29607 OMP_CLAUSE_DECL (l) = real_decl;
29608 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29609 if (code == OMP_SIMD)
29611 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29612 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29614 else
29616 OMP_CLAUSE_CHAIN (l) = clauses;
29617 clauses = l;
29619 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29620 CP_OMP_CLAUSE_INFO (*c) = NULL;
29621 add_private_clause = false;
29623 else
29625 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29626 && OMP_CLAUSE_DECL (*c) == real_decl)
29627 add_private_clause = false;
29628 c = &OMP_CLAUSE_CHAIN (*c);
29632 if (add_private_clause)
29634 tree c;
29635 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29637 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29638 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29639 && OMP_CLAUSE_DECL (c) == decl)
29640 break;
29641 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29642 && OMP_CLAUSE_DECL (c) == decl)
29643 error_at (loc, "iteration variable %qD "
29644 "should not be firstprivate",
29645 decl);
29646 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29647 && OMP_CLAUSE_DECL (c) == decl)
29648 error_at (loc, "iteration variable %qD should not be reduction",
29649 decl);
29651 if (c == NULL)
29653 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29654 OMP_CLAUSE_DECL (c) = decl;
29655 c = finish_omp_clauses (c);
29656 if (c)
29658 OMP_CLAUSE_CHAIN (c) = clauses;
29659 clauses = c;
29664 cond = NULL;
29665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29666 cond = cp_parser_omp_for_cond (parser, decl, code);
29667 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29669 incr = NULL;
29670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29672 /* If decl is an iterator, preserve the operator on decl
29673 until finish_omp_for. */
29674 if (real_decl
29675 && ((processing_template_decl
29676 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29677 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29678 incr = cp_parser_omp_for_incr (parser, real_decl);
29679 else
29680 incr = cp_parser_expression (parser);
29681 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29682 SET_EXPR_LOCATION (incr, input_location);
29685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29687 /*or_comma=*/false,
29688 /*consume_paren=*/true);
29690 TREE_VEC_ELT (declv, i) = decl;
29691 TREE_VEC_ELT (initv, i) = init;
29692 TREE_VEC_ELT (condv, i) = cond;
29693 TREE_VEC_ELT (incrv, i) = incr;
29695 if (i == collapse - 1)
29696 break;
29698 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29699 in between the collapsed for loops to be still considered perfectly
29700 nested. Hopefully the final version clarifies this.
29701 For now handle (multiple) {'s and empty statements. */
29702 cp_parser_parse_tentatively (parser);
29705 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29706 break;
29707 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29709 cp_lexer_consume_token (parser->lexer);
29710 bracecount++;
29712 else if (bracecount
29713 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29714 cp_lexer_consume_token (parser->lexer);
29715 else
29717 loc = cp_lexer_peek_token (parser->lexer)->location;
29718 error_at (loc, "not enough collapsed for loops");
29719 collapse_err = true;
29720 cp_parser_abort_tentative_parse (parser);
29721 declv = NULL_TREE;
29722 break;
29725 while (1);
29727 if (declv)
29729 cp_parser_parse_definitely (parser);
29730 nbraces += bracecount;
29734 /* Note that we saved the original contents of this flag when we entered
29735 the structured block, and so we don't need to re-save it here. */
29736 if (code == CILK_SIMD)
29737 parser->in_statement = IN_CILK_SIMD_FOR;
29738 else
29739 parser->in_statement = IN_OMP_FOR;
29741 /* Note that the grammar doesn't call for a structured block here,
29742 though the loop as a whole is a structured block. */
29743 body = push_stmt_list ();
29744 cp_parser_statement (parser, NULL_TREE, false, NULL);
29745 body = pop_stmt_list (body);
29747 if (declv == NULL_TREE)
29748 ret = NULL_TREE;
29749 else
29750 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29751 pre_body, clauses);
29753 while (nbraces)
29755 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29757 cp_lexer_consume_token (parser->lexer);
29758 nbraces--;
29760 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29761 cp_lexer_consume_token (parser->lexer);
29762 else
29764 if (!collapse_err)
29766 error_at (cp_lexer_peek_token (parser->lexer)->location,
29767 "collapsed loops not perfectly nested");
29769 collapse_err = true;
29770 cp_parser_statement_seq_opt (parser, NULL);
29771 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29772 break;
29776 while (!for_block->is_empty ())
29777 add_stmt (pop_stmt_list (for_block->pop ()));
29778 release_tree_vector (for_block);
29780 return ret;
29783 /* Helper function for OpenMP parsing, split clauses and call
29784 finish_omp_clauses on each of the set of clauses afterwards. */
29786 static void
29787 cp_omp_split_clauses (location_t loc, enum tree_code code,
29788 omp_clause_mask mask, tree clauses, tree *cclauses)
29790 int i;
29791 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29792 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29793 if (cclauses[i])
29794 cclauses[i] = finish_omp_clauses (cclauses[i]);
29797 /* OpenMP 4.0:
29798 #pragma omp simd simd-clause[optseq] new-line
29799 for-loop */
29801 #define OMP_SIMD_CLAUSE_MASK \
29802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29810 static tree
29811 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29812 char *p_name, omp_clause_mask mask, tree *cclauses)
29814 tree clauses, sb, ret;
29815 unsigned int save;
29816 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29818 strcat (p_name, " simd");
29819 mask |= OMP_SIMD_CLAUSE_MASK;
29820 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29822 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29823 cclauses == NULL);
29824 if (cclauses)
29826 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29827 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29830 sb = begin_omp_structured_block ();
29831 save = cp_parser_begin_omp_structured_block (parser);
29833 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29835 cp_parser_end_omp_structured_block (parser, save);
29836 add_stmt (finish_omp_structured_block (sb));
29838 return ret;
29841 /* OpenMP 2.5:
29842 #pragma omp for for-clause[optseq] new-line
29843 for-loop
29845 OpenMP 4.0:
29846 #pragma omp for simd for-simd-clause[optseq] new-line
29847 for-loop */
29849 #define OMP_FOR_CLAUSE_MASK \
29850 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29859 static tree
29860 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29861 char *p_name, omp_clause_mask mask, tree *cclauses)
29863 tree clauses, sb, ret;
29864 unsigned int save;
29865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29867 strcat (p_name, " for");
29868 mask |= OMP_FOR_CLAUSE_MASK;
29869 if (cclauses)
29870 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29872 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29874 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29875 const char *p = IDENTIFIER_POINTER (id);
29877 if (strcmp (p, "simd") == 0)
29879 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29880 if (cclauses == NULL)
29881 cclauses = cclauses_buf;
29883 cp_lexer_consume_token (parser->lexer);
29884 if (!flag_openmp) /* flag_openmp_simd */
29885 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29886 cclauses);
29887 sb = begin_omp_structured_block ();
29888 save = cp_parser_begin_omp_structured_block (parser);
29889 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29890 cclauses);
29891 cp_parser_end_omp_structured_block (parser, save);
29892 tree body = finish_omp_structured_block (sb);
29893 if (ret == NULL)
29894 return ret;
29895 ret = make_node (OMP_FOR);
29896 TREE_TYPE (ret) = void_type_node;
29897 OMP_FOR_BODY (ret) = body;
29898 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29899 SET_EXPR_LOCATION (ret, loc);
29900 add_stmt (ret);
29901 return ret;
29904 if (!flag_openmp) /* flag_openmp_simd */
29906 cp_parser_require_pragma_eol (parser, pragma_tok);
29907 return NULL_TREE;
29910 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29911 cclauses == NULL);
29912 if (cclauses)
29914 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29915 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29918 sb = begin_omp_structured_block ();
29919 save = cp_parser_begin_omp_structured_block (parser);
29921 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29923 cp_parser_end_omp_structured_block (parser, save);
29924 add_stmt (finish_omp_structured_block (sb));
29926 return ret;
29929 /* OpenMP 2.5:
29930 # pragma omp master new-line
29931 structured-block */
29933 static tree
29934 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29936 cp_parser_require_pragma_eol (parser, pragma_tok);
29937 return c_finish_omp_master (input_location,
29938 cp_parser_omp_structured_block (parser));
29941 /* OpenMP 2.5:
29942 # pragma omp ordered new-line
29943 structured-block */
29945 static tree
29946 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29948 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29949 cp_parser_require_pragma_eol (parser, pragma_tok);
29950 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29953 /* OpenMP 2.5:
29955 section-scope:
29956 { section-sequence }
29958 section-sequence:
29959 section-directive[opt] structured-block
29960 section-sequence section-directive structured-block */
29962 static tree
29963 cp_parser_omp_sections_scope (cp_parser *parser)
29965 tree stmt, substmt;
29966 bool error_suppress = false;
29967 cp_token *tok;
29969 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29970 return NULL_TREE;
29972 stmt = push_stmt_list ();
29974 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29976 substmt = cp_parser_omp_structured_block (parser);
29977 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29978 add_stmt (substmt);
29981 while (1)
29983 tok = cp_lexer_peek_token (parser->lexer);
29984 if (tok->type == CPP_CLOSE_BRACE)
29985 break;
29986 if (tok->type == CPP_EOF)
29987 break;
29989 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29991 cp_lexer_consume_token (parser->lexer);
29992 cp_parser_require_pragma_eol (parser, tok);
29993 error_suppress = false;
29995 else if (!error_suppress)
29997 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29998 error_suppress = true;
30001 substmt = cp_parser_omp_structured_block (parser);
30002 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30003 add_stmt (substmt);
30005 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30007 substmt = pop_stmt_list (stmt);
30009 stmt = make_node (OMP_SECTIONS);
30010 TREE_TYPE (stmt) = void_type_node;
30011 OMP_SECTIONS_BODY (stmt) = substmt;
30013 add_stmt (stmt);
30014 return stmt;
30017 /* OpenMP 2.5:
30018 # pragma omp sections sections-clause[optseq] newline
30019 sections-scope */
30021 #define OMP_SECTIONS_CLAUSE_MASK \
30022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30028 static tree
30029 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30030 char *p_name, omp_clause_mask mask, tree *cclauses)
30032 tree clauses, ret;
30033 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30035 strcat (p_name, " sections");
30036 mask |= OMP_SECTIONS_CLAUSE_MASK;
30037 if (cclauses)
30038 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30040 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30041 cclauses == NULL);
30042 if (cclauses)
30044 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30045 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30048 ret = cp_parser_omp_sections_scope (parser);
30049 if (ret)
30050 OMP_SECTIONS_CLAUSES (ret) = clauses;
30052 return ret;
30055 /* OpenMP 2.5:
30056 # pragma omp parallel parallel-clause[optseq] new-line
30057 structured-block
30058 # pragma omp parallel for parallel-for-clause[optseq] new-line
30059 structured-block
30060 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30061 structured-block
30063 OpenMP 4.0:
30064 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30065 structured-block */
30067 #define OMP_PARALLEL_CLAUSE_MASK \
30068 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30078 static tree
30079 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30080 char *p_name, omp_clause_mask mask, tree *cclauses)
30082 tree stmt, clauses, block;
30083 unsigned int save;
30084 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30086 strcat (p_name, " parallel");
30087 mask |= OMP_PARALLEL_CLAUSE_MASK;
30089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30091 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30092 if (cclauses == NULL)
30093 cclauses = cclauses_buf;
30095 cp_lexer_consume_token (parser->lexer);
30096 if (!flag_openmp) /* flag_openmp_simd */
30097 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30098 block = begin_omp_parallel ();
30099 save = cp_parser_begin_omp_structured_block (parser);
30100 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30101 cp_parser_end_omp_structured_block (parser, save);
30102 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30103 block);
30104 if (ret == NULL_TREE)
30105 return ret;
30106 OMP_PARALLEL_COMBINED (stmt) = 1;
30107 return stmt;
30109 else if (cclauses)
30111 error_at (loc, "expected %<for%> after %qs", p_name);
30112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30113 return NULL_TREE;
30115 else if (!flag_openmp) /* flag_openmp_simd */
30117 cp_parser_require_pragma_eol (parser, pragma_tok);
30118 return NULL_TREE;
30120 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30122 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30123 const char *p = IDENTIFIER_POINTER (id);
30124 if (strcmp (p, "sections") == 0)
30126 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30127 cclauses = cclauses_buf;
30129 cp_lexer_consume_token (parser->lexer);
30130 block = begin_omp_parallel ();
30131 save = cp_parser_begin_omp_structured_block (parser);
30132 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30133 cp_parser_end_omp_structured_block (parser, save);
30134 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30135 block);
30136 OMP_PARALLEL_COMBINED (stmt) = 1;
30137 return stmt;
30141 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30143 block = begin_omp_parallel ();
30144 save = cp_parser_begin_omp_structured_block (parser);
30145 cp_parser_statement (parser, NULL_TREE, false, NULL);
30146 cp_parser_end_omp_structured_block (parser, save);
30147 stmt = finish_omp_parallel (clauses, block);
30148 return stmt;
30151 /* OpenMP 2.5:
30152 # pragma omp single single-clause[optseq] new-line
30153 structured-block */
30155 #define OMP_SINGLE_CLAUSE_MASK \
30156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30161 static tree
30162 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30164 tree stmt = make_node (OMP_SINGLE);
30165 TREE_TYPE (stmt) = void_type_node;
30167 OMP_SINGLE_CLAUSES (stmt)
30168 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30169 "#pragma omp single", pragma_tok);
30170 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30172 return add_stmt (stmt);
30175 /* OpenMP 3.0:
30176 # pragma omp task task-clause[optseq] new-line
30177 structured-block */
30179 #define OMP_TASK_CLAUSE_MASK \
30180 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30190 static tree
30191 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30193 tree clauses, block;
30194 unsigned int save;
30196 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30197 "#pragma omp task", pragma_tok);
30198 block = begin_omp_task ();
30199 save = cp_parser_begin_omp_structured_block (parser);
30200 cp_parser_statement (parser, NULL_TREE, false, NULL);
30201 cp_parser_end_omp_structured_block (parser, save);
30202 return finish_omp_task (clauses, block);
30205 /* OpenMP 3.0:
30206 # pragma omp taskwait new-line */
30208 static void
30209 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30211 cp_parser_require_pragma_eol (parser, pragma_tok);
30212 finish_omp_taskwait ();
30215 /* OpenMP 3.1:
30216 # pragma omp taskyield new-line */
30218 static void
30219 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30221 cp_parser_require_pragma_eol (parser, pragma_tok);
30222 finish_omp_taskyield ();
30225 /* OpenMP 4.0:
30226 # pragma omp taskgroup new-line
30227 structured-block */
30229 static tree
30230 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30232 cp_parser_require_pragma_eol (parser, pragma_tok);
30233 return c_finish_omp_taskgroup (input_location,
30234 cp_parser_omp_structured_block (parser));
30238 /* OpenMP 2.5:
30239 # pragma omp threadprivate (variable-list) */
30241 static void
30242 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30244 tree vars;
30246 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30247 cp_parser_require_pragma_eol (parser, pragma_tok);
30249 finish_omp_threadprivate (vars);
30252 /* OpenMP 4.0:
30253 # pragma omp cancel cancel-clause[optseq] new-line */
30255 #define OMP_CANCEL_CLAUSE_MASK \
30256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30262 static void
30263 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30265 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30266 "#pragma omp cancel", pragma_tok);
30267 finish_omp_cancel (clauses);
30270 /* OpenMP 4.0:
30271 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30273 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30279 static void
30280 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30282 tree clauses;
30283 bool point_seen = false;
30285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30287 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30288 const char *p = IDENTIFIER_POINTER (id);
30290 if (strcmp (p, "point") == 0)
30292 cp_lexer_consume_token (parser->lexer);
30293 point_seen = true;
30296 if (!point_seen)
30298 cp_parser_error (parser, "expected %<point%>");
30299 cp_parser_require_pragma_eol (parser, pragma_tok);
30300 return;
30303 clauses = cp_parser_omp_all_clauses (parser,
30304 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30305 "#pragma omp cancellation point",
30306 pragma_tok);
30307 finish_omp_cancellation_point (clauses);
30310 /* OpenMP 4.0:
30311 #pragma omp distribute distribute-clause[optseq] new-line
30312 for-loop */
30314 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30315 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30320 static tree
30321 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30322 char *p_name, omp_clause_mask mask, tree *cclauses)
30324 tree clauses, sb, ret;
30325 unsigned int save;
30326 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30328 strcat (p_name, " distribute");
30329 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30331 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30333 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30334 const char *p = IDENTIFIER_POINTER (id);
30335 bool simd = false;
30336 bool parallel = false;
30338 if (strcmp (p, "simd") == 0)
30339 simd = true;
30340 else
30341 parallel = strcmp (p, "parallel") == 0;
30342 if (parallel || simd)
30344 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30345 if (cclauses == NULL)
30346 cclauses = cclauses_buf;
30347 cp_lexer_consume_token (parser->lexer);
30348 if (!flag_openmp) /* flag_openmp_simd */
30350 if (simd)
30351 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30352 cclauses);
30353 else
30354 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30355 cclauses);
30357 sb = begin_omp_structured_block ();
30358 save = cp_parser_begin_omp_structured_block (parser);
30359 if (simd)
30360 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30361 cclauses);
30362 else
30363 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30364 cclauses);
30365 cp_parser_end_omp_structured_block (parser, save);
30366 tree body = finish_omp_structured_block (sb);
30367 if (ret == NULL)
30368 return ret;
30369 ret = make_node (OMP_DISTRIBUTE);
30370 TREE_TYPE (ret) = void_type_node;
30371 OMP_FOR_BODY (ret) = body;
30372 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30373 SET_EXPR_LOCATION (ret, loc);
30374 add_stmt (ret);
30375 return ret;
30378 if (!flag_openmp) /* flag_openmp_simd */
30380 cp_parser_require_pragma_eol (parser, pragma_tok);
30381 return NULL_TREE;
30384 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30385 cclauses == NULL);
30386 if (cclauses)
30388 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30389 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30392 sb = begin_omp_structured_block ();
30393 save = cp_parser_begin_omp_structured_block (parser);
30395 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30397 cp_parser_end_omp_structured_block (parser, save);
30398 add_stmt (finish_omp_structured_block (sb));
30400 return ret;
30403 /* OpenMP 4.0:
30404 # pragma omp teams teams-clause[optseq] new-line
30405 structured-block */
30407 #define OMP_TEAMS_CLAUSE_MASK \
30408 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30416 static tree
30417 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30418 char *p_name, omp_clause_mask mask, tree *cclauses)
30420 tree clauses, sb, ret;
30421 unsigned int save;
30422 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30424 strcat (p_name, " teams");
30425 mask |= OMP_TEAMS_CLAUSE_MASK;
30427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30429 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30430 const char *p = IDENTIFIER_POINTER (id);
30431 if (strcmp (p, "distribute") == 0)
30433 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30434 if (cclauses == NULL)
30435 cclauses = cclauses_buf;
30437 cp_lexer_consume_token (parser->lexer);
30438 if (!flag_openmp) /* flag_openmp_simd */
30439 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30440 cclauses);
30441 sb = begin_omp_structured_block ();
30442 save = cp_parser_begin_omp_structured_block (parser);
30443 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30444 cclauses);
30445 cp_parser_end_omp_structured_block (parser, save);
30446 tree body = finish_omp_structured_block (sb);
30447 if (ret == NULL)
30448 return ret;
30449 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30450 ret = make_node (OMP_TEAMS);
30451 TREE_TYPE (ret) = void_type_node;
30452 OMP_TEAMS_CLAUSES (ret) = clauses;
30453 OMP_TEAMS_BODY (ret) = body;
30454 return add_stmt (ret);
30457 if (!flag_openmp) /* flag_openmp_simd */
30459 cp_parser_require_pragma_eol (parser, pragma_tok);
30460 return NULL_TREE;
30463 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30464 cclauses == NULL);
30465 if (cclauses)
30467 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30468 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30471 tree stmt = make_node (OMP_TEAMS);
30472 TREE_TYPE (stmt) = void_type_node;
30473 OMP_TEAMS_CLAUSES (stmt) = clauses;
30474 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30476 return add_stmt (stmt);
30479 /* OpenMP 4.0:
30480 # pragma omp target data target-data-clause[optseq] new-line
30481 structured-block */
30483 #define OMP_TARGET_DATA_CLAUSE_MASK \
30484 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30488 static tree
30489 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30491 tree stmt = make_node (OMP_TARGET_DATA);
30492 TREE_TYPE (stmt) = void_type_node;
30494 OMP_TARGET_DATA_CLAUSES (stmt)
30495 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30496 "#pragma omp target data", pragma_tok);
30497 keep_next_level (true);
30498 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30500 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30501 return add_stmt (stmt);
30504 /* OpenMP 4.0:
30505 # pragma omp target update target-update-clause[optseq] new-line */
30507 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30513 static bool
30514 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30515 enum pragma_context context)
30517 if (context == pragma_stmt)
30519 error_at (pragma_tok->location,
30520 "%<#pragma omp target update%> may only be "
30521 "used in compound statements");
30522 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30523 return false;
30526 tree clauses
30527 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30528 "#pragma omp target update", pragma_tok);
30529 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30530 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30532 error_at (pragma_tok->location,
30533 "%<#pragma omp target update must contain at least one "
30534 "%<from%> or %<to%> clauses");
30535 return false;
30538 tree stmt = make_node (OMP_TARGET_UPDATE);
30539 TREE_TYPE (stmt) = void_type_node;
30540 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30541 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30542 add_stmt (stmt);
30543 return false;
30546 /* OpenMP 4.0:
30547 # pragma omp target target-clause[optseq] new-line
30548 structured-block */
30550 #define OMP_TARGET_CLAUSE_MASK \
30551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30555 static bool
30556 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30557 enum pragma_context context)
30559 if (context != pragma_stmt && context != pragma_compound)
30561 cp_parser_error (parser, "expected declaration specifiers");
30562 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30563 return false;
30566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30568 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30569 const char *p = IDENTIFIER_POINTER (id);
30571 if (strcmp (p, "teams") == 0)
30573 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30574 char p_name[sizeof ("#pragma omp target teams distribute "
30575 "parallel for simd")];
30577 cp_lexer_consume_token (parser->lexer);
30578 strcpy (p_name, "#pragma omp target");
30579 if (!flag_openmp) /* flag_openmp_simd */
30581 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30582 OMP_TARGET_CLAUSE_MASK,
30583 cclauses);
30584 return stmt != NULL_TREE;
30586 keep_next_level (true);
30587 tree sb = begin_omp_structured_block ();
30588 unsigned save = cp_parser_begin_omp_structured_block (parser);
30589 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30590 OMP_TARGET_CLAUSE_MASK, cclauses);
30591 cp_parser_end_omp_structured_block (parser, save);
30592 tree body = finish_omp_structured_block (sb);
30593 if (ret == NULL_TREE)
30594 return false;
30595 tree stmt = make_node (OMP_TARGET);
30596 TREE_TYPE (stmt) = void_type_node;
30597 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30598 OMP_TARGET_BODY (stmt) = body;
30599 add_stmt (stmt);
30600 return true;
30602 else if (!flag_openmp) /* flag_openmp_simd */
30604 cp_parser_require_pragma_eol (parser, pragma_tok);
30605 return false;
30607 else if (strcmp (p, "data") == 0)
30609 cp_lexer_consume_token (parser->lexer);
30610 cp_parser_omp_target_data (parser, pragma_tok);
30611 return true;
30613 else if (strcmp (p, "update") == 0)
30615 cp_lexer_consume_token (parser->lexer);
30616 return cp_parser_omp_target_update (parser, pragma_tok, context);
30620 tree stmt = make_node (OMP_TARGET);
30621 TREE_TYPE (stmt) = void_type_node;
30623 OMP_TARGET_CLAUSES (stmt)
30624 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30625 "#pragma omp target", pragma_tok);
30626 keep_next_level (true);
30627 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30629 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30630 add_stmt (stmt);
30631 return true;
30634 /* OpenMP 4.0:
30635 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30637 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30638 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30645 static void
30646 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30647 enum pragma_context context)
30649 bool first_p = parser->omp_declare_simd == NULL;
30650 cp_omp_declare_simd_data data;
30651 if (first_p)
30653 data.error_seen = false;
30654 data.fndecl_seen = false;
30655 data.tokens = vNULL;
30656 parser->omp_declare_simd = &data;
30658 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30659 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30660 cp_lexer_consume_token (parser->lexer);
30661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30662 parser->omp_declare_simd->error_seen = true;
30663 cp_parser_require_pragma_eol (parser, pragma_tok);
30664 struct cp_token_cache *cp
30665 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30666 parser->omp_declare_simd->tokens.safe_push (cp);
30667 if (first_p)
30669 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30670 cp_parser_pragma (parser, context);
30671 switch (context)
30673 case pragma_external:
30674 cp_parser_declaration (parser);
30675 break;
30676 case pragma_member:
30677 cp_parser_member_declaration (parser);
30678 break;
30679 case pragma_objc_icode:
30680 cp_parser_block_declaration (parser, /*statement_p=*/false);
30681 break;
30682 default:
30683 cp_parser_declaration_statement (parser);
30684 break;
30686 if (parser->omp_declare_simd
30687 && !parser->omp_declare_simd->error_seen
30688 && !parser->omp_declare_simd->fndecl_seen)
30689 error_at (pragma_tok->location,
30690 "%<#pragma omp declare simd%> not immediately followed by "
30691 "function declaration or definition");
30692 data.tokens.release ();
30693 parser->omp_declare_simd = NULL;
30697 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30698 This function is modelled similar to the late parsing of omp declare
30699 simd. */
30701 static tree
30702 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30704 struct cp_token_cache *ce;
30705 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30706 int ii = 0;
30708 if (parser->omp_declare_simd != NULL)
30710 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30711 " marked as a Cilk Plus SIMD-enabled function");
30712 XDELETE (parser->cilk_simd_fn_info);
30713 parser->cilk_simd_fn_info = NULL;
30714 return attrs;
30716 if (!info->error_seen && info->fndecl_seen)
30718 error ("vector attribute not immediately followed by a single function"
30719 " declaration or definition");
30720 info->error_seen = true;
30722 if (info->error_seen)
30723 return attrs;
30725 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30727 tree c, cl;
30729 cp_parser_push_lexer_for_tokens (parser, ce);
30730 parser->lexer->in_pragma = true;
30731 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30732 "SIMD-enabled functions attribute",
30733 NULL);
30734 cp_parser_pop_lexer (parser);
30735 if (cl)
30736 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30738 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30739 TREE_CHAIN (c) = attrs;
30740 attrs = c;
30742 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30743 TREE_CHAIN (c) = attrs;
30744 if (processing_template_decl)
30745 ATTR_IS_DEPENDENT (c) = 1;
30746 attrs = c;
30748 info->fndecl_seen = true;
30749 XDELETE (parser->cilk_simd_fn_info);
30750 parser->cilk_simd_fn_info = NULL;
30751 return attrs;
30754 /* Finalize #pragma omp declare simd clauses after direct declarator has
30755 been parsed, and put that into "omp declare simd" attribute. */
30757 static tree
30758 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30760 struct cp_token_cache *ce;
30761 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30762 int i;
30764 if (!data->error_seen && data->fndecl_seen)
30766 error ("%<#pragma omp declare simd%> not immediately followed by "
30767 "a single function declaration or definition");
30768 data->error_seen = true;
30769 return attrs;
30771 if (data->error_seen)
30772 return attrs;
30774 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30776 tree c, cl;
30778 cp_parser_push_lexer_for_tokens (parser, ce);
30779 parser->lexer->in_pragma = true;
30780 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30781 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30782 cp_lexer_consume_token (parser->lexer);
30783 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30784 "#pragma omp declare simd", pragma_tok);
30785 cp_parser_pop_lexer (parser);
30786 if (cl)
30787 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30788 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30789 TREE_CHAIN (c) = attrs;
30790 if (processing_template_decl)
30791 ATTR_IS_DEPENDENT (c) = 1;
30792 attrs = c;
30795 data->fndecl_seen = true;
30796 return attrs;
30800 /* OpenMP 4.0:
30801 # pragma omp declare target new-line
30802 declarations and definitions
30803 # pragma omp end declare target new-line */
30805 static void
30806 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30808 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30809 scope_chain->omp_declare_target_attribute++;
30812 static void
30813 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30815 const char *p = "";
30816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30818 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30819 p = IDENTIFIER_POINTER (id);
30821 if (strcmp (p, "declare") == 0)
30823 cp_lexer_consume_token (parser->lexer);
30824 p = "";
30825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30827 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30828 p = IDENTIFIER_POINTER (id);
30830 if (strcmp (p, "target") == 0)
30831 cp_lexer_consume_token (parser->lexer);
30832 else
30834 cp_parser_error (parser, "expected %<target%>");
30835 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30836 return;
30839 else
30841 cp_parser_error (parser, "expected %<declare%>");
30842 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30843 return;
30845 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30846 if (!scope_chain->omp_declare_target_attribute)
30847 error_at (pragma_tok->location,
30848 "%<#pragma omp end declare target%> without corresponding "
30849 "%<#pragma omp declare target%>");
30850 else
30851 scope_chain->omp_declare_target_attribute--;
30854 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30855 expression and optional initializer clause of
30856 #pragma omp declare reduction. We store the expression(s) as
30857 either 3, 6 or 7 special statements inside of the artificial function's
30858 body. The first two statements are DECL_EXPRs for the artificial
30859 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30860 expression that uses those variables.
30861 If there was any INITIALIZER clause, this is followed by further statements,
30862 the fourth and fifth statements are DECL_EXPRs for the artificial
30863 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30864 constructor variant (first token after open paren is not omp_priv),
30865 then the sixth statement is a statement with the function call expression
30866 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30867 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30868 to initialize the OMP_PRIV artificial variable and there is seventh
30869 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30871 static bool
30872 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30874 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30875 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30876 type = TREE_TYPE (type);
30877 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30878 DECL_ARTIFICIAL (omp_out) = 1;
30879 pushdecl (omp_out);
30880 add_decl_expr (omp_out);
30881 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30882 DECL_ARTIFICIAL (omp_in) = 1;
30883 pushdecl (omp_in);
30884 add_decl_expr (omp_in);
30885 tree combiner;
30886 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30888 keep_next_level (true);
30889 tree block = begin_omp_structured_block ();
30890 combiner = cp_parser_expression (parser);
30891 finish_expr_stmt (combiner);
30892 block = finish_omp_structured_block (block);
30893 add_stmt (block);
30895 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30896 return false;
30898 const char *p = "";
30899 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30901 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30902 p = IDENTIFIER_POINTER (id);
30905 if (strcmp (p, "initializer") == 0)
30907 cp_lexer_consume_token (parser->lexer);
30908 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30909 return false;
30911 p = "";
30912 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30914 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30915 p = IDENTIFIER_POINTER (id);
30918 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30919 DECL_ARTIFICIAL (omp_priv) = 1;
30920 pushdecl (omp_priv);
30921 add_decl_expr (omp_priv);
30922 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30923 DECL_ARTIFICIAL (omp_orig) = 1;
30924 pushdecl (omp_orig);
30925 add_decl_expr (omp_orig);
30927 keep_next_level (true);
30928 block = begin_omp_structured_block ();
30930 bool ctor = false;
30931 if (strcmp (p, "omp_priv") == 0)
30933 bool is_direct_init, is_non_constant_init;
30934 ctor = true;
30935 cp_lexer_consume_token (parser->lexer);
30936 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30937 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30938 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30939 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30940 == CPP_CLOSE_PAREN
30941 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30942 == CPP_CLOSE_PAREN))
30944 finish_omp_structured_block (block);
30945 error ("invalid initializer clause");
30946 return false;
30948 initializer = cp_parser_initializer (parser, &is_direct_init,
30949 &is_non_constant_init);
30950 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30951 NULL_TREE, LOOKUP_ONLYCONVERTING);
30953 else
30955 cp_parser_parse_tentatively (parser);
30956 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30957 /*check_dependency_p=*/true,
30958 /*template_p=*/NULL,
30959 /*declarator_p=*/false,
30960 /*optional_p=*/false);
30961 vec<tree, va_gc> *args;
30962 if (fn_name == error_mark_node
30963 || cp_parser_error_occurred (parser)
30964 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30965 || ((args = cp_parser_parenthesized_expression_list
30966 (parser, non_attr, /*cast_p=*/false,
30967 /*allow_expansion_p=*/true,
30968 /*non_constant_p=*/NULL)),
30969 cp_parser_error_occurred (parser)))
30971 finish_omp_structured_block (block);
30972 cp_parser_abort_tentative_parse (parser);
30973 cp_parser_error (parser, "expected id-expression (arguments)");
30974 return false;
30976 unsigned int i;
30977 tree arg;
30978 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30979 if (arg == omp_priv
30980 || (TREE_CODE (arg) == ADDR_EXPR
30981 && TREE_OPERAND (arg, 0) == omp_priv))
30982 break;
30983 cp_parser_abort_tentative_parse (parser);
30984 if (arg == NULL_TREE)
30985 error ("one of the initializer call arguments should be %<omp_priv%>"
30986 " or %<&omp_priv%>");
30987 initializer = cp_parser_postfix_expression (parser, false, false, false,
30988 false, NULL);
30989 finish_expr_stmt (initializer);
30992 block = finish_omp_structured_block (block);
30993 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30994 finish_expr_stmt (block);
30996 if (ctor)
30997 add_decl_expr (omp_orig);
30999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31000 return false;
31003 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31004 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31006 return true;
31009 /* OpenMP 4.0
31010 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31011 initializer-clause[opt] new-line
31013 initializer-clause:
31014 initializer (omp_priv initializer)
31015 initializer (function-name (argument-list)) */
31017 static void
31018 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31019 enum pragma_context)
31021 auto_vec<tree> types;
31022 enum tree_code reduc_code = ERROR_MARK;
31023 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31024 unsigned int i;
31025 cp_token *first_token;
31026 cp_token_cache *cp;
31027 int errs;
31028 void *p;
31030 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31031 p = obstack_alloc (&declarator_obstack, 0);
31033 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31034 goto fail;
31036 switch (cp_lexer_peek_token (parser->lexer)->type)
31038 case CPP_PLUS:
31039 reduc_code = PLUS_EXPR;
31040 break;
31041 case CPP_MULT:
31042 reduc_code = MULT_EXPR;
31043 break;
31044 case CPP_MINUS:
31045 reduc_code = MINUS_EXPR;
31046 break;
31047 case CPP_AND:
31048 reduc_code = BIT_AND_EXPR;
31049 break;
31050 case CPP_XOR:
31051 reduc_code = BIT_XOR_EXPR;
31052 break;
31053 case CPP_OR:
31054 reduc_code = BIT_IOR_EXPR;
31055 break;
31056 case CPP_AND_AND:
31057 reduc_code = TRUTH_ANDIF_EXPR;
31058 break;
31059 case CPP_OR_OR:
31060 reduc_code = TRUTH_ORIF_EXPR;
31061 break;
31062 case CPP_NAME:
31063 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31064 break;
31065 default:
31066 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31067 "%<|%>, %<&&%>, %<||%> or identifier");
31068 goto fail;
31071 if (reduc_code != ERROR_MARK)
31072 cp_lexer_consume_token (parser->lexer);
31074 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31075 if (reduc_id == error_mark_node)
31076 goto fail;
31078 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31079 goto fail;
31081 /* Types may not be defined in declare reduction type list. */
31082 const char *saved_message;
31083 saved_message = parser->type_definition_forbidden_message;
31084 parser->type_definition_forbidden_message
31085 = G_("types may not be defined in declare reduction type list");
31086 bool saved_colon_corrects_to_scope_p;
31087 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31088 parser->colon_corrects_to_scope_p = false;
31089 bool saved_colon_doesnt_start_class_def_p;
31090 saved_colon_doesnt_start_class_def_p
31091 = parser->colon_doesnt_start_class_def_p;
31092 parser->colon_doesnt_start_class_def_p = true;
31094 while (true)
31096 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31097 type = cp_parser_type_id (parser);
31098 if (type == error_mark_node)
31100 else if (ARITHMETIC_TYPE_P (type)
31101 && (orig_reduc_id == NULL_TREE
31102 || (TREE_CODE (type) != COMPLEX_TYPE
31103 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31104 "min") == 0
31105 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31106 "max") == 0))))
31107 error_at (loc, "predeclared arithmetic type %qT in "
31108 "%<#pragma omp declare reduction%>", type);
31109 else if (TREE_CODE (type) == FUNCTION_TYPE
31110 || TREE_CODE (type) == METHOD_TYPE
31111 || TREE_CODE (type) == ARRAY_TYPE)
31112 error_at (loc, "function or array type %qT in "
31113 "%<#pragma omp declare reduction%>", type);
31114 else if (TREE_CODE (type) == REFERENCE_TYPE)
31115 error_at (loc, "reference type %qT in "
31116 "%<#pragma omp declare reduction%>", type);
31117 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31118 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31119 "%<#pragma omp declare reduction%>", type);
31120 else
31121 types.safe_push (type);
31123 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31124 cp_lexer_consume_token (parser->lexer);
31125 else
31126 break;
31129 /* Restore the saved message. */
31130 parser->type_definition_forbidden_message = saved_message;
31131 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31132 parser->colon_doesnt_start_class_def_p
31133 = saved_colon_doesnt_start_class_def_p;
31135 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31136 || types.is_empty ())
31138 fail:
31139 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31140 goto done;
31143 first_token = cp_lexer_peek_token (parser->lexer);
31144 cp = NULL;
31145 errs = errorcount;
31146 FOR_EACH_VEC_ELT (types, i, type)
31148 tree fntype
31149 = build_function_type_list (void_type_node,
31150 cp_build_reference_type (type, false),
31151 NULL_TREE);
31152 tree this_reduc_id = reduc_id;
31153 if (!dependent_type_p (type))
31154 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31155 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31156 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31157 DECL_ARTIFICIAL (fndecl) = 1;
31158 DECL_EXTERNAL (fndecl) = 1;
31159 DECL_DECLARED_INLINE_P (fndecl) = 1;
31160 DECL_IGNORED_P (fndecl) = 1;
31161 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31162 DECL_ATTRIBUTES (fndecl)
31163 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31164 DECL_ATTRIBUTES (fndecl));
31165 if (processing_template_decl)
31166 fndecl = push_template_decl (fndecl);
31167 bool block_scope = false;
31168 tree block = NULL_TREE;
31169 if (current_function_decl)
31171 block_scope = true;
31172 DECL_CONTEXT (fndecl) = global_namespace;
31173 if (!processing_template_decl)
31174 pushdecl (fndecl);
31176 else if (current_class_type)
31178 if (cp == NULL)
31180 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31181 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31182 cp_lexer_consume_token (parser->lexer);
31183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31184 goto fail;
31185 cp = cp_token_cache_new (first_token,
31186 cp_lexer_peek_nth_token (parser->lexer,
31187 2));
31189 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31190 finish_member_declaration (fndecl);
31191 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31192 DECL_PENDING_INLINE_P (fndecl) = 1;
31193 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31194 continue;
31196 else
31198 DECL_CONTEXT (fndecl) = current_namespace;
31199 pushdecl (fndecl);
31201 if (!block_scope)
31202 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31203 else
31204 block = begin_omp_structured_block ();
31205 if (cp)
31207 cp_parser_push_lexer_for_tokens (parser, cp);
31208 parser->lexer->in_pragma = true;
31210 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31212 if (!block_scope)
31213 finish_function (0);
31214 else
31215 DECL_CONTEXT (fndecl) = current_function_decl;
31216 if (cp)
31217 cp_parser_pop_lexer (parser);
31218 goto fail;
31220 if (cp)
31221 cp_parser_pop_lexer (parser);
31222 if (!block_scope)
31223 finish_function (0);
31224 else
31226 DECL_CONTEXT (fndecl) = current_function_decl;
31227 block = finish_omp_structured_block (block);
31228 if (TREE_CODE (block) == BIND_EXPR)
31229 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31230 else if (TREE_CODE (block) == STATEMENT_LIST)
31231 DECL_SAVED_TREE (fndecl) = block;
31232 if (processing_template_decl)
31233 add_decl_expr (fndecl);
31235 cp_check_omp_declare_reduction (fndecl);
31236 if (cp == NULL && types.length () > 1)
31237 cp = cp_token_cache_new (first_token,
31238 cp_lexer_peek_nth_token (parser->lexer, 2));
31239 if (errs != errorcount)
31240 break;
31243 cp_parser_require_pragma_eol (parser, pragma_tok);
31245 done:
31246 /* Free any declarators allocated. */
31247 obstack_free (&declarator_obstack, p);
31250 /* OpenMP 4.0
31251 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31252 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31253 initializer-clause[opt] new-line
31254 #pragma omp declare target new-line */
31256 static void
31257 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31258 enum pragma_context context)
31260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31262 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31263 const char *p = IDENTIFIER_POINTER (id);
31265 if (strcmp (p, "simd") == 0)
31267 cp_lexer_consume_token (parser->lexer);
31268 cp_parser_omp_declare_simd (parser, pragma_tok,
31269 context);
31270 return;
31272 cp_ensure_no_omp_declare_simd (parser);
31273 if (strcmp (p, "reduction") == 0)
31275 cp_lexer_consume_token (parser->lexer);
31276 cp_parser_omp_declare_reduction (parser, pragma_tok,
31277 context);
31278 return;
31280 if (!flag_openmp) /* flag_openmp_simd */
31282 cp_parser_require_pragma_eol (parser, pragma_tok);
31283 return;
31285 if (strcmp (p, "target") == 0)
31287 cp_lexer_consume_token (parser->lexer);
31288 cp_parser_omp_declare_target (parser, pragma_tok);
31289 return;
31292 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31293 "or %<target%>");
31294 cp_parser_require_pragma_eol (parser, pragma_tok);
31297 /* Main entry point to OpenMP statement pragmas. */
31299 static void
31300 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31302 tree stmt;
31303 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31304 omp_clause_mask mask (0);
31306 switch (pragma_tok->pragma_kind)
31308 case PRAGMA_OMP_ATOMIC:
31309 cp_parser_omp_atomic (parser, pragma_tok);
31310 return;
31311 case PRAGMA_OMP_CRITICAL:
31312 stmt = cp_parser_omp_critical (parser, pragma_tok);
31313 break;
31314 case PRAGMA_OMP_DISTRIBUTE:
31315 strcpy (p_name, "#pragma omp");
31316 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31317 break;
31318 case PRAGMA_OMP_FOR:
31319 strcpy (p_name, "#pragma omp");
31320 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31321 break;
31322 case PRAGMA_OMP_MASTER:
31323 stmt = cp_parser_omp_master (parser, pragma_tok);
31324 break;
31325 case PRAGMA_OMP_ORDERED:
31326 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31327 break;
31328 case PRAGMA_OMP_PARALLEL:
31329 strcpy (p_name, "#pragma omp");
31330 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31331 break;
31332 case PRAGMA_OMP_SECTIONS:
31333 strcpy (p_name, "#pragma omp");
31334 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31335 break;
31336 case PRAGMA_OMP_SIMD:
31337 strcpy (p_name, "#pragma omp");
31338 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31339 break;
31340 case PRAGMA_OMP_SINGLE:
31341 stmt = cp_parser_omp_single (parser, pragma_tok);
31342 break;
31343 case PRAGMA_OMP_TASK:
31344 stmt = cp_parser_omp_task (parser, pragma_tok);
31345 break;
31346 case PRAGMA_OMP_TASKGROUP:
31347 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31348 break;
31349 case PRAGMA_OMP_TEAMS:
31350 strcpy (p_name, "#pragma omp");
31351 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31352 break;
31353 default:
31354 gcc_unreachable ();
31357 if (stmt)
31358 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31361 /* Transactional Memory parsing routines. */
31363 /* Parse a transaction attribute.
31365 txn-attribute:
31366 attribute
31367 [ [ identifier ] ]
31369 ??? Simplify this when C++0x bracket attributes are
31370 implemented properly. */
31372 static tree
31373 cp_parser_txn_attribute_opt (cp_parser *parser)
31375 cp_token *token;
31376 tree attr_name, attr = NULL;
31378 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31379 return cp_parser_attributes_opt (parser);
31381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31382 return NULL_TREE;
31383 cp_lexer_consume_token (parser->lexer);
31384 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31385 goto error1;
31387 token = cp_lexer_peek_token (parser->lexer);
31388 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31390 token = cp_lexer_consume_token (parser->lexer);
31392 attr_name = (token->type == CPP_KEYWORD
31393 /* For keywords, use the canonical spelling,
31394 not the parsed identifier. */
31395 ? ridpointers[(int) token->keyword]
31396 : token->u.value);
31397 attr = build_tree_list (attr_name, NULL_TREE);
31399 else
31400 cp_parser_error (parser, "expected identifier");
31402 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31403 error1:
31404 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31405 return attr;
31408 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31410 transaction-statement:
31411 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31412 compound-statement
31413 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31416 static tree
31417 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31419 unsigned char old_in = parser->in_transaction;
31420 unsigned char this_in = 1, new_in;
31421 cp_token *token;
31422 tree stmt, attrs, noex;
31424 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31425 || keyword == RID_TRANSACTION_RELAXED);
31426 token = cp_parser_require_keyword (parser, keyword,
31427 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31428 : RT_TRANSACTION_RELAXED));
31429 gcc_assert (token != NULL);
31431 if (keyword == RID_TRANSACTION_RELAXED)
31432 this_in |= TM_STMT_ATTR_RELAXED;
31433 else
31435 attrs = cp_parser_txn_attribute_opt (parser);
31436 if (attrs)
31437 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31440 /* Parse a noexcept specification. */
31441 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31443 /* Keep track if we're in the lexical scope of an outer transaction. */
31444 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31446 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31448 parser->in_transaction = new_in;
31449 cp_parser_compound_statement (parser, NULL, false, false);
31450 parser->in_transaction = old_in;
31452 finish_transaction_stmt (stmt, NULL, this_in, noex);
31454 return stmt;
31457 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31459 transaction-expression:
31460 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31461 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31464 static tree
31465 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31467 unsigned char old_in = parser->in_transaction;
31468 unsigned char this_in = 1;
31469 cp_token *token;
31470 tree expr, noex;
31471 bool noex_expr;
31473 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31474 || keyword == RID_TRANSACTION_RELAXED);
31476 if (!flag_tm)
31477 error (keyword == RID_TRANSACTION_RELAXED
31478 ? G_("%<__transaction_relaxed%> without transactional memory "
31479 "support enabled")
31480 : G_("%<__transaction_atomic%> without transactional memory "
31481 "support enabled"));
31483 token = cp_parser_require_keyword (parser, keyword,
31484 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31485 : RT_TRANSACTION_RELAXED));
31486 gcc_assert (token != NULL);
31488 if (keyword == RID_TRANSACTION_RELAXED)
31489 this_in |= TM_STMT_ATTR_RELAXED;
31491 /* Set this early. This might mean that we allow transaction_cancel in
31492 an expression that we find out later actually has to be a constexpr.
31493 However, we expect that cxx_constant_value will be able to deal with
31494 this; also, if the noexcept has no constexpr, then what we parse next
31495 really is a transaction's body. */
31496 parser->in_transaction = this_in;
31498 /* Parse a noexcept specification. */
31499 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31500 true);
31502 if (!noex || !noex_expr
31503 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31505 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31507 expr = cp_parser_expression (parser);
31508 expr = finish_parenthesized_expr (expr);
31510 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31512 else
31514 /* The only expression that is available got parsed for the noexcept
31515 already. noexcept is true then. */
31516 expr = noex;
31517 noex = boolean_true_node;
31520 expr = build_transaction_expr (token->location, expr, this_in, noex);
31521 parser->in_transaction = old_in;
31523 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31524 return error_mark_node;
31526 return (flag_tm ? expr : error_mark_node);
31529 /* Parse a function-transaction-block.
31531 function-transaction-block:
31532 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31533 function-body
31534 __transaction_atomic txn-attribute[opt] function-try-block
31535 __transaction_relaxed ctor-initializer[opt] function-body
31536 __transaction_relaxed function-try-block
31539 static bool
31540 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31542 unsigned char old_in = parser->in_transaction;
31543 unsigned char new_in = 1;
31544 tree compound_stmt, stmt, attrs;
31545 bool ctor_initializer_p;
31546 cp_token *token;
31548 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31549 || keyword == RID_TRANSACTION_RELAXED);
31550 token = cp_parser_require_keyword (parser, keyword,
31551 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31552 : RT_TRANSACTION_RELAXED));
31553 gcc_assert (token != NULL);
31555 if (keyword == RID_TRANSACTION_RELAXED)
31556 new_in |= TM_STMT_ATTR_RELAXED;
31557 else
31559 attrs = cp_parser_txn_attribute_opt (parser);
31560 if (attrs)
31561 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31564 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31566 parser->in_transaction = new_in;
31568 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31569 ctor_initializer_p = cp_parser_function_try_block (parser);
31570 else
31571 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31572 (parser, /*in_function_try_block=*/false);
31574 parser->in_transaction = old_in;
31576 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31578 return ctor_initializer_p;
31581 /* Parse a __transaction_cancel statement.
31583 cancel-statement:
31584 __transaction_cancel txn-attribute[opt] ;
31585 __transaction_cancel txn-attribute[opt] throw-expression ;
31587 ??? Cancel and throw is not yet implemented. */
31589 static tree
31590 cp_parser_transaction_cancel (cp_parser *parser)
31592 cp_token *token;
31593 bool is_outer = false;
31594 tree stmt, attrs;
31596 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31597 RT_TRANSACTION_CANCEL);
31598 gcc_assert (token != NULL);
31600 attrs = cp_parser_txn_attribute_opt (parser);
31601 if (attrs)
31602 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31604 /* ??? Parse cancel-and-throw here. */
31606 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31608 if (!flag_tm)
31610 error_at (token->location, "%<__transaction_cancel%> without "
31611 "transactional memory support enabled");
31612 return error_mark_node;
31614 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31616 error_at (token->location, "%<__transaction_cancel%> within a "
31617 "%<__transaction_relaxed%>");
31618 return error_mark_node;
31620 else if (is_outer)
31622 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31623 && !is_tm_may_cancel_outer (current_function_decl))
31625 error_at (token->location, "outer %<__transaction_cancel%> not "
31626 "within outer %<__transaction_atomic%>");
31627 error_at (token->location,
31628 " or a %<transaction_may_cancel_outer%> function");
31629 return error_mark_node;
31632 else if (parser->in_transaction == 0)
31634 error_at (token->location, "%<__transaction_cancel%> not within "
31635 "%<__transaction_atomic%>");
31636 return error_mark_node;
31639 stmt = build_tm_abort_call (token->location, is_outer);
31640 add_stmt (stmt);
31642 return stmt;
31645 /* The parser. */
31647 static GTY (()) cp_parser *the_parser;
31650 /* Special handling for the first token or line in the file. The first
31651 thing in the file might be #pragma GCC pch_preprocess, which loads a
31652 PCH file, which is a GC collection point. So we need to handle this
31653 first pragma without benefit of an existing lexer structure.
31655 Always returns one token to the caller in *FIRST_TOKEN. This is
31656 either the true first token of the file, or the first token after
31657 the initial pragma. */
31659 static void
31660 cp_parser_initial_pragma (cp_token *first_token)
31662 tree name = NULL;
31664 cp_lexer_get_preprocessor_token (NULL, first_token);
31665 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31666 return;
31668 cp_lexer_get_preprocessor_token (NULL, first_token);
31669 if (first_token->type == CPP_STRING)
31671 name = first_token->u.value;
31673 cp_lexer_get_preprocessor_token (NULL, first_token);
31674 if (first_token->type != CPP_PRAGMA_EOL)
31675 error_at (first_token->location,
31676 "junk at end of %<#pragma GCC pch_preprocess%>");
31678 else
31679 error_at (first_token->location, "expected string literal");
31681 /* Skip to the end of the pragma. */
31682 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31683 cp_lexer_get_preprocessor_token (NULL, first_token);
31685 /* Now actually load the PCH file. */
31686 if (name)
31687 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31689 /* Read one more token to return to our caller. We have to do this
31690 after reading the PCH file in, since its pointers have to be
31691 live. */
31692 cp_lexer_get_preprocessor_token (NULL, first_token);
31695 /* Normal parsing of a pragma token. Here we can (and must) use the
31696 regular lexer. */
31698 static bool
31699 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31701 cp_token *pragma_tok;
31702 unsigned int id;
31704 pragma_tok = cp_lexer_consume_token (parser->lexer);
31705 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31706 parser->lexer->in_pragma = true;
31708 id = pragma_tok->pragma_kind;
31709 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31710 cp_ensure_no_omp_declare_simd (parser);
31711 switch (id)
31713 case PRAGMA_GCC_PCH_PREPROCESS:
31714 error_at (pragma_tok->location,
31715 "%<#pragma GCC pch_preprocess%> must be first");
31716 break;
31718 case PRAGMA_OMP_BARRIER:
31719 switch (context)
31721 case pragma_compound:
31722 cp_parser_omp_barrier (parser, pragma_tok);
31723 return false;
31724 case pragma_stmt:
31725 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31726 "used in compound statements");
31727 break;
31728 default:
31729 goto bad_stmt;
31731 break;
31733 case PRAGMA_OMP_FLUSH:
31734 switch (context)
31736 case pragma_compound:
31737 cp_parser_omp_flush (parser, pragma_tok);
31738 return false;
31739 case pragma_stmt:
31740 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31741 "used in compound statements");
31742 break;
31743 default:
31744 goto bad_stmt;
31746 break;
31748 case PRAGMA_OMP_TASKWAIT:
31749 switch (context)
31751 case pragma_compound:
31752 cp_parser_omp_taskwait (parser, pragma_tok);
31753 return false;
31754 case pragma_stmt:
31755 error_at (pragma_tok->location,
31756 "%<#pragma omp taskwait%> may only be "
31757 "used in compound statements");
31758 break;
31759 default:
31760 goto bad_stmt;
31762 break;
31764 case PRAGMA_OMP_TASKYIELD:
31765 switch (context)
31767 case pragma_compound:
31768 cp_parser_omp_taskyield (parser, pragma_tok);
31769 return false;
31770 case pragma_stmt:
31771 error_at (pragma_tok->location,
31772 "%<#pragma omp taskyield%> may only be "
31773 "used in compound statements");
31774 break;
31775 default:
31776 goto bad_stmt;
31778 break;
31780 case PRAGMA_OMP_CANCEL:
31781 switch (context)
31783 case pragma_compound:
31784 cp_parser_omp_cancel (parser, pragma_tok);
31785 return false;
31786 case pragma_stmt:
31787 error_at (pragma_tok->location,
31788 "%<#pragma omp cancel%> may only be "
31789 "used in compound statements");
31790 break;
31791 default:
31792 goto bad_stmt;
31794 break;
31796 case PRAGMA_OMP_CANCELLATION_POINT:
31797 switch (context)
31799 case pragma_compound:
31800 cp_parser_omp_cancellation_point (parser, pragma_tok);
31801 return false;
31802 case pragma_stmt:
31803 error_at (pragma_tok->location,
31804 "%<#pragma omp cancellation point%> may only be "
31805 "used in compound statements");
31806 break;
31807 default:
31808 goto bad_stmt;
31810 break;
31812 case PRAGMA_OMP_THREADPRIVATE:
31813 cp_parser_omp_threadprivate (parser, pragma_tok);
31814 return false;
31816 case PRAGMA_OMP_DECLARE_REDUCTION:
31817 cp_parser_omp_declare (parser, pragma_tok, context);
31818 return false;
31820 case PRAGMA_OMP_ATOMIC:
31821 case PRAGMA_OMP_CRITICAL:
31822 case PRAGMA_OMP_DISTRIBUTE:
31823 case PRAGMA_OMP_FOR:
31824 case PRAGMA_OMP_MASTER:
31825 case PRAGMA_OMP_ORDERED:
31826 case PRAGMA_OMP_PARALLEL:
31827 case PRAGMA_OMP_SECTIONS:
31828 case PRAGMA_OMP_SIMD:
31829 case PRAGMA_OMP_SINGLE:
31830 case PRAGMA_OMP_TASK:
31831 case PRAGMA_OMP_TASKGROUP:
31832 case PRAGMA_OMP_TEAMS:
31833 if (context != pragma_stmt && context != pragma_compound)
31834 goto bad_stmt;
31835 cp_parser_omp_construct (parser, pragma_tok);
31836 return true;
31838 case PRAGMA_OMP_TARGET:
31839 return cp_parser_omp_target (parser, pragma_tok, context);
31841 case PRAGMA_OMP_END_DECLARE_TARGET:
31842 cp_parser_omp_end_declare_target (parser, pragma_tok);
31843 return false;
31845 case PRAGMA_OMP_SECTION:
31846 error_at (pragma_tok->location,
31847 "%<#pragma omp section%> may only be used in "
31848 "%<#pragma omp sections%> construct");
31849 break;
31851 case PRAGMA_IVDEP:
31853 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31854 cp_token *tok;
31855 tok = cp_lexer_peek_token (the_parser->lexer);
31856 if (tok->type != CPP_KEYWORD
31857 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31858 && tok->keyword != RID_DO))
31860 cp_parser_error (parser, "for, while or do statement expected");
31861 return false;
31863 cp_parser_iteration_statement (parser, true);
31864 return true;
31867 case PRAGMA_CILK_SIMD:
31868 if (context == pragma_external)
31870 error_at (pragma_tok->location,
31871 "%<#pragma simd%> must be inside a function");
31872 break;
31874 cp_parser_cilk_simd (parser, pragma_tok);
31875 return true;
31877 default:
31878 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31879 c_invoke_pragma_handler (id);
31880 break;
31882 bad_stmt:
31883 cp_parser_error (parser, "expected declaration specifiers");
31884 break;
31887 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31888 return false;
31891 /* The interface the pragma parsers have to the lexer. */
31893 enum cpp_ttype
31894 pragma_lex (tree *value)
31896 cp_token *tok;
31897 enum cpp_ttype ret;
31899 tok = cp_lexer_peek_token (the_parser->lexer);
31901 ret = tok->type;
31902 *value = tok->u.value;
31904 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31905 ret = CPP_EOF;
31906 else if (ret == CPP_STRING)
31907 *value = cp_parser_string_literal (the_parser, false, false);
31908 else
31910 cp_lexer_consume_token (the_parser->lexer);
31911 if (ret == CPP_KEYWORD)
31912 ret = CPP_NAME;
31915 return ret;
31919 /* External interface. */
31921 /* Parse one entire translation unit. */
31923 void
31924 c_parse_file (void)
31926 static bool already_called = false;
31928 if (already_called)
31929 fatal_error ("inter-module optimizations not implemented for C++");
31930 already_called = true;
31932 the_parser = cp_parser_new ();
31933 push_deferring_access_checks (flag_access_control
31934 ? dk_no_deferred : dk_no_check);
31935 cp_parser_translation_unit (the_parser);
31936 the_parser = NULL;
31939 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31940 vectorlength clause:
31941 Syntax:
31942 vectorlength ( constant-expression ) */
31944 static tree
31945 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31946 bool is_simd_fn)
31948 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31949 tree expr;
31950 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31951 safelen clause. Thus, vectorlength is represented as OMP 4.0
31952 safelen. For SIMD-enabled function it is represented by OMP 4.0
31953 simdlen. */
31954 if (!is_simd_fn)
31955 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31956 loc);
31957 else
31958 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31959 loc);
31961 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31962 return error_mark_node;
31964 expr = cp_parser_constant_expression (parser, false, NULL);
31965 expr = maybe_constant_value (expr);
31967 /* If expr == error_mark_node, then don't emit any errors nor
31968 create a clause. if any of the above functions returns
31969 error mark node then they would have emitted an error message. */
31970 if (expr == error_mark_node)
31972 else if (!TREE_TYPE (expr)
31973 || !TREE_CONSTANT (expr)
31974 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31975 error_at (loc, "vectorlength must be an integer constant");
31976 else if (TREE_CONSTANT (expr)
31977 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31978 error_at (loc, "vectorlength must be a power of 2");
31979 else
31981 tree c;
31982 if (!is_simd_fn)
31984 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31985 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31986 OMP_CLAUSE_CHAIN (c) = clauses;
31987 clauses = c;
31989 else
31991 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31992 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31993 OMP_CLAUSE_CHAIN (c) = clauses;
31994 clauses = c;
31998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31999 return error_mark_node;
32000 return clauses;
32003 /* Handles the Cilk Plus #pragma simd linear clause.
32004 Syntax:
32005 linear ( simd-linear-variable-list )
32007 simd-linear-variable-list:
32008 simd-linear-variable
32009 simd-linear-variable-list , simd-linear-variable
32011 simd-linear-variable:
32012 id-expression
32013 id-expression : simd-linear-step
32015 simd-linear-step:
32016 conditional-expression */
32018 static tree
32019 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32021 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32023 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32024 return clauses;
32025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32027 cp_parser_error (parser, "expected identifier");
32028 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32029 return error_mark_node;
32032 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32033 parser->colon_corrects_to_scope_p = false;
32034 while (1)
32036 cp_token *token = cp_lexer_peek_token (parser->lexer);
32037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32039 cp_parser_error (parser, "expected variable-name");
32040 clauses = error_mark_node;
32041 break;
32044 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32045 false, false);
32046 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32047 token->location);
32048 if (decl == error_mark_node)
32050 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32051 token->location);
32052 clauses = error_mark_node;
32054 else
32056 tree e = NULL_TREE;
32057 tree step_size = integer_one_node;
32059 /* If present, parse the linear step. Otherwise, assume the default
32060 value of 1. */
32061 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32063 cp_lexer_consume_token (parser->lexer);
32065 e = cp_parser_assignment_expression (parser, false, NULL);
32066 e = maybe_constant_value (e);
32068 if (e == error_mark_node)
32070 /* If an error has occurred, then the whole pragma is
32071 considered ill-formed. Thus, no reason to keep
32072 parsing. */
32073 clauses = error_mark_node;
32074 break;
32076 else if (type_dependent_expression_p (e)
32077 || value_dependent_expression_p (e)
32078 || (TREE_TYPE (e)
32079 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32080 && (TREE_CONSTANT (e)
32081 || DECL_P (e))))
32082 step_size = e;
32083 else
32084 cp_parser_error (parser,
32085 "step size must be an integer constant "
32086 "expression or an integer variable");
32089 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32090 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32091 OMP_CLAUSE_DECL (l) = decl;
32092 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32093 OMP_CLAUSE_CHAIN (l) = clauses;
32094 clauses = l;
32096 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32097 cp_lexer_consume_token (parser->lexer);
32098 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32099 break;
32100 else
32102 error_at (cp_lexer_peek_token (parser->lexer)->location,
32103 "expected %<,%> or %<)%> after %qE", decl);
32104 clauses = error_mark_node;
32105 break;
32108 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32109 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32110 return clauses;
32113 /* Returns the name of the next clause. If the clause is not
32114 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32115 token is not consumed. Otherwise, the appropriate enum from the
32116 pragma_simd_clause is returned and the token is consumed. */
32118 static pragma_omp_clause
32119 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32121 pragma_omp_clause clause_type;
32122 cp_token *token = cp_lexer_peek_token (parser->lexer);
32124 if (token->keyword == RID_PRIVATE)
32125 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32126 else if (!token->u.value || token->type != CPP_NAME)
32127 return PRAGMA_CILK_CLAUSE_NONE;
32128 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32129 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32130 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32131 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32132 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32133 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32134 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32135 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32136 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32137 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32138 else
32139 return PRAGMA_CILK_CLAUSE_NONE;
32141 cp_lexer_consume_token (parser->lexer);
32142 return clause_type;
32145 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32147 static tree
32148 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32150 tree clauses = NULL_TREE;
32152 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32153 && clauses != error_mark_node)
32155 pragma_omp_clause c_kind;
32156 c_kind = cp_parser_cilk_simd_clause_name (parser);
32157 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32158 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32159 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32160 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32161 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32162 /* Use the OpenMP 4.0 equivalent function. */
32163 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32164 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32165 /* Use the OpenMP 4.0 equivalent function. */
32166 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32167 clauses);
32168 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32169 /* Use the OMP 4.0 equivalent function. */
32170 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32171 clauses);
32172 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32173 /* Use the OMP 4.0 equivalent function. */
32174 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32175 else
32177 clauses = error_mark_node;
32178 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32179 break;
32183 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32185 if (clauses == error_mark_node)
32186 return error_mark_node;
32187 else
32188 return c_finish_cilk_clauses (clauses);
32191 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32193 static void
32194 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32196 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32198 if (clauses == error_mark_node)
32199 return;
32201 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32203 error_at (cp_lexer_peek_token (parser->lexer)->location,
32204 "for statement expected");
32205 return;
32208 tree sb = begin_omp_structured_block ();
32209 int save = cp_parser_begin_omp_structured_block (parser);
32210 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32211 if (ret)
32212 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32213 cp_parser_end_omp_structured_block (parser, save);
32214 add_stmt (finish_omp_structured_block (sb));
32215 return;
32218 /* Create an identifier for a generic parameter type (a synthesized
32219 template parameter implied by `auto' or a concept identifier). */
32221 static GTY(()) int generic_parm_count;
32222 static tree
32223 make_generic_type_name ()
32225 char buf[32];
32226 sprintf (buf, "auto:%d", ++generic_parm_count);
32227 return get_identifier (buf);
32230 /* Predicate that behaves as is_auto_or_concept but matches the parent
32231 node of the generic type rather than the generic type itself. This
32232 allows for type transformation in add_implicit_template_parms. */
32234 static inline bool
32235 tree_type_is_auto_or_concept (const_tree t)
32237 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32240 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32241 (creating a new template parameter list if necessary). Returns the newly
32242 created template type parm. */
32244 tree
32245 synthesize_implicit_template_parm (cp_parser *parser)
32247 gcc_assert (current_binding_level->kind == sk_function_parms);
32249 /* We are either continuing a function template that already contains implicit
32250 template parameters, creating a new fully-implicit function template, or
32251 extending an existing explicit function template with implicit template
32252 parameters. */
32254 cp_binding_level *const entry_scope = current_binding_level;
32256 bool become_template = false;
32257 cp_binding_level *parent_scope = 0;
32259 if (parser->implicit_template_scope)
32261 gcc_assert (parser->implicit_template_parms);
32263 current_binding_level = parser->implicit_template_scope;
32265 else
32267 /* Roll back to the existing template parameter scope (in the case of
32268 extending an explicit function template) or introduce a new template
32269 parameter scope ahead of the function parameter scope (or class scope
32270 in the case of out-of-line member definitions). The function scope is
32271 added back after template parameter synthesis below. */
32273 cp_binding_level *scope = entry_scope;
32275 while (scope->kind == sk_function_parms)
32277 parent_scope = scope;
32278 scope = scope->level_chain;
32280 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32282 /* If not defining a class, then any class scope is a scope level in
32283 an out-of-line member definition. In this case simply wind back
32284 beyond the first such scope to inject the template parameter list.
32285 Otherwise wind back to the class being defined. The latter can
32286 occur in class member friend declarations such as:
32288 class A {
32289 void foo (auto);
32291 class B {
32292 friend void A::foo (auto);
32295 The template parameter list synthesized for the friend declaration
32296 must be injected in the scope of 'B'. This can also occur in
32297 erroneous cases such as:
32299 struct A {
32300 struct B {
32301 void foo (auto);
32303 void B::foo (auto) {}
32306 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32307 but, nevertheless, the template parameter list synthesized for the
32308 declarator should be injected into the scope of 'A' as if the
32309 ill-formed template was specified explicitly. */
32311 while (scope->kind == sk_class && !scope->defining_class_p)
32313 parent_scope = scope;
32314 scope = scope->level_chain;
32318 current_binding_level = scope;
32320 if (scope->kind != sk_template_parms
32321 || !function_being_declared_is_template_p (parser))
32323 /* Introduce a new template parameter list for implicit template
32324 parameters. */
32326 become_template = true;
32328 parser->implicit_template_scope
32329 = begin_scope (sk_template_parms, NULL);
32331 ++processing_template_decl;
32333 parser->fully_implicit_function_template_p = true;
32334 ++parser->num_template_parameter_lists;
32336 else
32338 /* Synthesize implicit template parameters at the end of the explicit
32339 template parameter list. */
32341 gcc_assert (current_template_parms);
32343 parser->implicit_template_scope = scope;
32345 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32346 parser->implicit_template_parms
32347 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32351 /* Synthesize a new template parameter and track the current template
32352 parameter chain with implicit_template_parms. */
32354 tree synth_id = make_generic_type_name ();
32355 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32356 synth_id);
32357 tree new_parm
32358 = process_template_parm (parser->implicit_template_parms,
32359 input_location,
32360 build_tree_list (NULL_TREE, synth_tmpl_parm),
32361 /*non_type=*/false,
32362 /*param_pack=*/false);
32365 if (parser->implicit_template_parms)
32366 parser->implicit_template_parms
32367 = TREE_CHAIN (parser->implicit_template_parms);
32368 else
32369 parser->implicit_template_parms = new_parm;
32371 tree new_type = TREE_TYPE (getdecls ());
32373 /* If creating a fully implicit function template, start the new implicit
32374 template parameter list with this synthesized type, otherwise grow the
32375 current template parameter list. */
32377 if (become_template)
32379 parent_scope->level_chain = current_binding_level;
32381 tree new_parms = make_tree_vec (1);
32382 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32383 current_template_parms = tree_cons (size_int (processing_template_decl),
32384 new_parms, current_template_parms);
32386 else
32388 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32389 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32390 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32391 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32394 current_binding_level = entry_scope;
32396 return new_type;
32399 /* Finish the declaration of a fully implicit function template. Such a
32400 template has no explicit template parameter list so has not been through the
32401 normal template head and tail processing. synthesize_implicit_template_parm
32402 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32403 provided if the declaration is a class member such that its template
32404 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32405 form is returned. Otherwise NULL_TREE is returned. */
32407 tree
32408 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32410 gcc_assert (parser->fully_implicit_function_template_p);
32412 if (member_decl_opt && member_decl_opt != error_mark_node
32413 && DECL_VIRTUAL_P (member_decl_opt))
32415 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32416 "implicit templates may not be %<virtual%>");
32417 DECL_VIRTUAL_P (member_decl_opt) = false;
32420 if (member_decl_opt)
32421 member_decl_opt = finish_member_template_decl (member_decl_opt);
32422 end_template_decl ();
32424 parser->fully_implicit_function_template_p = false;
32425 --parser->num_template_parameter_lists;
32427 return member_decl_opt;
32430 #include "gt-cp-parser.h"