* parser.c (cp_parser_type_id_1): Only allow 'auto' in C++1y if
[official-gcc.git] / gcc / cp / parser.c
blob572528358f5bcfc6cf694ef9b180b4bf14328055
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_alloc_cleared_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_alloc_cleared_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;
766 /* On some systems, some header files are surrounded by an
767 implicit extern "C" block. Set a flag in the token if it
768 comes from such a header. */
769 is_extern_c += pending_lang_change;
770 pending_lang_change = 0;
771 token->implicit_extern_c = is_extern_c > 0;
773 /* Check to see if this token is a keyword. */
774 if (token->type == CPP_NAME)
776 if (C_IS_RESERVED_WORD (token->u.value))
778 /* Mark this token as a keyword. */
779 token->type = CPP_KEYWORD;
780 /* Record which keyword. */
781 token->keyword = C_RID_CODE (token->u.value);
783 else
785 if (warn_cxx0x_compat
786 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
787 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
789 /* Warn about the C++0x keyword (but still treat it as
790 an identifier). */
791 warning (OPT_Wc__0x_compat,
792 "identifier %qE is a keyword in C++11",
793 token->u.value);
795 /* Clear out the C_RID_CODE so we don't warn about this
796 particular identifier-turned-keyword again. */
797 C_SET_RID_CODE (token->u.value, RID_MAX);
800 token->ambiguous_p = false;
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_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
897 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
900 /* Return true if the next token is not the indicated KEYWORD. */
902 static inline bool
903 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
905 return cp_lexer_peek_token (lexer)->keyword != keyword;
908 /* Return true if the next token is a keyword for a decl-specifier. */
910 static bool
911 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
913 cp_token *token;
915 token = cp_lexer_peek_token (lexer);
916 switch (token->keyword)
918 /* auto specifier: storage-class-specifier in C++,
919 simple-type-specifier in C++0x. */
920 case RID_AUTO:
921 /* Storage classes. */
922 case RID_REGISTER:
923 case RID_STATIC:
924 case RID_EXTERN:
925 case RID_MUTABLE:
926 case RID_THREAD:
927 /* Elaborated type specifiers. */
928 case RID_ENUM:
929 case RID_CLASS:
930 case RID_STRUCT:
931 case RID_UNION:
932 case RID_TYPENAME:
933 /* Simple type specifiers. */
934 case RID_CHAR:
935 case RID_CHAR16:
936 case RID_CHAR32:
937 case RID_WCHAR:
938 case RID_BOOL:
939 case RID_SHORT:
940 case RID_INT:
941 case RID_LONG:
942 case RID_INT128:
943 case RID_SIGNED:
944 case RID_UNSIGNED:
945 case RID_FLOAT:
946 case RID_DOUBLE:
947 case RID_VOID:
948 /* GNU extensions. */
949 case RID_ATTRIBUTE:
950 case RID_TYPEOF:
951 /* C++0x extensions. */
952 case RID_DECLTYPE:
953 case RID_UNDERLYING_TYPE:
954 return true;
956 default:
957 return false;
961 /* Returns TRUE iff the token T begins a decltype type. */
963 static bool
964 token_is_decltype (cp_token *t)
966 return (t->keyword == RID_DECLTYPE
967 || t->type == CPP_DECLTYPE);
970 /* Returns TRUE iff the next token begins a decltype type. */
972 static bool
973 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
975 cp_token *t = cp_lexer_peek_token (lexer);
976 return token_is_decltype (t);
979 /* Return a pointer to the Nth token in the token stream. If N is 1,
980 then this is precisely equivalent to cp_lexer_peek_token (except
981 that it is not inline). One would like to disallow that case, but
982 there is one case (cp_parser_nth_token_starts_template_id) where
983 the caller passes a variable for N and it might be 1. */
985 static cp_token *
986 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
988 cp_token *token;
990 /* N is 1-based, not zero-based. */
991 gcc_assert (n > 0);
993 if (cp_lexer_debugging_p (lexer))
994 fprintf (cp_lexer_debug_stream,
995 "cp_lexer: peeking ahead %ld at token: ", (long)n);
997 --n;
998 token = lexer->next_token;
999 gcc_assert (!n || token != &eof_token);
1000 while (n != 0)
1002 ++token;
1003 if (token == lexer->last_token)
1005 token = &eof_token;
1006 break;
1009 if (!token->purged_p)
1010 --n;
1013 if (cp_lexer_debugging_p (lexer))
1015 cp_lexer_print_token (cp_lexer_debug_stream, token);
1016 putc ('\n', cp_lexer_debug_stream);
1019 return token;
1022 /* Return the next token, and advance the lexer's next_token pointer
1023 to point to the next non-purged token. */
1025 static cp_token *
1026 cp_lexer_consume_token (cp_lexer* lexer)
1028 cp_token *token = lexer->next_token;
1030 gcc_assert (token != &eof_token);
1031 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1035 lexer->next_token++;
1036 if (lexer->next_token == lexer->last_token)
1038 lexer->next_token = &eof_token;
1039 break;
1043 while (lexer->next_token->purged_p);
1045 cp_lexer_set_source_position_from_token (token);
1047 /* Provide debugging output. */
1048 if (cp_lexer_debugging_p (lexer))
1050 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1051 cp_lexer_print_token (cp_lexer_debug_stream, token);
1052 putc ('\n', cp_lexer_debug_stream);
1055 return token;
1058 /* Permanently remove the next token from the token stream, and
1059 advance the next_token pointer to refer to the next non-purged
1060 token. */
1062 static void
1063 cp_lexer_purge_token (cp_lexer *lexer)
1065 cp_token *tok = lexer->next_token;
1067 gcc_assert (tok != &eof_token);
1068 tok->purged_p = true;
1069 tok->location = UNKNOWN_LOCATION;
1070 tok->u.value = NULL_TREE;
1071 tok->keyword = RID_MAX;
1075 tok++;
1076 if (tok == lexer->last_token)
1078 tok = &eof_token;
1079 break;
1082 while (tok->purged_p);
1083 lexer->next_token = tok;
1086 /* Permanently remove all tokens after TOK, up to, but not
1087 including, the token that will be returned next by
1088 cp_lexer_peek_token. */
1090 static void
1091 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1093 cp_token *peek = lexer->next_token;
1095 if (peek == &eof_token)
1096 peek = lexer->last_token;
1098 gcc_assert (tok < peek);
1100 for ( tok += 1; tok != peek; tok += 1)
1102 tok->purged_p = true;
1103 tok->location = UNKNOWN_LOCATION;
1104 tok->u.value = NULL_TREE;
1105 tok->keyword = RID_MAX;
1109 /* Begin saving tokens. All tokens consumed after this point will be
1110 preserved. */
1112 static void
1113 cp_lexer_save_tokens (cp_lexer* lexer)
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1117 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1119 lexer->saved_tokens.safe_push (lexer->next_token);
1122 /* Commit to the portion of the token stream most recently saved. */
1124 static void
1125 cp_lexer_commit_tokens (cp_lexer* lexer)
1127 /* Provide debugging output. */
1128 if (cp_lexer_debugging_p (lexer))
1129 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1131 lexer->saved_tokens.pop ();
1134 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1135 to the token stream. Stop saving tokens. */
1137 static void
1138 cp_lexer_rollback_tokens (cp_lexer* lexer)
1140 /* Provide debugging output. */
1141 if (cp_lexer_debugging_p (lexer))
1142 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1144 lexer->next_token = lexer->saved_tokens.pop ();
1147 /* Print a representation of the TOKEN on the STREAM. */
1149 static void
1150 cp_lexer_print_token (FILE * stream, cp_token *token)
1152 /* We don't use cpp_type2name here because the parser defines
1153 a few tokens of its own. */
1154 static const char *const token_names[] = {
1155 /* cpplib-defined token types */
1156 #define OP(e, s) #e,
1157 #define TK(e, s) #e,
1158 TTYPE_TABLE
1159 #undef OP
1160 #undef TK
1161 /* C++ parser token types - see "Manifest constants", above. */
1162 "KEYWORD",
1163 "TEMPLATE_ID",
1164 "NESTED_NAME_SPECIFIER",
1167 /* For some tokens, print the associated data. */
1168 switch (token->type)
1170 case CPP_KEYWORD:
1171 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1172 For example, `struct' is mapped to an INTEGER_CST. */
1173 if (!identifier_p (token->u.value))
1174 break;
1175 /* else fall through */
1176 case CPP_NAME:
1177 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1178 break;
1180 case CPP_STRING:
1181 case CPP_STRING16:
1182 case CPP_STRING32:
1183 case CPP_WSTRING:
1184 case CPP_UTF8STRING:
1185 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1186 break;
1188 case CPP_NUMBER:
1189 print_generic_expr (stream, token->u.value, 0);
1190 break;
1192 default:
1193 /* If we have a name for the token, print it out. Otherwise, we
1194 simply give the numeric code. */
1195 if (token->type < ARRAY_SIZE(token_names))
1196 fputs (token_names[token->type], stream);
1197 else
1198 fprintf (stream, "[%d]", token->type);
1199 break;
1203 DEBUG_FUNCTION void
1204 debug (cp_token &ref)
1206 cp_lexer_print_token (stderr, &ref);
1207 fprintf (stderr, "\n");
1210 DEBUG_FUNCTION void
1211 debug (cp_token *ptr)
1213 if (ptr)
1214 debug (*ptr);
1215 else
1216 fprintf (stderr, "<nil>\n");
1220 /* Start emitting debugging information. */
1222 static void
1223 cp_lexer_start_debugging (cp_lexer* lexer)
1225 lexer->debugging_p = true;
1226 cp_lexer_debug_stream = stderr;
1229 /* Stop emitting debugging information. */
1231 static void
1232 cp_lexer_stop_debugging (cp_lexer* lexer)
1234 lexer->debugging_p = false;
1235 cp_lexer_debug_stream = NULL;
1238 /* Create a new cp_token_cache, representing a range of tokens. */
1240 static cp_token_cache *
1241 cp_token_cache_new (cp_token *first, cp_token *last)
1243 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1244 cache->first = first;
1245 cache->last = last;
1246 return cache;
1249 /* Diagnose if #pragma omp declare simd isn't followed immediately
1250 by function declaration or definition. */
1252 static inline void
1253 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1255 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1257 error ("%<#pragma omp declare simd%> not immediately followed by "
1258 "function declaration or definition");
1259 parser->omp_declare_simd = NULL;
1263 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1264 and put that into "omp declare simd" attribute. */
1266 static inline void
1267 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1269 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1271 if (fndecl == error_mark_node)
1273 parser->omp_declare_simd = NULL;
1274 return;
1276 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1278 cp_ensure_no_omp_declare_simd (parser);
1279 return;
1284 /* Decl-specifiers. */
1286 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1288 static void
1289 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1291 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1294 /* Declarators. */
1296 /* Nothing other than the parser should be creating declarators;
1297 declarators are a semi-syntactic representation of C++ entities.
1298 Other parts of the front end that need to create entities (like
1299 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1301 static cp_declarator *make_call_declarator
1302 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1303 static cp_declarator *make_array_declarator
1304 (cp_declarator *, tree);
1305 static cp_declarator *make_pointer_declarator
1306 (cp_cv_quals, cp_declarator *, tree);
1307 static cp_declarator *make_reference_declarator
1308 (cp_cv_quals, cp_declarator *, bool, tree);
1309 static cp_parameter_declarator *make_parameter_declarator
1310 (cp_decl_specifier_seq *, cp_declarator *, tree);
1311 static cp_declarator *make_ptrmem_declarator
1312 (cp_cv_quals, tree, cp_declarator *, tree);
1314 /* An erroneous declarator. */
1315 static cp_declarator *cp_error_declarator;
1317 /* The obstack on which declarators and related data structures are
1318 allocated. */
1319 static struct obstack declarator_obstack;
1321 /* Alloc BYTES from the declarator memory pool. */
1323 static inline void *
1324 alloc_declarator (size_t bytes)
1326 return obstack_alloc (&declarator_obstack, bytes);
1329 /* Allocate a declarator of the indicated KIND. Clear fields that are
1330 common to all declarators. */
1332 static cp_declarator *
1333 make_declarator (cp_declarator_kind kind)
1335 cp_declarator *declarator;
1337 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1338 declarator->kind = kind;
1339 declarator->attributes = NULL_TREE;
1340 declarator->std_attributes = NULL_TREE;
1341 declarator->declarator = NULL;
1342 declarator->parameter_pack_p = false;
1343 declarator->id_loc = UNKNOWN_LOCATION;
1345 return declarator;
1348 /* Make a declarator for a generalized identifier. If
1349 QUALIFYING_SCOPE is non-NULL, the identifier is
1350 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1351 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1352 is, if any. */
1354 static cp_declarator *
1355 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1356 special_function_kind sfk)
1358 cp_declarator *declarator;
1360 /* It is valid to write:
1362 class C { void f(); };
1363 typedef C D;
1364 void D::f();
1366 The standard is not clear about whether `typedef const C D' is
1367 legal; as of 2002-09-15 the committee is considering that
1368 question. EDG 3.0 allows that syntax. Therefore, we do as
1369 well. */
1370 if (qualifying_scope && TYPE_P (qualifying_scope))
1371 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1373 gcc_assert (identifier_p (unqualified_name)
1374 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1375 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1377 declarator = make_declarator (cdk_id);
1378 declarator->u.id.qualifying_scope = qualifying_scope;
1379 declarator->u.id.unqualified_name = unqualified_name;
1380 declarator->u.id.sfk = sfk;
1382 return declarator;
1385 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1386 of modifiers such as const or volatile to apply to the pointer
1387 type, represented as identifiers. ATTRIBUTES represent the attributes that
1388 appertain to the pointer or reference. */
1390 cp_declarator *
1391 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1392 tree attributes)
1394 cp_declarator *declarator;
1396 declarator = make_declarator (cdk_pointer);
1397 declarator->declarator = target;
1398 declarator->u.pointer.qualifiers = cv_qualifiers;
1399 declarator->u.pointer.class_type = NULL_TREE;
1400 if (target)
1402 declarator->id_loc = target->id_loc;
1403 declarator->parameter_pack_p = target->parameter_pack_p;
1404 target->parameter_pack_p = false;
1406 else
1407 declarator->parameter_pack_p = false;
1409 declarator->std_attributes = attributes;
1411 return declarator;
1414 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1415 represent the attributes that appertain to the pointer or
1416 reference. */
1418 cp_declarator *
1419 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1420 bool rvalue_ref, tree attributes)
1422 cp_declarator *declarator;
1424 declarator = make_declarator (cdk_reference);
1425 declarator->declarator = target;
1426 declarator->u.reference.qualifiers = cv_qualifiers;
1427 declarator->u.reference.rvalue_ref = rvalue_ref;
1428 if (target)
1430 declarator->id_loc = target->id_loc;
1431 declarator->parameter_pack_p = target->parameter_pack_p;
1432 target->parameter_pack_p = false;
1434 else
1435 declarator->parameter_pack_p = false;
1437 declarator->std_attributes = attributes;
1439 return declarator;
1442 /* Like make_pointer_declarator -- but for a pointer to a non-static
1443 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1446 cp_declarator *
1447 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1448 cp_declarator *pointee,
1449 tree attributes)
1451 cp_declarator *declarator;
1453 declarator = make_declarator (cdk_ptrmem);
1454 declarator->declarator = pointee;
1455 declarator->u.pointer.qualifiers = cv_qualifiers;
1456 declarator->u.pointer.class_type = class_type;
1458 if (pointee)
1460 declarator->parameter_pack_p = pointee->parameter_pack_p;
1461 pointee->parameter_pack_p = false;
1463 else
1464 declarator->parameter_pack_p = false;
1466 declarator->std_attributes = attributes;
1468 return declarator;
1471 /* Make a declarator for the function given by TARGET, with the
1472 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1473 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1474 indicates what exceptions can be thrown. */
1476 cp_declarator *
1477 make_call_declarator (cp_declarator *target,
1478 tree parms,
1479 cp_cv_quals cv_qualifiers,
1480 cp_virt_specifiers virt_specifiers,
1481 cp_ref_qualifier ref_qualifier,
1482 tree exception_specification,
1483 tree late_return_type)
1485 cp_declarator *declarator;
1487 declarator = make_declarator (cdk_function);
1488 declarator->declarator = target;
1489 declarator->u.function.parameters = parms;
1490 declarator->u.function.qualifiers = cv_qualifiers;
1491 declarator->u.function.virt_specifiers = virt_specifiers;
1492 declarator->u.function.ref_qualifier = ref_qualifier;
1493 declarator->u.function.exception_specification = exception_specification;
1494 declarator->u.function.late_return_type = late_return_type;
1495 if (target)
1497 declarator->id_loc = target->id_loc;
1498 declarator->parameter_pack_p = target->parameter_pack_p;
1499 target->parameter_pack_p = false;
1501 else
1502 declarator->parameter_pack_p = false;
1504 return declarator;
1507 /* Make a declarator for an array of BOUNDS elements, each of which is
1508 defined by ELEMENT. */
1510 cp_declarator *
1511 make_array_declarator (cp_declarator *element, tree bounds)
1513 cp_declarator *declarator;
1515 declarator = make_declarator (cdk_array);
1516 declarator->declarator = element;
1517 declarator->u.array.bounds = bounds;
1518 if (element)
1520 declarator->id_loc = element->id_loc;
1521 declarator->parameter_pack_p = element->parameter_pack_p;
1522 element->parameter_pack_p = false;
1524 else
1525 declarator->parameter_pack_p = false;
1527 return declarator;
1530 /* Determine whether the declarator we've seen so far can be a
1531 parameter pack, when followed by an ellipsis. */
1532 static bool
1533 declarator_can_be_parameter_pack (cp_declarator *declarator)
1535 /* Search for a declarator name, or any other declarator that goes
1536 after the point where the ellipsis could appear in a parameter
1537 pack. If we find any of these, then this declarator can not be
1538 made into a parameter pack. */
1539 bool found = false;
1540 while (declarator && !found)
1542 switch ((int)declarator->kind)
1544 case cdk_id:
1545 case cdk_array:
1546 found = true;
1547 break;
1549 case cdk_error:
1550 return true;
1552 default:
1553 declarator = declarator->declarator;
1554 break;
1558 return !found;
1561 cp_parameter_declarator *no_parameters;
1563 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1564 DECLARATOR and DEFAULT_ARGUMENT. */
1566 cp_parameter_declarator *
1567 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1568 cp_declarator *declarator,
1569 tree default_argument)
1571 cp_parameter_declarator *parameter;
1573 parameter = ((cp_parameter_declarator *)
1574 alloc_declarator (sizeof (cp_parameter_declarator)));
1575 parameter->next = NULL;
1576 if (decl_specifiers)
1577 parameter->decl_specifiers = *decl_specifiers;
1578 else
1579 clear_decl_specs (&parameter->decl_specifiers);
1580 parameter->declarator = declarator;
1581 parameter->default_argument = default_argument;
1582 parameter->ellipsis_p = false;
1584 return parameter;
1587 /* Returns true iff DECLARATOR is a declaration for a function. */
1589 static bool
1590 function_declarator_p (const cp_declarator *declarator)
1592 while (declarator)
1594 if (declarator->kind == cdk_function
1595 && declarator->declarator->kind == cdk_id)
1596 return true;
1597 if (declarator->kind == cdk_id
1598 || declarator->kind == cdk_error)
1599 return false;
1600 declarator = declarator->declarator;
1602 return false;
1605 /* The parser. */
1607 /* Overview
1608 --------
1610 A cp_parser parses the token stream as specified by the C++
1611 grammar. Its job is purely parsing, not semantic analysis. For
1612 example, the parser breaks the token stream into declarators,
1613 expressions, statements, and other similar syntactic constructs.
1614 It does not check that the types of the expressions on either side
1615 of an assignment-statement are compatible, or that a function is
1616 not declared with a parameter of type `void'.
1618 The parser invokes routines elsewhere in the compiler to perform
1619 semantic analysis and to build up the abstract syntax tree for the
1620 code processed.
1622 The parser (and the template instantiation code, which is, in a
1623 way, a close relative of parsing) are the only parts of the
1624 compiler that should be calling push_scope and pop_scope, or
1625 related functions. The parser (and template instantiation code)
1626 keeps track of what scope is presently active; everything else
1627 should simply honor that. (The code that generates static
1628 initializers may also need to set the scope, in order to check
1629 access control correctly when emitting the initializers.)
1631 Methodology
1632 -----------
1634 The parser is of the standard recursive-descent variety. Upcoming
1635 tokens in the token stream are examined in order to determine which
1636 production to use when parsing a non-terminal. Some C++ constructs
1637 require arbitrary look ahead to disambiguate. For example, it is
1638 impossible, in the general case, to tell whether a statement is an
1639 expression or declaration without scanning the entire statement.
1640 Therefore, the parser is capable of "parsing tentatively." When the
1641 parser is not sure what construct comes next, it enters this mode.
1642 Then, while we attempt to parse the construct, the parser queues up
1643 error messages, rather than issuing them immediately, and saves the
1644 tokens it consumes. If the construct is parsed successfully, the
1645 parser "commits", i.e., it issues any queued error messages and
1646 the tokens that were being preserved are permanently discarded.
1647 If, however, the construct is not parsed successfully, the parser
1648 rolls back its state completely so that it can resume parsing using
1649 a different alternative.
1651 Future Improvements
1652 -------------------
1654 The performance of the parser could probably be improved substantially.
1655 We could often eliminate the need to parse tentatively by looking ahead
1656 a little bit. In some places, this approach might not entirely eliminate
1657 the need to parse tentatively, but it might still speed up the average
1658 case. */
1660 /* Flags that are passed to some parsing functions. These values can
1661 be bitwise-ored together. */
1663 enum
1665 /* No flags. */
1666 CP_PARSER_FLAGS_NONE = 0x0,
1667 /* The construct is optional. If it is not present, then no error
1668 should be issued. */
1669 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1670 /* When parsing a type-specifier, treat user-defined type-names
1671 as non-type identifiers. */
1672 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1673 /* When parsing a type-specifier, do not try to parse a class-specifier
1674 or enum-specifier. */
1675 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1676 /* When parsing a decl-specifier-seq, only allow type-specifier or
1677 constexpr. */
1678 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1681 /* This type is used for parameters and variables which hold
1682 combinations of the above flags. */
1683 typedef int cp_parser_flags;
1685 /* The different kinds of declarators we want to parse. */
1687 typedef enum cp_parser_declarator_kind
1689 /* We want an abstract declarator. */
1690 CP_PARSER_DECLARATOR_ABSTRACT,
1691 /* We want a named declarator. */
1692 CP_PARSER_DECLARATOR_NAMED,
1693 /* We don't mind, but the name must be an unqualified-id. */
1694 CP_PARSER_DECLARATOR_EITHER
1695 } cp_parser_declarator_kind;
1697 /* The precedence values used to parse binary expressions. The minimum value
1698 of PREC must be 1, because zero is reserved to quickly discriminate
1699 binary operators from other tokens. */
1701 enum cp_parser_prec
1703 PREC_NOT_OPERATOR,
1704 PREC_LOGICAL_OR_EXPRESSION,
1705 PREC_LOGICAL_AND_EXPRESSION,
1706 PREC_INCLUSIVE_OR_EXPRESSION,
1707 PREC_EXCLUSIVE_OR_EXPRESSION,
1708 PREC_AND_EXPRESSION,
1709 PREC_EQUALITY_EXPRESSION,
1710 PREC_RELATIONAL_EXPRESSION,
1711 PREC_SHIFT_EXPRESSION,
1712 PREC_ADDITIVE_EXPRESSION,
1713 PREC_MULTIPLICATIVE_EXPRESSION,
1714 PREC_PM_EXPRESSION,
1715 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1718 /* A mapping from a token type to a corresponding tree node type, with a
1719 precedence value. */
1721 typedef struct cp_parser_binary_operations_map_node
1723 /* The token type. */
1724 enum cpp_ttype token_type;
1725 /* The corresponding tree code. */
1726 enum tree_code tree_type;
1727 /* The precedence of this operator. */
1728 enum cp_parser_prec prec;
1729 } cp_parser_binary_operations_map_node;
1731 typedef struct cp_parser_expression_stack_entry
1733 /* Left hand side of the binary operation we are currently
1734 parsing. */
1735 tree lhs;
1736 /* Original tree code for left hand side, if it was a binary
1737 expression itself (used for -Wparentheses). */
1738 enum tree_code lhs_type;
1739 /* Tree code for the binary operation we are parsing. */
1740 enum tree_code tree_type;
1741 /* Precedence of the binary operation we are parsing. */
1742 enum cp_parser_prec prec;
1743 /* Location of the binary operation we are parsing. */
1744 location_t loc;
1745 } cp_parser_expression_stack_entry;
1747 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1748 entries because precedence levels on the stack are monotonically
1749 increasing. */
1750 typedef struct cp_parser_expression_stack_entry
1751 cp_parser_expression_stack[NUM_PREC_VALUES];
1753 /* Prototypes. */
1755 /* Constructors and destructors. */
1757 static cp_parser_context *cp_parser_context_new
1758 (cp_parser_context *);
1760 /* Class variables. */
1762 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1764 /* The operator-precedence table used by cp_parser_binary_expression.
1765 Transformed into an associative array (binops_by_token) by
1766 cp_parser_new. */
1768 static const cp_parser_binary_operations_map_node binops[] = {
1769 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1770 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1772 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1773 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1774 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1776 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1777 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1779 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1780 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1782 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1783 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1784 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1785 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1787 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1788 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1790 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1792 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1794 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1796 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1798 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1801 /* The same as binops, but initialized by cp_parser_new so that
1802 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1803 for speed. */
1804 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1806 /* Constructors and destructors. */
1808 /* Construct a new context. The context below this one on the stack
1809 is given by NEXT. */
1811 static cp_parser_context *
1812 cp_parser_context_new (cp_parser_context* next)
1814 cp_parser_context *context;
1816 /* Allocate the storage. */
1817 if (cp_parser_context_free_list != NULL)
1819 /* Pull the first entry from the free list. */
1820 context = cp_parser_context_free_list;
1821 cp_parser_context_free_list = context->next;
1822 memset (context, 0, sizeof (*context));
1824 else
1825 context = ggc_alloc_cleared_cp_parser_context ();
1827 /* No errors have occurred yet in this context. */
1828 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1829 /* If this is not the bottommost context, copy information that we
1830 need from the previous context. */
1831 if (next)
1833 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1834 expression, then we are parsing one in this context, too. */
1835 context->object_type = next->object_type;
1836 /* Thread the stack. */
1837 context->next = next;
1840 return context;
1843 /* Managing the unparsed function queues. */
1845 #define unparsed_funs_with_default_args \
1846 parser->unparsed_queues->last ().funs_with_default_args
1847 #define unparsed_funs_with_definitions \
1848 parser->unparsed_queues->last ().funs_with_definitions
1849 #define unparsed_nsdmis \
1850 parser->unparsed_queues->last ().nsdmis
1852 static void
1853 push_unparsed_function_queues (cp_parser *parser)
1855 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1856 vec_safe_push (parser->unparsed_queues, e);
1859 static void
1860 pop_unparsed_function_queues (cp_parser *parser)
1862 release_tree_vector (unparsed_funs_with_definitions);
1863 parser->unparsed_queues->pop ();
1866 /* Prototypes. */
1868 /* Constructors and destructors. */
1870 static cp_parser *cp_parser_new
1871 (void);
1873 /* Routines to parse various constructs.
1875 Those that return `tree' will return the error_mark_node (rather
1876 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1877 Sometimes, they will return an ordinary node if error-recovery was
1878 attempted, even though a parse error occurred. So, to check
1879 whether or not a parse error occurred, you should always use
1880 cp_parser_error_occurred. If the construct is optional (indicated
1881 either by an `_opt' in the name of the function that does the
1882 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1883 the construct is not present. */
1885 /* Lexical conventions [gram.lex] */
1887 static tree cp_parser_identifier
1888 (cp_parser *);
1889 static tree cp_parser_string_literal
1890 (cp_parser *, bool, bool);
1891 static tree cp_parser_userdef_char_literal
1892 (cp_parser *);
1893 static tree cp_parser_userdef_string_literal
1894 (cp_token *);
1895 static tree cp_parser_userdef_numeric_literal
1896 (cp_parser *);
1898 /* Basic concepts [gram.basic] */
1900 static bool cp_parser_translation_unit
1901 (cp_parser *);
1903 /* Expressions [gram.expr] */
1905 static tree cp_parser_primary_expression
1906 (cp_parser *, bool, bool, bool, cp_id_kind *);
1907 static tree cp_parser_id_expression
1908 (cp_parser *, bool, bool, bool *, bool, bool);
1909 static tree cp_parser_unqualified_id
1910 (cp_parser *, bool, bool, bool, bool);
1911 static tree cp_parser_nested_name_specifier_opt
1912 (cp_parser *, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier
1914 (cp_parser *, bool, bool, bool, bool);
1915 static tree cp_parser_qualifying_entity
1916 (cp_parser *, bool, bool, bool, bool, bool);
1917 static tree cp_parser_postfix_expression
1918 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1919 static tree cp_parser_postfix_open_square_expression
1920 (cp_parser *, tree, bool, bool);
1921 static tree cp_parser_postfix_dot_deref_expression
1922 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1923 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1924 (cp_parser *, int, bool, bool, bool *);
1925 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1926 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1927 static void cp_parser_pseudo_destructor_name
1928 (cp_parser *, tree, tree *, tree *);
1929 static tree cp_parser_unary_expression
1930 (cp_parser *, bool, bool, cp_id_kind *);
1931 static enum tree_code cp_parser_unary_operator
1932 (cp_token *);
1933 static tree cp_parser_new_expression
1934 (cp_parser *);
1935 static vec<tree, va_gc> *cp_parser_new_placement
1936 (cp_parser *);
1937 static tree cp_parser_new_type_id
1938 (cp_parser *, tree *);
1939 static cp_declarator *cp_parser_new_declarator_opt
1940 (cp_parser *);
1941 static cp_declarator *cp_parser_direct_new_declarator
1942 (cp_parser *);
1943 static vec<tree, va_gc> *cp_parser_new_initializer
1944 (cp_parser *);
1945 static tree cp_parser_delete_expression
1946 (cp_parser *);
1947 static tree cp_parser_cast_expression
1948 (cp_parser *, bool, bool, bool, cp_id_kind *);
1949 static tree cp_parser_binary_expression
1950 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1951 static tree cp_parser_question_colon_clause
1952 (cp_parser *, tree);
1953 static tree cp_parser_assignment_expression
1954 (cp_parser *, bool, cp_id_kind *);
1955 static enum tree_code cp_parser_assignment_operator_opt
1956 (cp_parser *);
1957 static tree cp_parser_expression
1958 (cp_parser *, bool, cp_id_kind *);
1959 static tree cp_parser_expression
1960 (cp_parser *, bool, bool, cp_id_kind *);
1961 static tree cp_parser_constant_expression
1962 (cp_parser *, bool, bool *);
1963 static tree cp_parser_builtin_offsetof
1964 (cp_parser *);
1965 static tree cp_parser_lambda_expression
1966 (cp_parser *);
1967 static void cp_parser_lambda_introducer
1968 (cp_parser *, tree);
1969 static bool cp_parser_lambda_declarator_opt
1970 (cp_parser *, tree);
1971 static void cp_parser_lambda_body
1972 (cp_parser *, tree);
1974 /* Statements [gram.stmt.stmt] */
1976 static void cp_parser_statement
1977 (cp_parser *, tree, bool, bool *);
1978 static void cp_parser_label_for_labeled_statement
1979 (cp_parser *, tree);
1980 static tree cp_parser_expression_statement
1981 (cp_parser *, tree);
1982 static tree cp_parser_compound_statement
1983 (cp_parser *, tree, bool, bool);
1984 static void cp_parser_statement_seq_opt
1985 (cp_parser *, tree);
1986 static tree cp_parser_selection_statement
1987 (cp_parser *, bool *);
1988 static tree cp_parser_condition
1989 (cp_parser *);
1990 static tree cp_parser_iteration_statement
1991 (cp_parser *, bool);
1992 static bool cp_parser_for_init_statement
1993 (cp_parser *, tree *decl);
1994 static tree cp_parser_for
1995 (cp_parser *, bool);
1996 static tree cp_parser_c_for
1997 (cp_parser *, tree, tree, bool);
1998 static tree cp_parser_range_for
1999 (cp_parser *, tree, tree, tree, bool);
2000 static void do_range_for_auto_deduction
2001 (tree, tree);
2002 static tree cp_parser_perform_range_for_lookup
2003 (tree, tree *, tree *);
2004 static tree cp_parser_range_for_member_function
2005 (tree, tree);
2006 static tree cp_parser_jump_statement
2007 (cp_parser *);
2008 static void cp_parser_declaration_statement
2009 (cp_parser *);
2011 static tree cp_parser_implicitly_scoped_statement
2012 (cp_parser *, bool *);
2013 static void cp_parser_already_scoped_statement
2014 (cp_parser *);
2016 /* Declarations [gram.dcl.dcl] */
2018 static void cp_parser_declaration_seq_opt
2019 (cp_parser *);
2020 static void cp_parser_declaration
2021 (cp_parser *);
2022 static void cp_parser_block_declaration
2023 (cp_parser *, bool);
2024 static void cp_parser_simple_declaration
2025 (cp_parser *, bool, tree *);
2026 static void cp_parser_decl_specifier_seq
2027 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2028 static tree cp_parser_storage_class_specifier_opt
2029 (cp_parser *);
2030 static tree cp_parser_function_specifier_opt
2031 (cp_parser *, cp_decl_specifier_seq *);
2032 static tree cp_parser_type_specifier
2033 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2034 int *, bool *);
2035 static tree cp_parser_simple_type_specifier
2036 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2037 static tree cp_parser_type_name
2038 (cp_parser *);
2039 static tree cp_parser_nonclass_name
2040 (cp_parser* parser);
2041 static tree cp_parser_elaborated_type_specifier
2042 (cp_parser *, bool, bool);
2043 static tree cp_parser_enum_specifier
2044 (cp_parser *);
2045 static void cp_parser_enumerator_list
2046 (cp_parser *, tree);
2047 static void cp_parser_enumerator_definition
2048 (cp_parser *, tree);
2049 static tree cp_parser_namespace_name
2050 (cp_parser *);
2051 static void cp_parser_namespace_definition
2052 (cp_parser *);
2053 static void cp_parser_namespace_body
2054 (cp_parser *);
2055 static tree cp_parser_qualified_namespace_specifier
2056 (cp_parser *);
2057 static void cp_parser_namespace_alias_definition
2058 (cp_parser *);
2059 static bool cp_parser_using_declaration
2060 (cp_parser *, bool);
2061 static void cp_parser_using_directive
2062 (cp_parser *);
2063 static tree cp_parser_alias_declaration
2064 (cp_parser *);
2065 static void cp_parser_asm_definition
2066 (cp_parser *);
2067 static void cp_parser_linkage_specification
2068 (cp_parser *);
2069 static void cp_parser_static_assert
2070 (cp_parser *, bool);
2071 static tree cp_parser_decltype
2072 (cp_parser *);
2074 /* Declarators [gram.dcl.decl] */
2076 static tree cp_parser_init_declarator
2077 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2078 static cp_declarator *cp_parser_declarator
2079 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2080 static cp_declarator *cp_parser_direct_declarator
2081 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2082 static enum tree_code cp_parser_ptr_operator
2083 (cp_parser *, tree *, cp_cv_quals *, tree *);
2084 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2085 (cp_parser *);
2086 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2087 (cp_parser *);
2088 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2089 (cp_parser *);
2090 static tree cp_parser_late_return_type_opt
2091 (cp_parser *, cp_declarator *, cp_cv_quals);
2092 static tree cp_parser_declarator_id
2093 (cp_parser *, bool);
2094 static tree cp_parser_type_id
2095 (cp_parser *);
2096 static tree cp_parser_template_type_arg
2097 (cp_parser *);
2098 static tree cp_parser_trailing_type_id (cp_parser *);
2099 static tree cp_parser_type_id_1
2100 (cp_parser *, bool, bool);
2101 static void cp_parser_type_specifier_seq
2102 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2103 static tree cp_parser_parameter_declaration_clause
2104 (cp_parser *);
2105 static tree cp_parser_parameter_declaration_list
2106 (cp_parser *, bool *);
2107 static cp_parameter_declarator *cp_parser_parameter_declaration
2108 (cp_parser *, bool, bool *);
2109 static tree cp_parser_default_argument
2110 (cp_parser *, bool);
2111 static void cp_parser_function_body
2112 (cp_parser *, bool);
2113 static tree cp_parser_initializer
2114 (cp_parser *, bool *, bool *);
2115 static tree cp_parser_initializer_clause
2116 (cp_parser *, bool *);
2117 static tree cp_parser_braced_list
2118 (cp_parser*, bool*);
2119 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2120 (cp_parser *, bool *);
2122 static bool cp_parser_ctor_initializer_opt_and_function_body
2123 (cp_parser *, bool);
2125 static tree cp_parser_late_parsing_omp_declare_simd
2126 (cp_parser *, tree);
2128 static tree cp_parser_late_parsing_cilk_simd_fn_info
2129 (cp_parser *, tree);
2131 static tree synthesize_implicit_template_parm
2132 (cp_parser *);
2133 static tree finish_fully_implicit_template
2134 (cp_parser *, tree);
2136 /* Classes [gram.class] */
2138 static tree cp_parser_class_name
2139 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2140 static tree cp_parser_class_specifier
2141 (cp_parser *);
2142 static tree cp_parser_class_head
2143 (cp_parser *, bool *);
2144 static enum tag_types cp_parser_class_key
2145 (cp_parser *);
2146 static void cp_parser_member_specification_opt
2147 (cp_parser *);
2148 static void cp_parser_member_declaration
2149 (cp_parser *);
2150 static tree cp_parser_pure_specifier
2151 (cp_parser *);
2152 static tree cp_parser_constant_initializer
2153 (cp_parser *);
2155 /* Derived classes [gram.class.derived] */
2157 static tree cp_parser_base_clause
2158 (cp_parser *);
2159 static tree cp_parser_base_specifier
2160 (cp_parser *);
2162 /* Special member functions [gram.special] */
2164 static tree cp_parser_conversion_function_id
2165 (cp_parser *);
2166 static tree cp_parser_conversion_type_id
2167 (cp_parser *);
2168 static cp_declarator *cp_parser_conversion_declarator_opt
2169 (cp_parser *);
2170 static bool cp_parser_ctor_initializer_opt
2171 (cp_parser *);
2172 static void cp_parser_mem_initializer_list
2173 (cp_parser *);
2174 static tree cp_parser_mem_initializer
2175 (cp_parser *);
2176 static tree cp_parser_mem_initializer_id
2177 (cp_parser *);
2179 /* Overloading [gram.over] */
2181 static tree cp_parser_operator_function_id
2182 (cp_parser *);
2183 static tree cp_parser_operator
2184 (cp_parser *);
2186 /* Templates [gram.temp] */
2188 static void cp_parser_template_declaration
2189 (cp_parser *, bool);
2190 static tree cp_parser_template_parameter_list
2191 (cp_parser *);
2192 static tree cp_parser_template_parameter
2193 (cp_parser *, bool *, bool *);
2194 static tree cp_parser_type_parameter
2195 (cp_parser *, bool *);
2196 static tree cp_parser_template_id
2197 (cp_parser *, bool, bool, enum tag_types, bool);
2198 static tree cp_parser_template_name
2199 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2200 static tree cp_parser_template_argument_list
2201 (cp_parser *);
2202 static tree cp_parser_template_argument
2203 (cp_parser *);
2204 static void cp_parser_explicit_instantiation
2205 (cp_parser *);
2206 static void cp_parser_explicit_specialization
2207 (cp_parser *);
2209 /* Exception handling [gram.exception] */
2211 static tree cp_parser_try_block
2212 (cp_parser *);
2213 static bool cp_parser_function_try_block
2214 (cp_parser *);
2215 static void cp_parser_handler_seq
2216 (cp_parser *);
2217 static void cp_parser_handler
2218 (cp_parser *);
2219 static tree cp_parser_exception_declaration
2220 (cp_parser *);
2221 static tree cp_parser_throw_expression
2222 (cp_parser *);
2223 static tree cp_parser_exception_specification_opt
2224 (cp_parser *);
2225 static tree cp_parser_type_id_list
2226 (cp_parser *);
2228 /* GNU Extensions */
2230 static tree cp_parser_asm_specification_opt
2231 (cp_parser *);
2232 static tree cp_parser_asm_operand_list
2233 (cp_parser *);
2234 static tree cp_parser_asm_clobber_list
2235 (cp_parser *);
2236 static tree cp_parser_asm_label_list
2237 (cp_parser *);
2238 static bool cp_next_tokens_can_be_attribute_p
2239 (cp_parser *);
2240 static bool cp_next_tokens_can_be_gnu_attribute_p
2241 (cp_parser *);
2242 static bool cp_next_tokens_can_be_std_attribute_p
2243 (cp_parser *);
2244 static bool cp_nth_tokens_can_be_std_attribute_p
2245 (cp_parser *, size_t);
2246 static bool cp_nth_tokens_can_be_gnu_attribute_p
2247 (cp_parser *, size_t);
2248 static bool cp_nth_tokens_can_be_attribute_p
2249 (cp_parser *, size_t);
2250 static tree cp_parser_attributes_opt
2251 (cp_parser *);
2252 static tree cp_parser_gnu_attributes_opt
2253 (cp_parser *);
2254 static tree cp_parser_gnu_attribute_list
2255 (cp_parser *);
2256 static tree cp_parser_std_attribute
2257 (cp_parser *);
2258 static tree cp_parser_std_attribute_spec
2259 (cp_parser *);
2260 static tree cp_parser_std_attribute_spec_seq
2261 (cp_parser *);
2262 static bool cp_parser_extension_opt
2263 (cp_parser *, int *);
2264 static void cp_parser_label_declaration
2265 (cp_parser *);
2267 /* Transactional Memory Extensions */
2269 static tree cp_parser_transaction
2270 (cp_parser *, enum rid);
2271 static tree cp_parser_transaction_expression
2272 (cp_parser *, enum rid);
2273 static bool cp_parser_function_transaction
2274 (cp_parser *, enum rid);
2275 static tree cp_parser_transaction_cancel
2276 (cp_parser *);
2278 enum pragma_context {
2279 pragma_external,
2280 pragma_member,
2281 pragma_objc_icode,
2282 pragma_stmt,
2283 pragma_compound
2285 static bool cp_parser_pragma
2286 (cp_parser *, enum pragma_context);
2288 /* Objective-C++ Productions */
2290 static tree cp_parser_objc_message_receiver
2291 (cp_parser *);
2292 static tree cp_parser_objc_message_args
2293 (cp_parser *);
2294 static tree cp_parser_objc_message_expression
2295 (cp_parser *);
2296 static tree cp_parser_objc_encode_expression
2297 (cp_parser *);
2298 static tree cp_parser_objc_defs_expression
2299 (cp_parser *);
2300 static tree cp_parser_objc_protocol_expression
2301 (cp_parser *);
2302 static tree cp_parser_objc_selector_expression
2303 (cp_parser *);
2304 static tree cp_parser_objc_expression
2305 (cp_parser *);
2306 static bool cp_parser_objc_selector_p
2307 (enum cpp_ttype);
2308 static tree cp_parser_objc_selector
2309 (cp_parser *);
2310 static tree cp_parser_objc_protocol_refs_opt
2311 (cp_parser *);
2312 static void cp_parser_objc_declaration
2313 (cp_parser *, tree);
2314 static tree cp_parser_objc_statement
2315 (cp_parser *);
2316 static bool cp_parser_objc_valid_prefix_attributes
2317 (cp_parser *, tree *);
2318 static void cp_parser_objc_at_property_declaration
2319 (cp_parser *) ;
2320 static void cp_parser_objc_at_synthesize_declaration
2321 (cp_parser *) ;
2322 static void cp_parser_objc_at_dynamic_declaration
2323 (cp_parser *) ;
2324 static tree cp_parser_objc_struct_declaration
2325 (cp_parser *) ;
2327 /* Utility Routines */
2329 static tree cp_parser_lookup_name
2330 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2331 static tree cp_parser_lookup_name_simple
2332 (cp_parser *, tree, location_t);
2333 static tree cp_parser_maybe_treat_template_as_class
2334 (tree, bool);
2335 static bool cp_parser_check_declarator_template_parameters
2336 (cp_parser *, cp_declarator *, location_t);
2337 static bool cp_parser_check_template_parameters
2338 (cp_parser *, unsigned, location_t, cp_declarator *);
2339 static tree cp_parser_simple_cast_expression
2340 (cp_parser *);
2341 static tree cp_parser_global_scope_opt
2342 (cp_parser *, bool);
2343 static bool cp_parser_constructor_declarator_p
2344 (cp_parser *, bool);
2345 static tree cp_parser_function_definition_from_specifiers_and_declarator
2346 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2347 static tree cp_parser_function_definition_after_declarator
2348 (cp_parser *, bool);
2349 static void cp_parser_template_declaration_after_export
2350 (cp_parser *, bool);
2351 static void cp_parser_perform_template_parameter_access_checks
2352 (vec<deferred_access_check, va_gc> *);
2353 static tree cp_parser_single_declaration
2354 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2355 static tree cp_parser_functional_cast
2356 (cp_parser *, tree);
2357 static tree cp_parser_save_member_function_body
2358 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2359 static tree cp_parser_save_nsdmi
2360 (cp_parser *);
2361 static tree cp_parser_enclosed_template_argument_list
2362 (cp_parser *);
2363 static void cp_parser_save_default_args
2364 (cp_parser *, tree);
2365 static void cp_parser_late_parsing_for_member
2366 (cp_parser *, tree);
2367 static tree cp_parser_late_parse_one_default_arg
2368 (cp_parser *, tree, tree, tree);
2369 static void cp_parser_late_parsing_nsdmi
2370 (cp_parser *, tree);
2371 static void cp_parser_late_parsing_default_args
2372 (cp_parser *, tree);
2373 static tree cp_parser_sizeof_operand
2374 (cp_parser *, enum rid);
2375 static tree cp_parser_trait_expr
2376 (cp_parser *, enum rid);
2377 static bool cp_parser_declares_only_class_p
2378 (cp_parser *);
2379 static void cp_parser_set_storage_class
2380 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2381 static void cp_parser_set_decl_spec_type
2382 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2383 static void set_and_check_decl_spec_loc
2384 (cp_decl_specifier_seq *decl_specs,
2385 cp_decl_spec ds, cp_token *);
2386 static bool cp_parser_friend_p
2387 (const cp_decl_specifier_seq *);
2388 static void cp_parser_required_error
2389 (cp_parser *, required_token, bool);
2390 static cp_token *cp_parser_require
2391 (cp_parser *, enum cpp_ttype, required_token);
2392 static cp_token *cp_parser_require_keyword
2393 (cp_parser *, enum rid, required_token);
2394 static bool cp_parser_token_starts_function_definition_p
2395 (cp_token *);
2396 static bool cp_parser_next_token_starts_class_definition_p
2397 (cp_parser *);
2398 static bool cp_parser_next_token_ends_template_argument_p
2399 (cp_parser *);
2400 static bool cp_parser_nth_token_starts_template_argument_list_p
2401 (cp_parser *, size_t);
2402 static enum tag_types cp_parser_token_is_class_key
2403 (cp_token *);
2404 static void cp_parser_check_class_key
2405 (enum tag_types, tree type);
2406 static void cp_parser_check_access_in_redeclaration
2407 (tree type, location_t location);
2408 static bool cp_parser_optional_template_keyword
2409 (cp_parser *);
2410 static void cp_parser_pre_parsed_nested_name_specifier
2411 (cp_parser *);
2412 static bool cp_parser_cache_group
2413 (cp_parser *, enum cpp_ttype, unsigned);
2414 static tree cp_parser_cache_defarg
2415 (cp_parser *parser, bool nsdmi);
2416 static void cp_parser_parse_tentatively
2417 (cp_parser *);
2418 static void cp_parser_commit_to_tentative_parse
2419 (cp_parser *);
2420 static void cp_parser_commit_to_topmost_tentative_parse
2421 (cp_parser *);
2422 static void cp_parser_abort_tentative_parse
2423 (cp_parser *);
2424 static bool cp_parser_parse_definitely
2425 (cp_parser *);
2426 static inline bool cp_parser_parsing_tentatively
2427 (cp_parser *);
2428 static bool cp_parser_uncommitted_to_tentative_parse_p
2429 (cp_parser *);
2430 static void cp_parser_error
2431 (cp_parser *, const char *);
2432 static void cp_parser_name_lookup_error
2433 (cp_parser *, tree, tree, name_lookup_error, location_t);
2434 static bool cp_parser_simulate_error
2435 (cp_parser *);
2436 static bool cp_parser_check_type_definition
2437 (cp_parser *);
2438 static void cp_parser_check_for_definition_in_return_type
2439 (cp_declarator *, tree, location_t type_location);
2440 static void cp_parser_check_for_invalid_template_id
2441 (cp_parser *, tree, enum tag_types, location_t location);
2442 static bool cp_parser_non_integral_constant_expression
2443 (cp_parser *, non_integral_constant);
2444 static void cp_parser_diagnose_invalid_type_name
2445 (cp_parser *, tree, tree, location_t);
2446 static bool cp_parser_parse_and_diagnose_invalid_type_name
2447 (cp_parser *);
2448 static int cp_parser_skip_to_closing_parenthesis
2449 (cp_parser *, bool, bool, bool);
2450 static void cp_parser_skip_to_end_of_statement
2451 (cp_parser *);
2452 static void cp_parser_consume_semicolon_at_end_of_statement
2453 (cp_parser *);
2454 static void cp_parser_skip_to_end_of_block_or_statement
2455 (cp_parser *);
2456 static bool cp_parser_skip_to_closing_brace
2457 (cp_parser *);
2458 static void cp_parser_skip_to_end_of_template_parameter_list
2459 (cp_parser *);
2460 static void cp_parser_skip_to_pragma_eol
2461 (cp_parser*, cp_token *);
2462 static bool cp_parser_error_occurred
2463 (cp_parser *);
2464 static bool cp_parser_allow_gnu_extensions_p
2465 (cp_parser *);
2466 static bool cp_parser_is_pure_string_literal
2467 (cp_token *);
2468 static bool cp_parser_is_string_literal
2469 (cp_token *);
2470 static bool cp_parser_is_keyword
2471 (cp_token *, enum rid);
2472 static tree cp_parser_make_typename_type
2473 (cp_parser *, tree, tree, location_t location);
2474 static cp_declarator * cp_parser_make_indirect_declarator
2475 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2477 /* Returns nonzero if we are parsing tentatively. */
2479 static inline bool
2480 cp_parser_parsing_tentatively (cp_parser* parser)
2482 return parser->context->next != NULL;
2485 /* Returns nonzero if TOKEN is a string literal. */
2487 static bool
2488 cp_parser_is_pure_string_literal (cp_token* token)
2490 return (token->type == CPP_STRING ||
2491 token->type == CPP_STRING16 ||
2492 token->type == CPP_STRING32 ||
2493 token->type == CPP_WSTRING ||
2494 token->type == CPP_UTF8STRING);
2497 /* Returns nonzero if TOKEN is a string literal
2498 of a user-defined string literal. */
2500 static bool
2501 cp_parser_is_string_literal (cp_token* token)
2503 return (cp_parser_is_pure_string_literal (token) ||
2504 token->type == CPP_STRING_USERDEF ||
2505 token->type == CPP_STRING16_USERDEF ||
2506 token->type == CPP_STRING32_USERDEF ||
2507 token->type == CPP_WSTRING_USERDEF ||
2508 token->type == CPP_UTF8STRING_USERDEF);
2511 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2513 static bool
2514 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2516 return token->keyword == keyword;
2519 /* If not parsing tentatively, issue a diagnostic of the form
2520 FILE:LINE: MESSAGE before TOKEN
2521 where TOKEN is the next token in the input stream. MESSAGE
2522 (specified by the caller) is usually of the form "expected
2523 OTHER-TOKEN". */
2525 static void
2526 cp_parser_error (cp_parser* parser, const char* gmsgid)
2528 if (!cp_parser_simulate_error (parser))
2530 cp_token *token = cp_lexer_peek_token (parser->lexer);
2531 /* This diagnostic makes more sense if it is tagged to the line
2532 of the token we just peeked at. */
2533 cp_lexer_set_source_position_from_token (token);
2535 if (token->type == CPP_PRAGMA)
2537 error_at (token->location,
2538 "%<#pragma%> is not allowed here");
2539 cp_parser_skip_to_pragma_eol (parser, token);
2540 return;
2543 c_parse_error (gmsgid,
2544 /* Because c_parser_error does not understand
2545 CPP_KEYWORD, keywords are treated like
2546 identifiers. */
2547 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2548 token->u.value, token->flags);
2552 /* Issue an error about name-lookup failing. NAME is the
2553 IDENTIFIER_NODE DECL is the result of
2554 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2555 the thing that we hoped to find. */
2557 static void
2558 cp_parser_name_lookup_error (cp_parser* parser,
2559 tree name,
2560 tree decl,
2561 name_lookup_error desired,
2562 location_t location)
2564 /* If name lookup completely failed, tell the user that NAME was not
2565 declared. */
2566 if (decl == error_mark_node)
2568 if (parser->scope && parser->scope != global_namespace)
2569 error_at (location, "%<%E::%E%> has not been declared",
2570 parser->scope, name);
2571 else if (parser->scope == global_namespace)
2572 error_at (location, "%<::%E%> has not been declared", name);
2573 else if (parser->object_scope
2574 && !CLASS_TYPE_P (parser->object_scope))
2575 error_at (location, "request for member %qE in non-class type %qT",
2576 name, parser->object_scope);
2577 else if (parser->object_scope)
2578 error_at (location, "%<%T::%E%> has not been declared",
2579 parser->object_scope, name);
2580 else
2581 error_at (location, "%qE has not been declared", name);
2583 else if (parser->scope && parser->scope != global_namespace)
2585 switch (desired)
2587 case NLE_TYPE:
2588 error_at (location, "%<%E::%E%> is not a type",
2589 parser->scope, name);
2590 break;
2591 case NLE_CXX98:
2592 error_at (location, "%<%E::%E%> is not a class or namespace",
2593 parser->scope, name);
2594 break;
2595 case NLE_NOT_CXX98:
2596 error_at (location,
2597 "%<%E::%E%> is not a class, namespace, or enumeration",
2598 parser->scope, name);
2599 break;
2600 default:
2601 gcc_unreachable ();
2605 else if (parser->scope == global_namespace)
2607 switch (desired)
2609 case NLE_TYPE:
2610 error_at (location, "%<::%E%> is not a type", name);
2611 break;
2612 case NLE_CXX98:
2613 error_at (location, "%<::%E%> is not a class or namespace", name);
2614 break;
2615 case NLE_NOT_CXX98:
2616 error_at (location,
2617 "%<::%E%> is not a class, namespace, or enumeration",
2618 name);
2619 break;
2620 default:
2621 gcc_unreachable ();
2624 else
2626 switch (desired)
2628 case NLE_TYPE:
2629 error_at (location, "%qE is not a type", name);
2630 break;
2631 case NLE_CXX98:
2632 error_at (location, "%qE is not a class or namespace", name);
2633 break;
2634 case NLE_NOT_CXX98:
2635 error_at (location,
2636 "%qE is not a class, namespace, or enumeration", name);
2637 break;
2638 default:
2639 gcc_unreachable ();
2644 /* If we are parsing tentatively, remember that an error has occurred
2645 during this tentative parse. Returns true if the error was
2646 simulated; false if a message should be issued by the caller. */
2648 static bool
2649 cp_parser_simulate_error (cp_parser* parser)
2651 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2653 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2654 return true;
2656 return false;
2659 /* This function is called when a type is defined. If type
2660 definitions are forbidden at this point, an error message is
2661 issued. */
2663 static bool
2664 cp_parser_check_type_definition (cp_parser* parser)
2666 /* If types are forbidden here, issue a message. */
2667 if (parser->type_definition_forbidden_message)
2669 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2670 in the message need to be interpreted. */
2671 error (parser->type_definition_forbidden_message);
2672 return false;
2674 return true;
2677 /* This function is called when the DECLARATOR is processed. The TYPE
2678 was a type defined in the decl-specifiers. If it is invalid to
2679 define a type in the decl-specifiers for DECLARATOR, an error is
2680 issued. TYPE_LOCATION is the location of TYPE and is used
2681 for error reporting. */
2683 static void
2684 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2685 tree type, location_t type_location)
2687 /* [dcl.fct] forbids type definitions in return types.
2688 Unfortunately, it's not easy to know whether or not we are
2689 processing a return type until after the fact. */
2690 while (declarator
2691 && (declarator->kind == cdk_pointer
2692 || declarator->kind == cdk_reference
2693 || declarator->kind == cdk_ptrmem))
2694 declarator = declarator->declarator;
2695 if (declarator
2696 && declarator->kind == cdk_function)
2698 error_at (type_location,
2699 "new types may not be defined in a return type");
2700 inform (type_location,
2701 "(perhaps a semicolon is missing after the definition of %qT)",
2702 type);
2706 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2707 "<" in any valid C++ program. If the next token is indeed "<",
2708 issue a message warning the user about what appears to be an
2709 invalid attempt to form a template-id. LOCATION is the location
2710 of the type-specifier (TYPE) */
2712 static void
2713 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2714 tree type,
2715 enum tag_types tag_type,
2716 location_t location)
2718 cp_token_position start = 0;
2720 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2722 if (TYPE_P (type))
2723 error_at (location, "%qT is not a template", type);
2724 else if (identifier_p (type))
2726 if (tag_type != none_type)
2727 error_at (location, "%qE is not a class template", type);
2728 else
2729 error_at (location, "%qE is not a template", type);
2731 else
2732 error_at (location, "invalid template-id");
2733 /* Remember the location of the invalid "<". */
2734 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2735 start = cp_lexer_token_position (parser->lexer, true);
2736 /* Consume the "<". */
2737 cp_lexer_consume_token (parser->lexer);
2738 /* Parse the template arguments. */
2739 cp_parser_enclosed_template_argument_list (parser);
2740 /* Permanently remove the invalid template arguments so that
2741 this error message is not issued again. */
2742 if (start)
2743 cp_lexer_purge_tokens_after (parser->lexer, start);
2747 /* If parsing an integral constant-expression, issue an error message
2748 about the fact that THING appeared and return true. Otherwise,
2749 return false. In either case, set
2750 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2752 static bool
2753 cp_parser_non_integral_constant_expression (cp_parser *parser,
2754 non_integral_constant thing)
2756 parser->non_integral_constant_expression_p = true;
2757 if (parser->integral_constant_expression_p)
2759 if (!parser->allow_non_integral_constant_expression_p)
2761 const char *msg = NULL;
2762 switch (thing)
2764 case NIC_FLOAT:
2765 error ("floating-point literal "
2766 "cannot appear in a constant-expression");
2767 return true;
2768 case NIC_CAST:
2769 error ("a cast to a type other than an integral or "
2770 "enumeration type cannot appear in a "
2771 "constant-expression");
2772 return true;
2773 case NIC_TYPEID:
2774 error ("%<typeid%> operator "
2775 "cannot appear in a constant-expression");
2776 return true;
2777 case NIC_NCC:
2778 error ("non-constant compound literals "
2779 "cannot appear in a constant-expression");
2780 return true;
2781 case NIC_FUNC_CALL:
2782 error ("a function call "
2783 "cannot appear in a constant-expression");
2784 return true;
2785 case NIC_INC:
2786 error ("an increment "
2787 "cannot appear in a constant-expression");
2788 return true;
2789 case NIC_DEC:
2790 error ("an decrement "
2791 "cannot appear in a constant-expression");
2792 return true;
2793 case NIC_ARRAY_REF:
2794 error ("an array reference "
2795 "cannot appear in a constant-expression");
2796 return true;
2797 case NIC_ADDR_LABEL:
2798 error ("the address of a label "
2799 "cannot appear in a constant-expression");
2800 return true;
2801 case NIC_OVERLOADED:
2802 error ("calls to overloaded operators "
2803 "cannot appear in a constant-expression");
2804 return true;
2805 case NIC_ASSIGNMENT:
2806 error ("an assignment cannot appear in a constant-expression");
2807 return true;
2808 case NIC_COMMA:
2809 error ("a comma operator "
2810 "cannot appear in a constant-expression");
2811 return true;
2812 case NIC_CONSTRUCTOR:
2813 error ("a call to a constructor "
2814 "cannot appear in a constant-expression");
2815 return true;
2816 case NIC_TRANSACTION:
2817 error ("a transaction expression "
2818 "cannot appear in a constant-expression");
2819 return true;
2820 case NIC_THIS:
2821 msg = "this";
2822 break;
2823 case NIC_FUNC_NAME:
2824 msg = "__FUNCTION__";
2825 break;
2826 case NIC_PRETTY_FUNC:
2827 msg = "__PRETTY_FUNCTION__";
2828 break;
2829 case NIC_C99_FUNC:
2830 msg = "__func__";
2831 break;
2832 case NIC_VA_ARG:
2833 msg = "va_arg";
2834 break;
2835 case NIC_ARROW:
2836 msg = "->";
2837 break;
2838 case NIC_POINT:
2839 msg = ".";
2840 break;
2841 case NIC_STAR:
2842 msg = "*";
2843 break;
2844 case NIC_ADDR:
2845 msg = "&";
2846 break;
2847 case NIC_PREINCREMENT:
2848 msg = "++";
2849 break;
2850 case NIC_PREDECREMENT:
2851 msg = "--";
2852 break;
2853 case NIC_NEW:
2854 msg = "new";
2855 break;
2856 case NIC_DEL:
2857 msg = "delete";
2858 break;
2859 default:
2860 gcc_unreachable ();
2862 if (msg)
2863 error ("%qs cannot appear in a constant-expression", msg);
2864 return true;
2867 return false;
2870 /* Emit a diagnostic for an invalid type name. SCOPE is the
2871 qualifying scope (or NULL, if none) for ID. This function commits
2872 to the current active tentative parse, if any. (Otherwise, the
2873 problematic construct might be encountered again later, resulting
2874 in duplicate error messages.) LOCATION is the location of ID. */
2876 static void
2877 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2878 tree scope, tree id,
2879 location_t location)
2881 tree decl, old_scope;
2882 cp_parser_commit_to_tentative_parse (parser);
2883 /* Try to lookup the identifier. */
2884 old_scope = parser->scope;
2885 parser->scope = scope;
2886 decl = cp_parser_lookup_name_simple (parser, id, location);
2887 parser->scope = old_scope;
2888 /* If the lookup found a template-name, it means that the user forgot
2889 to specify an argument list. Emit a useful error message. */
2890 if (TREE_CODE (decl) == TEMPLATE_DECL)
2891 error_at (location,
2892 "invalid use of template-name %qE without an argument list",
2893 decl);
2894 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2895 error_at (location, "invalid use of destructor %qD as a type", id);
2896 else if (TREE_CODE (decl) == TYPE_DECL)
2897 /* Something like 'unsigned A a;' */
2898 error_at (location, "invalid combination of multiple type-specifiers");
2899 else if (!parser->scope)
2901 /* Issue an error message. */
2902 error_at (location, "%qE does not name a type", id);
2903 /* If we're in a template class, it's possible that the user was
2904 referring to a type from a base class. For example:
2906 template <typename T> struct A { typedef T X; };
2907 template <typename T> struct B : public A<T> { X x; };
2909 The user should have said "typename A<T>::X". */
2910 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2911 inform (location, "C++11 %<constexpr%> only available with "
2912 "-std=c++11 or -std=gnu++11");
2913 else if (processing_template_decl && current_class_type
2914 && TYPE_BINFO (current_class_type))
2916 tree b;
2918 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2920 b = TREE_CHAIN (b))
2922 tree base_type = BINFO_TYPE (b);
2923 if (CLASS_TYPE_P (base_type)
2924 && dependent_type_p (base_type))
2926 tree field;
2927 /* Go from a particular instantiation of the
2928 template (which will have an empty TYPE_FIELDs),
2929 to the main version. */
2930 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2931 for (field = TYPE_FIELDS (base_type);
2932 field;
2933 field = DECL_CHAIN (field))
2934 if (TREE_CODE (field) == TYPE_DECL
2935 && DECL_NAME (field) == id)
2937 inform (location,
2938 "(perhaps %<typename %T::%E%> was intended)",
2939 BINFO_TYPE (b), id);
2940 break;
2942 if (field)
2943 break;
2948 /* Here we diagnose qualified-ids where the scope is actually correct,
2949 but the identifier does not resolve to a valid type name. */
2950 else if (parser->scope != error_mark_node)
2952 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2954 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2955 error_at (location_of (id),
2956 "%qE in namespace %qE does not name a template type",
2957 id, parser->scope);
2958 else
2959 error_at (location_of (id),
2960 "%qE in namespace %qE does not name a type",
2961 id, parser->scope);
2963 else if (CLASS_TYPE_P (parser->scope)
2964 && constructor_name_p (id, parser->scope))
2966 /* A<T>::A<T>() */
2967 error_at (location, "%<%T::%E%> names the constructor, not"
2968 " the type", parser->scope, id);
2969 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2970 error_at (location, "and %qT has no template constructors",
2971 parser->scope);
2973 else if (TYPE_P (parser->scope)
2974 && dependent_scope_p (parser->scope))
2975 error_at (location, "need %<typename%> before %<%T::%E%> because "
2976 "%qT is a dependent scope",
2977 parser->scope, id, parser->scope);
2978 else if (TYPE_P (parser->scope))
2980 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2981 error_at (location_of (id),
2982 "%qE in %q#T does not name a template type",
2983 id, parser->scope);
2984 else
2985 error_at (location_of (id),
2986 "%qE in %q#T does not name a type",
2987 id, parser->scope);
2989 else
2990 gcc_unreachable ();
2994 /* Check for a common situation where a type-name should be present,
2995 but is not, and issue a sensible error message. Returns true if an
2996 invalid type-name was detected.
2998 The situation handled by this function are variable declarations of the
2999 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3000 Usually, `ID' should name a type, but if we got here it means that it
3001 does not. We try to emit the best possible error message depending on
3002 how exactly the id-expression looks like. */
3004 static bool
3005 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3007 tree id;
3008 cp_token *token = cp_lexer_peek_token (parser->lexer);
3010 /* Avoid duplicate error about ambiguous lookup. */
3011 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3013 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3014 if (next->type == CPP_NAME && next->ambiguous_p)
3015 goto out;
3018 cp_parser_parse_tentatively (parser);
3019 id = cp_parser_id_expression (parser,
3020 /*template_keyword_p=*/false,
3021 /*check_dependency_p=*/true,
3022 /*template_p=*/NULL,
3023 /*declarator_p=*/true,
3024 /*optional_p=*/false);
3025 /* If the next token is a (, this is a function with no explicit return
3026 type, i.e. constructor, destructor or conversion op. */
3027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3028 || TREE_CODE (id) == TYPE_DECL)
3030 cp_parser_abort_tentative_parse (parser);
3031 return false;
3033 if (!cp_parser_parse_definitely (parser))
3034 return false;
3036 /* Emit a diagnostic for the invalid type. */
3037 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3038 id, token->location);
3039 out:
3040 /* If we aren't in the middle of a declarator (i.e. in a
3041 parameter-declaration-clause), skip to the end of the declaration;
3042 there's no point in trying to process it. */
3043 if (!parser->in_declarator_p)
3044 cp_parser_skip_to_end_of_block_or_statement (parser);
3045 return true;
3048 /* Consume tokens up to, and including, the next non-nested closing `)'.
3049 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3050 are doing error recovery. Returns -1 if OR_COMMA is true and we
3051 found an unnested comma. */
3053 static int
3054 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3055 bool recovering,
3056 bool or_comma,
3057 bool consume_paren)
3059 unsigned paren_depth = 0;
3060 unsigned brace_depth = 0;
3061 unsigned square_depth = 0;
3063 if (recovering && !or_comma
3064 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3065 return 0;
3067 while (true)
3069 cp_token * token = cp_lexer_peek_token (parser->lexer);
3071 switch (token->type)
3073 case CPP_EOF:
3074 case CPP_PRAGMA_EOL:
3075 /* If we've run out of tokens, then there is no closing `)'. */
3076 return 0;
3078 /* This is good for lambda expression capture-lists. */
3079 case CPP_OPEN_SQUARE:
3080 ++square_depth;
3081 break;
3082 case CPP_CLOSE_SQUARE:
3083 if (!square_depth--)
3084 return 0;
3085 break;
3087 case CPP_SEMICOLON:
3088 /* This matches the processing in skip_to_end_of_statement. */
3089 if (!brace_depth)
3090 return 0;
3091 break;
3093 case CPP_OPEN_BRACE:
3094 ++brace_depth;
3095 break;
3096 case CPP_CLOSE_BRACE:
3097 if (!brace_depth--)
3098 return 0;
3099 break;
3101 case CPP_COMMA:
3102 if (recovering && or_comma && !brace_depth && !paren_depth
3103 && !square_depth)
3104 return -1;
3105 break;
3107 case CPP_OPEN_PAREN:
3108 if (!brace_depth)
3109 ++paren_depth;
3110 break;
3112 case CPP_CLOSE_PAREN:
3113 if (!brace_depth && !paren_depth--)
3115 if (consume_paren)
3116 cp_lexer_consume_token (parser->lexer);
3117 return 1;
3119 break;
3121 default:
3122 break;
3125 /* Consume the token. */
3126 cp_lexer_consume_token (parser->lexer);
3130 /* Consume tokens until we reach the end of the current statement.
3131 Normally, that will be just before consuming a `;'. However, if a
3132 non-nested `}' comes first, then we stop before consuming that. */
3134 static void
3135 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3137 unsigned nesting_depth = 0;
3139 while (true)
3141 cp_token *token = cp_lexer_peek_token (parser->lexer);
3143 switch (token->type)
3145 case CPP_EOF:
3146 case CPP_PRAGMA_EOL:
3147 /* If we've run out of tokens, stop. */
3148 return;
3150 case CPP_SEMICOLON:
3151 /* If the next token is a `;', we have reached the end of the
3152 statement. */
3153 if (!nesting_depth)
3154 return;
3155 break;
3157 case CPP_CLOSE_BRACE:
3158 /* If this is a non-nested '}', stop before consuming it.
3159 That way, when confronted with something like:
3161 { 3 + }
3163 we stop before consuming the closing '}', even though we
3164 have not yet reached a `;'. */
3165 if (nesting_depth == 0)
3166 return;
3168 /* If it is the closing '}' for a block that we have
3169 scanned, stop -- but only after consuming the token.
3170 That way given:
3172 void f g () { ... }
3173 typedef int I;
3175 we will stop after the body of the erroneously declared
3176 function, but before consuming the following `typedef'
3177 declaration. */
3178 if (--nesting_depth == 0)
3180 cp_lexer_consume_token (parser->lexer);
3181 return;
3184 case CPP_OPEN_BRACE:
3185 ++nesting_depth;
3186 break;
3188 default:
3189 break;
3192 /* Consume the token. */
3193 cp_lexer_consume_token (parser->lexer);
3197 /* This function is called at the end of a statement or declaration.
3198 If the next token is a semicolon, it is consumed; otherwise, error
3199 recovery is attempted. */
3201 static void
3202 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3204 /* Look for the trailing `;'. */
3205 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3207 /* If there is additional (erroneous) input, skip to the end of
3208 the statement. */
3209 cp_parser_skip_to_end_of_statement (parser);
3210 /* If the next token is now a `;', consume it. */
3211 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3212 cp_lexer_consume_token (parser->lexer);
3216 /* Skip tokens until we have consumed an entire block, or until we
3217 have consumed a non-nested `;'. */
3219 static void
3220 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3222 int nesting_depth = 0;
3224 while (nesting_depth >= 0)
3226 cp_token *token = cp_lexer_peek_token (parser->lexer);
3228 switch (token->type)
3230 case CPP_EOF:
3231 case CPP_PRAGMA_EOL:
3232 /* If we've run out of tokens, stop. */
3233 return;
3235 case CPP_SEMICOLON:
3236 /* Stop if this is an unnested ';'. */
3237 if (!nesting_depth)
3238 nesting_depth = -1;
3239 break;
3241 case CPP_CLOSE_BRACE:
3242 /* Stop if this is an unnested '}', or closes the outermost
3243 nesting level. */
3244 nesting_depth--;
3245 if (nesting_depth < 0)
3246 return;
3247 if (!nesting_depth)
3248 nesting_depth = -1;
3249 break;
3251 case CPP_OPEN_BRACE:
3252 /* Nest. */
3253 nesting_depth++;
3254 break;
3256 default:
3257 break;
3260 /* Consume the token. */
3261 cp_lexer_consume_token (parser->lexer);
3265 /* Skip tokens until a non-nested closing curly brace is the next
3266 token, or there are no more tokens. Return true in the first case,
3267 false otherwise. */
3269 static bool
3270 cp_parser_skip_to_closing_brace (cp_parser *parser)
3272 unsigned nesting_depth = 0;
3274 while (true)
3276 cp_token *token = cp_lexer_peek_token (parser->lexer);
3278 switch (token->type)
3280 case CPP_EOF:
3281 case CPP_PRAGMA_EOL:
3282 /* If we've run out of tokens, stop. */
3283 return false;
3285 case CPP_CLOSE_BRACE:
3286 /* If the next token is a non-nested `}', then we have reached
3287 the end of the current block. */
3288 if (nesting_depth-- == 0)
3289 return true;
3290 break;
3292 case CPP_OPEN_BRACE:
3293 /* If it the next token is a `{', then we are entering a new
3294 block. Consume the entire block. */
3295 ++nesting_depth;
3296 break;
3298 default:
3299 break;
3302 /* Consume the token. */
3303 cp_lexer_consume_token (parser->lexer);
3307 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3308 parameter is the PRAGMA token, allowing us to purge the entire pragma
3309 sequence. */
3311 static void
3312 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3314 cp_token *token;
3316 parser->lexer->in_pragma = false;
3319 token = cp_lexer_consume_token (parser->lexer);
3320 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3322 /* Ensure that the pragma is not parsed again. */
3323 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3326 /* Require pragma end of line, resyncing with it as necessary. The
3327 arguments are as for cp_parser_skip_to_pragma_eol. */
3329 static void
3330 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3332 parser->lexer->in_pragma = false;
3333 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3334 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3337 /* This is a simple wrapper around make_typename_type. When the id is
3338 an unresolved identifier node, we can provide a superior diagnostic
3339 using cp_parser_diagnose_invalid_type_name. */
3341 static tree
3342 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3343 tree id, location_t id_location)
3345 tree result;
3346 if (identifier_p (id))
3348 result = make_typename_type (scope, id, typename_type,
3349 /*complain=*/tf_none);
3350 if (result == error_mark_node)
3351 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3352 return result;
3354 return make_typename_type (scope, id, typename_type, tf_error);
3357 /* This is a wrapper around the
3358 make_{pointer,ptrmem,reference}_declarator functions that decides
3359 which one to call based on the CODE and CLASS_TYPE arguments. The
3360 CODE argument should be one of the values returned by
3361 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3362 appertain to the pointer or reference. */
3364 static cp_declarator *
3365 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3366 cp_cv_quals cv_qualifiers,
3367 cp_declarator *target,
3368 tree attributes)
3370 if (code == ERROR_MARK)
3371 return cp_error_declarator;
3373 if (code == INDIRECT_REF)
3374 if (class_type == NULL_TREE)
3375 return make_pointer_declarator (cv_qualifiers, target, attributes);
3376 else
3377 return make_ptrmem_declarator (cv_qualifiers, class_type,
3378 target, attributes);
3379 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3380 return make_reference_declarator (cv_qualifiers, target,
3381 false, attributes);
3382 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3383 return make_reference_declarator (cv_qualifiers, target,
3384 true, attributes);
3385 gcc_unreachable ();
3388 /* Create a new C++ parser. */
3390 static cp_parser *
3391 cp_parser_new (void)
3393 cp_parser *parser;
3394 cp_lexer *lexer;
3395 unsigned i;
3397 /* cp_lexer_new_main is called before doing GC allocation because
3398 cp_lexer_new_main might load a PCH file. */
3399 lexer = cp_lexer_new_main ();
3401 /* Initialize the binops_by_token so that we can get the tree
3402 directly from the token. */
3403 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3404 binops_by_token[binops[i].token_type] = binops[i];
3406 parser = ggc_alloc_cleared_cp_parser ();
3407 parser->lexer = lexer;
3408 parser->context = cp_parser_context_new (NULL);
3410 /* For now, we always accept GNU extensions. */
3411 parser->allow_gnu_extensions_p = 1;
3413 /* The `>' token is a greater-than operator, not the end of a
3414 template-id. */
3415 parser->greater_than_is_operator_p = true;
3417 parser->default_arg_ok_p = true;
3419 /* We are not parsing a constant-expression. */
3420 parser->integral_constant_expression_p = false;
3421 parser->allow_non_integral_constant_expression_p = false;
3422 parser->non_integral_constant_expression_p = false;
3424 /* Local variable names are not forbidden. */
3425 parser->local_variables_forbidden_p = false;
3427 /* We are not processing an `extern "C"' declaration. */
3428 parser->in_unbraced_linkage_specification_p = false;
3430 /* We are not processing a declarator. */
3431 parser->in_declarator_p = false;
3433 /* We are not processing a template-argument-list. */
3434 parser->in_template_argument_list_p = false;
3436 /* We are not in an iteration statement. */
3437 parser->in_statement = 0;
3439 /* We are not in a switch statement. */
3440 parser->in_switch_statement_p = false;
3442 /* We are not parsing a type-id inside an expression. */
3443 parser->in_type_id_in_expr_p = false;
3445 /* Declarations aren't implicitly extern "C". */
3446 parser->implicit_extern_c = false;
3448 /* String literals should be translated to the execution character set. */
3449 parser->translate_strings_p = true;
3451 /* We are not parsing a function body. */
3452 parser->in_function_body = false;
3454 /* We can correct until told otherwise. */
3455 parser->colon_corrects_to_scope_p = true;
3457 /* The unparsed function queue is empty. */
3458 push_unparsed_function_queues (parser);
3460 /* There are no classes being defined. */
3461 parser->num_classes_being_defined = 0;
3463 /* No template parameters apply. */
3464 parser->num_template_parameter_lists = 0;
3466 /* Not declaring an implicit function template. */
3467 parser->auto_is_implicit_function_template_parm_p = false;
3468 parser->fully_implicit_function_template_p = false;
3469 parser->implicit_template_parms = 0;
3470 parser->implicit_template_scope = 0;
3472 return parser;
3475 /* Create a cp_lexer structure which will emit the tokens in CACHE
3476 and push it onto the parser's lexer stack. This is used for delayed
3477 parsing of in-class method bodies and default arguments, and should
3478 not be confused with tentative parsing. */
3479 static void
3480 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3482 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3483 lexer->next = parser->lexer;
3484 parser->lexer = lexer;
3486 /* Move the current source position to that of the first token in the
3487 new lexer. */
3488 cp_lexer_set_source_position_from_token (lexer->next_token);
3491 /* Pop the top lexer off the parser stack. This is never used for the
3492 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3493 static void
3494 cp_parser_pop_lexer (cp_parser *parser)
3496 cp_lexer *lexer = parser->lexer;
3497 parser->lexer = lexer->next;
3498 cp_lexer_destroy (lexer);
3500 /* Put the current source position back where it was before this
3501 lexer was pushed. */
3502 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3505 /* Lexical conventions [gram.lex] */
3507 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3508 identifier. */
3510 static tree
3511 cp_parser_identifier (cp_parser* parser)
3513 cp_token *token;
3515 /* Look for the identifier. */
3516 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3517 /* Return the value. */
3518 return token ? token->u.value : error_mark_node;
3521 /* Parse a sequence of adjacent string constants. Returns a
3522 TREE_STRING representing the combined, nul-terminated string
3523 constant. If TRANSLATE is true, translate the string to the
3524 execution character set. If WIDE_OK is true, a wide string is
3525 invalid here.
3527 C++98 [lex.string] says that if a narrow string literal token is
3528 adjacent to a wide string literal token, the behavior is undefined.
3529 However, C99 6.4.5p4 says that this results in a wide string literal.
3530 We follow C99 here, for consistency with the C front end.
3532 This code is largely lifted from lex_string() in c-lex.c.
3534 FUTURE: ObjC++ will need to handle @-strings here. */
3535 static tree
3536 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3538 tree value;
3539 size_t count;
3540 struct obstack str_ob;
3541 cpp_string str, istr, *strs;
3542 cp_token *tok;
3543 enum cpp_ttype type, curr_type;
3544 int have_suffix_p = 0;
3545 tree string_tree;
3546 tree suffix_id = NULL_TREE;
3547 bool curr_tok_is_userdef_p = false;
3549 tok = cp_lexer_peek_token (parser->lexer);
3550 if (!cp_parser_is_string_literal (tok))
3552 cp_parser_error (parser, "expected string-literal");
3553 return error_mark_node;
3556 if (cpp_userdef_string_p (tok->type))
3558 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3559 curr_type = cpp_userdef_string_remove_type (tok->type);
3560 curr_tok_is_userdef_p = true;
3562 else
3564 string_tree = tok->u.value;
3565 curr_type = tok->type;
3567 type = curr_type;
3569 /* Try to avoid the overhead of creating and destroying an obstack
3570 for the common case of just one string. */
3571 if (!cp_parser_is_string_literal
3572 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3574 cp_lexer_consume_token (parser->lexer);
3576 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3577 str.len = TREE_STRING_LENGTH (string_tree);
3578 count = 1;
3580 if (curr_tok_is_userdef_p)
3582 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3583 have_suffix_p = 1;
3584 curr_type = cpp_userdef_string_remove_type (tok->type);
3586 else
3587 curr_type = tok->type;
3589 strs = &str;
3591 else
3593 gcc_obstack_init (&str_ob);
3594 count = 0;
3598 cp_lexer_consume_token (parser->lexer);
3599 count++;
3600 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3601 str.len = TREE_STRING_LENGTH (string_tree);
3603 if (curr_tok_is_userdef_p)
3605 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3606 if (have_suffix_p == 0)
3608 suffix_id = curr_suffix_id;
3609 have_suffix_p = 1;
3611 else if (have_suffix_p == 1
3612 && curr_suffix_id != suffix_id)
3614 error ("inconsistent user-defined literal suffixes"
3615 " %qD and %qD in string literal",
3616 suffix_id, curr_suffix_id);
3617 have_suffix_p = -1;
3619 curr_type = cpp_userdef_string_remove_type (tok->type);
3621 else
3622 curr_type = tok->type;
3624 if (type != curr_type)
3626 if (type == CPP_STRING)
3627 type = curr_type;
3628 else if (curr_type != CPP_STRING)
3629 error_at (tok->location,
3630 "unsupported non-standard concatenation "
3631 "of string literals");
3634 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3636 tok = cp_lexer_peek_token (parser->lexer);
3637 if (cpp_userdef_string_p (tok->type))
3639 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3640 curr_type = cpp_userdef_string_remove_type (tok->type);
3641 curr_tok_is_userdef_p = true;
3643 else
3645 string_tree = tok->u.value;
3646 curr_type = tok->type;
3647 curr_tok_is_userdef_p = false;
3650 while (cp_parser_is_string_literal (tok));
3652 strs = (cpp_string *) obstack_finish (&str_ob);
3655 if (type != CPP_STRING && !wide_ok)
3657 cp_parser_error (parser, "a wide string is invalid in this context");
3658 type = CPP_STRING;
3661 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3662 (parse_in, strs, count, &istr, type))
3664 value = build_string (istr.len, (const char *)istr.text);
3665 free (CONST_CAST (unsigned char *, istr.text));
3667 switch (type)
3669 default:
3670 case CPP_STRING:
3671 case CPP_UTF8STRING:
3672 TREE_TYPE (value) = char_array_type_node;
3673 break;
3674 case CPP_STRING16:
3675 TREE_TYPE (value) = char16_array_type_node;
3676 break;
3677 case CPP_STRING32:
3678 TREE_TYPE (value) = char32_array_type_node;
3679 break;
3680 case CPP_WSTRING:
3681 TREE_TYPE (value) = wchar_array_type_node;
3682 break;
3685 value = fix_string_type (value);
3687 if (have_suffix_p)
3689 tree literal = build_userdef_literal (suffix_id, value,
3690 OT_NONE, NULL_TREE);
3691 tok->u.value = literal;
3692 return cp_parser_userdef_string_literal (tok);
3695 else
3696 /* cpp_interpret_string has issued an error. */
3697 value = error_mark_node;
3699 if (count > 1)
3700 obstack_free (&str_ob, 0);
3702 return value;
3705 /* Look up a literal operator with the name and the exact arguments. */
3707 static tree
3708 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3710 tree decl, fns;
3711 decl = lookup_name (name);
3712 if (!decl || !is_overloaded_fn (decl))
3713 return error_mark_node;
3715 for (fns = decl; fns; fns = OVL_NEXT (fns))
3717 unsigned int ix;
3718 bool found = true;
3719 tree fn = OVL_CURRENT (fns);
3720 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3721 if (parmtypes != NULL_TREE)
3723 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3724 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3726 tree tparm = TREE_VALUE (parmtypes);
3727 tree targ = TREE_TYPE ((*args)[ix]);
3728 bool ptr = TYPE_PTR_P (tparm);
3729 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3730 if ((ptr || arr || !same_type_p (tparm, targ))
3731 && (!ptr || !arr
3732 || !same_type_p (TREE_TYPE (tparm),
3733 TREE_TYPE (targ))))
3734 found = false;
3736 if (found
3737 && ix == vec_safe_length (args)
3738 /* May be this should be sufficient_parms_p instead,
3739 depending on how exactly should user-defined literals
3740 work in presence of default arguments on the literal
3741 operator parameters. */
3742 && parmtypes == void_list_node)
3743 return fn;
3747 return error_mark_node;
3750 /* Parse a user-defined char constant. Returns a call to a user-defined
3751 literal operator taking the character as an argument. */
3753 static tree
3754 cp_parser_userdef_char_literal (cp_parser *parser)
3756 cp_token *token = cp_lexer_consume_token (parser->lexer);
3757 tree literal = token->u.value;
3758 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3759 tree value = USERDEF_LITERAL_VALUE (literal);
3760 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3761 tree decl, result;
3763 /* Build up a call to the user-defined operator */
3764 /* Lookup the name we got back from the id-expression. */
3765 vec<tree, va_gc> *args = make_tree_vector ();
3766 vec_safe_push (args, value);
3767 decl = lookup_literal_operator (name, args);
3768 if (!decl || decl == error_mark_node)
3770 error ("unable to find character literal operator %qD with %qT argument",
3771 name, TREE_TYPE (value));
3772 release_tree_vector (args);
3773 return error_mark_node;
3775 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3776 release_tree_vector (args);
3777 if (result != error_mark_node)
3778 return result;
3780 error ("unable to find character literal operator %qD with %qT argument",
3781 name, TREE_TYPE (value));
3782 return error_mark_node;
3785 /* A subroutine of cp_parser_userdef_numeric_literal to
3786 create a char... template parameter pack from a string node. */
3788 static tree
3789 make_char_string_pack (tree value)
3791 tree charvec;
3792 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3793 const char *str = TREE_STRING_POINTER (value);
3794 int i, len = TREE_STRING_LENGTH (value) - 1;
3795 tree argvec = make_tree_vec (1);
3797 /* Fill in CHARVEC with all of the parameters. */
3798 charvec = make_tree_vec (len);
3799 for (i = 0; i < len; ++i)
3800 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3802 /* Build the argument packs. */
3803 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3804 TREE_TYPE (argpack) = char_type_node;
3806 TREE_VEC_ELT (argvec, 0) = argpack;
3808 return argvec;
3811 /* A subroutine of cp_parser_userdef_numeric_literal to
3812 create a char... template parameter pack from a string node. */
3814 static tree
3815 make_string_pack (tree value)
3817 tree charvec;
3818 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3819 const unsigned char *str
3820 = (const unsigned char *) TREE_STRING_POINTER (value);
3821 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3822 int len = TREE_STRING_LENGTH (value) / sz - 1;
3823 tree argvec = make_tree_vec (2);
3825 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3826 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3828 /* First template parm is character type. */
3829 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3831 /* Fill in CHARVEC with all of the parameters. */
3832 charvec = make_tree_vec (len);
3833 for (int i = 0; i < len; ++i)
3834 TREE_VEC_ELT (charvec, i)
3835 = double_int_to_tree (str_char_type_node,
3836 double_int::from_buffer (str + i * sz, sz));
3838 /* Build the argument packs. */
3839 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3840 TREE_TYPE (argpack) = str_char_type_node;
3842 TREE_VEC_ELT (argvec, 1) = argpack;
3844 return argvec;
3847 /* Parse a user-defined numeric constant. returns a call to a user-defined
3848 literal operator. */
3850 static tree
3851 cp_parser_userdef_numeric_literal (cp_parser *parser)
3853 cp_token *token = cp_lexer_consume_token (parser->lexer);
3854 tree literal = token->u.value;
3855 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3856 tree value = USERDEF_LITERAL_VALUE (literal);
3857 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3858 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3859 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3860 tree decl, result;
3861 vec<tree, va_gc> *args;
3863 /* Look for a literal operator taking the exact type of numeric argument
3864 as the literal value. */
3865 args = make_tree_vector ();
3866 vec_safe_push (args, value);
3867 decl = lookup_literal_operator (name, args);
3868 if (decl && decl != error_mark_node)
3870 result = finish_call_expr (decl, &args, false, true, tf_none);
3871 if (result != error_mark_node)
3873 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3874 warning_at (token->location, OPT_Woverflow,
3875 "integer literal exceeds range of %qT type",
3876 long_long_unsigned_type_node);
3877 else
3879 if (overflow > 0)
3880 warning_at (token->location, OPT_Woverflow,
3881 "floating literal exceeds range of %qT type",
3882 long_double_type_node);
3883 else if (overflow < 0)
3884 warning_at (token->location, OPT_Woverflow,
3885 "floating literal truncated to zero");
3887 release_tree_vector (args);
3888 return result;
3891 release_tree_vector (args);
3893 /* If the numeric argument didn't work, look for a raw literal
3894 operator taking a const char* argument consisting of the number
3895 in string format. */
3896 args = make_tree_vector ();
3897 vec_safe_push (args, num_string);
3898 decl = lookup_literal_operator (name, args);
3899 if (decl && decl != error_mark_node)
3901 result = finish_call_expr (decl, &args, false, true, tf_none);
3902 if (result != error_mark_node)
3904 release_tree_vector (args);
3905 return result;
3908 release_tree_vector (args);
3910 /* If the raw literal didn't work, look for a non-type template
3911 function with parameter pack char.... Call the function with
3912 template parameter characters representing the number. */
3913 args = make_tree_vector ();
3914 decl = lookup_literal_operator (name, args);
3915 if (decl && decl != error_mark_node)
3917 tree tmpl_args = make_char_string_pack (num_string);
3918 decl = lookup_template_function (decl, tmpl_args);
3919 result = finish_call_expr (decl, &args, false, true, tf_none);
3920 if (result != error_mark_node)
3922 release_tree_vector (args);
3923 return result;
3926 release_tree_vector (args);
3928 error ("unable to find numeric literal operator %qD", name);
3929 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3930 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3931 "to enable more built-in suffixes");
3932 return error_mark_node;
3935 /* Parse a user-defined string constant. Returns a call to a user-defined
3936 literal operator taking a character pointer and the length of the string
3937 as arguments. */
3939 static tree
3940 cp_parser_userdef_string_literal (cp_token *token)
3942 tree literal = token->u.value;
3943 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3944 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3945 tree value = USERDEF_LITERAL_VALUE (literal);
3946 int len = TREE_STRING_LENGTH (value)
3947 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3948 tree decl, result;
3949 vec<tree, va_gc> *args;
3951 /* Look for a template function with typename parameter CharT
3952 and parameter pack CharT... Call the function with
3953 template parameter characters representing the string. */
3954 args = make_tree_vector ();
3955 decl = lookup_literal_operator (name, args);
3956 if (decl && decl != error_mark_node)
3958 tree tmpl_args = make_string_pack (value);
3959 decl = lookup_template_function (decl, tmpl_args);
3960 result = finish_call_expr (decl, &args, false, true, tf_none);
3961 if (result != error_mark_node)
3963 release_tree_vector (args);
3964 return result;
3967 release_tree_vector (args);
3969 /* Build up a call to the user-defined operator */
3970 /* Lookup the name we got back from the id-expression. */
3971 args = make_tree_vector ();
3972 vec_safe_push (args, value);
3973 vec_safe_push (args, build_int_cst (size_type_node, len));
3974 decl = lookup_name (name);
3975 if (!decl || decl == error_mark_node)
3977 error ("unable to find string literal operator %qD", name);
3978 release_tree_vector (args);
3979 return error_mark_node;
3981 result = finish_call_expr (decl, &args, false, true, tf_none);
3982 release_tree_vector (args);
3983 if (result != error_mark_node)
3984 return result;
3986 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3987 name, TREE_TYPE (value), size_type_node);
3988 return error_mark_node;
3992 /* Basic concepts [gram.basic] */
3994 /* Parse a translation-unit.
3996 translation-unit:
3997 declaration-seq [opt]
3999 Returns TRUE if all went well. */
4001 static bool
4002 cp_parser_translation_unit (cp_parser* parser)
4004 /* The address of the first non-permanent object on the declarator
4005 obstack. */
4006 static void *declarator_obstack_base;
4008 bool success;
4010 /* Create the declarator obstack, if necessary. */
4011 if (!cp_error_declarator)
4013 gcc_obstack_init (&declarator_obstack);
4014 /* Create the error declarator. */
4015 cp_error_declarator = make_declarator (cdk_error);
4016 /* Create the empty parameter list. */
4017 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4018 /* Remember where the base of the declarator obstack lies. */
4019 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4022 cp_parser_declaration_seq_opt (parser);
4024 /* If there are no tokens left then all went well. */
4025 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4027 /* Get rid of the token array; we don't need it any more. */
4028 cp_lexer_destroy (parser->lexer);
4029 parser->lexer = NULL;
4031 /* This file might have been a context that's implicitly extern
4032 "C". If so, pop the lang context. (Only relevant for PCH.) */
4033 if (parser->implicit_extern_c)
4035 pop_lang_context ();
4036 parser->implicit_extern_c = false;
4039 /* Finish up. */
4040 finish_translation_unit ();
4042 success = true;
4044 else
4046 cp_parser_error (parser, "expected declaration");
4047 success = false;
4050 /* Make sure the declarator obstack was fully cleaned up. */
4051 gcc_assert (obstack_next_free (&declarator_obstack)
4052 == declarator_obstack_base);
4054 /* All went well. */
4055 return success;
4058 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4059 decltype context. */
4061 static inline tsubst_flags_t
4062 complain_flags (bool decltype_p)
4064 tsubst_flags_t complain = tf_warning_or_error;
4065 if (decltype_p)
4066 complain |= tf_decltype;
4067 return complain;
4070 /* Expressions [gram.expr] */
4072 /* Parse a primary-expression.
4074 primary-expression:
4075 literal
4076 this
4077 ( expression )
4078 id-expression
4080 GNU Extensions:
4082 primary-expression:
4083 ( compound-statement )
4084 __builtin_va_arg ( assignment-expression , type-id )
4085 __builtin_offsetof ( type-id , offsetof-expression )
4087 C++ Extensions:
4088 __has_nothrow_assign ( type-id )
4089 __has_nothrow_constructor ( type-id )
4090 __has_nothrow_copy ( type-id )
4091 __has_trivial_assign ( type-id )
4092 __has_trivial_constructor ( type-id )
4093 __has_trivial_copy ( type-id )
4094 __has_trivial_destructor ( type-id )
4095 __has_virtual_destructor ( type-id )
4096 __is_abstract ( type-id )
4097 __is_base_of ( type-id , type-id )
4098 __is_class ( type-id )
4099 __is_convertible_to ( type-id , type-id )
4100 __is_empty ( type-id )
4101 __is_enum ( type-id )
4102 __is_final ( type-id )
4103 __is_literal_type ( type-id )
4104 __is_pod ( type-id )
4105 __is_polymorphic ( type-id )
4106 __is_std_layout ( type-id )
4107 __is_trivial ( type-id )
4108 __is_union ( type-id )
4110 Objective-C++ Extension:
4112 primary-expression:
4113 objc-expression
4115 literal:
4116 __null
4118 ADDRESS_P is true iff this expression was immediately preceded by
4119 "&" and therefore might denote a pointer-to-member. CAST_P is true
4120 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4121 true iff this expression is a template argument.
4123 Returns a representation of the expression. Upon return, *IDK
4124 indicates what kind of id-expression (if any) was present. */
4126 static tree
4127 cp_parser_primary_expression (cp_parser *parser,
4128 bool address_p,
4129 bool cast_p,
4130 bool template_arg_p,
4131 bool decltype_p,
4132 cp_id_kind *idk)
4134 cp_token *token = NULL;
4136 /* Assume the primary expression is not an id-expression. */
4137 *idk = CP_ID_KIND_NONE;
4139 /* Peek at the next token. */
4140 token = cp_lexer_peek_token (parser->lexer);
4141 switch (token->type)
4143 /* literal:
4144 integer-literal
4145 character-literal
4146 floating-literal
4147 string-literal
4148 boolean-literal
4149 pointer-literal
4150 user-defined-literal */
4151 case CPP_CHAR:
4152 case CPP_CHAR16:
4153 case CPP_CHAR32:
4154 case CPP_WCHAR:
4155 case CPP_NUMBER:
4156 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4157 return cp_parser_userdef_numeric_literal (parser);
4158 token = cp_lexer_consume_token (parser->lexer);
4159 if (TREE_CODE (token->u.value) == FIXED_CST)
4161 error_at (token->location,
4162 "fixed-point types not supported in C++");
4163 return error_mark_node;
4165 /* Floating-point literals are only allowed in an integral
4166 constant expression if they are cast to an integral or
4167 enumeration type. */
4168 if (TREE_CODE (token->u.value) == REAL_CST
4169 && parser->integral_constant_expression_p
4170 && pedantic)
4172 /* CAST_P will be set even in invalid code like "int(2.7 +
4173 ...)". Therefore, we have to check that the next token
4174 is sure to end the cast. */
4175 if (cast_p)
4177 cp_token *next_token;
4179 next_token = cp_lexer_peek_token (parser->lexer);
4180 if (/* The comma at the end of an
4181 enumerator-definition. */
4182 next_token->type != CPP_COMMA
4183 /* The curly brace at the end of an enum-specifier. */
4184 && next_token->type != CPP_CLOSE_BRACE
4185 /* The end of a statement. */
4186 && next_token->type != CPP_SEMICOLON
4187 /* The end of the cast-expression. */
4188 && next_token->type != CPP_CLOSE_PAREN
4189 /* The end of an array bound. */
4190 && next_token->type != CPP_CLOSE_SQUARE
4191 /* The closing ">" in a template-argument-list. */
4192 && (next_token->type != CPP_GREATER
4193 || parser->greater_than_is_operator_p)
4194 /* C++0x only: A ">>" treated like two ">" tokens,
4195 in a template-argument-list. */
4196 && (next_token->type != CPP_RSHIFT
4197 || (cxx_dialect == cxx98)
4198 || parser->greater_than_is_operator_p))
4199 cast_p = false;
4202 /* If we are within a cast, then the constraint that the
4203 cast is to an integral or enumeration type will be
4204 checked at that point. If we are not within a cast, then
4205 this code is invalid. */
4206 if (!cast_p)
4207 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4209 return token->u.value;
4211 case CPP_CHAR_USERDEF:
4212 case CPP_CHAR16_USERDEF:
4213 case CPP_CHAR32_USERDEF:
4214 case CPP_WCHAR_USERDEF:
4215 return cp_parser_userdef_char_literal (parser);
4217 case CPP_STRING:
4218 case CPP_STRING16:
4219 case CPP_STRING32:
4220 case CPP_WSTRING:
4221 case CPP_UTF8STRING:
4222 case CPP_STRING_USERDEF:
4223 case CPP_STRING16_USERDEF:
4224 case CPP_STRING32_USERDEF:
4225 case CPP_WSTRING_USERDEF:
4226 case CPP_UTF8STRING_USERDEF:
4227 /* ??? Should wide strings be allowed when parser->translate_strings_p
4228 is false (i.e. in attributes)? If not, we can kill the third
4229 argument to cp_parser_string_literal. */
4230 return cp_parser_string_literal (parser,
4231 parser->translate_strings_p,
4232 true);
4234 case CPP_OPEN_PAREN:
4236 tree expr;
4237 bool saved_greater_than_is_operator_p;
4239 /* Consume the `('. */
4240 cp_lexer_consume_token (parser->lexer);
4241 /* Within a parenthesized expression, a `>' token is always
4242 the greater-than operator. */
4243 saved_greater_than_is_operator_p
4244 = parser->greater_than_is_operator_p;
4245 parser->greater_than_is_operator_p = true;
4246 /* If we see `( { ' then we are looking at the beginning of
4247 a GNU statement-expression. */
4248 if (cp_parser_allow_gnu_extensions_p (parser)
4249 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4251 /* Statement-expressions are not allowed by the standard. */
4252 pedwarn (token->location, OPT_Wpedantic,
4253 "ISO C++ forbids braced-groups within expressions");
4255 /* And they're not allowed outside of a function-body; you
4256 cannot, for example, write:
4258 int i = ({ int j = 3; j + 1; });
4260 at class or namespace scope. */
4261 if (!parser->in_function_body
4262 || parser->in_template_argument_list_p)
4264 error_at (token->location,
4265 "statement-expressions are not allowed outside "
4266 "functions nor in template-argument lists");
4267 cp_parser_skip_to_end_of_block_or_statement (parser);
4268 expr = error_mark_node;
4270 else
4272 /* Start the statement-expression. */
4273 expr = begin_stmt_expr ();
4274 /* Parse the compound-statement. */
4275 cp_parser_compound_statement (parser, expr, false, false);
4276 /* Finish up. */
4277 expr = finish_stmt_expr (expr, false);
4280 else
4282 /* Parse the parenthesized expression. */
4283 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4284 /* Let the front end know that this expression was
4285 enclosed in parentheses. This matters in case, for
4286 example, the expression is of the form `A::B', since
4287 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4288 not. */
4289 expr = finish_parenthesized_expr (expr);
4290 /* DR 705: Wrapping an unqualified name in parentheses
4291 suppresses arg-dependent lookup. We want to pass back
4292 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4293 (c++/37862), but none of the others. */
4294 if (*idk != CP_ID_KIND_QUALIFIED)
4295 *idk = CP_ID_KIND_NONE;
4297 /* The `>' token might be the end of a template-id or
4298 template-parameter-list now. */
4299 parser->greater_than_is_operator_p
4300 = saved_greater_than_is_operator_p;
4301 /* Consume the `)'. */
4302 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4303 cp_parser_skip_to_end_of_statement (parser);
4305 return expr;
4308 case CPP_OPEN_SQUARE:
4309 if (c_dialect_objc ())
4310 /* We have an Objective-C++ message. */
4311 return cp_parser_objc_expression (parser);
4313 tree lam = cp_parser_lambda_expression (parser);
4314 /* Don't warn about a failed tentative parse. */
4315 if (cp_parser_error_occurred (parser))
4316 return error_mark_node;
4317 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4318 return lam;
4321 case CPP_OBJC_STRING:
4322 if (c_dialect_objc ())
4323 /* We have an Objective-C++ string literal. */
4324 return cp_parser_objc_expression (parser);
4325 cp_parser_error (parser, "expected primary-expression");
4326 return error_mark_node;
4328 case CPP_KEYWORD:
4329 switch (token->keyword)
4331 /* These two are the boolean literals. */
4332 case RID_TRUE:
4333 cp_lexer_consume_token (parser->lexer);
4334 return boolean_true_node;
4335 case RID_FALSE:
4336 cp_lexer_consume_token (parser->lexer);
4337 return boolean_false_node;
4339 /* The `__null' literal. */
4340 case RID_NULL:
4341 cp_lexer_consume_token (parser->lexer);
4342 return null_node;
4344 /* The `nullptr' literal. */
4345 case RID_NULLPTR:
4346 cp_lexer_consume_token (parser->lexer);
4347 return nullptr_node;
4349 /* Recognize the `this' keyword. */
4350 case RID_THIS:
4351 cp_lexer_consume_token (parser->lexer);
4352 if (parser->local_variables_forbidden_p)
4354 error_at (token->location,
4355 "%<this%> may not be used in this context");
4356 return error_mark_node;
4358 /* Pointers cannot appear in constant-expressions. */
4359 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4360 return error_mark_node;
4361 return finish_this_expr ();
4363 /* The `operator' keyword can be the beginning of an
4364 id-expression. */
4365 case RID_OPERATOR:
4366 goto id_expression;
4368 case RID_FUNCTION_NAME:
4369 case RID_PRETTY_FUNCTION_NAME:
4370 case RID_C99_FUNCTION_NAME:
4372 non_integral_constant name;
4374 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4375 __func__ are the names of variables -- but they are
4376 treated specially. Therefore, they are handled here,
4377 rather than relying on the generic id-expression logic
4378 below. Grammatically, these names are id-expressions.
4380 Consume the token. */
4381 token = cp_lexer_consume_token (parser->lexer);
4383 switch (token->keyword)
4385 case RID_FUNCTION_NAME:
4386 name = NIC_FUNC_NAME;
4387 break;
4388 case RID_PRETTY_FUNCTION_NAME:
4389 name = NIC_PRETTY_FUNC;
4390 break;
4391 case RID_C99_FUNCTION_NAME:
4392 name = NIC_C99_FUNC;
4393 break;
4394 default:
4395 gcc_unreachable ();
4398 if (cp_parser_non_integral_constant_expression (parser, name))
4399 return error_mark_node;
4401 /* Look up the name. */
4402 return finish_fname (token->u.value);
4405 case RID_VA_ARG:
4407 tree expression;
4408 tree type;
4409 source_location type_location;
4411 /* The `__builtin_va_arg' construct is used to handle
4412 `va_arg'. Consume the `__builtin_va_arg' token. */
4413 cp_lexer_consume_token (parser->lexer);
4414 /* Look for the opening `('. */
4415 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4416 /* Now, parse the assignment-expression. */
4417 expression = cp_parser_assignment_expression (parser,
4418 /*cast_p=*/false, NULL);
4419 /* Look for the `,'. */
4420 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4421 type_location = cp_lexer_peek_token (parser->lexer)->location;
4422 /* Parse the type-id. */
4423 type = cp_parser_type_id (parser);
4424 /* Look for the closing `)'. */
4425 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4426 /* Using `va_arg' in a constant-expression is not
4427 allowed. */
4428 if (cp_parser_non_integral_constant_expression (parser,
4429 NIC_VA_ARG))
4430 return error_mark_node;
4431 return build_x_va_arg (type_location, expression, type);
4434 case RID_OFFSETOF:
4435 return cp_parser_builtin_offsetof (parser);
4437 case RID_HAS_NOTHROW_ASSIGN:
4438 case RID_HAS_NOTHROW_CONSTRUCTOR:
4439 case RID_HAS_NOTHROW_COPY:
4440 case RID_HAS_TRIVIAL_ASSIGN:
4441 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4442 case RID_HAS_TRIVIAL_COPY:
4443 case RID_HAS_TRIVIAL_DESTRUCTOR:
4444 case RID_HAS_VIRTUAL_DESTRUCTOR:
4445 case RID_IS_ABSTRACT:
4446 case RID_IS_BASE_OF:
4447 case RID_IS_CLASS:
4448 case RID_IS_CONVERTIBLE_TO:
4449 case RID_IS_EMPTY:
4450 case RID_IS_ENUM:
4451 case RID_IS_FINAL:
4452 case RID_IS_LITERAL_TYPE:
4453 case RID_IS_POD:
4454 case RID_IS_POLYMORPHIC:
4455 case RID_IS_STD_LAYOUT:
4456 case RID_IS_TRIVIAL:
4457 case RID_IS_UNION:
4458 return cp_parser_trait_expr (parser, token->keyword);
4460 /* Objective-C++ expressions. */
4461 case RID_AT_ENCODE:
4462 case RID_AT_PROTOCOL:
4463 case RID_AT_SELECTOR:
4464 return cp_parser_objc_expression (parser);
4466 case RID_TEMPLATE:
4467 if (parser->in_function_body
4468 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4469 == CPP_LESS))
4471 error_at (token->location,
4472 "a template declaration cannot appear at block scope");
4473 cp_parser_skip_to_end_of_block_or_statement (parser);
4474 return error_mark_node;
4476 default:
4477 cp_parser_error (parser, "expected primary-expression");
4478 return error_mark_node;
4481 /* An id-expression can start with either an identifier, a
4482 `::' as the beginning of a qualified-id, or the "operator"
4483 keyword. */
4484 case CPP_NAME:
4485 case CPP_SCOPE:
4486 case CPP_TEMPLATE_ID:
4487 case CPP_NESTED_NAME_SPECIFIER:
4489 tree id_expression;
4490 tree decl;
4491 const char *error_msg;
4492 bool template_p;
4493 bool done;
4494 cp_token *id_expr_token;
4496 id_expression:
4497 /* Parse the id-expression. */
4498 id_expression
4499 = cp_parser_id_expression (parser,
4500 /*template_keyword_p=*/false,
4501 /*check_dependency_p=*/true,
4502 &template_p,
4503 /*declarator_p=*/false,
4504 /*optional_p=*/false);
4505 if (id_expression == error_mark_node)
4506 return error_mark_node;
4507 id_expr_token = token;
4508 token = cp_lexer_peek_token (parser->lexer);
4509 done = (token->type != CPP_OPEN_SQUARE
4510 && token->type != CPP_OPEN_PAREN
4511 && token->type != CPP_DOT
4512 && token->type != CPP_DEREF
4513 && token->type != CPP_PLUS_PLUS
4514 && token->type != CPP_MINUS_MINUS);
4515 /* If we have a template-id, then no further lookup is
4516 required. If the template-id was for a template-class, we
4517 will sometimes have a TYPE_DECL at this point. */
4518 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4519 || TREE_CODE (id_expression) == TYPE_DECL)
4520 decl = id_expression;
4521 /* Look up the name. */
4522 else
4524 tree ambiguous_decls;
4526 /* If we already know that this lookup is ambiguous, then
4527 we've already issued an error message; there's no reason
4528 to check again. */
4529 if (id_expr_token->type == CPP_NAME
4530 && id_expr_token->ambiguous_p)
4532 cp_parser_simulate_error (parser);
4533 return error_mark_node;
4536 decl = cp_parser_lookup_name (parser, id_expression,
4537 none_type,
4538 template_p,
4539 /*is_namespace=*/false,
4540 /*check_dependency=*/true,
4541 &ambiguous_decls,
4542 id_expr_token->location);
4543 /* If the lookup was ambiguous, an error will already have
4544 been issued. */
4545 if (ambiguous_decls)
4546 return error_mark_node;
4548 /* In Objective-C++, we may have an Objective-C 2.0
4549 dot-syntax for classes here. */
4550 if (c_dialect_objc ()
4551 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4552 && TREE_CODE (decl) == TYPE_DECL
4553 && objc_is_class_name (decl))
4555 tree component;
4556 cp_lexer_consume_token (parser->lexer);
4557 component = cp_parser_identifier (parser);
4558 if (component == error_mark_node)
4559 return error_mark_node;
4561 return objc_build_class_component_ref (id_expression, component);
4564 /* In Objective-C++, an instance variable (ivar) may be preferred
4565 to whatever cp_parser_lookup_name() found. */
4566 decl = objc_lookup_ivar (decl, id_expression);
4568 /* If name lookup gives us a SCOPE_REF, then the
4569 qualifying scope was dependent. */
4570 if (TREE_CODE (decl) == SCOPE_REF)
4572 /* At this point, we do not know if DECL is a valid
4573 integral constant expression. We assume that it is
4574 in fact such an expression, so that code like:
4576 template <int N> struct A {
4577 int a[B<N>::i];
4580 is accepted. At template-instantiation time, we
4581 will check that B<N>::i is actually a constant. */
4582 return decl;
4584 /* Check to see if DECL is a local variable in a context
4585 where that is forbidden. */
4586 if (parser->local_variables_forbidden_p
4587 && local_variable_p (decl))
4589 /* It might be that we only found DECL because we are
4590 trying to be generous with pre-ISO scoping rules.
4591 For example, consider:
4593 int i;
4594 void g() {
4595 for (int i = 0; i < 10; ++i) {}
4596 extern void f(int j = i);
4599 Here, name look up will originally find the out
4600 of scope `i'. We need to issue a warning message,
4601 but then use the global `i'. */
4602 decl = check_for_out_of_scope_variable (decl);
4603 if (local_variable_p (decl))
4605 error_at (id_expr_token->location,
4606 "local variable %qD may not appear in this context",
4607 decl);
4608 return error_mark_node;
4613 decl = (finish_id_expression
4614 (id_expression, decl, parser->scope,
4615 idk,
4616 parser->integral_constant_expression_p,
4617 parser->allow_non_integral_constant_expression_p,
4618 &parser->non_integral_constant_expression_p,
4619 template_p, done, address_p,
4620 template_arg_p,
4621 &error_msg,
4622 id_expr_token->location));
4623 if (error_msg)
4624 cp_parser_error (parser, error_msg);
4625 return decl;
4628 /* Anything else is an error. */
4629 default:
4630 cp_parser_error (parser, "expected primary-expression");
4631 return error_mark_node;
4635 static inline tree
4636 cp_parser_primary_expression (cp_parser *parser,
4637 bool address_p,
4638 bool cast_p,
4639 bool template_arg_p,
4640 cp_id_kind *idk)
4642 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4643 /*decltype*/false, idk);
4646 /* Parse an id-expression.
4648 id-expression:
4649 unqualified-id
4650 qualified-id
4652 qualified-id:
4653 :: [opt] nested-name-specifier template [opt] unqualified-id
4654 :: identifier
4655 :: operator-function-id
4656 :: template-id
4658 Return a representation of the unqualified portion of the
4659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4660 a `::' or nested-name-specifier.
4662 Often, if the id-expression was a qualified-id, the caller will
4663 want to make a SCOPE_REF to represent the qualified-id. This
4664 function does not do this in order to avoid wastefully creating
4665 SCOPE_REFs when they are not required.
4667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4668 `template' keyword.
4670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4671 uninstantiated templates.
4673 If *TEMPLATE_P is non-NULL, it is set to true iff the
4674 `template' keyword is used to explicitly indicate that the entity
4675 named is a template.
4677 If DECLARATOR_P is true, the id-expression is appearing as part of
4678 a declarator, rather than as part of an expression. */
4680 static tree
4681 cp_parser_id_expression (cp_parser *parser,
4682 bool template_keyword_p,
4683 bool check_dependency_p,
4684 bool *template_p,
4685 bool declarator_p,
4686 bool optional_p)
4688 bool global_scope_p;
4689 bool nested_name_specifier_p;
4691 /* Assume the `template' keyword was not used. */
4692 if (template_p)
4693 *template_p = template_keyword_p;
4695 /* Look for the optional `::' operator. */
4696 global_scope_p
4697 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4698 != NULL_TREE);
4699 /* Look for the optional nested-name-specifier. */
4700 nested_name_specifier_p
4701 = (cp_parser_nested_name_specifier_opt (parser,
4702 /*typename_keyword_p=*/false,
4703 check_dependency_p,
4704 /*type_p=*/false,
4705 declarator_p)
4706 != NULL_TREE);
4707 /* If there is a nested-name-specifier, then we are looking at
4708 the first qualified-id production. */
4709 if (nested_name_specifier_p)
4711 tree saved_scope;
4712 tree saved_object_scope;
4713 tree saved_qualifying_scope;
4714 tree unqualified_id;
4715 bool is_template;
4717 /* See if the next token is the `template' keyword. */
4718 if (!template_p)
4719 template_p = &is_template;
4720 *template_p = cp_parser_optional_template_keyword (parser);
4721 /* Name lookup we do during the processing of the
4722 unqualified-id might obliterate SCOPE. */
4723 saved_scope = parser->scope;
4724 saved_object_scope = parser->object_scope;
4725 saved_qualifying_scope = parser->qualifying_scope;
4726 /* Process the final unqualified-id. */
4727 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4728 check_dependency_p,
4729 declarator_p,
4730 /*optional_p=*/false);
4731 /* Restore the SAVED_SCOPE for our caller. */
4732 parser->scope = saved_scope;
4733 parser->object_scope = saved_object_scope;
4734 parser->qualifying_scope = saved_qualifying_scope;
4736 return unqualified_id;
4738 /* Otherwise, if we are in global scope, then we are looking at one
4739 of the other qualified-id productions. */
4740 else if (global_scope_p)
4742 cp_token *token;
4743 tree id;
4745 /* Peek at the next token. */
4746 token = cp_lexer_peek_token (parser->lexer);
4748 /* If it's an identifier, and the next token is not a "<", then
4749 we can avoid the template-id case. This is an optimization
4750 for this common case. */
4751 if (token->type == CPP_NAME
4752 && !cp_parser_nth_token_starts_template_argument_list_p
4753 (parser, 2))
4754 return cp_parser_identifier (parser);
4756 cp_parser_parse_tentatively (parser);
4757 /* Try a template-id. */
4758 id = cp_parser_template_id (parser,
4759 /*template_keyword_p=*/false,
4760 /*check_dependency_p=*/true,
4761 none_type,
4762 declarator_p);
4763 /* If that worked, we're done. */
4764 if (cp_parser_parse_definitely (parser))
4765 return id;
4767 /* Peek at the next token. (Changes in the token buffer may
4768 have invalidated the pointer obtained above.) */
4769 token = cp_lexer_peek_token (parser->lexer);
4771 switch (token->type)
4773 case CPP_NAME:
4774 return cp_parser_identifier (parser);
4776 case CPP_KEYWORD:
4777 if (token->keyword == RID_OPERATOR)
4778 return cp_parser_operator_function_id (parser);
4779 /* Fall through. */
4781 default:
4782 cp_parser_error (parser, "expected id-expression");
4783 return error_mark_node;
4786 else
4787 return cp_parser_unqualified_id (parser, template_keyword_p,
4788 /*check_dependency_p=*/true,
4789 declarator_p,
4790 optional_p);
4793 /* Parse an unqualified-id.
4795 unqualified-id:
4796 identifier
4797 operator-function-id
4798 conversion-function-id
4799 ~ class-name
4800 template-id
4802 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4803 keyword, in a construct like `A::template ...'.
4805 Returns a representation of unqualified-id. For the `identifier'
4806 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4807 production a BIT_NOT_EXPR is returned; the operand of the
4808 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4809 other productions, see the documentation accompanying the
4810 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4811 names are looked up in uninstantiated templates. If DECLARATOR_P
4812 is true, the unqualified-id is appearing as part of a declarator,
4813 rather than as part of an expression. */
4815 static tree
4816 cp_parser_unqualified_id (cp_parser* parser,
4817 bool template_keyword_p,
4818 bool check_dependency_p,
4819 bool declarator_p,
4820 bool optional_p)
4822 cp_token *token;
4824 /* Peek at the next token. */
4825 token = cp_lexer_peek_token (parser->lexer);
4827 switch (token->type)
4829 case CPP_NAME:
4831 tree id;
4833 /* We don't know yet whether or not this will be a
4834 template-id. */
4835 cp_parser_parse_tentatively (parser);
4836 /* Try a template-id. */
4837 id = cp_parser_template_id (parser, template_keyword_p,
4838 check_dependency_p,
4839 none_type,
4840 declarator_p);
4841 /* If it worked, we're done. */
4842 if (cp_parser_parse_definitely (parser))
4843 return id;
4844 /* Otherwise, it's an ordinary identifier. */
4845 return cp_parser_identifier (parser);
4848 case CPP_TEMPLATE_ID:
4849 return cp_parser_template_id (parser, template_keyword_p,
4850 check_dependency_p,
4851 none_type,
4852 declarator_p);
4854 case CPP_COMPL:
4856 tree type_decl;
4857 tree qualifying_scope;
4858 tree object_scope;
4859 tree scope;
4860 bool done;
4862 /* Consume the `~' token. */
4863 cp_lexer_consume_token (parser->lexer);
4864 /* Parse the class-name. The standard, as written, seems to
4865 say that:
4867 template <typename T> struct S { ~S (); };
4868 template <typename T> S<T>::~S() {}
4870 is invalid, since `~' must be followed by a class-name, but
4871 `S<T>' is dependent, and so not known to be a class.
4872 That's not right; we need to look in uninstantiated
4873 templates. A further complication arises from:
4875 template <typename T> void f(T t) {
4876 t.T::~T();
4879 Here, it is not possible to look up `T' in the scope of `T'
4880 itself. We must look in both the current scope, and the
4881 scope of the containing complete expression.
4883 Yet another issue is:
4885 struct S {
4886 int S;
4887 ~S();
4890 S::~S() {}
4892 The standard does not seem to say that the `S' in `~S'
4893 should refer to the type `S' and not the data member
4894 `S::S'. */
4896 /* DR 244 says that we look up the name after the "~" in the
4897 same scope as we looked up the qualifying name. That idea
4898 isn't fully worked out; it's more complicated than that. */
4899 scope = parser->scope;
4900 object_scope = parser->object_scope;
4901 qualifying_scope = parser->qualifying_scope;
4903 /* Check for invalid scopes. */
4904 if (scope == error_mark_node)
4906 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4907 cp_lexer_consume_token (parser->lexer);
4908 return error_mark_node;
4910 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4912 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4913 error_at (token->location,
4914 "scope %qT before %<~%> is not a class-name",
4915 scope);
4916 cp_parser_simulate_error (parser);
4917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4918 cp_lexer_consume_token (parser->lexer);
4919 return error_mark_node;
4921 gcc_assert (!scope || TYPE_P (scope));
4923 /* If the name is of the form "X::~X" it's OK even if X is a
4924 typedef. */
4925 token = cp_lexer_peek_token (parser->lexer);
4926 if (scope
4927 && token->type == CPP_NAME
4928 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4929 != CPP_LESS)
4930 && (token->u.value == TYPE_IDENTIFIER (scope)
4931 || (CLASS_TYPE_P (scope)
4932 && constructor_name_p (token->u.value, scope))))
4934 cp_lexer_consume_token (parser->lexer);
4935 return build_nt (BIT_NOT_EXPR, scope);
4938 /* ~auto means the destructor of whatever the object is. */
4939 if (cp_parser_is_keyword (token, RID_AUTO))
4941 if (cxx_dialect < cxx1y)
4942 pedwarn (input_location, 0,
4943 "%<~auto%> only available with "
4944 "-std=c++1y or -std=gnu++1y");
4945 cp_lexer_consume_token (parser->lexer);
4946 return build_nt (BIT_NOT_EXPR, make_auto ());
4949 /* If there was an explicit qualification (S::~T), first look
4950 in the scope given by the qualification (i.e., S).
4952 Note: in the calls to cp_parser_class_name below we pass
4953 typename_type so that lookup finds the injected-class-name
4954 rather than the constructor. */
4955 done = false;
4956 type_decl = NULL_TREE;
4957 if (scope)
4959 cp_parser_parse_tentatively (parser);
4960 type_decl = cp_parser_class_name (parser,
4961 /*typename_keyword_p=*/false,
4962 /*template_keyword_p=*/false,
4963 typename_type,
4964 /*check_dependency=*/false,
4965 /*class_head_p=*/false,
4966 declarator_p);
4967 if (cp_parser_parse_definitely (parser))
4968 done = true;
4970 /* In "N::S::~S", look in "N" as well. */
4971 if (!done && scope && qualifying_scope)
4973 cp_parser_parse_tentatively (parser);
4974 parser->scope = qualifying_scope;
4975 parser->object_scope = NULL_TREE;
4976 parser->qualifying_scope = NULL_TREE;
4977 type_decl
4978 = cp_parser_class_name (parser,
4979 /*typename_keyword_p=*/false,
4980 /*template_keyword_p=*/false,
4981 typename_type,
4982 /*check_dependency=*/false,
4983 /*class_head_p=*/false,
4984 declarator_p);
4985 if (cp_parser_parse_definitely (parser))
4986 done = true;
4988 /* In "p->S::~T", look in the scope given by "*p" as well. */
4989 else if (!done && object_scope)
4991 cp_parser_parse_tentatively (parser);
4992 parser->scope = object_scope;
4993 parser->object_scope = NULL_TREE;
4994 parser->qualifying_scope = NULL_TREE;
4995 type_decl
4996 = cp_parser_class_name (parser,
4997 /*typename_keyword_p=*/false,
4998 /*template_keyword_p=*/false,
4999 typename_type,
5000 /*check_dependency=*/false,
5001 /*class_head_p=*/false,
5002 declarator_p);
5003 if (cp_parser_parse_definitely (parser))
5004 done = true;
5006 /* Look in the surrounding context. */
5007 if (!done)
5009 parser->scope = NULL_TREE;
5010 parser->object_scope = NULL_TREE;
5011 parser->qualifying_scope = NULL_TREE;
5012 if (processing_template_decl)
5013 cp_parser_parse_tentatively (parser);
5014 type_decl
5015 = cp_parser_class_name (parser,
5016 /*typename_keyword_p=*/false,
5017 /*template_keyword_p=*/false,
5018 typename_type,
5019 /*check_dependency=*/false,
5020 /*class_head_p=*/false,
5021 declarator_p);
5022 if (processing_template_decl
5023 && ! cp_parser_parse_definitely (parser))
5025 /* We couldn't find a type with this name, so just accept
5026 it and check for a match at instantiation time. */
5027 type_decl = cp_parser_identifier (parser);
5028 if (type_decl != error_mark_node)
5029 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5030 return type_decl;
5033 /* If an error occurred, assume that the name of the
5034 destructor is the same as the name of the qualifying
5035 class. That allows us to keep parsing after running
5036 into ill-formed destructor names. */
5037 if (type_decl == error_mark_node && scope)
5038 return build_nt (BIT_NOT_EXPR, scope);
5039 else if (type_decl == error_mark_node)
5040 return error_mark_node;
5042 /* Check that destructor name and scope match. */
5043 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5045 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5046 error_at (token->location,
5047 "declaration of %<~%T%> as member of %qT",
5048 type_decl, scope);
5049 cp_parser_simulate_error (parser);
5050 return error_mark_node;
5053 /* [class.dtor]
5055 A typedef-name that names a class shall not be used as the
5056 identifier in the declarator for a destructor declaration. */
5057 if (declarator_p
5058 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5059 && !DECL_SELF_REFERENCE_P (type_decl)
5060 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5061 error_at (token->location,
5062 "typedef-name %qD used as destructor declarator",
5063 type_decl);
5065 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5068 case CPP_KEYWORD:
5069 if (token->keyword == RID_OPERATOR)
5071 tree id;
5073 /* This could be a template-id, so we try that first. */
5074 cp_parser_parse_tentatively (parser);
5075 /* Try a template-id. */
5076 id = cp_parser_template_id (parser, template_keyword_p,
5077 /*check_dependency_p=*/true,
5078 none_type,
5079 declarator_p);
5080 /* If that worked, we're done. */
5081 if (cp_parser_parse_definitely (parser))
5082 return id;
5083 /* We still don't know whether we're looking at an
5084 operator-function-id or a conversion-function-id. */
5085 cp_parser_parse_tentatively (parser);
5086 /* Try an operator-function-id. */
5087 id = cp_parser_operator_function_id (parser);
5088 /* If that didn't work, try a conversion-function-id. */
5089 if (!cp_parser_parse_definitely (parser))
5090 id = cp_parser_conversion_function_id (parser);
5091 else if (UDLIT_OPER_P (id))
5093 /* 17.6.3.3.5 */
5094 const char *name = UDLIT_OP_SUFFIX (id);
5095 if (name[0] != '_' && !in_system_header_at (input_location)
5096 && declarator_p)
5097 warning (0, "literal operator suffixes not preceded by %<_%>"
5098 " are reserved for future standardization");
5101 return id;
5103 /* Fall through. */
5105 default:
5106 if (optional_p)
5107 return NULL_TREE;
5108 cp_parser_error (parser, "expected unqualified-id");
5109 return error_mark_node;
5113 /* Parse an (optional) nested-name-specifier.
5115 nested-name-specifier: [C++98]
5116 class-or-namespace-name :: nested-name-specifier [opt]
5117 class-or-namespace-name :: template nested-name-specifier [opt]
5119 nested-name-specifier: [C++0x]
5120 type-name ::
5121 namespace-name ::
5122 nested-name-specifier identifier ::
5123 nested-name-specifier template [opt] simple-template-id ::
5125 PARSER->SCOPE should be set appropriately before this function is
5126 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5127 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5128 in name lookups.
5130 Sets PARSER->SCOPE to the class (TYPE) or namespace
5131 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5132 it unchanged if there is no nested-name-specifier. Returns the new
5133 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5135 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5136 part of a declaration and/or decl-specifier. */
5138 static tree
5139 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5140 bool typename_keyword_p,
5141 bool check_dependency_p,
5142 bool type_p,
5143 bool is_declaration)
5145 bool success = false;
5146 cp_token_position start = 0;
5147 cp_token *token;
5149 /* Remember where the nested-name-specifier starts. */
5150 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5152 start = cp_lexer_token_position (parser->lexer, false);
5153 push_deferring_access_checks (dk_deferred);
5156 while (true)
5158 tree new_scope;
5159 tree old_scope;
5160 tree saved_qualifying_scope;
5161 bool template_keyword_p;
5163 /* Spot cases that cannot be the beginning of a
5164 nested-name-specifier. */
5165 token = cp_lexer_peek_token (parser->lexer);
5167 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5168 the already parsed nested-name-specifier. */
5169 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5171 /* Grab the nested-name-specifier and continue the loop. */
5172 cp_parser_pre_parsed_nested_name_specifier (parser);
5173 /* If we originally encountered this nested-name-specifier
5174 with IS_DECLARATION set to false, we will not have
5175 resolved TYPENAME_TYPEs, so we must do so here. */
5176 if (is_declaration
5177 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5179 new_scope = resolve_typename_type (parser->scope,
5180 /*only_current_p=*/false);
5181 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5182 parser->scope = new_scope;
5184 success = true;
5185 continue;
5188 /* Spot cases that cannot be the beginning of a
5189 nested-name-specifier. On the second and subsequent times
5190 through the loop, we look for the `template' keyword. */
5191 if (success && token->keyword == RID_TEMPLATE)
5193 /* A template-id can start a nested-name-specifier. */
5194 else if (token->type == CPP_TEMPLATE_ID)
5196 /* DR 743: decltype can be used in a nested-name-specifier. */
5197 else if (token_is_decltype (token))
5199 else
5201 /* If the next token is not an identifier, then it is
5202 definitely not a type-name or namespace-name. */
5203 if (token->type != CPP_NAME)
5204 break;
5205 /* If the following token is neither a `<' (to begin a
5206 template-id), nor a `::', then we are not looking at a
5207 nested-name-specifier. */
5208 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5210 if (token->type == CPP_COLON
5211 && parser->colon_corrects_to_scope_p
5212 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5214 error_at (token->location,
5215 "found %<:%> in nested-name-specifier, expected %<::%>");
5216 token->type = CPP_SCOPE;
5219 if (token->type != CPP_SCOPE
5220 && !cp_parser_nth_token_starts_template_argument_list_p
5221 (parser, 2))
5222 break;
5225 /* The nested-name-specifier is optional, so we parse
5226 tentatively. */
5227 cp_parser_parse_tentatively (parser);
5229 /* Look for the optional `template' keyword, if this isn't the
5230 first time through the loop. */
5231 if (success)
5232 template_keyword_p = cp_parser_optional_template_keyword (parser);
5233 else
5234 template_keyword_p = false;
5236 /* Save the old scope since the name lookup we are about to do
5237 might destroy it. */
5238 old_scope = parser->scope;
5239 saved_qualifying_scope = parser->qualifying_scope;
5240 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5241 look up names in "X<T>::I" in order to determine that "Y" is
5242 a template. So, if we have a typename at this point, we make
5243 an effort to look through it. */
5244 if (is_declaration
5245 && !typename_keyword_p
5246 && parser->scope
5247 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5248 parser->scope = resolve_typename_type (parser->scope,
5249 /*only_current_p=*/false);
5250 /* Parse the qualifying entity. */
5251 new_scope
5252 = cp_parser_qualifying_entity (parser,
5253 typename_keyword_p,
5254 template_keyword_p,
5255 check_dependency_p,
5256 type_p,
5257 is_declaration);
5258 /* Look for the `::' token. */
5259 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5261 /* If we found what we wanted, we keep going; otherwise, we're
5262 done. */
5263 if (!cp_parser_parse_definitely (parser))
5265 bool error_p = false;
5267 /* Restore the OLD_SCOPE since it was valid before the
5268 failed attempt at finding the last
5269 class-or-namespace-name. */
5270 parser->scope = old_scope;
5271 parser->qualifying_scope = saved_qualifying_scope;
5273 /* If the next token is a decltype, and the one after that is a
5274 `::', then the decltype has failed to resolve to a class or
5275 enumeration type. Give this error even when parsing
5276 tentatively since it can't possibly be valid--and we're going
5277 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5278 won't get another chance.*/
5279 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5280 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5281 == CPP_SCOPE))
5283 token = cp_lexer_consume_token (parser->lexer);
5284 error_at (token->location, "decltype evaluates to %qT, "
5285 "which is not a class or enumeration type",
5286 token->u.value);
5287 parser->scope = error_mark_node;
5288 error_p = true;
5289 /* As below. */
5290 success = true;
5291 cp_lexer_consume_token (parser->lexer);
5294 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5295 break;
5296 /* If the next token is an identifier, and the one after
5297 that is a `::', then any valid interpretation would have
5298 found a class-or-namespace-name. */
5299 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5300 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5301 == CPP_SCOPE)
5302 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5303 != CPP_COMPL))
5305 token = cp_lexer_consume_token (parser->lexer);
5306 if (!error_p)
5308 if (!token->ambiguous_p)
5310 tree decl;
5311 tree ambiguous_decls;
5313 decl = cp_parser_lookup_name (parser, token->u.value,
5314 none_type,
5315 /*is_template=*/false,
5316 /*is_namespace=*/false,
5317 /*check_dependency=*/true,
5318 &ambiguous_decls,
5319 token->location);
5320 if (TREE_CODE (decl) == TEMPLATE_DECL)
5321 error_at (token->location,
5322 "%qD used without template parameters",
5323 decl);
5324 else if (ambiguous_decls)
5326 // cp_parser_lookup_name has the same diagnostic,
5327 // thus make sure to emit it at most once.
5328 if (cp_parser_uncommitted_to_tentative_parse_p
5329 (parser))
5331 error_at (token->location,
5332 "reference to %qD is ambiguous",
5333 token->u.value);
5334 print_candidates (ambiguous_decls);
5336 decl = error_mark_node;
5338 else
5340 if (cxx_dialect != cxx98)
5341 cp_parser_name_lookup_error
5342 (parser, token->u.value, decl, NLE_NOT_CXX98,
5343 token->location);
5344 else
5345 cp_parser_name_lookup_error
5346 (parser, token->u.value, decl, NLE_CXX98,
5347 token->location);
5350 parser->scope = error_mark_node;
5351 error_p = true;
5352 /* Treat this as a successful nested-name-specifier
5353 due to:
5355 [basic.lookup.qual]
5357 If the name found is not a class-name (clause
5358 _class_) or namespace-name (_namespace.def_), the
5359 program is ill-formed. */
5360 success = true;
5362 cp_lexer_consume_token (parser->lexer);
5364 break;
5366 /* We've found one valid nested-name-specifier. */
5367 success = true;
5368 /* Name lookup always gives us a DECL. */
5369 if (TREE_CODE (new_scope) == TYPE_DECL)
5370 new_scope = TREE_TYPE (new_scope);
5371 /* Uses of "template" must be followed by actual templates. */
5372 if (template_keyword_p
5373 && !(CLASS_TYPE_P (new_scope)
5374 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5375 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5376 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5377 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5378 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5379 == TEMPLATE_ID_EXPR)))
5380 permerror (input_location, TYPE_P (new_scope)
5381 ? G_("%qT is not a template")
5382 : G_("%qD is not a template"),
5383 new_scope);
5384 /* If it is a class scope, try to complete it; we are about to
5385 be looking up names inside the class. */
5386 if (TYPE_P (new_scope)
5387 /* Since checking types for dependency can be expensive,
5388 avoid doing it if the type is already complete. */
5389 && !COMPLETE_TYPE_P (new_scope)
5390 /* Do not try to complete dependent types. */
5391 && !dependent_type_p (new_scope))
5393 new_scope = complete_type (new_scope);
5394 /* If it is a typedef to current class, use the current
5395 class instead, as the typedef won't have any names inside
5396 it yet. */
5397 if (!COMPLETE_TYPE_P (new_scope)
5398 && currently_open_class (new_scope))
5399 new_scope = TYPE_MAIN_VARIANT (new_scope);
5401 /* Make sure we look in the right scope the next time through
5402 the loop. */
5403 parser->scope = new_scope;
5406 /* If parsing tentatively, replace the sequence of tokens that makes
5407 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5408 token. That way, should we re-parse the token stream, we will
5409 not have to repeat the effort required to do the parse, nor will
5410 we issue duplicate error messages. */
5411 if (success && start)
5413 cp_token *token;
5415 token = cp_lexer_token_at (parser->lexer, start);
5416 /* Reset the contents of the START token. */
5417 token->type = CPP_NESTED_NAME_SPECIFIER;
5418 /* Retrieve any deferred checks. Do not pop this access checks yet
5419 so the memory will not be reclaimed during token replacing below. */
5420 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5421 token->u.tree_check_value->value = parser->scope;
5422 token->u.tree_check_value->checks = get_deferred_access_checks ();
5423 token->u.tree_check_value->qualifying_scope =
5424 parser->qualifying_scope;
5425 token->keyword = RID_MAX;
5427 /* Purge all subsequent tokens. */
5428 cp_lexer_purge_tokens_after (parser->lexer, start);
5431 if (start)
5432 pop_to_parent_deferring_access_checks ();
5434 return success ? parser->scope : NULL_TREE;
5437 /* Parse a nested-name-specifier. See
5438 cp_parser_nested_name_specifier_opt for details. This function
5439 behaves identically, except that it will an issue an error if no
5440 nested-name-specifier is present. */
5442 static tree
5443 cp_parser_nested_name_specifier (cp_parser *parser,
5444 bool typename_keyword_p,
5445 bool check_dependency_p,
5446 bool type_p,
5447 bool is_declaration)
5449 tree scope;
5451 /* Look for the nested-name-specifier. */
5452 scope = cp_parser_nested_name_specifier_opt (parser,
5453 typename_keyword_p,
5454 check_dependency_p,
5455 type_p,
5456 is_declaration);
5457 /* If it was not present, issue an error message. */
5458 if (!scope)
5460 cp_parser_error (parser, "expected nested-name-specifier");
5461 parser->scope = NULL_TREE;
5464 return scope;
5467 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5468 this is either a class-name or a namespace-name (which corresponds
5469 to the class-or-namespace-name production in the grammar). For
5470 C++0x, it can also be a type-name that refers to an enumeration
5471 type or a simple-template-id.
5473 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5474 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5475 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5476 TYPE_P is TRUE iff the next name should be taken as a class-name,
5477 even the same name is declared to be another entity in the same
5478 scope.
5480 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5481 specified by the class-or-namespace-name. If neither is found the
5482 ERROR_MARK_NODE is returned. */
5484 static tree
5485 cp_parser_qualifying_entity (cp_parser *parser,
5486 bool typename_keyword_p,
5487 bool template_keyword_p,
5488 bool check_dependency_p,
5489 bool type_p,
5490 bool is_declaration)
5492 tree saved_scope;
5493 tree saved_qualifying_scope;
5494 tree saved_object_scope;
5495 tree scope;
5496 bool only_class_p;
5497 bool successful_parse_p;
5499 /* DR 743: decltype can appear in a nested-name-specifier. */
5500 if (cp_lexer_next_token_is_decltype (parser->lexer))
5502 scope = cp_parser_decltype (parser);
5503 if (TREE_CODE (scope) != ENUMERAL_TYPE
5504 && !MAYBE_CLASS_TYPE_P (scope))
5506 cp_parser_simulate_error (parser);
5507 return error_mark_node;
5509 if (TYPE_NAME (scope))
5510 scope = TYPE_NAME (scope);
5511 return scope;
5514 /* Before we try to parse the class-name, we must save away the
5515 current PARSER->SCOPE since cp_parser_class_name will destroy
5516 it. */
5517 saved_scope = parser->scope;
5518 saved_qualifying_scope = parser->qualifying_scope;
5519 saved_object_scope = parser->object_scope;
5520 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5521 there is no need to look for a namespace-name. */
5522 only_class_p = template_keyword_p
5523 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5524 if (!only_class_p)
5525 cp_parser_parse_tentatively (parser);
5526 scope = cp_parser_class_name (parser,
5527 typename_keyword_p,
5528 template_keyword_p,
5529 type_p ? class_type : none_type,
5530 check_dependency_p,
5531 /*class_head_p=*/false,
5532 is_declaration);
5533 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5534 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5535 if (!only_class_p
5536 && cxx_dialect != cxx98
5537 && !successful_parse_p)
5539 /* Restore the saved scope. */
5540 parser->scope = saved_scope;
5541 parser->qualifying_scope = saved_qualifying_scope;
5542 parser->object_scope = saved_object_scope;
5544 /* Parse tentatively. */
5545 cp_parser_parse_tentatively (parser);
5547 /* Parse a type-name */
5548 scope = cp_parser_type_name (parser);
5550 /* "If the name found does not designate a namespace or a class,
5551 enumeration, or dependent type, the program is ill-formed."
5553 We cover classes and dependent types above and namespaces below,
5554 so this code is only looking for enums. */
5555 if (!scope || TREE_CODE (scope) != TYPE_DECL
5556 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5557 cp_parser_simulate_error (parser);
5559 successful_parse_p = cp_parser_parse_definitely (parser);
5561 /* If that didn't work, try for a namespace-name. */
5562 if (!only_class_p && !successful_parse_p)
5564 /* Restore the saved scope. */
5565 parser->scope = saved_scope;
5566 parser->qualifying_scope = saved_qualifying_scope;
5567 parser->object_scope = saved_object_scope;
5568 /* If we are not looking at an identifier followed by the scope
5569 resolution operator, then this is not part of a
5570 nested-name-specifier. (Note that this function is only used
5571 to parse the components of a nested-name-specifier.) */
5572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5573 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5574 return error_mark_node;
5575 scope = cp_parser_namespace_name (parser);
5578 return scope;
5581 /* Parse a postfix-expression.
5583 postfix-expression:
5584 primary-expression
5585 postfix-expression [ expression ]
5586 postfix-expression ( expression-list [opt] )
5587 simple-type-specifier ( expression-list [opt] )
5588 typename :: [opt] nested-name-specifier identifier
5589 ( expression-list [opt] )
5590 typename :: [opt] nested-name-specifier template [opt] template-id
5591 ( expression-list [opt] )
5592 postfix-expression . template [opt] id-expression
5593 postfix-expression -> template [opt] id-expression
5594 postfix-expression . pseudo-destructor-name
5595 postfix-expression -> pseudo-destructor-name
5596 postfix-expression ++
5597 postfix-expression --
5598 dynamic_cast < type-id > ( expression )
5599 static_cast < type-id > ( expression )
5600 reinterpret_cast < type-id > ( expression )
5601 const_cast < type-id > ( expression )
5602 typeid ( expression )
5603 typeid ( type-id )
5605 GNU Extension:
5607 postfix-expression:
5608 ( type-id ) { initializer-list , [opt] }
5610 This extension is a GNU version of the C99 compound-literal
5611 construct. (The C99 grammar uses `type-name' instead of `type-id',
5612 but they are essentially the same concept.)
5614 If ADDRESS_P is true, the postfix expression is the operand of the
5615 `&' operator. CAST_P is true if this expression is the target of a
5616 cast.
5618 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5619 class member access expressions [expr.ref].
5621 Returns a representation of the expression. */
5623 static tree
5624 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5625 bool member_access_only_p, bool decltype_p,
5626 cp_id_kind * pidk_return)
5628 cp_token *token;
5629 location_t loc;
5630 enum rid keyword;
5631 cp_id_kind idk = CP_ID_KIND_NONE;
5632 tree postfix_expression = NULL_TREE;
5633 bool is_member_access = false;
5634 int saved_in_statement = -1;
5636 /* Peek at the next token. */
5637 token = cp_lexer_peek_token (parser->lexer);
5638 loc = token->location;
5639 /* Some of the productions are determined by keywords. */
5640 keyword = token->keyword;
5641 switch (keyword)
5643 case RID_DYNCAST:
5644 case RID_STATCAST:
5645 case RID_REINTCAST:
5646 case RID_CONSTCAST:
5648 tree type;
5649 tree expression;
5650 const char *saved_message;
5651 bool saved_in_type_id_in_expr_p;
5653 /* All of these can be handled in the same way from the point
5654 of view of parsing. Begin by consuming the token
5655 identifying the cast. */
5656 cp_lexer_consume_token (parser->lexer);
5658 /* New types cannot be defined in the cast. */
5659 saved_message = parser->type_definition_forbidden_message;
5660 parser->type_definition_forbidden_message
5661 = G_("types may not be defined in casts");
5663 /* Look for the opening `<'. */
5664 cp_parser_require (parser, CPP_LESS, RT_LESS);
5665 /* Parse the type to which we are casting. */
5666 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5667 parser->in_type_id_in_expr_p = true;
5668 type = cp_parser_type_id (parser);
5669 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5670 /* Look for the closing `>'. */
5671 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5672 /* Restore the old message. */
5673 parser->type_definition_forbidden_message = saved_message;
5675 bool saved_greater_than_is_operator_p
5676 = parser->greater_than_is_operator_p;
5677 parser->greater_than_is_operator_p = true;
5679 /* And the expression which is being cast. */
5680 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5681 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5682 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5684 parser->greater_than_is_operator_p
5685 = saved_greater_than_is_operator_p;
5687 /* Only type conversions to integral or enumeration types
5688 can be used in constant-expressions. */
5689 if (!cast_valid_in_integral_constant_expression_p (type)
5690 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5691 return error_mark_node;
5693 switch (keyword)
5695 case RID_DYNCAST:
5696 postfix_expression
5697 = build_dynamic_cast (type, expression, tf_warning_or_error);
5698 break;
5699 case RID_STATCAST:
5700 postfix_expression
5701 = build_static_cast (type, expression, tf_warning_or_error);
5702 break;
5703 case RID_REINTCAST:
5704 postfix_expression
5705 = build_reinterpret_cast (type, expression,
5706 tf_warning_or_error);
5707 break;
5708 case RID_CONSTCAST:
5709 postfix_expression
5710 = build_const_cast (type, expression, tf_warning_or_error);
5711 break;
5712 default:
5713 gcc_unreachable ();
5716 break;
5718 case RID_TYPEID:
5720 tree type;
5721 const char *saved_message;
5722 bool saved_in_type_id_in_expr_p;
5724 /* Consume the `typeid' token. */
5725 cp_lexer_consume_token (parser->lexer);
5726 /* Look for the `(' token. */
5727 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5728 /* Types cannot be defined in a `typeid' expression. */
5729 saved_message = parser->type_definition_forbidden_message;
5730 parser->type_definition_forbidden_message
5731 = G_("types may not be defined in a %<typeid%> expression");
5732 /* We can't be sure yet whether we're looking at a type-id or an
5733 expression. */
5734 cp_parser_parse_tentatively (parser);
5735 /* Try a type-id first. */
5736 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5737 parser->in_type_id_in_expr_p = true;
5738 type = cp_parser_type_id (parser);
5739 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5740 /* Look for the `)' token. Otherwise, we can't be sure that
5741 we're not looking at an expression: consider `typeid (int
5742 (3))', for example. */
5743 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5744 /* If all went well, simply lookup the type-id. */
5745 if (cp_parser_parse_definitely (parser))
5746 postfix_expression = get_typeid (type, tf_warning_or_error);
5747 /* Otherwise, fall back to the expression variant. */
5748 else
5750 tree expression;
5752 /* Look for an expression. */
5753 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5754 /* Compute its typeid. */
5755 postfix_expression = build_typeid (expression, tf_warning_or_error);
5756 /* Look for the `)' token. */
5757 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5759 /* Restore the saved message. */
5760 parser->type_definition_forbidden_message = saved_message;
5761 /* `typeid' may not appear in an integral constant expression. */
5762 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5763 return error_mark_node;
5765 break;
5767 case RID_TYPENAME:
5769 tree type;
5770 /* The syntax permitted here is the same permitted for an
5771 elaborated-type-specifier. */
5772 type = cp_parser_elaborated_type_specifier (parser,
5773 /*is_friend=*/false,
5774 /*is_declaration=*/false);
5775 postfix_expression = cp_parser_functional_cast (parser, type);
5777 break;
5779 case RID_CILK_SPAWN:
5781 cp_lexer_consume_token (parser->lexer);
5782 token = cp_lexer_peek_token (parser->lexer);
5783 if (token->type == CPP_SEMICOLON)
5785 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5786 "an expression");
5787 postfix_expression = error_mark_node;
5788 break;
5790 else if (!current_function_decl)
5792 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5793 "inside a function");
5794 postfix_expression = error_mark_node;
5795 break;
5797 else
5799 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5800 saved_in_statement = parser->in_statement;
5801 parser->in_statement |= IN_CILK_SPAWN;
5803 cfun->calls_cilk_spawn = 1;
5804 postfix_expression =
5805 cp_parser_postfix_expression (parser, false, false,
5806 false, false, &idk);
5807 if (!flag_cilkplus)
5809 error_at (token->location, "-fcilkplus must be enabled to use"
5810 " %<_Cilk_spawn%>");
5811 cfun->calls_cilk_spawn = 0;
5813 else if (saved_in_statement & IN_CILK_SPAWN)
5815 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5816 "are not permitted");
5817 postfix_expression = error_mark_node;
5818 cfun->calls_cilk_spawn = 0;
5820 else
5822 postfix_expression = build_cilk_spawn (token->location,
5823 postfix_expression);
5824 if (postfix_expression != error_mark_node)
5825 SET_EXPR_LOCATION (postfix_expression, input_location);
5826 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5828 break;
5831 case RID_CILK_SYNC:
5832 if (flag_cilkplus)
5834 tree sync_expr = build_cilk_sync ();
5835 SET_EXPR_LOCATION (sync_expr,
5836 cp_lexer_peek_token (parser->lexer)->location);
5837 finish_expr_stmt (sync_expr);
5839 else
5840 error_at (token->location, "-fcilkplus must be enabled to use"
5841 " %<_Cilk_sync%>");
5842 cp_lexer_consume_token (parser->lexer);
5843 break;
5845 case RID_BUILTIN_SHUFFLE:
5847 vec<tree, va_gc> *vec;
5848 unsigned int i;
5849 tree p;
5851 cp_lexer_consume_token (parser->lexer);
5852 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5853 /*cast_p=*/false, /*allow_expansion_p=*/true,
5854 /*non_constant_p=*/NULL);
5855 if (vec == NULL)
5856 return error_mark_node;
5858 FOR_EACH_VEC_ELT (*vec, i, p)
5859 mark_exp_read (p);
5861 if (vec->length () == 2)
5862 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5863 tf_warning_or_error);
5864 else if (vec->length () == 3)
5865 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5866 tf_warning_or_error);
5867 else
5869 error_at (loc, "wrong number of arguments to "
5870 "%<__builtin_shuffle%>");
5871 return error_mark_node;
5873 break;
5876 default:
5878 tree type;
5880 /* If the next thing is a simple-type-specifier, we may be
5881 looking at a functional cast. We could also be looking at
5882 an id-expression. So, we try the functional cast, and if
5883 that doesn't work we fall back to the primary-expression. */
5884 cp_parser_parse_tentatively (parser);
5885 /* Look for the simple-type-specifier. */
5886 type = cp_parser_simple_type_specifier (parser,
5887 /*decl_specs=*/NULL,
5888 CP_PARSER_FLAGS_NONE);
5889 /* Parse the cast itself. */
5890 if (!cp_parser_error_occurred (parser))
5891 postfix_expression
5892 = cp_parser_functional_cast (parser, type);
5893 /* If that worked, we're done. */
5894 if (cp_parser_parse_definitely (parser))
5895 break;
5897 /* If the functional-cast didn't work out, try a
5898 compound-literal. */
5899 if (cp_parser_allow_gnu_extensions_p (parser)
5900 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5902 tree initializer = NULL_TREE;
5903 bool compound_literal_p;
5905 cp_parser_parse_tentatively (parser);
5906 /* Consume the `('. */
5907 cp_lexer_consume_token (parser->lexer);
5909 /* Avoid calling cp_parser_type_id pointlessly, see comment
5910 in cp_parser_cast_expression about c++/29234. */
5911 cp_lexer_save_tokens (parser->lexer);
5913 compound_literal_p
5914 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5915 /*consume_paren=*/true)
5916 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5918 /* Roll back the tokens we skipped. */
5919 cp_lexer_rollback_tokens (parser->lexer);
5921 if (!compound_literal_p)
5922 cp_parser_simulate_error (parser);
5923 else
5925 /* Parse the type. */
5926 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5927 parser->in_type_id_in_expr_p = true;
5928 type = cp_parser_type_id (parser);
5929 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5930 /* Look for the `)'. */
5931 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5934 /* If things aren't going well, there's no need to
5935 keep going. */
5936 if (!cp_parser_error_occurred (parser))
5938 bool non_constant_p;
5939 /* Parse the brace-enclosed initializer list. */
5940 initializer = cp_parser_braced_list (parser,
5941 &non_constant_p);
5943 /* If that worked, we're definitely looking at a
5944 compound-literal expression. */
5945 if (cp_parser_parse_definitely (parser))
5947 /* Warn the user that a compound literal is not
5948 allowed in standard C++. */
5949 pedwarn (input_location, OPT_Wpedantic,
5950 "ISO C++ forbids compound-literals");
5951 /* For simplicity, we disallow compound literals in
5952 constant-expressions. We could
5953 allow compound literals of integer type, whose
5954 initializer was a constant, in constant
5955 expressions. Permitting that usage, as a further
5956 extension, would not change the meaning of any
5957 currently accepted programs. (Of course, as
5958 compound literals are not part of ISO C++, the
5959 standard has nothing to say.) */
5960 if (cp_parser_non_integral_constant_expression (parser,
5961 NIC_NCC))
5963 postfix_expression = error_mark_node;
5964 break;
5966 /* Form the representation of the compound-literal. */
5967 postfix_expression
5968 = finish_compound_literal (type, initializer,
5969 tf_warning_or_error);
5970 break;
5974 /* It must be a primary-expression. */
5975 postfix_expression
5976 = cp_parser_primary_expression (parser, address_p, cast_p,
5977 /*template_arg_p=*/false,
5978 decltype_p,
5979 &idk);
5981 break;
5984 /* Note that we don't need to worry about calling build_cplus_new on a
5985 class-valued CALL_EXPR in decltype when it isn't the end of the
5986 postfix-expression; unary_complex_lvalue will take care of that for
5987 all these cases. */
5989 /* Keep looping until the postfix-expression is complete. */
5990 while (true)
5992 if (idk == CP_ID_KIND_UNQUALIFIED
5993 && identifier_p (postfix_expression)
5994 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5995 /* It is not a Koenig lookup function call. */
5996 postfix_expression
5997 = unqualified_name_lookup_error (postfix_expression);
5999 /* Peek at the next token. */
6000 token = cp_lexer_peek_token (parser->lexer);
6002 switch (token->type)
6004 case CPP_OPEN_SQUARE:
6005 if (cp_next_tokens_can_be_std_attribute_p (parser))
6007 cp_parser_error (parser,
6008 "two consecutive %<[%> shall "
6009 "only introduce an attribute");
6010 return error_mark_node;
6012 postfix_expression
6013 = cp_parser_postfix_open_square_expression (parser,
6014 postfix_expression,
6015 false,
6016 decltype_p);
6017 idk = CP_ID_KIND_NONE;
6018 is_member_access = false;
6019 break;
6021 case CPP_OPEN_PAREN:
6022 /* postfix-expression ( expression-list [opt] ) */
6024 bool koenig_p;
6025 bool is_builtin_constant_p;
6026 bool saved_integral_constant_expression_p = false;
6027 bool saved_non_integral_constant_expression_p = false;
6028 tsubst_flags_t complain = complain_flags (decltype_p);
6029 vec<tree, va_gc> *args;
6031 is_member_access = false;
6033 is_builtin_constant_p
6034 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6035 if (is_builtin_constant_p)
6037 /* The whole point of __builtin_constant_p is to allow
6038 non-constant expressions to appear as arguments. */
6039 saved_integral_constant_expression_p
6040 = parser->integral_constant_expression_p;
6041 saved_non_integral_constant_expression_p
6042 = parser->non_integral_constant_expression_p;
6043 parser->integral_constant_expression_p = false;
6045 args = (cp_parser_parenthesized_expression_list
6046 (parser, non_attr,
6047 /*cast_p=*/false, /*allow_expansion_p=*/true,
6048 /*non_constant_p=*/NULL));
6049 if (is_builtin_constant_p)
6051 parser->integral_constant_expression_p
6052 = saved_integral_constant_expression_p;
6053 parser->non_integral_constant_expression_p
6054 = saved_non_integral_constant_expression_p;
6057 if (args == NULL)
6059 postfix_expression = error_mark_node;
6060 break;
6063 /* Function calls are not permitted in
6064 constant-expressions. */
6065 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6066 && cp_parser_non_integral_constant_expression (parser,
6067 NIC_FUNC_CALL))
6069 postfix_expression = error_mark_node;
6070 release_tree_vector (args);
6071 break;
6074 koenig_p = false;
6075 if (idk == CP_ID_KIND_UNQUALIFIED
6076 || idk == CP_ID_KIND_TEMPLATE_ID)
6078 if (identifier_p (postfix_expression))
6080 if (!args->is_empty ())
6082 koenig_p = true;
6083 if (!any_type_dependent_arguments_p (args))
6084 postfix_expression
6085 = perform_koenig_lookup (postfix_expression, args,
6086 complain);
6088 else
6089 postfix_expression
6090 = unqualified_fn_lookup_error (postfix_expression);
6092 /* We do not perform argument-dependent lookup if
6093 normal lookup finds a non-function, in accordance
6094 with the expected resolution of DR 218. */
6095 else if (!args->is_empty ()
6096 && is_overloaded_fn (postfix_expression))
6098 tree fn = get_first_fn (postfix_expression);
6099 fn = STRIP_TEMPLATE (fn);
6101 /* Do not do argument dependent lookup if regular
6102 lookup finds a member function or a block-scope
6103 function declaration. [basic.lookup.argdep]/3 */
6104 if (!DECL_FUNCTION_MEMBER_P (fn)
6105 && !DECL_LOCAL_FUNCTION_P (fn))
6107 koenig_p = true;
6108 if (!any_type_dependent_arguments_p (args))
6109 postfix_expression
6110 = perform_koenig_lookup (postfix_expression, args,
6111 complain);
6116 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6118 tree instance = TREE_OPERAND (postfix_expression, 0);
6119 tree fn = TREE_OPERAND (postfix_expression, 1);
6121 if (processing_template_decl
6122 && (type_dependent_expression_p (instance)
6123 || (!BASELINK_P (fn)
6124 && TREE_CODE (fn) != FIELD_DECL)
6125 || type_dependent_expression_p (fn)
6126 || any_type_dependent_arguments_p (args)))
6128 postfix_expression
6129 = build_nt_call_vec (postfix_expression, args);
6130 release_tree_vector (args);
6131 break;
6134 if (BASELINK_P (fn))
6136 postfix_expression
6137 = (build_new_method_call
6138 (instance, fn, &args, NULL_TREE,
6139 (idk == CP_ID_KIND_QUALIFIED
6140 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6141 : LOOKUP_NORMAL),
6142 /*fn_p=*/NULL,
6143 complain));
6145 else
6146 postfix_expression
6147 = finish_call_expr (postfix_expression, &args,
6148 /*disallow_virtual=*/false,
6149 /*koenig_p=*/false,
6150 complain);
6152 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6153 || TREE_CODE (postfix_expression) == MEMBER_REF
6154 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6155 postfix_expression = (build_offset_ref_call_from_tree
6156 (postfix_expression, &args,
6157 complain));
6158 else if (idk == CP_ID_KIND_QUALIFIED)
6159 /* A call to a static class member, or a namespace-scope
6160 function. */
6161 postfix_expression
6162 = finish_call_expr (postfix_expression, &args,
6163 /*disallow_virtual=*/true,
6164 koenig_p,
6165 complain);
6166 else
6167 /* All other function calls. */
6168 postfix_expression
6169 = finish_call_expr (postfix_expression, &args,
6170 /*disallow_virtual=*/false,
6171 koenig_p,
6172 complain);
6174 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6175 idk = CP_ID_KIND_NONE;
6177 release_tree_vector (args);
6179 break;
6181 case CPP_DOT:
6182 case CPP_DEREF:
6183 /* postfix-expression . template [opt] id-expression
6184 postfix-expression . pseudo-destructor-name
6185 postfix-expression -> template [opt] id-expression
6186 postfix-expression -> pseudo-destructor-name */
6188 /* Consume the `.' or `->' operator. */
6189 cp_lexer_consume_token (parser->lexer);
6191 postfix_expression
6192 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6193 postfix_expression,
6194 false, &idk, loc);
6196 is_member_access = true;
6197 break;
6199 case CPP_PLUS_PLUS:
6200 /* postfix-expression ++ */
6201 /* Consume the `++' token. */
6202 cp_lexer_consume_token (parser->lexer);
6203 /* Generate a representation for the complete expression. */
6204 postfix_expression
6205 = finish_increment_expr (postfix_expression,
6206 POSTINCREMENT_EXPR);
6207 /* Increments may not appear in constant-expressions. */
6208 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6209 postfix_expression = error_mark_node;
6210 idk = CP_ID_KIND_NONE;
6211 is_member_access = false;
6212 break;
6214 case CPP_MINUS_MINUS:
6215 /* postfix-expression -- */
6216 /* Consume the `--' token. */
6217 cp_lexer_consume_token (parser->lexer);
6218 /* Generate a representation for the complete expression. */
6219 postfix_expression
6220 = finish_increment_expr (postfix_expression,
6221 POSTDECREMENT_EXPR);
6222 /* Decrements may not appear in constant-expressions. */
6223 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6224 postfix_expression = error_mark_node;
6225 idk = CP_ID_KIND_NONE;
6226 is_member_access = false;
6227 break;
6229 default:
6230 if (pidk_return != NULL)
6231 * pidk_return = idk;
6232 if (member_access_only_p)
6233 return is_member_access? postfix_expression : error_mark_node;
6234 else
6235 return postfix_expression;
6239 /* We should never get here. */
6240 gcc_unreachable ();
6241 return error_mark_node;
6244 /* This function parses Cilk Plus array notations. If a normal array expr. is
6245 parsed then the array index is passed back to the caller through *INIT_INDEX
6246 and the function returns a NULL_TREE. If array notation expr. is parsed,
6247 then *INIT_INDEX is ignored by the caller and the function returns
6248 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6249 error_mark_node. */
6251 static tree
6252 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6253 tree array_value)
6255 cp_token *token = NULL;
6256 tree length_index, stride = NULL_TREE, value_tree, array_type;
6257 if (!array_value || array_value == error_mark_node)
6259 cp_parser_skip_to_end_of_statement (parser);
6260 return error_mark_node;
6263 array_type = TREE_TYPE (array_value);
6265 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6266 parser->colon_corrects_to_scope_p = false;
6267 token = cp_lexer_peek_token (parser->lexer);
6269 if (!token)
6271 cp_parser_error (parser, "expected %<:%> or numeral");
6272 return error_mark_node;
6274 else if (token->type == CPP_COLON)
6276 /* Consume the ':'. */
6277 cp_lexer_consume_token (parser->lexer);
6279 /* If we are here, then we have a case like this A[:]. */
6280 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6282 cp_parser_error (parser, "expected %<]%>");
6283 cp_parser_skip_to_end_of_statement (parser);
6284 return error_mark_node;
6286 *init_index = NULL_TREE;
6287 stride = NULL_TREE;
6288 length_index = NULL_TREE;
6290 else
6292 /* If we are here, then there are three valid possibilities:
6293 1. ARRAY [ EXP ]
6294 2. ARRAY [ EXP : EXP ]
6295 3. ARRAY [ EXP : EXP : EXP ] */
6297 *init_index = cp_parser_expression (parser, false, NULL);
6298 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6300 /* This indicates that we have a normal array expression. */
6301 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6302 return NULL_TREE;
6305 /* Consume the ':'. */
6306 cp_lexer_consume_token (parser->lexer);
6307 length_index = cp_parser_expression (parser, false, NULL);
6308 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6310 cp_lexer_consume_token (parser->lexer);
6311 stride = cp_parser_expression (parser, false, NULL);
6314 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6316 if (*init_index == error_mark_node || length_index == error_mark_node
6317 || stride == error_mark_node)
6319 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6320 cp_lexer_consume_token (parser->lexer);
6321 return error_mark_node;
6323 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6325 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6326 length_index, stride, array_type);
6327 return value_tree;
6330 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6331 by cp_parser_builtin_offsetof. We're looking for
6333 postfix-expression [ expression ]
6334 postfix-expression [ braced-init-list ] (C++11)
6336 FOR_OFFSETOF is set if we're being called in that context, which
6337 changes how we deal with integer constant expressions. */
6339 static tree
6340 cp_parser_postfix_open_square_expression (cp_parser *parser,
6341 tree postfix_expression,
6342 bool for_offsetof,
6343 bool decltype_p)
6345 tree index = NULL_TREE;
6346 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6347 bool saved_greater_than_is_operator_p;
6349 /* Consume the `[' token. */
6350 cp_lexer_consume_token (parser->lexer);
6352 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6353 parser->greater_than_is_operator_p = true;
6355 /* Parse the index expression. */
6356 /* ??? For offsetof, there is a question of what to allow here. If
6357 offsetof is not being used in an integral constant expression context,
6358 then we *could* get the right answer by computing the value at runtime.
6359 If we are in an integral constant expression context, then we might
6360 could accept any constant expression; hard to say without analysis.
6361 Rather than open the barn door too wide right away, allow only integer
6362 constant expressions here. */
6363 if (for_offsetof)
6364 index = cp_parser_constant_expression (parser, false, NULL);
6365 else
6367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6369 bool expr_nonconst_p;
6370 cp_lexer_set_source_position (parser->lexer);
6371 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6372 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6373 if (flag_cilkplus
6374 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6376 error_at (cp_lexer_peek_token (parser->lexer)->location,
6377 "braced list index is not allowed with array "
6378 "notation");
6379 cp_parser_skip_to_end_of_statement (parser);
6380 return error_mark_node;
6383 else if (flag_cilkplus)
6385 /* Here are have these two options:
6386 ARRAY[EXP : EXP] - Array notation expr with default
6387 stride of 1.
6388 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6389 stride. */
6390 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6391 postfix_expression);
6392 if (an_exp)
6393 return an_exp;
6395 else
6396 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6399 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6401 /* Look for the closing `]'. */
6402 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6404 /* Build the ARRAY_REF. */
6405 postfix_expression = grok_array_decl (loc, postfix_expression,
6406 index, decltype_p);
6408 /* When not doing offsetof, array references are not permitted in
6409 constant-expressions. */
6410 if (!for_offsetof
6411 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6412 postfix_expression = error_mark_node;
6414 return postfix_expression;
6417 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6418 by cp_parser_builtin_offsetof. We're looking for
6420 postfix-expression . template [opt] id-expression
6421 postfix-expression . pseudo-destructor-name
6422 postfix-expression -> template [opt] id-expression
6423 postfix-expression -> pseudo-destructor-name
6425 FOR_OFFSETOF is set if we're being called in that context. That sorta
6426 limits what of the above we'll actually accept, but nevermind.
6427 TOKEN_TYPE is the "." or "->" token, which will already have been
6428 removed from the stream. */
6430 static tree
6431 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6432 enum cpp_ttype token_type,
6433 tree postfix_expression,
6434 bool for_offsetof, cp_id_kind *idk,
6435 location_t location)
6437 tree name;
6438 bool dependent_p;
6439 bool pseudo_destructor_p;
6440 tree scope = NULL_TREE;
6442 /* If this is a `->' operator, dereference the pointer. */
6443 if (token_type == CPP_DEREF)
6444 postfix_expression = build_x_arrow (location, postfix_expression,
6445 tf_warning_or_error);
6446 /* Check to see whether or not the expression is type-dependent. */
6447 dependent_p = type_dependent_expression_p (postfix_expression);
6448 /* The identifier following the `->' or `.' is not qualified. */
6449 parser->scope = NULL_TREE;
6450 parser->qualifying_scope = NULL_TREE;
6451 parser->object_scope = NULL_TREE;
6452 *idk = CP_ID_KIND_NONE;
6454 /* Enter the scope corresponding to the type of the object
6455 given by the POSTFIX_EXPRESSION. */
6456 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6458 scope = TREE_TYPE (postfix_expression);
6459 /* According to the standard, no expression should ever have
6460 reference type. Unfortunately, we do not currently match
6461 the standard in this respect in that our internal representation
6462 of an expression may have reference type even when the standard
6463 says it does not. Therefore, we have to manually obtain the
6464 underlying type here. */
6465 scope = non_reference (scope);
6466 /* The type of the POSTFIX_EXPRESSION must be complete. */
6467 if (scope == unknown_type_node)
6469 error_at (location, "%qE does not have class type",
6470 postfix_expression);
6471 scope = NULL_TREE;
6473 /* Unlike the object expression in other contexts, *this is not
6474 required to be of complete type for purposes of class member
6475 access (5.2.5) outside the member function body. */
6476 else if (postfix_expression != current_class_ref
6477 && !(processing_template_decl && scope == current_class_type))
6478 scope = complete_type_or_else (scope, NULL_TREE);
6479 /* Let the name lookup machinery know that we are processing a
6480 class member access expression. */
6481 parser->context->object_type = scope;
6482 /* If something went wrong, we want to be able to discern that case,
6483 as opposed to the case where there was no SCOPE due to the type
6484 of expression being dependent. */
6485 if (!scope)
6486 scope = error_mark_node;
6487 /* If the SCOPE was erroneous, make the various semantic analysis
6488 functions exit quickly -- and without issuing additional error
6489 messages. */
6490 if (scope == error_mark_node)
6491 postfix_expression = error_mark_node;
6494 /* Assume this expression is not a pseudo-destructor access. */
6495 pseudo_destructor_p = false;
6497 /* If the SCOPE is a scalar type, then, if this is a valid program,
6498 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6499 is type dependent, it can be pseudo-destructor-name or something else.
6500 Try to parse it as pseudo-destructor-name first. */
6501 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6503 tree s;
6504 tree type;
6506 cp_parser_parse_tentatively (parser);
6507 /* Parse the pseudo-destructor-name. */
6508 s = NULL_TREE;
6509 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6510 &s, &type);
6511 if (dependent_p
6512 && (cp_parser_error_occurred (parser)
6513 || !SCALAR_TYPE_P (type)))
6514 cp_parser_abort_tentative_parse (parser);
6515 else if (cp_parser_parse_definitely (parser))
6517 pseudo_destructor_p = true;
6518 postfix_expression
6519 = finish_pseudo_destructor_expr (postfix_expression,
6520 s, type, location);
6524 if (!pseudo_destructor_p)
6526 /* If the SCOPE is not a scalar type, we are looking at an
6527 ordinary class member access expression, rather than a
6528 pseudo-destructor-name. */
6529 bool template_p;
6530 cp_token *token = cp_lexer_peek_token (parser->lexer);
6531 /* Parse the id-expression. */
6532 name = (cp_parser_id_expression
6533 (parser,
6534 cp_parser_optional_template_keyword (parser),
6535 /*check_dependency_p=*/true,
6536 &template_p,
6537 /*declarator_p=*/false,
6538 /*optional_p=*/false));
6539 /* In general, build a SCOPE_REF if the member name is qualified.
6540 However, if the name was not dependent and has already been
6541 resolved; there is no need to build the SCOPE_REF. For example;
6543 struct X { void f(); };
6544 template <typename T> void f(T* t) { t->X::f(); }
6546 Even though "t" is dependent, "X::f" is not and has been resolved
6547 to a BASELINK; there is no need to include scope information. */
6549 /* But we do need to remember that there was an explicit scope for
6550 virtual function calls. */
6551 if (parser->scope)
6552 *idk = CP_ID_KIND_QUALIFIED;
6554 /* If the name is a template-id that names a type, we will get a
6555 TYPE_DECL here. That is invalid code. */
6556 if (TREE_CODE (name) == TYPE_DECL)
6558 error_at (token->location, "invalid use of %qD", name);
6559 postfix_expression = error_mark_node;
6561 else
6563 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6565 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6567 error_at (token->location, "%<%D::%D%> is not a class member",
6568 parser->scope, name);
6569 postfix_expression = error_mark_node;
6571 else
6572 name = build_qualified_name (/*type=*/NULL_TREE,
6573 parser->scope,
6574 name,
6575 template_p);
6576 parser->scope = NULL_TREE;
6577 parser->qualifying_scope = NULL_TREE;
6578 parser->object_scope = NULL_TREE;
6580 if (parser->scope && name && BASELINK_P (name))
6581 adjust_result_of_qualified_name_lookup
6582 (name, parser->scope, scope);
6583 postfix_expression
6584 = finish_class_member_access_expr (postfix_expression, name,
6585 template_p,
6586 tf_warning_or_error);
6590 /* We no longer need to look up names in the scope of the object on
6591 the left-hand side of the `.' or `->' operator. */
6592 parser->context->object_type = NULL_TREE;
6594 /* Outside of offsetof, these operators may not appear in
6595 constant-expressions. */
6596 if (!for_offsetof
6597 && (cp_parser_non_integral_constant_expression
6598 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6599 postfix_expression = error_mark_node;
6601 return postfix_expression;
6604 /* Parse a parenthesized expression-list.
6606 expression-list:
6607 assignment-expression
6608 expression-list, assignment-expression
6610 attribute-list:
6611 expression-list
6612 identifier
6613 identifier, expression-list
6615 CAST_P is true if this expression is the target of a cast.
6617 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6618 argument pack.
6620 Returns a vector of trees. Each element is a representation of an
6621 assignment-expression. NULL is returned if the ( and or ) are
6622 missing. An empty, but allocated, vector is returned on no
6623 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6624 if we are parsing an attribute list for an attribute that wants a
6625 plain identifier argument, normal_attr for an attribute that wants
6626 an expression, or non_attr if we aren't parsing an attribute list. If
6627 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6628 not all of the expressions in the list were constant. */
6630 static vec<tree, va_gc> *
6631 cp_parser_parenthesized_expression_list (cp_parser* parser,
6632 int is_attribute_list,
6633 bool cast_p,
6634 bool allow_expansion_p,
6635 bool *non_constant_p)
6637 vec<tree, va_gc> *expression_list;
6638 bool fold_expr_p = is_attribute_list != non_attr;
6639 tree identifier = NULL_TREE;
6640 bool saved_greater_than_is_operator_p;
6642 /* Assume all the expressions will be constant. */
6643 if (non_constant_p)
6644 *non_constant_p = false;
6646 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6647 return NULL;
6649 expression_list = make_tree_vector ();
6651 /* Within a parenthesized expression, a `>' token is always
6652 the greater-than operator. */
6653 saved_greater_than_is_operator_p
6654 = parser->greater_than_is_operator_p;
6655 parser->greater_than_is_operator_p = true;
6657 /* Consume expressions until there are no more. */
6658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6659 while (true)
6661 tree expr;
6663 /* At the beginning of attribute lists, check to see if the
6664 next token is an identifier. */
6665 if (is_attribute_list == id_attr
6666 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6668 cp_token *token;
6670 /* Consume the identifier. */
6671 token = cp_lexer_consume_token (parser->lexer);
6672 /* Save the identifier. */
6673 identifier = token->u.value;
6675 else
6677 bool expr_non_constant_p;
6679 /* Parse the next assignment-expression. */
6680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6682 /* A braced-init-list. */
6683 cp_lexer_set_source_position (parser->lexer);
6684 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6685 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6686 if (non_constant_p && expr_non_constant_p)
6687 *non_constant_p = true;
6689 else if (non_constant_p)
6691 expr = (cp_parser_constant_expression
6692 (parser, /*allow_non_constant_p=*/true,
6693 &expr_non_constant_p));
6694 if (expr_non_constant_p)
6695 *non_constant_p = true;
6697 else
6698 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6700 if (fold_expr_p)
6701 expr = fold_non_dependent_expr (expr);
6703 /* If we have an ellipsis, then this is an expression
6704 expansion. */
6705 if (allow_expansion_p
6706 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6708 /* Consume the `...'. */
6709 cp_lexer_consume_token (parser->lexer);
6711 /* Build the argument pack. */
6712 expr = make_pack_expansion (expr);
6715 /* Add it to the list. We add error_mark_node
6716 expressions to the list, so that we can still tell if
6717 the correct form for a parenthesized expression-list
6718 is found. That gives better errors. */
6719 vec_safe_push (expression_list, expr);
6721 if (expr == error_mark_node)
6722 goto skip_comma;
6725 /* After the first item, attribute lists look the same as
6726 expression lists. */
6727 is_attribute_list = non_attr;
6729 get_comma:;
6730 /* If the next token isn't a `,', then we are done. */
6731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6732 break;
6734 /* Otherwise, consume the `,' and keep going. */
6735 cp_lexer_consume_token (parser->lexer);
6738 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6740 int ending;
6742 skip_comma:;
6743 /* We try and resync to an unnested comma, as that will give the
6744 user better diagnostics. */
6745 ending = cp_parser_skip_to_closing_parenthesis (parser,
6746 /*recovering=*/true,
6747 /*or_comma=*/true,
6748 /*consume_paren=*/true);
6749 if (ending < 0)
6750 goto get_comma;
6751 if (!ending)
6753 parser->greater_than_is_operator_p
6754 = saved_greater_than_is_operator_p;
6755 return NULL;
6759 parser->greater_than_is_operator_p
6760 = saved_greater_than_is_operator_p;
6762 if (identifier)
6763 vec_safe_insert (expression_list, 0, identifier);
6765 return expression_list;
6768 /* Parse a pseudo-destructor-name.
6770 pseudo-destructor-name:
6771 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6772 :: [opt] nested-name-specifier template template-id :: ~ type-name
6773 :: [opt] nested-name-specifier [opt] ~ type-name
6775 If either of the first two productions is used, sets *SCOPE to the
6776 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6777 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6778 or ERROR_MARK_NODE if the parse fails. */
6780 static void
6781 cp_parser_pseudo_destructor_name (cp_parser* parser,
6782 tree object,
6783 tree* scope,
6784 tree* type)
6786 bool nested_name_specifier_p;
6788 /* Handle ~auto. */
6789 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6790 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6791 && !type_dependent_expression_p (object))
6793 if (cxx_dialect < cxx1y)
6794 pedwarn (input_location, 0,
6795 "%<~auto%> only available with "
6796 "-std=c++1y or -std=gnu++1y");
6797 cp_lexer_consume_token (parser->lexer);
6798 cp_lexer_consume_token (parser->lexer);
6799 *scope = NULL_TREE;
6800 *type = TREE_TYPE (object);
6801 return;
6804 /* Assume that things will not work out. */
6805 *type = error_mark_node;
6807 /* Look for the optional `::' operator. */
6808 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6809 /* Look for the optional nested-name-specifier. */
6810 nested_name_specifier_p
6811 = (cp_parser_nested_name_specifier_opt (parser,
6812 /*typename_keyword_p=*/false,
6813 /*check_dependency_p=*/true,
6814 /*type_p=*/false,
6815 /*is_declaration=*/false)
6816 != NULL_TREE);
6817 /* Now, if we saw a nested-name-specifier, we might be doing the
6818 second production. */
6819 if (nested_name_specifier_p
6820 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6822 /* Consume the `template' keyword. */
6823 cp_lexer_consume_token (parser->lexer);
6824 /* Parse the template-id. */
6825 cp_parser_template_id (parser,
6826 /*template_keyword_p=*/true,
6827 /*check_dependency_p=*/false,
6828 class_type,
6829 /*is_declaration=*/true);
6830 /* Look for the `::' token. */
6831 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6833 /* If the next token is not a `~', then there might be some
6834 additional qualification. */
6835 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6837 /* At this point, we're looking for "type-name :: ~". The type-name
6838 must not be a class-name, since this is a pseudo-destructor. So,
6839 it must be either an enum-name, or a typedef-name -- both of which
6840 are just identifiers. So, we peek ahead to check that the "::"
6841 and "~" tokens are present; if they are not, then we can avoid
6842 calling type_name. */
6843 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6844 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6845 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6847 cp_parser_error (parser, "non-scalar type");
6848 return;
6851 /* Look for the type-name. */
6852 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6853 if (*scope == error_mark_node)
6854 return;
6856 /* Look for the `::' token. */
6857 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6859 else
6860 *scope = NULL_TREE;
6862 /* Look for the `~'. */
6863 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6865 /* Once we see the ~, this has to be a pseudo-destructor. */
6866 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6867 cp_parser_commit_to_topmost_tentative_parse (parser);
6869 /* Look for the type-name again. We are not responsible for
6870 checking that it matches the first type-name. */
6871 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6874 /* Parse a unary-expression.
6876 unary-expression:
6877 postfix-expression
6878 ++ cast-expression
6879 -- cast-expression
6880 unary-operator cast-expression
6881 sizeof unary-expression
6882 sizeof ( type-id )
6883 alignof ( type-id ) [C++0x]
6884 new-expression
6885 delete-expression
6887 GNU Extensions:
6889 unary-expression:
6890 __extension__ cast-expression
6891 __alignof__ unary-expression
6892 __alignof__ ( type-id )
6893 alignof unary-expression [C++0x]
6894 __real__ cast-expression
6895 __imag__ cast-expression
6896 && identifier
6897 sizeof ( type-id ) { initializer-list , [opt] }
6898 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6899 __alignof__ ( type-id ) { initializer-list , [opt] }
6901 ADDRESS_P is true iff the unary-expression is appearing as the
6902 operand of the `&' operator. CAST_P is true if this expression is
6903 the target of a cast.
6905 Returns a representation of the expression. */
6907 static tree
6908 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6909 bool decltype_p, cp_id_kind * pidk)
6911 cp_token *token;
6912 enum tree_code unary_operator;
6914 /* Peek at the next token. */
6915 token = cp_lexer_peek_token (parser->lexer);
6916 /* Some keywords give away the kind of expression. */
6917 if (token->type == CPP_KEYWORD)
6919 enum rid keyword = token->keyword;
6921 switch (keyword)
6923 case RID_ALIGNOF:
6924 case RID_SIZEOF:
6926 tree operand, ret;
6927 enum tree_code op;
6928 location_t first_loc;
6930 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6931 /* Consume the token. */
6932 cp_lexer_consume_token (parser->lexer);
6933 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6934 /* Parse the operand. */
6935 operand = cp_parser_sizeof_operand (parser, keyword);
6937 if (TYPE_P (operand))
6938 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6939 else
6941 /* ISO C++ defines alignof only with types, not with
6942 expressions. So pedwarn if alignof is used with a non-
6943 type expression. However, __alignof__ is ok. */
6944 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6945 pedwarn (token->location, OPT_Wpedantic,
6946 "ISO C++ does not allow %<alignof%> "
6947 "with a non-type");
6949 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6951 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6952 SIZEOF_EXPR with the original operand. */
6953 if (op == SIZEOF_EXPR && ret != error_mark_node)
6955 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6957 if (!processing_template_decl && TYPE_P (operand))
6959 ret = build_min (SIZEOF_EXPR, size_type_node,
6960 build1 (NOP_EXPR, operand,
6961 error_mark_node));
6962 SIZEOF_EXPR_TYPE_P (ret) = 1;
6964 else
6965 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6966 TREE_SIDE_EFFECTS (ret) = 0;
6967 TREE_READONLY (ret) = 1;
6969 SET_EXPR_LOCATION (ret, first_loc);
6971 return ret;
6974 case RID_NEW:
6975 return cp_parser_new_expression (parser);
6977 case RID_DELETE:
6978 return cp_parser_delete_expression (parser);
6980 case RID_EXTENSION:
6982 /* The saved value of the PEDANTIC flag. */
6983 int saved_pedantic;
6984 tree expr;
6986 /* Save away the PEDANTIC flag. */
6987 cp_parser_extension_opt (parser, &saved_pedantic);
6988 /* Parse the cast-expression. */
6989 expr = cp_parser_simple_cast_expression (parser);
6990 /* Restore the PEDANTIC flag. */
6991 pedantic = saved_pedantic;
6993 return expr;
6996 case RID_REALPART:
6997 case RID_IMAGPART:
6999 tree expression;
7001 /* Consume the `__real__' or `__imag__' token. */
7002 cp_lexer_consume_token (parser->lexer);
7003 /* Parse the cast-expression. */
7004 expression = cp_parser_simple_cast_expression (parser);
7005 /* Create the complete representation. */
7006 return build_x_unary_op (token->location,
7007 (keyword == RID_REALPART
7008 ? REALPART_EXPR : IMAGPART_EXPR),
7009 expression,
7010 tf_warning_or_error);
7012 break;
7014 case RID_TRANSACTION_ATOMIC:
7015 case RID_TRANSACTION_RELAXED:
7016 return cp_parser_transaction_expression (parser, keyword);
7018 case RID_NOEXCEPT:
7020 tree expr;
7021 const char *saved_message;
7022 bool saved_integral_constant_expression_p;
7023 bool saved_non_integral_constant_expression_p;
7024 bool saved_greater_than_is_operator_p;
7026 cp_lexer_consume_token (parser->lexer);
7027 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7029 saved_message = parser->type_definition_forbidden_message;
7030 parser->type_definition_forbidden_message
7031 = G_("types may not be defined in %<noexcept%> expressions");
7033 saved_integral_constant_expression_p
7034 = parser->integral_constant_expression_p;
7035 saved_non_integral_constant_expression_p
7036 = parser->non_integral_constant_expression_p;
7037 parser->integral_constant_expression_p = false;
7039 saved_greater_than_is_operator_p
7040 = parser->greater_than_is_operator_p;
7041 parser->greater_than_is_operator_p = true;
7043 ++cp_unevaluated_operand;
7044 ++c_inhibit_evaluation_warnings;
7045 expr = cp_parser_expression (parser, false, NULL);
7046 --c_inhibit_evaluation_warnings;
7047 --cp_unevaluated_operand;
7049 parser->greater_than_is_operator_p
7050 = saved_greater_than_is_operator_p;
7052 parser->integral_constant_expression_p
7053 = saved_integral_constant_expression_p;
7054 parser->non_integral_constant_expression_p
7055 = saved_non_integral_constant_expression_p;
7057 parser->type_definition_forbidden_message = saved_message;
7059 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7060 return finish_noexcept_expr (expr, tf_warning_or_error);
7063 default:
7064 break;
7068 /* Look for the `:: new' and `:: delete', which also signal the
7069 beginning of a new-expression, or delete-expression,
7070 respectively. If the next token is `::', then it might be one of
7071 these. */
7072 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7074 enum rid keyword;
7076 /* See if the token after the `::' is one of the keywords in
7077 which we're interested. */
7078 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7079 /* If it's `new', we have a new-expression. */
7080 if (keyword == RID_NEW)
7081 return cp_parser_new_expression (parser);
7082 /* Similarly, for `delete'. */
7083 else if (keyword == RID_DELETE)
7084 return cp_parser_delete_expression (parser);
7087 /* Look for a unary operator. */
7088 unary_operator = cp_parser_unary_operator (token);
7089 /* The `++' and `--' operators can be handled similarly, even though
7090 they are not technically unary-operators in the grammar. */
7091 if (unary_operator == ERROR_MARK)
7093 if (token->type == CPP_PLUS_PLUS)
7094 unary_operator = PREINCREMENT_EXPR;
7095 else if (token->type == CPP_MINUS_MINUS)
7096 unary_operator = PREDECREMENT_EXPR;
7097 /* Handle the GNU address-of-label extension. */
7098 else if (cp_parser_allow_gnu_extensions_p (parser)
7099 && token->type == CPP_AND_AND)
7101 tree identifier;
7102 tree expression;
7103 location_t loc = token->location;
7105 /* Consume the '&&' token. */
7106 cp_lexer_consume_token (parser->lexer);
7107 /* Look for the identifier. */
7108 identifier = cp_parser_identifier (parser);
7109 /* Create an expression representing the address. */
7110 expression = finish_label_address_expr (identifier, loc);
7111 if (cp_parser_non_integral_constant_expression (parser,
7112 NIC_ADDR_LABEL))
7113 expression = error_mark_node;
7114 return expression;
7117 if (unary_operator != ERROR_MARK)
7119 tree cast_expression;
7120 tree expression = error_mark_node;
7121 non_integral_constant non_constant_p = NIC_NONE;
7122 location_t loc = token->location;
7123 tsubst_flags_t complain = complain_flags (decltype_p);
7125 /* Consume the operator token. */
7126 token = cp_lexer_consume_token (parser->lexer);
7127 /* Parse the cast-expression. */
7128 cast_expression
7129 = cp_parser_cast_expression (parser,
7130 unary_operator == ADDR_EXPR,
7131 /*cast_p=*/false,
7132 /*decltype*/false,
7133 pidk);
7134 /* Now, build an appropriate representation. */
7135 switch (unary_operator)
7137 case INDIRECT_REF:
7138 non_constant_p = NIC_STAR;
7139 expression = build_x_indirect_ref (loc, cast_expression,
7140 RO_UNARY_STAR,
7141 complain);
7142 break;
7144 case ADDR_EXPR:
7145 non_constant_p = NIC_ADDR;
7146 /* Fall through. */
7147 case BIT_NOT_EXPR:
7148 expression = build_x_unary_op (loc, unary_operator,
7149 cast_expression,
7150 complain);
7151 break;
7153 case PREINCREMENT_EXPR:
7154 case PREDECREMENT_EXPR:
7155 non_constant_p = unary_operator == PREINCREMENT_EXPR
7156 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7157 /* Fall through. */
7158 case UNARY_PLUS_EXPR:
7159 case NEGATE_EXPR:
7160 case TRUTH_NOT_EXPR:
7161 expression = finish_unary_op_expr (loc, unary_operator,
7162 cast_expression, complain);
7163 break;
7165 default:
7166 gcc_unreachable ();
7169 if (non_constant_p != NIC_NONE
7170 && cp_parser_non_integral_constant_expression (parser,
7171 non_constant_p))
7172 expression = error_mark_node;
7174 return expression;
7177 return cp_parser_postfix_expression (parser, address_p, cast_p,
7178 /*member_access_only_p=*/false,
7179 decltype_p,
7180 pidk);
7183 static inline tree
7184 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7185 cp_id_kind * pidk)
7187 return cp_parser_unary_expression (parser, address_p, cast_p,
7188 /*decltype*/false, pidk);
7191 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7192 unary-operator, the corresponding tree code is returned. */
7194 static enum tree_code
7195 cp_parser_unary_operator (cp_token* token)
7197 switch (token->type)
7199 case CPP_MULT:
7200 return INDIRECT_REF;
7202 case CPP_AND:
7203 return ADDR_EXPR;
7205 case CPP_PLUS:
7206 return UNARY_PLUS_EXPR;
7208 case CPP_MINUS:
7209 return NEGATE_EXPR;
7211 case CPP_NOT:
7212 return TRUTH_NOT_EXPR;
7214 case CPP_COMPL:
7215 return BIT_NOT_EXPR;
7217 default:
7218 return ERROR_MARK;
7222 /* Parse a new-expression.
7224 new-expression:
7225 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7226 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7228 Returns a representation of the expression. */
7230 static tree
7231 cp_parser_new_expression (cp_parser* parser)
7233 bool global_scope_p;
7234 vec<tree, va_gc> *placement;
7235 tree type;
7236 vec<tree, va_gc> *initializer;
7237 tree nelts = NULL_TREE;
7238 tree ret;
7240 /* Look for the optional `::' operator. */
7241 global_scope_p
7242 = (cp_parser_global_scope_opt (parser,
7243 /*current_scope_valid_p=*/false)
7244 != NULL_TREE);
7245 /* Look for the `new' operator. */
7246 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7247 /* There's no easy way to tell a new-placement from the
7248 `( type-id )' construct. */
7249 cp_parser_parse_tentatively (parser);
7250 /* Look for a new-placement. */
7251 placement = cp_parser_new_placement (parser);
7252 /* If that didn't work out, there's no new-placement. */
7253 if (!cp_parser_parse_definitely (parser))
7255 if (placement != NULL)
7256 release_tree_vector (placement);
7257 placement = NULL;
7260 /* If the next token is a `(', then we have a parenthesized
7261 type-id. */
7262 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7264 cp_token *token;
7265 const char *saved_message = parser->type_definition_forbidden_message;
7267 /* Consume the `('. */
7268 cp_lexer_consume_token (parser->lexer);
7270 /* Parse the type-id. */
7271 parser->type_definition_forbidden_message
7272 = G_("types may not be defined in a new-expression");
7273 type = cp_parser_type_id (parser);
7274 parser->type_definition_forbidden_message = saved_message;
7276 /* Look for the closing `)'. */
7277 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7278 token = cp_lexer_peek_token (parser->lexer);
7279 /* There should not be a direct-new-declarator in this production,
7280 but GCC used to allowed this, so we check and emit a sensible error
7281 message for this case. */
7282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7284 error_at (token->location,
7285 "array bound forbidden after parenthesized type-id");
7286 inform (token->location,
7287 "try removing the parentheses around the type-id");
7288 cp_parser_direct_new_declarator (parser);
7291 /* Otherwise, there must be a new-type-id. */
7292 else
7293 type = cp_parser_new_type_id (parser, &nelts);
7295 /* If the next token is a `(' or '{', then we have a new-initializer. */
7296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7297 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7298 initializer = cp_parser_new_initializer (parser);
7299 else
7300 initializer = NULL;
7302 /* A new-expression may not appear in an integral constant
7303 expression. */
7304 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7305 ret = error_mark_node;
7306 else
7308 /* Create a representation of the new-expression. */
7309 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7310 tf_warning_or_error);
7313 if (placement != NULL)
7314 release_tree_vector (placement);
7315 if (initializer != NULL)
7316 release_tree_vector (initializer);
7318 return ret;
7321 /* Parse a new-placement.
7323 new-placement:
7324 ( expression-list )
7326 Returns the same representation as for an expression-list. */
7328 static vec<tree, va_gc> *
7329 cp_parser_new_placement (cp_parser* parser)
7331 vec<tree, va_gc> *expression_list;
7333 /* Parse the expression-list. */
7334 expression_list = (cp_parser_parenthesized_expression_list
7335 (parser, non_attr, /*cast_p=*/false,
7336 /*allow_expansion_p=*/true,
7337 /*non_constant_p=*/NULL));
7339 return expression_list;
7342 /* Parse a new-type-id.
7344 new-type-id:
7345 type-specifier-seq new-declarator [opt]
7347 Returns the TYPE allocated. If the new-type-id indicates an array
7348 type, *NELTS is set to the number of elements in the last array
7349 bound; the TYPE will not include the last array bound. */
7351 static tree
7352 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7354 cp_decl_specifier_seq type_specifier_seq;
7355 cp_declarator *new_declarator;
7356 cp_declarator *declarator;
7357 cp_declarator *outer_declarator;
7358 const char *saved_message;
7360 /* The type-specifier sequence must not contain type definitions.
7361 (It cannot contain declarations of new types either, but if they
7362 are not definitions we will catch that because they are not
7363 complete.) */
7364 saved_message = parser->type_definition_forbidden_message;
7365 parser->type_definition_forbidden_message
7366 = G_("types may not be defined in a new-type-id");
7367 /* Parse the type-specifier-seq. */
7368 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7369 /*is_trailing_return=*/false,
7370 &type_specifier_seq);
7371 /* Restore the old message. */
7372 parser->type_definition_forbidden_message = saved_message;
7374 if (type_specifier_seq.type == error_mark_node)
7375 return error_mark_node;
7377 /* Parse the new-declarator. */
7378 new_declarator = cp_parser_new_declarator_opt (parser);
7380 /* Determine the number of elements in the last array dimension, if
7381 any. */
7382 *nelts = NULL_TREE;
7383 /* Skip down to the last array dimension. */
7384 declarator = new_declarator;
7385 outer_declarator = NULL;
7386 while (declarator && (declarator->kind == cdk_pointer
7387 || declarator->kind == cdk_ptrmem))
7389 outer_declarator = declarator;
7390 declarator = declarator->declarator;
7392 while (declarator
7393 && declarator->kind == cdk_array
7394 && declarator->declarator
7395 && declarator->declarator->kind == cdk_array)
7397 outer_declarator = declarator;
7398 declarator = declarator->declarator;
7401 if (declarator && declarator->kind == cdk_array)
7403 *nelts = declarator->u.array.bounds;
7404 if (*nelts == error_mark_node)
7405 *nelts = integer_one_node;
7407 if (outer_declarator)
7408 outer_declarator->declarator = declarator->declarator;
7409 else
7410 new_declarator = NULL;
7413 return groktypename (&type_specifier_seq, new_declarator, false);
7416 /* Parse an (optional) new-declarator.
7418 new-declarator:
7419 ptr-operator new-declarator [opt]
7420 direct-new-declarator
7422 Returns the declarator. */
7424 static cp_declarator *
7425 cp_parser_new_declarator_opt (cp_parser* parser)
7427 enum tree_code code;
7428 tree type, std_attributes = NULL_TREE;
7429 cp_cv_quals cv_quals;
7431 /* We don't know if there's a ptr-operator next, or not. */
7432 cp_parser_parse_tentatively (parser);
7433 /* Look for a ptr-operator. */
7434 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7435 /* If that worked, look for more new-declarators. */
7436 if (cp_parser_parse_definitely (parser))
7438 cp_declarator *declarator;
7440 /* Parse another optional declarator. */
7441 declarator = cp_parser_new_declarator_opt (parser);
7443 declarator = cp_parser_make_indirect_declarator
7444 (code, type, cv_quals, declarator, std_attributes);
7446 return declarator;
7449 /* If the next token is a `[', there is a direct-new-declarator. */
7450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7451 return cp_parser_direct_new_declarator (parser);
7453 return NULL;
7456 /* Parse a direct-new-declarator.
7458 direct-new-declarator:
7459 [ expression ]
7460 direct-new-declarator [constant-expression]
7464 static cp_declarator *
7465 cp_parser_direct_new_declarator (cp_parser* parser)
7467 cp_declarator *declarator = NULL;
7469 while (true)
7471 tree expression;
7472 cp_token *token;
7474 /* Look for the opening `['. */
7475 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7477 token = cp_lexer_peek_token (parser->lexer);
7478 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7479 /* The standard requires that the expression have integral
7480 type. DR 74 adds enumeration types. We believe that the
7481 real intent is that these expressions be handled like the
7482 expression in a `switch' condition, which also allows
7483 classes with a single conversion to integral or
7484 enumeration type. */
7485 if (!processing_template_decl)
7487 expression
7488 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7489 expression,
7490 /*complain=*/true);
7491 if (!expression)
7493 error_at (token->location,
7494 "expression in new-declarator must have integral "
7495 "or enumeration type");
7496 expression = error_mark_node;
7500 /* Look for the closing `]'. */
7501 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7503 /* Add this bound to the declarator. */
7504 declarator = make_array_declarator (declarator, expression);
7506 /* If the next token is not a `[', then there are no more
7507 bounds. */
7508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7509 break;
7512 return declarator;
7515 /* Parse a new-initializer.
7517 new-initializer:
7518 ( expression-list [opt] )
7519 braced-init-list
7521 Returns a representation of the expression-list. */
7523 static vec<tree, va_gc> *
7524 cp_parser_new_initializer (cp_parser* parser)
7526 vec<tree, va_gc> *expression_list;
7528 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7530 tree t;
7531 bool expr_non_constant_p;
7532 cp_lexer_set_source_position (parser->lexer);
7533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7534 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7535 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7536 expression_list = make_tree_vector_single (t);
7538 else
7539 expression_list = (cp_parser_parenthesized_expression_list
7540 (parser, non_attr, /*cast_p=*/false,
7541 /*allow_expansion_p=*/true,
7542 /*non_constant_p=*/NULL));
7544 return expression_list;
7547 /* Parse a delete-expression.
7549 delete-expression:
7550 :: [opt] delete cast-expression
7551 :: [opt] delete [ ] cast-expression
7553 Returns a representation of the expression. */
7555 static tree
7556 cp_parser_delete_expression (cp_parser* parser)
7558 bool global_scope_p;
7559 bool array_p;
7560 tree expression;
7562 /* Look for the optional `::' operator. */
7563 global_scope_p
7564 = (cp_parser_global_scope_opt (parser,
7565 /*current_scope_valid_p=*/false)
7566 != NULL_TREE);
7567 /* Look for the `delete' keyword. */
7568 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7569 /* See if the array syntax is in use. */
7570 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7572 /* Consume the `[' token. */
7573 cp_lexer_consume_token (parser->lexer);
7574 /* Look for the `]' token. */
7575 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7576 /* Remember that this is the `[]' construct. */
7577 array_p = true;
7579 else
7580 array_p = false;
7582 /* Parse the cast-expression. */
7583 expression = cp_parser_simple_cast_expression (parser);
7585 /* A delete-expression may not appear in an integral constant
7586 expression. */
7587 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7588 return error_mark_node;
7590 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7591 tf_warning_or_error);
7594 /* Returns true if TOKEN may start a cast-expression and false
7595 otherwise. */
7597 static bool
7598 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7600 cp_token *token = cp_lexer_peek_token (parser->lexer);
7601 switch (token->type)
7603 case CPP_COMMA:
7604 case CPP_SEMICOLON:
7605 case CPP_QUERY:
7606 case CPP_COLON:
7607 case CPP_CLOSE_SQUARE:
7608 case CPP_CLOSE_PAREN:
7609 case CPP_CLOSE_BRACE:
7610 case CPP_OPEN_BRACE:
7611 case CPP_DOT:
7612 case CPP_DOT_STAR:
7613 case CPP_DEREF:
7614 case CPP_DEREF_STAR:
7615 case CPP_DIV:
7616 case CPP_MOD:
7617 case CPP_LSHIFT:
7618 case CPP_RSHIFT:
7619 case CPP_LESS:
7620 case CPP_GREATER:
7621 case CPP_LESS_EQ:
7622 case CPP_GREATER_EQ:
7623 case CPP_EQ_EQ:
7624 case CPP_NOT_EQ:
7625 case CPP_EQ:
7626 case CPP_MULT_EQ:
7627 case CPP_DIV_EQ:
7628 case CPP_MOD_EQ:
7629 case CPP_PLUS_EQ:
7630 case CPP_MINUS_EQ:
7631 case CPP_RSHIFT_EQ:
7632 case CPP_LSHIFT_EQ:
7633 case CPP_AND_EQ:
7634 case CPP_XOR_EQ:
7635 case CPP_OR_EQ:
7636 case CPP_XOR:
7637 case CPP_OR:
7638 case CPP_OR_OR:
7639 case CPP_EOF:
7640 return false;
7642 case CPP_OPEN_PAREN:
7643 /* In ((type ()) () the last () isn't a valid cast-expression,
7644 so the whole must be parsed as postfix-expression. */
7645 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7646 != CPP_CLOSE_PAREN;
7648 /* '[' may start a primary-expression in obj-c++. */
7649 case CPP_OPEN_SQUARE:
7650 return c_dialect_objc ();
7652 default:
7653 return true;
7657 /* Parse a cast-expression.
7659 cast-expression:
7660 unary-expression
7661 ( type-id ) cast-expression
7663 ADDRESS_P is true iff the unary-expression is appearing as the
7664 operand of the `&' operator. CAST_P is true if this expression is
7665 the target of a cast.
7667 Returns a representation of the expression. */
7669 static tree
7670 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7671 bool decltype_p, cp_id_kind * pidk)
7673 /* If it's a `(', then we might be looking at a cast. */
7674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7676 tree type = NULL_TREE;
7677 tree expr = NULL_TREE;
7678 bool cast_expression_p;
7679 const char *saved_message;
7681 /* There's no way to know yet whether or not this is a cast.
7682 For example, `(int (3))' is a unary-expression, while `(int)
7683 3' is a cast. So, we resort to parsing tentatively. */
7684 cp_parser_parse_tentatively (parser);
7685 /* Types may not be defined in a cast. */
7686 saved_message = parser->type_definition_forbidden_message;
7687 parser->type_definition_forbidden_message
7688 = G_("types may not be defined in casts");
7689 /* Consume the `('. */
7690 cp_lexer_consume_token (parser->lexer);
7691 /* A very tricky bit is that `(struct S) { 3 }' is a
7692 compound-literal (which we permit in C++ as an extension).
7693 But, that construct is not a cast-expression -- it is a
7694 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7695 is legal; if the compound-literal were a cast-expression,
7696 you'd need an extra set of parentheses.) But, if we parse
7697 the type-id, and it happens to be a class-specifier, then we
7698 will commit to the parse at that point, because we cannot
7699 undo the action that is done when creating a new class. So,
7700 then we cannot back up and do a postfix-expression.
7701 Another tricky case is the following (c++/29234):
7703 struct S { void operator () (); };
7705 void foo ()
7707 ( S()() );
7710 As a type-id we parse the parenthesized S()() as a function
7711 returning a function, groktypename complains and we cannot
7712 back up in this case either.
7714 Therefore, we scan ahead to the closing `)', and check to see
7715 if the tokens after the `)' can start a cast-expression. Otherwise
7716 we are dealing with an unary-expression, a postfix-expression
7717 or something else.
7719 Save tokens so that we can put them back. */
7720 cp_lexer_save_tokens (parser->lexer);
7722 /* We may be looking at a cast-expression. */
7723 cast_expression_p
7724 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7725 /*consume_paren=*/true)
7726 && cp_parser_tokens_start_cast_expression (parser));
7728 /* Roll back the tokens we skipped. */
7729 cp_lexer_rollback_tokens (parser->lexer);
7730 /* If we aren't looking at a cast-expression, simulate an error so
7731 that the call to cp_parser_parse_definitely below will fail. */
7732 if (!cast_expression_p)
7733 cp_parser_simulate_error (parser);
7734 else
7736 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7737 parser->in_type_id_in_expr_p = true;
7738 /* Look for the type-id. */
7739 type = cp_parser_type_id (parser);
7740 /* Look for the closing `)'. */
7741 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7742 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7745 /* Restore the saved message. */
7746 parser->type_definition_forbidden_message = saved_message;
7748 /* At this point this can only be either a cast or a
7749 parenthesized ctor such as `(T ())' that looks like a cast to
7750 function returning T. */
7751 if (!cp_parser_error_occurred (parser))
7753 cp_parser_parse_definitely (parser);
7754 expr = cp_parser_cast_expression (parser,
7755 /*address_p=*/false,
7756 /*cast_p=*/true,
7757 /*decltype_p=*/false,
7758 pidk);
7760 /* Warn about old-style casts, if so requested. */
7761 if (warn_old_style_cast
7762 && !in_system_header_at (input_location)
7763 && !VOID_TYPE_P (type)
7764 && current_lang_name != lang_name_c)
7765 warning (OPT_Wold_style_cast, "use of old-style cast");
7767 /* Only type conversions to integral or enumeration types
7768 can be used in constant-expressions. */
7769 if (!cast_valid_in_integral_constant_expression_p (type)
7770 && cp_parser_non_integral_constant_expression (parser,
7771 NIC_CAST))
7772 return error_mark_node;
7774 /* Perform the cast. */
7775 expr = build_c_cast (input_location, type, expr);
7776 return expr;
7778 else
7779 cp_parser_abort_tentative_parse (parser);
7782 /* If we get here, then it's not a cast, so it must be a
7783 unary-expression. */
7784 return cp_parser_unary_expression (parser, address_p, cast_p,
7785 decltype_p, pidk);
7788 /* Parse a binary expression of the general form:
7790 pm-expression:
7791 cast-expression
7792 pm-expression .* cast-expression
7793 pm-expression ->* cast-expression
7795 multiplicative-expression:
7796 pm-expression
7797 multiplicative-expression * pm-expression
7798 multiplicative-expression / pm-expression
7799 multiplicative-expression % pm-expression
7801 additive-expression:
7802 multiplicative-expression
7803 additive-expression + multiplicative-expression
7804 additive-expression - multiplicative-expression
7806 shift-expression:
7807 additive-expression
7808 shift-expression << additive-expression
7809 shift-expression >> additive-expression
7811 relational-expression:
7812 shift-expression
7813 relational-expression < shift-expression
7814 relational-expression > shift-expression
7815 relational-expression <= shift-expression
7816 relational-expression >= shift-expression
7818 GNU Extension:
7820 relational-expression:
7821 relational-expression <? shift-expression
7822 relational-expression >? shift-expression
7824 equality-expression:
7825 relational-expression
7826 equality-expression == relational-expression
7827 equality-expression != relational-expression
7829 and-expression:
7830 equality-expression
7831 and-expression & equality-expression
7833 exclusive-or-expression:
7834 and-expression
7835 exclusive-or-expression ^ and-expression
7837 inclusive-or-expression:
7838 exclusive-or-expression
7839 inclusive-or-expression | exclusive-or-expression
7841 logical-and-expression:
7842 inclusive-or-expression
7843 logical-and-expression && inclusive-or-expression
7845 logical-or-expression:
7846 logical-and-expression
7847 logical-or-expression || logical-and-expression
7849 All these are implemented with a single function like:
7851 binary-expression:
7852 simple-cast-expression
7853 binary-expression <token> binary-expression
7855 CAST_P is true if this expression is the target of a cast.
7857 The binops_by_token map is used to get the tree codes for each <token> type.
7858 binary-expressions are associated according to a precedence table. */
7860 #define TOKEN_PRECEDENCE(token) \
7861 (((token->type == CPP_GREATER \
7862 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7863 && !parser->greater_than_is_operator_p) \
7864 ? PREC_NOT_OPERATOR \
7865 : binops_by_token[token->type].prec)
7867 static tree
7868 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7869 bool no_toplevel_fold_p,
7870 bool decltype_p,
7871 enum cp_parser_prec prec,
7872 cp_id_kind * pidk)
7874 cp_parser_expression_stack stack;
7875 cp_parser_expression_stack_entry *sp = &stack[0];
7876 cp_parser_expression_stack_entry current;
7877 tree rhs;
7878 cp_token *token;
7879 enum tree_code rhs_type;
7880 enum cp_parser_prec new_prec, lookahead_prec;
7881 tree overload;
7883 /* Parse the first expression. */
7884 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7885 cast_p, decltype_p, pidk);
7886 current.lhs_type = ERROR_MARK;
7887 current.prec = prec;
7889 if (cp_parser_error_occurred (parser))
7890 return error_mark_node;
7892 for (;;)
7894 /* Get an operator token. */
7895 token = cp_lexer_peek_token (parser->lexer);
7897 if (warn_cxx0x_compat
7898 && token->type == CPP_RSHIFT
7899 && !parser->greater_than_is_operator_p)
7901 if (warning_at (token->location, OPT_Wc__0x_compat,
7902 "%<>>%> operator is treated"
7903 " as two right angle brackets in C++11"))
7904 inform (token->location,
7905 "suggest parentheses around %<>>%> expression");
7908 new_prec = TOKEN_PRECEDENCE (token);
7910 /* Popping an entry off the stack means we completed a subexpression:
7911 - either we found a token which is not an operator (`>' where it is not
7912 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7913 will happen repeatedly;
7914 - or, we found an operator which has lower priority. This is the case
7915 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7916 parsing `3 * 4'. */
7917 if (new_prec <= current.prec)
7919 if (sp == stack)
7920 break;
7921 else
7922 goto pop;
7925 get_rhs:
7926 current.tree_type = binops_by_token[token->type].tree_type;
7927 current.loc = token->location;
7929 /* We used the operator token. */
7930 cp_lexer_consume_token (parser->lexer);
7932 /* For "false && x" or "true || x", x will never be executed;
7933 disable warnings while evaluating it. */
7934 if (current.tree_type == TRUTH_ANDIF_EXPR)
7935 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7936 else if (current.tree_type == TRUTH_ORIF_EXPR)
7937 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7939 /* Extract another operand. It may be the RHS of this expression
7940 or the LHS of a new, higher priority expression. */
7941 rhs = cp_parser_simple_cast_expression (parser);
7942 rhs_type = ERROR_MARK;
7944 /* Get another operator token. Look up its precedence to avoid
7945 building a useless (immediately popped) stack entry for common
7946 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7947 token = cp_lexer_peek_token (parser->lexer);
7948 lookahead_prec = TOKEN_PRECEDENCE (token);
7949 if (lookahead_prec > new_prec)
7951 /* ... and prepare to parse the RHS of the new, higher priority
7952 expression. Since precedence levels on the stack are
7953 monotonically increasing, we do not have to care about
7954 stack overflows. */
7955 *sp = current;
7956 ++sp;
7957 current.lhs = rhs;
7958 current.lhs_type = rhs_type;
7959 current.prec = new_prec;
7960 new_prec = lookahead_prec;
7961 goto get_rhs;
7963 pop:
7964 lookahead_prec = new_prec;
7965 /* If the stack is not empty, we have parsed into LHS the right side
7966 (`4' in the example above) of an expression we had suspended.
7967 We can use the information on the stack to recover the LHS (`3')
7968 from the stack together with the tree code (`MULT_EXPR'), and
7969 the precedence of the higher level subexpression
7970 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7971 which will be used to actually build the additive expression. */
7972 rhs = current.lhs;
7973 rhs_type = current.lhs_type;
7974 --sp;
7975 current = *sp;
7978 /* Undo the disabling of warnings done above. */
7979 if (current.tree_type == TRUTH_ANDIF_EXPR)
7980 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7981 else if (current.tree_type == TRUTH_ORIF_EXPR)
7982 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7984 overload = NULL;
7985 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7986 ERROR_MARK for everything that is not a binary expression.
7987 This makes warn_about_parentheses miss some warnings that
7988 involve unary operators. For unary expressions we should
7989 pass the correct tree_code unless the unary expression was
7990 surrounded by parentheses.
7992 if (no_toplevel_fold_p
7993 && lookahead_prec <= current.prec
7994 && sp == stack)
7995 current.lhs = build2 (current.tree_type,
7996 TREE_CODE_CLASS (current.tree_type)
7997 == tcc_comparison
7998 ? boolean_type_node : TREE_TYPE (current.lhs),
7999 current.lhs, rhs);
8000 else
8001 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8002 current.lhs, current.lhs_type,
8003 rhs, rhs_type, &overload,
8004 complain_flags (decltype_p));
8005 current.lhs_type = current.tree_type;
8006 if (EXPR_P (current.lhs))
8007 SET_EXPR_LOCATION (current.lhs, current.loc);
8009 /* If the binary operator required the use of an overloaded operator,
8010 then this expression cannot be an integral constant-expression.
8011 An overloaded operator can be used even if both operands are
8012 otherwise permissible in an integral constant-expression if at
8013 least one of the operands is of enumeration type. */
8015 if (overload
8016 && cp_parser_non_integral_constant_expression (parser,
8017 NIC_OVERLOADED))
8018 return error_mark_node;
8021 return current.lhs;
8024 static tree
8025 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8026 bool no_toplevel_fold_p,
8027 enum cp_parser_prec prec,
8028 cp_id_kind * pidk)
8030 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8031 /*decltype*/false, prec, pidk);
8034 /* Parse the `? expression : assignment-expression' part of a
8035 conditional-expression. The LOGICAL_OR_EXPR is the
8036 logical-or-expression that started the conditional-expression.
8037 Returns a representation of the entire conditional-expression.
8039 This routine is used by cp_parser_assignment_expression.
8041 ? expression : assignment-expression
8043 GNU Extensions:
8045 ? : assignment-expression */
8047 static tree
8048 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8050 tree expr;
8051 tree assignment_expr;
8052 struct cp_token *token;
8053 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8055 /* Consume the `?' token. */
8056 cp_lexer_consume_token (parser->lexer);
8057 token = cp_lexer_peek_token (parser->lexer);
8058 if (cp_parser_allow_gnu_extensions_p (parser)
8059 && token->type == CPP_COLON)
8061 pedwarn (token->location, OPT_Wpedantic,
8062 "ISO C++ does not allow ?: with omitted middle operand");
8063 /* Implicit true clause. */
8064 expr = NULL_TREE;
8065 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8066 warn_for_omitted_condop (token->location, logical_or_expr);
8068 else
8070 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8071 parser->colon_corrects_to_scope_p = false;
8072 /* Parse the expression. */
8073 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8074 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8075 c_inhibit_evaluation_warnings +=
8076 ((logical_or_expr == truthvalue_true_node)
8077 - (logical_or_expr == truthvalue_false_node));
8078 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8081 /* The next token should be a `:'. */
8082 cp_parser_require (parser, CPP_COLON, RT_COLON);
8083 /* Parse the assignment-expression. */
8084 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8085 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8087 /* Build the conditional-expression. */
8088 return build_x_conditional_expr (loc, logical_or_expr,
8089 expr,
8090 assignment_expr,
8091 tf_warning_or_error);
8094 /* Parse an assignment-expression.
8096 assignment-expression:
8097 conditional-expression
8098 logical-or-expression assignment-operator assignment_expression
8099 throw-expression
8101 CAST_P is true if this expression is the target of a cast.
8102 DECLTYPE_P is true if this expression is the operand of decltype.
8104 Returns a representation for the expression. */
8106 static tree
8107 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8108 bool decltype_p, cp_id_kind * pidk)
8110 tree expr;
8112 /* If the next token is the `throw' keyword, then we're looking at
8113 a throw-expression. */
8114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8115 expr = cp_parser_throw_expression (parser);
8116 /* Otherwise, it must be that we are looking at a
8117 logical-or-expression. */
8118 else
8120 /* Parse the binary expressions (logical-or-expression). */
8121 expr = cp_parser_binary_expression (parser, cast_p, false,
8122 decltype_p,
8123 PREC_NOT_OPERATOR, pidk);
8124 /* If the next token is a `?' then we're actually looking at a
8125 conditional-expression. */
8126 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8127 return cp_parser_question_colon_clause (parser, expr);
8128 else
8130 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8132 /* If it's an assignment-operator, we're using the second
8133 production. */
8134 enum tree_code assignment_operator
8135 = cp_parser_assignment_operator_opt (parser);
8136 if (assignment_operator != ERROR_MARK)
8138 bool non_constant_p;
8139 location_t saved_input_location;
8141 /* Parse the right-hand side of the assignment. */
8142 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8144 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8145 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8147 /* An assignment may not appear in a
8148 constant-expression. */
8149 if (cp_parser_non_integral_constant_expression (parser,
8150 NIC_ASSIGNMENT))
8151 return error_mark_node;
8152 /* Build the assignment expression. Its default
8153 location is the location of the '=' token. */
8154 saved_input_location = input_location;
8155 input_location = loc;
8156 expr = build_x_modify_expr (loc, expr,
8157 assignment_operator,
8158 rhs,
8159 complain_flags (decltype_p));
8160 input_location = saved_input_location;
8165 return expr;
8168 static tree
8169 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8170 cp_id_kind * pidk)
8172 return cp_parser_assignment_expression (parser, cast_p,
8173 /*decltype*/false, pidk);
8176 /* Parse an (optional) assignment-operator.
8178 assignment-operator: one of
8179 = *= /= %= += -= >>= <<= &= ^= |=
8181 GNU Extension:
8183 assignment-operator: one of
8184 <?= >?=
8186 If the next token is an assignment operator, the corresponding tree
8187 code is returned, and the token is consumed. For example, for
8188 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8189 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8190 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8191 operator, ERROR_MARK is returned. */
8193 static enum tree_code
8194 cp_parser_assignment_operator_opt (cp_parser* parser)
8196 enum tree_code op;
8197 cp_token *token;
8199 /* Peek at the next token. */
8200 token = cp_lexer_peek_token (parser->lexer);
8202 switch (token->type)
8204 case CPP_EQ:
8205 op = NOP_EXPR;
8206 break;
8208 case CPP_MULT_EQ:
8209 op = MULT_EXPR;
8210 break;
8212 case CPP_DIV_EQ:
8213 op = TRUNC_DIV_EXPR;
8214 break;
8216 case CPP_MOD_EQ:
8217 op = TRUNC_MOD_EXPR;
8218 break;
8220 case CPP_PLUS_EQ:
8221 op = PLUS_EXPR;
8222 break;
8224 case CPP_MINUS_EQ:
8225 op = MINUS_EXPR;
8226 break;
8228 case CPP_RSHIFT_EQ:
8229 op = RSHIFT_EXPR;
8230 break;
8232 case CPP_LSHIFT_EQ:
8233 op = LSHIFT_EXPR;
8234 break;
8236 case CPP_AND_EQ:
8237 op = BIT_AND_EXPR;
8238 break;
8240 case CPP_XOR_EQ:
8241 op = BIT_XOR_EXPR;
8242 break;
8244 case CPP_OR_EQ:
8245 op = BIT_IOR_EXPR;
8246 break;
8248 default:
8249 /* Nothing else is an assignment operator. */
8250 op = ERROR_MARK;
8253 /* If it was an assignment operator, consume it. */
8254 if (op != ERROR_MARK)
8255 cp_lexer_consume_token (parser->lexer);
8257 return op;
8260 /* Parse an expression.
8262 expression:
8263 assignment-expression
8264 expression , assignment-expression
8266 CAST_P is true if this expression is the target of a cast.
8267 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8268 except possibly parenthesized or on the RHS of a comma (N3276).
8270 Returns a representation of the expression. */
8272 static tree
8273 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8274 cp_id_kind * pidk)
8276 tree expression = NULL_TREE;
8277 location_t loc = UNKNOWN_LOCATION;
8279 while (true)
8281 tree assignment_expression;
8283 /* Parse the next assignment-expression. */
8284 assignment_expression
8285 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8287 /* We don't create a temporary for a call that is the immediate operand
8288 of decltype or on the RHS of a comma. But when we see a comma, we
8289 need to create a temporary for a call on the LHS. */
8290 if (decltype_p && !processing_template_decl
8291 && TREE_CODE (assignment_expression) == CALL_EXPR
8292 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8293 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8294 assignment_expression
8295 = build_cplus_new (TREE_TYPE (assignment_expression),
8296 assignment_expression, tf_warning_or_error);
8298 /* If this is the first assignment-expression, we can just
8299 save it away. */
8300 if (!expression)
8301 expression = assignment_expression;
8302 else
8303 expression = build_x_compound_expr (loc, expression,
8304 assignment_expression,
8305 complain_flags (decltype_p));
8306 /* If the next token is not a comma, then we are done with the
8307 expression. */
8308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8309 break;
8310 /* Consume the `,'. */
8311 loc = cp_lexer_peek_token (parser->lexer)->location;
8312 cp_lexer_consume_token (parser->lexer);
8313 /* A comma operator cannot appear in a constant-expression. */
8314 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8315 expression = error_mark_node;
8318 return expression;
8321 static inline tree
8322 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8324 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8327 /* Parse a constant-expression.
8329 constant-expression:
8330 conditional-expression
8332 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8333 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8334 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8335 is false, NON_CONSTANT_P should be NULL. */
8337 static tree
8338 cp_parser_constant_expression (cp_parser* parser,
8339 bool allow_non_constant_p,
8340 bool *non_constant_p)
8342 bool saved_integral_constant_expression_p;
8343 bool saved_allow_non_integral_constant_expression_p;
8344 bool saved_non_integral_constant_expression_p;
8345 tree expression;
8347 /* It might seem that we could simply parse the
8348 conditional-expression, and then check to see if it were
8349 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8350 one that the compiler can figure out is constant, possibly after
8351 doing some simplifications or optimizations. The standard has a
8352 precise definition of constant-expression, and we must honor
8353 that, even though it is somewhat more restrictive.
8355 For example:
8357 int i[(2, 3)];
8359 is not a legal declaration, because `(2, 3)' is not a
8360 constant-expression. The `,' operator is forbidden in a
8361 constant-expression. However, GCC's constant-folding machinery
8362 will fold this operation to an INTEGER_CST for `3'. */
8364 /* Save the old settings. */
8365 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8366 saved_allow_non_integral_constant_expression_p
8367 = parser->allow_non_integral_constant_expression_p;
8368 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8369 /* We are now parsing a constant-expression. */
8370 parser->integral_constant_expression_p = true;
8371 parser->allow_non_integral_constant_expression_p
8372 = (allow_non_constant_p || cxx_dialect >= cxx11);
8373 parser->non_integral_constant_expression_p = false;
8374 /* Although the grammar says "conditional-expression", we parse an
8375 "assignment-expression", which also permits "throw-expression"
8376 and the use of assignment operators. In the case that
8377 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8378 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8379 actually essential that we look for an assignment-expression.
8380 For example, cp_parser_initializer_clauses uses this function to
8381 determine whether a particular assignment-expression is in fact
8382 constant. */
8383 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8384 /* Restore the old settings. */
8385 parser->integral_constant_expression_p
8386 = saved_integral_constant_expression_p;
8387 parser->allow_non_integral_constant_expression_p
8388 = saved_allow_non_integral_constant_expression_p;
8389 if (cxx_dialect >= cxx11)
8391 /* Require an rvalue constant expression here; that's what our
8392 callers expect. Reference constant expressions are handled
8393 separately in e.g. cp_parser_template_argument. */
8394 bool is_const = potential_rvalue_constant_expression (expression);
8395 parser->non_integral_constant_expression_p = !is_const;
8396 if (!is_const && !allow_non_constant_p)
8397 require_potential_rvalue_constant_expression (expression);
8399 if (allow_non_constant_p)
8400 *non_constant_p = parser->non_integral_constant_expression_p;
8401 parser->non_integral_constant_expression_p
8402 = saved_non_integral_constant_expression_p;
8404 return expression;
8407 /* Parse __builtin_offsetof.
8409 offsetof-expression:
8410 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8412 offsetof-member-designator:
8413 id-expression
8414 | offsetof-member-designator "." id-expression
8415 | offsetof-member-designator "[" expression "]"
8416 | offsetof-member-designator "->" id-expression */
8418 static tree
8419 cp_parser_builtin_offsetof (cp_parser *parser)
8421 int save_ice_p, save_non_ice_p;
8422 tree type, expr;
8423 cp_id_kind dummy;
8424 cp_token *token;
8426 /* We're about to accept non-integral-constant things, but will
8427 definitely yield an integral constant expression. Save and
8428 restore these values around our local parsing. */
8429 save_ice_p = parser->integral_constant_expression_p;
8430 save_non_ice_p = parser->non_integral_constant_expression_p;
8432 /* Consume the "__builtin_offsetof" token. */
8433 cp_lexer_consume_token (parser->lexer);
8434 /* Consume the opening `('. */
8435 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8436 /* Parse the type-id. */
8437 type = cp_parser_type_id (parser);
8438 /* Look for the `,'. */
8439 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8440 token = cp_lexer_peek_token (parser->lexer);
8442 /* Build the (type *)null that begins the traditional offsetof macro. */
8443 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8444 tf_warning_or_error);
8446 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8447 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8448 true, &dummy, token->location);
8449 while (true)
8451 token = cp_lexer_peek_token (parser->lexer);
8452 switch (token->type)
8454 case CPP_OPEN_SQUARE:
8455 /* offsetof-member-designator "[" expression "]" */
8456 expr = cp_parser_postfix_open_square_expression (parser, expr,
8457 true, false);
8458 break;
8460 case CPP_DEREF:
8461 /* offsetof-member-designator "->" identifier */
8462 expr = grok_array_decl (token->location, expr,
8463 integer_zero_node, false);
8464 /* FALLTHRU */
8466 case CPP_DOT:
8467 /* offsetof-member-designator "." identifier */
8468 cp_lexer_consume_token (parser->lexer);
8469 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8470 expr, true, &dummy,
8471 token->location);
8472 break;
8474 case CPP_CLOSE_PAREN:
8475 /* Consume the ")" token. */
8476 cp_lexer_consume_token (parser->lexer);
8477 goto success;
8479 default:
8480 /* Error. We know the following require will fail, but
8481 that gives the proper error message. */
8482 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8483 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8484 expr = error_mark_node;
8485 goto failure;
8489 success:
8490 /* If we're processing a template, we can't finish the semantics yet.
8491 Otherwise we can fold the entire expression now. */
8492 if (processing_template_decl)
8493 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8494 else
8495 expr = finish_offsetof (expr);
8497 failure:
8498 parser->integral_constant_expression_p = save_ice_p;
8499 parser->non_integral_constant_expression_p = save_non_ice_p;
8501 return expr;
8504 /* Parse a trait expression.
8506 Returns a representation of the expression, the underlying type
8507 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8509 static tree
8510 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8512 cp_trait_kind kind;
8513 tree type1, type2 = NULL_TREE;
8514 bool binary = false;
8515 cp_decl_specifier_seq decl_specs;
8517 switch (keyword)
8519 case RID_HAS_NOTHROW_ASSIGN:
8520 kind = CPTK_HAS_NOTHROW_ASSIGN;
8521 break;
8522 case RID_HAS_NOTHROW_CONSTRUCTOR:
8523 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8524 break;
8525 case RID_HAS_NOTHROW_COPY:
8526 kind = CPTK_HAS_NOTHROW_COPY;
8527 break;
8528 case RID_HAS_TRIVIAL_ASSIGN:
8529 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8530 break;
8531 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8532 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8533 break;
8534 case RID_HAS_TRIVIAL_COPY:
8535 kind = CPTK_HAS_TRIVIAL_COPY;
8536 break;
8537 case RID_HAS_TRIVIAL_DESTRUCTOR:
8538 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8539 break;
8540 case RID_HAS_VIRTUAL_DESTRUCTOR:
8541 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8542 break;
8543 case RID_IS_ABSTRACT:
8544 kind = CPTK_IS_ABSTRACT;
8545 break;
8546 case RID_IS_BASE_OF:
8547 kind = CPTK_IS_BASE_OF;
8548 binary = true;
8549 break;
8550 case RID_IS_CLASS:
8551 kind = CPTK_IS_CLASS;
8552 break;
8553 case RID_IS_CONVERTIBLE_TO:
8554 kind = CPTK_IS_CONVERTIBLE_TO;
8555 binary = true;
8556 break;
8557 case RID_IS_EMPTY:
8558 kind = CPTK_IS_EMPTY;
8559 break;
8560 case RID_IS_ENUM:
8561 kind = CPTK_IS_ENUM;
8562 break;
8563 case RID_IS_FINAL:
8564 kind = CPTK_IS_FINAL;
8565 break;
8566 case RID_IS_LITERAL_TYPE:
8567 kind = CPTK_IS_LITERAL_TYPE;
8568 break;
8569 case RID_IS_POD:
8570 kind = CPTK_IS_POD;
8571 break;
8572 case RID_IS_POLYMORPHIC:
8573 kind = CPTK_IS_POLYMORPHIC;
8574 break;
8575 case RID_IS_STD_LAYOUT:
8576 kind = CPTK_IS_STD_LAYOUT;
8577 break;
8578 case RID_IS_TRIVIAL:
8579 kind = CPTK_IS_TRIVIAL;
8580 break;
8581 case RID_IS_UNION:
8582 kind = CPTK_IS_UNION;
8583 break;
8584 case RID_UNDERLYING_TYPE:
8585 kind = CPTK_UNDERLYING_TYPE;
8586 break;
8587 case RID_BASES:
8588 kind = CPTK_BASES;
8589 break;
8590 case RID_DIRECT_BASES:
8591 kind = CPTK_DIRECT_BASES;
8592 break;
8593 default:
8594 gcc_unreachable ();
8597 /* Consume the token. */
8598 cp_lexer_consume_token (parser->lexer);
8600 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8602 type1 = cp_parser_type_id (parser);
8604 if (type1 == error_mark_node)
8605 return error_mark_node;
8607 /* Build a trivial decl-specifier-seq. */
8608 clear_decl_specs (&decl_specs);
8609 decl_specs.type = type1;
8611 /* Call grokdeclarator to figure out what type this is. */
8612 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8613 /*initialized=*/0, /*attrlist=*/NULL);
8615 if (binary)
8617 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8619 type2 = cp_parser_type_id (parser);
8621 if (type2 == error_mark_node)
8622 return error_mark_node;
8624 /* Build a trivial decl-specifier-seq. */
8625 clear_decl_specs (&decl_specs);
8626 decl_specs.type = type2;
8628 /* Call grokdeclarator to figure out what type this is. */
8629 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8630 /*initialized=*/0, /*attrlist=*/NULL);
8633 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8635 /* Complete the trait expression, which may mean either processing
8636 the trait expr now or saving it for template instantiation. */
8637 switch(kind)
8639 case CPTK_UNDERLYING_TYPE:
8640 return finish_underlying_type (type1);
8641 case CPTK_BASES:
8642 return finish_bases (type1, false);
8643 case CPTK_DIRECT_BASES:
8644 return finish_bases (type1, true);
8645 default:
8646 return finish_trait_expr (kind, type1, type2);
8650 /* Lambdas that appear in variable initializer or default argument scope
8651 get that in their mangling, so we need to record it. We might as well
8652 use the count for function and namespace scopes as well. */
8653 static GTY(()) tree lambda_scope;
8654 static GTY(()) int lambda_count;
8655 typedef struct GTY(()) tree_int
8657 tree t;
8658 int i;
8659 } tree_int;
8660 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8662 static void
8663 start_lambda_scope (tree decl)
8665 tree_int ti;
8666 gcc_assert (decl);
8667 /* Once we're inside a function, we ignore other scopes and just push
8668 the function again so that popping works properly. */
8669 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8670 decl = current_function_decl;
8671 ti.t = lambda_scope;
8672 ti.i = lambda_count;
8673 vec_safe_push (lambda_scope_stack, ti);
8674 if (lambda_scope != decl)
8676 /* Don't reset the count if we're still in the same function. */
8677 lambda_scope = decl;
8678 lambda_count = 0;
8682 static void
8683 record_lambda_scope (tree lambda)
8685 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8686 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8689 static void
8690 finish_lambda_scope (void)
8692 tree_int *p = &lambda_scope_stack->last ();
8693 if (lambda_scope != p->t)
8695 lambda_scope = p->t;
8696 lambda_count = p->i;
8698 lambda_scope_stack->pop ();
8701 /* Parse a lambda expression.
8703 lambda-expression:
8704 lambda-introducer lambda-declarator [opt] compound-statement
8706 Returns a representation of the expression. */
8708 static tree
8709 cp_parser_lambda_expression (cp_parser* parser)
8711 tree lambda_expr = build_lambda_expr ();
8712 tree type;
8713 bool ok;
8715 LAMBDA_EXPR_LOCATION (lambda_expr)
8716 = cp_lexer_peek_token (parser->lexer)->location;
8718 if (cp_unevaluated_operand)
8719 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8720 "lambda-expression in unevaluated context");
8722 /* We may be in the middle of deferred access check. Disable
8723 it now. */
8724 push_deferring_access_checks (dk_no_deferred);
8726 cp_parser_lambda_introducer (parser, lambda_expr);
8728 type = begin_lambda_type (lambda_expr);
8729 if (type == error_mark_node)
8730 return error_mark_node;
8732 record_lambda_scope (lambda_expr);
8734 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8735 determine_visibility (TYPE_NAME (type));
8737 /* Now that we've started the type, add the capture fields for any
8738 explicit captures. */
8739 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8742 /* Inside the class, surrounding template-parameter-lists do not apply. */
8743 unsigned int saved_num_template_parameter_lists
8744 = parser->num_template_parameter_lists;
8745 unsigned char in_statement = parser->in_statement;
8746 bool in_switch_statement_p = parser->in_switch_statement_p;
8747 bool fully_implicit_function_template_p
8748 = parser->fully_implicit_function_template_p;
8749 tree implicit_template_parms = parser->implicit_template_parms;
8750 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8751 bool auto_is_implicit_function_template_parm_p
8752 = parser->auto_is_implicit_function_template_parm_p;
8754 parser->num_template_parameter_lists = 0;
8755 parser->in_statement = 0;
8756 parser->in_switch_statement_p = false;
8757 parser->fully_implicit_function_template_p = false;
8758 parser->implicit_template_parms = 0;
8759 parser->implicit_template_scope = 0;
8760 parser->auto_is_implicit_function_template_parm_p = false;
8762 /* By virtue of defining a local class, a lambda expression has access to
8763 the private variables of enclosing classes. */
8765 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8767 if (ok)
8768 cp_parser_lambda_body (parser, lambda_expr);
8769 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8770 cp_parser_skip_to_end_of_block_or_statement (parser);
8772 /* The capture list was built up in reverse order; fix that now. */
8773 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8774 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8776 if (ok)
8777 maybe_add_lambda_conv_op (type);
8779 type = finish_struct (type, /*attributes=*/NULL_TREE);
8781 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8782 parser->in_statement = in_statement;
8783 parser->in_switch_statement_p = in_switch_statement_p;
8784 parser->fully_implicit_function_template_p
8785 = fully_implicit_function_template_p;
8786 parser->implicit_template_parms = implicit_template_parms;
8787 parser->implicit_template_scope = implicit_template_scope;
8788 parser->auto_is_implicit_function_template_parm_p
8789 = auto_is_implicit_function_template_parm_p;
8792 pop_deferring_access_checks ();
8794 /* This field is only used during parsing of the lambda. */
8795 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8797 /* This lambda shouldn't have any proxies left at this point. */
8798 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8799 /* And now that we're done, push proxies for an enclosing lambda. */
8800 insert_pending_capture_proxies ();
8802 if (ok)
8803 return build_lambda_object (lambda_expr);
8804 else
8805 return error_mark_node;
8808 /* Parse the beginning of a lambda expression.
8810 lambda-introducer:
8811 [ lambda-capture [opt] ]
8813 LAMBDA_EXPR is the current representation of the lambda expression. */
8815 static void
8816 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8818 /* Need commas after the first capture. */
8819 bool first = true;
8821 /* Eat the leading `['. */
8822 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8824 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8825 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8826 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8827 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8828 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8829 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8831 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8833 cp_lexer_consume_token (parser->lexer);
8834 first = false;
8837 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8839 cp_token* capture_token;
8840 tree capture_id;
8841 tree capture_init_expr;
8842 cp_id_kind idk = CP_ID_KIND_NONE;
8843 bool explicit_init_p = false;
8845 enum capture_kind_type
8847 BY_COPY,
8848 BY_REFERENCE
8850 enum capture_kind_type capture_kind = BY_COPY;
8852 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8854 error ("expected end of capture-list");
8855 return;
8858 if (first)
8859 first = false;
8860 else
8861 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8863 /* Possibly capture `this'. */
8864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8866 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8867 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8868 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8869 "with by-copy capture default");
8870 cp_lexer_consume_token (parser->lexer);
8871 add_capture (lambda_expr,
8872 /*id=*/this_identifier,
8873 /*initializer=*/finish_this_expr(),
8874 /*by_reference_p=*/false,
8875 explicit_init_p);
8876 continue;
8879 /* Remember whether we want to capture as a reference or not. */
8880 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8882 capture_kind = BY_REFERENCE;
8883 cp_lexer_consume_token (parser->lexer);
8886 /* Get the identifier. */
8887 capture_token = cp_lexer_peek_token (parser->lexer);
8888 capture_id = cp_parser_identifier (parser);
8890 if (capture_id == error_mark_node)
8891 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8892 delimiters, but I modified this to stop on unnested ']' as well. It
8893 was already changed to stop on unnested '}', so the
8894 "closing_parenthesis" name is no more misleading with my change. */
8896 cp_parser_skip_to_closing_parenthesis (parser,
8897 /*recovering=*/true,
8898 /*or_comma=*/true,
8899 /*consume_paren=*/true);
8900 break;
8903 /* Find the initializer for this capture. */
8904 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8905 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8906 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8908 bool direct, non_constant;
8909 /* An explicit initializer exists. */
8910 if (cxx_dialect < cxx1y)
8911 pedwarn (input_location, 0,
8912 "lambda capture initializers "
8913 "only available with -std=c++1y or -std=gnu++1y");
8914 capture_init_expr = cp_parser_initializer (parser, &direct,
8915 &non_constant);
8916 explicit_init_p = true;
8917 if (capture_init_expr == NULL_TREE)
8919 error ("empty initializer for lambda init-capture");
8920 capture_init_expr = error_mark_node;
8923 else
8925 const char* error_msg;
8927 /* Turn the identifier into an id-expression. */
8928 capture_init_expr
8929 = cp_parser_lookup_name_simple (parser, capture_id,
8930 capture_token->location);
8932 if (capture_init_expr == error_mark_node)
8934 unqualified_name_lookup_error (capture_id);
8935 continue;
8937 else if (DECL_P (capture_init_expr)
8938 && (!VAR_P (capture_init_expr)
8939 && TREE_CODE (capture_init_expr) != PARM_DECL))
8941 error_at (capture_token->location,
8942 "capture of non-variable %qD ",
8943 capture_init_expr);
8944 inform (0, "%q+#D declared here", capture_init_expr);
8945 continue;
8947 if (VAR_P (capture_init_expr)
8948 && decl_storage_duration (capture_init_expr) != dk_auto)
8950 pedwarn (capture_token->location, 0, "capture of variable "
8951 "%qD with non-automatic storage duration",
8952 capture_init_expr);
8953 inform (0, "%q+#D declared here", capture_init_expr);
8954 continue;
8957 capture_init_expr
8958 = finish_id_expression
8959 (capture_id,
8960 capture_init_expr,
8961 parser->scope,
8962 &idk,
8963 /*integral_constant_expression_p=*/false,
8964 /*allow_non_integral_constant_expression_p=*/false,
8965 /*non_integral_constant_expression_p=*/NULL,
8966 /*template_p=*/false,
8967 /*done=*/true,
8968 /*address_p=*/false,
8969 /*template_arg_p=*/false,
8970 &error_msg,
8971 capture_token->location);
8973 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8975 cp_lexer_consume_token (parser->lexer);
8976 capture_init_expr = make_pack_expansion (capture_init_expr);
8978 else
8979 check_for_bare_parameter_packs (capture_init_expr);
8982 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8983 && !explicit_init_p)
8985 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8986 && capture_kind == BY_COPY)
8987 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8988 "of %qD redundant with by-copy capture default",
8989 capture_id);
8990 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8991 && capture_kind == BY_REFERENCE)
8992 pedwarn (capture_token->location, 0, "explicit by-reference "
8993 "capture of %qD redundant with by-reference capture "
8994 "default", capture_id);
8997 add_capture (lambda_expr,
8998 capture_id,
8999 capture_init_expr,
9000 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9001 explicit_init_p);
9004 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9007 /* Parse the (optional) middle of a lambda expression.
9009 lambda-declarator:
9010 < template-parameter-list [opt] >
9011 ( parameter-declaration-clause [opt] )
9012 attribute-specifier [opt]
9013 mutable [opt]
9014 exception-specification [opt]
9015 lambda-return-type-clause [opt]
9017 LAMBDA_EXPR is the current representation of the lambda expression. */
9019 static bool
9020 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9022 /* 5.1.1.4 of the standard says:
9023 If a lambda-expression does not include a lambda-declarator, it is as if
9024 the lambda-declarator were ().
9025 This means an empty parameter list, no attributes, and no exception
9026 specification. */
9027 tree param_list = void_list_node;
9028 tree attributes = NULL_TREE;
9029 tree exception_spec = NULL_TREE;
9030 tree template_param_list = NULL_TREE;
9032 /* The template-parameter-list is optional, but must begin with
9033 an opening angle if present. */
9034 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9036 if (cxx_dialect < cxx1y)
9037 pedwarn (parser->lexer->next_token->location, 0,
9038 "lambda templates are only available with "
9039 "-std=c++1y or -std=gnu++1y");
9041 cp_lexer_consume_token (parser->lexer);
9043 template_param_list = cp_parser_template_parameter_list (parser);
9045 cp_parser_skip_to_end_of_template_parameter_list (parser);
9047 /* We just processed one more parameter list. */
9048 ++parser->num_template_parameter_lists;
9051 /* The parameter-declaration-clause is optional (unless
9052 template-parameter-list was given), but must begin with an
9053 opening parenthesis if present. */
9054 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9056 cp_lexer_consume_token (parser->lexer);
9058 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9060 /* Parse parameters. */
9061 param_list = cp_parser_parameter_declaration_clause (parser);
9063 /* Default arguments shall not be specified in the
9064 parameter-declaration-clause of a lambda-declarator. */
9065 for (tree t = param_list; t; t = TREE_CHAIN (t))
9066 if (TREE_PURPOSE (t))
9067 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9068 "default argument specified for lambda parameter");
9070 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9072 attributes = cp_parser_attributes_opt (parser);
9074 /* Parse optional `mutable' keyword. */
9075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9077 cp_lexer_consume_token (parser->lexer);
9078 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9081 /* Parse optional exception specification. */
9082 exception_spec = cp_parser_exception_specification_opt (parser);
9084 /* Parse optional trailing return type. */
9085 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9087 cp_lexer_consume_token (parser->lexer);
9088 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9089 = cp_parser_trailing_type_id (parser);
9092 /* The function parameters must be in scope all the way until after the
9093 trailing-return-type in case of decltype. */
9094 pop_bindings_and_leave_scope ();
9096 else if (template_param_list != NULL_TREE) // generate diagnostic
9097 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9099 /* Create the function call operator.
9101 Messing with declarators like this is no uglier than building up the
9102 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9103 other code. */
9105 cp_decl_specifier_seq return_type_specs;
9106 cp_declarator* declarator;
9107 tree fco;
9108 int quals;
9109 void *p;
9111 clear_decl_specs (&return_type_specs);
9112 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9113 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9114 else
9115 /* Maybe we will deduce the return type later. */
9116 return_type_specs.type = make_auto ();
9118 p = obstack_alloc (&declarator_obstack, 0);
9120 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9121 sfk_none);
9123 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9124 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9125 declarator = make_call_declarator (declarator, param_list, quals,
9126 VIRT_SPEC_UNSPECIFIED,
9127 REF_QUAL_NONE,
9128 exception_spec,
9129 /*late_return_type=*/NULL_TREE);
9130 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9132 fco = grokmethod (&return_type_specs,
9133 declarator,
9134 attributes);
9135 if (fco != error_mark_node)
9137 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9138 DECL_ARTIFICIAL (fco) = 1;
9139 /* Give the object parameter a different name. */
9140 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9142 if (template_param_list)
9144 fco = finish_member_template_decl (fco);
9145 finish_template_decl (template_param_list);
9146 --parser->num_template_parameter_lists;
9148 else if (parser->fully_implicit_function_template_p)
9149 fco = finish_fully_implicit_template (parser, fco);
9151 finish_member_declaration (fco);
9153 obstack_free (&declarator_obstack, p);
9155 return (fco != error_mark_node);
9159 /* Parse the body of a lambda expression, which is simply
9161 compound-statement
9163 but which requires special handling.
9164 LAMBDA_EXPR is the current representation of the lambda expression. */
9166 static void
9167 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9169 bool nested = (current_function_decl != NULL_TREE);
9170 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9171 if (nested)
9172 push_function_context ();
9173 else
9174 /* Still increment function_depth so that we don't GC in the
9175 middle of an expression. */
9176 ++function_depth;
9177 /* Clear this in case we're in the middle of a default argument. */
9178 parser->local_variables_forbidden_p = false;
9180 /* Finish the function call operator
9181 - class_specifier
9182 + late_parsing_for_member
9183 + function_definition_after_declarator
9184 + ctor_initializer_opt_and_function_body */
9186 tree fco = lambda_function (lambda_expr);
9187 tree body;
9188 bool done = false;
9189 tree compound_stmt;
9190 tree cap;
9192 /* Let the front end know that we are going to be defining this
9193 function. */
9194 start_preparsed_function (fco,
9195 NULL_TREE,
9196 SF_PRE_PARSED | SF_INCLASS_INLINE);
9198 start_lambda_scope (fco);
9199 body = begin_function_body ();
9201 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9202 goto out;
9204 /* Push the proxies for any explicit captures. */
9205 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9206 cap = TREE_CHAIN (cap))
9207 build_capture_proxy (TREE_PURPOSE (cap));
9209 compound_stmt = begin_compound_stmt (0);
9211 /* 5.1.1.4 of the standard says:
9212 If a lambda-expression does not include a trailing-return-type, it
9213 is as if the trailing-return-type denotes the following type:
9214 * if the compound-statement is of the form
9215 { return attribute-specifier [opt] expression ; }
9216 the type of the returned expression after lvalue-to-rvalue
9217 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9218 (_conv.array_ 4.2), and function-to-pointer conversion
9219 (_conv.func_ 4.3);
9220 * otherwise, void. */
9222 /* In a lambda that has neither a lambda-return-type-clause
9223 nor a deducible form, errors should be reported for return statements
9224 in the body. Since we used void as the placeholder return type, parsing
9225 the body as usual will give such desired behavior. */
9226 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9227 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9228 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9230 tree expr = NULL_TREE;
9231 cp_id_kind idk = CP_ID_KIND_NONE;
9233 /* Parse tentatively in case there's more after the initial return
9234 statement. */
9235 cp_parser_parse_tentatively (parser);
9237 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9239 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9242 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9244 if (cp_parser_parse_definitely (parser))
9246 if (!processing_template_decl)
9247 apply_deduced_return_type (fco, lambda_return_type (expr));
9249 /* Will get error here if type not deduced yet. */
9250 finish_return_stmt (expr);
9252 done = true;
9256 if (!done)
9258 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9259 cp_parser_label_declaration (parser);
9260 cp_parser_statement_seq_opt (parser, NULL_TREE);
9261 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9264 finish_compound_stmt (compound_stmt);
9266 out:
9267 finish_function_body (body);
9268 finish_lambda_scope ();
9270 /* Finish the function and generate code for it if necessary. */
9271 tree fn = finish_function (/*inline*/2);
9273 /* Only expand if the call op is not a template. */
9274 if (!DECL_TEMPLATE_INFO (fco))
9275 expand_or_defer_fn (fn);
9278 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9279 if (nested)
9280 pop_function_context();
9281 else
9282 --function_depth;
9285 /* Statements [gram.stmt.stmt] */
9287 /* Parse a statement.
9289 statement:
9290 labeled-statement
9291 expression-statement
9292 compound-statement
9293 selection-statement
9294 iteration-statement
9295 jump-statement
9296 declaration-statement
9297 try-block
9299 C++11:
9301 statement:
9302 labeled-statement
9303 attribute-specifier-seq (opt) expression-statement
9304 attribute-specifier-seq (opt) compound-statement
9305 attribute-specifier-seq (opt) selection-statement
9306 attribute-specifier-seq (opt) iteration-statement
9307 attribute-specifier-seq (opt) jump-statement
9308 declaration-statement
9309 attribute-specifier-seq (opt) try-block
9311 TM Extension:
9313 statement:
9314 atomic-statement
9316 IN_COMPOUND is true when the statement is nested inside a
9317 cp_parser_compound_statement; this matters for certain pragmas.
9319 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9320 is a (possibly labeled) if statement which is not enclosed in braces
9321 and has an else clause. This is used to implement -Wparentheses. */
9323 static void
9324 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9325 bool in_compound, bool *if_p)
9327 tree statement, std_attrs = NULL_TREE;
9328 cp_token *token;
9329 location_t statement_location, attrs_location;
9331 restart:
9332 if (if_p != NULL)
9333 *if_p = false;
9334 /* There is no statement yet. */
9335 statement = NULL_TREE;
9337 cp_lexer_save_tokens (parser->lexer);
9338 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9339 if (c_dialect_objc ())
9340 /* In obj-c++, seeing '[[' might be the either the beginning of
9341 c++11 attributes, or a nested objc-message-expression. So
9342 let's parse the c++11 attributes tentatively. */
9343 cp_parser_parse_tentatively (parser);
9344 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9345 if (c_dialect_objc ())
9347 if (!cp_parser_parse_definitely (parser))
9348 std_attrs = NULL_TREE;
9351 /* Peek at the next token. */
9352 token = cp_lexer_peek_token (parser->lexer);
9353 /* Remember the location of the first token in the statement. */
9354 statement_location = token->location;
9355 /* If this is a keyword, then that will often determine what kind of
9356 statement we have. */
9357 if (token->type == CPP_KEYWORD)
9359 enum rid keyword = token->keyword;
9361 switch (keyword)
9363 case RID_CASE:
9364 case RID_DEFAULT:
9365 /* Looks like a labeled-statement with a case label.
9366 Parse the label, and then use tail recursion to parse
9367 the statement. */
9368 cp_parser_label_for_labeled_statement (parser, std_attrs);
9369 goto restart;
9371 case RID_IF:
9372 case RID_SWITCH:
9373 statement = cp_parser_selection_statement (parser, if_p);
9374 break;
9376 case RID_WHILE:
9377 case RID_DO:
9378 case RID_FOR:
9379 statement = cp_parser_iteration_statement (parser, false);
9380 break;
9382 case RID_BREAK:
9383 case RID_CONTINUE:
9384 case RID_RETURN:
9385 case RID_GOTO:
9386 statement = cp_parser_jump_statement (parser);
9387 break;
9389 /* Objective-C++ exception-handling constructs. */
9390 case RID_AT_TRY:
9391 case RID_AT_CATCH:
9392 case RID_AT_FINALLY:
9393 case RID_AT_SYNCHRONIZED:
9394 case RID_AT_THROW:
9395 statement = cp_parser_objc_statement (parser);
9396 break;
9398 case RID_TRY:
9399 statement = cp_parser_try_block (parser);
9400 break;
9402 case RID_NAMESPACE:
9403 /* This must be a namespace alias definition. */
9404 cp_parser_declaration_statement (parser);
9405 return;
9407 case RID_TRANSACTION_ATOMIC:
9408 case RID_TRANSACTION_RELAXED:
9409 statement = cp_parser_transaction (parser, keyword);
9410 break;
9411 case RID_TRANSACTION_CANCEL:
9412 statement = cp_parser_transaction_cancel (parser);
9413 break;
9415 default:
9416 /* It might be a keyword like `int' that can start a
9417 declaration-statement. */
9418 break;
9421 else if (token->type == CPP_NAME)
9423 /* If the next token is a `:', then we are looking at a
9424 labeled-statement. */
9425 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9426 if (token->type == CPP_COLON)
9428 /* Looks like a labeled-statement with an ordinary label.
9429 Parse the label, and then use tail recursion to parse
9430 the statement. */
9432 cp_parser_label_for_labeled_statement (parser, std_attrs);
9433 goto restart;
9436 /* Anything that starts with a `{' must be a compound-statement. */
9437 else if (token->type == CPP_OPEN_BRACE)
9438 statement = cp_parser_compound_statement (parser, NULL, false, false);
9439 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9440 a statement all its own. */
9441 else if (token->type == CPP_PRAGMA)
9443 /* Only certain OpenMP pragmas are attached to statements, and thus
9444 are considered statements themselves. All others are not. In
9445 the context of a compound, accept the pragma as a "statement" and
9446 return so that we can check for a close brace. Otherwise we
9447 require a real statement and must go back and read one. */
9448 if (in_compound)
9449 cp_parser_pragma (parser, pragma_compound);
9450 else if (!cp_parser_pragma (parser, pragma_stmt))
9451 goto restart;
9452 return;
9454 else if (token->type == CPP_EOF)
9456 cp_parser_error (parser, "expected statement");
9457 return;
9460 /* Everything else must be a declaration-statement or an
9461 expression-statement. Try for the declaration-statement
9462 first, unless we are looking at a `;', in which case we know that
9463 we have an expression-statement. */
9464 if (!statement)
9466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9468 if (std_attrs != NULL_TREE)
9470 /* Attributes should be parsed as part of the the
9471 declaration, so let's un-parse them. */
9472 cp_lexer_rollback_tokens (parser->lexer);
9473 std_attrs = NULL_TREE;
9476 cp_parser_parse_tentatively (parser);
9477 /* Try to parse the declaration-statement. */
9478 cp_parser_declaration_statement (parser);
9479 /* If that worked, we're done. */
9480 if (cp_parser_parse_definitely (parser))
9481 return;
9483 /* Look for an expression-statement instead. */
9484 statement = cp_parser_expression_statement (parser, in_statement_expr);
9487 /* Set the line number for the statement. */
9488 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9489 SET_EXPR_LOCATION (statement, statement_location);
9491 /* Note that for now, we don't do anything with c++11 statements
9492 parsed at this level. */
9493 if (std_attrs != NULL_TREE)
9494 warning_at (attrs_location,
9495 OPT_Wattributes,
9496 "attributes at the beginning of statement are ignored");
9499 /* Parse the label for a labeled-statement, i.e.
9501 identifier :
9502 case constant-expression :
9503 default :
9505 GNU Extension:
9506 case constant-expression ... constant-expression : statement
9508 When a label is parsed without errors, the label is added to the
9509 parse tree by the finish_* functions, so this function doesn't
9510 have to return the label. */
9512 static void
9513 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9515 cp_token *token;
9516 tree label = NULL_TREE;
9517 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9519 /* The next token should be an identifier. */
9520 token = cp_lexer_peek_token (parser->lexer);
9521 if (token->type != CPP_NAME
9522 && token->type != CPP_KEYWORD)
9524 cp_parser_error (parser, "expected labeled-statement");
9525 return;
9528 parser->colon_corrects_to_scope_p = false;
9529 switch (token->keyword)
9531 case RID_CASE:
9533 tree expr, expr_hi;
9534 cp_token *ellipsis;
9536 /* Consume the `case' token. */
9537 cp_lexer_consume_token (parser->lexer);
9538 /* Parse the constant-expression. */
9539 expr = cp_parser_constant_expression (parser,
9540 /*allow_non_constant_p=*/false,
9541 NULL);
9543 ellipsis = cp_lexer_peek_token (parser->lexer);
9544 if (ellipsis->type == CPP_ELLIPSIS)
9546 /* Consume the `...' token. */
9547 cp_lexer_consume_token (parser->lexer);
9548 expr_hi =
9549 cp_parser_constant_expression (parser,
9550 /*allow_non_constant_p=*/false,
9551 NULL);
9552 /* We don't need to emit warnings here, as the common code
9553 will do this for us. */
9555 else
9556 expr_hi = NULL_TREE;
9558 if (parser->in_switch_statement_p)
9559 finish_case_label (token->location, expr, expr_hi);
9560 else
9561 error_at (token->location,
9562 "case label %qE not within a switch statement",
9563 expr);
9565 break;
9567 case RID_DEFAULT:
9568 /* Consume the `default' token. */
9569 cp_lexer_consume_token (parser->lexer);
9571 if (parser->in_switch_statement_p)
9572 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9573 else
9574 error_at (token->location, "case label not within a switch statement");
9575 break;
9577 default:
9578 /* Anything else must be an ordinary label. */
9579 label = finish_label_stmt (cp_parser_identifier (parser));
9580 break;
9583 /* Require the `:' token. */
9584 cp_parser_require (parser, CPP_COLON, RT_COLON);
9586 /* An ordinary label may optionally be followed by attributes.
9587 However, this is only permitted if the attributes are then
9588 followed by a semicolon. This is because, for backward
9589 compatibility, when parsing
9590 lab: __attribute__ ((unused)) int i;
9591 we want the attribute to attach to "i", not "lab". */
9592 if (label != NULL_TREE
9593 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9595 tree attrs;
9596 cp_parser_parse_tentatively (parser);
9597 attrs = cp_parser_gnu_attributes_opt (parser);
9598 if (attrs == NULL_TREE
9599 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9600 cp_parser_abort_tentative_parse (parser);
9601 else if (!cp_parser_parse_definitely (parser))
9603 else
9604 attributes = chainon (attributes, attrs);
9607 if (attributes != NULL_TREE)
9608 cplus_decl_attributes (&label, attributes, 0);
9610 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9613 /* Parse an expression-statement.
9615 expression-statement:
9616 expression [opt] ;
9618 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9619 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9620 indicates whether this expression-statement is part of an
9621 expression statement. */
9623 static tree
9624 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9626 tree statement = NULL_TREE;
9627 cp_token *token = cp_lexer_peek_token (parser->lexer);
9629 /* If the next token is a ';', then there is no expression
9630 statement. */
9631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9633 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9634 if (statement == error_mark_node
9635 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9637 cp_parser_skip_to_end_of_block_or_statement (parser);
9638 return error_mark_node;
9642 /* Give a helpful message for "A<T>::type t;" and the like. */
9643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9644 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9646 if (TREE_CODE (statement) == SCOPE_REF)
9647 error_at (token->location, "need %<typename%> before %qE because "
9648 "%qT is a dependent scope",
9649 statement, TREE_OPERAND (statement, 0));
9650 else if (is_overloaded_fn (statement)
9651 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9653 /* A::A a; */
9654 tree fn = get_first_fn (statement);
9655 error_at (token->location,
9656 "%<%T::%D%> names the constructor, not the type",
9657 DECL_CONTEXT (fn), DECL_NAME (fn));
9661 /* Consume the final `;'. */
9662 cp_parser_consume_semicolon_at_end_of_statement (parser);
9664 if (in_statement_expr
9665 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9666 /* This is the final expression statement of a statement
9667 expression. */
9668 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9669 else if (statement)
9670 statement = finish_expr_stmt (statement);
9672 return statement;
9675 /* Parse a compound-statement.
9677 compound-statement:
9678 { statement-seq [opt] }
9680 GNU extension:
9682 compound-statement:
9683 { label-declaration-seq [opt] statement-seq [opt] }
9685 label-declaration-seq:
9686 label-declaration
9687 label-declaration-seq label-declaration
9689 Returns a tree representing the statement. */
9691 static tree
9692 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9693 bool in_try, bool function_body)
9695 tree compound_stmt;
9697 /* Consume the `{'. */
9698 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9699 return error_mark_node;
9700 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9701 && !function_body)
9702 pedwarn (input_location, OPT_Wpedantic,
9703 "compound-statement in constexpr function");
9704 /* Begin the compound-statement. */
9705 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9706 /* If the next keyword is `__label__' we have a label declaration. */
9707 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9708 cp_parser_label_declaration (parser);
9709 /* Parse an (optional) statement-seq. */
9710 cp_parser_statement_seq_opt (parser, in_statement_expr);
9711 /* Finish the compound-statement. */
9712 finish_compound_stmt (compound_stmt);
9713 /* Consume the `}'. */
9714 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9716 return compound_stmt;
9719 /* Parse an (optional) statement-seq.
9721 statement-seq:
9722 statement
9723 statement-seq [opt] statement */
9725 static void
9726 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9728 /* Scan statements until there aren't any more. */
9729 while (true)
9731 cp_token *token = cp_lexer_peek_token (parser->lexer);
9733 /* If we are looking at a `}', then we have run out of
9734 statements; the same is true if we have reached the end
9735 of file, or have stumbled upon a stray '@end'. */
9736 if (token->type == CPP_CLOSE_BRACE
9737 || token->type == CPP_EOF
9738 || token->type == CPP_PRAGMA_EOL
9739 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9740 break;
9742 /* If we are in a compound statement and find 'else' then
9743 something went wrong. */
9744 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9746 if (parser->in_statement & IN_IF_STMT)
9747 break;
9748 else
9750 token = cp_lexer_consume_token (parser->lexer);
9751 error_at (token->location, "%<else%> without a previous %<if%>");
9755 /* Parse the statement. */
9756 cp_parser_statement (parser, in_statement_expr, true, NULL);
9760 /* Parse a selection-statement.
9762 selection-statement:
9763 if ( condition ) statement
9764 if ( condition ) statement else statement
9765 switch ( condition ) statement
9767 Returns the new IF_STMT or SWITCH_STMT.
9769 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9770 is a (possibly labeled) if statement which is not enclosed in
9771 braces and has an else clause. This is used to implement
9772 -Wparentheses. */
9774 static tree
9775 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9777 cp_token *token;
9778 enum rid keyword;
9780 if (if_p != NULL)
9781 *if_p = false;
9783 /* Peek at the next token. */
9784 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9786 /* See what kind of keyword it is. */
9787 keyword = token->keyword;
9788 switch (keyword)
9790 case RID_IF:
9791 case RID_SWITCH:
9793 tree statement;
9794 tree condition;
9796 /* Look for the `('. */
9797 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9799 cp_parser_skip_to_end_of_statement (parser);
9800 return error_mark_node;
9803 /* Begin the selection-statement. */
9804 if (keyword == RID_IF)
9805 statement = begin_if_stmt ();
9806 else
9807 statement = begin_switch_stmt ();
9809 /* Parse the condition. */
9810 condition = cp_parser_condition (parser);
9811 /* Look for the `)'. */
9812 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9813 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9814 /*consume_paren=*/true);
9816 if (keyword == RID_IF)
9818 bool nested_if;
9819 unsigned char in_statement;
9821 /* Add the condition. */
9822 finish_if_stmt_cond (condition, statement);
9824 /* Parse the then-clause. */
9825 in_statement = parser->in_statement;
9826 parser->in_statement |= IN_IF_STMT;
9827 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9829 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9830 add_stmt (build_empty_stmt (loc));
9831 cp_lexer_consume_token (parser->lexer);
9832 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9833 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9834 "empty body in an %<if%> statement");
9835 nested_if = false;
9837 else
9838 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9839 parser->in_statement = in_statement;
9841 finish_then_clause (statement);
9843 /* If the next token is `else', parse the else-clause. */
9844 if (cp_lexer_next_token_is_keyword (parser->lexer,
9845 RID_ELSE))
9847 /* Consume the `else' keyword. */
9848 cp_lexer_consume_token (parser->lexer);
9849 begin_else_clause (statement);
9850 /* Parse the else-clause. */
9851 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9853 location_t loc;
9854 loc = cp_lexer_peek_token (parser->lexer)->location;
9855 warning_at (loc,
9856 OPT_Wempty_body, "suggest braces around "
9857 "empty body in an %<else%> statement");
9858 add_stmt (build_empty_stmt (loc));
9859 cp_lexer_consume_token (parser->lexer);
9861 else
9862 cp_parser_implicitly_scoped_statement (parser, NULL);
9864 finish_else_clause (statement);
9866 /* If we are currently parsing a then-clause, then
9867 IF_P will not be NULL. We set it to true to
9868 indicate that this if statement has an else clause.
9869 This may trigger the Wparentheses warning below
9870 when we get back up to the parent if statement. */
9871 if (if_p != NULL)
9872 *if_p = true;
9874 else
9876 /* This if statement does not have an else clause. If
9877 NESTED_IF is true, then the then-clause is an if
9878 statement which does have an else clause. We warn
9879 about the potential ambiguity. */
9880 if (nested_if)
9881 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9882 "suggest explicit braces to avoid ambiguous"
9883 " %<else%>");
9886 /* Now we're all done with the if-statement. */
9887 finish_if_stmt (statement);
9889 else
9891 bool in_switch_statement_p;
9892 unsigned char in_statement;
9894 /* Add the condition. */
9895 finish_switch_cond (condition, statement);
9897 /* Parse the body of the switch-statement. */
9898 in_switch_statement_p = parser->in_switch_statement_p;
9899 in_statement = parser->in_statement;
9900 parser->in_switch_statement_p = true;
9901 parser->in_statement |= IN_SWITCH_STMT;
9902 cp_parser_implicitly_scoped_statement (parser, NULL);
9903 parser->in_switch_statement_p = in_switch_statement_p;
9904 parser->in_statement = in_statement;
9906 /* Now we're all done with the switch-statement. */
9907 finish_switch_stmt (statement);
9910 return statement;
9912 break;
9914 default:
9915 cp_parser_error (parser, "expected selection-statement");
9916 return error_mark_node;
9920 /* Parse a condition.
9922 condition:
9923 expression
9924 type-specifier-seq declarator = initializer-clause
9925 type-specifier-seq declarator braced-init-list
9927 GNU Extension:
9929 condition:
9930 type-specifier-seq declarator asm-specification [opt]
9931 attributes [opt] = assignment-expression
9933 Returns the expression that should be tested. */
9935 static tree
9936 cp_parser_condition (cp_parser* parser)
9938 cp_decl_specifier_seq type_specifiers;
9939 const char *saved_message;
9940 int declares_class_or_enum;
9942 /* Try the declaration first. */
9943 cp_parser_parse_tentatively (parser);
9944 /* New types are not allowed in the type-specifier-seq for a
9945 condition. */
9946 saved_message = parser->type_definition_forbidden_message;
9947 parser->type_definition_forbidden_message
9948 = G_("types may not be defined in conditions");
9949 /* Parse the type-specifier-seq. */
9950 cp_parser_decl_specifier_seq (parser,
9951 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9952 &type_specifiers,
9953 &declares_class_or_enum);
9954 /* Restore the saved message. */
9955 parser->type_definition_forbidden_message = saved_message;
9956 /* If all is well, we might be looking at a declaration. */
9957 if (!cp_parser_error_occurred (parser))
9959 tree decl;
9960 tree asm_specification;
9961 tree attributes;
9962 cp_declarator *declarator;
9963 tree initializer = NULL_TREE;
9965 /* Parse the declarator. */
9966 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9967 /*ctor_dtor_or_conv_p=*/NULL,
9968 /*parenthesized_p=*/NULL,
9969 /*member_p=*/false);
9970 /* Parse the attributes. */
9971 attributes = cp_parser_attributes_opt (parser);
9972 /* Parse the asm-specification. */
9973 asm_specification = cp_parser_asm_specification_opt (parser);
9974 /* If the next token is not an `=' or '{', then we might still be
9975 looking at an expression. For example:
9977 if (A(a).x)
9979 looks like a decl-specifier-seq and a declarator -- but then
9980 there is no `=', so this is an expression. */
9981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9982 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9983 cp_parser_simulate_error (parser);
9985 /* If we did see an `=' or '{', then we are looking at a declaration
9986 for sure. */
9987 if (cp_parser_parse_definitely (parser))
9989 tree pushed_scope;
9990 bool non_constant_p;
9991 bool flags = LOOKUP_ONLYCONVERTING;
9993 /* Create the declaration. */
9994 decl = start_decl (declarator, &type_specifiers,
9995 /*initialized_p=*/true,
9996 attributes, /*prefix_attributes=*/NULL_TREE,
9997 &pushed_scope);
9999 /* Parse the initializer. */
10000 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10002 initializer = cp_parser_braced_list (parser, &non_constant_p);
10003 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10004 flags = 0;
10006 else
10008 /* Consume the `='. */
10009 cp_parser_require (parser, CPP_EQ, RT_EQ);
10010 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10012 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10013 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10015 /* Process the initializer. */
10016 cp_finish_decl (decl,
10017 initializer, !non_constant_p,
10018 asm_specification,
10019 flags);
10021 if (pushed_scope)
10022 pop_scope (pushed_scope);
10024 return convert_from_reference (decl);
10027 /* If we didn't even get past the declarator successfully, we are
10028 definitely not looking at a declaration. */
10029 else
10030 cp_parser_abort_tentative_parse (parser);
10032 /* Otherwise, we are looking at an expression. */
10033 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10036 /* Parses a for-statement or range-for-statement until the closing ')',
10037 not included. */
10039 static tree
10040 cp_parser_for (cp_parser *parser, bool ivdep)
10042 tree init, scope, decl;
10043 bool is_range_for;
10045 /* Begin the for-statement. */
10046 scope = begin_for_scope (&init);
10048 /* Parse the initialization. */
10049 is_range_for = cp_parser_for_init_statement (parser, &decl);
10051 if (is_range_for)
10052 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10053 else
10054 return cp_parser_c_for (parser, scope, init, ivdep);
10057 static tree
10058 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10060 /* Normal for loop */
10061 tree condition = NULL_TREE;
10062 tree expression = NULL_TREE;
10063 tree stmt;
10065 stmt = begin_for_stmt (scope, init);
10066 /* The for-init-statement has already been parsed in
10067 cp_parser_for_init_statement, so no work is needed here. */
10068 finish_for_init_stmt (stmt);
10070 /* If there's a condition, process it. */
10071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10072 condition = cp_parser_condition (parser);
10073 else if (ivdep)
10075 cp_parser_error (parser, "missing loop condition in loop with "
10076 "%<GCC ivdep%> pragma");
10077 condition = error_mark_node;
10079 finish_for_cond (condition, stmt, ivdep);
10080 /* Look for the `;'. */
10081 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10083 /* If there's an expression, process it. */
10084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10085 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10086 finish_for_expr (expression, stmt);
10088 return stmt;
10091 /* Tries to parse a range-based for-statement:
10093 range-based-for:
10094 decl-specifier-seq declarator : expression
10096 The decl-specifier-seq declarator and the `:' are already parsed by
10097 cp_parser_for_init_statement. If processing_template_decl it returns a
10098 newly created RANGE_FOR_STMT; if not, it is converted to a
10099 regular FOR_STMT. */
10101 static tree
10102 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10103 bool ivdep)
10105 tree stmt, range_expr;
10107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10109 bool expr_non_constant_p;
10110 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10112 else
10113 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10115 /* If in template, STMT is converted to a normal for-statement
10116 at instantiation. If not, it is done just ahead. */
10117 if (processing_template_decl)
10119 if (check_for_bare_parameter_packs (range_expr))
10120 range_expr = error_mark_node;
10121 stmt = begin_range_for_stmt (scope, init);
10122 if (ivdep)
10123 RANGE_FOR_IVDEP (stmt) = 1;
10124 finish_range_for_decl (stmt, range_decl, range_expr);
10125 if (!type_dependent_expression_p (range_expr)
10126 /* do_auto_deduction doesn't mess with template init-lists. */
10127 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10128 do_range_for_auto_deduction (range_decl, range_expr);
10130 else
10132 stmt = begin_for_stmt (scope, init);
10133 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10135 return stmt;
10138 /* Subroutine of cp_convert_range_for: given the initializer expression,
10139 builds up the range temporary. */
10141 static tree
10142 build_range_temp (tree range_expr)
10144 tree range_type, range_temp;
10146 /* Find out the type deduced by the declaration
10147 `auto &&__range = range_expr'. */
10148 range_type = cp_build_reference_type (make_auto (), true);
10149 range_type = do_auto_deduction (range_type, range_expr,
10150 type_uses_auto (range_type));
10152 /* Create the __range variable. */
10153 range_temp = build_decl (input_location, VAR_DECL,
10154 get_identifier ("__for_range"), range_type);
10155 TREE_USED (range_temp) = 1;
10156 DECL_ARTIFICIAL (range_temp) = 1;
10158 return range_temp;
10161 /* Used by cp_parser_range_for in template context: we aren't going to
10162 do a full conversion yet, but we still need to resolve auto in the
10163 type of the for-range-declaration if present. This is basically
10164 a shortcut version of cp_convert_range_for. */
10166 static void
10167 do_range_for_auto_deduction (tree decl, tree range_expr)
10169 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10170 if (auto_node)
10172 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10173 range_temp = convert_from_reference (build_range_temp (range_expr));
10174 iter_type = (cp_parser_perform_range_for_lookup
10175 (range_temp, &begin_dummy, &end_dummy));
10176 if (iter_type)
10178 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10179 iter_type);
10180 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10181 tf_warning_or_error);
10182 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10183 iter_decl, auto_node);
10188 /* Converts a range-based for-statement into a normal
10189 for-statement, as per the definition.
10191 for (RANGE_DECL : RANGE_EXPR)
10192 BLOCK
10194 should be equivalent to:
10197 auto &&__range = RANGE_EXPR;
10198 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10199 __begin != __end;
10200 ++__begin)
10202 RANGE_DECL = *__begin;
10203 BLOCK
10207 If RANGE_EXPR is an array:
10208 BEGIN_EXPR = __range
10209 END_EXPR = __range + ARRAY_SIZE(__range)
10210 Else if RANGE_EXPR has a member 'begin' or 'end':
10211 BEGIN_EXPR = __range.begin()
10212 END_EXPR = __range.end()
10213 Else:
10214 BEGIN_EXPR = begin(__range)
10215 END_EXPR = end(__range);
10217 If __range has a member 'begin' but not 'end', or vice versa, we must
10218 still use the second alternative (it will surely fail, however).
10219 When calling begin()/end() in the third alternative we must use
10220 argument dependent lookup, but always considering 'std' as an associated
10221 namespace. */
10223 tree
10224 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10225 bool ivdep)
10227 tree begin, end;
10228 tree iter_type, begin_expr, end_expr;
10229 tree condition, expression;
10231 if (range_decl == error_mark_node || range_expr == error_mark_node)
10232 /* If an error happened previously do nothing or else a lot of
10233 unhelpful errors would be issued. */
10234 begin_expr = end_expr = iter_type = error_mark_node;
10235 else
10237 tree range_temp;
10239 if (TREE_CODE (range_expr) == VAR_DECL
10240 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10241 /* Can't bind a reference to an array of runtime bound. */
10242 range_temp = range_expr;
10243 else
10245 range_temp = build_range_temp (range_expr);
10246 pushdecl (range_temp);
10247 cp_finish_decl (range_temp, range_expr,
10248 /*is_constant_init*/false, NULL_TREE,
10249 LOOKUP_ONLYCONVERTING);
10250 range_temp = convert_from_reference (range_temp);
10252 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10253 &begin_expr, &end_expr);
10256 /* The new for initialization statement. */
10257 begin = build_decl (input_location, VAR_DECL,
10258 get_identifier ("__for_begin"), iter_type);
10259 TREE_USED (begin) = 1;
10260 DECL_ARTIFICIAL (begin) = 1;
10261 pushdecl (begin);
10262 cp_finish_decl (begin, begin_expr,
10263 /*is_constant_init*/false, NULL_TREE,
10264 LOOKUP_ONLYCONVERTING);
10266 end = build_decl (input_location, VAR_DECL,
10267 get_identifier ("__for_end"), iter_type);
10268 TREE_USED (end) = 1;
10269 DECL_ARTIFICIAL (end) = 1;
10270 pushdecl (end);
10271 cp_finish_decl (end, end_expr,
10272 /*is_constant_init*/false, NULL_TREE,
10273 LOOKUP_ONLYCONVERTING);
10275 finish_for_init_stmt (statement);
10277 /* The new for condition. */
10278 condition = build_x_binary_op (input_location, NE_EXPR,
10279 begin, ERROR_MARK,
10280 end, ERROR_MARK,
10281 NULL, tf_warning_or_error);
10282 finish_for_cond (condition, statement, ivdep);
10284 /* The new increment expression. */
10285 expression = finish_unary_op_expr (input_location,
10286 PREINCREMENT_EXPR, begin,
10287 tf_warning_or_error);
10288 finish_for_expr (expression, statement);
10290 /* The declaration is initialized with *__begin inside the loop body. */
10291 cp_finish_decl (range_decl,
10292 build_x_indirect_ref (input_location, begin, RO_NULL,
10293 tf_warning_or_error),
10294 /*is_constant_init*/false, NULL_TREE,
10295 LOOKUP_ONLYCONVERTING);
10297 return statement;
10300 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10301 We need to solve both at the same time because the method used
10302 depends on the existence of members begin or end.
10303 Returns the type deduced for the iterator expression. */
10305 static tree
10306 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10308 if (error_operand_p (range))
10310 *begin = *end = error_mark_node;
10311 return error_mark_node;
10314 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10316 error ("range-based %<for%> expression of type %qT "
10317 "has incomplete type", TREE_TYPE (range));
10318 *begin = *end = error_mark_node;
10319 return error_mark_node;
10321 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10323 /* If RANGE is an array, we will use pointer arithmetic. */
10324 *begin = range;
10325 *end = build_binary_op (input_location, PLUS_EXPR,
10326 range,
10327 array_type_nelts_top (TREE_TYPE (range)),
10329 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10331 else
10333 /* If it is not an array, we must do a bit of magic. */
10334 tree id_begin, id_end;
10335 tree member_begin, member_end;
10337 *begin = *end = error_mark_node;
10339 id_begin = get_identifier ("begin");
10340 id_end = get_identifier ("end");
10341 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10342 /*protect=*/2, /*want_type=*/false,
10343 tf_warning_or_error);
10344 member_end = lookup_member (TREE_TYPE (range), id_end,
10345 /*protect=*/2, /*want_type=*/false,
10346 tf_warning_or_error);
10348 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10350 /* Use the member functions. */
10351 if (member_begin != NULL_TREE)
10352 *begin = cp_parser_range_for_member_function (range, id_begin);
10353 else
10354 error ("range-based %<for%> expression of type %qT has an "
10355 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10357 if (member_end != NULL_TREE)
10358 *end = cp_parser_range_for_member_function (range, id_end);
10359 else
10360 error ("range-based %<for%> expression of type %qT has a "
10361 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10363 else
10365 /* Use global functions with ADL. */
10366 vec<tree, va_gc> *vec;
10367 vec = make_tree_vector ();
10369 vec_safe_push (vec, range);
10371 member_begin = perform_koenig_lookup (id_begin, vec,
10372 tf_warning_or_error);
10373 *begin = finish_call_expr (member_begin, &vec, false, true,
10374 tf_warning_or_error);
10375 member_end = perform_koenig_lookup (id_end, vec,
10376 tf_warning_or_error);
10377 *end = finish_call_expr (member_end, &vec, false, true,
10378 tf_warning_or_error);
10380 release_tree_vector (vec);
10383 /* Last common checks. */
10384 if (*begin == error_mark_node || *end == error_mark_node)
10386 /* If one of the expressions is an error do no more checks. */
10387 *begin = *end = error_mark_node;
10388 return error_mark_node;
10390 else if (type_dependent_expression_p (*begin)
10391 || type_dependent_expression_p (*end))
10392 /* Can happen, when, eg, in a template context, Koenig lookup
10393 can't resolve begin/end (c++/58503). */
10394 return NULL_TREE;
10395 else
10397 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10398 /* The unqualified type of the __begin and __end temporaries should
10399 be the same, as required by the multiple auto declaration. */
10400 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10401 error ("inconsistent begin/end types in range-based %<for%> "
10402 "statement: %qT and %qT",
10403 TREE_TYPE (*begin), TREE_TYPE (*end));
10404 return iter_type;
10409 /* Helper function for cp_parser_perform_range_for_lookup.
10410 Builds a tree for RANGE.IDENTIFIER(). */
10412 static tree
10413 cp_parser_range_for_member_function (tree range, tree identifier)
10415 tree member, res;
10416 vec<tree, va_gc> *vec;
10418 member = finish_class_member_access_expr (range, identifier,
10419 false, tf_warning_or_error);
10420 if (member == error_mark_node)
10421 return error_mark_node;
10423 vec = make_tree_vector ();
10424 res = finish_call_expr (member, &vec,
10425 /*disallow_virtual=*/false,
10426 /*koenig_p=*/false,
10427 tf_warning_or_error);
10428 release_tree_vector (vec);
10429 return res;
10432 /* Parse an iteration-statement.
10434 iteration-statement:
10435 while ( condition ) statement
10436 do statement while ( expression ) ;
10437 for ( for-init-statement condition [opt] ; expression [opt] )
10438 statement
10440 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10442 static tree
10443 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10445 cp_token *token;
10446 enum rid keyword;
10447 tree statement;
10448 unsigned char in_statement;
10450 /* Peek at the next token. */
10451 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10452 if (!token)
10453 return error_mark_node;
10455 /* Remember whether or not we are already within an iteration
10456 statement. */
10457 in_statement = parser->in_statement;
10459 /* See what kind of keyword it is. */
10460 keyword = token->keyword;
10461 switch (keyword)
10463 case RID_WHILE:
10465 tree condition;
10467 /* Begin the while-statement. */
10468 statement = begin_while_stmt ();
10469 /* Look for the `('. */
10470 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10471 /* Parse the condition. */
10472 condition = cp_parser_condition (parser);
10473 finish_while_stmt_cond (condition, statement, ivdep);
10474 /* Look for the `)'. */
10475 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10476 /* Parse the dependent statement. */
10477 parser->in_statement = IN_ITERATION_STMT;
10478 cp_parser_already_scoped_statement (parser);
10479 parser->in_statement = in_statement;
10480 /* We're done with the while-statement. */
10481 finish_while_stmt (statement);
10483 break;
10485 case RID_DO:
10487 tree expression;
10489 /* Begin the do-statement. */
10490 statement = begin_do_stmt ();
10491 /* Parse the body of the do-statement. */
10492 parser->in_statement = IN_ITERATION_STMT;
10493 cp_parser_implicitly_scoped_statement (parser, NULL);
10494 parser->in_statement = in_statement;
10495 finish_do_body (statement);
10496 /* Look for the `while' keyword. */
10497 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10498 /* Look for the `('. */
10499 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10500 /* Parse the expression. */
10501 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10502 /* We're done with the do-statement. */
10503 finish_do_stmt (expression, statement, ivdep);
10504 /* Look for the `)'. */
10505 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10506 /* Look for the `;'. */
10507 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10509 break;
10511 case RID_FOR:
10513 /* Look for the `('. */
10514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10516 statement = cp_parser_for (parser, ivdep);
10518 /* Look for the `)'. */
10519 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10521 /* Parse the body of the for-statement. */
10522 parser->in_statement = IN_ITERATION_STMT;
10523 cp_parser_already_scoped_statement (parser);
10524 parser->in_statement = in_statement;
10526 /* We're done with the for-statement. */
10527 finish_for_stmt (statement);
10529 break;
10531 default:
10532 cp_parser_error (parser, "expected iteration-statement");
10533 statement = error_mark_node;
10534 break;
10537 return statement;
10540 /* Parse a for-init-statement or the declarator of a range-based-for.
10541 Returns true if a range-based-for declaration is seen.
10543 for-init-statement:
10544 expression-statement
10545 simple-declaration */
10547 static bool
10548 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10550 /* If the next token is a `;', then we have an empty
10551 expression-statement. Grammatically, this is also a
10552 simple-declaration, but an invalid one, because it does not
10553 declare anything. Therefore, if we did not handle this case
10554 specially, we would issue an error message about an invalid
10555 declaration. */
10556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10558 bool is_range_for = false;
10559 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10561 parser->colon_corrects_to_scope_p = false;
10563 /* We're going to speculatively look for a declaration, falling back
10564 to an expression, if necessary. */
10565 cp_parser_parse_tentatively (parser);
10566 /* Parse the declaration. */
10567 cp_parser_simple_declaration (parser,
10568 /*function_definition_allowed_p=*/false,
10569 decl);
10570 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10571 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10573 /* It is a range-for, consume the ':' */
10574 cp_lexer_consume_token (parser->lexer);
10575 is_range_for = true;
10576 if (cxx_dialect < cxx11)
10578 error_at (cp_lexer_peek_token (parser->lexer)->location,
10579 "range-based %<for%> loops are not allowed "
10580 "in C++98 mode");
10581 *decl = error_mark_node;
10584 else
10585 /* The ';' is not consumed yet because we told
10586 cp_parser_simple_declaration not to. */
10587 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10589 if (cp_parser_parse_definitely (parser))
10590 return is_range_for;
10591 /* If the tentative parse failed, then we shall need to look for an
10592 expression-statement. */
10594 /* If we are here, it is an expression-statement. */
10595 cp_parser_expression_statement (parser, NULL_TREE);
10596 return false;
10599 /* Parse a jump-statement.
10601 jump-statement:
10602 break ;
10603 continue ;
10604 return expression [opt] ;
10605 return braced-init-list ;
10606 goto identifier ;
10608 GNU extension:
10610 jump-statement:
10611 goto * expression ;
10613 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10615 static tree
10616 cp_parser_jump_statement (cp_parser* parser)
10618 tree statement = error_mark_node;
10619 cp_token *token;
10620 enum rid keyword;
10621 unsigned char in_statement;
10623 /* Peek at the next token. */
10624 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10625 if (!token)
10626 return error_mark_node;
10628 /* See what kind of keyword it is. */
10629 keyword = token->keyword;
10630 switch (keyword)
10632 case RID_BREAK:
10633 in_statement = parser->in_statement & ~IN_IF_STMT;
10634 switch (in_statement)
10636 case 0:
10637 error_at (token->location, "break statement not within loop or switch");
10638 break;
10639 default:
10640 gcc_assert ((in_statement & IN_SWITCH_STMT)
10641 || in_statement == IN_ITERATION_STMT);
10642 statement = finish_break_stmt ();
10643 if (in_statement == IN_ITERATION_STMT)
10644 break_maybe_infinite_loop ();
10645 break;
10646 case IN_OMP_BLOCK:
10647 error_at (token->location, "invalid exit from OpenMP structured block");
10648 break;
10649 case IN_OMP_FOR:
10650 error_at (token->location, "break statement used with OpenMP for loop");
10651 break;
10652 case IN_CILK_SIMD_FOR:
10653 error_at (token->location, "break statement used with Cilk Plus for loop");
10654 break;
10656 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10657 break;
10659 case RID_CONTINUE:
10660 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10662 case 0:
10663 error_at (token->location, "continue statement not within a loop");
10664 break;
10665 case IN_CILK_SIMD_FOR:
10666 error_at (token->location,
10667 "continue statement within %<#pragma simd%> loop body");
10668 /* Fall through. */
10669 case IN_ITERATION_STMT:
10670 case IN_OMP_FOR:
10671 statement = finish_continue_stmt ();
10672 break;
10673 case IN_OMP_BLOCK:
10674 error_at (token->location, "invalid exit from OpenMP structured block");
10675 break;
10676 default:
10677 gcc_unreachable ();
10679 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10680 break;
10682 case RID_RETURN:
10684 tree expr;
10685 bool expr_non_constant_p;
10687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10689 cp_lexer_set_source_position (parser->lexer);
10690 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10691 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10693 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10694 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10695 else
10696 /* If the next token is a `;', then there is no
10697 expression. */
10698 expr = NULL_TREE;
10699 /* Build the return-statement. */
10700 statement = finish_return_stmt (expr);
10701 /* Look for the final `;'. */
10702 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10704 break;
10706 case RID_GOTO:
10707 /* Create the goto-statement. */
10708 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10710 /* Issue a warning about this use of a GNU extension. */
10711 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10712 /* Consume the '*' token. */
10713 cp_lexer_consume_token (parser->lexer);
10714 /* Parse the dependent expression. */
10715 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10717 else
10718 finish_goto_stmt (cp_parser_identifier (parser));
10719 /* Look for the final `;'. */
10720 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10721 break;
10723 default:
10724 cp_parser_error (parser, "expected jump-statement");
10725 break;
10728 return statement;
10731 /* Parse a declaration-statement.
10733 declaration-statement:
10734 block-declaration */
10736 static void
10737 cp_parser_declaration_statement (cp_parser* parser)
10739 void *p;
10741 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10742 p = obstack_alloc (&declarator_obstack, 0);
10744 /* Parse the block-declaration. */
10745 cp_parser_block_declaration (parser, /*statement_p=*/true);
10747 /* Free any declarators allocated. */
10748 obstack_free (&declarator_obstack, p);
10751 /* Some dependent statements (like `if (cond) statement'), are
10752 implicitly in their own scope. In other words, if the statement is
10753 a single statement (as opposed to a compound-statement), it is
10754 none-the-less treated as if it were enclosed in braces. Any
10755 declarations appearing in the dependent statement are out of scope
10756 after control passes that point. This function parses a statement,
10757 but ensures that is in its own scope, even if it is not a
10758 compound-statement.
10760 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10761 is a (possibly labeled) if statement which is not enclosed in
10762 braces and has an else clause. This is used to implement
10763 -Wparentheses.
10765 Returns the new statement. */
10767 static tree
10768 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10770 tree statement;
10772 if (if_p != NULL)
10773 *if_p = false;
10775 /* Mark if () ; with a special NOP_EXPR. */
10776 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10778 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10779 cp_lexer_consume_token (parser->lexer);
10780 statement = add_stmt (build_empty_stmt (loc));
10782 /* if a compound is opened, we simply parse the statement directly. */
10783 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10784 statement = cp_parser_compound_statement (parser, NULL, false, false);
10785 /* If the token is not a `{', then we must take special action. */
10786 else
10788 /* Create a compound-statement. */
10789 statement = begin_compound_stmt (0);
10790 /* Parse the dependent-statement. */
10791 cp_parser_statement (parser, NULL_TREE, false, if_p);
10792 /* Finish the dummy compound-statement. */
10793 finish_compound_stmt (statement);
10796 /* Return the statement. */
10797 return statement;
10800 /* For some dependent statements (like `while (cond) statement'), we
10801 have already created a scope. Therefore, even if the dependent
10802 statement is a compound-statement, we do not want to create another
10803 scope. */
10805 static void
10806 cp_parser_already_scoped_statement (cp_parser* parser)
10808 /* If the token is a `{', then we must take special action. */
10809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10810 cp_parser_statement (parser, NULL_TREE, false, NULL);
10811 else
10813 /* Avoid calling cp_parser_compound_statement, so that we
10814 don't create a new scope. Do everything else by hand. */
10815 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10816 /* If the next keyword is `__label__' we have a label declaration. */
10817 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10818 cp_parser_label_declaration (parser);
10819 /* Parse an (optional) statement-seq. */
10820 cp_parser_statement_seq_opt (parser, NULL_TREE);
10821 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10825 /* Declarations [gram.dcl.dcl] */
10827 /* Parse an optional declaration-sequence.
10829 declaration-seq:
10830 declaration
10831 declaration-seq declaration */
10833 static void
10834 cp_parser_declaration_seq_opt (cp_parser* parser)
10836 while (true)
10838 cp_token *token;
10840 token = cp_lexer_peek_token (parser->lexer);
10842 if (token->type == CPP_CLOSE_BRACE
10843 || token->type == CPP_EOF
10844 || token->type == CPP_PRAGMA_EOL)
10845 break;
10847 if (token->type == CPP_SEMICOLON)
10849 /* A declaration consisting of a single semicolon is
10850 invalid. Allow it unless we're being pedantic. */
10851 cp_lexer_consume_token (parser->lexer);
10852 if (!in_system_header_at (input_location))
10853 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10854 continue;
10857 /* If we're entering or exiting a region that's implicitly
10858 extern "C", modify the lang context appropriately. */
10859 if (!parser->implicit_extern_c && token->implicit_extern_c)
10861 push_lang_context (lang_name_c);
10862 parser->implicit_extern_c = true;
10864 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10866 pop_lang_context ();
10867 parser->implicit_extern_c = false;
10870 if (token->type == CPP_PRAGMA)
10872 /* A top-level declaration can consist solely of a #pragma.
10873 A nested declaration cannot, so this is done here and not
10874 in cp_parser_declaration. (A #pragma at block scope is
10875 handled in cp_parser_statement.) */
10876 cp_parser_pragma (parser, pragma_external);
10877 continue;
10880 /* Parse the declaration itself. */
10881 cp_parser_declaration (parser);
10885 /* Parse a declaration.
10887 declaration:
10888 block-declaration
10889 function-definition
10890 template-declaration
10891 explicit-instantiation
10892 explicit-specialization
10893 linkage-specification
10894 namespace-definition
10896 GNU extension:
10898 declaration:
10899 __extension__ declaration */
10901 static void
10902 cp_parser_declaration (cp_parser* parser)
10904 cp_token token1;
10905 cp_token token2;
10906 int saved_pedantic;
10907 void *p;
10908 tree attributes = NULL_TREE;
10910 /* Check for the `__extension__' keyword. */
10911 if (cp_parser_extension_opt (parser, &saved_pedantic))
10913 /* Parse the qualified declaration. */
10914 cp_parser_declaration (parser);
10915 /* Restore the PEDANTIC flag. */
10916 pedantic = saved_pedantic;
10918 return;
10921 /* Try to figure out what kind of declaration is present. */
10922 token1 = *cp_lexer_peek_token (parser->lexer);
10924 if (token1.type != CPP_EOF)
10925 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10926 else
10928 token2.type = CPP_EOF;
10929 token2.keyword = RID_MAX;
10932 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10933 p = obstack_alloc (&declarator_obstack, 0);
10935 /* If the next token is `extern' and the following token is a string
10936 literal, then we have a linkage specification. */
10937 if (token1.keyword == RID_EXTERN
10938 && cp_parser_is_pure_string_literal (&token2))
10939 cp_parser_linkage_specification (parser);
10940 /* If the next token is `template', then we have either a template
10941 declaration, an explicit instantiation, or an explicit
10942 specialization. */
10943 else if (token1.keyword == RID_TEMPLATE)
10945 /* `template <>' indicates a template specialization. */
10946 if (token2.type == CPP_LESS
10947 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10948 cp_parser_explicit_specialization (parser);
10949 /* `template <' indicates a template declaration. */
10950 else if (token2.type == CPP_LESS)
10951 cp_parser_template_declaration (parser, /*member_p=*/false);
10952 /* Anything else must be an explicit instantiation. */
10953 else
10954 cp_parser_explicit_instantiation (parser);
10956 /* If the next token is `export', then we have a template
10957 declaration. */
10958 else if (token1.keyword == RID_EXPORT)
10959 cp_parser_template_declaration (parser, /*member_p=*/false);
10960 /* If the next token is `extern', 'static' or 'inline' and the one
10961 after that is `template', we have a GNU extended explicit
10962 instantiation directive. */
10963 else if (cp_parser_allow_gnu_extensions_p (parser)
10964 && (token1.keyword == RID_EXTERN
10965 || token1.keyword == RID_STATIC
10966 || token1.keyword == RID_INLINE)
10967 && token2.keyword == RID_TEMPLATE)
10968 cp_parser_explicit_instantiation (parser);
10969 /* If the next token is `namespace', check for a named or unnamed
10970 namespace definition. */
10971 else if (token1.keyword == RID_NAMESPACE
10972 && (/* A named namespace definition. */
10973 (token2.type == CPP_NAME
10974 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10975 != CPP_EQ))
10976 /* An unnamed namespace definition. */
10977 || token2.type == CPP_OPEN_BRACE
10978 || token2.keyword == RID_ATTRIBUTE))
10979 cp_parser_namespace_definition (parser);
10980 /* An inline (associated) namespace definition. */
10981 else if (token1.keyword == RID_INLINE
10982 && token2.keyword == RID_NAMESPACE)
10983 cp_parser_namespace_definition (parser);
10984 /* Objective-C++ declaration/definition. */
10985 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10986 cp_parser_objc_declaration (parser, NULL_TREE);
10987 else if (c_dialect_objc ()
10988 && token1.keyword == RID_ATTRIBUTE
10989 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10990 cp_parser_objc_declaration (parser, attributes);
10991 /* We must have either a block declaration or a function
10992 definition. */
10993 else
10994 /* Try to parse a block-declaration, or a function-definition. */
10995 cp_parser_block_declaration (parser, /*statement_p=*/false);
10997 /* Free any declarators allocated. */
10998 obstack_free (&declarator_obstack, p);
11001 /* Parse a block-declaration.
11003 block-declaration:
11004 simple-declaration
11005 asm-definition
11006 namespace-alias-definition
11007 using-declaration
11008 using-directive
11010 GNU Extension:
11012 block-declaration:
11013 __extension__ block-declaration
11015 C++0x Extension:
11017 block-declaration:
11018 static_assert-declaration
11020 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11021 part of a declaration-statement. */
11023 static void
11024 cp_parser_block_declaration (cp_parser *parser,
11025 bool statement_p)
11027 cp_token *token1;
11028 int saved_pedantic;
11030 /* Check for the `__extension__' keyword. */
11031 if (cp_parser_extension_opt (parser, &saved_pedantic))
11033 /* Parse the qualified declaration. */
11034 cp_parser_block_declaration (parser, statement_p);
11035 /* Restore the PEDANTIC flag. */
11036 pedantic = saved_pedantic;
11038 return;
11041 /* Peek at the next token to figure out which kind of declaration is
11042 present. */
11043 token1 = cp_lexer_peek_token (parser->lexer);
11045 /* If the next keyword is `asm', we have an asm-definition. */
11046 if (token1->keyword == RID_ASM)
11048 if (statement_p)
11049 cp_parser_commit_to_tentative_parse (parser);
11050 cp_parser_asm_definition (parser);
11052 /* If the next keyword is `namespace', we have a
11053 namespace-alias-definition. */
11054 else if (token1->keyword == RID_NAMESPACE)
11055 cp_parser_namespace_alias_definition (parser);
11056 /* If the next keyword is `using', we have a
11057 using-declaration, a using-directive, or an alias-declaration. */
11058 else if (token1->keyword == RID_USING)
11060 cp_token *token2;
11062 if (statement_p)
11063 cp_parser_commit_to_tentative_parse (parser);
11064 /* If the token after `using' is `namespace', then we have a
11065 using-directive. */
11066 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11067 if (token2->keyword == RID_NAMESPACE)
11068 cp_parser_using_directive (parser);
11069 /* If the second token after 'using' is '=', then we have an
11070 alias-declaration. */
11071 else if (cxx_dialect >= cxx11
11072 && token2->type == CPP_NAME
11073 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11074 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11075 cp_parser_alias_declaration (parser);
11076 /* Otherwise, it's a using-declaration. */
11077 else
11078 cp_parser_using_declaration (parser,
11079 /*access_declaration_p=*/false);
11081 /* If the next keyword is `__label__' we have a misplaced label
11082 declaration. */
11083 else if (token1->keyword == RID_LABEL)
11085 cp_lexer_consume_token (parser->lexer);
11086 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11087 cp_parser_skip_to_end_of_statement (parser);
11088 /* If the next token is now a `;', consume it. */
11089 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11090 cp_lexer_consume_token (parser->lexer);
11092 /* If the next token is `static_assert' we have a static assertion. */
11093 else if (token1->keyword == RID_STATIC_ASSERT)
11094 cp_parser_static_assert (parser, /*member_p=*/false);
11095 /* Anything else must be a simple-declaration. */
11096 else
11097 cp_parser_simple_declaration (parser, !statement_p,
11098 /*maybe_range_for_decl*/NULL);
11101 /* Parse a simple-declaration.
11103 simple-declaration:
11104 decl-specifier-seq [opt] init-declarator-list [opt] ;
11106 init-declarator-list:
11107 init-declarator
11108 init-declarator-list , init-declarator
11110 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11111 function-definition as a simple-declaration.
11113 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11114 parsed declaration if it is an uninitialized single declarator not followed
11115 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11116 if present, will not be consumed. */
11118 static void
11119 cp_parser_simple_declaration (cp_parser* parser,
11120 bool function_definition_allowed_p,
11121 tree *maybe_range_for_decl)
11123 cp_decl_specifier_seq decl_specifiers;
11124 int declares_class_or_enum;
11125 bool saw_declarator;
11127 if (maybe_range_for_decl)
11128 *maybe_range_for_decl = NULL_TREE;
11130 /* Defer access checks until we know what is being declared; the
11131 checks for names appearing in the decl-specifier-seq should be
11132 done as if we were in the scope of the thing being declared. */
11133 push_deferring_access_checks (dk_deferred);
11135 /* Parse the decl-specifier-seq. We have to keep track of whether
11136 or not the decl-specifier-seq declares a named class or
11137 enumeration type, since that is the only case in which the
11138 init-declarator-list is allowed to be empty.
11140 [dcl.dcl]
11142 In a simple-declaration, the optional init-declarator-list can be
11143 omitted only when declaring a class or enumeration, that is when
11144 the decl-specifier-seq contains either a class-specifier, an
11145 elaborated-type-specifier, or an enum-specifier. */
11146 cp_parser_decl_specifier_seq (parser,
11147 CP_PARSER_FLAGS_OPTIONAL,
11148 &decl_specifiers,
11149 &declares_class_or_enum);
11150 /* We no longer need to defer access checks. */
11151 stop_deferring_access_checks ();
11153 /* In a block scope, a valid declaration must always have a
11154 decl-specifier-seq. By not trying to parse declarators, we can
11155 resolve the declaration/expression ambiguity more quickly. */
11156 if (!function_definition_allowed_p
11157 && !decl_specifiers.any_specifiers_p)
11159 cp_parser_error (parser, "expected declaration");
11160 goto done;
11163 /* If the next two tokens are both identifiers, the code is
11164 erroneous. The usual cause of this situation is code like:
11166 T t;
11168 where "T" should name a type -- but does not. */
11169 if (!decl_specifiers.any_type_specifiers_p
11170 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11172 /* If parsing tentatively, we should commit; we really are
11173 looking at a declaration. */
11174 cp_parser_commit_to_tentative_parse (parser);
11175 /* Give up. */
11176 goto done;
11179 /* If we have seen at least one decl-specifier, and the next token
11180 is not a parenthesis, then we must be looking at a declaration.
11181 (After "int (" we might be looking at a functional cast.) */
11182 if (decl_specifiers.any_specifiers_p
11183 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11184 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11185 && !cp_parser_error_occurred (parser))
11186 cp_parser_commit_to_tentative_parse (parser);
11188 /* Keep going until we hit the `;' at the end of the simple
11189 declaration. */
11190 saw_declarator = false;
11191 while (cp_lexer_next_token_is_not (parser->lexer,
11192 CPP_SEMICOLON))
11194 cp_token *token;
11195 bool function_definition_p;
11196 tree decl;
11198 if (saw_declarator)
11200 /* If we are processing next declarator, coma is expected */
11201 token = cp_lexer_peek_token (parser->lexer);
11202 gcc_assert (token->type == CPP_COMMA);
11203 cp_lexer_consume_token (parser->lexer);
11204 if (maybe_range_for_decl)
11205 *maybe_range_for_decl = error_mark_node;
11207 else
11208 saw_declarator = true;
11210 /* Parse the init-declarator. */
11211 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11212 /*checks=*/NULL,
11213 function_definition_allowed_p,
11214 /*member_p=*/false,
11215 declares_class_or_enum,
11216 &function_definition_p,
11217 maybe_range_for_decl);
11218 /* If an error occurred while parsing tentatively, exit quickly.
11219 (That usually happens when in the body of a function; each
11220 statement is treated as a declaration-statement until proven
11221 otherwise.) */
11222 if (cp_parser_error_occurred (parser))
11223 goto done;
11224 /* Handle function definitions specially. */
11225 if (function_definition_p)
11227 /* If the next token is a `,', then we are probably
11228 processing something like:
11230 void f() {}, *p;
11232 which is erroneous. */
11233 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11235 cp_token *token = cp_lexer_peek_token (parser->lexer);
11236 error_at (token->location,
11237 "mixing"
11238 " declarations and function-definitions is forbidden");
11240 /* Otherwise, we're done with the list of declarators. */
11241 else
11243 pop_deferring_access_checks ();
11244 return;
11247 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11248 *maybe_range_for_decl = decl;
11249 /* The next token should be either a `,' or a `;'. */
11250 token = cp_lexer_peek_token (parser->lexer);
11251 /* If it's a `,', there are more declarators to come. */
11252 if (token->type == CPP_COMMA)
11253 /* will be consumed next time around */;
11254 /* If it's a `;', we are done. */
11255 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11256 break;
11257 /* Anything else is an error. */
11258 else
11260 /* If we have already issued an error message we don't need
11261 to issue another one. */
11262 if (decl != error_mark_node
11263 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11264 cp_parser_error (parser, "expected %<,%> or %<;%>");
11265 /* Skip tokens until we reach the end of the statement. */
11266 cp_parser_skip_to_end_of_statement (parser);
11267 /* If the next token is now a `;', consume it. */
11268 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11269 cp_lexer_consume_token (parser->lexer);
11270 goto done;
11272 /* After the first time around, a function-definition is not
11273 allowed -- even if it was OK at first. For example:
11275 int i, f() {}
11277 is not valid. */
11278 function_definition_allowed_p = false;
11281 /* Issue an error message if no declarators are present, and the
11282 decl-specifier-seq does not itself declare a class or
11283 enumeration: [dcl.dcl]/3. */
11284 if (!saw_declarator)
11286 if (cp_parser_declares_only_class_p (parser))
11288 if (!declares_class_or_enum
11289 && decl_specifiers.type
11290 && OVERLOAD_TYPE_P (decl_specifiers.type))
11291 /* Ensure an error is issued anyway when finish_decltype_type,
11292 called via cp_parser_decl_specifier_seq, returns a class or
11293 an enumeration (c++/51786). */
11294 decl_specifiers.type = NULL_TREE;
11295 shadow_tag (&decl_specifiers);
11297 /* Perform any deferred access checks. */
11298 perform_deferred_access_checks (tf_warning_or_error);
11301 /* Consume the `;'. */
11302 if (!maybe_range_for_decl)
11303 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11305 done:
11306 pop_deferring_access_checks ();
11309 /* Parse a decl-specifier-seq.
11311 decl-specifier-seq:
11312 decl-specifier-seq [opt] decl-specifier
11313 decl-specifier attribute-specifier-seq [opt] (C++11)
11315 decl-specifier:
11316 storage-class-specifier
11317 type-specifier
11318 function-specifier
11319 friend
11320 typedef
11322 GNU Extension:
11324 decl-specifier:
11325 attributes
11327 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11329 The parser flags FLAGS is used to control type-specifier parsing.
11331 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11332 flags:
11334 1: one of the decl-specifiers is an elaborated-type-specifier
11335 (i.e., a type declaration)
11336 2: one of the decl-specifiers is an enum-specifier or a
11337 class-specifier (i.e., a type definition)
11341 static void
11342 cp_parser_decl_specifier_seq (cp_parser* parser,
11343 cp_parser_flags flags,
11344 cp_decl_specifier_seq *decl_specs,
11345 int* declares_class_or_enum)
11347 bool constructor_possible_p = !parser->in_declarator_p;
11348 bool found_decl_spec = false;
11349 cp_token *start_token = NULL;
11350 cp_decl_spec ds;
11352 /* Clear DECL_SPECS. */
11353 clear_decl_specs (decl_specs);
11355 /* Assume no class or enumeration type is declared. */
11356 *declares_class_or_enum = 0;
11358 /* Keep reading specifiers until there are no more to read. */
11359 while (true)
11361 bool constructor_p;
11362 cp_token *token;
11363 ds = ds_last;
11365 /* Peek at the next token. */
11366 token = cp_lexer_peek_token (parser->lexer);
11368 /* Save the first token of the decl spec list for error
11369 reporting. */
11370 if (!start_token)
11371 start_token = token;
11372 /* Handle attributes. */
11373 if (cp_next_tokens_can_be_attribute_p (parser))
11375 /* Parse the attributes. */
11376 tree attrs = cp_parser_attributes_opt (parser);
11378 /* In a sequence of declaration specifiers, c++11 attributes
11379 appertain to the type that precede them. In that case
11380 [dcl.spec]/1 says:
11382 The attribute-specifier-seq affects the type only for
11383 the declaration it appears in, not other declarations
11384 involving the same type.
11386 But for now let's force the user to position the
11387 attribute either at the beginning of the declaration or
11388 after the declarator-id, which would clearly mean that it
11389 applies to the declarator. */
11390 if (cxx11_attribute_p (attrs))
11392 if (!found_decl_spec)
11393 /* The c++11 attribute is at the beginning of the
11394 declaration. It appertains to the entity being
11395 declared. */;
11396 else
11398 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11400 /* This is an attribute following a
11401 class-specifier. */
11402 if (decl_specs->type_definition_p)
11403 warn_misplaced_attr_for_class_type (token->location,
11404 decl_specs->type);
11405 attrs = NULL_TREE;
11407 else
11409 decl_specs->std_attributes
11410 = chainon (decl_specs->std_attributes,
11411 attrs);
11412 if (decl_specs->locations[ds_std_attribute] == 0)
11413 decl_specs->locations[ds_std_attribute] = token->location;
11415 continue;
11419 decl_specs->attributes
11420 = chainon (decl_specs->attributes,
11421 attrs);
11422 if (decl_specs->locations[ds_attribute] == 0)
11423 decl_specs->locations[ds_attribute] = token->location;
11424 continue;
11426 /* Assume we will find a decl-specifier keyword. */
11427 found_decl_spec = true;
11428 /* If the next token is an appropriate keyword, we can simply
11429 add it to the list. */
11430 switch (token->keyword)
11432 /* decl-specifier:
11433 friend
11434 constexpr */
11435 case RID_FRIEND:
11436 if (!at_class_scope_p ())
11438 error_at (token->location, "%<friend%> used outside of class");
11439 cp_lexer_purge_token (parser->lexer);
11441 else
11443 ds = ds_friend;
11444 /* Consume the token. */
11445 cp_lexer_consume_token (parser->lexer);
11447 break;
11449 case RID_CONSTEXPR:
11450 ds = ds_constexpr;
11451 cp_lexer_consume_token (parser->lexer);
11452 break;
11454 /* function-specifier:
11455 inline
11456 virtual
11457 explicit */
11458 case RID_INLINE:
11459 case RID_VIRTUAL:
11460 case RID_EXPLICIT:
11461 cp_parser_function_specifier_opt (parser, decl_specs);
11462 break;
11464 /* decl-specifier:
11465 typedef */
11466 case RID_TYPEDEF:
11467 ds = ds_typedef;
11468 /* Consume the token. */
11469 cp_lexer_consume_token (parser->lexer);
11470 /* A constructor declarator cannot appear in a typedef. */
11471 constructor_possible_p = false;
11472 /* The "typedef" keyword can only occur in a declaration; we
11473 may as well commit at this point. */
11474 cp_parser_commit_to_tentative_parse (parser);
11476 if (decl_specs->storage_class != sc_none)
11477 decl_specs->conflicting_specifiers_p = true;
11478 break;
11480 /* storage-class-specifier:
11481 auto
11482 register
11483 static
11484 extern
11485 mutable
11487 GNU Extension:
11488 thread */
11489 case RID_AUTO:
11490 if (cxx_dialect == cxx98)
11492 /* Consume the token. */
11493 cp_lexer_consume_token (parser->lexer);
11495 /* Complain about `auto' as a storage specifier, if
11496 we're complaining about C++0x compatibility. */
11497 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11498 " changes meaning in C++11; please remove it");
11500 /* Set the storage class anyway. */
11501 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11502 token);
11504 else
11505 /* C++0x auto type-specifier. */
11506 found_decl_spec = false;
11507 break;
11509 case RID_REGISTER:
11510 case RID_STATIC:
11511 case RID_EXTERN:
11512 case RID_MUTABLE:
11513 /* Consume the token. */
11514 cp_lexer_consume_token (parser->lexer);
11515 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11516 token);
11517 break;
11518 case RID_THREAD:
11519 /* Consume the token. */
11520 ds = ds_thread;
11521 cp_lexer_consume_token (parser->lexer);
11522 break;
11524 default:
11525 /* We did not yet find a decl-specifier yet. */
11526 found_decl_spec = false;
11527 break;
11530 if (found_decl_spec
11531 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11532 && token->keyword != RID_CONSTEXPR)
11533 error ("decl-specifier invalid in condition");
11535 if (ds != ds_last)
11536 set_and_check_decl_spec_loc (decl_specs, ds, token);
11538 /* Constructors are a special case. The `S' in `S()' is not a
11539 decl-specifier; it is the beginning of the declarator. */
11540 constructor_p
11541 = (!found_decl_spec
11542 && constructor_possible_p
11543 && (cp_parser_constructor_declarator_p
11544 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11546 /* If we don't have a DECL_SPEC yet, then we must be looking at
11547 a type-specifier. */
11548 if (!found_decl_spec && !constructor_p)
11550 int decl_spec_declares_class_or_enum;
11551 bool is_cv_qualifier;
11552 tree type_spec;
11554 type_spec
11555 = cp_parser_type_specifier (parser, flags,
11556 decl_specs,
11557 /*is_declaration=*/true,
11558 &decl_spec_declares_class_or_enum,
11559 &is_cv_qualifier);
11560 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11562 /* If this type-specifier referenced a user-defined type
11563 (a typedef, class-name, etc.), then we can't allow any
11564 more such type-specifiers henceforth.
11566 [dcl.spec]
11568 The longest sequence of decl-specifiers that could
11569 possibly be a type name is taken as the
11570 decl-specifier-seq of a declaration. The sequence shall
11571 be self-consistent as described below.
11573 [dcl.type]
11575 As a general rule, at most one type-specifier is allowed
11576 in the complete decl-specifier-seq of a declaration. The
11577 only exceptions are the following:
11579 -- const or volatile can be combined with any other
11580 type-specifier.
11582 -- signed or unsigned can be combined with char, long,
11583 short, or int.
11585 -- ..
11587 Example:
11589 typedef char* Pc;
11590 void g (const int Pc);
11592 Here, Pc is *not* part of the decl-specifier seq; it's
11593 the declarator. Therefore, once we see a type-specifier
11594 (other than a cv-qualifier), we forbid any additional
11595 user-defined types. We *do* still allow things like `int
11596 int' to be considered a decl-specifier-seq, and issue the
11597 error message later. */
11598 if (type_spec && !is_cv_qualifier)
11599 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11600 /* A constructor declarator cannot follow a type-specifier. */
11601 if (type_spec)
11603 constructor_possible_p = false;
11604 found_decl_spec = true;
11605 if (!is_cv_qualifier)
11606 decl_specs->any_type_specifiers_p = true;
11610 /* If we still do not have a DECL_SPEC, then there are no more
11611 decl-specifiers. */
11612 if (!found_decl_spec)
11613 break;
11615 decl_specs->any_specifiers_p = true;
11616 /* After we see one decl-specifier, further decl-specifiers are
11617 always optional. */
11618 flags |= CP_PARSER_FLAGS_OPTIONAL;
11621 /* Don't allow a friend specifier with a class definition. */
11622 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11623 && (*declares_class_or_enum & 2))
11624 error_at (decl_specs->locations[ds_friend],
11625 "class definition may not be declared a friend");
11628 /* Parse an (optional) storage-class-specifier.
11630 storage-class-specifier:
11631 auto
11632 register
11633 static
11634 extern
11635 mutable
11637 GNU Extension:
11639 storage-class-specifier:
11640 thread
11642 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11644 static tree
11645 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11647 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11649 case RID_AUTO:
11650 if (cxx_dialect != cxx98)
11651 return NULL_TREE;
11652 /* Fall through for C++98. */
11654 case RID_REGISTER:
11655 case RID_STATIC:
11656 case RID_EXTERN:
11657 case RID_MUTABLE:
11658 case RID_THREAD:
11659 /* Consume the token. */
11660 return cp_lexer_consume_token (parser->lexer)->u.value;
11662 default:
11663 return NULL_TREE;
11667 /* Parse an (optional) function-specifier.
11669 function-specifier:
11670 inline
11671 virtual
11672 explicit
11674 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11675 Updates DECL_SPECS, if it is non-NULL. */
11677 static tree
11678 cp_parser_function_specifier_opt (cp_parser* parser,
11679 cp_decl_specifier_seq *decl_specs)
11681 cp_token *token = cp_lexer_peek_token (parser->lexer);
11682 switch (token->keyword)
11684 case RID_INLINE:
11685 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11686 break;
11688 case RID_VIRTUAL:
11689 /* 14.5.2.3 [temp.mem]
11691 A member function template shall not be virtual. */
11692 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11693 error_at (token->location, "templates may not be %<virtual%>");
11694 else
11695 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11696 break;
11698 case RID_EXPLICIT:
11699 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11700 break;
11702 default:
11703 return NULL_TREE;
11706 /* Consume the token. */
11707 return cp_lexer_consume_token (parser->lexer)->u.value;
11710 /* Parse a linkage-specification.
11712 linkage-specification:
11713 extern string-literal { declaration-seq [opt] }
11714 extern string-literal declaration */
11716 static void
11717 cp_parser_linkage_specification (cp_parser* parser)
11719 tree linkage;
11721 /* Look for the `extern' keyword. */
11722 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11724 /* Look for the string-literal. */
11725 linkage = cp_parser_string_literal (parser, false, false);
11727 /* Transform the literal into an identifier. If the literal is a
11728 wide-character string, or contains embedded NULs, then we can't
11729 handle it as the user wants. */
11730 if (strlen (TREE_STRING_POINTER (linkage))
11731 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11733 cp_parser_error (parser, "invalid linkage-specification");
11734 /* Assume C++ linkage. */
11735 linkage = lang_name_cplusplus;
11737 else
11738 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11740 /* We're now using the new linkage. */
11741 push_lang_context (linkage);
11743 /* If the next token is a `{', then we're using the first
11744 production. */
11745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11747 cp_ensure_no_omp_declare_simd (parser);
11749 /* Consume the `{' token. */
11750 cp_lexer_consume_token (parser->lexer);
11751 /* Parse the declarations. */
11752 cp_parser_declaration_seq_opt (parser);
11753 /* Look for the closing `}'. */
11754 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11756 /* Otherwise, there's just one declaration. */
11757 else
11759 bool saved_in_unbraced_linkage_specification_p;
11761 saved_in_unbraced_linkage_specification_p
11762 = parser->in_unbraced_linkage_specification_p;
11763 parser->in_unbraced_linkage_specification_p = true;
11764 cp_parser_declaration (parser);
11765 parser->in_unbraced_linkage_specification_p
11766 = saved_in_unbraced_linkage_specification_p;
11769 /* We're done with the linkage-specification. */
11770 pop_lang_context ();
11773 /* Parse a static_assert-declaration.
11775 static_assert-declaration:
11776 static_assert ( constant-expression , string-literal ) ;
11778 If MEMBER_P, this static_assert is a class member. */
11780 static void
11781 cp_parser_static_assert(cp_parser *parser, bool member_p)
11783 tree condition;
11784 tree message;
11785 cp_token *token;
11786 location_t saved_loc;
11787 bool dummy;
11789 /* Peek at the `static_assert' token so we can keep track of exactly
11790 where the static assertion started. */
11791 token = cp_lexer_peek_token (parser->lexer);
11792 saved_loc = token->location;
11794 /* Look for the `static_assert' keyword. */
11795 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11796 RT_STATIC_ASSERT))
11797 return;
11799 /* We know we are in a static assertion; commit to any tentative
11800 parse. */
11801 if (cp_parser_parsing_tentatively (parser))
11802 cp_parser_commit_to_tentative_parse (parser);
11804 /* Parse the `(' starting the static assertion condition. */
11805 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11807 /* Parse the constant-expression. Allow a non-constant expression
11808 here in order to give better diagnostics in finish_static_assert. */
11809 condition =
11810 cp_parser_constant_expression (parser,
11811 /*allow_non_constant_p=*/true,
11812 /*non_constant_p=*/&dummy);
11814 /* Parse the separating `,'. */
11815 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11817 /* Parse the string-literal message. */
11818 message = cp_parser_string_literal (parser,
11819 /*translate=*/false,
11820 /*wide_ok=*/true);
11822 /* A `)' completes the static assertion. */
11823 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11824 cp_parser_skip_to_closing_parenthesis (parser,
11825 /*recovering=*/true,
11826 /*or_comma=*/false,
11827 /*consume_paren=*/true);
11829 /* A semicolon terminates the declaration. */
11830 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11832 /* Complete the static assertion, which may mean either processing
11833 the static assert now or saving it for template instantiation. */
11834 finish_static_assert (condition, message, saved_loc, member_p);
11837 /* Parse the expression in decltype ( expression ). */
11839 static tree
11840 cp_parser_decltype_expr (cp_parser *parser,
11841 bool &id_expression_or_member_access_p)
11843 cp_token *id_expr_start_token;
11844 tree expr;
11846 /* First, try parsing an id-expression. */
11847 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11848 cp_parser_parse_tentatively (parser);
11849 expr = cp_parser_id_expression (parser,
11850 /*template_keyword_p=*/false,
11851 /*check_dependency_p=*/true,
11852 /*template_p=*/NULL,
11853 /*declarator_p=*/false,
11854 /*optional_p=*/false);
11856 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11858 bool non_integral_constant_expression_p = false;
11859 tree id_expression = expr;
11860 cp_id_kind idk;
11861 const char *error_msg;
11863 if (identifier_p (expr))
11864 /* Lookup the name we got back from the id-expression. */
11865 expr = cp_parser_lookup_name_simple (parser, expr,
11866 id_expr_start_token->location);
11868 if (expr
11869 && expr != error_mark_node
11870 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11871 && TREE_CODE (expr) != TYPE_DECL
11872 && (TREE_CODE (expr) != BIT_NOT_EXPR
11873 || !TYPE_P (TREE_OPERAND (expr, 0)))
11874 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11876 /* Complete lookup of the id-expression. */
11877 expr = (finish_id_expression
11878 (id_expression, expr, parser->scope, &idk,
11879 /*integral_constant_expression_p=*/false,
11880 /*allow_non_integral_constant_expression_p=*/true,
11881 &non_integral_constant_expression_p,
11882 /*template_p=*/false,
11883 /*done=*/true,
11884 /*address_p=*/false,
11885 /*template_arg_p=*/false,
11886 &error_msg,
11887 id_expr_start_token->location));
11889 if (expr == error_mark_node)
11890 /* We found an id-expression, but it was something that we
11891 should not have found. This is an error, not something
11892 we can recover from, so note that we found an
11893 id-expression and we'll recover as gracefully as
11894 possible. */
11895 id_expression_or_member_access_p = true;
11898 if (expr
11899 && expr != error_mark_node
11900 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11901 /* We have an id-expression. */
11902 id_expression_or_member_access_p = true;
11905 if (!id_expression_or_member_access_p)
11907 /* Abort the id-expression parse. */
11908 cp_parser_abort_tentative_parse (parser);
11910 /* Parsing tentatively, again. */
11911 cp_parser_parse_tentatively (parser);
11913 /* Parse a class member access. */
11914 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11915 /*cast_p=*/false, /*decltype*/true,
11916 /*member_access_only_p=*/true, NULL);
11918 if (expr
11919 && expr != error_mark_node
11920 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11921 /* We have an id-expression. */
11922 id_expression_or_member_access_p = true;
11925 if (id_expression_or_member_access_p)
11926 /* We have parsed the complete id-expression or member access. */
11927 cp_parser_parse_definitely (parser);
11928 else
11930 /* Abort our attempt to parse an id-expression or member access
11931 expression. */
11932 cp_parser_abort_tentative_parse (parser);
11934 /* Parse a full expression. */
11935 expr = cp_parser_expression (parser, /*cast_p=*/false,
11936 /*decltype*/true, NULL);
11939 return expr;
11942 /* Parse a `decltype' type. Returns the type.
11944 simple-type-specifier:
11945 decltype ( expression )
11946 C++14 proposal:
11947 decltype ( auto ) */
11949 static tree
11950 cp_parser_decltype (cp_parser *parser)
11952 tree expr;
11953 bool id_expression_or_member_access_p = false;
11954 const char *saved_message;
11955 bool saved_integral_constant_expression_p;
11956 bool saved_non_integral_constant_expression_p;
11957 bool saved_greater_than_is_operator_p;
11958 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11960 if (start_token->type == CPP_DECLTYPE)
11962 /* Already parsed. */
11963 cp_lexer_consume_token (parser->lexer);
11964 return start_token->u.value;
11967 /* Look for the `decltype' token. */
11968 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11969 return error_mark_node;
11971 /* Parse the opening `('. */
11972 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11973 return error_mark_node;
11975 /* decltype (auto) */
11976 if (cxx_dialect >= cxx1y
11977 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11979 cp_lexer_consume_token (parser->lexer);
11980 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11981 return error_mark_node;
11982 expr = make_decltype_auto ();
11983 AUTO_IS_DECLTYPE (expr) = true;
11984 goto rewrite;
11987 /* Types cannot be defined in a `decltype' expression. Save away the
11988 old message. */
11989 saved_message = parser->type_definition_forbidden_message;
11991 /* And create the new one. */
11992 parser->type_definition_forbidden_message
11993 = G_("types may not be defined in %<decltype%> expressions");
11995 /* The restrictions on constant-expressions do not apply inside
11996 decltype expressions. */
11997 saved_integral_constant_expression_p
11998 = parser->integral_constant_expression_p;
11999 saved_non_integral_constant_expression_p
12000 = parser->non_integral_constant_expression_p;
12001 parser->integral_constant_expression_p = false;
12003 /* Within a parenthesized expression, a `>' token is always
12004 the greater-than operator. */
12005 saved_greater_than_is_operator_p
12006 = parser->greater_than_is_operator_p;
12007 parser->greater_than_is_operator_p = true;
12009 /* Do not actually evaluate the expression. */
12010 ++cp_unevaluated_operand;
12012 /* Do not warn about problems with the expression. */
12013 ++c_inhibit_evaluation_warnings;
12015 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12017 /* Go back to evaluating expressions. */
12018 --cp_unevaluated_operand;
12019 --c_inhibit_evaluation_warnings;
12021 /* The `>' token might be the end of a template-id or
12022 template-parameter-list now. */
12023 parser->greater_than_is_operator_p
12024 = saved_greater_than_is_operator_p;
12026 /* Restore the old message and the integral constant expression
12027 flags. */
12028 parser->type_definition_forbidden_message = saved_message;
12029 parser->integral_constant_expression_p
12030 = saved_integral_constant_expression_p;
12031 parser->non_integral_constant_expression_p
12032 = saved_non_integral_constant_expression_p;
12034 /* Parse to the closing `)'. */
12035 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12037 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12038 /*consume_paren=*/true);
12039 return error_mark_node;
12042 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12043 tf_warning_or_error);
12045 rewrite:
12046 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12047 it again. */
12048 start_token->type = CPP_DECLTYPE;
12049 start_token->u.value = expr;
12050 start_token->keyword = RID_MAX;
12051 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12053 return expr;
12056 /* Special member functions [gram.special] */
12058 /* Parse a conversion-function-id.
12060 conversion-function-id:
12061 operator conversion-type-id
12063 Returns an IDENTIFIER_NODE representing the operator. */
12065 static tree
12066 cp_parser_conversion_function_id (cp_parser* parser)
12068 tree type;
12069 tree saved_scope;
12070 tree saved_qualifying_scope;
12071 tree saved_object_scope;
12072 tree pushed_scope = NULL_TREE;
12074 /* Look for the `operator' token. */
12075 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12076 return error_mark_node;
12077 /* When we parse the conversion-type-id, the current scope will be
12078 reset. However, we need that information in able to look up the
12079 conversion function later, so we save it here. */
12080 saved_scope = parser->scope;
12081 saved_qualifying_scope = parser->qualifying_scope;
12082 saved_object_scope = parser->object_scope;
12083 /* We must enter the scope of the class so that the names of
12084 entities declared within the class are available in the
12085 conversion-type-id. For example, consider:
12087 struct S {
12088 typedef int I;
12089 operator I();
12092 S::operator I() { ... }
12094 In order to see that `I' is a type-name in the definition, we
12095 must be in the scope of `S'. */
12096 if (saved_scope)
12097 pushed_scope = push_scope (saved_scope);
12098 /* Parse the conversion-type-id. */
12099 type = cp_parser_conversion_type_id (parser);
12100 /* Leave the scope of the class, if any. */
12101 if (pushed_scope)
12102 pop_scope (pushed_scope);
12103 /* Restore the saved scope. */
12104 parser->scope = saved_scope;
12105 parser->qualifying_scope = saved_qualifying_scope;
12106 parser->object_scope = saved_object_scope;
12107 /* If the TYPE is invalid, indicate failure. */
12108 if (type == error_mark_node)
12109 return error_mark_node;
12110 return mangle_conv_op_name_for_type (type);
12113 /* Parse a conversion-type-id:
12115 conversion-type-id:
12116 type-specifier-seq conversion-declarator [opt]
12118 Returns the TYPE specified. */
12120 static tree
12121 cp_parser_conversion_type_id (cp_parser* parser)
12123 tree attributes;
12124 cp_decl_specifier_seq type_specifiers;
12125 cp_declarator *declarator;
12126 tree type_specified;
12127 const char *saved_message;
12129 /* Parse the attributes. */
12130 attributes = cp_parser_attributes_opt (parser);
12132 saved_message = parser->type_definition_forbidden_message;
12133 parser->type_definition_forbidden_message
12134 = G_("types may not be defined in a conversion-type-id");
12136 /* Parse the type-specifiers. */
12137 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12138 /*is_trailing_return=*/false,
12139 &type_specifiers);
12141 parser->type_definition_forbidden_message = saved_message;
12143 /* If that didn't work, stop. */
12144 if (type_specifiers.type == error_mark_node)
12145 return error_mark_node;
12146 /* Parse the conversion-declarator. */
12147 declarator = cp_parser_conversion_declarator_opt (parser);
12149 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12150 /*initialized=*/0, &attributes);
12151 if (attributes)
12152 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12154 /* Don't give this error when parsing tentatively. This happens to
12155 work because we always parse this definitively once. */
12156 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12157 && type_uses_auto (type_specified))
12159 if (cxx_dialect < cxx1y)
12161 error ("invalid use of %<auto%> in conversion operator");
12162 return error_mark_node;
12164 else if (template_parm_scope_p ())
12165 warning (0, "use of %<auto%> in member template "
12166 "conversion operator can never be deduced");
12169 return type_specified;
12172 /* Parse an (optional) conversion-declarator.
12174 conversion-declarator:
12175 ptr-operator conversion-declarator [opt]
12179 static cp_declarator *
12180 cp_parser_conversion_declarator_opt (cp_parser* parser)
12182 enum tree_code code;
12183 tree class_type, std_attributes = NULL_TREE;
12184 cp_cv_quals cv_quals;
12186 /* We don't know if there's a ptr-operator next, or not. */
12187 cp_parser_parse_tentatively (parser);
12188 /* Try the ptr-operator. */
12189 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12190 &std_attributes);
12191 /* If it worked, look for more conversion-declarators. */
12192 if (cp_parser_parse_definitely (parser))
12194 cp_declarator *declarator;
12196 /* Parse another optional declarator. */
12197 declarator = cp_parser_conversion_declarator_opt (parser);
12199 declarator = cp_parser_make_indirect_declarator
12200 (code, class_type, cv_quals, declarator, std_attributes);
12202 return declarator;
12205 return NULL;
12208 /* Parse an (optional) ctor-initializer.
12210 ctor-initializer:
12211 : mem-initializer-list
12213 Returns TRUE iff the ctor-initializer was actually present. */
12215 static bool
12216 cp_parser_ctor_initializer_opt (cp_parser* parser)
12218 /* If the next token is not a `:', then there is no
12219 ctor-initializer. */
12220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12222 /* Do default initialization of any bases and members. */
12223 if (DECL_CONSTRUCTOR_P (current_function_decl))
12224 finish_mem_initializers (NULL_TREE);
12226 return false;
12229 /* Consume the `:' token. */
12230 cp_lexer_consume_token (parser->lexer);
12231 /* And the mem-initializer-list. */
12232 cp_parser_mem_initializer_list (parser);
12234 return true;
12237 /* Parse a mem-initializer-list.
12239 mem-initializer-list:
12240 mem-initializer ... [opt]
12241 mem-initializer ... [opt] , mem-initializer-list */
12243 static void
12244 cp_parser_mem_initializer_list (cp_parser* parser)
12246 tree mem_initializer_list = NULL_TREE;
12247 tree target_ctor = error_mark_node;
12248 cp_token *token = cp_lexer_peek_token (parser->lexer);
12250 /* Let the semantic analysis code know that we are starting the
12251 mem-initializer-list. */
12252 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12253 error_at (token->location,
12254 "only constructors take member initializers");
12256 /* Loop through the list. */
12257 while (true)
12259 tree mem_initializer;
12261 token = cp_lexer_peek_token (parser->lexer);
12262 /* Parse the mem-initializer. */
12263 mem_initializer = cp_parser_mem_initializer (parser);
12264 /* If the next token is a `...', we're expanding member initializers. */
12265 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12267 /* Consume the `...'. */
12268 cp_lexer_consume_token (parser->lexer);
12270 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12271 can be expanded but members cannot. */
12272 if (mem_initializer != error_mark_node
12273 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12275 error_at (token->location,
12276 "cannot expand initializer for member %<%D%>",
12277 TREE_PURPOSE (mem_initializer));
12278 mem_initializer = error_mark_node;
12281 /* Construct the pack expansion type. */
12282 if (mem_initializer != error_mark_node)
12283 mem_initializer = make_pack_expansion (mem_initializer);
12285 if (target_ctor != error_mark_node
12286 && mem_initializer != error_mark_node)
12288 error ("mem-initializer for %qD follows constructor delegation",
12289 TREE_PURPOSE (mem_initializer));
12290 mem_initializer = error_mark_node;
12292 /* Look for a target constructor. */
12293 if (mem_initializer != error_mark_node
12294 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12295 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12297 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12298 if (mem_initializer_list)
12300 error ("constructor delegation follows mem-initializer for %qD",
12301 TREE_PURPOSE (mem_initializer_list));
12302 mem_initializer = error_mark_node;
12304 target_ctor = mem_initializer;
12306 /* Add it to the list, unless it was erroneous. */
12307 if (mem_initializer != error_mark_node)
12309 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12310 mem_initializer_list = mem_initializer;
12312 /* If the next token is not a `,', we're done. */
12313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12314 break;
12315 /* Consume the `,' token. */
12316 cp_lexer_consume_token (parser->lexer);
12319 /* Perform semantic analysis. */
12320 if (DECL_CONSTRUCTOR_P (current_function_decl))
12321 finish_mem_initializers (mem_initializer_list);
12324 /* Parse a mem-initializer.
12326 mem-initializer:
12327 mem-initializer-id ( expression-list [opt] )
12328 mem-initializer-id braced-init-list
12330 GNU extension:
12332 mem-initializer:
12333 ( expression-list [opt] )
12335 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12336 class) or FIELD_DECL (for a non-static data member) to initialize;
12337 the TREE_VALUE is the expression-list. An empty initialization
12338 list is represented by void_list_node. */
12340 static tree
12341 cp_parser_mem_initializer (cp_parser* parser)
12343 tree mem_initializer_id;
12344 tree expression_list;
12345 tree member;
12346 cp_token *token = cp_lexer_peek_token (parser->lexer);
12348 /* Find out what is being initialized. */
12349 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12351 permerror (token->location,
12352 "anachronistic old-style base class initializer");
12353 mem_initializer_id = NULL_TREE;
12355 else
12357 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12358 if (mem_initializer_id == error_mark_node)
12359 return mem_initializer_id;
12361 member = expand_member_init (mem_initializer_id);
12362 if (member && !DECL_P (member))
12363 in_base_initializer = 1;
12365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12367 bool expr_non_constant_p;
12368 cp_lexer_set_source_position (parser->lexer);
12369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12370 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12371 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12372 expression_list = build_tree_list (NULL_TREE, expression_list);
12374 else
12376 vec<tree, va_gc> *vec;
12377 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12378 /*cast_p=*/false,
12379 /*allow_expansion_p=*/true,
12380 /*non_constant_p=*/NULL);
12381 if (vec == NULL)
12382 return error_mark_node;
12383 expression_list = build_tree_list_vec (vec);
12384 release_tree_vector (vec);
12387 if (expression_list == error_mark_node)
12388 return error_mark_node;
12389 if (!expression_list)
12390 expression_list = void_type_node;
12392 in_base_initializer = 0;
12394 return member ? build_tree_list (member, expression_list) : error_mark_node;
12397 /* Parse a mem-initializer-id.
12399 mem-initializer-id:
12400 :: [opt] nested-name-specifier [opt] class-name
12401 identifier
12403 Returns a TYPE indicating the class to be initializer for the first
12404 production. Returns an IDENTIFIER_NODE indicating the data member
12405 to be initialized for the second production. */
12407 static tree
12408 cp_parser_mem_initializer_id (cp_parser* parser)
12410 bool global_scope_p;
12411 bool nested_name_specifier_p;
12412 bool template_p = false;
12413 tree id;
12415 cp_token *token = cp_lexer_peek_token (parser->lexer);
12417 /* `typename' is not allowed in this context ([temp.res]). */
12418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12420 error_at (token->location,
12421 "keyword %<typename%> not allowed in this context (a qualified "
12422 "member initializer is implicitly a type)");
12423 cp_lexer_consume_token (parser->lexer);
12425 /* Look for the optional `::' operator. */
12426 global_scope_p
12427 = (cp_parser_global_scope_opt (parser,
12428 /*current_scope_valid_p=*/false)
12429 != NULL_TREE);
12430 /* Look for the optional nested-name-specifier. The simplest way to
12431 implement:
12433 [temp.res]
12435 The keyword `typename' is not permitted in a base-specifier or
12436 mem-initializer; in these contexts a qualified name that
12437 depends on a template-parameter is implicitly assumed to be a
12438 type name.
12440 is to assume that we have seen the `typename' keyword at this
12441 point. */
12442 nested_name_specifier_p
12443 = (cp_parser_nested_name_specifier_opt (parser,
12444 /*typename_keyword_p=*/true,
12445 /*check_dependency_p=*/true,
12446 /*type_p=*/true,
12447 /*is_declaration=*/true)
12448 != NULL_TREE);
12449 if (nested_name_specifier_p)
12450 template_p = cp_parser_optional_template_keyword (parser);
12451 /* If there is a `::' operator or a nested-name-specifier, then we
12452 are definitely looking for a class-name. */
12453 if (global_scope_p || nested_name_specifier_p)
12454 return cp_parser_class_name (parser,
12455 /*typename_keyword_p=*/true,
12456 /*template_keyword_p=*/template_p,
12457 typename_type,
12458 /*check_dependency_p=*/true,
12459 /*class_head_p=*/false,
12460 /*is_declaration=*/true);
12461 /* Otherwise, we could also be looking for an ordinary identifier. */
12462 cp_parser_parse_tentatively (parser);
12463 /* Try a class-name. */
12464 id = cp_parser_class_name (parser,
12465 /*typename_keyword_p=*/true,
12466 /*template_keyword_p=*/false,
12467 none_type,
12468 /*check_dependency_p=*/true,
12469 /*class_head_p=*/false,
12470 /*is_declaration=*/true);
12471 /* If we found one, we're done. */
12472 if (cp_parser_parse_definitely (parser))
12473 return id;
12474 /* Otherwise, look for an ordinary identifier. */
12475 return cp_parser_identifier (parser);
12478 /* Overloading [gram.over] */
12480 /* Parse an operator-function-id.
12482 operator-function-id:
12483 operator operator
12485 Returns an IDENTIFIER_NODE for the operator which is a
12486 human-readable spelling of the identifier, e.g., `operator +'. */
12488 static tree
12489 cp_parser_operator_function_id (cp_parser* parser)
12491 /* Look for the `operator' keyword. */
12492 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12493 return error_mark_node;
12494 /* And then the name of the operator itself. */
12495 return cp_parser_operator (parser);
12498 /* Return an identifier node for a user-defined literal operator.
12499 The suffix identifier is chained to the operator name identifier. */
12501 static tree
12502 cp_literal_operator_id (const char* name)
12504 tree identifier;
12505 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12506 + strlen (name) + 10);
12507 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12508 identifier = get_identifier (buffer);
12510 return identifier;
12513 /* Parse an operator.
12515 operator:
12516 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12517 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12518 || ++ -- , ->* -> () []
12520 GNU Extensions:
12522 operator:
12523 <? >? <?= >?=
12525 Returns an IDENTIFIER_NODE for the operator which is a
12526 human-readable spelling of the identifier, e.g., `operator +'. */
12528 static tree
12529 cp_parser_operator (cp_parser* parser)
12531 tree id = NULL_TREE;
12532 cp_token *token;
12533 bool bad_encoding_prefix = false;
12535 /* Peek at the next token. */
12536 token = cp_lexer_peek_token (parser->lexer);
12537 /* Figure out which operator we have. */
12538 switch (token->type)
12540 case CPP_KEYWORD:
12542 enum tree_code op;
12544 /* The keyword should be either `new' or `delete'. */
12545 if (token->keyword == RID_NEW)
12546 op = NEW_EXPR;
12547 else if (token->keyword == RID_DELETE)
12548 op = DELETE_EXPR;
12549 else
12550 break;
12552 /* Consume the `new' or `delete' token. */
12553 cp_lexer_consume_token (parser->lexer);
12555 /* Peek at the next token. */
12556 token = cp_lexer_peek_token (parser->lexer);
12557 /* If it's a `[' token then this is the array variant of the
12558 operator. */
12559 if (token->type == CPP_OPEN_SQUARE)
12561 /* Consume the `[' token. */
12562 cp_lexer_consume_token (parser->lexer);
12563 /* Look for the `]' token. */
12564 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12565 id = ansi_opname (op == NEW_EXPR
12566 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12568 /* Otherwise, we have the non-array variant. */
12569 else
12570 id = ansi_opname (op);
12572 return id;
12575 case CPP_PLUS:
12576 id = ansi_opname (PLUS_EXPR);
12577 break;
12579 case CPP_MINUS:
12580 id = ansi_opname (MINUS_EXPR);
12581 break;
12583 case CPP_MULT:
12584 id = ansi_opname (MULT_EXPR);
12585 break;
12587 case CPP_DIV:
12588 id = ansi_opname (TRUNC_DIV_EXPR);
12589 break;
12591 case CPP_MOD:
12592 id = ansi_opname (TRUNC_MOD_EXPR);
12593 break;
12595 case CPP_XOR:
12596 id = ansi_opname (BIT_XOR_EXPR);
12597 break;
12599 case CPP_AND:
12600 id = ansi_opname (BIT_AND_EXPR);
12601 break;
12603 case CPP_OR:
12604 id = ansi_opname (BIT_IOR_EXPR);
12605 break;
12607 case CPP_COMPL:
12608 id = ansi_opname (BIT_NOT_EXPR);
12609 break;
12611 case CPP_NOT:
12612 id = ansi_opname (TRUTH_NOT_EXPR);
12613 break;
12615 case CPP_EQ:
12616 id = ansi_assopname (NOP_EXPR);
12617 break;
12619 case CPP_LESS:
12620 id = ansi_opname (LT_EXPR);
12621 break;
12623 case CPP_GREATER:
12624 id = ansi_opname (GT_EXPR);
12625 break;
12627 case CPP_PLUS_EQ:
12628 id = ansi_assopname (PLUS_EXPR);
12629 break;
12631 case CPP_MINUS_EQ:
12632 id = ansi_assopname (MINUS_EXPR);
12633 break;
12635 case CPP_MULT_EQ:
12636 id = ansi_assopname (MULT_EXPR);
12637 break;
12639 case CPP_DIV_EQ:
12640 id = ansi_assopname (TRUNC_DIV_EXPR);
12641 break;
12643 case CPP_MOD_EQ:
12644 id = ansi_assopname (TRUNC_MOD_EXPR);
12645 break;
12647 case CPP_XOR_EQ:
12648 id = ansi_assopname (BIT_XOR_EXPR);
12649 break;
12651 case CPP_AND_EQ:
12652 id = ansi_assopname (BIT_AND_EXPR);
12653 break;
12655 case CPP_OR_EQ:
12656 id = ansi_assopname (BIT_IOR_EXPR);
12657 break;
12659 case CPP_LSHIFT:
12660 id = ansi_opname (LSHIFT_EXPR);
12661 break;
12663 case CPP_RSHIFT:
12664 id = ansi_opname (RSHIFT_EXPR);
12665 break;
12667 case CPP_LSHIFT_EQ:
12668 id = ansi_assopname (LSHIFT_EXPR);
12669 break;
12671 case CPP_RSHIFT_EQ:
12672 id = ansi_assopname (RSHIFT_EXPR);
12673 break;
12675 case CPP_EQ_EQ:
12676 id = ansi_opname (EQ_EXPR);
12677 break;
12679 case CPP_NOT_EQ:
12680 id = ansi_opname (NE_EXPR);
12681 break;
12683 case CPP_LESS_EQ:
12684 id = ansi_opname (LE_EXPR);
12685 break;
12687 case CPP_GREATER_EQ:
12688 id = ansi_opname (GE_EXPR);
12689 break;
12691 case CPP_AND_AND:
12692 id = ansi_opname (TRUTH_ANDIF_EXPR);
12693 break;
12695 case CPP_OR_OR:
12696 id = ansi_opname (TRUTH_ORIF_EXPR);
12697 break;
12699 case CPP_PLUS_PLUS:
12700 id = ansi_opname (POSTINCREMENT_EXPR);
12701 break;
12703 case CPP_MINUS_MINUS:
12704 id = ansi_opname (PREDECREMENT_EXPR);
12705 break;
12707 case CPP_COMMA:
12708 id = ansi_opname (COMPOUND_EXPR);
12709 break;
12711 case CPP_DEREF_STAR:
12712 id = ansi_opname (MEMBER_REF);
12713 break;
12715 case CPP_DEREF:
12716 id = ansi_opname (COMPONENT_REF);
12717 break;
12719 case CPP_OPEN_PAREN:
12720 /* Consume the `('. */
12721 cp_lexer_consume_token (parser->lexer);
12722 /* Look for the matching `)'. */
12723 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12724 return ansi_opname (CALL_EXPR);
12726 case CPP_OPEN_SQUARE:
12727 /* Consume the `['. */
12728 cp_lexer_consume_token (parser->lexer);
12729 /* Look for the matching `]'. */
12730 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12731 return ansi_opname (ARRAY_REF);
12733 case CPP_WSTRING:
12734 case CPP_STRING16:
12735 case CPP_STRING32:
12736 case CPP_UTF8STRING:
12737 bad_encoding_prefix = true;
12738 /* Fall through. */
12740 case CPP_STRING:
12741 if (cxx_dialect == cxx98)
12742 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12743 if (bad_encoding_prefix)
12745 error ("invalid encoding prefix in literal operator");
12746 return error_mark_node;
12748 if (TREE_STRING_LENGTH (token->u.value) > 2)
12750 error ("expected empty string after %<operator%> keyword");
12751 return error_mark_node;
12753 /* Consume the string. */
12754 cp_lexer_consume_token (parser->lexer);
12755 /* Look for the suffix identifier. */
12756 token = cp_lexer_peek_token (parser->lexer);
12757 if (token->type == CPP_NAME)
12759 id = cp_parser_identifier (parser);
12760 if (id != error_mark_node)
12762 const char *name = IDENTIFIER_POINTER (id);
12763 return cp_literal_operator_id (name);
12766 else if (token->type == CPP_KEYWORD)
12768 error ("unexpected keyword;"
12769 " remove space between quotes and suffix identifier");
12770 return error_mark_node;
12772 else
12774 error ("expected suffix identifier");
12775 return error_mark_node;
12778 case CPP_WSTRING_USERDEF:
12779 case CPP_STRING16_USERDEF:
12780 case CPP_STRING32_USERDEF:
12781 case CPP_UTF8STRING_USERDEF:
12782 bad_encoding_prefix = true;
12783 /* Fall through. */
12785 case CPP_STRING_USERDEF:
12786 if (cxx_dialect == cxx98)
12787 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12788 if (bad_encoding_prefix)
12790 error ("invalid encoding prefix in literal operator");
12791 return error_mark_node;
12794 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12795 if (TREE_STRING_LENGTH (string_tree) > 2)
12797 error ("expected empty string after %<operator%> keyword");
12798 return error_mark_node;
12800 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12801 /* Consume the user-defined string literal. */
12802 cp_lexer_consume_token (parser->lexer);
12803 if (id != error_mark_node)
12805 const char *name = IDENTIFIER_POINTER (id);
12806 return cp_literal_operator_id (name);
12808 else
12809 return error_mark_node;
12812 default:
12813 /* Anything else is an error. */
12814 break;
12817 /* If we have selected an identifier, we need to consume the
12818 operator token. */
12819 if (id)
12820 cp_lexer_consume_token (parser->lexer);
12821 /* Otherwise, no valid operator name was present. */
12822 else
12824 cp_parser_error (parser, "expected operator");
12825 id = error_mark_node;
12828 return id;
12831 /* Parse a template-declaration.
12833 template-declaration:
12834 export [opt] template < template-parameter-list > declaration
12836 If MEMBER_P is TRUE, this template-declaration occurs within a
12837 class-specifier.
12839 The grammar rule given by the standard isn't correct. What
12840 is really meant is:
12842 template-declaration:
12843 export [opt] template-parameter-list-seq
12844 decl-specifier-seq [opt] init-declarator [opt] ;
12845 export [opt] template-parameter-list-seq
12846 function-definition
12848 template-parameter-list-seq:
12849 template-parameter-list-seq [opt]
12850 template < template-parameter-list > */
12852 static void
12853 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12855 /* Check for `export'. */
12856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12858 /* Consume the `export' token. */
12859 cp_lexer_consume_token (parser->lexer);
12860 /* Warn that we do not support `export'. */
12861 warning (0, "keyword %<export%> not implemented, and will be ignored");
12864 cp_parser_template_declaration_after_export (parser, member_p);
12867 /* Parse a template-parameter-list.
12869 template-parameter-list:
12870 template-parameter
12871 template-parameter-list , template-parameter
12873 Returns a TREE_LIST. Each node represents a template parameter.
12874 The nodes are connected via their TREE_CHAINs. */
12876 static tree
12877 cp_parser_template_parameter_list (cp_parser* parser)
12879 tree parameter_list = NULL_TREE;
12881 begin_template_parm_list ();
12883 /* The loop below parses the template parms. We first need to know
12884 the total number of template parms to be able to compute proper
12885 canonical types of each dependent type. So after the loop, when
12886 we know the total number of template parms,
12887 end_template_parm_list computes the proper canonical types and
12888 fixes up the dependent types accordingly. */
12889 while (true)
12891 tree parameter;
12892 bool is_non_type;
12893 bool is_parameter_pack;
12894 location_t parm_loc;
12896 /* Parse the template-parameter. */
12897 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12898 parameter = cp_parser_template_parameter (parser,
12899 &is_non_type,
12900 &is_parameter_pack);
12901 /* Add it to the list. */
12902 if (parameter != error_mark_node)
12903 parameter_list = process_template_parm (parameter_list,
12904 parm_loc,
12905 parameter,
12906 is_non_type,
12907 is_parameter_pack);
12908 else
12910 tree err_parm = build_tree_list (parameter, parameter);
12911 parameter_list = chainon (parameter_list, err_parm);
12914 /* If the next token is not a `,', we're done. */
12915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12916 break;
12917 /* Otherwise, consume the `,' token. */
12918 cp_lexer_consume_token (parser->lexer);
12921 return end_template_parm_list (parameter_list);
12924 /* Parse a template-parameter.
12926 template-parameter:
12927 type-parameter
12928 parameter-declaration
12930 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12931 the parameter. The TREE_PURPOSE is the default value, if any.
12932 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12933 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12934 set to true iff this parameter is a parameter pack. */
12936 static tree
12937 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12938 bool *is_parameter_pack)
12940 cp_token *token;
12941 cp_parameter_declarator *parameter_declarator;
12942 cp_declarator *id_declarator;
12943 tree parm;
12945 /* Assume it is a type parameter or a template parameter. */
12946 *is_non_type = false;
12947 /* Assume it not a parameter pack. */
12948 *is_parameter_pack = false;
12949 /* Peek at the next token. */
12950 token = cp_lexer_peek_token (parser->lexer);
12951 /* If it is `class' or `template', we have a type-parameter. */
12952 if (token->keyword == RID_TEMPLATE)
12953 return cp_parser_type_parameter (parser, is_parameter_pack);
12954 /* If it is `class' or `typename' we do not know yet whether it is a
12955 type parameter or a non-type parameter. Consider:
12957 template <typename T, typename T::X X> ...
12961 template <class C, class D*> ...
12963 Here, the first parameter is a type parameter, and the second is
12964 a non-type parameter. We can tell by looking at the token after
12965 the identifier -- if it is a `,', `=', or `>' then we have a type
12966 parameter. */
12967 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12969 /* Peek at the token after `class' or `typename'. */
12970 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12971 /* If it's an ellipsis, we have a template type parameter
12972 pack. */
12973 if (token->type == CPP_ELLIPSIS)
12974 return cp_parser_type_parameter (parser, is_parameter_pack);
12975 /* If it's an identifier, skip it. */
12976 if (token->type == CPP_NAME)
12977 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12978 /* Now, see if the token looks like the end of a template
12979 parameter. */
12980 if (token->type == CPP_COMMA
12981 || token->type == CPP_EQ
12982 || token->type == CPP_GREATER)
12983 return cp_parser_type_parameter (parser, is_parameter_pack);
12986 /* Otherwise, it is a non-type parameter.
12988 [temp.param]
12990 When parsing a default template-argument for a non-type
12991 template-parameter, the first non-nested `>' is taken as the end
12992 of the template parameter-list rather than a greater-than
12993 operator. */
12994 *is_non_type = true;
12995 parameter_declarator
12996 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12997 /*parenthesized_p=*/NULL);
12999 if (!parameter_declarator)
13000 return error_mark_node;
13002 /* If the parameter declaration is marked as a parameter pack, set
13003 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13004 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13005 grokdeclarator. */
13006 if (parameter_declarator->declarator
13007 && parameter_declarator->declarator->parameter_pack_p)
13009 *is_parameter_pack = true;
13010 parameter_declarator->declarator->parameter_pack_p = false;
13013 if (parameter_declarator->default_argument)
13015 /* Can happen in some cases of erroneous input (c++/34892). */
13016 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13017 /* Consume the `...' for better error recovery. */
13018 cp_lexer_consume_token (parser->lexer);
13020 /* If the next token is an ellipsis, and we don't already have it
13021 marked as a parameter pack, then we have a parameter pack (that
13022 has no declarator). */
13023 else if (!*is_parameter_pack
13024 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13025 && (declarator_can_be_parameter_pack
13026 (parameter_declarator->declarator)))
13028 /* Consume the `...'. */
13029 cp_lexer_consume_token (parser->lexer);
13030 maybe_warn_variadic_templates ();
13032 *is_parameter_pack = true;
13034 /* We might end up with a pack expansion as the type of the non-type
13035 template parameter, in which case this is a non-type template
13036 parameter pack. */
13037 else if (parameter_declarator->decl_specifiers.type
13038 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13040 *is_parameter_pack = true;
13041 parameter_declarator->decl_specifiers.type =
13042 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13045 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13047 /* Parameter packs cannot have default arguments. However, a
13048 user may try to do so, so we'll parse them and give an
13049 appropriate diagnostic here. */
13051 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13053 /* Find the name of the parameter pack. */
13054 id_declarator = parameter_declarator->declarator;
13055 while (id_declarator && id_declarator->kind != cdk_id)
13056 id_declarator = id_declarator->declarator;
13058 if (id_declarator && id_declarator->kind == cdk_id)
13059 error_at (start_token->location,
13060 "template parameter pack %qD cannot have a default argument",
13061 id_declarator->u.id.unqualified_name);
13062 else
13063 error_at (start_token->location,
13064 "template parameter pack cannot have a default argument");
13066 /* Parse the default argument, but throw away the result. */
13067 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13070 parm = grokdeclarator (parameter_declarator->declarator,
13071 &parameter_declarator->decl_specifiers,
13072 TPARM, /*initialized=*/0,
13073 /*attrlist=*/NULL);
13074 if (parm == error_mark_node)
13075 return error_mark_node;
13077 return build_tree_list (parameter_declarator->default_argument, parm);
13080 /* Parse a type-parameter.
13082 type-parameter:
13083 class identifier [opt]
13084 class identifier [opt] = type-id
13085 typename identifier [opt]
13086 typename identifier [opt] = type-id
13087 template < template-parameter-list > class identifier [opt]
13088 template < template-parameter-list > class identifier [opt]
13089 = id-expression
13091 GNU Extension (variadic templates):
13093 type-parameter:
13094 class ... identifier [opt]
13095 typename ... identifier [opt]
13097 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13098 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13099 the declaration of the parameter.
13101 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13103 static tree
13104 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13106 cp_token *token;
13107 tree parameter;
13109 /* Look for a keyword to tell us what kind of parameter this is. */
13110 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13111 if (!token)
13112 return error_mark_node;
13114 switch (token->keyword)
13116 case RID_CLASS:
13117 case RID_TYPENAME:
13119 tree identifier;
13120 tree default_argument;
13122 /* If the next token is an ellipsis, we have a template
13123 argument pack. */
13124 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13126 /* Consume the `...' token. */
13127 cp_lexer_consume_token (parser->lexer);
13128 maybe_warn_variadic_templates ();
13130 *is_parameter_pack = true;
13133 /* If the next token is an identifier, then it names the
13134 parameter. */
13135 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13136 identifier = cp_parser_identifier (parser);
13137 else
13138 identifier = NULL_TREE;
13140 /* Create the parameter. */
13141 parameter = finish_template_type_parm (class_type_node, identifier);
13143 /* If the next token is an `=', we have a default argument. */
13144 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13146 /* Consume the `=' token. */
13147 cp_lexer_consume_token (parser->lexer);
13148 /* Parse the default-argument. */
13149 push_deferring_access_checks (dk_no_deferred);
13150 default_argument = cp_parser_type_id (parser);
13152 /* Template parameter packs cannot have default
13153 arguments. */
13154 if (*is_parameter_pack)
13156 if (identifier)
13157 error_at (token->location,
13158 "template parameter pack %qD cannot have a "
13159 "default argument", identifier);
13160 else
13161 error_at (token->location,
13162 "template parameter packs cannot have "
13163 "default arguments");
13164 default_argument = NULL_TREE;
13166 pop_deferring_access_checks ();
13168 else
13169 default_argument = NULL_TREE;
13171 /* Create the combined representation of the parameter and the
13172 default argument. */
13173 parameter = build_tree_list (default_argument, parameter);
13175 break;
13177 case RID_TEMPLATE:
13179 tree identifier;
13180 tree default_argument;
13182 /* Look for the `<'. */
13183 cp_parser_require (parser, CPP_LESS, RT_LESS);
13184 /* Parse the template-parameter-list. */
13185 cp_parser_template_parameter_list (parser);
13186 /* Look for the `>'. */
13187 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13188 /* Look for the `class' keyword. */
13189 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13190 /* If the next token is an ellipsis, we have a template
13191 argument pack. */
13192 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13194 /* Consume the `...' token. */
13195 cp_lexer_consume_token (parser->lexer);
13196 maybe_warn_variadic_templates ();
13198 *is_parameter_pack = true;
13200 /* If the next token is an `=', then there is a
13201 default-argument. If the next token is a `>', we are at
13202 the end of the parameter-list. If the next token is a `,',
13203 then we are at the end of this parameter. */
13204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13205 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13206 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13208 identifier = cp_parser_identifier (parser);
13209 /* Treat invalid names as if the parameter were nameless. */
13210 if (identifier == error_mark_node)
13211 identifier = NULL_TREE;
13213 else
13214 identifier = NULL_TREE;
13216 /* Create the template parameter. */
13217 parameter = finish_template_template_parm (class_type_node,
13218 identifier);
13220 /* If the next token is an `=', then there is a
13221 default-argument. */
13222 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13224 bool is_template;
13226 /* Consume the `='. */
13227 cp_lexer_consume_token (parser->lexer);
13228 /* Parse the id-expression. */
13229 push_deferring_access_checks (dk_no_deferred);
13230 /* save token before parsing the id-expression, for error
13231 reporting */
13232 token = cp_lexer_peek_token (parser->lexer);
13233 default_argument
13234 = cp_parser_id_expression (parser,
13235 /*template_keyword_p=*/false,
13236 /*check_dependency_p=*/true,
13237 /*template_p=*/&is_template,
13238 /*declarator_p=*/false,
13239 /*optional_p=*/false);
13240 if (TREE_CODE (default_argument) == TYPE_DECL)
13241 /* If the id-expression was a template-id that refers to
13242 a template-class, we already have the declaration here,
13243 so no further lookup is needed. */
13245 else
13246 /* Look up the name. */
13247 default_argument
13248 = cp_parser_lookup_name (parser, default_argument,
13249 none_type,
13250 /*is_template=*/is_template,
13251 /*is_namespace=*/false,
13252 /*check_dependency=*/true,
13253 /*ambiguous_decls=*/NULL,
13254 token->location);
13255 /* See if the default argument is valid. */
13256 default_argument
13257 = check_template_template_default_arg (default_argument);
13259 /* Template parameter packs cannot have default
13260 arguments. */
13261 if (*is_parameter_pack)
13263 if (identifier)
13264 error_at (token->location,
13265 "template parameter pack %qD cannot "
13266 "have a default argument",
13267 identifier);
13268 else
13269 error_at (token->location, "template parameter packs cannot "
13270 "have default arguments");
13271 default_argument = NULL_TREE;
13273 pop_deferring_access_checks ();
13275 else
13276 default_argument = NULL_TREE;
13278 /* Create the combined representation of the parameter and the
13279 default argument. */
13280 parameter = build_tree_list (default_argument, parameter);
13282 break;
13284 default:
13285 gcc_unreachable ();
13286 break;
13289 return parameter;
13292 /* Parse a template-id.
13294 template-id:
13295 template-name < template-argument-list [opt] >
13297 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13298 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13299 returned. Otherwise, if the template-name names a function, or set
13300 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13301 names a class, returns a TYPE_DECL for the specialization.
13303 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13304 uninstantiated templates. */
13306 static tree
13307 cp_parser_template_id (cp_parser *parser,
13308 bool template_keyword_p,
13309 bool check_dependency_p,
13310 enum tag_types tag_type,
13311 bool is_declaration)
13313 int i;
13314 tree templ;
13315 tree arguments;
13316 tree template_id;
13317 cp_token_position start_of_id = 0;
13318 deferred_access_check *chk;
13319 vec<deferred_access_check, va_gc> *access_check;
13320 cp_token *next_token = NULL, *next_token_2 = NULL;
13321 bool is_identifier;
13323 /* If the next token corresponds to a template-id, there is no need
13324 to reparse it. */
13325 next_token = cp_lexer_peek_token (parser->lexer);
13326 if (next_token->type == CPP_TEMPLATE_ID)
13328 struct tree_check *check_value;
13330 /* Get the stored value. */
13331 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13332 /* Perform any access checks that were deferred. */
13333 access_check = check_value->checks;
13334 if (access_check)
13336 FOR_EACH_VEC_ELT (*access_check, i, chk)
13337 perform_or_defer_access_check (chk->binfo,
13338 chk->decl,
13339 chk->diag_decl,
13340 tf_warning_or_error);
13342 /* Return the stored value. */
13343 return check_value->value;
13346 /* Avoid performing name lookup if there is no possibility of
13347 finding a template-id. */
13348 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13349 || (next_token->type == CPP_NAME
13350 && !cp_parser_nth_token_starts_template_argument_list_p
13351 (parser, 2)))
13353 cp_parser_error (parser, "expected template-id");
13354 return error_mark_node;
13357 /* Remember where the template-id starts. */
13358 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13359 start_of_id = cp_lexer_token_position (parser->lexer, false);
13361 push_deferring_access_checks (dk_deferred);
13363 /* Parse the template-name. */
13364 is_identifier = false;
13365 templ = cp_parser_template_name (parser, template_keyword_p,
13366 check_dependency_p,
13367 is_declaration,
13368 tag_type,
13369 &is_identifier);
13370 if (templ == error_mark_node || is_identifier)
13372 pop_deferring_access_checks ();
13373 return templ;
13376 /* If we find the sequence `[:' after a template-name, it's probably
13377 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13378 parse correctly the argument list. */
13379 next_token = cp_lexer_peek_token (parser->lexer);
13380 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13381 if (next_token->type == CPP_OPEN_SQUARE
13382 && next_token->flags & DIGRAPH
13383 && next_token_2->type == CPP_COLON
13384 && !(next_token_2->flags & PREV_WHITE))
13386 cp_parser_parse_tentatively (parser);
13387 /* Change `:' into `::'. */
13388 next_token_2->type = CPP_SCOPE;
13389 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13390 CPP_LESS. */
13391 cp_lexer_consume_token (parser->lexer);
13393 /* Parse the arguments. */
13394 arguments = cp_parser_enclosed_template_argument_list (parser);
13395 if (!cp_parser_parse_definitely (parser))
13397 /* If we couldn't parse an argument list, then we revert our changes
13398 and return simply an error. Maybe this is not a template-id
13399 after all. */
13400 next_token_2->type = CPP_COLON;
13401 cp_parser_error (parser, "expected %<<%>");
13402 pop_deferring_access_checks ();
13403 return error_mark_node;
13405 /* Otherwise, emit an error about the invalid digraph, but continue
13406 parsing because we got our argument list. */
13407 if (permerror (next_token->location,
13408 "%<<::%> cannot begin a template-argument list"))
13410 static bool hint = false;
13411 inform (next_token->location,
13412 "%<<:%> is an alternate spelling for %<[%>."
13413 " Insert whitespace between %<<%> and %<::%>");
13414 if (!hint && !flag_permissive)
13416 inform (next_token->location, "(if you use %<-fpermissive%> "
13417 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13418 "accept your code)");
13419 hint = true;
13423 else
13425 /* Look for the `<' that starts the template-argument-list. */
13426 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13428 pop_deferring_access_checks ();
13429 return error_mark_node;
13431 /* Parse the arguments. */
13432 arguments = cp_parser_enclosed_template_argument_list (parser);
13435 /* Build a representation of the specialization. */
13436 if (identifier_p (templ))
13437 template_id = build_min_nt_loc (next_token->location,
13438 TEMPLATE_ID_EXPR,
13439 templ, arguments);
13440 else if (DECL_TYPE_TEMPLATE_P (templ)
13441 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13443 bool entering_scope;
13444 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13445 template (rather than some instantiation thereof) only if
13446 is not nested within some other construct. For example, in
13447 "template <typename T> void f(T) { A<T>::", A<T> is just an
13448 instantiation of A. */
13449 entering_scope = (template_parm_scope_p ()
13450 && cp_lexer_next_token_is (parser->lexer,
13451 CPP_SCOPE));
13452 template_id
13453 = finish_template_type (templ, arguments, entering_scope);
13455 else
13457 /* If it's not a class-template or a template-template, it should be
13458 a function-template. */
13459 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13460 || TREE_CODE (templ) == OVERLOAD
13461 || BASELINK_P (templ)));
13463 template_id = lookup_template_function (templ, arguments);
13466 /* If parsing tentatively, replace the sequence of tokens that makes
13467 up the template-id with a CPP_TEMPLATE_ID token. That way,
13468 should we re-parse the token stream, we will not have to repeat
13469 the effort required to do the parse, nor will we issue duplicate
13470 error messages about problems during instantiation of the
13471 template. */
13472 if (start_of_id
13473 /* Don't do this if we had a parse error in a declarator; re-parsing
13474 might succeed if a name changes meaning (60361). */
13475 && !(cp_parser_error_occurred (parser)
13476 && cp_parser_parsing_tentatively (parser)
13477 && parser->in_declarator_p))
13479 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13481 /* Reset the contents of the START_OF_ID token. */
13482 token->type = CPP_TEMPLATE_ID;
13483 /* Retrieve any deferred checks. Do not pop this access checks yet
13484 so the memory will not be reclaimed during token replacing below. */
13485 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13486 token->u.tree_check_value->value = template_id;
13487 token->u.tree_check_value->checks = get_deferred_access_checks ();
13488 token->keyword = RID_MAX;
13490 /* Purge all subsequent tokens. */
13491 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13493 /* ??? Can we actually assume that, if template_id ==
13494 error_mark_node, we will have issued a diagnostic to the
13495 user, as opposed to simply marking the tentative parse as
13496 failed? */
13497 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13498 error_at (token->location, "parse error in template argument list");
13501 pop_to_parent_deferring_access_checks ();
13502 return template_id;
13505 /* Parse a template-name.
13507 template-name:
13508 identifier
13510 The standard should actually say:
13512 template-name:
13513 identifier
13514 operator-function-id
13516 A defect report has been filed about this issue.
13518 A conversion-function-id cannot be a template name because they cannot
13519 be part of a template-id. In fact, looking at this code:
13521 a.operator K<int>()
13523 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13524 It is impossible to call a templated conversion-function-id with an
13525 explicit argument list, since the only allowed template parameter is
13526 the type to which it is converting.
13528 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13529 `template' keyword, in a construction like:
13531 T::template f<3>()
13533 In that case `f' is taken to be a template-name, even though there
13534 is no way of knowing for sure.
13536 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13537 name refers to a set of overloaded functions, at least one of which
13538 is a template, or an IDENTIFIER_NODE with the name of the template,
13539 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13540 names are looked up inside uninstantiated templates. */
13542 static tree
13543 cp_parser_template_name (cp_parser* parser,
13544 bool template_keyword_p,
13545 bool check_dependency_p,
13546 bool is_declaration,
13547 enum tag_types tag_type,
13548 bool *is_identifier)
13550 tree identifier;
13551 tree decl;
13552 tree fns;
13553 cp_token *token = cp_lexer_peek_token (parser->lexer);
13555 /* If the next token is `operator', then we have either an
13556 operator-function-id or a conversion-function-id. */
13557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13559 /* We don't know whether we're looking at an
13560 operator-function-id or a conversion-function-id. */
13561 cp_parser_parse_tentatively (parser);
13562 /* Try an operator-function-id. */
13563 identifier = cp_parser_operator_function_id (parser);
13564 /* If that didn't work, try a conversion-function-id. */
13565 if (!cp_parser_parse_definitely (parser))
13567 cp_parser_error (parser, "expected template-name");
13568 return error_mark_node;
13571 /* Look for the identifier. */
13572 else
13573 identifier = cp_parser_identifier (parser);
13575 /* If we didn't find an identifier, we don't have a template-id. */
13576 if (identifier == error_mark_node)
13577 return error_mark_node;
13579 /* If the name immediately followed the `template' keyword, then it
13580 is a template-name. However, if the next token is not `<', then
13581 we do not treat it as a template-name, since it is not being used
13582 as part of a template-id. This enables us to handle constructs
13583 like:
13585 template <typename T> struct S { S(); };
13586 template <typename T> S<T>::S();
13588 correctly. We would treat `S' as a template -- if it were `S<T>'
13589 -- but we do not if there is no `<'. */
13591 if (processing_template_decl
13592 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13594 /* In a declaration, in a dependent context, we pretend that the
13595 "template" keyword was present in order to improve error
13596 recovery. For example, given:
13598 template <typename T> void f(T::X<int>);
13600 we want to treat "X<int>" as a template-id. */
13601 if (is_declaration
13602 && !template_keyword_p
13603 && parser->scope && TYPE_P (parser->scope)
13604 && check_dependency_p
13605 && dependent_scope_p (parser->scope)
13606 /* Do not do this for dtors (or ctors), since they never
13607 need the template keyword before their name. */
13608 && !constructor_name_p (identifier, parser->scope))
13610 cp_token_position start = 0;
13612 /* Explain what went wrong. */
13613 error_at (token->location, "non-template %qD used as template",
13614 identifier);
13615 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13616 parser->scope, identifier);
13617 /* If parsing tentatively, find the location of the "<" token. */
13618 if (cp_parser_simulate_error (parser))
13619 start = cp_lexer_token_position (parser->lexer, true);
13620 /* Parse the template arguments so that we can issue error
13621 messages about them. */
13622 cp_lexer_consume_token (parser->lexer);
13623 cp_parser_enclosed_template_argument_list (parser);
13624 /* Skip tokens until we find a good place from which to
13625 continue parsing. */
13626 cp_parser_skip_to_closing_parenthesis (parser,
13627 /*recovering=*/true,
13628 /*or_comma=*/true,
13629 /*consume_paren=*/false);
13630 /* If parsing tentatively, permanently remove the
13631 template argument list. That will prevent duplicate
13632 error messages from being issued about the missing
13633 "template" keyword. */
13634 if (start)
13635 cp_lexer_purge_tokens_after (parser->lexer, start);
13636 if (is_identifier)
13637 *is_identifier = true;
13638 return identifier;
13641 /* If the "template" keyword is present, then there is generally
13642 no point in doing name-lookup, so we just return IDENTIFIER.
13643 But, if the qualifying scope is non-dependent then we can
13644 (and must) do name-lookup normally. */
13645 if (template_keyword_p
13646 && (!parser->scope
13647 || (TYPE_P (parser->scope)
13648 && dependent_type_p (parser->scope))))
13649 return identifier;
13652 /* Look up the name. */
13653 decl = cp_parser_lookup_name (parser, identifier,
13654 tag_type,
13655 /*is_template=*/true,
13656 /*is_namespace=*/false,
13657 check_dependency_p,
13658 /*ambiguous_decls=*/NULL,
13659 token->location);
13661 /* If DECL is a template, then the name was a template-name. */
13662 if (TREE_CODE (decl) == TEMPLATE_DECL)
13664 else
13666 tree fn = NULL_TREE;
13668 /* The standard does not explicitly indicate whether a name that
13669 names a set of overloaded declarations, some of which are
13670 templates, is a template-name. However, such a name should
13671 be a template-name; otherwise, there is no way to form a
13672 template-id for the overloaded templates. */
13673 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13674 if (TREE_CODE (fns) == OVERLOAD)
13675 for (fn = fns; fn; fn = OVL_NEXT (fn))
13676 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13677 break;
13679 if (!fn)
13681 /* The name does not name a template. */
13682 cp_parser_error (parser, "expected template-name");
13683 return error_mark_node;
13687 /* If DECL is dependent, and refers to a function, then just return
13688 its name; we will look it up again during template instantiation. */
13689 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13691 tree scope = ovl_scope (decl);
13692 if (TYPE_P (scope) && dependent_type_p (scope))
13693 return identifier;
13696 return decl;
13699 /* Parse a template-argument-list.
13701 template-argument-list:
13702 template-argument ... [opt]
13703 template-argument-list , template-argument ... [opt]
13705 Returns a TREE_VEC containing the arguments. */
13707 static tree
13708 cp_parser_template_argument_list (cp_parser* parser)
13710 tree fixed_args[10];
13711 unsigned n_args = 0;
13712 unsigned alloced = 10;
13713 tree *arg_ary = fixed_args;
13714 tree vec;
13715 bool saved_in_template_argument_list_p;
13716 bool saved_ice_p;
13717 bool saved_non_ice_p;
13719 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13720 parser->in_template_argument_list_p = true;
13721 /* Even if the template-id appears in an integral
13722 constant-expression, the contents of the argument list do
13723 not. */
13724 saved_ice_p = parser->integral_constant_expression_p;
13725 parser->integral_constant_expression_p = false;
13726 saved_non_ice_p = parser->non_integral_constant_expression_p;
13727 parser->non_integral_constant_expression_p = false;
13729 /* Parse the arguments. */
13732 tree argument;
13734 if (n_args)
13735 /* Consume the comma. */
13736 cp_lexer_consume_token (parser->lexer);
13738 /* Parse the template-argument. */
13739 argument = cp_parser_template_argument (parser);
13741 /* If the next token is an ellipsis, we're expanding a template
13742 argument pack. */
13743 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13745 if (argument == error_mark_node)
13747 cp_token *token = cp_lexer_peek_token (parser->lexer);
13748 error_at (token->location,
13749 "expected parameter pack before %<...%>");
13751 /* Consume the `...' token. */
13752 cp_lexer_consume_token (parser->lexer);
13754 /* Make the argument into a TYPE_PACK_EXPANSION or
13755 EXPR_PACK_EXPANSION. */
13756 argument = make_pack_expansion (argument);
13759 if (n_args == alloced)
13761 alloced *= 2;
13763 if (arg_ary == fixed_args)
13765 arg_ary = XNEWVEC (tree, alloced);
13766 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13768 else
13769 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13771 arg_ary[n_args++] = argument;
13773 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13775 vec = make_tree_vec (n_args);
13777 while (n_args--)
13778 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13780 if (arg_ary != fixed_args)
13781 free (arg_ary);
13782 parser->non_integral_constant_expression_p = saved_non_ice_p;
13783 parser->integral_constant_expression_p = saved_ice_p;
13784 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13785 #ifdef ENABLE_CHECKING
13786 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13787 #endif
13788 return vec;
13791 /* Parse a template-argument.
13793 template-argument:
13794 assignment-expression
13795 type-id
13796 id-expression
13798 The representation is that of an assignment-expression, type-id, or
13799 id-expression -- except that the qualified id-expression is
13800 evaluated, so that the value returned is either a DECL or an
13801 OVERLOAD.
13803 Although the standard says "assignment-expression", it forbids
13804 throw-expressions or assignments in the template argument.
13805 Therefore, we use "conditional-expression" instead. */
13807 static tree
13808 cp_parser_template_argument (cp_parser* parser)
13810 tree argument;
13811 bool template_p;
13812 bool address_p;
13813 bool maybe_type_id = false;
13814 cp_token *token = NULL, *argument_start_token = NULL;
13815 location_t loc = 0;
13816 cp_id_kind idk;
13818 /* There's really no way to know what we're looking at, so we just
13819 try each alternative in order.
13821 [temp.arg]
13823 In a template-argument, an ambiguity between a type-id and an
13824 expression is resolved to a type-id, regardless of the form of
13825 the corresponding template-parameter.
13827 Therefore, we try a type-id first. */
13828 cp_parser_parse_tentatively (parser);
13829 argument = cp_parser_template_type_arg (parser);
13830 /* If there was no error parsing the type-id but the next token is a
13831 '>>', our behavior depends on which dialect of C++ we're
13832 parsing. In C++98, we probably found a typo for '> >'. But there
13833 are type-id which are also valid expressions. For instance:
13835 struct X { int operator >> (int); };
13836 template <int V> struct Foo {};
13837 Foo<X () >> 5> r;
13839 Here 'X()' is a valid type-id of a function type, but the user just
13840 wanted to write the expression "X() >> 5". Thus, we remember that we
13841 found a valid type-id, but we still try to parse the argument as an
13842 expression to see what happens.
13844 In C++0x, the '>>' will be considered two separate '>'
13845 tokens. */
13846 if (!cp_parser_error_occurred (parser)
13847 && cxx_dialect == cxx98
13848 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13850 maybe_type_id = true;
13851 cp_parser_abort_tentative_parse (parser);
13853 else
13855 /* If the next token isn't a `,' or a `>', then this argument wasn't
13856 really finished. This means that the argument is not a valid
13857 type-id. */
13858 if (!cp_parser_next_token_ends_template_argument_p (parser))
13859 cp_parser_error (parser, "expected template-argument");
13860 /* If that worked, we're done. */
13861 if (cp_parser_parse_definitely (parser))
13862 return argument;
13864 /* We're still not sure what the argument will be. */
13865 cp_parser_parse_tentatively (parser);
13866 /* Try a template. */
13867 argument_start_token = cp_lexer_peek_token (parser->lexer);
13868 argument = cp_parser_id_expression (parser,
13869 /*template_keyword_p=*/false,
13870 /*check_dependency_p=*/true,
13871 &template_p,
13872 /*declarator_p=*/false,
13873 /*optional_p=*/false);
13874 /* If the next token isn't a `,' or a `>', then this argument wasn't
13875 really finished. */
13876 if (!cp_parser_next_token_ends_template_argument_p (parser))
13877 cp_parser_error (parser, "expected template-argument");
13878 if (!cp_parser_error_occurred (parser))
13880 /* Figure out what is being referred to. If the id-expression
13881 was for a class template specialization, then we will have a
13882 TYPE_DECL at this point. There is no need to do name lookup
13883 at this point in that case. */
13884 if (TREE_CODE (argument) != TYPE_DECL)
13885 argument = cp_parser_lookup_name (parser, argument,
13886 none_type,
13887 /*is_template=*/template_p,
13888 /*is_namespace=*/false,
13889 /*check_dependency=*/true,
13890 /*ambiguous_decls=*/NULL,
13891 argument_start_token->location);
13892 if (TREE_CODE (argument) != TEMPLATE_DECL
13893 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13894 cp_parser_error (parser, "expected template-name");
13896 if (cp_parser_parse_definitely (parser))
13897 return argument;
13898 /* It must be a non-type argument. There permitted cases are given
13899 in [temp.arg.nontype]:
13901 -- an integral constant-expression of integral or enumeration
13902 type; or
13904 -- the name of a non-type template-parameter; or
13906 -- the name of an object or function with external linkage...
13908 -- the address of an object or function with external linkage...
13910 -- a pointer to member... */
13911 /* Look for a non-type template parameter. */
13912 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13914 cp_parser_parse_tentatively (parser);
13915 argument = cp_parser_primary_expression (parser,
13916 /*address_p=*/false,
13917 /*cast_p=*/false,
13918 /*template_arg_p=*/true,
13919 &idk);
13920 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13921 || !cp_parser_next_token_ends_template_argument_p (parser))
13922 cp_parser_simulate_error (parser);
13923 if (cp_parser_parse_definitely (parser))
13924 return argument;
13927 /* If the next token is "&", the argument must be the address of an
13928 object or function with external linkage. */
13929 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13930 if (address_p)
13932 loc = cp_lexer_peek_token (parser->lexer)->location;
13933 cp_lexer_consume_token (parser->lexer);
13935 /* See if we might have an id-expression. */
13936 token = cp_lexer_peek_token (parser->lexer);
13937 if (token->type == CPP_NAME
13938 || token->keyword == RID_OPERATOR
13939 || token->type == CPP_SCOPE
13940 || token->type == CPP_TEMPLATE_ID
13941 || token->type == CPP_NESTED_NAME_SPECIFIER)
13943 cp_parser_parse_tentatively (parser);
13944 argument = cp_parser_primary_expression (parser,
13945 address_p,
13946 /*cast_p=*/false,
13947 /*template_arg_p=*/true,
13948 &idk);
13949 if (cp_parser_error_occurred (parser)
13950 || !cp_parser_next_token_ends_template_argument_p (parser))
13951 cp_parser_abort_tentative_parse (parser);
13952 else
13954 tree probe;
13956 if (INDIRECT_REF_P (argument))
13958 /* Strip the dereference temporarily. */
13959 gcc_assert (REFERENCE_REF_P (argument));
13960 argument = TREE_OPERAND (argument, 0);
13963 /* If we're in a template, we represent a qualified-id referring
13964 to a static data member as a SCOPE_REF even if the scope isn't
13965 dependent so that we can check access control later. */
13966 probe = argument;
13967 if (TREE_CODE (probe) == SCOPE_REF)
13968 probe = TREE_OPERAND (probe, 1);
13969 if (VAR_P (probe))
13971 /* A variable without external linkage might still be a
13972 valid constant-expression, so no error is issued here
13973 if the external-linkage check fails. */
13974 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13975 cp_parser_simulate_error (parser);
13977 else if (is_overloaded_fn (argument))
13978 /* All overloaded functions are allowed; if the external
13979 linkage test does not pass, an error will be issued
13980 later. */
13982 else if (address_p
13983 && (TREE_CODE (argument) == OFFSET_REF
13984 || TREE_CODE (argument) == SCOPE_REF))
13985 /* A pointer-to-member. */
13987 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13989 else
13990 cp_parser_simulate_error (parser);
13992 if (cp_parser_parse_definitely (parser))
13994 if (address_p)
13995 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13996 tf_warning_or_error);
13997 else
13998 argument = convert_from_reference (argument);
13999 return argument;
14003 /* If the argument started with "&", there are no other valid
14004 alternatives at this point. */
14005 if (address_p)
14007 cp_parser_error (parser, "invalid non-type template argument");
14008 return error_mark_node;
14011 /* If the argument wasn't successfully parsed as a type-id followed
14012 by '>>', the argument can only be a constant expression now.
14013 Otherwise, we try parsing the constant-expression tentatively,
14014 because the argument could really be a type-id. */
14015 if (maybe_type_id)
14016 cp_parser_parse_tentatively (parser);
14017 argument = cp_parser_constant_expression (parser,
14018 /*allow_non_constant_p=*/false,
14019 /*non_constant_p=*/NULL);
14020 if (!maybe_type_id)
14021 return argument;
14022 if (!cp_parser_next_token_ends_template_argument_p (parser))
14023 cp_parser_error (parser, "expected template-argument");
14024 if (cp_parser_parse_definitely (parser))
14025 return argument;
14026 /* We did our best to parse the argument as a non type-id, but that
14027 was the only alternative that matched (albeit with a '>' after
14028 it). We can assume it's just a typo from the user, and a
14029 diagnostic will then be issued. */
14030 return cp_parser_template_type_arg (parser);
14033 /* Parse an explicit-instantiation.
14035 explicit-instantiation:
14036 template declaration
14038 Although the standard says `declaration', what it really means is:
14040 explicit-instantiation:
14041 template decl-specifier-seq [opt] declarator [opt] ;
14043 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14044 supposed to be allowed. A defect report has been filed about this
14045 issue.
14047 GNU Extension:
14049 explicit-instantiation:
14050 storage-class-specifier template
14051 decl-specifier-seq [opt] declarator [opt] ;
14052 function-specifier template
14053 decl-specifier-seq [opt] declarator [opt] ; */
14055 static void
14056 cp_parser_explicit_instantiation (cp_parser* parser)
14058 int declares_class_or_enum;
14059 cp_decl_specifier_seq decl_specifiers;
14060 tree extension_specifier = NULL_TREE;
14062 timevar_push (TV_TEMPLATE_INST);
14064 /* Look for an (optional) storage-class-specifier or
14065 function-specifier. */
14066 if (cp_parser_allow_gnu_extensions_p (parser))
14068 extension_specifier
14069 = cp_parser_storage_class_specifier_opt (parser);
14070 if (!extension_specifier)
14071 extension_specifier
14072 = cp_parser_function_specifier_opt (parser,
14073 /*decl_specs=*/NULL);
14076 /* Look for the `template' keyword. */
14077 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14078 /* Let the front end know that we are processing an explicit
14079 instantiation. */
14080 begin_explicit_instantiation ();
14081 /* [temp.explicit] says that we are supposed to ignore access
14082 control while processing explicit instantiation directives. */
14083 push_deferring_access_checks (dk_no_check);
14084 /* Parse a decl-specifier-seq. */
14085 cp_parser_decl_specifier_seq (parser,
14086 CP_PARSER_FLAGS_OPTIONAL,
14087 &decl_specifiers,
14088 &declares_class_or_enum);
14089 /* If there was exactly one decl-specifier, and it declared a class,
14090 and there's no declarator, then we have an explicit type
14091 instantiation. */
14092 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14094 tree type;
14096 type = check_tag_decl (&decl_specifiers,
14097 /*explicit_type_instantiation_p=*/true);
14098 /* Turn access control back on for names used during
14099 template instantiation. */
14100 pop_deferring_access_checks ();
14101 if (type)
14102 do_type_instantiation (type, extension_specifier,
14103 /*complain=*/tf_error);
14105 else
14107 cp_declarator *declarator;
14108 tree decl;
14110 /* Parse the declarator. */
14111 declarator
14112 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14113 /*ctor_dtor_or_conv_p=*/NULL,
14114 /*parenthesized_p=*/NULL,
14115 /*member_p=*/false);
14116 if (declares_class_or_enum & 2)
14117 cp_parser_check_for_definition_in_return_type (declarator,
14118 decl_specifiers.type,
14119 decl_specifiers.locations[ds_type_spec]);
14120 if (declarator != cp_error_declarator)
14122 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14123 permerror (decl_specifiers.locations[ds_inline],
14124 "explicit instantiation shall not use"
14125 " %<inline%> specifier");
14126 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14127 permerror (decl_specifiers.locations[ds_constexpr],
14128 "explicit instantiation shall not use"
14129 " %<constexpr%> specifier");
14131 decl = grokdeclarator (declarator, &decl_specifiers,
14132 NORMAL, 0, &decl_specifiers.attributes);
14133 /* Turn access control back on for names used during
14134 template instantiation. */
14135 pop_deferring_access_checks ();
14136 /* Do the explicit instantiation. */
14137 do_decl_instantiation (decl, extension_specifier);
14139 else
14141 pop_deferring_access_checks ();
14142 /* Skip the body of the explicit instantiation. */
14143 cp_parser_skip_to_end_of_statement (parser);
14146 /* We're done with the instantiation. */
14147 end_explicit_instantiation ();
14149 cp_parser_consume_semicolon_at_end_of_statement (parser);
14151 timevar_pop (TV_TEMPLATE_INST);
14154 /* Parse an explicit-specialization.
14156 explicit-specialization:
14157 template < > declaration
14159 Although the standard says `declaration', what it really means is:
14161 explicit-specialization:
14162 template <> decl-specifier [opt] init-declarator [opt] ;
14163 template <> function-definition
14164 template <> explicit-specialization
14165 template <> template-declaration */
14167 static void
14168 cp_parser_explicit_specialization (cp_parser* parser)
14170 bool need_lang_pop;
14171 cp_token *token = cp_lexer_peek_token (parser->lexer);
14173 /* Look for the `template' keyword. */
14174 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14175 /* Look for the `<'. */
14176 cp_parser_require (parser, CPP_LESS, RT_LESS);
14177 /* Look for the `>'. */
14178 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14179 /* We have processed another parameter list. */
14180 ++parser->num_template_parameter_lists;
14181 /* [temp]
14183 A template ... explicit specialization ... shall not have C
14184 linkage. */
14185 if (current_lang_name == lang_name_c)
14187 error_at (token->location, "template specialization with C linkage");
14188 /* Give it C++ linkage to avoid confusing other parts of the
14189 front end. */
14190 push_lang_context (lang_name_cplusplus);
14191 need_lang_pop = true;
14193 else
14194 need_lang_pop = false;
14195 /* Let the front end know that we are beginning a specialization. */
14196 if (!begin_specialization ())
14198 end_specialization ();
14199 return;
14202 /* If the next keyword is `template', we need to figure out whether
14203 or not we're looking a template-declaration. */
14204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14206 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14207 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14208 cp_parser_template_declaration_after_export (parser,
14209 /*member_p=*/false);
14210 else
14211 cp_parser_explicit_specialization (parser);
14213 else
14214 /* Parse the dependent declaration. */
14215 cp_parser_single_declaration (parser,
14216 /*checks=*/NULL,
14217 /*member_p=*/false,
14218 /*explicit_specialization_p=*/true,
14219 /*friend_p=*/NULL);
14220 /* We're done with the specialization. */
14221 end_specialization ();
14222 /* For the erroneous case of a template with C linkage, we pushed an
14223 implicit C++ linkage scope; exit that scope now. */
14224 if (need_lang_pop)
14225 pop_lang_context ();
14226 /* We're done with this parameter list. */
14227 --parser->num_template_parameter_lists;
14230 /* Parse a type-specifier.
14232 type-specifier:
14233 simple-type-specifier
14234 class-specifier
14235 enum-specifier
14236 elaborated-type-specifier
14237 cv-qualifier
14239 GNU Extension:
14241 type-specifier:
14242 __complex__
14244 Returns a representation of the type-specifier. For a
14245 class-specifier, enum-specifier, or elaborated-type-specifier, a
14246 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14248 The parser flags FLAGS is used to control type-specifier parsing.
14250 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14251 in a decl-specifier-seq.
14253 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14254 class-specifier, enum-specifier, or elaborated-type-specifier, then
14255 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14256 if a type is declared; 2 if it is defined. Otherwise, it is set to
14257 zero.
14259 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14260 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14261 is set to FALSE. */
14263 static tree
14264 cp_parser_type_specifier (cp_parser* parser,
14265 cp_parser_flags flags,
14266 cp_decl_specifier_seq *decl_specs,
14267 bool is_declaration,
14268 int* declares_class_or_enum,
14269 bool* is_cv_qualifier)
14271 tree type_spec = NULL_TREE;
14272 cp_token *token;
14273 enum rid keyword;
14274 cp_decl_spec ds = ds_last;
14276 /* Assume this type-specifier does not declare a new type. */
14277 if (declares_class_or_enum)
14278 *declares_class_or_enum = 0;
14279 /* And that it does not specify a cv-qualifier. */
14280 if (is_cv_qualifier)
14281 *is_cv_qualifier = false;
14282 /* Peek at the next token. */
14283 token = cp_lexer_peek_token (parser->lexer);
14285 /* If we're looking at a keyword, we can use that to guide the
14286 production we choose. */
14287 keyword = token->keyword;
14288 switch (keyword)
14290 case RID_ENUM:
14291 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14292 goto elaborated_type_specifier;
14294 /* Look for the enum-specifier. */
14295 type_spec = cp_parser_enum_specifier (parser);
14296 /* If that worked, we're done. */
14297 if (type_spec)
14299 if (declares_class_or_enum)
14300 *declares_class_or_enum = 2;
14301 if (decl_specs)
14302 cp_parser_set_decl_spec_type (decl_specs,
14303 type_spec,
14304 token,
14305 /*type_definition_p=*/true);
14306 return type_spec;
14308 else
14309 goto elaborated_type_specifier;
14311 /* Any of these indicate either a class-specifier, or an
14312 elaborated-type-specifier. */
14313 case RID_CLASS:
14314 case RID_STRUCT:
14315 case RID_UNION:
14316 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14317 goto elaborated_type_specifier;
14319 /* Parse tentatively so that we can back up if we don't find a
14320 class-specifier. */
14321 cp_parser_parse_tentatively (parser);
14322 /* Look for the class-specifier. */
14323 type_spec = cp_parser_class_specifier (parser);
14324 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14325 /* If that worked, we're done. */
14326 if (cp_parser_parse_definitely (parser))
14328 if (declares_class_or_enum)
14329 *declares_class_or_enum = 2;
14330 if (decl_specs)
14331 cp_parser_set_decl_spec_type (decl_specs,
14332 type_spec,
14333 token,
14334 /*type_definition_p=*/true);
14335 return type_spec;
14338 /* Fall through. */
14339 elaborated_type_specifier:
14340 /* We're declaring (not defining) a class or enum. */
14341 if (declares_class_or_enum)
14342 *declares_class_or_enum = 1;
14344 /* Fall through. */
14345 case RID_TYPENAME:
14346 /* Look for an elaborated-type-specifier. */
14347 type_spec
14348 = (cp_parser_elaborated_type_specifier
14349 (parser,
14350 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14351 is_declaration));
14352 if (decl_specs)
14353 cp_parser_set_decl_spec_type (decl_specs,
14354 type_spec,
14355 token,
14356 /*type_definition_p=*/false);
14357 return type_spec;
14359 case RID_CONST:
14360 ds = ds_const;
14361 if (is_cv_qualifier)
14362 *is_cv_qualifier = true;
14363 break;
14365 case RID_VOLATILE:
14366 ds = ds_volatile;
14367 if (is_cv_qualifier)
14368 *is_cv_qualifier = true;
14369 break;
14371 case RID_RESTRICT:
14372 ds = ds_restrict;
14373 if (is_cv_qualifier)
14374 *is_cv_qualifier = true;
14375 break;
14377 case RID_COMPLEX:
14378 /* The `__complex__' keyword is a GNU extension. */
14379 ds = ds_complex;
14380 break;
14382 default:
14383 break;
14386 /* Handle simple keywords. */
14387 if (ds != ds_last)
14389 if (decl_specs)
14391 set_and_check_decl_spec_loc (decl_specs, ds, token);
14392 decl_specs->any_specifiers_p = true;
14394 return cp_lexer_consume_token (parser->lexer)->u.value;
14397 /* If we do not already have a type-specifier, assume we are looking
14398 at a simple-type-specifier. */
14399 type_spec = cp_parser_simple_type_specifier (parser,
14400 decl_specs,
14401 flags);
14403 /* If we didn't find a type-specifier, and a type-specifier was not
14404 optional in this context, issue an error message. */
14405 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14407 cp_parser_error (parser, "expected type specifier");
14408 return error_mark_node;
14411 return type_spec;
14414 /* Parse a simple-type-specifier.
14416 simple-type-specifier:
14417 :: [opt] nested-name-specifier [opt] type-name
14418 :: [opt] nested-name-specifier template template-id
14419 char
14420 wchar_t
14421 bool
14422 short
14424 long
14425 signed
14426 unsigned
14427 float
14428 double
14429 void
14431 C++0x Extension:
14433 simple-type-specifier:
14434 auto
14435 decltype ( expression )
14436 char16_t
14437 char32_t
14438 __underlying_type ( type-id )
14440 GNU Extension:
14442 simple-type-specifier:
14443 __int128
14444 __typeof__ unary-expression
14445 __typeof__ ( type-id )
14446 __typeof__ ( type-id ) { initializer-list , [opt] }
14448 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14449 appropriately updated. */
14451 static tree
14452 cp_parser_simple_type_specifier (cp_parser* parser,
14453 cp_decl_specifier_seq *decl_specs,
14454 cp_parser_flags flags)
14456 tree type = NULL_TREE;
14457 cp_token *token;
14459 /* Peek at the next token. */
14460 token = cp_lexer_peek_token (parser->lexer);
14462 /* If we're looking at a keyword, things are easy. */
14463 switch (token->keyword)
14465 case RID_CHAR:
14466 if (decl_specs)
14467 decl_specs->explicit_char_p = true;
14468 type = char_type_node;
14469 break;
14470 case RID_CHAR16:
14471 type = char16_type_node;
14472 break;
14473 case RID_CHAR32:
14474 type = char32_type_node;
14475 break;
14476 case RID_WCHAR:
14477 type = wchar_type_node;
14478 break;
14479 case RID_BOOL:
14480 type = boolean_type_node;
14481 break;
14482 case RID_SHORT:
14483 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14484 type = short_integer_type_node;
14485 break;
14486 case RID_INT:
14487 if (decl_specs)
14488 decl_specs->explicit_int_p = true;
14489 type = integer_type_node;
14490 break;
14491 case RID_INT128:
14492 if (!int128_integer_type_node)
14493 break;
14494 if (decl_specs)
14495 decl_specs->explicit_int128_p = true;
14496 type = int128_integer_type_node;
14497 break;
14498 case RID_LONG:
14499 if (decl_specs)
14500 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14501 type = long_integer_type_node;
14502 break;
14503 case RID_SIGNED:
14504 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14505 type = integer_type_node;
14506 break;
14507 case RID_UNSIGNED:
14508 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14509 type = unsigned_type_node;
14510 break;
14511 case RID_FLOAT:
14512 type = float_type_node;
14513 break;
14514 case RID_DOUBLE:
14515 type = double_type_node;
14516 break;
14517 case RID_VOID:
14518 type = void_type_node;
14519 break;
14521 case RID_AUTO:
14522 maybe_warn_cpp0x (CPP0X_AUTO);
14523 if (parser->auto_is_implicit_function_template_parm_p)
14525 type = synthesize_implicit_template_parm (parser);
14527 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14529 if (cxx_dialect < cxx1y)
14530 pedwarn (location_of (type), 0,
14531 "use of %<auto%> in lambda parameter declaration "
14532 "only available with "
14533 "-std=c++1y or -std=gnu++1y");
14535 else if (cxx_dialect < cxx1y)
14536 pedwarn (location_of (type), 0,
14537 "use of %<auto%> in parameter declaration "
14538 "only available with "
14539 "-std=c++1y or -std=gnu++1y");
14540 else
14541 pedwarn (location_of (type), OPT_Wpedantic,
14542 "ISO C++ forbids use of %<auto%> in parameter "
14543 "declaration");
14545 else
14546 type = make_auto ();
14547 break;
14549 case RID_DECLTYPE:
14550 /* Since DR 743, decltype can either be a simple-type-specifier by
14551 itself or begin a nested-name-specifier. Parsing it will replace
14552 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14553 handling below decide what to do. */
14554 cp_parser_decltype (parser);
14555 cp_lexer_set_token_position (parser->lexer, token);
14556 break;
14558 case RID_TYPEOF:
14559 /* Consume the `typeof' token. */
14560 cp_lexer_consume_token (parser->lexer);
14561 /* Parse the operand to `typeof'. */
14562 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14563 /* If it is not already a TYPE, take its type. */
14564 if (!TYPE_P (type))
14565 type = finish_typeof (type);
14567 if (decl_specs)
14568 cp_parser_set_decl_spec_type (decl_specs, type,
14569 token,
14570 /*type_definition_p=*/false);
14572 return type;
14574 case RID_UNDERLYING_TYPE:
14575 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14576 if (decl_specs)
14577 cp_parser_set_decl_spec_type (decl_specs, type,
14578 token,
14579 /*type_definition_p=*/false);
14581 return type;
14583 case RID_BASES:
14584 case RID_DIRECT_BASES:
14585 type = cp_parser_trait_expr (parser, token->keyword);
14586 if (decl_specs)
14587 cp_parser_set_decl_spec_type (decl_specs, type,
14588 token,
14589 /*type_definition_p=*/false);
14590 return type;
14591 default:
14592 break;
14595 /* If token is an already-parsed decltype not followed by ::,
14596 it's a simple-type-specifier. */
14597 if (token->type == CPP_DECLTYPE
14598 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14600 type = token->u.value;
14601 if (decl_specs)
14602 cp_parser_set_decl_spec_type (decl_specs, type,
14603 token,
14604 /*type_definition_p=*/false);
14605 cp_lexer_consume_token (parser->lexer);
14606 return type;
14609 /* If the type-specifier was for a built-in type, we're done. */
14610 if (type)
14612 /* Record the type. */
14613 if (decl_specs
14614 && (token->keyword != RID_SIGNED
14615 && token->keyword != RID_UNSIGNED
14616 && token->keyword != RID_SHORT
14617 && token->keyword != RID_LONG))
14618 cp_parser_set_decl_spec_type (decl_specs,
14619 type,
14620 token,
14621 /*type_definition_p=*/false);
14622 if (decl_specs)
14623 decl_specs->any_specifiers_p = true;
14625 /* Consume the token. */
14626 cp_lexer_consume_token (parser->lexer);
14628 /* There is no valid C++ program where a non-template type is
14629 followed by a "<". That usually indicates that the user thought
14630 that the type was a template. */
14631 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14632 token->location);
14634 return TYPE_NAME (type);
14637 /* The type-specifier must be a user-defined type. */
14638 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14640 bool qualified_p;
14641 bool global_p;
14643 /* Don't gobble tokens or issue error messages if this is an
14644 optional type-specifier. */
14645 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14646 cp_parser_parse_tentatively (parser);
14648 /* Look for the optional `::' operator. */
14649 global_p
14650 = (cp_parser_global_scope_opt (parser,
14651 /*current_scope_valid_p=*/false)
14652 != NULL_TREE);
14653 /* Look for the nested-name specifier. */
14654 qualified_p
14655 = (cp_parser_nested_name_specifier_opt (parser,
14656 /*typename_keyword_p=*/false,
14657 /*check_dependency_p=*/true,
14658 /*type_p=*/false,
14659 /*is_declaration=*/false)
14660 != NULL_TREE);
14661 token = cp_lexer_peek_token (parser->lexer);
14662 /* If we have seen a nested-name-specifier, and the next token
14663 is `template', then we are using the template-id production. */
14664 if (parser->scope
14665 && cp_parser_optional_template_keyword (parser))
14667 /* Look for the template-id. */
14668 type = cp_parser_template_id (parser,
14669 /*template_keyword_p=*/true,
14670 /*check_dependency_p=*/true,
14671 none_type,
14672 /*is_declaration=*/false);
14673 /* If the template-id did not name a type, we are out of
14674 luck. */
14675 if (TREE_CODE (type) != TYPE_DECL)
14677 cp_parser_error (parser, "expected template-id for type");
14678 type = NULL_TREE;
14681 /* Otherwise, look for a type-name. */
14682 else
14683 type = cp_parser_type_name (parser);
14684 /* Keep track of all name-lookups performed in class scopes. */
14685 if (type
14686 && !global_p
14687 && !qualified_p
14688 && TREE_CODE (type) == TYPE_DECL
14689 && identifier_p (DECL_NAME (type)))
14690 maybe_note_name_used_in_class (DECL_NAME (type), type);
14691 /* If it didn't work out, we don't have a TYPE. */
14692 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14693 && !cp_parser_parse_definitely (parser))
14694 type = NULL_TREE;
14695 if (type && decl_specs)
14696 cp_parser_set_decl_spec_type (decl_specs, type,
14697 token,
14698 /*type_definition_p=*/false);
14701 /* If we didn't get a type-name, issue an error message. */
14702 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14704 cp_parser_error (parser, "expected type-name");
14705 return error_mark_node;
14708 if (type && type != error_mark_node)
14710 /* See if TYPE is an Objective-C type, and if so, parse and
14711 accept any protocol references following it. Do this before
14712 the cp_parser_check_for_invalid_template_id() call, because
14713 Objective-C types can be followed by '<...>' which would
14714 enclose protocol names rather than template arguments, and so
14715 everything is fine. */
14716 if (c_dialect_objc () && !parser->scope
14717 && (objc_is_id (type) || objc_is_class_name (type)))
14719 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14720 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14722 /* Clobber the "unqualified" type previously entered into
14723 DECL_SPECS with the new, improved protocol-qualified version. */
14724 if (decl_specs)
14725 decl_specs->type = qual_type;
14727 return qual_type;
14730 /* There is no valid C++ program where a non-template type is
14731 followed by a "<". That usually indicates that the user
14732 thought that the type was a template. */
14733 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14734 none_type,
14735 token->location);
14738 return type;
14741 /* Parse a type-name.
14743 type-name:
14744 class-name
14745 enum-name
14746 typedef-name
14747 simple-template-id [in c++0x]
14749 enum-name:
14750 identifier
14752 typedef-name:
14753 identifier
14755 Returns a TYPE_DECL for the type. */
14757 static tree
14758 cp_parser_type_name (cp_parser* parser)
14760 tree type_decl;
14762 /* We can't know yet whether it is a class-name or not. */
14763 cp_parser_parse_tentatively (parser);
14764 /* Try a class-name. */
14765 type_decl = cp_parser_class_name (parser,
14766 /*typename_keyword_p=*/false,
14767 /*template_keyword_p=*/false,
14768 none_type,
14769 /*check_dependency_p=*/true,
14770 /*class_head_p=*/false,
14771 /*is_declaration=*/false);
14772 /* If it's not a class-name, keep looking. */
14773 if (!cp_parser_parse_definitely (parser))
14775 if (cxx_dialect < cxx11)
14776 /* It must be a typedef-name or an enum-name. */
14777 return cp_parser_nonclass_name (parser);
14779 cp_parser_parse_tentatively (parser);
14780 /* It is either a simple-template-id representing an
14781 instantiation of an alias template... */
14782 type_decl = cp_parser_template_id (parser,
14783 /*template_keyword_p=*/false,
14784 /*check_dependency_p=*/true,
14785 none_type,
14786 /*is_declaration=*/false);
14787 /* Note that this must be an instantiation of an alias template
14788 because [temp.names]/6 says:
14790 A template-id that names an alias template specialization
14791 is a type-name.
14793 Whereas [temp.names]/7 says:
14795 A simple-template-id that names a class template
14796 specialization is a class-name. */
14797 if (type_decl != NULL_TREE
14798 && TREE_CODE (type_decl) == TYPE_DECL
14799 && TYPE_DECL_ALIAS_P (type_decl))
14800 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14801 else
14802 cp_parser_simulate_error (parser);
14804 if (!cp_parser_parse_definitely (parser))
14805 /* ... Or a typedef-name or an enum-name. */
14806 return cp_parser_nonclass_name (parser);
14809 return type_decl;
14812 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14814 enum-name:
14815 identifier
14817 typedef-name:
14818 identifier
14820 Returns a TYPE_DECL for the type. */
14822 static tree
14823 cp_parser_nonclass_name (cp_parser* parser)
14825 tree type_decl;
14826 tree identifier;
14828 cp_token *token = cp_lexer_peek_token (parser->lexer);
14829 identifier = cp_parser_identifier (parser);
14830 if (identifier == error_mark_node)
14831 return error_mark_node;
14833 /* Look up the type-name. */
14834 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14836 type_decl = strip_using_decl (type_decl);
14838 if (TREE_CODE (type_decl) != TYPE_DECL
14839 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14841 /* See if this is an Objective-C type. */
14842 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14843 tree type = objc_get_protocol_qualified_type (identifier, protos);
14844 if (type)
14845 type_decl = TYPE_NAME (type);
14848 /* Issue an error if we did not find a type-name. */
14849 if (TREE_CODE (type_decl) != TYPE_DECL
14850 /* In Objective-C, we have the complication that class names are
14851 normally type names and start declarations (eg, the
14852 "NSObject" in "NSObject *object;"), but can be used in an
14853 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14854 is an expression. So, a classname followed by a dot is not a
14855 valid type-name. */
14856 || (objc_is_class_name (TREE_TYPE (type_decl))
14857 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14859 if (!cp_parser_simulate_error (parser))
14860 cp_parser_name_lookup_error (parser, identifier, type_decl,
14861 NLE_TYPE, token->location);
14862 return error_mark_node;
14864 /* Remember that the name was used in the definition of the
14865 current class so that we can check later to see if the
14866 meaning would have been different after the class was
14867 entirely defined. */
14868 else if (type_decl != error_mark_node
14869 && !parser->scope)
14870 maybe_note_name_used_in_class (identifier, type_decl);
14872 return type_decl;
14875 /* Parse an elaborated-type-specifier. Note that the grammar given
14876 here incorporates the resolution to DR68.
14878 elaborated-type-specifier:
14879 class-key :: [opt] nested-name-specifier [opt] identifier
14880 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14881 enum-key :: [opt] nested-name-specifier [opt] identifier
14882 typename :: [opt] nested-name-specifier identifier
14883 typename :: [opt] nested-name-specifier template [opt]
14884 template-id
14886 GNU extension:
14888 elaborated-type-specifier:
14889 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14890 class-key attributes :: [opt] nested-name-specifier [opt]
14891 template [opt] template-id
14892 enum attributes :: [opt] nested-name-specifier [opt] identifier
14894 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14895 declared `friend'. If IS_DECLARATION is TRUE, then this
14896 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14897 something is being declared.
14899 Returns the TYPE specified. */
14901 static tree
14902 cp_parser_elaborated_type_specifier (cp_parser* parser,
14903 bool is_friend,
14904 bool is_declaration)
14906 enum tag_types tag_type;
14907 tree identifier;
14908 tree type = NULL_TREE;
14909 tree attributes = NULL_TREE;
14910 tree globalscope;
14911 cp_token *token = NULL;
14913 /* See if we're looking at the `enum' keyword. */
14914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14916 /* Consume the `enum' token. */
14917 cp_lexer_consume_token (parser->lexer);
14918 /* Remember that it's an enumeration type. */
14919 tag_type = enum_type;
14920 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14921 enums) is used here. */
14922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14923 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14925 pedwarn (input_location, 0, "elaborated-type-specifier "
14926 "for a scoped enum must not use the %<%D%> keyword",
14927 cp_lexer_peek_token (parser->lexer)->u.value);
14928 /* Consume the `struct' or `class' and parse it anyway. */
14929 cp_lexer_consume_token (parser->lexer);
14931 /* Parse the attributes. */
14932 attributes = cp_parser_attributes_opt (parser);
14934 /* Or, it might be `typename'. */
14935 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14936 RID_TYPENAME))
14938 /* Consume the `typename' token. */
14939 cp_lexer_consume_token (parser->lexer);
14940 /* Remember that it's a `typename' type. */
14941 tag_type = typename_type;
14943 /* Otherwise it must be a class-key. */
14944 else
14946 tag_type = cp_parser_class_key (parser);
14947 if (tag_type == none_type)
14948 return error_mark_node;
14949 /* Parse the attributes. */
14950 attributes = cp_parser_attributes_opt (parser);
14953 /* Look for the `::' operator. */
14954 globalscope = cp_parser_global_scope_opt (parser,
14955 /*current_scope_valid_p=*/false);
14956 /* Look for the nested-name-specifier. */
14957 if (tag_type == typename_type && !globalscope)
14959 if (!cp_parser_nested_name_specifier (parser,
14960 /*typename_keyword_p=*/true,
14961 /*check_dependency_p=*/true,
14962 /*type_p=*/true,
14963 is_declaration))
14964 return error_mark_node;
14966 else
14967 /* Even though `typename' is not present, the proposed resolution
14968 to Core Issue 180 says that in `class A<T>::B', `B' should be
14969 considered a type-name, even if `A<T>' is dependent. */
14970 cp_parser_nested_name_specifier_opt (parser,
14971 /*typename_keyword_p=*/true,
14972 /*check_dependency_p=*/true,
14973 /*type_p=*/true,
14974 is_declaration);
14975 /* For everything but enumeration types, consider a template-id.
14976 For an enumeration type, consider only a plain identifier. */
14977 if (tag_type != enum_type)
14979 bool template_p = false;
14980 tree decl;
14982 /* Allow the `template' keyword. */
14983 template_p = cp_parser_optional_template_keyword (parser);
14984 /* If we didn't see `template', we don't know if there's a
14985 template-id or not. */
14986 if (!template_p)
14987 cp_parser_parse_tentatively (parser);
14988 /* Parse the template-id. */
14989 token = cp_lexer_peek_token (parser->lexer);
14990 decl = cp_parser_template_id (parser, template_p,
14991 /*check_dependency_p=*/true,
14992 tag_type,
14993 is_declaration);
14994 /* If we didn't find a template-id, look for an ordinary
14995 identifier. */
14996 if (!template_p && !cp_parser_parse_definitely (parser))
14998 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14999 in effect, then we must assume that, upon instantiation, the
15000 template will correspond to a class. */
15001 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15002 && tag_type == typename_type)
15003 type = make_typename_type (parser->scope, decl,
15004 typename_type,
15005 /*complain=*/tf_error);
15006 /* If the `typename' keyword is in effect and DECL is not a type
15007 decl, then type is non existent. */
15008 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15010 else if (TREE_CODE (decl) == TYPE_DECL)
15011 type = check_elaborated_type_specifier (tag_type, decl,
15012 /*allow_template_p=*/true);
15013 else if (decl == error_mark_node)
15014 type = error_mark_node;
15017 if (!type)
15019 token = cp_lexer_peek_token (parser->lexer);
15020 identifier = cp_parser_identifier (parser);
15022 if (identifier == error_mark_node)
15024 parser->scope = NULL_TREE;
15025 return error_mark_node;
15028 /* For a `typename', we needn't call xref_tag. */
15029 if (tag_type == typename_type
15030 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15031 return cp_parser_make_typename_type (parser, parser->scope,
15032 identifier,
15033 token->location);
15034 /* Look up a qualified name in the usual way. */
15035 if (parser->scope)
15037 tree decl;
15038 tree ambiguous_decls;
15040 decl = cp_parser_lookup_name (parser, identifier,
15041 tag_type,
15042 /*is_template=*/false,
15043 /*is_namespace=*/false,
15044 /*check_dependency=*/true,
15045 &ambiguous_decls,
15046 token->location);
15048 /* If the lookup was ambiguous, an error will already have been
15049 issued. */
15050 if (ambiguous_decls)
15051 return error_mark_node;
15053 /* If we are parsing friend declaration, DECL may be a
15054 TEMPLATE_DECL tree node here. However, we need to check
15055 whether this TEMPLATE_DECL results in valid code. Consider
15056 the following example:
15058 namespace N {
15059 template <class T> class C {};
15061 class X {
15062 template <class T> friend class N::C; // #1, valid code
15064 template <class T> class Y {
15065 friend class N::C; // #2, invalid code
15068 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15069 name lookup of `N::C'. We see that friend declaration must
15070 be template for the code to be valid. Note that
15071 processing_template_decl does not work here since it is
15072 always 1 for the above two cases. */
15074 decl = (cp_parser_maybe_treat_template_as_class
15075 (decl, /*tag_name_p=*/is_friend
15076 && parser->num_template_parameter_lists));
15078 if (TREE_CODE (decl) != TYPE_DECL)
15080 cp_parser_diagnose_invalid_type_name (parser,
15081 parser->scope,
15082 identifier,
15083 token->location);
15084 return error_mark_node;
15087 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15089 bool allow_template = (parser->num_template_parameter_lists
15090 || DECL_SELF_REFERENCE_P (decl));
15091 type = check_elaborated_type_specifier (tag_type, decl,
15092 allow_template);
15094 if (type == error_mark_node)
15095 return error_mark_node;
15098 /* Forward declarations of nested types, such as
15100 class C1::C2;
15101 class C1::C2::C3;
15103 are invalid unless all components preceding the final '::'
15104 are complete. If all enclosing types are complete, these
15105 declarations become merely pointless.
15107 Invalid forward declarations of nested types are errors
15108 caught elsewhere in parsing. Those that are pointless arrive
15109 here. */
15111 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15112 && !is_friend && !processing_explicit_instantiation)
15113 warning (0, "declaration %qD does not declare anything", decl);
15115 type = TREE_TYPE (decl);
15117 else
15119 /* An elaborated-type-specifier sometimes introduces a new type and
15120 sometimes names an existing type. Normally, the rule is that it
15121 introduces a new type only if there is not an existing type of
15122 the same name already in scope. For example, given:
15124 struct S {};
15125 void f() { struct S s; }
15127 the `struct S' in the body of `f' is the same `struct S' as in
15128 the global scope; the existing definition is used. However, if
15129 there were no global declaration, this would introduce a new
15130 local class named `S'.
15132 An exception to this rule applies to the following code:
15134 namespace N { struct S; }
15136 Here, the elaborated-type-specifier names a new type
15137 unconditionally; even if there is already an `S' in the
15138 containing scope this declaration names a new type.
15139 This exception only applies if the elaborated-type-specifier
15140 forms the complete declaration:
15142 [class.name]
15144 A declaration consisting solely of `class-key identifier ;' is
15145 either a redeclaration of the name in the current scope or a
15146 forward declaration of the identifier as a class name. It
15147 introduces the name into the current scope.
15149 We are in this situation precisely when the next token is a `;'.
15151 An exception to the exception is that a `friend' declaration does
15152 *not* name a new type; i.e., given:
15154 struct S { friend struct T; };
15156 `T' is not a new type in the scope of `S'.
15158 Also, `new struct S' or `sizeof (struct S)' never results in the
15159 definition of a new type; a new type can only be declared in a
15160 declaration context. */
15162 tag_scope ts;
15163 bool template_p;
15165 if (is_friend)
15166 /* Friends have special name lookup rules. */
15167 ts = ts_within_enclosing_non_class;
15168 else if (is_declaration
15169 && cp_lexer_next_token_is (parser->lexer,
15170 CPP_SEMICOLON))
15171 /* This is a `class-key identifier ;' */
15172 ts = ts_current;
15173 else
15174 ts = ts_global;
15176 template_p =
15177 (parser->num_template_parameter_lists
15178 && (cp_parser_next_token_starts_class_definition_p (parser)
15179 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15180 /* An unqualified name was used to reference this type, so
15181 there were no qualifying templates. */
15182 if (!cp_parser_check_template_parameters (parser,
15183 /*num_templates=*/0,
15184 token->location,
15185 /*declarator=*/NULL))
15186 return error_mark_node;
15187 type = xref_tag (tag_type, identifier, ts, template_p);
15191 if (type == error_mark_node)
15192 return error_mark_node;
15194 /* Allow attributes on forward declarations of classes. */
15195 if (attributes)
15197 if (TREE_CODE (type) == TYPENAME_TYPE)
15198 warning (OPT_Wattributes,
15199 "attributes ignored on uninstantiated type");
15200 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15201 && ! processing_explicit_instantiation)
15202 warning (OPT_Wattributes,
15203 "attributes ignored on template instantiation");
15204 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15205 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15206 else
15207 warning (OPT_Wattributes,
15208 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15211 if (tag_type != enum_type)
15213 /* Indicate whether this class was declared as a `class' or as a
15214 `struct'. */
15215 if (TREE_CODE (type) == RECORD_TYPE)
15216 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15217 cp_parser_check_class_key (tag_type, type);
15220 /* A "<" cannot follow an elaborated type specifier. If that
15221 happens, the user was probably trying to form a template-id. */
15222 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15223 token->location);
15225 return type;
15228 /* Parse an enum-specifier.
15230 enum-specifier:
15231 enum-head { enumerator-list [opt] }
15232 enum-head { enumerator-list , } [C++0x]
15234 enum-head:
15235 enum-key identifier [opt] enum-base [opt]
15236 enum-key nested-name-specifier identifier enum-base [opt]
15238 enum-key:
15239 enum
15240 enum class [C++0x]
15241 enum struct [C++0x]
15243 enum-base: [C++0x]
15244 : type-specifier-seq
15246 opaque-enum-specifier:
15247 enum-key identifier enum-base [opt] ;
15249 GNU Extensions:
15250 enum-key attributes[opt] identifier [opt] enum-base [opt]
15251 { enumerator-list [opt] }attributes[opt]
15252 enum-key attributes[opt] identifier [opt] enum-base [opt]
15253 { enumerator-list, }attributes[opt] [C++0x]
15255 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15256 if the token stream isn't an enum-specifier after all. */
15258 static tree
15259 cp_parser_enum_specifier (cp_parser* parser)
15261 tree identifier;
15262 tree type = NULL_TREE;
15263 tree prev_scope;
15264 tree nested_name_specifier = NULL_TREE;
15265 tree attributes;
15266 bool scoped_enum_p = false;
15267 bool has_underlying_type = false;
15268 bool nested_being_defined = false;
15269 bool new_value_list = false;
15270 bool is_new_type = false;
15271 bool is_anonymous = false;
15272 tree underlying_type = NULL_TREE;
15273 cp_token *type_start_token = NULL;
15274 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15276 parser->colon_corrects_to_scope_p = false;
15278 /* Parse tentatively so that we can back up if we don't find a
15279 enum-specifier. */
15280 cp_parser_parse_tentatively (parser);
15282 /* Caller guarantees that the current token is 'enum', an identifier
15283 possibly follows, and the token after that is an opening brace.
15284 If we don't have an identifier, fabricate an anonymous name for
15285 the enumeration being defined. */
15286 cp_lexer_consume_token (parser->lexer);
15288 /* Parse the "class" or "struct", which indicates a scoped
15289 enumeration type in C++0x. */
15290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15291 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15293 if (cxx_dialect < cxx11)
15294 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15296 /* Consume the `struct' or `class' token. */
15297 cp_lexer_consume_token (parser->lexer);
15299 scoped_enum_p = true;
15302 attributes = cp_parser_attributes_opt (parser);
15304 /* Clear the qualification. */
15305 parser->scope = NULL_TREE;
15306 parser->qualifying_scope = NULL_TREE;
15307 parser->object_scope = NULL_TREE;
15309 /* Figure out in what scope the declaration is being placed. */
15310 prev_scope = current_scope ();
15312 type_start_token = cp_lexer_peek_token (parser->lexer);
15314 push_deferring_access_checks (dk_no_check);
15315 nested_name_specifier
15316 = cp_parser_nested_name_specifier_opt (parser,
15317 /*typename_keyword_p=*/true,
15318 /*check_dependency_p=*/false,
15319 /*type_p=*/false,
15320 /*is_declaration=*/false);
15322 if (nested_name_specifier)
15324 tree name;
15326 identifier = cp_parser_identifier (parser);
15327 name = cp_parser_lookup_name (parser, identifier,
15328 enum_type,
15329 /*is_template=*/false,
15330 /*is_namespace=*/false,
15331 /*check_dependency=*/true,
15332 /*ambiguous_decls=*/NULL,
15333 input_location);
15334 if (name && name != error_mark_node)
15336 type = TREE_TYPE (name);
15337 if (TREE_CODE (type) == TYPENAME_TYPE)
15339 /* Are template enums allowed in ISO? */
15340 if (template_parm_scope_p ())
15341 pedwarn (type_start_token->location, OPT_Wpedantic,
15342 "%qD is an enumeration template", name);
15343 /* ignore a typename reference, for it will be solved by name
15344 in start_enum. */
15345 type = NULL_TREE;
15348 else if (nested_name_specifier == error_mark_node)
15349 /* We already issued an error. */;
15350 else
15351 error_at (type_start_token->location,
15352 "%qD is not an enumerator-name", identifier);
15354 else
15356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15357 identifier = cp_parser_identifier (parser);
15358 else
15360 identifier = make_anon_name ();
15361 is_anonymous = true;
15362 if (scoped_enum_p)
15363 error_at (type_start_token->location,
15364 "anonymous scoped enum is not allowed");
15367 pop_deferring_access_checks ();
15369 /* Check for the `:' that denotes a specified underlying type in C++0x.
15370 Note that a ':' could also indicate a bitfield width, however. */
15371 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15373 cp_decl_specifier_seq type_specifiers;
15375 /* Consume the `:'. */
15376 cp_lexer_consume_token (parser->lexer);
15378 /* Parse the type-specifier-seq. */
15379 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15380 /*is_trailing_return=*/false,
15381 &type_specifiers);
15383 /* At this point this is surely not elaborated type specifier. */
15384 if (!cp_parser_parse_definitely (parser))
15385 return NULL_TREE;
15387 if (cxx_dialect < cxx11)
15388 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15390 has_underlying_type = true;
15392 /* If that didn't work, stop. */
15393 if (type_specifiers.type != error_mark_node)
15395 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15396 /*initialized=*/0, NULL);
15397 if (underlying_type == error_mark_node
15398 || check_for_bare_parameter_packs (underlying_type))
15399 underlying_type = NULL_TREE;
15403 /* Look for the `{' but don't consume it yet. */
15404 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15406 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15408 cp_parser_error (parser, "expected %<{%>");
15409 if (has_underlying_type)
15411 type = NULL_TREE;
15412 goto out;
15415 /* An opaque-enum-specifier must have a ';' here. */
15416 if ((scoped_enum_p || underlying_type)
15417 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15419 cp_parser_error (parser, "expected %<;%> or %<{%>");
15420 if (has_underlying_type)
15422 type = NULL_TREE;
15423 goto out;
15428 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15429 return NULL_TREE;
15431 if (nested_name_specifier)
15433 if (CLASS_TYPE_P (nested_name_specifier))
15435 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15436 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15437 push_scope (nested_name_specifier);
15439 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15441 push_nested_namespace (nested_name_specifier);
15445 /* Issue an error message if type-definitions are forbidden here. */
15446 if (!cp_parser_check_type_definition (parser))
15447 type = error_mark_node;
15448 else
15449 /* Create the new type. We do this before consuming the opening
15450 brace so the enum will be recorded as being on the line of its
15451 tag (or the 'enum' keyword, if there is no tag). */
15452 type = start_enum (identifier, type, underlying_type,
15453 scoped_enum_p, &is_new_type);
15455 /* If the next token is not '{' it is an opaque-enum-specifier or an
15456 elaborated-type-specifier. */
15457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15459 timevar_push (TV_PARSE_ENUM);
15460 if (nested_name_specifier
15461 && nested_name_specifier != error_mark_node)
15463 /* The following catches invalid code such as:
15464 enum class S<int>::E { A, B, C }; */
15465 if (!processing_specialization
15466 && CLASS_TYPE_P (nested_name_specifier)
15467 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15468 error_at (type_start_token->location, "cannot add an enumerator "
15469 "list to a template instantiation");
15471 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15473 error_at (type_start_token->location,
15474 "%<%T::%E%> has not been declared",
15475 TYPE_CONTEXT (nested_name_specifier),
15476 nested_name_specifier);
15477 type = error_mark_node;
15479 /* If that scope does not contain the scope in which the
15480 class was originally declared, the program is invalid. */
15481 else if (prev_scope && !is_ancestor (prev_scope,
15482 nested_name_specifier))
15484 if (at_namespace_scope_p ())
15485 error_at (type_start_token->location,
15486 "declaration of %qD in namespace %qD which does not "
15487 "enclose %qD",
15488 type, prev_scope, nested_name_specifier);
15489 else
15490 error_at (type_start_token->location,
15491 "declaration of %qD in %qD which does not "
15492 "enclose %qD",
15493 type, prev_scope, nested_name_specifier);
15494 type = error_mark_node;
15498 if (scoped_enum_p)
15499 begin_scope (sk_scoped_enum, type);
15501 /* Consume the opening brace. */
15502 cp_lexer_consume_token (parser->lexer);
15504 if (type == error_mark_node)
15505 ; /* Nothing to add */
15506 else if (OPAQUE_ENUM_P (type)
15507 || (cxx_dialect > cxx98 && processing_specialization))
15509 new_value_list = true;
15510 SET_OPAQUE_ENUM_P (type, false);
15511 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15513 else
15515 error_at (type_start_token->location, "multiple definition of %q#T", type);
15516 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15517 "previous definition here");
15518 type = error_mark_node;
15521 if (type == error_mark_node)
15522 cp_parser_skip_to_end_of_block_or_statement (parser);
15523 /* If the next token is not '}', then there are some enumerators. */
15524 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15526 if (is_anonymous && !scoped_enum_p)
15527 pedwarn (type_start_token->location, OPT_Wpedantic,
15528 "ISO C++ forbids empty anonymous enum");
15530 else
15531 cp_parser_enumerator_list (parser, type);
15533 /* Consume the final '}'. */
15534 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15536 if (scoped_enum_p)
15537 finish_scope ();
15538 timevar_pop (TV_PARSE_ENUM);
15540 else
15542 /* If a ';' follows, then it is an opaque-enum-specifier
15543 and additional restrictions apply. */
15544 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15546 if (is_anonymous)
15547 error_at (type_start_token->location,
15548 "opaque-enum-specifier without name");
15549 else if (nested_name_specifier)
15550 error_at (type_start_token->location,
15551 "opaque-enum-specifier must use a simple identifier");
15555 /* Look for trailing attributes to apply to this enumeration, and
15556 apply them if appropriate. */
15557 if (cp_parser_allow_gnu_extensions_p (parser))
15559 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15560 trailing_attr = chainon (trailing_attr, attributes);
15561 cplus_decl_attributes (&type,
15562 trailing_attr,
15563 (int) ATTR_FLAG_TYPE_IN_PLACE);
15566 /* Finish up the enumeration. */
15567 if (type != error_mark_node)
15569 if (new_value_list)
15570 finish_enum_value_list (type);
15571 if (is_new_type)
15572 finish_enum (type);
15575 if (nested_name_specifier)
15577 if (CLASS_TYPE_P (nested_name_specifier))
15579 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15580 pop_scope (nested_name_specifier);
15582 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15584 pop_nested_namespace (nested_name_specifier);
15587 out:
15588 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15589 return type;
15592 /* Parse an enumerator-list. The enumerators all have the indicated
15593 TYPE.
15595 enumerator-list:
15596 enumerator-definition
15597 enumerator-list , enumerator-definition */
15599 static void
15600 cp_parser_enumerator_list (cp_parser* parser, tree type)
15602 while (true)
15604 /* Parse an enumerator-definition. */
15605 cp_parser_enumerator_definition (parser, type);
15607 /* If the next token is not a ',', we've reached the end of
15608 the list. */
15609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15610 break;
15611 /* Otherwise, consume the `,' and keep going. */
15612 cp_lexer_consume_token (parser->lexer);
15613 /* If the next token is a `}', there is a trailing comma. */
15614 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15616 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15617 pedwarn (input_location, OPT_Wpedantic,
15618 "comma at end of enumerator list");
15619 break;
15624 /* Parse an enumerator-definition. The enumerator has the indicated
15625 TYPE.
15627 enumerator-definition:
15628 enumerator
15629 enumerator = constant-expression
15631 enumerator:
15632 identifier */
15634 static void
15635 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15637 tree identifier;
15638 tree value;
15639 location_t loc;
15641 /* Save the input location because we are interested in the location
15642 of the identifier and not the location of the explicit value. */
15643 loc = cp_lexer_peek_token (parser->lexer)->location;
15645 /* Look for the identifier. */
15646 identifier = cp_parser_identifier (parser);
15647 if (identifier == error_mark_node)
15648 return;
15650 /* If the next token is an '=', then there is an explicit value. */
15651 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15653 /* Consume the `=' token. */
15654 cp_lexer_consume_token (parser->lexer);
15655 /* Parse the value. */
15656 value = cp_parser_constant_expression (parser,
15657 /*allow_non_constant_p=*/false,
15658 NULL);
15660 else
15661 value = NULL_TREE;
15663 /* If we are processing a template, make sure the initializer of the
15664 enumerator doesn't contain any bare template parameter pack. */
15665 if (check_for_bare_parameter_packs (value))
15666 value = error_mark_node;
15668 /* integral_constant_value will pull out this expression, so make sure
15669 it's folded as appropriate. */
15670 value = fold_non_dependent_expr (value);
15672 /* Create the enumerator. */
15673 build_enumerator (identifier, value, type, loc);
15676 /* Parse a namespace-name.
15678 namespace-name:
15679 original-namespace-name
15680 namespace-alias
15682 Returns the NAMESPACE_DECL for the namespace. */
15684 static tree
15685 cp_parser_namespace_name (cp_parser* parser)
15687 tree identifier;
15688 tree namespace_decl;
15690 cp_token *token = cp_lexer_peek_token (parser->lexer);
15692 /* Get the name of the namespace. */
15693 identifier = cp_parser_identifier (parser);
15694 if (identifier == error_mark_node)
15695 return error_mark_node;
15697 /* Look up the identifier in the currently active scope. Look only
15698 for namespaces, due to:
15700 [basic.lookup.udir]
15702 When looking up a namespace-name in a using-directive or alias
15703 definition, only namespace names are considered.
15705 And:
15707 [basic.lookup.qual]
15709 During the lookup of a name preceding the :: scope resolution
15710 operator, object, function, and enumerator names are ignored.
15712 (Note that cp_parser_qualifying_entity only calls this
15713 function if the token after the name is the scope resolution
15714 operator.) */
15715 namespace_decl = cp_parser_lookup_name (parser, identifier,
15716 none_type,
15717 /*is_template=*/false,
15718 /*is_namespace=*/true,
15719 /*check_dependency=*/true,
15720 /*ambiguous_decls=*/NULL,
15721 token->location);
15722 /* If it's not a namespace, issue an error. */
15723 if (namespace_decl == error_mark_node
15724 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15726 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15727 error_at (token->location, "%qD is not a namespace-name", identifier);
15728 cp_parser_error (parser, "expected namespace-name");
15729 namespace_decl = error_mark_node;
15732 return namespace_decl;
15735 /* Parse a namespace-definition.
15737 namespace-definition:
15738 named-namespace-definition
15739 unnamed-namespace-definition
15741 named-namespace-definition:
15742 original-namespace-definition
15743 extension-namespace-definition
15745 original-namespace-definition:
15746 namespace identifier { namespace-body }
15748 extension-namespace-definition:
15749 namespace original-namespace-name { namespace-body }
15751 unnamed-namespace-definition:
15752 namespace { namespace-body } */
15754 static void
15755 cp_parser_namespace_definition (cp_parser* parser)
15757 tree identifier, attribs;
15758 bool has_visibility;
15759 bool is_inline;
15761 cp_ensure_no_omp_declare_simd (parser);
15762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15764 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15765 is_inline = true;
15766 cp_lexer_consume_token (parser->lexer);
15768 else
15769 is_inline = false;
15771 /* Look for the `namespace' keyword. */
15772 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15774 /* Get the name of the namespace. We do not attempt to distinguish
15775 between an original-namespace-definition and an
15776 extension-namespace-definition at this point. The semantic
15777 analysis routines are responsible for that. */
15778 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15779 identifier = cp_parser_identifier (parser);
15780 else
15781 identifier = NULL_TREE;
15783 /* Parse any specified attributes. */
15784 attribs = cp_parser_attributes_opt (parser);
15786 /* Look for the `{' to start the namespace. */
15787 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15788 /* Start the namespace. */
15789 push_namespace (identifier);
15791 /* "inline namespace" is equivalent to a stub namespace definition
15792 followed by a strong using directive. */
15793 if (is_inline)
15795 tree name_space = current_namespace;
15796 /* Set up namespace association. */
15797 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15798 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15799 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15800 /* Import the contents of the inline namespace. */
15801 pop_namespace ();
15802 do_using_directive (name_space);
15803 push_namespace (identifier);
15806 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15808 /* Parse the body of the namespace. */
15809 cp_parser_namespace_body (parser);
15811 if (has_visibility)
15812 pop_visibility (1);
15814 /* Finish the namespace. */
15815 pop_namespace ();
15816 /* Look for the final `}'. */
15817 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15820 /* Parse a namespace-body.
15822 namespace-body:
15823 declaration-seq [opt] */
15825 static void
15826 cp_parser_namespace_body (cp_parser* parser)
15828 cp_parser_declaration_seq_opt (parser);
15831 /* Parse a namespace-alias-definition.
15833 namespace-alias-definition:
15834 namespace identifier = qualified-namespace-specifier ; */
15836 static void
15837 cp_parser_namespace_alias_definition (cp_parser* parser)
15839 tree identifier;
15840 tree namespace_specifier;
15842 cp_token *token = cp_lexer_peek_token (parser->lexer);
15844 /* Look for the `namespace' keyword. */
15845 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15846 /* Look for the identifier. */
15847 identifier = cp_parser_identifier (parser);
15848 if (identifier == error_mark_node)
15849 return;
15850 /* Look for the `=' token. */
15851 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15852 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15854 error_at (token->location, "%<namespace%> definition is not allowed here");
15855 /* Skip the definition. */
15856 cp_lexer_consume_token (parser->lexer);
15857 if (cp_parser_skip_to_closing_brace (parser))
15858 cp_lexer_consume_token (parser->lexer);
15859 return;
15861 cp_parser_require (parser, CPP_EQ, RT_EQ);
15862 /* Look for the qualified-namespace-specifier. */
15863 namespace_specifier
15864 = cp_parser_qualified_namespace_specifier (parser);
15865 /* Look for the `;' token. */
15866 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15868 /* Register the alias in the symbol table. */
15869 do_namespace_alias (identifier, namespace_specifier);
15872 /* Parse a qualified-namespace-specifier.
15874 qualified-namespace-specifier:
15875 :: [opt] nested-name-specifier [opt] namespace-name
15877 Returns a NAMESPACE_DECL corresponding to the specified
15878 namespace. */
15880 static tree
15881 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15883 /* Look for the optional `::'. */
15884 cp_parser_global_scope_opt (parser,
15885 /*current_scope_valid_p=*/false);
15887 /* Look for the optional nested-name-specifier. */
15888 cp_parser_nested_name_specifier_opt (parser,
15889 /*typename_keyword_p=*/false,
15890 /*check_dependency_p=*/true,
15891 /*type_p=*/false,
15892 /*is_declaration=*/true);
15894 return cp_parser_namespace_name (parser);
15897 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15898 access declaration.
15900 using-declaration:
15901 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15902 using :: unqualified-id ;
15904 access-declaration:
15905 qualified-id ;
15909 static bool
15910 cp_parser_using_declaration (cp_parser* parser,
15911 bool access_declaration_p)
15913 cp_token *token;
15914 bool typename_p = false;
15915 bool global_scope_p;
15916 tree decl;
15917 tree identifier;
15918 tree qscope;
15919 int oldcount = errorcount;
15920 cp_token *diag_token = NULL;
15922 if (access_declaration_p)
15924 diag_token = cp_lexer_peek_token (parser->lexer);
15925 cp_parser_parse_tentatively (parser);
15927 else
15929 /* Look for the `using' keyword. */
15930 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15932 /* Peek at the next token. */
15933 token = cp_lexer_peek_token (parser->lexer);
15934 /* See if it's `typename'. */
15935 if (token->keyword == RID_TYPENAME)
15937 /* Remember that we've seen it. */
15938 typename_p = true;
15939 /* Consume the `typename' token. */
15940 cp_lexer_consume_token (parser->lexer);
15944 /* Look for the optional global scope qualification. */
15945 global_scope_p
15946 = (cp_parser_global_scope_opt (parser,
15947 /*current_scope_valid_p=*/false)
15948 != NULL_TREE);
15950 /* If we saw `typename', or didn't see `::', then there must be a
15951 nested-name-specifier present. */
15952 if (typename_p || !global_scope_p)
15954 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15955 /*check_dependency_p=*/true,
15956 /*type_p=*/false,
15957 /*is_declaration=*/true);
15958 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
15960 cp_parser_skip_to_end_of_block_or_statement (parser);
15961 return false;
15964 /* Otherwise, we could be in either of the two productions. In that
15965 case, treat the nested-name-specifier as optional. */
15966 else
15967 qscope = cp_parser_nested_name_specifier_opt (parser,
15968 /*typename_keyword_p=*/false,
15969 /*check_dependency_p=*/true,
15970 /*type_p=*/false,
15971 /*is_declaration=*/true);
15972 if (!qscope)
15973 qscope = global_namespace;
15975 if (access_declaration_p && cp_parser_error_occurred (parser))
15976 /* Something has already gone wrong; there's no need to parse
15977 further. Since an error has occurred, the return value of
15978 cp_parser_parse_definitely will be false, as required. */
15979 return cp_parser_parse_definitely (parser);
15981 token = cp_lexer_peek_token (parser->lexer);
15982 /* Parse the unqualified-id. */
15983 identifier = cp_parser_unqualified_id (parser,
15984 /*template_keyword_p=*/false,
15985 /*check_dependency_p=*/true,
15986 /*declarator_p=*/true,
15987 /*optional_p=*/false);
15989 if (access_declaration_p)
15991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15992 cp_parser_simulate_error (parser);
15993 if (!cp_parser_parse_definitely (parser))
15994 return false;
15997 /* The function we call to handle a using-declaration is different
15998 depending on what scope we are in. */
15999 if (qscope == error_mark_node || identifier == error_mark_node)
16001 else if (!identifier_p (identifier)
16002 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16003 /* [namespace.udecl]
16005 A using declaration shall not name a template-id. */
16006 error_at (token->location,
16007 "a template-id may not appear in a using-declaration");
16008 else
16010 if (at_class_scope_p ())
16012 /* Create the USING_DECL. */
16013 decl = do_class_using_decl (parser->scope, identifier);
16015 if (decl && typename_p)
16016 USING_DECL_TYPENAME_P (decl) = 1;
16018 if (check_for_bare_parameter_packs (decl))
16020 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16021 return false;
16023 else
16024 /* Add it to the list of members in this class. */
16025 finish_member_declaration (decl);
16027 else
16029 decl = cp_parser_lookup_name_simple (parser,
16030 identifier,
16031 token->location);
16032 if (decl == error_mark_node)
16033 cp_parser_name_lookup_error (parser, identifier,
16034 decl, NLE_NULL,
16035 token->location);
16036 else if (check_for_bare_parameter_packs (decl))
16038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16039 return false;
16041 else if (!at_namespace_scope_p ())
16042 do_local_using_decl (decl, qscope, identifier);
16043 else
16044 do_toplevel_using_decl (decl, qscope, identifier);
16048 /* Look for the final `;'. */
16049 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16051 if (access_declaration_p && errorcount == oldcount)
16052 warning_at (diag_token->location, OPT_Wdeprecated,
16053 "access declarations are deprecated "
16054 "in favour of using-declarations; "
16055 "suggestion: add the %<using%> keyword");
16057 return true;
16060 /* Parse an alias-declaration.
16062 alias-declaration:
16063 using identifier attribute-specifier-seq [opt] = type-id */
16065 static tree
16066 cp_parser_alias_declaration (cp_parser* parser)
16068 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16069 location_t id_location;
16070 cp_declarator *declarator;
16071 cp_decl_specifier_seq decl_specs;
16072 bool member_p;
16073 const char *saved_message = NULL;
16075 /* Look for the `using' keyword. */
16076 cp_token *using_token
16077 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16078 if (using_token == NULL)
16079 return error_mark_node;
16081 id_location = cp_lexer_peek_token (parser->lexer)->location;
16082 id = cp_parser_identifier (parser);
16083 if (id == error_mark_node)
16084 return error_mark_node;
16086 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16087 attributes = cp_parser_attributes_opt (parser);
16088 if (attributes == error_mark_node)
16089 return error_mark_node;
16091 cp_parser_require (parser, CPP_EQ, RT_EQ);
16093 if (cp_parser_error_occurred (parser))
16094 return error_mark_node;
16096 cp_parser_commit_to_tentative_parse (parser);
16098 /* Now we are going to parse the type-id of the declaration. */
16101 [dcl.type]/3 says:
16103 "A type-specifier-seq shall not define a class or enumeration
16104 unless it appears in the type-id of an alias-declaration (7.1.3) that
16105 is not the declaration of a template-declaration."
16107 In other words, if we currently are in an alias template, the
16108 type-id should not define a type.
16110 So let's set parser->type_definition_forbidden_message in that
16111 case; cp_parser_check_type_definition (called by
16112 cp_parser_class_specifier) will then emit an error if a type is
16113 defined in the type-id. */
16114 if (parser->num_template_parameter_lists)
16116 saved_message = parser->type_definition_forbidden_message;
16117 parser->type_definition_forbidden_message =
16118 G_("types may not be defined in alias template declarations");
16121 type = cp_parser_type_id (parser);
16123 /* Restore the error message if need be. */
16124 if (parser->num_template_parameter_lists)
16125 parser->type_definition_forbidden_message = saved_message;
16127 if (type == error_mark_node)
16129 cp_parser_skip_to_end_of_block_or_statement (parser);
16130 return error_mark_node;
16133 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16135 if (cp_parser_error_occurred (parser))
16137 cp_parser_skip_to_end_of_block_or_statement (parser);
16138 return error_mark_node;
16141 /* A typedef-name can also be introduced by an alias-declaration. The
16142 identifier following the using keyword becomes a typedef-name. It has
16143 the same semantics as if it were introduced by the typedef
16144 specifier. In particular, it does not define a new type and it shall
16145 not appear in the type-id. */
16147 clear_decl_specs (&decl_specs);
16148 decl_specs.type = type;
16149 if (attributes != NULL_TREE)
16151 decl_specs.attributes = attributes;
16152 set_and_check_decl_spec_loc (&decl_specs,
16153 ds_attribute,
16154 attrs_token);
16156 set_and_check_decl_spec_loc (&decl_specs,
16157 ds_typedef,
16158 using_token);
16159 set_and_check_decl_spec_loc (&decl_specs,
16160 ds_alias,
16161 using_token);
16163 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16164 declarator->id_loc = id_location;
16166 member_p = at_class_scope_p ();
16167 if (member_p)
16168 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16169 NULL_TREE, attributes);
16170 else
16171 decl = start_decl (declarator, &decl_specs, 0,
16172 attributes, NULL_TREE, &pushed_scope);
16173 if (decl == error_mark_node)
16174 return decl;
16176 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16178 if (pushed_scope)
16179 pop_scope (pushed_scope);
16181 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16182 added into the symbol table; otherwise, return the TYPE_DECL. */
16183 if (DECL_LANG_SPECIFIC (decl)
16184 && DECL_TEMPLATE_INFO (decl)
16185 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16187 decl = DECL_TI_TEMPLATE (decl);
16188 if (member_p)
16189 check_member_template (decl);
16192 return decl;
16195 /* Parse a using-directive.
16197 using-directive:
16198 using namespace :: [opt] nested-name-specifier [opt]
16199 namespace-name ; */
16201 static void
16202 cp_parser_using_directive (cp_parser* parser)
16204 tree namespace_decl;
16205 tree attribs;
16207 /* Look for the `using' keyword. */
16208 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16209 /* And the `namespace' keyword. */
16210 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16211 /* Look for the optional `::' operator. */
16212 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16213 /* And the optional nested-name-specifier. */
16214 cp_parser_nested_name_specifier_opt (parser,
16215 /*typename_keyword_p=*/false,
16216 /*check_dependency_p=*/true,
16217 /*type_p=*/false,
16218 /*is_declaration=*/true);
16219 /* Get the namespace being used. */
16220 namespace_decl = cp_parser_namespace_name (parser);
16221 /* And any specified attributes. */
16222 attribs = cp_parser_attributes_opt (parser);
16223 /* Update the symbol table. */
16224 parse_using_directive (namespace_decl, attribs);
16225 /* Look for the final `;'. */
16226 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16229 /* Parse an asm-definition.
16231 asm-definition:
16232 asm ( string-literal ) ;
16234 GNU Extension:
16236 asm-definition:
16237 asm volatile [opt] ( string-literal ) ;
16238 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16239 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16240 : asm-operand-list [opt] ) ;
16241 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16242 : asm-operand-list [opt]
16243 : asm-clobber-list [opt] ) ;
16244 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16245 : asm-clobber-list [opt]
16246 : asm-goto-list ) ; */
16248 static void
16249 cp_parser_asm_definition (cp_parser* parser)
16251 tree string;
16252 tree outputs = NULL_TREE;
16253 tree inputs = NULL_TREE;
16254 tree clobbers = NULL_TREE;
16255 tree labels = NULL_TREE;
16256 tree asm_stmt;
16257 bool volatile_p = false;
16258 bool extended_p = false;
16259 bool invalid_inputs_p = false;
16260 bool invalid_outputs_p = false;
16261 bool goto_p = false;
16262 required_token missing = RT_NONE;
16264 /* Look for the `asm' keyword. */
16265 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16266 /* See if the next token is `volatile'. */
16267 if (cp_parser_allow_gnu_extensions_p (parser)
16268 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16270 /* Remember that we saw the `volatile' keyword. */
16271 volatile_p = true;
16272 /* Consume the token. */
16273 cp_lexer_consume_token (parser->lexer);
16275 if (cp_parser_allow_gnu_extensions_p (parser)
16276 && parser->in_function_body
16277 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16279 /* Remember that we saw the `goto' keyword. */
16280 goto_p = true;
16281 /* Consume the token. */
16282 cp_lexer_consume_token (parser->lexer);
16284 /* Look for the opening `('. */
16285 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16286 return;
16287 /* Look for the string. */
16288 string = cp_parser_string_literal (parser, false, false);
16289 if (string == error_mark_node)
16291 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16292 /*consume_paren=*/true);
16293 return;
16296 /* If we're allowing GNU extensions, check for the extended assembly
16297 syntax. Unfortunately, the `:' tokens need not be separated by
16298 a space in C, and so, for compatibility, we tolerate that here
16299 too. Doing that means that we have to treat the `::' operator as
16300 two `:' tokens. */
16301 if (cp_parser_allow_gnu_extensions_p (parser)
16302 && parser->in_function_body
16303 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16304 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16306 bool inputs_p = false;
16307 bool clobbers_p = false;
16308 bool labels_p = false;
16310 /* The extended syntax was used. */
16311 extended_p = true;
16313 /* Look for outputs. */
16314 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16316 /* Consume the `:'. */
16317 cp_lexer_consume_token (parser->lexer);
16318 /* Parse the output-operands. */
16319 if (cp_lexer_next_token_is_not (parser->lexer,
16320 CPP_COLON)
16321 && cp_lexer_next_token_is_not (parser->lexer,
16322 CPP_SCOPE)
16323 && cp_lexer_next_token_is_not (parser->lexer,
16324 CPP_CLOSE_PAREN)
16325 && !goto_p)
16326 outputs = cp_parser_asm_operand_list (parser);
16328 if (outputs == error_mark_node)
16329 invalid_outputs_p = true;
16331 /* If the next token is `::', there are no outputs, and the
16332 next token is the beginning of the inputs. */
16333 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16334 /* The inputs are coming next. */
16335 inputs_p = true;
16337 /* Look for inputs. */
16338 if (inputs_p
16339 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16341 /* Consume the `:' or `::'. */
16342 cp_lexer_consume_token (parser->lexer);
16343 /* Parse the output-operands. */
16344 if (cp_lexer_next_token_is_not (parser->lexer,
16345 CPP_COLON)
16346 && cp_lexer_next_token_is_not (parser->lexer,
16347 CPP_SCOPE)
16348 && cp_lexer_next_token_is_not (parser->lexer,
16349 CPP_CLOSE_PAREN))
16350 inputs = cp_parser_asm_operand_list (parser);
16352 if (inputs == error_mark_node)
16353 invalid_inputs_p = true;
16355 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16356 /* The clobbers are coming next. */
16357 clobbers_p = true;
16359 /* Look for clobbers. */
16360 if (clobbers_p
16361 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16363 clobbers_p = true;
16364 /* Consume the `:' or `::'. */
16365 cp_lexer_consume_token (parser->lexer);
16366 /* Parse the clobbers. */
16367 if (cp_lexer_next_token_is_not (parser->lexer,
16368 CPP_COLON)
16369 && cp_lexer_next_token_is_not (parser->lexer,
16370 CPP_CLOSE_PAREN))
16371 clobbers = cp_parser_asm_clobber_list (parser);
16373 else if (goto_p
16374 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16375 /* The labels are coming next. */
16376 labels_p = true;
16378 /* Look for labels. */
16379 if (labels_p
16380 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16382 labels_p = true;
16383 /* Consume the `:' or `::'. */
16384 cp_lexer_consume_token (parser->lexer);
16385 /* Parse the labels. */
16386 labels = cp_parser_asm_label_list (parser);
16389 if (goto_p && !labels_p)
16390 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16392 else if (goto_p)
16393 missing = RT_COLON_SCOPE;
16395 /* Look for the closing `)'. */
16396 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16397 missing ? missing : RT_CLOSE_PAREN))
16398 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16399 /*consume_paren=*/true);
16400 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16402 if (!invalid_inputs_p && !invalid_outputs_p)
16404 /* Create the ASM_EXPR. */
16405 if (parser->in_function_body)
16407 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16408 inputs, clobbers, labels);
16409 /* If the extended syntax was not used, mark the ASM_EXPR. */
16410 if (!extended_p)
16412 tree temp = asm_stmt;
16413 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16414 temp = TREE_OPERAND (temp, 0);
16416 ASM_INPUT_P (temp) = 1;
16419 else
16420 add_asm_node (string);
16424 /* Declarators [gram.dcl.decl] */
16426 /* Parse an init-declarator.
16428 init-declarator:
16429 declarator initializer [opt]
16431 GNU Extension:
16433 init-declarator:
16434 declarator asm-specification [opt] attributes [opt] initializer [opt]
16436 function-definition:
16437 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16438 function-body
16439 decl-specifier-seq [opt] declarator function-try-block
16441 GNU Extension:
16443 function-definition:
16444 __extension__ function-definition
16446 TM Extension:
16448 function-definition:
16449 decl-specifier-seq [opt] declarator function-transaction-block
16451 The DECL_SPECIFIERS apply to this declarator. Returns a
16452 representation of the entity declared. If MEMBER_P is TRUE, then
16453 this declarator appears in a class scope. The new DECL created by
16454 this declarator is returned.
16456 The CHECKS are access checks that should be performed once we know
16457 what entity is being declared (and, therefore, what classes have
16458 befriended it).
16460 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16461 for a function-definition here as well. If the declarator is a
16462 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16463 be TRUE upon return. By that point, the function-definition will
16464 have been completely parsed.
16466 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16467 is FALSE.
16469 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16470 parsed declaration if it is an uninitialized single declarator not followed
16471 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16472 if present, will not be consumed. If returned, this declarator will be
16473 created with SD_INITIALIZED but will not call cp_finish_decl. */
16475 static tree
16476 cp_parser_init_declarator (cp_parser* parser,
16477 cp_decl_specifier_seq *decl_specifiers,
16478 vec<deferred_access_check, va_gc> *checks,
16479 bool function_definition_allowed_p,
16480 bool member_p,
16481 int declares_class_or_enum,
16482 bool* function_definition_p,
16483 tree* maybe_range_for_decl)
16485 cp_token *token = NULL, *asm_spec_start_token = NULL,
16486 *attributes_start_token = NULL;
16487 cp_declarator *declarator;
16488 tree prefix_attributes;
16489 tree attributes = NULL;
16490 tree asm_specification;
16491 tree initializer;
16492 tree decl = NULL_TREE;
16493 tree scope;
16494 int is_initialized;
16495 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16496 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16497 "(...)". */
16498 enum cpp_ttype initialization_kind;
16499 bool is_direct_init = false;
16500 bool is_non_constant_init;
16501 int ctor_dtor_or_conv_p;
16502 bool friend_p;
16503 tree pushed_scope = NULL_TREE;
16504 bool range_for_decl_p = false;
16505 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16507 /* Gather the attributes that were provided with the
16508 decl-specifiers. */
16509 prefix_attributes = decl_specifiers->attributes;
16511 /* Assume that this is not the declarator for a function
16512 definition. */
16513 if (function_definition_p)
16514 *function_definition_p = false;
16516 /* Default arguments are only permitted for function parameters. */
16517 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16518 parser->default_arg_ok_p = false;
16520 /* Defer access checks while parsing the declarator; we cannot know
16521 what names are accessible until we know what is being
16522 declared. */
16523 resume_deferring_access_checks ();
16525 /* Parse the declarator. */
16526 token = cp_lexer_peek_token (parser->lexer);
16527 declarator
16528 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16529 &ctor_dtor_or_conv_p,
16530 /*parenthesized_p=*/NULL,
16531 member_p);
16532 /* Gather up the deferred checks. */
16533 stop_deferring_access_checks ();
16535 parser->default_arg_ok_p = saved_default_arg_ok_p;
16537 /* If the DECLARATOR was erroneous, there's no need to go
16538 further. */
16539 if (declarator == cp_error_declarator)
16540 return error_mark_node;
16542 /* Check that the number of template-parameter-lists is OK. */
16543 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16544 token->location))
16545 return error_mark_node;
16547 if (declares_class_or_enum & 2)
16548 cp_parser_check_for_definition_in_return_type (declarator,
16549 decl_specifiers->type,
16550 decl_specifiers->locations[ds_type_spec]);
16552 /* Figure out what scope the entity declared by the DECLARATOR is
16553 located in. `grokdeclarator' sometimes changes the scope, so
16554 we compute it now. */
16555 scope = get_scope_of_declarator (declarator);
16557 /* Perform any lookups in the declared type which were thought to be
16558 dependent, but are not in the scope of the declarator. */
16559 decl_specifiers->type
16560 = maybe_update_decl_type (decl_specifiers->type, scope);
16562 /* If we're allowing GNU extensions, look for an
16563 asm-specification. */
16564 if (cp_parser_allow_gnu_extensions_p (parser))
16566 /* Look for an asm-specification. */
16567 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16568 asm_specification = cp_parser_asm_specification_opt (parser);
16570 else
16571 asm_specification = NULL_TREE;
16573 /* Look for attributes. */
16574 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16575 attributes = cp_parser_attributes_opt (parser);
16577 /* Peek at the next token. */
16578 token = cp_lexer_peek_token (parser->lexer);
16580 if (function_declarator_p (declarator))
16582 /* Check to see if the token indicates the start of a
16583 function-definition. */
16584 if (cp_parser_token_starts_function_definition_p (token))
16586 if (!function_definition_allowed_p)
16588 /* If a function-definition should not appear here, issue an
16589 error message. */
16590 cp_parser_error (parser,
16591 "a function-definition is not allowed here");
16592 return error_mark_node;
16595 location_t func_brace_location
16596 = cp_lexer_peek_token (parser->lexer)->location;
16598 /* Neither attributes nor an asm-specification are allowed
16599 on a function-definition. */
16600 if (asm_specification)
16601 error_at (asm_spec_start_token->location,
16602 "an asm-specification is not allowed "
16603 "on a function-definition");
16604 if (attributes)
16605 error_at (attributes_start_token->location,
16606 "attributes are not allowed "
16607 "on a function-definition");
16608 /* This is a function-definition. */
16609 *function_definition_p = true;
16611 /* Parse the function definition. */
16612 if (member_p)
16613 decl = cp_parser_save_member_function_body (parser,
16614 decl_specifiers,
16615 declarator,
16616 prefix_attributes);
16617 else
16618 decl =
16619 (cp_parser_function_definition_from_specifiers_and_declarator
16620 (parser, decl_specifiers, prefix_attributes, declarator));
16622 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16624 /* This is where the prologue starts... */
16625 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16626 = func_brace_location;
16629 return decl;
16633 /* [dcl.dcl]
16635 Only in function declarations for constructors, destructors, and
16636 type conversions can the decl-specifier-seq be omitted.
16638 We explicitly postpone this check past the point where we handle
16639 function-definitions because we tolerate function-definitions
16640 that are missing their return types in some modes. */
16641 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16643 cp_parser_error (parser,
16644 "expected constructor, destructor, or type conversion");
16645 return error_mark_node;
16648 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16649 if (token->type == CPP_EQ
16650 || token->type == CPP_OPEN_PAREN
16651 || token->type == CPP_OPEN_BRACE)
16653 is_initialized = SD_INITIALIZED;
16654 initialization_kind = token->type;
16655 if (maybe_range_for_decl)
16656 *maybe_range_for_decl = error_mark_node;
16658 if (token->type == CPP_EQ
16659 && function_declarator_p (declarator))
16661 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16662 if (t2->keyword == RID_DEFAULT)
16663 is_initialized = SD_DEFAULTED;
16664 else if (t2->keyword == RID_DELETE)
16665 is_initialized = SD_DELETED;
16668 else
16670 /* If the init-declarator isn't initialized and isn't followed by a
16671 `,' or `;', it's not a valid init-declarator. */
16672 if (token->type != CPP_COMMA
16673 && token->type != CPP_SEMICOLON)
16675 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16676 range_for_decl_p = true;
16677 else
16679 cp_parser_error (parser, "expected initializer");
16680 return error_mark_node;
16683 is_initialized = SD_UNINITIALIZED;
16684 initialization_kind = CPP_EOF;
16687 /* Because start_decl has side-effects, we should only call it if we
16688 know we're going ahead. By this point, we know that we cannot
16689 possibly be looking at any other construct. */
16690 cp_parser_commit_to_tentative_parse (parser);
16692 /* If the decl specifiers were bad, issue an error now that we're
16693 sure this was intended to be a declarator. Then continue
16694 declaring the variable(s), as int, to try to cut down on further
16695 errors. */
16696 if (decl_specifiers->any_specifiers_p
16697 && decl_specifiers->type == error_mark_node)
16699 cp_parser_error (parser, "invalid type in declaration");
16700 decl_specifiers->type = integer_type_node;
16703 /* Check to see whether or not this declaration is a friend. */
16704 friend_p = cp_parser_friend_p (decl_specifiers);
16706 /* Enter the newly declared entry in the symbol table. If we're
16707 processing a declaration in a class-specifier, we wait until
16708 after processing the initializer. */
16709 if (!member_p)
16711 if (parser->in_unbraced_linkage_specification_p)
16712 decl_specifiers->storage_class = sc_extern;
16713 decl = start_decl (declarator, decl_specifiers,
16714 range_for_decl_p? SD_INITIALIZED : is_initialized,
16715 attributes, prefix_attributes, &pushed_scope);
16716 cp_finalize_omp_declare_simd (parser, decl);
16717 /* Adjust location of decl if declarator->id_loc is more appropriate:
16718 set, and decl wasn't merged with another decl, in which case its
16719 location would be different from input_location, and more accurate. */
16720 if (DECL_P (decl)
16721 && declarator->id_loc != UNKNOWN_LOCATION
16722 && DECL_SOURCE_LOCATION (decl) == input_location)
16723 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16725 else if (scope)
16726 /* Enter the SCOPE. That way unqualified names appearing in the
16727 initializer will be looked up in SCOPE. */
16728 pushed_scope = push_scope (scope);
16730 /* Perform deferred access control checks, now that we know in which
16731 SCOPE the declared entity resides. */
16732 if (!member_p && decl)
16734 tree saved_current_function_decl = NULL_TREE;
16736 /* If the entity being declared is a function, pretend that we
16737 are in its scope. If it is a `friend', it may have access to
16738 things that would not otherwise be accessible. */
16739 if (TREE_CODE (decl) == FUNCTION_DECL)
16741 saved_current_function_decl = current_function_decl;
16742 current_function_decl = decl;
16745 /* Perform access checks for template parameters. */
16746 cp_parser_perform_template_parameter_access_checks (checks);
16748 /* Perform the access control checks for the declarator and the
16749 decl-specifiers. */
16750 perform_deferred_access_checks (tf_warning_or_error);
16752 /* Restore the saved value. */
16753 if (TREE_CODE (decl) == FUNCTION_DECL)
16754 current_function_decl = saved_current_function_decl;
16757 /* Parse the initializer. */
16758 initializer = NULL_TREE;
16759 is_direct_init = false;
16760 is_non_constant_init = true;
16761 if (is_initialized)
16763 if (function_declarator_p (declarator))
16765 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16766 if (initialization_kind == CPP_EQ)
16767 initializer = cp_parser_pure_specifier (parser);
16768 else
16770 /* If the declaration was erroneous, we don't really
16771 know what the user intended, so just silently
16772 consume the initializer. */
16773 if (decl != error_mark_node)
16774 error_at (initializer_start_token->location,
16775 "initializer provided for function");
16776 cp_parser_skip_to_closing_parenthesis (parser,
16777 /*recovering=*/true,
16778 /*or_comma=*/false,
16779 /*consume_paren=*/true);
16782 else
16784 /* We want to record the extra mangling scope for in-class
16785 initializers of class members and initializers of static data
16786 member templates. The former involves deferring
16787 parsing of the initializer until end of class as with default
16788 arguments. So right here we only handle the latter. */
16789 if (!member_p && processing_template_decl)
16790 start_lambda_scope (decl);
16791 initializer = cp_parser_initializer (parser,
16792 &is_direct_init,
16793 &is_non_constant_init);
16794 if (!member_p && processing_template_decl)
16795 finish_lambda_scope ();
16796 if (initializer == error_mark_node)
16797 cp_parser_skip_to_end_of_statement (parser);
16801 /* The old parser allows attributes to appear after a parenthesized
16802 initializer. Mark Mitchell proposed removing this functionality
16803 on the GCC mailing lists on 2002-08-13. This parser accepts the
16804 attributes -- but ignores them. */
16805 if (cp_parser_allow_gnu_extensions_p (parser)
16806 && initialization_kind == CPP_OPEN_PAREN)
16807 if (cp_parser_attributes_opt (parser))
16808 warning (OPT_Wattributes,
16809 "attributes after parenthesized initializer ignored");
16811 /* A non-template declaration involving a function parameter list containing
16812 an implicit template parameter will have been made into a template. If it
16813 turns out that the resulting declaration is not an actual function then
16814 finish the template declaration here. An error message will already have
16815 been issued. */
16816 if (parser->fully_implicit_function_template_p)
16817 if (!function_declarator_p (declarator))
16818 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16820 /* For an in-class declaration, use `grokfield' to create the
16821 declaration. */
16822 if (member_p)
16824 if (pushed_scope)
16826 pop_scope (pushed_scope);
16827 pushed_scope = NULL_TREE;
16829 decl = grokfield (declarator, decl_specifiers,
16830 initializer, !is_non_constant_init,
16831 /*asmspec=*/NULL_TREE,
16832 chainon (attributes, prefix_attributes));
16833 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16834 cp_parser_save_default_args (parser, decl);
16835 cp_finalize_omp_declare_simd (parser, decl);
16838 /* Finish processing the declaration. But, skip member
16839 declarations. */
16840 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16842 cp_finish_decl (decl,
16843 initializer, !is_non_constant_init,
16844 asm_specification,
16845 /* If the initializer is in parentheses, then this is
16846 a direct-initialization, which means that an
16847 `explicit' constructor is OK. Otherwise, an
16848 `explicit' constructor cannot be used. */
16849 ((is_direct_init || !is_initialized)
16850 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16852 else if ((cxx_dialect != cxx98) && friend_p
16853 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16854 /* Core issue #226 (C++0x only): A default template-argument
16855 shall not be specified in a friend class template
16856 declaration. */
16857 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16858 /*is_partial=*/false, /*is_friend_decl=*/1);
16860 if (!friend_p && pushed_scope)
16861 pop_scope (pushed_scope);
16863 if (function_declarator_p (declarator)
16864 && parser->fully_implicit_function_template_p)
16866 if (member_p)
16867 decl = finish_fully_implicit_template (parser, decl);
16868 else
16869 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16872 return decl;
16875 /* Parse a declarator.
16877 declarator:
16878 direct-declarator
16879 ptr-operator declarator
16881 abstract-declarator:
16882 ptr-operator abstract-declarator [opt]
16883 direct-abstract-declarator
16885 GNU Extensions:
16887 declarator:
16888 attributes [opt] direct-declarator
16889 attributes [opt] ptr-operator declarator
16891 abstract-declarator:
16892 attributes [opt] ptr-operator abstract-declarator [opt]
16893 attributes [opt] direct-abstract-declarator
16895 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16896 detect constructor, destructor or conversion operators. It is set
16897 to -1 if the declarator is a name, and +1 if it is a
16898 function. Otherwise it is set to zero. Usually you just want to
16899 test for >0, but internally the negative value is used.
16901 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16902 a decl-specifier-seq unless it declares a constructor, destructor,
16903 or conversion. It might seem that we could check this condition in
16904 semantic analysis, rather than parsing, but that makes it difficult
16905 to handle something like `f()'. We want to notice that there are
16906 no decl-specifiers, and therefore realize that this is an
16907 expression, not a declaration.)
16909 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16910 the declarator is a direct-declarator of the form "(...)".
16912 MEMBER_P is true iff this declarator is a member-declarator. */
16914 static cp_declarator *
16915 cp_parser_declarator (cp_parser* parser,
16916 cp_parser_declarator_kind dcl_kind,
16917 int* ctor_dtor_or_conv_p,
16918 bool* parenthesized_p,
16919 bool member_p)
16921 cp_declarator *declarator;
16922 enum tree_code code;
16923 cp_cv_quals cv_quals;
16924 tree class_type;
16925 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16927 /* Assume this is not a constructor, destructor, or type-conversion
16928 operator. */
16929 if (ctor_dtor_or_conv_p)
16930 *ctor_dtor_or_conv_p = 0;
16932 if (cp_parser_allow_gnu_extensions_p (parser))
16933 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16935 /* Check for the ptr-operator production. */
16936 cp_parser_parse_tentatively (parser);
16937 /* Parse the ptr-operator. */
16938 code = cp_parser_ptr_operator (parser,
16939 &class_type,
16940 &cv_quals,
16941 &std_attributes);
16943 /* If that worked, then we have a ptr-operator. */
16944 if (cp_parser_parse_definitely (parser))
16946 /* If a ptr-operator was found, then this declarator was not
16947 parenthesized. */
16948 if (parenthesized_p)
16949 *parenthesized_p = true;
16950 /* The dependent declarator is optional if we are parsing an
16951 abstract-declarator. */
16952 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16953 cp_parser_parse_tentatively (parser);
16955 /* Parse the dependent declarator. */
16956 declarator = cp_parser_declarator (parser, dcl_kind,
16957 /*ctor_dtor_or_conv_p=*/NULL,
16958 /*parenthesized_p=*/NULL,
16959 /*member_p=*/false);
16961 /* If we are parsing an abstract-declarator, we must handle the
16962 case where the dependent declarator is absent. */
16963 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16964 && !cp_parser_parse_definitely (parser))
16965 declarator = NULL;
16967 declarator = cp_parser_make_indirect_declarator
16968 (code, class_type, cv_quals, declarator, std_attributes);
16970 /* Everything else is a direct-declarator. */
16971 else
16973 if (parenthesized_p)
16974 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16975 CPP_OPEN_PAREN);
16976 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16977 ctor_dtor_or_conv_p,
16978 member_p);
16981 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16982 declarator->attributes = gnu_attributes;
16983 return declarator;
16986 /* Parse a direct-declarator or direct-abstract-declarator.
16988 direct-declarator:
16989 declarator-id
16990 direct-declarator ( parameter-declaration-clause )
16991 cv-qualifier-seq [opt]
16992 ref-qualifier [opt]
16993 exception-specification [opt]
16994 direct-declarator [ constant-expression [opt] ]
16995 ( declarator )
16997 direct-abstract-declarator:
16998 direct-abstract-declarator [opt]
16999 ( parameter-declaration-clause )
17000 cv-qualifier-seq [opt]
17001 ref-qualifier [opt]
17002 exception-specification [opt]
17003 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17004 ( abstract-declarator )
17006 Returns a representation of the declarator. DCL_KIND is
17007 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17008 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17009 we are parsing a direct-declarator. It is
17010 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17011 of ambiguity we prefer an abstract declarator, as per
17012 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17013 cp_parser_declarator. */
17015 static cp_declarator *
17016 cp_parser_direct_declarator (cp_parser* parser,
17017 cp_parser_declarator_kind dcl_kind,
17018 int* ctor_dtor_or_conv_p,
17019 bool member_p)
17021 cp_token *token;
17022 cp_declarator *declarator = NULL;
17023 tree scope = NULL_TREE;
17024 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17025 bool saved_in_declarator_p = parser->in_declarator_p;
17026 bool first = true;
17027 tree pushed_scope = NULL_TREE;
17029 while (true)
17031 /* Peek at the next token. */
17032 token = cp_lexer_peek_token (parser->lexer);
17033 if (token->type == CPP_OPEN_PAREN)
17035 /* This is either a parameter-declaration-clause, or a
17036 parenthesized declarator. When we know we are parsing a
17037 named declarator, it must be a parenthesized declarator
17038 if FIRST is true. For instance, `(int)' is a
17039 parameter-declaration-clause, with an omitted
17040 direct-abstract-declarator. But `((*))', is a
17041 parenthesized abstract declarator. Finally, when T is a
17042 template parameter `(T)' is a
17043 parameter-declaration-clause, and not a parenthesized
17044 named declarator.
17046 We first try and parse a parameter-declaration-clause,
17047 and then try a nested declarator (if FIRST is true).
17049 It is not an error for it not to be a
17050 parameter-declaration-clause, even when FIRST is
17051 false. Consider,
17053 int i (int);
17054 int i (3);
17056 The first is the declaration of a function while the
17057 second is the definition of a variable, including its
17058 initializer.
17060 Having seen only the parenthesis, we cannot know which of
17061 these two alternatives should be selected. Even more
17062 complex are examples like:
17064 int i (int (a));
17065 int i (int (3));
17067 The former is a function-declaration; the latter is a
17068 variable initialization.
17070 Thus again, we try a parameter-declaration-clause, and if
17071 that fails, we back out and return. */
17073 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17075 tree params;
17076 bool is_declarator = false;
17078 /* In a member-declarator, the only valid interpretation
17079 of a parenthesis is the start of a
17080 parameter-declaration-clause. (It is invalid to
17081 initialize a static data member with a parenthesized
17082 initializer; only the "=" form of initialization is
17083 permitted.) */
17084 if (!member_p)
17085 cp_parser_parse_tentatively (parser);
17087 /* Consume the `('. */
17088 cp_lexer_consume_token (parser->lexer);
17089 if (first)
17091 /* If this is going to be an abstract declarator, we're
17092 in a declarator and we can't have default args. */
17093 parser->default_arg_ok_p = false;
17094 parser->in_declarator_p = true;
17097 begin_scope (sk_function_parms, NULL_TREE);
17099 /* Parse the parameter-declaration-clause. */
17100 params = cp_parser_parameter_declaration_clause (parser);
17102 /* Consume the `)'. */
17103 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17105 /* If all went well, parse the cv-qualifier-seq,
17106 ref-qualifier and the exception-specification. */
17107 if (member_p || cp_parser_parse_definitely (parser))
17109 cp_cv_quals cv_quals;
17110 cp_virt_specifiers virt_specifiers;
17111 cp_ref_qualifier ref_qual;
17112 tree exception_specification;
17113 tree late_return;
17114 tree attrs;
17115 bool memfn = (member_p || (pushed_scope
17116 && CLASS_TYPE_P (pushed_scope)));
17118 is_declarator = true;
17120 if (ctor_dtor_or_conv_p)
17121 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17122 first = false;
17124 /* Parse the cv-qualifier-seq. */
17125 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17126 /* Parse the ref-qualifier. */
17127 ref_qual = cp_parser_ref_qualifier_opt (parser);
17128 /* And the exception-specification. */
17129 exception_specification
17130 = cp_parser_exception_specification_opt (parser);
17132 attrs = cp_parser_std_attribute_spec_seq (parser);
17134 /* In here, we handle cases where attribute is used after
17135 the function declaration. For example:
17136 void func (int x) __attribute__((vector(..))); */
17137 if (flag_cilkplus
17138 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17140 cp_parser_parse_tentatively (parser);
17141 tree attr = cp_parser_gnu_attributes_opt (parser);
17142 if (cp_lexer_next_token_is_not (parser->lexer,
17143 CPP_SEMICOLON)
17144 && cp_lexer_next_token_is_not (parser->lexer,
17145 CPP_OPEN_BRACE))
17146 cp_parser_abort_tentative_parse (parser);
17147 else if (!cp_parser_parse_definitely (parser))
17149 else
17150 attrs = chainon (attr, attrs);
17152 late_return = (cp_parser_late_return_type_opt
17153 (parser, declarator,
17154 memfn ? cv_quals : -1));
17157 /* Parse the virt-specifier-seq. */
17158 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17160 /* Create the function-declarator. */
17161 declarator = make_call_declarator (declarator,
17162 params,
17163 cv_quals,
17164 virt_specifiers,
17165 ref_qual,
17166 exception_specification,
17167 late_return);
17168 declarator->std_attributes = attrs;
17169 /* Any subsequent parameter lists are to do with
17170 return type, so are not those of the declared
17171 function. */
17172 parser->default_arg_ok_p = false;
17175 /* Remove the function parms from scope. */
17176 pop_bindings_and_leave_scope ();
17178 if (is_declarator)
17179 /* Repeat the main loop. */
17180 continue;
17183 /* If this is the first, we can try a parenthesized
17184 declarator. */
17185 if (first)
17187 bool saved_in_type_id_in_expr_p;
17189 parser->default_arg_ok_p = saved_default_arg_ok_p;
17190 parser->in_declarator_p = saved_in_declarator_p;
17192 /* Consume the `('. */
17193 cp_lexer_consume_token (parser->lexer);
17194 /* Parse the nested declarator. */
17195 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17196 parser->in_type_id_in_expr_p = true;
17197 declarator
17198 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17199 /*parenthesized_p=*/NULL,
17200 member_p);
17201 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17202 first = false;
17203 /* Expect a `)'. */
17204 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17205 declarator = cp_error_declarator;
17206 if (declarator == cp_error_declarator)
17207 break;
17209 goto handle_declarator;
17211 /* Otherwise, we must be done. */
17212 else
17213 break;
17215 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17216 && token->type == CPP_OPEN_SQUARE
17217 && !cp_next_tokens_can_be_attribute_p (parser))
17219 /* Parse an array-declarator. */
17220 tree bounds, attrs;
17222 if (ctor_dtor_or_conv_p)
17223 *ctor_dtor_or_conv_p = 0;
17225 first = false;
17226 parser->default_arg_ok_p = false;
17227 parser->in_declarator_p = true;
17228 /* Consume the `['. */
17229 cp_lexer_consume_token (parser->lexer);
17230 /* Peek at the next token. */
17231 token = cp_lexer_peek_token (parser->lexer);
17232 /* If the next token is `]', then there is no
17233 constant-expression. */
17234 if (token->type != CPP_CLOSE_SQUARE)
17236 bool non_constant_p;
17237 bounds
17238 = cp_parser_constant_expression (parser,
17239 /*allow_non_constant=*/true,
17240 &non_constant_p);
17241 if (!non_constant_p)
17242 /* OK */;
17243 else if (error_operand_p (bounds))
17244 /* Already gave an error. */;
17245 else if (!parser->in_function_body
17246 || current_binding_level->kind == sk_function_parms)
17248 /* Normally, the array bound must be an integral constant
17249 expression. However, as an extension, we allow VLAs
17250 in function scopes as long as they aren't part of a
17251 parameter declaration. */
17252 cp_parser_error (parser,
17253 "array bound is not an integer constant");
17254 bounds = error_mark_node;
17256 else if (processing_template_decl
17257 && !type_dependent_expression_p (bounds))
17259 /* Remember this wasn't a constant-expression. */
17260 bounds = build_nop (TREE_TYPE (bounds), bounds);
17261 TREE_SIDE_EFFECTS (bounds) = 1;
17264 else
17265 bounds = NULL_TREE;
17266 /* Look for the closing `]'. */
17267 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17269 declarator = cp_error_declarator;
17270 break;
17273 attrs = cp_parser_std_attribute_spec_seq (parser);
17274 declarator = make_array_declarator (declarator, bounds);
17275 declarator->std_attributes = attrs;
17277 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17280 tree qualifying_scope;
17281 tree unqualified_name;
17282 tree attrs;
17283 special_function_kind sfk;
17284 bool abstract_ok;
17285 bool pack_expansion_p = false;
17286 cp_token *declarator_id_start_token;
17288 /* Parse a declarator-id */
17289 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17290 if (abstract_ok)
17292 cp_parser_parse_tentatively (parser);
17294 /* If we see an ellipsis, we should be looking at a
17295 parameter pack. */
17296 if (token->type == CPP_ELLIPSIS)
17298 /* Consume the `...' */
17299 cp_lexer_consume_token (parser->lexer);
17301 pack_expansion_p = true;
17305 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17306 unqualified_name
17307 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17308 qualifying_scope = parser->scope;
17309 if (abstract_ok)
17311 bool okay = false;
17313 if (!unqualified_name && pack_expansion_p)
17315 /* Check whether an error occurred. */
17316 okay = !cp_parser_error_occurred (parser);
17318 /* We already consumed the ellipsis to mark a
17319 parameter pack, but we have no way to report it,
17320 so abort the tentative parse. We will be exiting
17321 immediately anyway. */
17322 cp_parser_abort_tentative_parse (parser);
17324 else
17325 okay = cp_parser_parse_definitely (parser);
17327 if (!okay)
17328 unqualified_name = error_mark_node;
17329 else if (unqualified_name
17330 && (qualifying_scope
17331 || (!identifier_p (unqualified_name))))
17333 cp_parser_error (parser, "expected unqualified-id");
17334 unqualified_name = error_mark_node;
17338 if (!unqualified_name)
17339 return NULL;
17340 if (unqualified_name == error_mark_node)
17342 declarator = cp_error_declarator;
17343 pack_expansion_p = false;
17344 declarator->parameter_pack_p = false;
17345 break;
17348 attrs = cp_parser_std_attribute_spec_seq (parser);
17350 if (qualifying_scope && at_namespace_scope_p ()
17351 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17353 /* In the declaration of a member of a template class
17354 outside of the class itself, the SCOPE will sometimes
17355 be a TYPENAME_TYPE. For example, given:
17357 template <typename T>
17358 int S<T>::R::i = 3;
17360 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17361 this context, we must resolve S<T>::R to an ordinary
17362 type, rather than a typename type.
17364 The reason we normally avoid resolving TYPENAME_TYPEs
17365 is that a specialization of `S' might render
17366 `S<T>::R' not a type. However, if `S' is
17367 specialized, then this `i' will not be used, so there
17368 is no harm in resolving the types here. */
17369 tree type;
17371 /* Resolve the TYPENAME_TYPE. */
17372 type = resolve_typename_type (qualifying_scope,
17373 /*only_current_p=*/false);
17374 /* If that failed, the declarator is invalid. */
17375 if (TREE_CODE (type) == TYPENAME_TYPE)
17377 if (typedef_variant_p (type))
17378 error_at (declarator_id_start_token->location,
17379 "cannot define member of dependent typedef "
17380 "%qT", type);
17381 else
17382 error_at (declarator_id_start_token->location,
17383 "%<%T::%E%> is not a type",
17384 TYPE_CONTEXT (qualifying_scope),
17385 TYPE_IDENTIFIER (qualifying_scope));
17387 qualifying_scope = type;
17390 sfk = sfk_none;
17392 if (unqualified_name)
17394 tree class_type;
17396 if (qualifying_scope
17397 && CLASS_TYPE_P (qualifying_scope))
17398 class_type = qualifying_scope;
17399 else
17400 class_type = current_class_type;
17402 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17404 tree name_type = TREE_TYPE (unqualified_name);
17405 if (class_type && same_type_p (name_type, class_type))
17407 if (qualifying_scope
17408 && CLASSTYPE_USE_TEMPLATE (name_type))
17410 error_at (declarator_id_start_token->location,
17411 "invalid use of constructor as a template");
17412 inform (declarator_id_start_token->location,
17413 "use %<%T::%D%> instead of %<%T::%D%> to "
17414 "name the constructor in a qualified name",
17415 class_type,
17416 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17417 class_type, name_type);
17418 declarator = cp_error_declarator;
17419 break;
17421 else
17422 unqualified_name = constructor_name (class_type);
17424 else
17426 /* We do not attempt to print the declarator
17427 here because we do not have enough
17428 information about its original syntactic
17429 form. */
17430 cp_parser_error (parser, "invalid declarator");
17431 declarator = cp_error_declarator;
17432 break;
17436 if (class_type)
17438 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17439 sfk = sfk_destructor;
17440 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17441 sfk = sfk_conversion;
17442 else if (/* There's no way to declare a constructor
17443 for an anonymous type, even if the type
17444 got a name for linkage purposes. */
17445 !TYPE_WAS_ANONYMOUS (class_type)
17446 && constructor_name_p (unqualified_name,
17447 class_type))
17449 unqualified_name = constructor_name (class_type);
17450 sfk = sfk_constructor;
17452 else if (is_overloaded_fn (unqualified_name)
17453 && DECL_CONSTRUCTOR_P (get_first_fn
17454 (unqualified_name)))
17455 sfk = sfk_constructor;
17457 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17458 *ctor_dtor_or_conv_p = -1;
17461 declarator = make_id_declarator (qualifying_scope,
17462 unqualified_name,
17463 sfk);
17464 declarator->std_attributes = attrs;
17465 declarator->id_loc = token->location;
17466 declarator->parameter_pack_p = pack_expansion_p;
17468 if (pack_expansion_p)
17469 maybe_warn_variadic_templates ();
17472 handle_declarator:;
17473 scope = get_scope_of_declarator (declarator);
17474 if (scope)
17476 /* Any names that appear after the declarator-id for a
17477 member are looked up in the containing scope. */
17478 if (at_function_scope_p ())
17480 /* But declarations with qualified-ids can't appear in a
17481 function. */
17482 cp_parser_error (parser, "qualified-id in declaration");
17483 declarator = cp_error_declarator;
17484 break;
17486 pushed_scope = push_scope (scope);
17488 parser->in_declarator_p = true;
17489 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17490 || (declarator && declarator->kind == cdk_id))
17491 /* Default args are only allowed on function
17492 declarations. */
17493 parser->default_arg_ok_p = saved_default_arg_ok_p;
17494 else
17495 parser->default_arg_ok_p = false;
17497 first = false;
17499 /* We're done. */
17500 else
17501 break;
17504 /* For an abstract declarator, we might wind up with nothing at this
17505 point. That's an error; the declarator is not optional. */
17506 if (!declarator)
17507 cp_parser_error (parser, "expected declarator");
17509 /* If we entered a scope, we must exit it now. */
17510 if (pushed_scope)
17511 pop_scope (pushed_scope);
17513 parser->default_arg_ok_p = saved_default_arg_ok_p;
17514 parser->in_declarator_p = saved_in_declarator_p;
17516 return declarator;
17519 /* Parse a ptr-operator.
17521 ptr-operator:
17522 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17523 * cv-qualifier-seq [opt]
17525 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17526 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17528 GNU Extension:
17530 ptr-operator:
17531 & cv-qualifier-seq [opt]
17533 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17534 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17535 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17536 filled in with the TYPE containing the member. *CV_QUALS is
17537 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17538 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17539 Note that the tree codes returned by this function have nothing
17540 to do with the types of trees that will be eventually be created
17541 to represent the pointer or reference type being parsed. They are
17542 just constants with suggestive names. */
17543 static enum tree_code
17544 cp_parser_ptr_operator (cp_parser* parser,
17545 tree* type,
17546 cp_cv_quals *cv_quals,
17547 tree *attributes)
17549 enum tree_code code = ERROR_MARK;
17550 cp_token *token;
17551 tree attrs = NULL_TREE;
17553 /* Assume that it's not a pointer-to-member. */
17554 *type = NULL_TREE;
17555 /* And that there are no cv-qualifiers. */
17556 *cv_quals = TYPE_UNQUALIFIED;
17558 /* Peek at the next token. */
17559 token = cp_lexer_peek_token (parser->lexer);
17561 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17562 if (token->type == CPP_MULT)
17563 code = INDIRECT_REF;
17564 else if (token->type == CPP_AND)
17565 code = ADDR_EXPR;
17566 else if ((cxx_dialect != cxx98) &&
17567 token->type == CPP_AND_AND) /* C++0x only */
17568 code = NON_LVALUE_EXPR;
17570 if (code != ERROR_MARK)
17572 /* Consume the `*', `&' or `&&'. */
17573 cp_lexer_consume_token (parser->lexer);
17575 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17576 `&', if we are allowing GNU extensions. (The only qualifier
17577 that can legally appear after `&' is `restrict', but that is
17578 enforced during semantic analysis. */
17579 if (code == INDIRECT_REF
17580 || cp_parser_allow_gnu_extensions_p (parser))
17581 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17583 attrs = cp_parser_std_attribute_spec_seq (parser);
17584 if (attributes != NULL)
17585 *attributes = attrs;
17587 else
17589 /* Try the pointer-to-member case. */
17590 cp_parser_parse_tentatively (parser);
17591 /* Look for the optional `::' operator. */
17592 cp_parser_global_scope_opt (parser,
17593 /*current_scope_valid_p=*/false);
17594 /* Look for the nested-name specifier. */
17595 token = cp_lexer_peek_token (parser->lexer);
17596 cp_parser_nested_name_specifier (parser,
17597 /*typename_keyword_p=*/false,
17598 /*check_dependency_p=*/true,
17599 /*type_p=*/false,
17600 /*is_declaration=*/false);
17601 /* If we found it, and the next token is a `*', then we are
17602 indeed looking at a pointer-to-member operator. */
17603 if (!cp_parser_error_occurred (parser)
17604 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17606 /* Indicate that the `*' operator was used. */
17607 code = INDIRECT_REF;
17609 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17610 error_at (token->location, "%qD is a namespace", parser->scope);
17611 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17612 error_at (token->location, "cannot form pointer to member of "
17613 "non-class %q#T", parser->scope);
17614 else
17616 /* The type of which the member is a member is given by the
17617 current SCOPE. */
17618 *type = parser->scope;
17619 /* The next name will not be qualified. */
17620 parser->scope = NULL_TREE;
17621 parser->qualifying_scope = NULL_TREE;
17622 parser->object_scope = NULL_TREE;
17623 /* Look for optional c++11 attributes. */
17624 attrs = cp_parser_std_attribute_spec_seq (parser);
17625 if (attributes != NULL)
17626 *attributes = attrs;
17627 /* Look for the optional cv-qualifier-seq. */
17628 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17631 /* If that didn't work we don't have a ptr-operator. */
17632 if (!cp_parser_parse_definitely (parser))
17633 cp_parser_error (parser, "expected ptr-operator");
17636 return code;
17639 /* Parse an (optional) cv-qualifier-seq.
17641 cv-qualifier-seq:
17642 cv-qualifier cv-qualifier-seq [opt]
17644 cv-qualifier:
17645 const
17646 volatile
17648 GNU Extension:
17650 cv-qualifier:
17651 __restrict__
17653 Returns a bitmask representing the cv-qualifiers. */
17655 static cp_cv_quals
17656 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17658 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17660 while (true)
17662 cp_token *token;
17663 cp_cv_quals cv_qualifier;
17665 /* Peek at the next token. */
17666 token = cp_lexer_peek_token (parser->lexer);
17667 /* See if it's a cv-qualifier. */
17668 switch (token->keyword)
17670 case RID_CONST:
17671 cv_qualifier = TYPE_QUAL_CONST;
17672 break;
17674 case RID_VOLATILE:
17675 cv_qualifier = TYPE_QUAL_VOLATILE;
17676 break;
17678 case RID_RESTRICT:
17679 cv_qualifier = TYPE_QUAL_RESTRICT;
17680 break;
17682 default:
17683 cv_qualifier = TYPE_UNQUALIFIED;
17684 break;
17687 if (!cv_qualifier)
17688 break;
17690 if (cv_quals & cv_qualifier)
17692 error_at (token->location, "duplicate cv-qualifier");
17693 cp_lexer_purge_token (parser->lexer);
17695 else
17697 cp_lexer_consume_token (parser->lexer);
17698 cv_quals |= cv_qualifier;
17702 return cv_quals;
17705 /* Parse an (optional) ref-qualifier
17707 ref-qualifier:
17711 Returns cp_ref_qualifier representing ref-qualifier. */
17713 static cp_ref_qualifier
17714 cp_parser_ref_qualifier_opt (cp_parser* parser)
17716 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17718 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17719 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17720 return ref_qual;
17722 while (true)
17724 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17725 cp_token *token = cp_lexer_peek_token (parser->lexer);
17727 switch (token->type)
17729 case CPP_AND:
17730 curr_ref_qual = REF_QUAL_LVALUE;
17731 break;
17733 case CPP_AND_AND:
17734 curr_ref_qual = REF_QUAL_RVALUE;
17735 break;
17737 default:
17738 curr_ref_qual = REF_QUAL_NONE;
17739 break;
17742 if (!curr_ref_qual)
17743 break;
17744 else if (ref_qual)
17746 error_at (token->location, "multiple ref-qualifiers");
17747 cp_lexer_purge_token (parser->lexer);
17749 else
17751 ref_qual = curr_ref_qual;
17752 cp_lexer_consume_token (parser->lexer);
17756 return ref_qual;
17759 /* Parse an (optional) virt-specifier-seq.
17761 virt-specifier-seq:
17762 virt-specifier virt-specifier-seq [opt]
17764 virt-specifier:
17765 override
17766 final
17768 Returns a bitmask representing the virt-specifiers. */
17770 static cp_virt_specifiers
17771 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17773 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17775 while (true)
17777 cp_token *token;
17778 cp_virt_specifiers virt_specifier;
17780 /* Peek at the next token. */
17781 token = cp_lexer_peek_token (parser->lexer);
17782 /* See if it's a virt-specifier-qualifier. */
17783 if (token->type != CPP_NAME)
17784 break;
17785 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17787 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17788 virt_specifier = VIRT_SPEC_OVERRIDE;
17790 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17792 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17793 virt_specifier = VIRT_SPEC_FINAL;
17795 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17797 virt_specifier = VIRT_SPEC_FINAL;
17799 else
17800 break;
17802 if (virt_specifiers & virt_specifier)
17804 error_at (token->location, "duplicate virt-specifier");
17805 cp_lexer_purge_token (parser->lexer);
17807 else
17809 cp_lexer_consume_token (parser->lexer);
17810 virt_specifiers |= virt_specifier;
17813 return virt_specifiers;
17816 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17817 is in scope even though it isn't real. */
17819 static void
17820 inject_this_parameter (tree ctype, cp_cv_quals quals)
17822 tree this_parm;
17824 if (current_class_ptr)
17826 /* We don't clear this between NSDMIs. Is it already what we want? */
17827 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17828 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17829 && cp_type_quals (type) == quals)
17830 return;
17833 this_parm = build_this_parm (ctype, quals);
17834 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17835 current_class_ptr = NULL_TREE;
17836 current_class_ref
17837 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17838 current_class_ptr = this_parm;
17841 /* Return true iff our current scope is a non-static data member
17842 initializer. */
17844 bool
17845 parsing_nsdmi (void)
17847 /* We recognize NSDMI context by the context-less 'this' pointer set up
17848 by the function above. */
17849 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17850 return true;
17851 return false;
17854 /* Parse a late-specified return type, if any. This is not a separate
17855 non-terminal, but part of a function declarator, which looks like
17857 -> trailing-type-specifier-seq abstract-declarator(opt)
17859 Returns the type indicated by the type-id.
17861 In addition to this this parses any queued up omp declare simd
17862 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17864 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17865 function. */
17867 static tree
17868 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17869 cp_cv_quals quals)
17871 cp_token *token;
17872 tree type = NULL_TREE;
17873 bool declare_simd_p = (parser->omp_declare_simd
17874 && declarator
17875 && declarator->kind == cdk_id);
17877 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
17878 && declarator && declarator->kind == cdk_id);
17880 /* Peek at the next token. */
17881 token = cp_lexer_peek_token (parser->lexer);
17882 /* A late-specified return type is indicated by an initial '->'. */
17883 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
17884 return NULL_TREE;
17886 tree save_ccp = current_class_ptr;
17887 tree save_ccr = current_class_ref;
17888 if (quals >= 0)
17890 /* DR 1207: 'this' is in scope in the trailing return type. */
17891 inject_this_parameter (current_class_type, quals);
17894 if (token->type == CPP_DEREF)
17896 /* Consume the ->. */
17897 cp_lexer_consume_token (parser->lexer);
17899 type = cp_parser_trailing_type_id (parser);
17902 if (cilk_simd_fn_vector_p)
17903 declarator->std_attributes
17904 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
17905 declarator->std_attributes);
17906 if (declare_simd_p)
17907 declarator->std_attributes
17908 = cp_parser_late_parsing_omp_declare_simd (parser,
17909 declarator->std_attributes);
17911 if (quals >= 0)
17913 current_class_ptr = save_ccp;
17914 current_class_ref = save_ccr;
17917 return type;
17920 /* Parse a declarator-id.
17922 declarator-id:
17923 id-expression
17924 :: [opt] nested-name-specifier [opt] type-name
17926 In the `id-expression' case, the value returned is as for
17927 cp_parser_id_expression if the id-expression was an unqualified-id.
17928 If the id-expression was a qualified-id, then a SCOPE_REF is
17929 returned. The first operand is the scope (either a NAMESPACE_DECL
17930 or TREE_TYPE), but the second is still just a representation of an
17931 unqualified-id. */
17933 static tree
17934 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17936 tree id;
17937 /* The expression must be an id-expression. Assume that qualified
17938 names are the names of types so that:
17940 template <class T>
17941 int S<T>::R::i = 3;
17943 will work; we must treat `S<T>::R' as the name of a type.
17944 Similarly, assume that qualified names are templates, where
17945 required, so that:
17947 template <class T>
17948 int S<T>::R<T>::i = 3;
17950 will work, too. */
17951 id = cp_parser_id_expression (parser,
17952 /*template_keyword_p=*/false,
17953 /*check_dependency_p=*/false,
17954 /*template_p=*/NULL,
17955 /*declarator_p=*/true,
17956 optional_p);
17957 if (id && BASELINK_P (id))
17958 id = BASELINK_FUNCTIONS (id);
17959 return id;
17962 /* Parse a type-id.
17964 type-id:
17965 type-specifier-seq abstract-declarator [opt]
17967 Returns the TYPE specified. */
17969 static tree
17970 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17971 bool is_trailing_return)
17973 cp_decl_specifier_seq type_specifier_seq;
17974 cp_declarator *abstract_declarator;
17976 /* Parse the type-specifier-seq. */
17977 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17978 is_trailing_return,
17979 &type_specifier_seq);
17980 if (type_specifier_seq.type == error_mark_node)
17981 return error_mark_node;
17983 /* There might or might not be an abstract declarator. */
17984 cp_parser_parse_tentatively (parser);
17985 /* Look for the declarator. */
17986 abstract_declarator
17987 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17988 /*parenthesized_p=*/NULL,
17989 /*member_p=*/false);
17990 /* Check to see if there really was a declarator. */
17991 if (!cp_parser_parse_definitely (parser))
17992 abstract_declarator = NULL;
17994 if (type_specifier_seq.type
17995 /* None of the valid uses of 'auto' in C++14 involve the type-id
17996 nonterminal, but it is valid in a trailing-return-type. */
17997 && !(cxx_dialect >= cxx1y && is_trailing_return)
17998 && type_uses_auto (type_specifier_seq.type))
18000 /* A type-id with type 'auto' is only ok if the abstract declarator
18001 is a function declarator with a late-specified return type. */
18002 if (abstract_declarator
18003 && abstract_declarator->kind == cdk_function
18004 && abstract_declarator->u.function.late_return_type)
18005 /* OK */;
18006 else
18008 error ("invalid use of %<auto%>");
18009 return error_mark_node;
18013 return groktypename (&type_specifier_seq, abstract_declarator,
18014 is_template_arg);
18017 static tree cp_parser_type_id (cp_parser *parser)
18019 return cp_parser_type_id_1 (parser, false, false);
18022 static tree cp_parser_template_type_arg (cp_parser *parser)
18024 tree r;
18025 const char *saved_message = parser->type_definition_forbidden_message;
18026 parser->type_definition_forbidden_message
18027 = G_("types may not be defined in template arguments");
18028 r = cp_parser_type_id_1 (parser, true, false);
18029 parser->type_definition_forbidden_message = saved_message;
18030 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18032 error ("invalid use of %<auto%> in template argument");
18033 r = error_mark_node;
18035 return r;
18038 static tree cp_parser_trailing_type_id (cp_parser *parser)
18040 return cp_parser_type_id_1 (parser, false, true);
18043 /* Parse a type-specifier-seq.
18045 type-specifier-seq:
18046 type-specifier type-specifier-seq [opt]
18048 GNU extension:
18050 type-specifier-seq:
18051 attributes type-specifier-seq [opt]
18053 If IS_DECLARATION is true, we are at the start of a "condition" or
18054 exception-declaration, so we might be followed by a declarator-id.
18056 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18057 i.e. we've just seen "->".
18059 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18061 static void
18062 cp_parser_type_specifier_seq (cp_parser* parser,
18063 bool is_declaration,
18064 bool is_trailing_return,
18065 cp_decl_specifier_seq *type_specifier_seq)
18067 bool seen_type_specifier = false;
18068 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18069 cp_token *start_token = NULL;
18071 /* Clear the TYPE_SPECIFIER_SEQ. */
18072 clear_decl_specs (type_specifier_seq);
18074 /* In the context of a trailing return type, enum E { } is an
18075 elaborated-type-specifier followed by a function-body, not an
18076 enum-specifier. */
18077 if (is_trailing_return)
18078 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18080 /* Parse the type-specifiers and attributes. */
18081 while (true)
18083 tree type_specifier;
18084 bool is_cv_qualifier;
18086 /* Check for attributes first. */
18087 if (cp_next_tokens_can_be_attribute_p (parser))
18089 type_specifier_seq->attributes =
18090 chainon (type_specifier_seq->attributes,
18091 cp_parser_attributes_opt (parser));
18092 continue;
18095 /* record the token of the beginning of the type specifier seq,
18096 for error reporting purposes*/
18097 if (!start_token)
18098 start_token = cp_lexer_peek_token (parser->lexer);
18100 /* Look for the type-specifier. */
18101 type_specifier = cp_parser_type_specifier (parser,
18102 flags,
18103 type_specifier_seq,
18104 /*is_declaration=*/false,
18105 NULL,
18106 &is_cv_qualifier);
18107 if (!type_specifier)
18109 /* If the first type-specifier could not be found, this is not a
18110 type-specifier-seq at all. */
18111 if (!seen_type_specifier)
18113 /* Set in_declarator_p to avoid skipping to the semicolon. */
18114 int in_decl = parser->in_declarator_p;
18115 parser->in_declarator_p = true;
18117 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18118 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18119 cp_parser_error (parser, "expected type-specifier");
18121 parser->in_declarator_p = in_decl;
18123 type_specifier_seq->type = error_mark_node;
18124 return;
18126 /* If subsequent type-specifiers could not be found, the
18127 type-specifier-seq is complete. */
18128 break;
18131 seen_type_specifier = true;
18132 /* The standard says that a condition can be:
18134 type-specifier-seq declarator = assignment-expression
18136 However, given:
18138 struct S {};
18139 if (int S = ...)
18141 we should treat the "S" as a declarator, not as a
18142 type-specifier. The standard doesn't say that explicitly for
18143 type-specifier-seq, but it does say that for
18144 decl-specifier-seq in an ordinary declaration. Perhaps it
18145 would be clearer just to allow a decl-specifier-seq here, and
18146 then add a semantic restriction that if any decl-specifiers
18147 that are not type-specifiers appear, the program is invalid. */
18148 if (is_declaration && !is_cv_qualifier)
18149 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18153 /* Return whether the function currently being declared has an associated
18154 template parameter list. */
18156 static bool
18157 function_being_declared_is_template_p (cp_parser* parser)
18159 if (!current_template_parms || processing_template_parmlist)
18160 return false;
18162 if (parser->implicit_template_scope)
18163 return true;
18165 if (at_class_scope_p ()
18166 && TYPE_BEING_DEFINED (current_class_type))
18167 return parser->num_template_parameter_lists != 0;
18169 return ((int) parser->num_template_parameter_lists > template_class_depth
18170 (current_class_type));
18173 /* Parse a parameter-declaration-clause.
18175 parameter-declaration-clause:
18176 parameter-declaration-list [opt] ... [opt]
18177 parameter-declaration-list , ...
18179 Returns a representation for the parameter declarations. A return
18180 value of NULL indicates a parameter-declaration-clause consisting
18181 only of an ellipsis. */
18183 static tree
18184 cp_parser_parameter_declaration_clause (cp_parser* parser)
18186 tree parameters;
18187 cp_token *token;
18188 bool ellipsis_p;
18189 bool is_error;
18191 struct cleanup {
18192 cp_parser* parser;
18193 int auto_is_implicit_function_template_parm_p;
18194 ~cleanup() {
18195 parser->auto_is_implicit_function_template_parm_p
18196 = auto_is_implicit_function_template_parm_p;
18198 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18200 (void) cleanup;
18202 if (!processing_specialization && !processing_template_parmlist)
18203 if (!current_function_decl
18204 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18205 parser->auto_is_implicit_function_template_parm_p = true;
18207 /* Peek at the next token. */
18208 token = cp_lexer_peek_token (parser->lexer);
18209 /* Check for trivial parameter-declaration-clauses. */
18210 if (token->type == CPP_ELLIPSIS)
18212 /* Consume the `...' token. */
18213 cp_lexer_consume_token (parser->lexer);
18214 return NULL_TREE;
18216 else if (token->type == CPP_CLOSE_PAREN)
18217 /* There are no parameters. */
18219 #ifndef NO_IMPLICIT_EXTERN_C
18220 if (in_system_header_at (input_location)
18221 && current_class_type == NULL
18222 && current_lang_name == lang_name_c)
18223 return NULL_TREE;
18224 else
18225 #endif
18226 return void_list_node;
18228 /* Check for `(void)', too, which is a special case. */
18229 else if (token->keyword == RID_VOID
18230 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18231 == CPP_CLOSE_PAREN))
18233 /* Consume the `void' token. */
18234 cp_lexer_consume_token (parser->lexer);
18235 /* There are no parameters. */
18236 return void_list_node;
18239 /* Parse the parameter-declaration-list. */
18240 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18241 /* If a parse error occurred while parsing the
18242 parameter-declaration-list, then the entire
18243 parameter-declaration-clause is erroneous. */
18244 if (is_error)
18246 /* Unwind generic function template scope if necessary. */
18247 if (parser->fully_implicit_function_template_p)
18248 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18249 return NULL;
18252 /* Peek at the next token. */
18253 token = cp_lexer_peek_token (parser->lexer);
18254 /* If it's a `,', the clause should terminate with an ellipsis. */
18255 if (token->type == CPP_COMMA)
18257 /* Consume the `,'. */
18258 cp_lexer_consume_token (parser->lexer);
18259 /* Expect an ellipsis. */
18260 ellipsis_p
18261 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18263 /* It might also be `...' if the optional trailing `,' was
18264 omitted. */
18265 else if (token->type == CPP_ELLIPSIS)
18267 /* Consume the `...' token. */
18268 cp_lexer_consume_token (parser->lexer);
18269 /* And remember that we saw it. */
18270 ellipsis_p = true;
18272 else
18273 ellipsis_p = false;
18275 /* Finish the parameter list. */
18276 if (!ellipsis_p)
18277 parameters = chainon (parameters, void_list_node);
18279 return parameters;
18282 /* Parse a parameter-declaration-list.
18284 parameter-declaration-list:
18285 parameter-declaration
18286 parameter-declaration-list , parameter-declaration
18288 Returns a representation of the parameter-declaration-list, as for
18289 cp_parser_parameter_declaration_clause. However, the
18290 `void_list_node' is never appended to the list. Upon return,
18291 *IS_ERROR will be true iff an error occurred. */
18293 static tree
18294 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18296 tree parameters = NULL_TREE;
18297 tree *tail = &parameters;
18298 bool saved_in_unbraced_linkage_specification_p;
18299 int index = 0;
18301 /* Assume all will go well. */
18302 *is_error = false;
18303 /* The special considerations that apply to a function within an
18304 unbraced linkage specifications do not apply to the parameters
18305 to the function. */
18306 saved_in_unbraced_linkage_specification_p
18307 = parser->in_unbraced_linkage_specification_p;
18308 parser->in_unbraced_linkage_specification_p = false;
18310 /* Look for more parameters. */
18311 while (true)
18313 cp_parameter_declarator *parameter;
18314 tree decl = error_mark_node;
18315 bool parenthesized_p = false;
18316 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18317 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18318 (current_template_parms)) : 0);
18320 /* Parse the parameter. */
18321 parameter
18322 = cp_parser_parameter_declaration (parser,
18323 /*template_parm_p=*/false,
18324 &parenthesized_p);
18326 /* We don't know yet if the enclosing context is deprecated, so wait
18327 and warn in grokparms if appropriate. */
18328 deprecated_state = DEPRECATED_SUPPRESS;
18330 if (parameter)
18332 /* If a function parameter pack was specified and an implicit template
18333 parameter was introduced during cp_parser_parameter_declaration,
18334 change any implicit parameters introduced into packs. */
18335 if (parser->implicit_template_parms
18336 && parameter->declarator
18337 && parameter->declarator->parameter_pack_p)
18339 int latest_template_parm_idx = TREE_VEC_LENGTH
18340 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18342 if (latest_template_parm_idx != template_parm_idx)
18343 parameter->decl_specifiers.type = convert_generic_types_to_packs
18344 (parameter->decl_specifiers.type,
18345 template_parm_idx, latest_template_parm_idx);
18348 decl = grokdeclarator (parameter->declarator,
18349 &parameter->decl_specifiers,
18350 PARM,
18351 parameter->default_argument != NULL_TREE,
18352 &parameter->decl_specifiers.attributes);
18355 deprecated_state = DEPRECATED_NORMAL;
18357 /* If a parse error occurred parsing the parameter declaration,
18358 then the entire parameter-declaration-list is erroneous. */
18359 if (decl == error_mark_node)
18361 *is_error = true;
18362 parameters = error_mark_node;
18363 break;
18366 if (parameter->decl_specifiers.attributes)
18367 cplus_decl_attributes (&decl,
18368 parameter->decl_specifiers.attributes,
18370 if (DECL_NAME (decl))
18371 decl = pushdecl (decl);
18373 if (decl != error_mark_node)
18375 retrofit_lang_decl (decl);
18376 DECL_PARM_INDEX (decl) = ++index;
18377 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18380 /* Add the new parameter to the list. */
18381 *tail = build_tree_list (parameter->default_argument, decl);
18382 tail = &TREE_CHAIN (*tail);
18384 /* Peek at the next token. */
18385 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18386 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18387 /* These are for Objective-C++ */
18388 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18389 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18390 /* The parameter-declaration-list is complete. */
18391 break;
18392 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18394 cp_token *token;
18396 /* Peek at the next token. */
18397 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18398 /* If it's an ellipsis, then the list is complete. */
18399 if (token->type == CPP_ELLIPSIS)
18400 break;
18401 /* Otherwise, there must be more parameters. Consume the
18402 `,'. */
18403 cp_lexer_consume_token (parser->lexer);
18404 /* When parsing something like:
18406 int i(float f, double d)
18408 we can tell after seeing the declaration for "f" that we
18409 are not looking at an initialization of a variable "i",
18410 but rather at the declaration of a function "i".
18412 Due to the fact that the parsing of template arguments
18413 (as specified to a template-id) requires backtracking we
18414 cannot use this technique when inside a template argument
18415 list. */
18416 if (!parser->in_template_argument_list_p
18417 && !parser->in_type_id_in_expr_p
18418 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18419 /* However, a parameter-declaration of the form
18420 "float(f)" (which is a valid declaration of a
18421 parameter "f") can also be interpreted as an
18422 expression (the conversion of "f" to "float"). */
18423 && !parenthesized_p)
18424 cp_parser_commit_to_tentative_parse (parser);
18426 else
18428 cp_parser_error (parser, "expected %<,%> or %<...%>");
18429 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18430 cp_parser_skip_to_closing_parenthesis (parser,
18431 /*recovering=*/true,
18432 /*or_comma=*/false,
18433 /*consume_paren=*/false);
18434 break;
18438 parser->in_unbraced_linkage_specification_p
18439 = saved_in_unbraced_linkage_specification_p;
18441 /* Reset implicit_template_scope if we are about to leave the function
18442 parameter list that introduced it. Note that for out-of-line member
18443 definitions, there will be one or more class scopes before we get to
18444 the template parameter scope. */
18446 if (cp_binding_level *its = parser->implicit_template_scope)
18447 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18449 while (maybe_its->kind == sk_class)
18450 maybe_its = maybe_its->level_chain;
18451 if (maybe_its == its)
18453 parser->implicit_template_parms = 0;
18454 parser->implicit_template_scope = 0;
18458 return parameters;
18461 /* Parse a parameter declaration.
18463 parameter-declaration:
18464 decl-specifier-seq ... [opt] declarator
18465 decl-specifier-seq declarator = assignment-expression
18466 decl-specifier-seq ... [opt] abstract-declarator [opt]
18467 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18469 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18470 declares a template parameter. (In that case, a non-nested `>'
18471 token encountered during the parsing of the assignment-expression
18472 is not interpreted as a greater-than operator.)
18474 Returns a representation of the parameter, or NULL if an error
18475 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18476 true iff the declarator is of the form "(p)". */
18478 static cp_parameter_declarator *
18479 cp_parser_parameter_declaration (cp_parser *parser,
18480 bool template_parm_p,
18481 bool *parenthesized_p)
18483 int declares_class_or_enum;
18484 cp_decl_specifier_seq decl_specifiers;
18485 cp_declarator *declarator;
18486 tree default_argument;
18487 cp_token *token = NULL, *declarator_token_start = NULL;
18488 const char *saved_message;
18490 /* In a template parameter, `>' is not an operator.
18492 [temp.param]
18494 When parsing a default template-argument for a non-type
18495 template-parameter, the first non-nested `>' is taken as the end
18496 of the template parameter-list rather than a greater-than
18497 operator. */
18499 /* Type definitions may not appear in parameter types. */
18500 saved_message = parser->type_definition_forbidden_message;
18501 parser->type_definition_forbidden_message
18502 = G_("types may not be defined in parameter types");
18504 /* Parse the declaration-specifiers. */
18505 cp_parser_decl_specifier_seq (parser,
18506 CP_PARSER_FLAGS_NONE,
18507 &decl_specifiers,
18508 &declares_class_or_enum);
18510 /* Complain about missing 'typename' or other invalid type names. */
18511 if (!decl_specifiers.any_type_specifiers_p
18512 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18513 decl_specifiers.type = error_mark_node;
18515 /* If an error occurred, there's no reason to attempt to parse the
18516 rest of the declaration. */
18517 if (cp_parser_error_occurred (parser))
18519 parser->type_definition_forbidden_message = saved_message;
18520 return NULL;
18523 /* Peek at the next token. */
18524 token = cp_lexer_peek_token (parser->lexer);
18526 /* If the next token is a `)', `,', `=', `>', or `...', then there
18527 is no declarator. However, when variadic templates are enabled,
18528 there may be a declarator following `...'. */
18529 if (token->type == CPP_CLOSE_PAREN
18530 || token->type == CPP_COMMA
18531 || token->type == CPP_EQ
18532 || token->type == CPP_GREATER)
18534 declarator = NULL;
18535 if (parenthesized_p)
18536 *parenthesized_p = false;
18538 /* Otherwise, there should be a declarator. */
18539 else
18541 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18542 parser->default_arg_ok_p = false;
18544 /* After seeing a decl-specifier-seq, if the next token is not a
18545 "(", there is no possibility that the code is a valid
18546 expression. Therefore, if parsing tentatively, we commit at
18547 this point. */
18548 if (!parser->in_template_argument_list_p
18549 /* In an expression context, having seen:
18551 (int((char ...
18553 we cannot be sure whether we are looking at a
18554 function-type (taking a "char" as a parameter) or a cast
18555 of some object of type "char" to "int". */
18556 && !parser->in_type_id_in_expr_p
18557 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18558 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18559 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18560 cp_parser_commit_to_tentative_parse (parser);
18561 /* Parse the declarator. */
18562 declarator_token_start = token;
18563 declarator = cp_parser_declarator (parser,
18564 CP_PARSER_DECLARATOR_EITHER,
18565 /*ctor_dtor_or_conv_p=*/NULL,
18566 parenthesized_p,
18567 /*member_p=*/false);
18568 parser->default_arg_ok_p = saved_default_arg_ok_p;
18569 /* After the declarator, allow more attributes. */
18570 decl_specifiers.attributes
18571 = chainon (decl_specifiers.attributes,
18572 cp_parser_attributes_opt (parser));
18575 /* If the next token is an ellipsis, and we have not seen a
18576 declarator name, and the type of the declarator contains parameter
18577 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18578 a parameter pack expansion expression. Otherwise, leave the
18579 ellipsis for a C-style variadic function. */
18580 token = cp_lexer_peek_token (parser->lexer);
18581 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18583 tree type = decl_specifiers.type;
18585 if (type && DECL_P (type))
18586 type = TREE_TYPE (type);
18588 if (type
18589 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18590 && declarator_can_be_parameter_pack (declarator)
18591 && (!declarator || !declarator->parameter_pack_p)
18592 && uses_parameter_packs (type))
18594 /* Consume the `...'. */
18595 cp_lexer_consume_token (parser->lexer);
18596 maybe_warn_variadic_templates ();
18598 /* Build a pack expansion type */
18599 if (declarator)
18600 declarator->parameter_pack_p = true;
18601 else
18602 decl_specifiers.type = make_pack_expansion (type);
18606 /* The restriction on defining new types applies only to the type
18607 of the parameter, not to the default argument. */
18608 parser->type_definition_forbidden_message = saved_message;
18610 /* If the next token is `=', then process a default argument. */
18611 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18613 token = cp_lexer_peek_token (parser->lexer);
18614 /* If we are defining a class, then the tokens that make up the
18615 default argument must be saved and processed later. */
18616 if (!template_parm_p && at_class_scope_p ()
18617 && TYPE_BEING_DEFINED (current_class_type)
18618 && !LAMBDA_TYPE_P (current_class_type))
18619 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18620 /* Outside of a class definition, we can just parse the
18621 assignment-expression. */
18622 else
18623 default_argument
18624 = cp_parser_default_argument (parser, template_parm_p);
18626 if (!parser->default_arg_ok_p)
18628 if (flag_permissive)
18629 warning (0, "deprecated use of default argument for parameter of non-function");
18630 else
18632 error_at (token->location,
18633 "default arguments are only "
18634 "permitted for function parameters");
18635 default_argument = NULL_TREE;
18638 else if ((declarator && declarator->parameter_pack_p)
18639 || (decl_specifiers.type
18640 && PACK_EXPANSION_P (decl_specifiers.type)))
18642 /* Find the name of the parameter pack. */
18643 cp_declarator *id_declarator = declarator;
18644 while (id_declarator && id_declarator->kind != cdk_id)
18645 id_declarator = id_declarator->declarator;
18647 if (id_declarator && id_declarator->kind == cdk_id)
18648 error_at (declarator_token_start->location,
18649 template_parm_p
18650 ? G_("template parameter pack %qD "
18651 "cannot have a default argument")
18652 : G_("parameter pack %qD cannot have "
18653 "a default argument"),
18654 id_declarator->u.id.unqualified_name);
18655 else
18656 error_at (declarator_token_start->location,
18657 template_parm_p
18658 ? G_("template parameter pack cannot have "
18659 "a default argument")
18660 : G_("parameter pack cannot have a "
18661 "default argument"));
18663 default_argument = NULL_TREE;
18666 else
18667 default_argument = NULL_TREE;
18669 return make_parameter_declarator (&decl_specifiers,
18670 declarator,
18671 default_argument);
18674 /* Parse a default argument and return it.
18676 TEMPLATE_PARM_P is true if this is a default argument for a
18677 non-type template parameter. */
18678 static tree
18679 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18681 tree default_argument = NULL_TREE;
18682 bool saved_greater_than_is_operator_p;
18683 bool saved_local_variables_forbidden_p;
18684 bool non_constant_p, is_direct_init;
18686 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18687 set correctly. */
18688 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18689 parser->greater_than_is_operator_p = !template_parm_p;
18690 /* Local variable names (and the `this' keyword) may not
18691 appear in a default argument. */
18692 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18693 parser->local_variables_forbidden_p = true;
18694 /* Parse the assignment-expression. */
18695 if (template_parm_p)
18696 push_deferring_access_checks (dk_no_deferred);
18697 tree saved_class_ptr = NULL_TREE;
18698 tree saved_class_ref = NULL_TREE;
18699 /* The "this" pointer is not valid in a default argument. */
18700 if (cfun)
18702 saved_class_ptr = current_class_ptr;
18703 cp_function_chain->x_current_class_ptr = NULL_TREE;
18704 saved_class_ref = current_class_ref;
18705 cp_function_chain->x_current_class_ref = NULL_TREE;
18707 default_argument
18708 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18709 /* Restore the "this" pointer. */
18710 if (cfun)
18712 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18713 cp_function_chain->x_current_class_ref = saved_class_ref;
18715 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18716 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18717 if (template_parm_p)
18718 pop_deferring_access_checks ();
18719 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18720 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18722 return default_argument;
18725 /* Parse a function-body.
18727 function-body:
18728 compound_statement */
18730 static void
18731 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18733 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18736 /* Parse a ctor-initializer-opt followed by a function-body. Return
18737 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18738 is true we are parsing a function-try-block. */
18740 static bool
18741 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18742 bool in_function_try_block)
18744 tree body, list;
18745 bool ctor_initializer_p;
18746 const bool check_body_p =
18747 DECL_CONSTRUCTOR_P (current_function_decl)
18748 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18749 tree last = NULL;
18751 /* Begin the function body. */
18752 body = begin_function_body ();
18753 /* Parse the optional ctor-initializer. */
18754 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18756 /* If we're parsing a constexpr constructor definition, we need
18757 to check that the constructor body is indeed empty. However,
18758 before we get to cp_parser_function_body lot of junk has been
18759 generated, so we can't just check that we have an empty block.
18760 Rather we take a snapshot of the outermost block, and check whether
18761 cp_parser_function_body changed its state. */
18762 if (check_body_p)
18764 list = cur_stmt_list;
18765 if (STATEMENT_LIST_TAIL (list))
18766 last = STATEMENT_LIST_TAIL (list)->stmt;
18768 /* Parse the function-body. */
18769 cp_parser_function_body (parser, in_function_try_block);
18770 if (check_body_p)
18771 check_constexpr_ctor_body (last, list);
18772 /* Finish the function body. */
18773 finish_function_body (body);
18775 return ctor_initializer_p;
18778 /* Parse an initializer.
18780 initializer:
18781 = initializer-clause
18782 ( expression-list )
18784 Returns an expression representing the initializer. If no
18785 initializer is present, NULL_TREE is returned.
18787 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18788 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18789 set to TRUE if there is no initializer present. If there is an
18790 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18791 is set to true; otherwise it is set to false. */
18793 static tree
18794 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18795 bool* non_constant_p)
18797 cp_token *token;
18798 tree init;
18800 /* Peek at the next token. */
18801 token = cp_lexer_peek_token (parser->lexer);
18803 /* Let our caller know whether or not this initializer was
18804 parenthesized. */
18805 *is_direct_init = (token->type != CPP_EQ);
18806 /* Assume that the initializer is constant. */
18807 *non_constant_p = false;
18809 if (token->type == CPP_EQ)
18811 /* Consume the `='. */
18812 cp_lexer_consume_token (parser->lexer);
18813 /* Parse the initializer-clause. */
18814 init = cp_parser_initializer_clause (parser, non_constant_p);
18816 else if (token->type == CPP_OPEN_PAREN)
18818 vec<tree, va_gc> *vec;
18819 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18820 /*cast_p=*/false,
18821 /*allow_expansion_p=*/true,
18822 non_constant_p);
18823 if (vec == NULL)
18824 return error_mark_node;
18825 init = build_tree_list_vec (vec);
18826 release_tree_vector (vec);
18828 else if (token->type == CPP_OPEN_BRACE)
18830 cp_lexer_set_source_position (parser->lexer);
18831 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18832 init = cp_parser_braced_list (parser, non_constant_p);
18833 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18835 else
18837 /* Anything else is an error. */
18838 cp_parser_error (parser, "expected initializer");
18839 init = error_mark_node;
18842 return init;
18845 /* Parse an initializer-clause.
18847 initializer-clause:
18848 assignment-expression
18849 braced-init-list
18851 Returns an expression representing the initializer.
18853 If the `assignment-expression' production is used the value
18854 returned is simply a representation for the expression.
18856 Otherwise, calls cp_parser_braced_list. */
18858 static tree
18859 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18861 tree initializer;
18863 /* Assume the expression is constant. */
18864 *non_constant_p = false;
18866 /* If it is not a `{', then we are looking at an
18867 assignment-expression. */
18868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18870 initializer
18871 = cp_parser_constant_expression (parser,
18872 /*allow_non_constant_p=*/true,
18873 non_constant_p);
18875 else
18876 initializer = cp_parser_braced_list (parser, non_constant_p);
18878 return initializer;
18881 /* Parse a brace-enclosed initializer list.
18883 braced-init-list:
18884 { initializer-list , [opt] }
18887 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18888 the elements of the initializer-list (or NULL, if the last
18889 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18890 NULL_TREE. There is no way to detect whether or not the optional
18891 trailing `,' was provided. NON_CONSTANT_P is as for
18892 cp_parser_initializer. */
18894 static tree
18895 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18897 tree initializer;
18899 /* Consume the `{' token. */
18900 cp_lexer_consume_token (parser->lexer);
18901 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18902 initializer = make_node (CONSTRUCTOR);
18903 /* If it's not a `}', then there is a non-trivial initializer. */
18904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18906 /* Parse the initializer list. */
18907 CONSTRUCTOR_ELTS (initializer)
18908 = cp_parser_initializer_list (parser, non_constant_p);
18909 /* A trailing `,' token is allowed. */
18910 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18911 cp_lexer_consume_token (parser->lexer);
18913 else
18914 *non_constant_p = false;
18915 /* Now, there should be a trailing `}'. */
18916 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18917 TREE_TYPE (initializer) = init_list_type_node;
18918 return initializer;
18921 /* Parse an initializer-list.
18923 initializer-list:
18924 initializer-clause ... [opt]
18925 initializer-list , initializer-clause ... [opt]
18927 GNU Extension:
18929 initializer-list:
18930 designation initializer-clause ...[opt]
18931 initializer-list , designation initializer-clause ...[opt]
18933 designation:
18934 . identifier =
18935 identifier :
18936 [ constant-expression ] =
18938 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18939 for the initializer. If the INDEX of the elt is non-NULL, it is the
18940 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18941 as for cp_parser_initializer. */
18943 static vec<constructor_elt, va_gc> *
18944 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18946 vec<constructor_elt, va_gc> *v = NULL;
18948 /* Assume all of the expressions are constant. */
18949 *non_constant_p = false;
18951 /* Parse the rest of the list. */
18952 while (true)
18954 cp_token *token;
18955 tree designator;
18956 tree initializer;
18957 bool clause_non_constant_p;
18959 /* If the next token is an identifier and the following one is a
18960 colon, we are looking at the GNU designated-initializer
18961 syntax. */
18962 if (cp_parser_allow_gnu_extensions_p (parser)
18963 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18964 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18966 /* Warn the user that they are using an extension. */
18967 pedwarn (input_location, OPT_Wpedantic,
18968 "ISO C++ does not allow designated initializers");
18969 /* Consume the identifier. */
18970 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18971 /* Consume the `:'. */
18972 cp_lexer_consume_token (parser->lexer);
18974 /* Also handle the C99 syntax, '. id ='. */
18975 else if (cp_parser_allow_gnu_extensions_p (parser)
18976 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18977 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18978 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18980 /* Warn the user that they are using an extension. */
18981 pedwarn (input_location, OPT_Wpedantic,
18982 "ISO C++ does not allow C99 designated initializers");
18983 /* Consume the `.'. */
18984 cp_lexer_consume_token (parser->lexer);
18985 /* Consume the identifier. */
18986 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18987 /* Consume the `='. */
18988 cp_lexer_consume_token (parser->lexer);
18990 /* Also handle C99 array designators, '[ const ] ='. */
18991 else if (cp_parser_allow_gnu_extensions_p (parser)
18992 && !c_dialect_objc ()
18993 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18995 /* In C++11, [ could start a lambda-introducer. */
18996 bool non_const = false;
18998 cp_parser_parse_tentatively (parser);
18999 cp_lexer_consume_token (parser->lexer);
19000 designator = cp_parser_constant_expression (parser, true, &non_const);
19001 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19002 cp_parser_require (parser, CPP_EQ, RT_EQ);
19003 if (!cp_parser_parse_definitely (parser))
19004 designator = NULL_TREE;
19005 else if (non_const)
19006 require_potential_rvalue_constant_expression (designator);
19008 else
19009 designator = NULL_TREE;
19011 /* Parse the initializer. */
19012 initializer = cp_parser_initializer_clause (parser,
19013 &clause_non_constant_p);
19014 /* If any clause is non-constant, so is the entire initializer. */
19015 if (clause_non_constant_p)
19016 *non_constant_p = true;
19018 /* If we have an ellipsis, this is an initializer pack
19019 expansion. */
19020 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19022 /* Consume the `...'. */
19023 cp_lexer_consume_token (parser->lexer);
19025 /* Turn the initializer into an initializer expansion. */
19026 initializer = make_pack_expansion (initializer);
19029 /* Add it to the vector. */
19030 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19032 /* If the next token is not a comma, we have reached the end of
19033 the list. */
19034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19035 break;
19037 /* Peek at the next token. */
19038 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19039 /* If the next token is a `}', then we're still done. An
19040 initializer-clause can have a trailing `,' after the
19041 initializer-list and before the closing `}'. */
19042 if (token->type == CPP_CLOSE_BRACE)
19043 break;
19045 /* Consume the `,' token. */
19046 cp_lexer_consume_token (parser->lexer);
19049 return v;
19052 /* Classes [gram.class] */
19054 /* Parse a class-name.
19056 class-name:
19057 identifier
19058 template-id
19060 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19061 to indicate that names looked up in dependent types should be
19062 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19063 keyword has been used to indicate that the name that appears next
19064 is a template. TAG_TYPE indicates the explicit tag given before
19065 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19066 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19067 is the class being defined in a class-head.
19069 Returns the TYPE_DECL representing the class. */
19071 static tree
19072 cp_parser_class_name (cp_parser *parser,
19073 bool typename_keyword_p,
19074 bool template_keyword_p,
19075 enum tag_types tag_type,
19076 bool check_dependency_p,
19077 bool class_head_p,
19078 bool is_declaration)
19080 tree decl;
19081 tree scope;
19082 bool typename_p;
19083 cp_token *token;
19084 tree identifier = NULL_TREE;
19086 /* All class-names start with an identifier. */
19087 token = cp_lexer_peek_token (parser->lexer);
19088 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19090 cp_parser_error (parser, "expected class-name");
19091 return error_mark_node;
19094 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19095 to a template-id, so we save it here. */
19096 scope = parser->scope;
19097 if (scope == error_mark_node)
19098 return error_mark_node;
19100 /* Any name names a type if we're following the `typename' keyword
19101 in a qualified name where the enclosing scope is type-dependent. */
19102 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19103 && dependent_type_p (scope));
19104 /* Handle the common case (an identifier, but not a template-id)
19105 efficiently. */
19106 if (token->type == CPP_NAME
19107 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19109 cp_token *identifier_token;
19110 bool ambiguous_p;
19112 /* Look for the identifier. */
19113 identifier_token = cp_lexer_peek_token (parser->lexer);
19114 ambiguous_p = identifier_token->ambiguous_p;
19115 identifier = cp_parser_identifier (parser);
19116 /* If the next token isn't an identifier, we are certainly not
19117 looking at a class-name. */
19118 if (identifier == error_mark_node)
19119 decl = error_mark_node;
19120 /* If we know this is a type-name, there's no need to look it
19121 up. */
19122 else if (typename_p)
19123 decl = identifier;
19124 else
19126 tree ambiguous_decls;
19127 /* If we already know that this lookup is ambiguous, then
19128 we've already issued an error message; there's no reason
19129 to check again. */
19130 if (ambiguous_p)
19132 cp_parser_simulate_error (parser);
19133 return error_mark_node;
19135 /* If the next token is a `::', then the name must be a type
19136 name.
19138 [basic.lookup.qual]
19140 During the lookup for a name preceding the :: scope
19141 resolution operator, object, function, and enumerator
19142 names are ignored. */
19143 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19144 tag_type = typename_type;
19145 /* Look up the name. */
19146 decl = cp_parser_lookup_name (parser, identifier,
19147 tag_type,
19148 /*is_template=*/false,
19149 /*is_namespace=*/false,
19150 check_dependency_p,
19151 &ambiguous_decls,
19152 identifier_token->location);
19153 if (ambiguous_decls)
19155 if (cp_parser_parsing_tentatively (parser))
19156 cp_parser_simulate_error (parser);
19157 return error_mark_node;
19161 else
19163 /* Try a template-id. */
19164 decl = cp_parser_template_id (parser, template_keyword_p,
19165 check_dependency_p,
19166 tag_type,
19167 is_declaration);
19168 if (decl == error_mark_node)
19169 return error_mark_node;
19172 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19174 /* If this is a typename, create a TYPENAME_TYPE. */
19175 if (typename_p && decl != error_mark_node)
19177 decl = make_typename_type (scope, decl, typename_type,
19178 /*complain=*/tf_error);
19179 if (decl != error_mark_node)
19180 decl = TYPE_NAME (decl);
19183 decl = strip_using_decl (decl);
19185 /* Check to see that it is really the name of a class. */
19186 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19187 && identifier_p (TREE_OPERAND (decl, 0))
19188 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19189 /* Situations like this:
19191 template <typename T> struct A {
19192 typename T::template X<int>::I i;
19195 are problematic. Is `T::template X<int>' a class-name? The
19196 standard does not seem to be definitive, but there is no other
19197 valid interpretation of the following `::'. Therefore, those
19198 names are considered class-names. */
19200 decl = make_typename_type (scope, decl, tag_type, tf_error);
19201 if (decl != error_mark_node)
19202 decl = TYPE_NAME (decl);
19204 else if (TREE_CODE (decl) != TYPE_DECL
19205 || TREE_TYPE (decl) == error_mark_node
19206 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19207 /* In Objective-C 2.0, a classname followed by '.' starts a
19208 dot-syntax expression, and it's not a type-name. */
19209 || (c_dialect_objc ()
19210 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19211 && objc_is_class_name (decl)))
19212 decl = error_mark_node;
19214 if (decl == error_mark_node)
19215 cp_parser_error (parser, "expected class-name");
19216 else if (identifier && !parser->scope)
19217 maybe_note_name_used_in_class (identifier, decl);
19219 return decl;
19222 /* Parse a class-specifier.
19224 class-specifier:
19225 class-head { member-specification [opt] }
19227 Returns the TREE_TYPE representing the class. */
19229 static tree
19230 cp_parser_class_specifier_1 (cp_parser* parser)
19232 tree type;
19233 tree attributes = NULL_TREE;
19234 bool nested_name_specifier_p;
19235 unsigned saved_num_template_parameter_lists;
19236 bool saved_in_function_body;
19237 unsigned char in_statement;
19238 bool in_switch_statement_p;
19239 bool saved_in_unbraced_linkage_specification_p;
19240 tree old_scope = NULL_TREE;
19241 tree scope = NULL_TREE;
19242 cp_token *closing_brace;
19244 push_deferring_access_checks (dk_no_deferred);
19246 /* Parse the class-head. */
19247 type = cp_parser_class_head (parser,
19248 &nested_name_specifier_p);
19249 /* If the class-head was a semantic disaster, skip the entire body
19250 of the class. */
19251 if (!type)
19253 cp_parser_skip_to_end_of_block_or_statement (parser);
19254 pop_deferring_access_checks ();
19255 return error_mark_node;
19258 /* Look for the `{'. */
19259 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19261 pop_deferring_access_checks ();
19262 return error_mark_node;
19265 cp_ensure_no_omp_declare_simd (parser);
19267 /* Issue an error message if type-definitions are forbidden here. */
19268 cp_parser_check_type_definition (parser);
19269 /* Remember that we are defining one more class. */
19270 ++parser->num_classes_being_defined;
19271 /* Inside the class, surrounding template-parameter-lists do not
19272 apply. */
19273 saved_num_template_parameter_lists
19274 = parser->num_template_parameter_lists;
19275 parser->num_template_parameter_lists = 0;
19276 /* We are not in a function body. */
19277 saved_in_function_body = parser->in_function_body;
19278 parser->in_function_body = false;
19279 /* Or in a loop. */
19280 in_statement = parser->in_statement;
19281 parser->in_statement = 0;
19282 /* Or in a switch. */
19283 in_switch_statement_p = parser->in_switch_statement_p;
19284 parser->in_switch_statement_p = false;
19285 /* We are not immediately inside an extern "lang" block. */
19286 saved_in_unbraced_linkage_specification_p
19287 = parser->in_unbraced_linkage_specification_p;
19288 parser->in_unbraced_linkage_specification_p = false;
19290 /* Start the class. */
19291 if (nested_name_specifier_p)
19293 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19294 old_scope = push_inner_scope (scope);
19296 type = begin_class_definition (type);
19298 if (type == error_mark_node)
19299 /* If the type is erroneous, skip the entire body of the class. */
19300 cp_parser_skip_to_closing_brace (parser);
19301 else
19302 /* Parse the member-specification. */
19303 cp_parser_member_specification_opt (parser);
19305 /* Look for the trailing `}'. */
19306 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19307 /* Look for trailing attributes to apply to this class. */
19308 if (cp_parser_allow_gnu_extensions_p (parser))
19309 attributes = cp_parser_gnu_attributes_opt (parser);
19310 if (type != error_mark_node)
19311 type = finish_struct (type, attributes);
19312 if (nested_name_specifier_p)
19313 pop_inner_scope (old_scope, scope);
19315 /* We've finished a type definition. Check for the common syntax
19316 error of forgetting a semicolon after the definition. We need to
19317 be careful, as we can't just check for not-a-semicolon and be done
19318 with it; the user might have typed:
19320 class X { } c = ...;
19321 class X { } *p = ...;
19323 and so forth. Instead, enumerate all the possible tokens that
19324 might follow this production; if we don't see one of them, then
19325 complain and silently insert the semicolon. */
19327 cp_token *token = cp_lexer_peek_token (parser->lexer);
19328 bool want_semicolon = true;
19330 if (cp_next_tokens_can_be_std_attribute_p (parser))
19331 /* Don't try to parse c++11 attributes here. As per the
19332 grammar, that should be a task for
19333 cp_parser_decl_specifier_seq. */
19334 want_semicolon = false;
19336 switch (token->type)
19338 case CPP_NAME:
19339 case CPP_SEMICOLON:
19340 case CPP_MULT:
19341 case CPP_AND:
19342 case CPP_OPEN_PAREN:
19343 case CPP_CLOSE_PAREN:
19344 case CPP_COMMA:
19345 want_semicolon = false;
19346 break;
19348 /* While it's legal for type qualifiers and storage class
19349 specifiers to follow type definitions in the grammar, only
19350 compiler testsuites contain code like that. Assume that if
19351 we see such code, then what we're really seeing is a case
19352 like:
19354 class X { }
19355 const <type> var = ...;
19359 class Y { }
19360 static <type> func (...) ...
19362 i.e. the qualifier or specifier applies to the next
19363 declaration. To do so, however, we need to look ahead one
19364 more token to see if *that* token is a type specifier.
19366 This code could be improved to handle:
19368 class Z { }
19369 static const <type> var = ...; */
19370 case CPP_KEYWORD:
19371 if (keyword_is_decl_specifier (token->keyword))
19373 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19375 /* Handling user-defined types here would be nice, but very
19376 tricky. */
19377 want_semicolon
19378 = (lookahead->type == CPP_KEYWORD
19379 && keyword_begins_type_specifier (lookahead->keyword));
19381 break;
19382 default:
19383 break;
19386 /* If we don't have a type, then something is very wrong and we
19387 shouldn't try to do anything clever. Likewise for not seeing the
19388 closing brace. */
19389 if (closing_brace && TYPE_P (type) && want_semicolon)
19391 cp_token_position prev
19392 = cp_lexer_previous_token_position (parser->lexer);
19393 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19394 location_t loc = prev_token->location;
19396 if (CLASSTYPE_DECLARED_CLASS (type))
19397 error_at (loc, "expected %<;%> after class definition");
19398 else if (TREE_CODE (type) == RECORD_TYPE)
19399 error_at (loc, "expected %<;%> after struct definition");
19400 else if (TREE_CODE (type) == UNION_TYPE)
19401 error_at (loc, "expected %<;%> after union definition");
19402 else
19403 gcc_unreachable ();
19405 /* Unget one token and smash it to look as though we encountered
19406 a semicolon in the input stream. */
19407 cp_lexer_set_token_position (parser->lexer, prev);
19408 token = cp_lexer_peek_token (parser->lexer);
19409 token->type = CPP_SEMICOLON;
19410 token->keyword = RID_MAX;
19414 /* If this class is not itself within the scope of another class,
19415 then we need to parse the bodies of all of the queued function
19416 definitions. Note that the queued functions defined in a class
19417 are not always processed immediately following the
19418 class-specifier for that class. Consider:
19420 struct A {
19421 struct B { void f() { sizeof (A); } };
19424 If `f' were processed before the processing of `A' were
19425 completed, there would be no way to compute the size of `A'.
19426 Note that the nesting we are interested in here is lexical --
19427 not the semantic nesting given by TYPE_CONTEXT. In particular,
19428 for:
19430 struct A { struct B; };
19431 struct A::B { void f() { } };
19433 there is no need to delay the parsing of `A::B::f'. */
19434 if (--parser->num_classes_being_defined == 0)
19436 tree decl;
19437 tree class_type = NULL_TREE;
19438 tree pushed_scope = NULL_TREE;
19439 unsigned ix;
19440 cp_default_arg_entry *e;
19441 tree save_ccp, save_ccr;
19443 /* In a first pass, parse default arguments to the functions.
19444 Then, in a second pass, parse the bodies of the functions.
19445 This two-phased approach handles cases like:
19447 struct S {
19448 void f() { g(); }
19449 void g(int i = 3);
19453 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19455 decl = e->decl;
19456 /* If there are default arguments that have not yet been processed,
19457 take care of them now. */
19458 if (class_type != e->class_type)
19460 if (pushed_scope)
19461 pop_scope (pushed_scope);
19462 class_type = e->class_type;
19463 pushed_scope = push_scope (class_type);
19465 /* Make sure that any template parameters are in scope. */
19466 maybe_begin_member_template_processing (decl);
19467 /* Parse the default argument expressions. */
19468 cp_parser_late_parsing_default_args (parser, decl);
19469 /* Remove any template parameters from the symbol table. */
19470 maybe_end_member_template_processing ();
19472 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19473 /* Now parse any NSDMIs. */
19474 save_ccp = current_class_ptr;
19475 save_ccr = current_class_ref;
19476 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19478 if (class_type != DECL_CONTEXT (decl))
19480 if (pushed_scope)
19481 pop_scope (pushed_scope);
19482 class_type = DECL_CONTEXT (decl);
19483 pushed_scope = push_scope (class_type);
19485 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19486 cp_parser_late_parsing_nsdmi (parser, decl);
19488 vec_safe_truncate (unparsed_nsdmis, 0);
19489 current_class_ptr = save_ccp;
19490 current_class_ref = save_ccr;
19491 if (pushed_scope)
19492 pop_scope (pushed_scope);
19493 /* Now parse the body of the functions. */
19494 if (flag_openmp)
19496 /* OpenMP UDRs need to be parsed before all other functions. */
19497 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19498 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19499 cp_parser_late_parsing_for_member (parser, decl);
19500 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19501 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19502 cp_parser_late_parsing_for_member (parser, decl);
19504 else
19505 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19506 cp_parser_late_parsing_for_member (parser, decl);
19507 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19510 /* Put back any saved access checks. */
19511 pop_deferring_access_checks ();
19513 /* Restore saved state. */
19514 parser->in_switch_statement_p = in_switch_statement_p;
19515 parser->in_statement = in_statement;
19516 parser->in_function_body = saved_in_function_body;
19517 parser->num_template_parameter_lists
19518 = saved_num_template_parameter_lists;
19519 parser->in_unbraced_linkage_specification_p
19520 = saved_in_unbraced_linkage_specification_p;
19522 return type;
19525 static tree
19526 cp_parser_class_specifier (cp_parser* parser)
19528 tree ret;
19529 timevar_push (TV_PARSE_STRUCT);
19530 ret = cp_parser_class_specifier_1 (parser);
19531 timevar_pop (TV_PARSE_STRUCT);
19532 return ret;
19535 /* Parse a class-head.
19537 class-head:
19538 class-key identifier [opt] base-clause [opt]
19539 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19540 class-key nested-name-specifier [opt] template-id
19541 base-clause [opt]
19543 class-virt-specifier:
19544 final
19546 GNU Extensions:
19547 class-key attributes identifier [opt] base-clause [opt]
19548 class-key attributes nested-name-specifier identifier base-clause [opt]
19549 class-key attributes nested-name-specifier [opt] template-id
19550 base-clause [opt]
19552 Upon return BASES is initialized to the list of base classes (or
19553 NULL, if there are none) in the same form returned by
19554 cp_parser_base_clause.
19556 Returns the TYPE of the indicated class. Sets
19557 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19558 involving a nested-name-specifier was used, and FALSE otherwise.
19560 Returns error_mark_node if this is not a class-head.
19562 Returns NULL_TREE if the class-head is syntactically valid, but
19563 semantically invalid in a way that means we should skip the entire
19564 body of the class. */
19566 static tree
19567 cp_parser_class_head (cp_parser* parser,
19568 bool* nested_name_specifier_p)
19570 tree nested_name_specifier;
19571 enum tag_types class_key;
19572 tree id = NULL_TREE;
19573 tree type = NULL_TREE;
19574 tree attributes;
19575 tree bases;
19576 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19577 bool template_id_p = false;
19578 bool qualified_p = false;
19579 bool invalid_nested_name_p = false;
19580 bool invalid_explicit_specialization_p = false;
19581 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19582 tree pushed_scope = NULL_TREE;
19583 unsigned num_templates;
19584 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19585 /* Assume no nested-name-specifier will be present. */
19586 *nested_name_specifier_p = false;
19587 /* Assume no template parameter lists will be used in defining the
19588 type. */
19589 num_templates = 0;
19590 parser->colon_corrects_to_scope_p = false;
19592 /* Look for the class-key. */
19593 class_key = cp_parser_class_key (parser);
19594 if (class_key == none_type)
19595 return error_mark_node;
19597 /* Parse the attributes. */
19598 attributes = cp_parser_attributes_opt (parser);
19600 /* If the next token is `::', that is invalid -- but sometimes
19601 people do try to write:
19603 struct ::S {};
19605 Handle this gracefully by accepting the extra qualifier, and then
19606 issuing an error about it later if this really is a
19607 class-head. If it turns out just to be an elaborated type
19608 specifier, remain silent. */
19609 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19610 qualified_p = true;
19612 push_deferring_access_checks (dk_no_check);
19614 /* Determine the name of the class. Begin by looking for an
19615 optional nested-name-specifier. */
19616 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19617 nested_name_specifier
19618 = cp_parser_nested_name_specifier_opt (parser,
19619 /*typename_keyword_p=*/false,
19620 /*check_dependency_p=*/false,
19621 /*type_p=*/true,
19622 /*is_declaration=*/false);
19623 /* If there was a nested-name-specifier, then there *must* be an
19624 identifier. */
19625 if (nested_name_specifier)
19627 type_start_token = cp_lexer_peek_token (parser->lexer);
19628 /* Although the grammar says `identifier', it really means
19629 `class-name' or `template-name'. You are only allowed to
19630 define a class that has already been declared with this
19631 syntax.
19633 The proposed resolution for Core Issue 180 says that wherever
19634 you see `class T::X' you should treat `X' as a type-name.
19636 It is OK to define an inaccessible class; for example:
19638 class A { class B; };
19639 class A::B {};
19641 We do not know if we will see a class-name, or a
19642 template-name. We look for a class-name first, in case the
19643 class-name is a template-id; if we looked for the
19644 template-name first we would stop after the template-name. */
19645 cp_parser_parse_tentatively (parser);
19646 type = cp_parser_class_name (parser,
19647 /*typename_keyword_p=*/false,
19648 /*template_keyword_p=*/false,
19649 class_type,
19650 /*check_dependency_p=*/false,
19651 /*class_head_p=*/true,
19652 /*is_declaration=*/false);
19653 /* If that didn't work, ignore the nested-name-specifier. */
19654 if (!cp_parser_parse_definitely (parser))
19656 invalid_nested_name_p = true;
19657 type_start_token = cp_lexer_peek_token (parser->lexer);
19658 id = cp_parser_identifier (parser);
19659 if (id == error_mark_node)
19660 id = NULL_TREE;
19662 /* If we could not find a corresponding TYPE, treat this
19663 declaration like an unqualified declaration. */
19664 if (type == error_mark_node)
19665 nested_name_specifier = NULL_TREE;
19666 /* Otherwise, count the number of templates used in TYPE and its
19667 containing scopes. */
19668 else
19670 tree scope;
19672 for (scope = TREE_TYPE (type);
19673 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19674 scope = get_containing_scope (scope))
19675 if (TYPE_P (scope)
19676 && CLASS_TYPE_P (scope)
19677 && CLASSTYPE_TEMPLATE_INFO (scope)
19678 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19679 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19680 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19681 ++num_templates;
19684 /* Otherwise, the identifier is optional. */
19685 else
19687 /* We don't know whether what comes next is a template-id,
19688 an identifier, or nothing at all. */
19689 cp_parser_parse_tentatively (parser);
19690 /* Check for a template-id. */
19691 type_start_token = cp_lexer_peek_token (parser->lexer);
19692 id = cp_parser_template_id (parser,
19693 /*template_keyword_p=*/false,
19694 /*check_dependency_p=*/true,
19695 class_key,
19696 /*is_declaration=*/true);
19697 /* If that didn't work, it could still be an identifier. */
19698 if (!cp_parser_parse_definitely (parser))
19700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19702 type_start_token = cp_lexer_peek_token (parser->lexer);
19703 id = cp_parser_identifier (parser);
19705 else
19706 id = NULL_TREE;
19708 else
19710 template_id_p = true;
19711 ++num_templates;
19715 pop_deferring_access_checks ();
19717 if (id)
19719 cp_parser_check_for_invalid_template_id (parser, id,
19720 class_key,
19721 type_start_token->location);
19723 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19725 /* If it's not a `:' or a `{' then we can't really be looking at a
19726 class-head, since a class-head only appears as part of a
19727 class-specifier. We have to detect this situation before calling
19728 xref_tag, since that has irreversible side-effects. */
19729 if (!cp_parser_next_token_starts_class_definition_p (parser))
19731 cp_parser_error (parser, "expected %<{%> or %<:%>");
19732 type = error_mark_node;
19733 goto out;
19736 /* At this point, we're going ahead with the class-specifier, even
19737 if some other problem occurs. */
19738 cp_parser_commit_to_tentative_parse (parser);
19739 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19741 cp_parser_error (parser,
19742 "cannot specify %<override%> for a class");
19743 type = error_mark_node;
19744 goto out;
19746 /* Issue the error about the overly-qualified name now. */
19747 if (qualified_p)
19749 cp_parser_error (parser,
19750 "global qualification of class name is invalid");
19751 type = error_mark_node;
19752 goto out;
19754 else if (invalid_nested_name_p)
19756 cp_parser_error (parser,
19757 "qualified name does not name a class");
19758 type = error_mark_node;
19759 goto out;
19761 else if (nested_name_specifier)
19763 tree scope;
19765 /* Reject typedef-names in class heads. */
19766 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19768 error_at (type_start_token->location,
19769 "invalid class name in declaration of %qD",
19770 type);
19771 type = NULL_TREE;
19772 goto done;
19775 /* Figure out in what scope the declaration is being placed. */
19776 scope = current_scope ();
19777 /* If that scope does not contain the scope in which the
19778 class was originally declared, the program is invalid. */
19779 if (scope && !is_ancestor (scope, nested_name_specifier))
19781 if (at_namespace_scope_p ())
19782 error_at (type_start_token->location,
19783 "declaration of %qD in namespace %qD which does not "
19784 "enclose %qD",
19785 type, scope, nested_name_specifier);
19786 else
19787 error_at (type_start_token->location,
19788 "declaration of %qD in %qD which does not enclose %qD",
19789 type, scope, nested_name_specifier);
19790 type = NULL_TREE;
19791 goto done;
19793 /* [dcl.meaning]
19795 A declarator-id shall not be qualified except for the
19796 definition of a ... nested class outside of its class
19797 ... [or] the definition or explicit instantiation of a
19798 class member of a namespace outside of its namespace. */
19799 if (scope == nested_name_specifier)
19801 permerror (nested_name_specifier_token_start->location,
19802 "extra qualification not allowed");
19803 nested_name_specifier = NULL_TREE;
19804 num_templates = 0;
19807 /* An explicit-specialization must be preceded by "template <>". If
19808 it is not, try to recover gracefully. */
19809 if (at_namespace_scope_p ()
19810 && parser->num_template_parameter_lists == 0
19811 && template_id_p)
19813 error_at (type_start_token->location,
19814 "an explicit specialization must be preceded by %<template <>%>");
19815 invalid_explicit_specialization_p = true;
19816 /* Take the same action that would have been taken by
19817 cp_parser_explicit_specialization. */
19818 ++parser->num_template_parameter_lists;
19819 begin_specialization ();
19821 /* There must be no "return" statements between this point and the
19822 end of this function; set "type "to the correct return value and
19823 use "goto done;" to return. */
19824 /* Make sure that the right number of template parameters were
19825 present. */
19826 if (!cp_parser_check_template_parameters (parser, num_templates,
19827 type_start_token->location,
19828 /*declarator=*/NULL))
19830 /* If something went wrong, there is no point in even trying to
19831 process the class-definition. */
19832 type = NULL_TREE;
19833 goto done;
19836 /* Look up the type. */
19837 if (template_id_p)
19839 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19840 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19841 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19843 error_at (type_start_token->location,
19844 "function template %qD redeclared as a class template", id);
19845 type = error_mark_node;
19847 else
19849 type = TREE_TYPE (id);
19850 type = maybe_process_partial_specialization (type);
19852 if (nested_name_specifier)
19853 pushed_scope = push_scope (nested_name_specifier);
19855 else if (nested_name_specifier)
19857 tree class_type;
19859 /* Given:
19861 template <typename T> struct S { struct T };
19862 template <typename T> struct S<T>::T { };
19864 we will get a TYPENAME_TYPE when processing the definition of
19865 `S::T'. We need to resolve it to the actual type before we
19866 try to define it. */
19867 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19869 class_type = resolve_typename_type (TREE_TYPE (type),
19870 /*only_current_p=*/false);
19871 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19872 type = TYPE_NAME (class_type);
19873 else
19875 cp_parser_error (parser, "could not resolve typename type");
19876 type = error_mark_node;
19880 if (maybe_process_partial_specialization (TREE_TYPE (type))
19881 == error_mark_node)
19883 type = NULL_TREE;
19884 goto done;
19887 class_type = current_class_type;
19888 /* Enter the scope indicated by the nested-name-specifier. */
19889 pushed_scope = push_scope (nested_name_specifier);
19890 /* Get the canonical version of this type. */
19891 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19892 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19893 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19895 type = push_template_decl (type);
19896 if (type == error_mark_node)
19898 type = NULL_TREE;
19899 goto done;
19903 type = TREE_TYPE (type);
19904 *nested_name_specifier_p = true;
19906 else /* The name is not a nested name. */
19908 /* If the class was unnamed, create a dummy name. */
19909 if (!id)
19910 id = make_anon_name ();
19911 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19912 parser->num_template_parameter_lists);
19915 /* Indicate whether this class was declared as a `class' or as a
19916 `struct'. */
19917 if (TREE_CODE (type) == RECORD_TYPE)
19918 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19919 cp_parser_check_class_key (class_key, type);
19921 /* If this type was already complete, and we see another definition,
19922 that's an error. */
19923 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19925 error_at (type_start_token->location, "redefinition of %q#T",
19926 type);
19927 error_at (type_start_token->location, "previous definition of %q+#T",
19928 type);
19929 type = NULL_TREE;
19930 goto done;
19932 else if (type == error_mark_node)
19933 type = NULL_TREE;
19935 if (type)
19937 /* Apply attributes now, before any use of the class as a template
19938 argument in its base list. */
19939 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19940 fixup_attribute_variants (type);
19943 /* We will have entered the scope containing the class; the names of
19944 base classes should be looked up in that context. For example:
19946 struct A { struct B {}; struct C; };
19947 struct A::C : B {};
19949 is valid. */
19951 /* Get the list of base-classes, if there is one. */
19952 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19954 /* PR59482: enter the class scope so that base-specifiers are looked
19955 up correctly. */
19956 if (type)
19957 pushclass (type);
19958 bases = cp_parser_base_clause (parser);
19959 /* PR59482: get out of the previously pushed class scope so that the
19960 subsequent pops pop the right thing. */
19961 if (type)
19962 popclass ();
19964 else
19965 bases = NULL_TREE;
19967 /* If we're really defining a class, process the base classes.
19968 If they're invalid, fail. */
19969 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19970 && !xref_basetypes (type, bases))
19971 type = NULL_TREE;
19973 done:
19974 /* Leave the scope given by the nested-name-specifier. We will
19975 enter the class scope itself while processing the members. */
19976 if (pushed_scope)
19977 pop_scope (pushed_scope);
19979 if (invalid_explicit_specialization_p)
19981 end_specialization ();
19982 --parser->num_template_parameter_lists;
19985 if (type)
19986 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19987 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19988 CLASSTYPE_FINAL (type) = 1;
19989 out:
19990 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19991 return type;
19994 /* Parse a class-key.
19996 class-key:
19997 class
19998 struct
19999 union
20001 Returns the kind of class-key specified, or none_type to indicate
20002 error. */
20004 static enum tag_types
20005 cp_parser_class_key (cp_parser* parser)
20007 cp_token *token;
20008 enum tag_types tag_type;
20010 /* Look for the class-key. */
20011 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20012 if (!token)
20013 return none_type;
20015 /* Check to see if the TOKEN is a class-key. */
20016 tag_type = cp_parser_token_is_class_key (token);
20017 if (!tag_type)
20018 cp_parser_error (parser, "expected class-key");
20019 return tag_type;
20022 /* Parse an (optional) member-specification.
20024 member-specification:
20025 member-declaration member-specification [opt]
20026 access-specifier : member-specification [opt] */
20028 static void
20029 cp_parser_member_specification_opt (cp_parser* parser)
20031 while (true)
20033 cp_token *token;
20034 enum rid keyword;
20036 /* Peek at the next token. */
20037 token = cp_lexer_peek_token (parser->lexer);
20038 /* If it's a `}', or EOF then we've seen all the members. */
20039 if (token->type == CPP_CLOSE_BRACE
20040 || token->type == CPP_EOF
20041 || token->type == CPP_PRAGMA_EOL)
20042 break;
20044 /* See if this token is a keyword. */
20045 keyword = token->keyword;
20046 switch (keyword)
20048 case RID_PUBLIC:
20049 case RID_PROTECTED:
20050 case RID_PRIVATE:
20051 /* Consume the access-specifier. */
20052 cp_lexer_consume_token (parser->lexer);
20053 /* Remember which access-specifier is active. */
20054 current_access_specifier = token->u.value;
20055 /* Look for the `:'. */
20056 cp_parser_require (parser, CPP_COLON, RT_COLON);
20057 break;
20059 default:
20060 /* Accept #pragmas at class scope. */
20061 if (token->type == CPP_PRAGMA)
20063 cp_parser_pragma (parser, pragma_member);
20064 break;
20067 /* Otherwise, the next construction must be a
20068 member-declaration. */
20069 cp_parser_member_declaration (parser);
20074 /* Parse a member-declaration.
20076 member-declaration:
20077 decl-specifier-seq [opt] member-declarator-list [opt] ;
20078 function-definition ; [opt]
20079 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20080 using-declaration
20081 template-declaration
20082 alias-declaration
20084 member-declarator-list:
20085 member-declarator
20086 member-declarator-list , member-declarator
20088 member-declarator:
20089 declarator pure-specifier [opt]
20090 declarator constant-initializer [opt]
20091 identifier [opt] : constant-expression
20093 GNU Extensions:
20095 member-declaration:
20096 __extension__ member-declaration
20098 member-declarator:
20099 declarator attributes [opt] pure-specifier [opt]
20100 declarator attributes [opt] constant-initializer [opt]
20101 identifier [opt] attributes [opt] : constant-expression
20103 C++0x Extensions:
20105 member-declaration:
20106 static_assert-declaration */
20108 static void
20109 cp_parser_member_declaration (cp_parser* parser)
20111 cp_decl_specifier_seq decl_specifiers;
20112 tree prefix_attributes;
20113 tree decl;
20114 int declares_class_or_enum;
20115 bool friend_p;
20116 cp_token *token = NULL;
20117 cp_token *decl_spec_token_start = NULL;
20118 cp_token *initializer_token_start = NULL;
20119 int saved_pedantic;
20120 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20122 /* Check for the `__extension__' keyword. */
20123 if (cp_parser_extension_opt (parser, &saved_pedantic))
20125 /* Recurse. */
20126 cp_parser_member_declaration (parser);
20127 /* Restore the old value of the PEDANTIC flag. */
20128 pedantic = saved_pedantic;
20130 return;
20133 /* Check for a template-declaration. */
20134 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20136 /* An explicit specialization here is an error condition, and we
20137 expect the specialization handler to detect and report this. */
20138 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20139 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20140 cp_parser_explicit_specialization (parser);
20141 else
20142 cp_parser_template_declaration (parser, /*member_p=*/true);
20144 return;
20147 /* Check for a using-declaration. */
20148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20150 if (cxx_dialect < cxx11)
20152 /* Parse the using-declaration. */
20153 cp_parser_using_declaration (parser,
20154 /*access_declaration_p=*/false);
20155 return;
20157 else
20159 tree decl;
20160 bool alias_decl_expected;
20161 cp_parser_parse_tentatively (parser);
20162 decl = cp_parser_alias_declaration (parser);
20163 /* Note that if we actually see the '=' token after the
20164 identifier, cp_parser_alias_declaration commits the
20165 tentative parse. In that case, we really expects an
20166 alias-declaration. Otherwise, we expect a using
20167 declaration. */
20168 alias_decl_expected =
20169 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20170 cp_parser_parse_definitely (parser);
20172 if (alias_decl_expected)
20173 finish_member_declaration (decl);
20174 else
20175 cp_parser_using_declaration (parser,
20176 /*access_declaration_p=*/false);
20177 return;
20181 /* Check for @defs. */
20182 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20184 tree ivar, member;
20185 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20186 ivar = ivar_chains;
20187 while (ivar)
20189 member = ivar;
20190 ivar = TREE_CHAIN (member);
20191 TREE_CHAIN (member) = NULL_TREE;
20192 finish_member_declaration (member);
20194 return;
20197 /* If the next token is `static_assert' we have a static assertion. */
20198 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20200 cp_parser_static_assert (parser, /*member_p=*/true);
20201 return;
20204 parser->colon_corrects_to_scope_p = false;
20206 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20207 goto out;
20209 /* Parse the decl-specifier-seq. */
20210 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20211 cp_parser_decl_specifier_seq (parser,
20212 CP_PARSER_FLAGS_OPTIONAL,
20213 &decl_specifiers,
20214 &declares_class_or_enum);
20215 /* Check for an invalid type-name. */
20216 if (!decl_specifiers.any_type_specifiers_p
20217 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20218 goto out;
20219 /* If there is no declarator, then the decl-specifier-seq should
20220 specify a type. */
20221 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20223 /* If there was no decl-specifier-seq, and the next token is a
20224 `;', then we have something like:
20226 struct S { ; };
20228 [class.mem]
20230 Each member-declaration shall declare at least one member
20231 name of the class. */
20232 if (!decl_specifiers.any_specifiers_p)
20234 cp_token *token = cp_lexer_peek_token (parser->lexer);
20235 if (!in_system_header_at (token->location))
20236 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20238 else
20240 tree type;
20242 /* See if this declaration is a friend. */
20243 friend_p = cp_parser_friend_p (&decl_specifiers);
20244 /* If there were decl-specifiers, check to see if there was
20245 a class-declaration. */
20246 type = check_tag_decl (&decl_specifiers,
20247 /*explicit_type_instantiation_p=*/false);
20248 /* Nested classes have already been added to the class, but
20249 a `friend' needs to be explicitly registered. */
20250 if (friend_p)
20252 /* If the `friend' keyword was present, the friend must
20253 be introduced with a class-key. */
20254 if (!declares_class_or_enum && cxx_dialect < cxx11)
20255 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20256 "in C++03 a class-key must be used "
20257 "when declaring a friend");
20258 /* In this case:
20260 template <typename T> struct A {
20261 friend struct A<T>::B;
20264 A<T>::B will be represented by a TYPENAME_TYPE, and
20265 therefore not recognized by check_tag_decl. */
20266 if (!type)
20268 type = decl_specifiers.type;
20269 if (type && TREE_CODE (type) == TYPE_DECL)
20270 type = TREE_TYPE (type);
20272 if (!type || !TYPE_P (type))
20273 error_at (decl_spec_token_start->location,
20274 "friend declaration does not name a class or "
20275 "function");
20276 else
20277 make_friend_class (current_class_type, type,
20278 /*complain=*/true);
20280 /* If there is no TYPE, an error message will already have
20281 been issued. */
20282 else if (!type || type == error_mark_node)
20284 /* An anonymous aggregate has to be handled specially; such
20285 a declaration really declares a data member (with a
20286 particular type), as opposed to a nested class. */
20287 else if (ANON_AGGR_TYPE_P (type))
20289 /* C++11 9.5/6. */
20290 if (decl_specifiers.storage_class != sc_none)
20291 error_at (decl_spec_token_start->location,
20292 "a storage class on an anonymous aggregate "
20293 "in class scope is not allowed");
20295 /* Remove constructors and such from TYPE, now that we
20296 know it is an anonymous aggregate. */
20297 fixup_anonymous_aggr (type);
20298 /* And make the corresponding data member. */
20299 decl = build_decl (decl_spec_token_start->location,
20300 FIELD_DECL, NULL_TREE, type);
20301 /* Add it to the class. */
20302 finish_member_declaration (decl);
20304 else
20305 cp_parser_check_access_in_redeclaration
20306 (TYPE_NAME (type),
20307 decl_spec_token_start->location);
20310 else
20312 bool assume_semicolon = false;
20314 /* Clear attributes from the decl_specifiers but keep them
20315 around as prefix attributes that apply them to the entity
20316 being declared. */
20317 prefix_attributes = decl_specifiers.attributes;
20318 decl_specifiers.attributes = NULL_TREE;
20320 /* See if these declarations will be friends. */
20321 friend_p = cp_parser_friend_p (&decl_specifiers);
20323 /* Keep going until we hit the `;' at the end of the
20324 declaration. */
20325 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20327 tree attributes = NULL_TREE;
20328 tree first_attribute;
20330 /* Peek at the next token. */
20331 token = cp_lexer_peek_token (parser->lexer);
20333 /* Check for a bitfield declaration. */
20334 if (token->type == CPP_COLON
20335 || (token->type == CPP_NAME
20336 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20337 == CPP_COLON))
20339 tree identifier;
20340 tree width;
20342 /* Get the name of the bitfield. Note that we cannot just
20343 check TOKEN here because it may have been invalidated by
20344 the call to cp_lexer_peek_nth_token above. */
20345 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20346 identifier = cp_parser_identifier (parser);
20347 else
20348 identifier = NULL_TREE;
20350 /* Consume the `:' token. */
20351 cp_lexer_consume_token (parser->lexer);
20352 /* Get the width of the bitfield. */
20353 width
20354 = cp_parser_constant_expression (parser,
20355 /*allow_non_constant=*/false,
20356 NULL);
20358 /* Look for attributes that apply to the bitfield. */
20359 attributes = cp_parser_attributes_opt (parser);
20360 /* Remember which attributes are prefix attributes and
20361 which are not. */
20362 first_attribute = attributes;
20363 /* Combine the attributes. */
20364 attributes = chainon (prefix_attributes, attributes);
20366 /* Create the bitfield declaration. */
20367 decl = grokbitfield (identifier
20368 ? make_id_declarator (NULL_TREE,
20369 identifier,
20370 sfk_none)
20371 : NULL,
20372 &decl_specifiers,
20373 width,
20374 attributes);
20376 else
20378 cp_declarator *declarator;
20379 tree initializer;
20380 tree asm_specification;
20381 int ctor_dtor_or_conv_p;
20383 /* Parse the declarator. */
20384 declarator
20385 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20386 &ctor_dtor_or_conv_p,
20387 /*parenthesized_p=*/NULL,
20388 /*member_p=*/true);
20390 /* If something went wrong parsing the declarator, make sure
20391 that we at least consume some tokens. */
20392 if (declarator == cp_error_declarator)
20394 /* Skip to the end of the statement. */
20395 cp_parser_skip_to_end_of_statement (parser);
20396 /* If the next token is not a semicolon, that is
20397 probably because we just skipped over the body of
20398 a function. So, we consume a semicolon if
20399 present, but do not issue an error message if it
20400 is not present. */
20401 if (cp_lexer_next_token_is (parser->lexer,
20402 CPP_SEMICOLON))
20403 cp_lexer_consume_token (parser->lexer);
20404 goto out;
20407 if (declares_class_or_enum & 2)
20408 cp_parser_check_for_definition_in_return_type
20409 (declarator, decl_specifiers.type,
20410 decl_specifiers.locations[ds_type_spec]);
20412 /* Look for an asm-specification. */
20413 asm_specification = cp_parser_asm_specification_opt (parser);
20414 /* Look for attributes that apply to the declaration. */
20415 attributes = cp_parser_attributes_opt (parser);
20416 /* Remember which attributes are prefix attributes and
20417 which are not. */
20418 first_attribute = attributes;
20419 /* Combine the attributes. */
20420 attributes = chainon (prefix_attributes, attributes);
20422 /* If it's an `=', then we have a constant-initializer or a
20423 pure-specifier. It is not correct to parse the
20424 initializer before registering the member declaration
20425 since the member declaration should be in scope while
20426 its initializer is processed. However, the rest of the
20427 front end does not yet provide an interface that allows
20428 us to handle this correctly. */
20429 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20431 /* In [class.mem]:
20433 A pure-specifier shall be used only in the declaration of
20434 a virtual function.
20436 A member-declarator can contain a constant-initializer
20437 only if it declares a static member of integral or
20438 enumeration type.
20440 Therefore, if the DECLARATOR is for a function, we look
20441 for a pure-specifier; otherwise, we look for a
20442 constant-initializer. When we call `grokfield', it will
20443 perform more stringent semantics checks. */
20444 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20445 if (function_declarator_p (declarator)
20446 || (decl_specifiers.type
20447 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20448 && declarator->kind == cdk_id
20449 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20450 == FUNCTION_TYPE)))
20451 initializer = cp_parser_pure_specifier (parser);
20452 else if (decl_specifiers.storage_class != sc_static)
20453 initializer = cp_parser_save_nsdmi (parser);
20454 else if (cxx_dialect >= cxx11)
20456 bool nonconst;
20457 /* Don't require a constant rvalue in C++11, since we
20458 might want a reference constant. We'll enforce
20459 constancy later. */
20460 cp_lexer_consume_token (parser->lexer);
20461 /* Parse the initializer. */
20462 initializer = cp_parser_initializer_clause (parser,
20463 &nonconst);
20465 else
20466 /* Parse the initializer. */
20467 initializer = cp_parser_constant_initializer (parser);
20469 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20470 && !function_declarator_p (declarator))
20472 bool x;
20473 if (decl_specifiers.storage_class != sc_static)
20474 initializer = cp_parser_save_nsdmi (parser);
20475 else
20476 initializer = cp_parser_initializer (parser, &x, &x);
20478 /* Otherwise, there is no initializer. */
20479 else
20480 initializer = NULL_TREE;
20482 /* See if we are probably looking at a function
20483 definition. We are certainly not looking at a
20484 member-declarator. Calling `grokfield' has
20485 side-effects, so we must not do it unless we are sure
20486 that we are looking at a member-declarator. */
20487 if (cp_parser_token_starts_function_definition_p
20488 (cp_lexer_peek_token (parser->lexer)))
20490 /* The grammar does not allow a pure-specifier to be
20491 used when a member function is defined. (It is
20492 possible that this fact is an oversight in the
20493 standard, since a pure function may be defined
20494 outside of the class-specifier. */
20495 if (initializer && initializer_token_start)
20496 error_at (initializer_token_start->location,
20497 "pure-specifier on function-definition");
20498 decl = cp_parser_save_member_function_body (parser,
20499 &decl_specifiers,
20500 declarator,
20501 attributes);
20502 if (parser->fully_implicit_function_template_p)
20503 decl = finish_fully_implicit_template (parser, decl);
20504 /* If the member was not a friend, declare it here. */
20505 if (!friend_p)
20506 finish_member_declaration (decl);
20507 /* Peek at the next token. */
20508 token = cp_lexer_peek_token (parser->lexer);
20509 /* If the next token is a semicolon, consume it. */
20510 if (token->type == CPP_SEMICOLON)
20511 cp_lexer_consume_token (parser->lexer);
20512 goto out;
20514 else
20515 if (declarator->kind == cdk_function)
20516 declarator->id_loc = token->location;
20517 /* Create the declaration. */
20518 decl = grokfield (declarator, &decl_specifiers,
20519 initializer, /*init_const_expr_p=*/true,
20520 asm_specification, attributes);
20521 if (parser->fully_implicit_function_template_p)
20522 decl = finish_fully_implicit_template (parser, decl);
20525 cp_finalize_omp_declare_simd (parser, decl);
20527 /* Reset PREFIX_ATTRIBUTES. */
20528 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20529 attributes = TREE_CHAIN (attributes);
20530 if (attributes)
20531 TREE_CHAIN (attributes) = NULL_TREE;
20533 /* If there is any qualification still in effect, clear it
20534 now; we will be starting fresh with the next declarator. */
20535 parser->scope = NULL_TREE;
20536 parser->qualifying_scope = NULL_TREE;
20537 parser->object_scope = NULL_TREE;
20538 /* If it's a `,', then there are more declarators. */
20539 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20541 cp_lexer_consume_token (parser->lexer);
20542 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20544 cp_token *token = cp_lexer_previous_token (parser->lexer);
20545 error_at (token->location,
20546 "stray %<,%> at end of member declaration");
20549 /* If the next token isn't a `;', then we have a parse error. */
20550 else if (cp_lexer_next_token_is_not (parser->lexer,
20551 CPP_SEMICOLON))
20553 /* The next token might be a ways away from where the
20554 actual semicolon is missing. Find the previous token
20555 and use that for our error position. */
20556 cp_token *token = cp_lexer_previous_token (parser->lexer);
20557 error_at (token->location,
20558 "expected %<;%> at end of member declaration");
20560 /* Assume that the user meant to provide a semicolon. If
20561 we were to cp_parser_skip_to_end_of_statement, we might
20562 skip to a semicolon inside a member function definition
20563 and issue nonsensical error messages. */
20564 assume_semicolon = true;
20567 if (decl)
20569 /* Add DECL to the list of members. */
20570 if (!friend_p)
20571 finish_member_declaration (decl);
20573 if (TREE_CODE (decl) == FUNCTION_DECL)
20574 cp_parser_save_default_args (parser, decl);
20575 else if (TREE_CODE (decl) == FIELD_DECL
20576 && !DECL_C_BIT_FIELD (decl)
20577 && DECL_INITIAL (decl))
20578 /* Add DECL to the queue of NSDMI to be parsed later. */
20579 vec_safe_push (unparsed_nsdmis, decl);
20582 if (assume_semicolon)
20583 goto out;
20587 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20588 out:
20589 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20592 /* Parse a pure-specifier.
20594 pure-specifier:
20597 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20598 Otherwise, ERROR_MARK_NODE is returned. */
20600 static tree
20601 cp_parser_pure_specifier (cp_parser* parser)
20603 cp_token *token;
20605 /* Look for the `=' token. */
20606 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20607 return error_mark_node;
20608 /* Look for the `0' token. */
20609 token = cp_lexer_peek_token (parser->lexer);
20611 if (token->type == CPP_EOF
20612 || token->type == CPP_PRAGMA_EOL)
20613 return error_mark_node;
20615 cp_lexer_consume_token (parser->lexer);
20617 /* Accept = default or = delete in c++0x mode. */
20618 if (token->keyword == RID_DEFAULT
20619 || token->keyword == RID_DELETE)
20621 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20622 return token->u.value;
20625 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20626 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20628 cp_parser_error (parser,
20629 "invalid pure specifier (only %<= 0%> is allowed)");
20630 cp_parser_skip_to_end_of_statement (parser);
20631 return error_mark_node;
20633 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20635 error_at (token->location, "templates may not be %<virtual%>");
20636 return error_mark_node;
20639 return integer_zero_node;
20642 /* Parse a constant-initializer.
20644 constant-initializer:
20645 = constant-expression
20647 Returns a representation of the constant-expression. */
20649 static tree
20650 cp_parser_constant_initializer (cp_parser* parser)
20652 /* Look for the `=' token. */
20653 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20654 return error_mark_node;
20656 /* It is invalid to write:
20658 struct S { static const int i = { 7 }; };
20661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20663 cp_parser_error (parser,
20664 "a brace-enclosed initializer is not allowed here");
20665 /* Consume the opening brace. */
20666 cp_lexer_consume_token (parser->lexer);
20667 /* Skip the initializer. */
20668 cp_parser_skip_to_closing_brace (parser);
20669 /* Look for the trailing `}'. */
20670 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20672 return error_mark_node;
20675 return cp_parser_constant_expression (parser,
20676 /*allow_non_constant=*/false,
20677 NULL);
20680 /* Derived classes [gram.class.derived] */
20682 /* Parse a base-clause.
20684 base-clause:
20685 : base-specifier-list
20687 base-specifier-list:
20688 base-specifier ... [opt]
20689 base-specifier-list , base-specifier ... [opt]
20691 Returns a TREE_LIST representing the base-classes, in the order in
20692 which they were declared. The representation of each node is as
20693 described by cp_parser_base_specifier.
20695 In the case that no bases are specified, this function will return
20696 NULL_TREE, not ERROR_MARK_NODE. */
20698 static tree
20699 cp_parser_base_clause (cp_parser* parser)
20701 tree bases = NULL_TREE;
20703 /* Look for the `:' that begins the list. */
20704 cp_parser_require (parser, CPP_COLON, RT_COLON);
20706 /* Scan the base-specifier-list. */
20707 while (true)
20709 cp_token *token;
20710 tree base;
20711 bool pack_expansion_p = false;
20713 /* Look for the base-specifier. */
20714 base = cp_parser_base_specifier (parser);
20715 /* Look for the (optional) ellipsis. */
20716 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20718 /* Consume the `...'. */
20719 cp_lexer_consume_token (parser->lexer);
20721 pack_expansion_p = true;
20724 /* Add BASE to the front of the list. */
20725 if (base && base != error_mark_node)
20727 if (pack_expansion_p)
20728 /* Make this a pack expansion type. */
20729 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20731 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20733 TREE_CHAIN (base) = bases;
20734 bases = base;
20737 /* Peek at the next token. */
20738 token = cp_lexer_peek_token (parser->lexer);
20739 /* If it's not a comma, then the list is complete. */
20740 if (token->type != CPP_COMMA)
20741 break;
20742 /* Consume the `,'. */
20743 cp_lexer_consume_token (parser->lexer);
20746 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20747 base class had a qualified name. However, the next name that
20748 appears is certainly not qualified. */
20749 parser->scope = NULL_TREE;
20750 parser->qualifying_scope = NULL_TREE;
20751 parser->object_scope = NULL_TREE;
20753 return nreverse (bases);
20756 /* Parse a base-specifier.
20758 base-specifier:
20759 :: [opt] nested-name-specifier [opt] class-name
20760 virtual access-specifier [opt] :: [opt] nested-name-specifier
20761 [opt] class-name
20762 access-specifier virtual [opt] :: [opt] nested-name-specifier
20763 [opt] class-name
20765 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20766 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20767 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20768 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20770 static tree
20771 cp_parser_base_specifier (cp_parser* parser)
20773 cp_token *token;
20774 bool done = false;
20775 bool virtual_p = false;
20776 bool duplicate_virtual_error_issued_p = false;
20777 bool duplicate_access_error_issued_p = false;
20778 bool class_scope_p, template_p;
20779 tree access = access_default_node;
20780 tree type;
20782 /* Process the optional `virtual' and `access-specifier'. */
20783 while (!done)
20785 /* Peek at the next token. */
20786 token = cp_lexer_peek_token (parser->lexer);
20787 /* Process `virtual'. */
20788 switch (token->keyword)
20790 case RID_VIRTUAL:
20791 /* If `virtual' appears more than once, issue an error. */
20792 if (virtual_p && !duplicate_virtual_error_issued_p)
20794 cp_parser_error (parser,
20795 "%<virtual%> specified more than once in base-specified");
20796 duplicate_virtual_error_issued_p = true;
20799 virtual_p = true;
20801 /* Consume the `virtual' token. */
20802 cp_lexer_consume_token (parser->lexer);
20804 break;
20806 case RID_PUBLIC:
20807 case RID_PROTECTED:
20808 case RID_PRIVATE:
20809 /* If more than one access specifier appears, issue an
20810 error. */
20811 if (access != access_default_node
20812 && !duplicate_access_error_issued_p)
20814 cp_parser_error (parser,
20815 "more than one access specifier in base-specified");
20816 duplicate_access_error_issued_p = true;
20819 access = ridpointers[(int) token->keyword];
20821 /* Consume the access-specifier. */
20822 cp_lexer_consume_token (parser->lexer);
20824 break;
20826 default:
20827 done = true;
20828 break;
20831 /* It is not uncommon to see programs mechanically, erroneously, use
20832 the 'typename' keyword to denote (dependent) qualified types
20833 as base classes. */
20834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20836 token = cp_lexer_peek_token (parser->lexer);
20837 if (!processing_template_decl)
20838 error_at (token->location,
20839 "keyword %<typename%> not allowed outside of templates");
20840 else
20841 error_at (token->location,
20842 "keyword %<typename%> not allowed in this context "
20843 "(the base class is implicitly a type)");
20844 cp_lexer_consume_token (parser->lexer);
20847 /* Look for the optional `::' operator. */
20848 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20849 /* Look for the nested-name-specifier. The simplest way to
20850 implement:
20852 [temp.res]
20854 The keyword `typename' is not permitted in a base-specifier or
20855 mem-initializer; in these contexts a qualified name that
20856 depends on a template-parameter is implicitly assumed to be a
20857 type name.
20859 is to pretend that we have seen the `typename' keyword at this
20860 point. */
20861 cp_parser_nested_name_specifier_opt (parser,
20862 /*typename_keyword_p=*/true,
20863 /*check_dependency_p=*/true,
20864 typename_type,
20865 /*is_declaration=*/true);
20866 /* If the base class is given by a qualified name, assume that names
20867 we see are type names or templates, as appropriate. */
20868 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20869 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20871 if (!parser->scope
20872 && cp_lexer_next_token_is_decltype (parser->lexer))
20873 /* DR 950 allows decltype as a base-specifier. */
20874 type = cp_parser_decltype (parser);
20875 else
20877 /* Otherwise, look for the class-name. */
20878 type = cp_parser_class_name (parser,
20879 class_scope_p,
20880 template_p,
20881 typename_type,
20882 /*check_dependency_p=*/true,
20883 /*class_head_p=*/false,
20884 /*is_declaration=*/true);
20885 type = TREE_TYPE (type);
20888 if (type == error_mark_node)
20889 return error_mark_node;
20891 return finish_base_specifier (type, access, virtual_p);
20894 /* Exception handling [gram.exception] */
20896 /* Parse an (optional) noexcept-specification.
20898 noexcept-specification:
20899 noexcept ( constant-expression ) [opt]
20901 If no noexcept-specification is present, returns NULL_TREE.
20902 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20903 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20904 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20905 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20906 in which case a boolean condition is returned instead. */
20908 static tree
20909 cp_parser_noexcept_specification_opt (cp_parser* parser,
20910 bool require_constexpr,
20911 bool* consumed_expr,
20912 bool return_cond)
20914 cp_token *token;
20915 const char *saved_message;
20917 /* Peek at the next token. */
20918 token = cp_lexer_peek_token (parser->lexer);
20920 /* Is it a noexcept-specification? */
20921 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20923 tree expr;
20924 cp_lexer_consume_token (parser->lexer);
20926 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20928 cp_lexer_consume_token (parser->lexer);
20930 if (require_constexpr)
20932 /* Types may not be defined in an exception-specification. */
20933 saved_message = parser->type_definition_forbidden_message;
20934 parser->type_definition_forbidden_message
20935 = G_("types may not be defined in an exception-specification");
20937 expr = cp_parser_constant_expression (parser, false, NULL);
20939 /* Restore the saved message. */
20940 parser->type_definition_forbidden_message = saved_message;
20942 else
20944 expr = cp_parser_expression (parser, false, NULL);
20945 *consumed_expr = true;
20948 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20950 else
20952 expr = boolean_true_node;
20953 if (!require_constexpr)
20954 *consumed_expr = false;
20957 /* We cannot build a noexcept-spec right away because this will check
20958 that expr is a constexpr. */
20959 if (!return_cond)
20960 return build_noexcept_spec (expr, tf_warning_or_error);
20961 else
20962 return expr;
20964 else
20965 return NULL_TREE;
20968 /* Parse an (optional) exception-specification.
20970 exception-specification:
20971 throw ( type-id-list [opt] )
20973 Returns a TREE_LIST representing the exception-specification. The
20974 TREE_VALUE of each node is a type. */
20976 static tree
20977 cp_parser_exception_specification_opt (cp_parser* parser)
20979 cp_token *token;
20980 tree type_id_list;
20981 const char *saved_message;
20983 /* Peek at the next token. */
20984 token = cp_lexer_peek_token (parser->lexer);
20986 /* Is it a noexcept-specification? */
20987 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20988 false);
20989 if (type_id_list != NULL_TREE)
20990 return type_id_list;
20992 /* If it's not `throw', then there's no exception-specification. */
20993 if (!cp_parser_is_keyword (token, RID_THROW))
20994 return NULL_TREE;
20996 #if 0
20997 /* Enable this once a lot of code has transitioned to noexcept? */
20998 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
20999 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21000 "deprecated in C++0x; use %<noexcept%> instead");
21001 #endif
21003 /* Consume the `throw'. */
21004 cp_lexer_consume_token (parser->lexer);
21006 /* Look for the `('. */
21007 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21009 /* Peek at the next token. */
21010 token = cp_lexer_peek_token (parser->lexer);
21011 /* If it's not a `)', then there is a type-id-list. */
21012 if (token->type != CPP_CLOSE_PAREN)
21014 /* Types may not be defined in an exception-specification. */
21015 saved_message = parser->type_definition_forbidden_message;
21016 parser->type_definition_forbidden_message
21017 = G_("types may not be defined in an exception-specification");
21018 /* Parse the type-id-list. */
21019 type_id_list = cp_parser_type_id_list (parser);
21020 /* Restore the saved message. */
21021 parser->type_definition_forbidden_message = saved_message;
21023 else
21024 type_id_list = empty_except_spec;
21026 /* Look for the `)'. */
21027 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21029 return type_id_list;
21032 /* Parse an (optional) type-id-list.
21034 type-id-list:
21035 type-id ... [opt]
21036 type-id-list , type-id ... [opt]
21038 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21039 in the order that the types were presented. */
21041 static tree
21042 cp_parser_type_id_list (cp_parser* parser)
21044 tree types = NULL_TREE;
21046 while (true)
21048 cp_token *token;
21049 tree type;
21051 /* Get the next type-id. */
21052 type = cp_parser_type_id (parser);
21053 /* Parse the optional ellipsis. */
21054 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21056 /* Consume the `...'. */
21057 cp_lexer_consume_token (parser->lexer);
21059 /* Turn the type into a pack expansion expression. */
21060 type = make_pack_expansion (type);
21062 /* Add it to the list. */
21063 types = add_exception_specifier (types, type, /*complain=*/1);
21064 /* Peek at the next token. */
21065 token = cp_lexer_peek_token (parser->lexer);
21066 /* If it is not a `,', we are done. */
21067 if (token->type != CPP_COMMA)
21068 break;
21069 /* Consume the `,'. */
21070 cp_lexer_consume_token (parser->lexer);
21073 return nreverse (types);
21076 /* Parse a try-block.
21078 try-block:
21079 try compound-statement handler-seq */
21081 static tree
21082 cp_parser_try_block (cp_parser* parser)
21084 tree try_block;
21086 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21087 try_block = begin_try_block ();
21088 cp_parser_compound_statement (parser, NULL, true, false);
21089 finish_try_block (try_block);
21090 cp_parser_handler_seq (parser);
21091 finish_handler_sequence (try_block);
21093 return try_block;
21096 /* Parse a function-try-block.
21098 function-try-block:
21099 try ctor-initializer [opt] function-body handler-seq */
21101 static bool
21102 cp_parser_function_try_block (cp_parser* parser)
21104 tree compound_stmt;
21105 tree try_block;
21106 bool ctor_initializer_p;
21108 /* Look for the `try' keyword. */
21109 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21110 return false;
21111 /* Let the rest of the front end know where we are. */
21112 try_block = begin_function_try_block (&compound_stmt);
21113 /* Parse the function-body. */
21114 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21115 (parser, /*in_function_try_block=*/true);
21116 /* We're done with the `try' part. */
21117 finish_function_try_block (try_block);
21118 /* Parse the handlers. */
21119 cp_parser_handler_seq (parser);
21120 /* We're done with the handlers. */
21121 finish_function_handler_sequence (try_block, compound_stmt);
21123 return ctor_initializer_p;
21126 /* Parse a handler-seq.
21128 handler-seq:
21129 handler handler-seq [opt] */
21131 static void
21132 cp_parser_handler_seq (cp_parser* parser)
21134 while (true)
21136 cp_token *token;
21138 /* Parse the handler. */
21139 cp_parser_handler (parser);
21140 /* Peek at the next token. */
21141 token = cp_lexer_peek_token (parser->lexer);
21142 /* If it's not `catch' then there are no more handlers. */
21143 if (!cp_parser_is_keyword (token, RID_CATCH))
21144 break;
21148 /* Parse a handler.
21150 handler:
21151 catch ( exception-declaration ) compound-statement */
21153 static void
21154 cp_parser_handler (cp_parser* parser)
21156 tree handler;
21157 tree declaration;
21159 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21160 handler = begin_handler ();
21161 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21162 declaration = cp_parser_exception_declaration (parser);
21163 finish_handler_parms (declaration, handler);
21164 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21165 cp_parser_compound_statement (parser, NULL, false, false);
21166 finish_handler (handler);
21169 /* Parse an exception-declaration.
21171 exception-declaration:
21172 type-specifier-seq declarator
21173 type-specifier-seq abstract-declarator
21174 type-specifier-seq
21177 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21178 ellipsis variant is used. */
21180 static tree
21181 cp_parser_exception_declaration (cp_parser* parser)
21183 cp_decl_specifier_seq type_specifiers;
21184 cp_declarator *declarator;
21185 const char *saved_message;
21187 /* If it's an ellipsis, it's easy to handle. */
21188 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21190 /* Consume the `...' token. */
21191 cp_lexer_consume_token (parser->lexer);
21192 return NULL_TREE;
21195 /* Types may not be defined in exception-declarations. */
21196 saved_message = parser->type_definition_forbidden_message;
21197 parser->type_definition_forbidden_message
21198 = G_("types may not be defined in exception-declarations");
21200 /* Parse the type-specifier-seq. */
21201 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21202 /*is_trailing_return=*/false,
21203 &type_specifiers);
21204 /* If it's a `)', then there is no declarator. */
21205 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21206 declarator = NULL;
21207 else
21208 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21209 /*ctor_dtor_or_conv_p=*/NULL,
21210 /*parenthesized_p=*/NULL,
21211 /*member_p=*/false);
21213 /* Restore the saved message. */
21214 parser->type_definition_forbidden_message = saved_message;
21216 if (!type_specifiers.any_specifiers_p)
21217 return error_mark_node;
21219 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21222 /* Parse a throw-expression.
21224 throw-expression:
21225 throw assignment-expression [opt]
21227 Returns a THROW_EXPR representing the throw-expression. */
21229 static tree
21230 cp_parser_throw_expression (cp_parser* parser)
21232 tree expression;
21233 cp_token* token;
21235 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21236 token = cp_lexer_peek_token (parser->lexer);
21237 /* Figure out whether or not there is an assignment-expression
21238 following the "throw" keyword. */
21239 if (token->type == CPP_COMMA
21240 || token->type == CPP_SEMICOLON
21241 || token->type == CPP_CLOSE_PAREN
21242 || token->type == CPP_CLOSE_SQUARE
21243 || token->type == CPP_CLOSE_BRACE
21244 || token->type == CPP_COLON)
21245 expression = NULL_TREE;
21246 else
21247 expression = cp_parser_assignment_expression (parser,
21248 /*cast_p=*/false, NULL);
21250 return build_throw (expression);
21253 /* GNU Extensions */
21255 /* Parse an (optional) asm-specification.
21257 asm-specification:
21258 asm ( string-literal )
21260 If the asm-specification is present, returns a STRING_CST
21261 corresponding to the string-literal. Otherwise, returns
21262 NULL_TREE. */
21264 static tree
21265 cp_parser_asm_specification_opt (cp_parser* parser)
21267 cp_token *token;
21268 tree asm_specification;
21270 /* Peek at the next token. */
21271 token = cp_lexer_peek_token (parser->lexer);
21272 /* If the next token isn't the `asm' keyword, then there's no
21273 asm-specification. */
21274 if (!cp_parser_is_keyword (token, RID_ASM))
21275 return NULL_TREE;
21277 /* Consume the `asm' token. */
21278 cp_lexer_consume_token (parser->lexer);
21279 /* Look for the `('. */
21280 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21282 /* Look for the string-literal. */
21283 asm_specification = cp_parser_string_literal (parser, false, false);
21285 /* Look for the `)'. */
21286 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21288 return asm_specification;
21291 /* Parse an asm-operand-list.
21293 asm-operand-list:
21294 asm-operand
21295 asm-operand-list , asm-operand
21297 asm-operand:
21298 string-literal ( expression )
21299 [ string-literal ] string-literal ( expression )
21301 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21302 each node is the expression. The TREE_PURPOSE is itself a
21303 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21304 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21305 is a STRING_CST for the string literal before the parenthesis. Returns
21306 ERROR_MARK_NODE if any of the operands are invalid. */
21308 static tree
21309 cp_parser_asm_operand_list (cp_parser* parser)
21311 tree asm_operands = NULL_TREE;
21312 bool invalid_operands = false;
21314 while (true)
21316 tree string_literal;
21317 tree expression;
21318 tree name;
21320 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21322 /* Consume the `[' token. */
21323 cp_lexer_consume_token (parser->lexer);
21324 /* Read the operand name. */
21325 name = cp_parser_identifier (parser);
21326 if (name != error_mark_node)
21327 name = build_string (IDENTIFIER_LENGTH (name),
21328 IDENTIFIER_POINTER (name));
21329 /* Look for the closing `]'. */
21330 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21332 else
21333 name = NULL_TREE;
21334 /* Look for the string-literal. */
21335 string_literal = cp_parser_string_literal (parser, false, false);
21337 /* Look for the `('. */
21338 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21339 /* Parse the expression. */
21340 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21341 /* Look for the `)'. */
21342 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21344 if (name == error_mark_node
21345 || string_literal == error_mark_node
21346 || expression == error_mark_node)
21347 invalid_operands = true;
21349 /* Add this operand to the list. */
21350 asm_operands = tree_cons (build_tree_list (name, string_literal),
21351 expression,
21352 asm_operands);
21353 /* If the next token is not a `,', there are no more
21354 operands. */
21355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21356 break;
21357 /* Consume the `,'. */
21358 cp_lexer_consume_token (parser->lexer);
21361 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21364 /* Parse an asm-clobber-list.
21366 asm-clobber-list:
21367 string-literal
21368 asm-clobber-list , string-literal
21370 Returns a TREE_LIST, indicating the clobbers in the order that they
21371 appeared. The TREE_VALUE of each node is a STRING_CST. */
21373 static tree
21374 cp_parser_asm_clobber_list (cp_parser* parser)
21376 tree clobbers = NULL_TREE;
21378 while (true)
21380 tree string_literal;
21382 /* Look for the string literal. */
21383 string_literal = cp_parser_string_literal (parser, false, false);
21384 /* Add it to the list. */
21385 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21386 /* If the next token is not a `,', then the list is
21387 complete. */
21388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21389 break;
21390 /* Consume the `,' token. */
21391 cp_lexer_consume_token (parser->lexer);
21394 return clobbers;
21397 /* Parse an asm-label-list.
21399 asm-label-list:
21400 identifier
21401 asm-label-list , identifier
21403 Returns a TREE_LIST, indicating the labels in the order that they
21404 appeared. The TREE_VALUE of each node is a label. */
21406 static tree
21407 cp_parser_asm_label_list (cp_parser* parser)
21409 tree labels = NULL_TREE;
21411 while (true)
21413 tree identifier, label, name;
21415 /* Look for the identifier. */
21416 identifier = cp_parser_identifier (parser);
21417 if (!error_operand_p (identifier))
21419 label = lookup_label (identifier);
21420 if (TREE_CODE (label) == LABEL_DECL)
21422 TREE_USED (label) = 1;
21423 check_goto (label);
21424 name = build_string (IDENTIFIER_LENGTH (identifier),
21425 IDENTIFIER_POINTER (identifier));
21426 labels = tree_cons (name, label, labels);
21429 /* If the next token is not a `,', then the list is
21430 complete. */
21431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21432 break;
21433 /* Consume the `,' token. */
21434 cp_lexer_consume_token (parser->lexer);
21437 return nreverse (labels);
21440 /* Return TRUE iff the next tokens in the stream are possibly the
21441 beginning of a GNU extension attribute. */
21443 static bool
21444 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21446 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21449 /* Return TRUE iff the next tokens in the stream are possibly the
21450 beginning of a standard C++-11 attribute specifier. */
21452 static bool
21453 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21455 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21458 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21459 beginning of a standard C++-11 attribute specifier. */
21461 static bool
21462 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21464 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21466 return (cxx_dialect >= cxx11
21467 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21468 || (token->type == CPP_OPEN_SQUARE
21469 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21470 && token->type == CPP_OPEN_SQUARE)));
21473 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21474 beginning of a GNU extension attribute. */
21476 static bool
21477 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21479 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21481 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21484 /* Return true iff the next tokens can be the beginning of either a
21485 GNU attribute list, or a standard C++11 attribute sequence. */
21487 static bool
21488 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21490 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21491 || cp_next_tokens_can_be_std_attribute_p (parser));
21494 /* Return true iff the next Nth tokens can be the beginning of either
21495 a GNU attribute list, or a standard C++11 attribute sequence. */
21497 static bool
21498 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21500 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21501 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21504 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21505 of GNU attributes, or return NULL. */
21507 static tree
21508 cp_parser_attributes_opt (cp_parser *parser)
21510 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21511 return cp_parser_gnu_attributes_opt (parser);
21512 return cp_parser_std_attribute_spec_seq (parser);
21515 #define CILK_SIMD_FN_CLAUSE_MASK \
21516 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21517 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21518 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21519 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21520 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21522 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21523 vector [(<clauses>)] */
21525 static void
21526 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21528 bool first_p = parser->cilk_simd_fn_info == NULL;
21529 cp_token *token = v_token;
21530 if (first_p)
21532 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21533 parser->cilk_simd_fn_info->error_seen = false;
21534 parser->cilk_simd_fn_info->fndecl_seen = false;
21535 parser->cilk_simd_fn_info->tokens = vNULL;
21537 int paren_scope = 0;
21538 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21540 cp_lexer_consume_token (parser->lexer);
21541 v_token = cp_lexer_peek_token (parser->lexer);
21542 paren_scope++;
21544 while (paren_scope > 0)
21546 token = cp_lexer_peek_token (parser->lexer);
21547 if (token->type == CPP_OPEN_PAREN)
21548 paren_scope++;
21549 else if (token->type == CPP_CLOSE_PAREN)
21550 paren_scope--;
21551 /* Do not push the last ')' */
21552 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21553 cp_lexer_consume_token (parser->lexer);
21556 token->type = CPP_PRAGMA_EOL;
21557 parser->lexer->next_token = token;
21558 cp_lexer_consume_token (parser->lexer);
21560 struct cp_token_cache *cp
21561 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21562 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21565 /* Parse an (optional) series of attributes.
21567 attributes:
21568 attributes attribute
21570 attribute:
21571 __attribute__ (( attribute-list [opt] ))
21573 The return value is as for cp_parser_gnu_attribute_list. */
21575 static tree
21576 cp_parser_gnu_attributes_opt (cp_parser* parser)
21578 tree attributes = NULL_TREE;
21580 while (true)
21582 cp_token *token;
21583 tree attribute_list;
21584 bool ok = true;
21586 /* Peek at the next token. */
21587 token = cp_lexer_peek_token (parser->lexer);
21588 /* If it's not `__attribute__', then we're done. */
21589 if (token->keyword != RID_ATTRIBUTE)
21590 break;
21592 /* Consume the `__attribute__' keyword. */
21593 cp_lexer_consume_token (parser->lexer);
21594 /* Look for the two `(' tokens. */
21595 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21596 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21598 /* Peek at the next token. */
21599 token = cp_lexer_peek_token (parser->lexer);
21600 if (token->type != CPP_CLOSE_PAREN)
21601 /* Parse the attribute-list. */
21602 attribute_list = cp_parser_gnu_attribute_list (parser);
21603 else
21604 /* If the next token is a `)', then there is no attribute
21605 list. */
21606 attribute_list = NULL;
21608 /* Look for the two `)' tokens. */
21609 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21610 ok = false;
21611 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21612 ok = false;
21613 if (!ok)
21614 cp_parser_skip_to_end_of_statement (parser);
21616 /* Add these new attributes to the list. */
21617 attributes = chainon (attributes, attribute_list);
21620 return attributes;
21623 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21624 "__vector" or "__vector__." */
21626 static inline bool
21627 is_cilkplus_vector_p (tree name)
21629 if (flag_cilkplus && is_attribute_p ("vector", name))
21630 return true;
21631 return false;
21634 /* Parse a GNU attribute-list.
21636 attribute-list:
21637 attribute
21638 attribute-list , attribute
21640 attribute:
21641 identifier
21642 identifier ( identifier )
21643 identifier ( identifier , expression-list )
21644 identifier ( expression-list )
21646 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21647 to an attribute. The TREE_PURPOSE of each node is the identifier
21648 indicating which attribute is in use. The TREE_VALUE represents
21649 the arguments, if any. */
21651 static tree
21652 cp_parser_gnu_attribute_list (cp_parser* parser)
21654 tree attribute_list = NULL_TREE;
21655 bool save_translate_strings_p = parser->translate_strings_p;
21657 parser->translate_strings_p = false;
21658 while (true)
21660 cp_token *token;
21661 tree identifier;
21662 tree attribute;
21664 /* Look for the identifier. We also allow keywords here; for
21665 example `__attribute__ ((const))' is legal. */
21666 token = cp_lexer_peek_token (parser->lexer);
21667 if (token->type == CPP_NAME
21668 || token->type == CPP_KEYWORD)
21670 tree arguments = NULL_TREE;
21672 /* Consume the token, but save it since we need it for the
21673 SIMD enabled function parsing. */
21674 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21676 /* Save away the identifier that indicates which attribute
21677 this is. */
21678 identifier = (token->type == CPP_KEYWORD)
21679 /* For keywords, use the canonical spelling, not the
21680 parsed identifier. */
21681 ? ridpointers[(int) token->keyword]
21682 : id_token->u.value;
21684 attribute = build_tree_list (identifier, NULL_TREE);
21686 /* Peek at the next token. */
21687 token = cp_lexer_peek_token (parser->lexer);
21688 /* If it's an `(', then parse the attribute arguments. */
21689 if (token->type == CPP_OPEN_PAREN)
21691 vec<tree, va_gc> *vec;
21692 int attr_flag = (attribute_takes_identifier_p (identifier)
21693 ? id_attr : normal_attr);
21694 if (is_cilkplus_vector_p (identifier))
21696 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21697 continue;
21699 else
21700 vec = cp_parser_parenthesized_expression_list
21701 (parser, attr_flag, /*cast_p=*/false,
21702 /*allow_expansion_p=*/false,
21703 /*non_constant_p=*/NULL);
21704 if (vec == NULL)
21705 arguments = error_mark_node;
21706 else
21708 arguments = build_tree_list_vec (vec);
21709 release_tree_vector (vec);
21711 /* Save the arguments away. */
21712 TREE_VALUE (attribute) = arguments;
21714 else if (is_cilkplus_vector_p (identifier))
21716 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21717 continue;
21720 if (arguments != error_mark_node)
21722 /* Add this attribute to the list. */
21723 TREE_CHAIN (attribute) = attribute_list;
21724 attribute_list = attribute;
21727 token = cp_lexer_peek_token (parser->lexer);
21729 /* Now, look for more attributes. If the next token isn't a
21730 `,', we're done. */
21731 if (token->type != CPP_COMMA)
21732 break;
21734 /* Consume the comma and keep going. */
21735 cp_lexer_consume_token (parser->lexer);
21737 parser->translate_strings_p = save_translate_strings_p;
21739 /* We built up the list in reverse order. */
21740 return nreverse (attribute_list);
21743 /* Parse a standard C++11 attribute.
21745 The returned representation is a TREE_LIST which TREE_PURPOSE is
21746 the scoped name of the attribute, and the TREE_VALUE is its
21747 arguments list.
21749 Note that the scoped name of the attribute is itself a TREE_LIST
21750 which TREE_PURPOSE is the namespace of the attribute, and
21751 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21752 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21753 and which TREE_PURPOSE is directly the attribute name.
21755 Clients of the attribute code should use get_attribute_namespace
21756 and get_attribute_name to get the actual namespace and name of
21757 attributes, regardless of their being GNU or C++11 attributes.
21759 attribute:
21760 attribute-token attribute-argument-clause [opt]
21762 attribute-token:
21763 identifier
21764 attribute-scoped-token
21766 attribute-scoped-token:
21767 attribute-namespace :: identifier
21769 attribute-namespace:
21770 identifier
21772 attribute-argument-clause:
21773 ( balanced-token-seq )
21775 balanced-token-seq:
21776 balanced-token [opt]
21777 balanced-token-seq balanced-token
21779 balanced-token:
21780 ( balanced-token-seq )
21781 [ balanced-token-seq ]
21782 { balanced-token-seq }. */
21784 static tree
21785 cp_parser_std_attribute (cp_parser *parser)
21787 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21788 cp_token *token;
21790 /* First, parse name of the the attribute, a.k.a
21791 attribute-token. */
21793 token = cp_lexer_peek_token (parser->lexer);
21794 if (token->type == CPP_NAME)
21795 attr_id = token->u.value;
21796 else if (token->type == CPP_KEYWORD)
21797 attr_id = ridpointers[(int) token->keyword];
21798 else if (token->flags & NAMED_OP)
21799 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21801 if (attr_id == NULL_TREE)
21802 return NULL_TREE;
21804 cp_lexer_consume_token (parser->lexer);
21806 token = cp_lexer_peek_token (parser->lexer);
21807 if (token->type == CPP_SCOPE)
21809 /* We are seeing a scoped attribute token. */
21811 cp_lexer_consume_token (parser->lexer);
21812 attr_ns = attr_id;
21814 token = cp_lexer_consume_token (parser->lexer);
21815 if (token->type == CPP_NAME)
21816 attr_id = token->u.value;
21817 else if (token->type == CPP_KEYWORD)
21818 attr_id = ridpointers[(int) token->keyword];
21819 else
21821 error_at (token->location,
21822 "expected an identifier for the attribute name");
21823 return error_mark_node;
21825 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21826 NULL_TREE);
21827 token = cp_lexer_peek_token (parser->lexer);
21829 else
21831 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21832 NULL_TREE);
21833 /* C++11 noreturn attribute is equivalent to GNU's. */
21834 if (is_attribute_p ("noreturn", attr_id))
21835 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21836 /* C++14 deprecated attribute is equivalent to GNU's. */
21837 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21838 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21841 /* Now parse the optional argument clause of the attribute. */
21843 if (token->type != CPP_OPEN_PAREN)
21844 return attribute;
21847 vec<tree, va_gc> *vec;
21848 int attr_flag = normal_attr;
21850 if (attr_ns == get_identifier ("gnu")
21851 && attribute_takes_identifier_p (attr_id))
21852 /* A GNU attribute that takes an identifier in parameter. */
21853 attr_flag = id_attr;
21855 vec = cp_parser_parenthesized_expression_list
21856 (parser, attr_flag, /*cast_p=*/false,
21857 /*allow_expansion_p=*/true,
21858 /*non_constant_p=*/NULL);
21859 if (vec == NULL)
21860 arguments = error_mark_node;
21861 else
21863 arguments = build_tree_list_vec (vec);
21864 release_tree_vector (vec);
21867 if (arguments == error_mark_node)
21868 attribute = error_mark_node;
21869 else
21870 TREE_VALUE (attribute) = arguments;
21873 return attribute;
21876 /* Parse a list of standard C++-11 attributes.
21878 attribute-list:
21879 attribute [opt]
21880 attribute-list , attribute[opt]
21881 attribute ...
21882 attribute-list , attribute ...
21885 static tree
21886 cp_parser_std_attribute_list (cp_parser *parser)
21888 tree attributes = NULL_TREE, attribute = NULL_TREE;
21889 cp_token *token = NULL;
21891 while (true)
21893 attribute = cp_parser_std_attribute (parser);
21894 if (attribute == error_mark_node)
21895 break;
21896 if (attribute != NULL_TREE)
21898 TREE_CHAIN (attribute) = attributes;
21899 attributes = attribute;
21901 token = cp_lexer_peek_token (parser->lexer);
21902 if (token->type != CPP_COMMA)
21903 break;
21904 cp_lexer_consume_token (parser->lexer);
21906 attributes = nreverse (attributes);
21907 return attributes;
21910 /* Parse a standard C++-11 attribute specifier.
21912 attribute-specifier:
21913 [ [ attribute-list ] ]
21914 alignment-specifier
21916 alignment-specifier:
21917 alignas ( type-id ... [opt] )
21918 alignas ( alignment-expression ... [opt] ). */
21920 static tree
21921 cp_parser_std_attribute_spec (cp_parser *parser)
21923 tree attributes = NULL_TREE;
21924 cp_token *token = cp_lexer_peek_token (parser->lexer);
21926 if (token->type == CPP_OPEN_SQUARE
21927 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21929 cp_lexer_consume_token (parser->lexer);
21930 cp_lexer_consume_token (parser->lexer);
21932 attributes = cp_parser_std_attribute_list (parser);
21934 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21935 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21936 cp_parser_skip_to_end_of_statement (parser);
21937 else
21938 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21939 when we are sure that we have actually parsed them. */
21940 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21942 else
21944 tree alignas_expr;
21946 /* Look for an alignment-specifier. */
21948 token = cp_lexer_peek_token (parser->lexer);
21950 if (token->type != CPP_KEYWORD
21951 || token->keyword != RID_ALIGNAS)
21952 return NULL_TREE;
21954 cp_lexer_consume_token (parser->lexer);
21955 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21957 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21959 cp_parser_error (parser, "expected %<(%>");
21960 return error_mark_node;
21963 cp_parser_parse_tentatively (parser);
21964 alignas_expr = cp_parser_type_id (parser);
21966 if (!cp_parser_parse_definitely (parser))
21968 gcc_assert (alignas_expr == error_mark_node
21969 || alignas_expr == NULL_TREE);
21971 alignas_expr =
21972 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21973 /**cp_id_kind=*/NULL);
21974 if (alignas_expr == error_mark_node)
21975 cp_parser_skip_to_end_of_statement (parser);
21976 if (alignas_expr == NULL_TREE
21977 || alignas_expr == error_mark_node)
21978 return alignas_expr;
21981 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21983 cp_parser_error (parser, "expected %<)%>");
21984 return error_mark_node;
21987 alignas_expr = cxx_alignas_expr (alignas_expr);
21989 /* Build the C++-11 representation of an 'aligned'
21990 attribute. */
21991 attributes =
21992 build_tree_list (build_tree_list (get_identifier ("gnu"),
21993 get_identifier ("aligned")),
21994 build_tree_list (NULL_TREE, alignas_expr));
21997 return attributes;
22000 /* Parse a standard C++-11 attribute-specifier-seq.
22002 attribute-specifier-seq:
22003 attribute-specifier-seq [opt] attribute-specifier
22006 static tree
22007 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22009 tree attr_specs = NULL;
22011 while (true)
22013 tree attr_spec = cp_parser_std_attribute_spec (parser);
22014 if (attr_spec == NULL_TREE)
22015 break;
22016 if (attr_spec == error_mark_node)
22017 return error_mark_node;
22019 TREE_CHAIN (attr_spec) = attr_specs;
22020 attr_specs = attr_spec;
22023 attr_specs = nreverse (attr_specs);
22024 return attr_specs;
22027 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22028 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22029 current value of the PEDANTIC flag, regardless of whether or not
22030 the `__extension__' keyword is present. The caller is responsible
22031 for restoring the value of the PEDANTIC flag. */
22033 static bool
22034 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22036 /* Save the old value of the PEDANTIC flag. */
22037 *saved_pedantic = pedantic;
22039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22041 /* Consume the `__extension__' token. */
22042 cp_lexer_consume_token (parser->lexer);
22043 /* We're not being pedantic while the `__extension__' keyword is
22044 in effect. */
22045 pedantic = 0;
22047 return true;
22050 return false;
22053 /* Parse a label declaration.
22055 label-declaration:
22056 __label__ label-declarator-seq ;
22058 label-declarator-seq:
22059 identifier , label-declarator-seq
22060 identifier */
22062 static void
22063 cp_parser_label_declaration (cp_parser* parser)
22065 /* Look for the `__label__' keyword. */
22066 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22068 while (true)
22070 tree identifier;
22072 /* Look for an identifier. */
22073 identifier = cp_parser_identifier (parser);
22074 /* If we failed, stop. */
22075 if (identifier == error_mark_node)
22076 break;
22077 /* Declare it as a label. */
22078 finish_label_decl (identifier);
22079 /* If the next token is a `;', stop. */
22080 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22081 break;
22082 /* Look for the `,' separating the label declarations. */
22083 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22086 /* Look for the final `;'. */
22087 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22090 /* Support Functions */
22092 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22093 NAME should have one of the representations used for an
22094 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22095 is returned. If PARSER->SCOPE is a dependent type, then a
22096 SCOPE_REF is returned.
22098 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22099 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22100 was formed. Abstractly, such entities should not be passed to this
22101 function, because they do not need to be looked up, but it is
22102 simpler to check for this special case here, rather than at the
22103 call-sites.
22105 In cases not explicitly covered above, this function returns a
22106 DECL, OVERLOAD, or baselink representing the result of the lookup.
22107 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22108 is returned.
22110 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22111 (e.g., "struct") that was used. In that case bindings that do not
22112 refer to types are ignored.
22114 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22115 ignored.
22117 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22118 are ignored.
22120 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22121 types.
22123 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22124 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22125 NULL_TREE otherwise. */
22127 static tree
22128 cp_parser_lookup_name (cp_parser *parser, tree name,
22129 enum tag_types tag_type,
22130 bool is_template,
22131 bool is_namespace,
22132 bool check_dependency,
22133 tree *ambiguous_decls,
22134 location_t name_location)
22136 tree decl;
22137 tree object_type = parser->context->object_type;
22139 /* Assume that the lookup will be unambiguous. */
22140 if (ambiguous_decls)
22141 *ambiguous_decls = NULL_TREE;
22143 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22144 no longer valid. Note that if we are parsing tentatively, and
22145 the parse fails, OBJECT_TYPE will be automatically restored. */
22146 parser->context->object_type = NULL_TREE;
22148 if (name == error_mark_node)
22149 return error_mark_node;
22151 /* A template-id has already been resolved; there is no lookup to
22152 do. */
22153 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22154 return name;
22155 if (BASELINK_P (name))
22157 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22158 == TEMPLATE_ID_EXPR);
22159 return name;
22162 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22163 it should already have been checked to make sure that the name
22164 used matches the type being destroyed. */
22165 if (TREE_CODE (name) == BIT_NOT_EXPR)
22167 tree type;
22169 /* Figure out to which type this destructor applies. */
22170 if (parser->scope)
22171 type = parser->scope;
22172 else if (object_type)
22173 type = object_type;
22174 else
22175 type = current_class_type;
22176 /* If that's not a class type, there is no destructor. */
22177 if (!type || !CLASS_TYPE_P (type))
22178 return error_mark_node;
22179 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22180 lazily_declare_fn (sfk_destructor, type);
22181 if (!CLASSTYPE_DESTRUCTORS (type))
22182 return error_mark_node;
22183 /* If it was a class type, return the destructor. */
22184 return CLASSTYPE_DESTRUCTORS (type);
22187 /* By this point, the NAME should be an ordinary identifier. If
22188 the id-expression was a qualified name, the qualifying scope is
22189 stored in PARSER->SCOPE at this point. */
22190 gcc_assert (identifier_p (name));
22192 /* Perform the lookup. */
22193 if (parser->scope)
22195 bool dependent_p;
22197 if (parser->scope == error_mark_node)
22198 return error_mark_node;
22200 /* If the SCOPE is dependent, the lookup must be deferred until
22201 the template is instantiated -- unless we are explicitly
22202 looking up names in uninstantiated templates. Even then, we
22203 cannot look up the name if the scope is not a class type; it
22204 might, for example, be a template type parameter. */
22205 dependent_p = (TYPE_P (parser->scope)
22206 && dependent_scope_p (parser->scope));
22207 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22208 && dependent_p)
22209 /* Defer lookup. */
22210 decl = error_mark_node;
22211 else
22213 tree pushed_scope = NULL_TREE;
22215 /* If PARSER->SCOPE is a dependent type, then it must be a
22216 class type, and we must not be checking dependencies;
22217 otherwise, we would have processed this lookup above. So
22218 that PARSER->SCOPE is not considered a dependent base by
22219 lookup_member, we must enter the scope here. */
22220 if (dependent_p)
22221 pushed_scope = push_scope (parser->scope);
22223 /* If the PARSER->SCOPE is a template specialization, it
22224 may be instantiated during name lookup. In that case,
22225 errors may be issued. Even if we rollback the current
22226 tentative parse, those errors are valid. */
22227 decl = lookup_qualified_name (parser->scope, name,
22228 tag_type != none_type,
22229 /*complain=*/true);
22231 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22232 lookup result and the nested-name-specifier nominates a class C:
22233 * if the name specified after the nested-name-specifier, when
22234 looked up in C, is the injected-class-name of C (Clause 9), or
22235 * if the name specified after the nested-name-specifier is the
22236 same as the identifier or the simple-template-id's template-
22237 name in the last component of the nested-name-specifier,
22238 the name is instead considered to name the constructor of
22239 class C. [ Note: for example, the constructor is not an
22240 acceptable lookup result in an elaborated-type-specifier so
22241 the constructor would not be used in place of the
22242 injected-class-name. --end note ] Such a constructor name
22243 shall be used only in the declarator-id of a declaration that
22244 names a constructor or in a using-declaration. */
22245 if (tag_type == none_type
22246 && DECL_SELF_REFERENCE_P (decl)
22247 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22248 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22249 tag_type != none_type,
22250 /*complain=*/true);
22252 /* If we have a single function from a using decl, pull it out. */
22253 if (TREE_CODE (decl) == OVERLOAD
22254 && !really_overloaded_fn (decl))
22255 decl = OVL_FUNCTION (decl);
22257 if (pushed_scope)
22258 pop_scope (pushed_scope);
22261 /* If the scope is a dependent type and either we deferred lookup or
22262 we did lookup but didn't find the name, rememeber the name. */
22263 if (decl == error_mark_node && TYPE_P (parser->scope)
22264 && dependent_type_p (parser->scope))
22266 if (tag_type)
22268 tree type;
22270 /* The resolution to Core Issue 180 says that `struct
22271 A::B' should be considered a type-name, even if `A'
22272 is dependent. */
22273 type = make_typename_type (parser->scope, name, tag_type,
22274 /*complain=*/tf_error);
22275 if (type != error_mark_node)
22276 decl = TYPE_NAME (type);
22278 else if (is_template
22279 && (cp_parser_next_token_ends_template_argument_p (parser)
22280 || cp_lexer_next_token_is (parser->lexer,
22281 CPP_CLOSE_PAREN)))
22282 decl = make_unbound_class_template (parser->scope,
22283 name, NULL_TREE,
22284 /*complain=*/tf_error);
22285 else
22286 decl = build_qualified_name (/*type=*/NULL_TREE,
22287 parser->scope, name,
22288 is_template);
22290 parser->qualifying_scope = parser->scope;
22291 parser->object_scope = NULL_TREE;
22293 else if (object_type)
22295 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22296 OBJECT_TYPE is not a class. */
22297 if (CLASS_TYPE_P (object_type))
22298 /* If the OBJECT_TYPE is a template specialization, it may
22299 be instantiated during name lookup. In that case, errors
22300 may be issued. Even if we rollback the current tentative
22301 parse, those errors are valid. */
22302 decl = lookup_member (object_type,
22303 name,
22304 /*protect=*/0,
22305 tag_type != none_type,
22306 tf_warning_or_error);
22307 else
22308 decl = NULL_TREE;
22310 if (!decl)
22311 /* Look it up in the enclosing context. */
22312 decl = lookup_name_real (name, tag_type != none_type,
22313 /*nonclass=*/0,
22314 /*block_p=*/true, is_namespace, 0);
22315 parser->object_scope = object_type;
22316 parser->qualifying_scope = NULL_TREE;
22318 else
22320 decl = lookup_name_real (name, tag_type != none_type,
22321 /*nonclass=*/0,
22322 /*block_p=*/true, is_namespace, 0);
22323 parser->qualifying_scope = NULL_TREE;
22324 parser->object_scope = NULL_TREE;
22327 /* If the lookup failed, let our caller know. */
22328 if (!decl || decl == error_mark_node)
22329 return error_mark_node;
22331 /* Pull out the template from an injected-class-name (or multiple). */
22332 if (is_template)
22333 decl = maybe_get_template_decl_from_type_decl (decl);
22335 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22336 if (TREE_CODE (decl) == TREE_LIST)
22338 if (ambiguous_decls)
22339 *ambiguous_decls = decl;
22340 /* The error message we have to print is too complicated for
22341 cp_parser_error, so we incorporate its actions directly. */
22342 if (!cp_parser_simulate_error (parser))
22344 error_at (name_location, "reference to %qD is ambiguous",
22345 name);
22346 print_candidates (decl);
22348 return error_mark_node;
22351 gcc_assert (DECL_P (decl)
22352 || TREE_CODE (decl) == OVERLOAD
22353 || TREE_CODE (decl) == SCOPE_REF
22354 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22355 || BASELINK_P (decl));
22357 /* If we have resolved the name of a member declaration, check to
22358 see if the declaration is accessible. When the name resolves to
22359 set of overloaded functions, accessibility is checked when
22360 overload resolution is done.
22362 During an explicit instantiation, access is not checked at all,
22363 as per [temp.explicit]. */
22364 if (DECL_P (decl))
22365 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22367 maybe_record_typedef_use (decl);
22369 return decl;
22372 /* Like cp_parser_lookup_name, but for use in the typical case where
22373 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22374 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22376 static tree
22377 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22379 return cp_parser_lookup_name (parser, name,
22380 none_type,
22381 /*is_template=*/false,
22382 /*is_namespace=*/false,
22383 /*check_dependency=*/true,
22384 /*ambiguous_decls=*/NULL,
22385 location);
22388 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22389 the current context, return the TYPE_DECL. If TAG_NAME_P is
22390 true, the DECL indicates the class being defined in a class-head,
22391 or declared in an elaborated-type-specifier.
22393 Otherwise, return DECL. */
22395 static tree
22396 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22398 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22399 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22401 struct A {
22402 template <typename T> struct B;
22405 template <typename T> struct A::B {};
22407 Similarly, in an elaborated-type-specifier:
22409 namespace N { struct X{}; }
22411 struct A {
22412 template <typename T> friend struct N::X;
22415 However, if the DECL refers to a class type, and we are in
22416 the scope of the class, then the name lookup automatically
22417 finds the TYPE_DECL created by build_self_reference rather
22418 than a TEMPLATE_DECL. For example, in:
22420 template <class T> struct S {
22421 S s;
22424 there is no need to handle such case. */
22426 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22427 return DECL_TEMPLATE_RESULT (decl);
22429 return decl;
22432 /* If too many, or too few, template-parameter lists apply to the
22433 declarator, issue an error message. Returns TRUE if all went well,
22434 and FALSE otherwise. */
22436 static bool
22437 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22438 cp_declarator *declarator,
22439 location_t declarator_location)
22441 switch (declarator->kind)
22443 case cdk_id:
22445 unsigned num_templates = 0;
22446 tree scope = declarator->u.id.qualifying_scope;
22448 if (scope)
22449 num_templates = num_template_headers_for_class (scope);
22450 else if (TREE_CODE (declarator->u.id.unqualified_name)
22451 == TEMPLATE_ID_EXPR)
22452 /* If the DECLARATOR has the form `X<y>' then it uses one
22453 additional level of template parameters. */
22454 ++num_templates;
22456 return cp_parser_check_template_parameters
22457 (parser, num_templates, declarator_location, declarator);
22460 case cdk_function:
22461 case cdk_array:
22462 case cdk_pointer:
22463 case cdk_reference:
22464 case cdk_ptrmem:
22465 return (cp_parser_check_declarator_template_parameters
22466 (parser, declarator->declarator, declarator_location));
22468 case cdk_error:
22469 return true;
22471 default:
22472 gcc_unreachable ();
22474 return false;
22477 /* NUM_TEMPLATES were used in the current declaration. If that is
22478 invalid, return FALSE and issue an error messages. Otherwise,
22479 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22480 declarator and we can print more accurate diagnostics. */
22482 static bool
22483 cp_parser_check_template_parameters (cp_parser* parser,
22484 unsigned num_templates,
22485 location_t location,
22486 cp_declarator *declarator)
22488 /* If there are the same number of template classes and parameter
22489 lists, that's OK. */
22490 if (parser->num_template_parameter_lists == num_templates)
22491 return true;
22492 /* If there are more, but only one more, then we are referring to a
22493 member template. That's OK too. */
22494 if (parser->num_template_parameter_lists == num_templates + 1)
22495 return true;
22496 /* If there are more template classes than parameter lists, we have
22497 something like:
22499 template <class T> void S<T>::R<T>::f (); */
22500 if (parser->num_template_parameter_lists < num_templates)
22502 if (declarator && !current_function_decl)
22503 error_at (location, "specializing member %<%T::%E%> "
22504 "requires %<template<>%> syntax",
22505 declarator->u.id.qualifying_scope,
22506 declarator->u.id.unqualified_name);
22507 else if (declarator)
22508 error_at (location, "invalid declaration of %<%T::%E%>",
22509 declarator->u.id.qualifying_scope,
22510 declarator->u.id.unqualified_name);
22511 else
22512 error_at (location, "too few template-parameter-lists");
22513 return false;
22515 /* Otherwise, there are too many template parameter lists. We have
22516 something like:
22518 template <class T> template <class U> void S::f(); */
22519 error_at (location, "too many template-parameter-lists");
22520 return false;
22523 /* Parse an optional `::' token indicating that the following name is
22524 from the global namespace. If so, PARSER->SCOPE is set to the
22525 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22526 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22527 Returns the new value of PARSER->SCOPE, if the `::' token is
22528 present, and NULL_TREE otherwise. */
22530 static tree
22531 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22533 cp_token *token;
22535 /* Peek at the next token. */
22536 token = cp_lexer_peek_token (parser->lexer);
22537 /* If we're looking at a `::' token then we're starting from the
22538 global namespace, not our current location. */
22539 if (token->type == CPP_SCOPE)
22541 /* Consume the `::' token. */
22542 cp_lexer_consume_token (parser->lexer);
22543 /* Set the SCOPE so that we know where to start the lookup. */
22544 parser->scope = global_namespace;
22545 parser->qualifying_scope = global_namespace;
22546 parser->object_scope = NULL_TREE;
22548 return parser->scope;
22550 else if (!current_scope_valid_p)
22552 parser->scope = NULL_TREE;
22553 parser->qualifying_scope = NULL_TREE;
22554 parser->object_scope = NULL_TREE;
22557 return NULL_TREE;
22560 /* Returns TRUE if the upcoming token sequence is the start of a
22561 constructor declarator. If FRIEND_P is true, the declarator is
22562 preceded by the `friend' specifier. */
22564 static bool
22565 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22567 bool constructor_p;
22568 bool outside_class_specifier_p;
22569 tree nested_name_specifier;
22570 cp_token *next_token;
22572 /* The common case is that this is not a constructor declarator, so
22573 try to avoid doing lots of work if at all possible. It's not
22574 valid declare a constructor at function scope. */
22575 if (parser->in_function_body)
22576 return false;
22577 /* And only certain tokens can begin a constructor declarator. */
22578 next_token = cp_lexer_peek_token (parser->lexer);
22579 if (next_token->type != CPP_NAME
22580 && next_token->type != CPP_SCOPE
22581 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22582 && next_token->type != CPP_TEMPLATE_ID)
22583 return false;
22585 /* Parse tentatively; we are going to roll back all of the tokens
22586 consumed here. */
22587 cp_parser_parse_tentatively (parser);
22588 /* Assume that we are looking at a constructor declarator. */
22589 constructor_p = true;
22591 /* Look for the optional `::' operator. */
22592 cp_parser_global_scope_opt (parser,
22593 /*current_scope_valid_p=*/false);
22594 /* Look for the nested-name-specifier. */
22595 nested_name_specifier
22596 = (cp_parser_nested_name_specifier_opt (parser,
22597 /*typename_keyword_p=*/false,
22598 /*check_dependency_p=*/false,
22599 /*type_p=*/false,
22600 /*is_declaration=*/false));
22602 outside_class_specifier_p = (!at_class_scope_p ()
22603 || !TYPE_BEING_DEFINED (current_class_type)
22604 || friend_p);
22606 /* Outside of a class-specifier, there must be a
22607 nested-name-specifier. */
22608 if (!nested_name_specifier && outside_class_specifier_p)
22609 constructor_p = false;
22610 else if (nested_name_specifier == error_mark_node)
22611 constructor_p = false;
22613 /* If we have a class scope, this is easy; DR 147 says that S::S always
22614 names the constructor, and no other qualified name could. */
22615 if (constructor_p && nested_name_specifier
22616 && CLASS_TYPE_P (nested_name_specifier))
22618 tree id = cp_parser_unqualified_id (parser,
22619 /*template_keyword_p=*/false,
22620 /*check_dependency_p=*/false,
22621 /*declarator_p=*/true,
22622 /*optional_p=*/false);
22623 if (is_overloaded_fn (id))
22624 id = DECL_NAME (get_first_fn (id));
22625 if (!constructor_name_p (id, nested_name_specifier))
22626 constructor_p = false;
22628 /* If we still think that this might be a constructor-declarator,
22629 look for a class-name. */
22630 else if (constructor_p)
22632 /* If we have:
22634 template <typename T> struct S {
22635 S();
22638 we must recognize that the nested `S' names a class. */
22639 tree type_decl;
22640 type_decl = cp_parser_class_name (parser,
22641 /*typename_keyword_p=*/false,
22642 /*template_keyword_p=*/false,
22643 none_type,
22644 /*check_dependency_p=*/false,
22645 /*class_head_p=*/false,
22646 /*is_declaration=*/false);
22647 /* If there was no class-name, then this is not a constructor.
22648 Otherwise, if we are in a class-specifier and we aren't
22649 handling a friend declaration, check that its type matches
22650 current_class_type (c++/38313). Note: error_mark_node
22651 is left alone for error recovery purposes. */
22652 constructor_p = (!cp_parser_error_occurred (parser)
22653 && (outside_class_specifier_p
22654 || type_decl == error_mark_node
22655 || same_type_p (current_class_type,
22656 TREE_TYPE (type_decl))));
22658 /* If we're still considering a constructor, we have to see a `(',
22659 to begin the parameter-declaration-clause, followed by either a
22660 `)', an `...', or a decl-specifier. We need to check for a
22661 type-specifier to avoid being fooled into thinking that:
22663 S (f) (int);
22665 is a constructor. (It is actually a function named `f' that
22666 takes one parameter (of type `int') and returns a value of type
22667 `S'. */
22668 if (constructor_p
22669 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22670 constructor_p = false;
22672 if (constructor_p
22673 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22674 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22675 /* A parameter declaration begins with a decl-specifier,
22676 which is either the "attribute" keyword, a storage class
22677 specifier, or (usually) a type-specifier. */
22678 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22680 tree type;
22681 tree pushed_scope = NULL_TREE;
22682 unsigned saved_num_template_parameter_lists;
22684 /* Names appearing in the type-specifier should be looked up
22685 in the scope of the class. */
22686 if (current_class_type)
22687 type = NULL_TREE;
22688 else
22690 type = TREE_TYPE (type_decl);
22691 if (TREE_CODE (type) == TYPENAME_TYPE)
22693 type = resolve_typename_type (type,
22694 /*only_current_p=*/false);
22695 if (TREE_CODE (type) == TYPENAME_TYPE)
22697 cp_parser_abort_tentative_parse (parser);
22698 return false;
22701 pushed_scope = push_scope (type);
22704 /* Inside the constructor parameter list, surrounding
22705 template-parameter-lists do not apply. */
22706 saved_num_template_parameter_lists
22707 = parser->num_template_parameter_lists;
22708 parser->num_template_parameter_lists = 0;
22710 /* Look for the type-specifier. */
22711 cp_parser_type_specifier (parser,
22712 CP_PARSER_FLAGS_NONE,
22713 /*decl_specs=*/NULL,
22714 /*is_declarator=*/true,
22715 /*declares_class_or_enum=*/NULL,
22716 /*is_cv_qualifier=*/NULL);
22718 parser->num_template_parameter_lists
22719 = saved_num_template_parameter_lists;
22721 /* Leave the scope of the class. */
22722 if (pushed_scope)
22723 pop_scope (pushed_scope);
22725 constructor_p = !cp_parser_error_occurred (parser);
22729 /* We did not really want to consume any tokens. */
22730 cp_parser_abort_tentative_parse (parser);
22732 return constructor_p;
22735 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22736 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22737 they must be performed once we are in the scope of the function.
22739 Returns the function defined. */
22741 static tree
22742 cp_parser_function_definition_from_specifiers_and_declarator
22743 (cp_parser* parser,
22744 cp_decl_specifier_seq *decl_specifiers,
22745 tree attributes,
22746 const cp_declarator *declarator)
22748 tree fn;
22749 bool success_p;
22751 /* Begin the function-definition. */
22752 success_p = start_function (decl_specifiers, declarator, attributes);
22754 /* The things we're about to see are not directly qualified by any
22755 template headers we've seen thus far. */
22756 reset_specialization ();
22758 /* If there were names looked up in the decl-specifier-seq that we
22759 did not check, check them now. We must wait until we are in the
22760 scope of the function to perform the checks, since the function
22761 might be a friend. */
22762 perform_deferred_access_checks (tf_warning_or_error);
22764 if (success_p)
22766 cp_finalize_omp_declare_simd (parser, current_function_decl);
22767 parser->omp_declare_simd = NULL;
22770 if (!success_p)
22772 /* Skip the entire function. */
22773 cp_parser_skip_to_end_of_block_or_statement (parser);
22774 fn = error_mark_node;
22776 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22778 /* Seen already, skip it. An error message has already been output. */
22779 cp_parser_skip_to_end_of_block_or_statement (parser);
22780 fn = current_function_decl;
22781 current_function_decl = NULL_TREE;
22782 /* If this is a function from a class, pop the nested class. */
22783 if (current_class_name)
22784 pop_nested_class ();
22786 else
22788 timevar_id_t tv;
22789 if (DECL_DECLARED_INLINE_P (current_function_decl))
22790 tv = TV_PARSE_INLINE;
22791 else
22792 tv = TV_PARSE_FUNC;
22793 timevar_push (tv);
22794 fn = cp_parser_function_definition_after_declarator (parser,
22795 /*inline_p=*/false);
22796 timevar_pop (tv);
22799 return fn;
22802 /* Parse the part of a function-definition that follows the
22803 declarator. INLINE_P is TRUE iff this function is an inline
22804 function defined within a class-specifier.
22806 Returns the function defined. */
22808 static tree
22809 cp_parser_function_definition_after_declarator (cp_parser* parser,
22810 bool inline_p)
22812 tree fn;
22813 bool ctor_initializer_p = false;
22814 bool saved_in_unbraced_linkage_specification_p;
22815 bool saved_in_function_body;
22816 unsigned saved_num_template_parameter_lists;
22817 cp_token *token;
22818 bool fully_implicit_function_template_p
22819 = parser->fully_implicit_function_template_p;
22820 parser->fully_implicit_function_template_p = false;
22821 tree implicit_template_parms
22822 = parser->implicit_template_parms;
22823 parser->implicit_template_parms = 0;
22824 cp_binding_level* implicit_template_scope
22825 = parser->implicit_template_scope;
22826 parser->implicit_template_scope = 0;
22828 saved_in_function_body = parser->in_function_body;
22829 parser->in_function_body = true;
22830 /* If the next token is `return', then the code may be trying to
22831 make use of the "named return value" extension that G++ used to
22832 support. */
22833 token = cp_lexer_peek_token (parser->lexer);
22834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22836 /* Consume the `return' keyword. */
22837 cp_lexer_consume_token (parser->lexer);
22838 /* Look for the identifier that indicates what value is to be
22839 returned. */
22840 cp_parser_identifier (parser);
22841 /* Issue an error message. */
22842 error_at (token->location,
22843 "named return values are no longer supported");
22844 /* Skip tokens until we reach the start of the function body. */
22845 while (true)
22847 cp_token *token = cp_lexer_peek_token (parser->lexer);
22848 if (token->type == CPP_OPEN_BRACE
22849 || token->type == CPP_EOF
22850 || token->type == CPP_PRAGMA_EOL)
22851 break;
22852 cp_lexer_consume_token (parser->lexer);
22855 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22856 anything declared inside `f'. */
22857 saved_in_unbraced_linkage_specification_p
22858 = parser->in_unbraced_linkage_specification_p;
22859 parser->in_unbraced_linkage_specification_p = false;
22860 /* Inside the function, surrounding template-parameter-lists do not
22861 apply. */
22862 saved_num_template_parameter_lists
22863 = parser->num_template_parameter_lists;
22864 parser->num_template_parameter_lists = 0;
22866 start_lambda_scope (current_function_decl);
22868 /* If the next token is `try', `__transaction_atomic', or
22869 `__transaction_relaxed`, then we are looking at either function-try-block
22870 or function-transaction-block. Note that all of these include the
22871 function-body. */
22872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22873 ctor_initializer_p = cp_parser_function_transaction (parser,
22874 RID_TRANSACTION_ATOMIC);
22875 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22876 RID_TRANSACTION_RELAXED))
22877 ctor_initializer_p = cp_parser_function_transaction (parser,
22878 RID_TRANSACTION_RELAXED);
22879 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22880 ctor_initializer_p = cp_parser_function_try_block (parser);
22881 else
22882 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22883 (parser, /*in_function_try_block=*/false);
22885 finish_lambda_scope ();
22887 /* Finish the function. */
22888 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22889 (inline_p ? 2 : 0));
22890 /* Generate code for it, if necessary. */
22891 expand_or_defer_fn (fn);
22892 /* Restore the saved values. */
22893 parser->in_unbraced_linkage_specification_p
22894 = saved_in_unbraced_linkage_specification_p;
22895 parser->num_template_parameter_lists
22896 = saved_num_template_parameter_lists;
22897 parser->in_function_body = saved_in_function_body;
22899 parser->fully_implicit_function_template_p
22900 = fully_implicit_function_template_p;
22901 parser->implicit_template_parms
22902 = implicit_template_parms;
22903 parser->implicit_template_scope
22904 = implicit_template_scope;
22906 if (parser->fully_implicit_function_template_p)
22907 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22909 return fn;
22912 /* Parse a template-declaration, assuming that the `export' (and
22913 `extern') keywords, if present, has already been scanned. MEMBER_P
22914 is as for cp_parser_template_declaration. */
22916 static void
22917 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22919 tree decl = NULL_TREE;
22920 vec<deferred_access_check, va_gc> *checks;
22921 tree parameter_list;
22922 bool friend_p = false;
22923 bool need_lang_pop;
22924 cp_token *token;
22926 /* Look for the `template' keyword. */
22927 token = cp_lexer_peek_token (parser->lexer);
22928 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22929 return;
22931 /* And the `<'. */
22932 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22933 return;
22934 if (at_class_scope_p () && current_function_decl)
22936 /* 14.5.2.2 [temp.mem]
22938 A local class shall not have member templates. */
22939 error_at (token->location,
22940 "invalid declaration of member template in local class");
22941 cp_parser_skip_to_end_of_block_or_statement (parser);
22942 return;
22944 /* [temp]
22946 A template ... shall not have C linkage. */
22947 if (current_lang_name == lang_name_c)
22949 error_at (token->location, "template with C linkage");
22950 /* Give it C++ linkage to avoid confusing other parts of the
22951 front end. */
22952 push_lang_context (lang_name_cplusplus);
22953 need_lang_pop = true;
22955 else
22956 need_lang_pop = false;
22958 /* We cannot perform access checks on the template parameter
22959 declarations until we know what is being declared, just as we
22960 cannot check the decl-specifier list. */
22961 push_deferring_access_checks (dk_deferred);
22963 /* If the next token is `>', then we have an invalid
22964 specialization. Rather than complain about an invalid template
22965 parameter, issue an error message here. */
22966 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22968 cp_parser_error (parser, "invalid explicit specialization");
22969 begin_specialization ();
22970 parameter_list = NULL_TREE;
22972 else
22974 /* Parse the template parameters. */
22975 parameter_list = cp_parser_template_parameter_list (parser);
22978 /* Get the deferred access checks from the parameter list. These
22979 will be checked once we know what is being declared, as for a
22980 member template the checks must be performed in the scope of the
22981 class containing the member. */
22982 checks = get_deferred_access_checks ();
22984 /* Look for the `>'. */
22985 cp_parser_skip_to_end_of_template_parameter_list (parser);
22986 /* We just processed one more parameter list. */
22987 ++parser->num_template_parameter_lists;
22988 /* If the next token is `template', there are more template
22989 parameters. */
22990 if (cp_lexer_next_token_is_keyword (parser->lexer,
22991 RID_TEMPLATE))
22992 cp_parser_template_declaration_after_export (parser, member_p);
22993 else if (cxx_dialect >= cxx11
22994 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22995 decl = cp_parser_alias_declaration (parser);
22996 else
22998 /* There are no access checks when parsing a template, as we do not
22999 know if a specialization will be a friend. */
23000 push_deferring_access_checks (dk_no_check);
23001 token = cp_lexer_peek_token (parser->lexer);
23002 decl = cp_parser_single_declaration (parser,
23003 checks,
23004 member_p,
23005 /*explicit_specialization_p=*/false,
23006 &friend_p);
23007 pop_deferring_access_checks ();
23009 /* If this is a member template declaration, let the front
23010 end know. */
23011 if (member_p && !friend_p && decl)
23013 if (TREE_CODE (decl) == TYPE_DECL)
23014 cp_parser_check_access_in_redeclaration (decl, token->location);
23016 decl = finish_member_template_decl (decl);
23018 else if (friend_p && decl
23019 && DECL_DECLARES_TYPE_P (decl))
23020 make_friend_class (current_class_type, TREE_TYPE (decl),
23021 /*complain=*/true);
23023 /* We are done with the current parameter list. */
23024 --parser->num_template_parameter_lists;
23026 pop_deferring_access_checks ();
23028 /* Finish up. */
23029 finish_template_decl (parameter_list);
23031 /* Check the template arguments for a literal operator template. */
23032 if (decl
23033 && DECL_DECLARES_FUNCTION_P (decl)
23034 && UDLIT_OPER_P (DECL_NAME (decl)))
23036 bool ok = true;
23037 if (parameter_list == NULL_TREE)
23038 ok = false;
23039 else
23041 int num_parms = TREE_VEC_LENGTH (parameter_list);
23042 if (num_parms == 1)
23044 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23045 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23046 if (TREE_TYPE (parm) != char_type_node
23047 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23048 ok = false;
23050 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23052 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23053 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23054 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23055 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23056 if (TREE_TYPE (parm) != TREE_TYPE (type)
23057 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23058 ok = false;
23060 else
23061 ok = false;
23063 if (!ok)
23064 error ("literal operator template %qD has invalid parameter list."
23065 " Expected non-type template argument pack <char...>"
23066 " or <typename CharT, CharT...>",
23067 decl);
23069 /* Register member declarations. */
23070 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23071 finish_member_declaration (decl);
23072 /* For the erroneous case of a template with C linkage, we pushed an
23073 implicit C++ linkage scope; exit that scope now. */
23074 if (need_lang_pop)
23075 pop_lang_context ();
23076 /* If DECL is a function template, we must return to parse it later.
23077 (Even though there is no definition, there might be default
23078 arguments that need handling.) */
23079 if (member_p && decl
23080 && DECL_DECLARES_FUNCTION_P (decl))
23081 vec_safe_push (unparsed_funs_with_definitions, decl);
23084 /* Perform the deferred access checks from a template-parameter-list.
23085 CHECKS is a TREE_LIST of access checks, as returned by
23086 get_deferred_access_checks. */
23088 static void
23089 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23091 ++processing_template_parmlist;
23092 perform_access_checks (checks, tf_warning_or_error);
23093 --processing_template_parmlist;
23096 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23097 `function-definition' sequence that follows a template header.
23098 If MEMBER_P is true, this declaration appears in a class scope.
23100 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23101 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23103 static tree
23104 cp_parser_single_declaration (cp_parser* parser,
23105 vec<deferred_access_check, va_gc> *checks,
23106 bool member_p,
23107 bool explicit_specialization_p,
23108 bool* friend_p)
23110 int declares_class_or_enum;
23111 tree decl = NULL_TREE;
23112 cp_decl_specifier_seq decl_specifiers;
23113 bool function_definition_p = false;
23114 cp_token *decl_spec_token_start;
23116 /* This function is only used when processing a template
23117 declaration. */
23118 gcc_assert (innermost_scope_kind () == sk_template_parms
23119 || innermost_scope_kind () == sk_template_spec);
23121 /* Defer access checks until we know what is being declared. */
23122 push_deferring_access_checks (dk_deferred);
23124 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23125 alternative. */
23126 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23127 cp_parser_decl_specifier_seq (parser,
23128 CP_PARSER_FLAGS_OPTIONAL,
23129 &decl_specifiers,
23130 &declares_class_or_enum);
23131 if (friend_p)
23132 *friend_p = cp_parser_friend_p (&decl_specifiers);
23134 /* There are no template typedefs. */
23135 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23137 error_at (decl_spec_token_start->location,
23138 "template declaration of %<typedef%>");
23139 decl = error_mark_node;
23142 /* Gather up the access checks that occurred the
23143 decl-specifier-seq. */
23144 stop_deferring_access_checks ();
23146 /* Check for the declaration of a template class. */
23147 if (declares_class_or_enum)
23149 if (cp_parser_declares_only_class_p (parser))
23151 decl = shadow_tag (&decl_specifiers);
23153 /* In this case:
23155 struct C {
23156 friend template <typename T> struct A<T>::B;
23159 A<T>::B will be represented by a TYPENAME_TYPE, and
23160 therefore not recognized by shadow_tag. */
23161 if (friend_p && *friend_p
23162 && !decl
23163 && decl_specifiers.type
23164 && TYPE_P (decl_specifiers.type))
23165 decl = decl_specifiers.type;
23167 if (decl && decl != error_mark_node)
23168 decl = TYPE_NAME (decl);
23169 else
23170 decl = error_mark_node;
23172 /* Perform access checks for template parameters. */
23173 cp_parser_perform_template_parameter_access_checks (checks);
23177 /* Complain about missing 'typename' or other invalid type names. */
23178 if (!decl_specifiers.any_type_specifiers_p
23179 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23181 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23182 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23183 the rest of this declaration. */
23184 decl = error_mark_node;
23185 goto out;
23188 /* If it's not a template class, try for a template function. If
23189 the next token is a `;', then this declaration does not declare
23190 anything. But, if there were errors in the decl-specifiers, then
23191 the error might well have come from an attempted class-specifier.
23192 In that case, there's no need to warn about a missing declarator. */
23193 if (!decl
23194 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23195 || decl_specifiers.type != error_mark_node))
23197 decl = cp_parser_init_declarator (parser,
23198 &decl_specifiers,
23199 checks,
23200 /*function_definition_allowed_p=*/true,
23201 member_p,
23202 declares_class_or_enum,
23203 &function_definition_p,
23204 NULL);
23206 /* 7.1.1-1 [dcl.stc]
23208 A storage-class-specifier shall not be specified in an explicit
23209 specialization... */
23210 if (decl
23211 && explicit_specialization_p
23212 && decl_specifiers.storage_class != sc_none)
23214 error_at (decl_spec_token_start->location,
23215 "explicit template specialization cannot have a storage class");
23216 decl = error_mark_node;
23219 if (decl && VAR_P (decl))
23220 check_template_variable (decl);
23223 /* Look for a trailing `;' after the declaration. */
23224 if (!function_definition_p
23225 && (decl == error_mark_node
23226 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23227 cp_parser_skip_to_end_of_block_or_statement (parser);
23229 out:
23230 pop_deferring_access_checks ();
23232 /* Clear any current qualification; whatever comes next is the start
23233 of something new. */
23234 parser->scope = NULL_TREE;
23235 parser->qualifying_scope = NULL_TREE;
23236 parser->object_scope = NULL_TREE;
23238 return decl;
23241 /* Parse a cast-expression that is not the operand of a unary "&". */
23243 static tree
23244 cp_parser_simple_cast_expression (cp_parser *parser)
23246 return cp_parser_cast_expression (parser, /*address_p=*/false,
23247 /*cast_p=*/false, /*decltype*/false, NULL);
23250 /* Parse a functional cast to TYPE. Returns an expression
23251 representing the cast. */
23253 static tree
23254 cp_parser_functional_cast (cp_parser* parser, tree type)
23256 vec<tree, va_gc> *vec;
23257 tree expression_list;
23258 tree cast;
23259 bool nonconst_p;
23261 if (!type)
23262 type = error_mark_node;
23264 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23266 cp_lexer_set_source_position (parser->lexer);
23267 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23268 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23269 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23270 if (TREE_CODE (type) == TYPE_DECL)
23271 type = TREE_TYPE (type);
23272 return finish_compound_literal (type, expression_list,
23273 tf_warning_or_error);
23277 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23278 /*cast_p=*/true,
23279 /*allow_expansion_p=*/true,
23280 /*non_constant_p=*/NULL);
23281 if (vec == NULL)
23282 expression_list = error_mark_node;
23283 else
23285 expression_list = build_tree_list_vec (vec);
23286 release_tree_vector (vec);
23289 cast = build_functional_cast (type, expression_list,
23290 tf_warning_or_error);
23291 /* [expr.const]/1: In an integral constant expression "only type
23292 conversions to integral or enumeration type can be used". */
23293 if (TREE_CODE (type) == TYPE_DECL)
23294 type = TREE_TYPE (type);
23295 if (cast != error_mark_node
23296 && !cast_valid_in_integral_constant_expression_p (type)
23297 && cp_parser_non_integral_constant_expression (parser,
23298 NIC_CONSTRUCTOR))
23299 return error_mark_node;
23300 return cast;
23303 /* Save the tokens that make up the body of a member function defined
23304 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23305 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23306 specifiers applied to the declaration. Returns the FUNCTION_DECL
23307 for the member function. */
23309 static tree
23310 cp_parser_save_member_function_body (cp_parser* parser,
23311 cp_decl_specifier_seq *decl_specifiers,
23312 cp_declarator *declarator,
23313 tree attributes)
23315 cp_token *first;
23316 cp_token *last;
23317 tree fn;
23319 /* Create the FUNCTION_DECL. */
23320 fn = grokmethod (decl_specifiers, declarator, attributes);
23321 cp_finalize_omp_declare_simd (parser, fn);
23322 /* If something went badly wrong, bail out now. */
23323 if (fn == error_mark_node)
23325 /* If there's a function-body, skip it. */
23326 if (cp_parser_token_starts_function_definition_p
23327 (cp_lexer_peek_token (parser->lexer)))
23328 cp_parser_skip_to_end_of_block_or_statement (parser);
23329 return error_mark_node;
23332 /* Remember it, if there default args to post process. */
23333 cp_parser_save_default_args (parser, fn);
23335 /* Save away the tokens that make up the body of the
23336 function. */
23337 first = parser->lexer->next_token;
23338 /* Handle function try blocks. */
23339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23340 cp_lexer_consume_token (parser->lexer);
23341 /* We can have braced-init-list mem-initializers before the fn body. */
23342 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23344 cp_lexer_consume_token (parser->lexer);
23345 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23347 /* cache_group will stop after an un-nested { } pair, too. */
23348 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23349 break;
23351 /* variadic mem-inits have ... after the ')'. */
23352 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23353 cp_lexer_consume_token (parser->lexer);
23356 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23357 /* Handle function try blocks. */
23358 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23359 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23360 last = parser->lexer->next_token;
23362 /* Save away the inline definition; we will process it when the
23363 class is complete. */
23364 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23365 DECL_PENDING_INLINE_P (fn) = 1;
23367 /* We need to know that this was defined in the class, so that
23368 friend templates are handled correctly. */
23369 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23371 /* Add FN to the queue of functions to be parsed later. */
23372 vec_safe_push (unparsed_funs_with_definitions, fn);
23374 return fn;
23377 /* Save the tokens that make up the in-class initializer for a non-static
23378 data member. Returns a DEFAULT_ARG. */
23380 static tree
23381 cp_parser_save_nsdmi (cp_parser* parser)
23383 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23386 /* Parse a template-argument-list, as well as the trailing ">" (but
23387 not the opening "<"). See cp_parser_template_argument_list for the
23388 return value. */
23390 static tree
23391 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23393 tree arguments;
23394 tree saved_scope;
23395 tree saved_qualifying_scope;
23396 tree saved_object_scope;
23397 bool saved_greater_than_is_operator_p;
23398 int saved_unevaluated_operand;
23399 int saved_inhibit_evaluation_warnings;
23401 /* [temp.names]
23403 When parsing a template-id, the first non-nested `>' is taken as
23404 the end of the template-argument-list rather than a greater-than
23405 operator. */
23406 saved_greater_than_is_operator_p
23407 = parser->greater_than_is_operator_p;
23408 parser->greater_than_is_operator_p = false;
23409 /* Parsing the argument list may modify SCOPE, so we save it
23410 here. */
23411 saved_scope = parser->scope;
23412 saved_qualifying_scope = parser->qualifying_scope;
23413 saved_object_scope = parser->object_scope;
23414 /* We need to evaluate the template arguments, even though this
23415 template-id may be nested within a "sizeof". */
23416 saved_unevaluated_operand = cp_unevaluated_operand;
23417 cp_unevaluated_operand = 0;
23418 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23419 c_inhibit_evaluation_warnings = 0;
23420 /* Parse the template-argument-list itself. */
23421 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23422 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23423 arguments = NULL_TREE;
23424 else
23425 arguments = cp_parser_template_argument_list (parser);
23426 /* Look for the `>' that ends the template-argument-list. If we find
23427 a '>>' instead, it's probably just a typo. */
23428 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23430 if (cxx_dialect != cxx98)
23432 /* In C++0x, a `>>' in a template argument list or cast
23433 expression is considered to be two separate `>'
23434 tokens. So, change the current token to a `>', but don't
23435 consume it: it will be consumed later when the outer
23436 template argument list (or cast expression) is parsed.
23437 Note that this replacement of `>' for `>>' is necessary
23438 even if we are parsing tentatively: in the tentative
23439 case, after calling
23440 cp_parser_enclosed_template_argument_list we will always
23441 throw away all of the template arguments and the first
23442 closing `>', either because the template argument list
23443 was erroneous or because we are replacing those tokens
23444 with a CPP_TEMPLATE_ID token. The second `>' (which will
23445 not have been thrown away) is needed either to close an
23446 outer template argument list or to complete a new-style
23447 cast. */
23448 cp_token *token = cp_lexer_peek_token (parser->lexer);
23449 token->type = CPP_GREATER;
23451 else if (!saved_greater_than_is_operator_p)
23453 /* If we're in a nested template argument list, the '>>' has
23454 to be a typo for '> >'. We emit the error message, but we
23455 continue parsing and we push a '>' as next token, so that
23456 the argument list will be parsed correctly. Note that the
23457 global source location is still on the token before the
23458 '>>', so we need to say explicitly where we want it. */
23459 cp_token *token = cp_lexer_peek_token (parser->lexer);
23460 error_at (token->location, "%<>>%> should be %<> >%> "
23461 "within a nested template argument list");
23463 token->type = CPP_GREATER;
23465 else
23467 /* If this is not a nested template argument list, the '>>'
23468 is a typo for '>'. Emit an error message and continue.
23469 Same deal about the token location, but here we can get it
23470 right by consuming the '>>' before issuing the diagnostic. */
23471 cp_token *token = cp_lexer_consume_token (parser->lexer);
23472 error_at (token->location,
23473 "spurious %<>>%>, use %<>%> to terminate "
23474 "a template argument list");
23477 else
23478 cp_parser_skip_to_end_of_template_parameter_list (parser);
23479 /* The `>' token might be a greater-than operator again now. */
23480 parser->greater_than_is_operator_p
23481 = saved_greater_than_is_operator_p;
23482 /* Restore the SAVED_SCOPE. */
23483 parser->scope = saved_scope;
23484 parser->qualifying_scope = saved_qualifying_scope;
23485 parser->object_scope = saved_object_scope;
23486 cp_unevaluated_operand = saved_unevaluated_operand;
23487 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23489 return arguments;
23492 /* MEMBER_FUNCTION is a member function, or a friend. If default
23493 arguments, or the body of the function have not yet been parsed,
23494 parse them now. */
23496 static void
23497 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23499 timevar_push (TV_PARSE_INMETH);
23500 /* If this member is a template, get the underlying
23501 FUNCTION_DECL. */
23502 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23503 member_function = DECL_TEMPLATE_RESULT (member_function);
23505 /* There should not be any class definitions in progress at this
23506 point; the bodies of members are only parsed outside of all class
23507 definitions. */
23508 gcc_assert (parser->num_classes_being_defined == 0);
23509 /* While we're parsing the member functions we might encounter more
23510 classes. We want to handle them right away, but we don't want
23511 them getting mixed up with functions that are currently in the
23512 queue. */
23513 push_unparsed_function_queues (parser);
23515 /* Make sure that any template parameters are in scope. */
23516 maybe_begin_member_template_processing (member_function);
23518 /* If the body of the function has not yet been parsed, parse it
23519 now. */
23520 if (DECL_PENDING_INLINE_P (member_function))
23522 tree function_scope;
23523 cp_token_cache *tokens;
23525 /* The function is no longer pending; we are processing it. */
23526 tokens = DECL_PENDING_INLINE_INFO (member_function);
23527 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23528 DECL_PENDING_INLINE_P (member_function) = 0;
23530 /* If this is a local class, enter the scope of the containing
23531 function. */
23532 function_scope = current_function_decl;
23533 if (function_scope)
23534 push_function_context ();
23536 /* Push the body of the function onto the lexer stack. */
23537 cp_parser_push_lexer_for_tokens (parser, tokens);
23539 /* Let the front end know that we going to be defining this
23540 function. */
23541 start_preparsed_function (member_function, NULL_TREE,
23542 SF_PRE_PARSED | SF_INCLASS_INLINE);
23544 /* Don't do access checking if it is a templated function. */
23545 if (processing_template_decl)
23546 push_deferring_access_checks (dk_no_check);
23548 /* #pragma omp declare reduction needs special parsing. */
23549 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23551 parser->lexer->in_pragma = true;
23552 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23553 finish_function (/*inline*/2);
23554 cp_check_omp_declare_reduction (member_function);
23556 else
23557 /* Now, parse the body of the function. */
23558 cp_parser_function_definition_after_declarator (parser,
23559 /*inline_p=*/true);
23561 if (processing_template_decl)
23562 pop_deferring_access_checks ();
23564 /* Leave the scope of the containing function. */
23565 if (function_scope)
23566 pop_function_context ();
23567 cp_parser_pop_lexer (parser);
23570 /* Remove any template parameters from the symbol table. */
23571 maybe_end_member_template_processing ();
23573 /* Restore the queue. */
23574 pop_unparsed_function_queues (parser);
23575 timevar_pop (TV_PARSE_INMETH);
23578 /* If DECL contains any default args, remember it on the unparsed
23579 functions queue. */
23581 static void
23582 cp_parser_save_default_args (cp_parser* parser, tree decl)
23584 tree probe;
23586 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23587 probe;
23588 probe = TREE_CHAIN (probe))
23589 if (TREE_PURPOSE (probe))
23591 cp_default_arg_entry entry = {current_class_type, decl};
23592 vec_safe_push (unparsed_funs_with_default_args, entry);
23593 break;
23597 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23598 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23599 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23600 from the parameter-type-list. */
23602 static tree
23603 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23604 tree default_arg, tree parmtype)
23606 cp_token_cache *tokens;
23607 tree parsed_arg;
23608 bool dummy;
23610 if (default_arg == error_mark_node)
23611 return error_mark_node;
23613 /* Push the saved tokens for the default argument onto the parser's
23614 lexer stack. */
23615 tokens = DEFARG_TOKENS (default_arg);
23616 cp_parser_push_lexer_for_tokens (parser, tokens);
23618 start_lambda_scope (decl);
23620 /* Parse the default argument. */
23621 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23622 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23623 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23625 finish_lambda_scope ();
23627 if (parsed_arg == error_mark_node)
23628 cp_parser_skip_to_end_of_statement (parser);
23630 if (!processing_template_decl)
23632 /* In a non-template class, check conversions now. In a template,
23633 we'll wait and instantiate these as needed. */
23634 if (TREE_CODE (decl) == PARM_DECL)
23635 parsed_arg = check_default_argument (parmtype, parsed_arg,
23636 tf_warning_or_error);
23637 else
23639 int flags = LOOKUP_IMPLICIT;
23640 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23641 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23642 flags = LOOKUP_NORMAL;
23643 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23644 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23645 /* This represents the whole initialization. */
23646 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23650 /* If the token stream has not been completely used up, then
23651 there was extra junk after the end of the default
23652 argument. */
23653 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23655 if (TREE_CODE (decl) == PARM_DECL)
23656 cp_parser_error (parser, "expected %<,%>");
23657 else
23658 cp_parser_error (parser, "expected %<;%>");
23661 /* Revert to the main lexer. */
23662 cp_parser_pop_lexer (parser);
23664 return parsed_arg;
23667 /* FIELD is a non-static data member with an initializer which we saved for
23668 later; parse it now. */
23670 static void
23671 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23673 tree def;
23675 maybe_begin_member_template_processing (field);
23677 push_unparsed_function_queues (parser);
23678 def = cp_parser_late_parse_one_default_arg (parser, field,
23679 DECL_INITIAL (field),
23680 NULL_TREE);
23681 pop_unparsed_function_queues (parser);
23683 maybe_end_member_template_processing ();
23685 DECL_INITIAL (field) = def;
23688 /* FN is a FUNCTION_DECL which may contains a parameter with an
23689 unparsed DEFAULT_ARG. Parse the default args now. This function
23690 assumes that the current scope is the scope in which the default
23691 argument should be processed. */
23693 static void
23694 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23696 bool saved_local_variables_forbidden_p;
23697 tree parm, parmdecl;
23699 /* While we're parsing the default args, we might (due to the
23700 statement expression extension) encounter more classes. We want
23701 to handle them right away, but we don't want them getting mixed
23702 up with default args that are currently in the queue. */
23703 push_unparsed_function_queues (parser);
23705 /* Local variable names (and the `this' keyword) may not appear
23706 in a default argument. */
23707 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23708 parser->local_variables_forbidden_p = true;
23710 push_defarg_context (fn);
23712 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23713 parmdecl = DECL_ARGUMENTS (fn);
23714 parm && parm != void_list_node;
23715 parm = TREE_CHAIN (parm),
23716 parmdecl = DECL_CHAIN (parmdecl))
23718 tree default_arg = TREE_PURPOSE (parm);
23719 tree parsed_arg;
23720 vec<tree, va_gc> *insts;
23721 tree copy;
23722 unsigned ix;
23724 if (!default_arg)
23725 continue;
23727 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23728 /* This can happen for a friend declaration for a function
23729 already declared with default arguments. */
23730 continue;
23732 parsed_arg
23733 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23734 default_arg,
23735 TREE_VALUE (parm));
23736 if (parsed_arg == error_mark_node)
23738 continue;
23741 TREE_PURPOSE (parm) = parsed_arg;
23743 /* Update any instantiations we've already created. */
23744 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23745 vec_safe_iterate (insts, ix, &copy); ix++)
23746 TREE_PURPOSE (copy) = parsed_arg;
23749 pop_defarg_context ();
23751 /* Make sure no default arg is missing. */
23752 check_default_args (fn);
23754 /* Restore the state of local_variables_forbidden_p. */
23755 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23757 /* Restore the queue. */
23758 pop_unparsed_function_queues (parser);
23761 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23763 sizeof ... ( identifier )
23765 where the 'sizeof' token has already been consumed. */
23767 static tree
23768 cp_parser_sizeof_pack (cp_parser *parser)
23770 /* Consume the `...'. */
23771 cp_lexer_consume_token (parser->lexer);
23772 maybe_warn_variadic_templates ();
23774 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23775 if (paren)
23776 cp_lexer_consume_token (parser->lexer);
23777 else
23778 permerror (cp_lexer_peek_token (parser->lexer)->location,
23779 "%<sizeof...%> argument must be surrounded by parentheses");
23781 cp_token *token = cp_lexer_peek_token (parser->lexer);
23782 tree name = cp_parser_identifier (parser);
23783 if (name == error_mark_node)
23784 return error_mark_node;
23785 /* The name is not qualified. */
23786 parser->scope = NULL_TREE;
23787 parser->qualifying_scope = NULL_TREE;
23788 parser->object_scope = NULL_TREE;
23789 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23790 if (expr == error_mark_node)
23791 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23792 token->location);
23793 if (TREE_CODE (expr) == TYPE_DECL)
23794 expr = TREE_TYPE (expr);
23795 else if (TREE_CODE (expr) == CONST_DECL)
23796 expr = DECL_INITIAL (expr);
23797 expr = make_pack_expansion (expr);
23799 if (paren)
23800 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23802 return expr;
23805 /* Parse the operand of `sizeof' (or a similar operator). Returns
23806 either a TYPE or an expression, depending on the form of the
23807 input. The KEYWORD indicates which kind of expression we have
23808 encountered. */
23810 static tree
23811 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23813 tree expr = NULL_TREE;
23814 const char *saved_message;
23815 char *tmp;
23816 bool saved_integral_constant_expression_p;
23817 bool saved_non_integral_constant_expression_p;
23819 /* If it's a `...', then we are computing the length of a parameter
23820 pack. */
23821 if (keyword == RID_SIZEOF
23822 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23823 return cp_parser_sizeof_pack (parser);
23825 /* Types cannot be defined in a `sizeof' expression. Save away the
23826 old message. */
23827 saved_message = parser->type_definition_forbidden_message;
23828 /* And create the new one. */
23829 tmp = concat ("types may not be defined in %<",
23830 IDENTIFIER_POINTER (ridpointers[keyword]),
23831 "%> expressions", NULL);
23832 parser->type_definition_forbidden_message = tmp;
23834 /* The restrictions on constant-expressions do not apply inside
23835 sizeof expressions. */
23836 saved_integral_constant_expression_p
23837 = parser->integral_constant_expression_p;
23838 saved_non_integral_constant_expression_p
23839 = parser->non_integral_constant_expression_p;
23840 parser->integral_constant_expression_p = false;
23842 /* Do not actually evaluate the expression. */
23843 ++cp_unevaluated_operand;
23844 ++c_inhibit_evaluation_warnings;
23845 /* If it's a `(', then we might be looking at the type-id
23846 construction. */
23847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23849 tree type = NULL_TREE;
23850 bool compound_literal_p;
23852 /* We can't be sure yet whether we're looking at a type-id or an
23853 expression. */
23854 cp_parser_parse_tentatively (parser);
23855 /* Consume the `('. */
23856 cp_lexer_consume_token (parser->lexer);
23857 /* Note: as a GNU Extension, compound literals are considered
23858 postfix-expressions as they are in C99, so they are valid
23859 arguments to sizeof. See comment in cp_parser_cast_expression
23860 for details. */
23861 cp_lexer_save_tokens (parser->lexer);
23862 /* Skip tokens until the next token is a closing parenthesis.
23863 If we find the closing `)', and the next token is a `{', then
23864 we are looking at a compound-literal. */
23865 compound_literal_p
23866 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23867 /*consume_paren=*/true)
23868 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23869 /* Roll back the tokens we skipped. */
23870 cp_lexer_rollback_tokens (parser->lexer);
23871 /* If we were looking at a compound-literal, simulate an error
23872 so that the call to cp_parser_parse_definitely below will
23873 fail. */
23874 if (compound_literal_p)
23875 cp_parser_simulate_error (parser);
23876 else
23878 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23879 parser->in_type_id_in_expr_p = true;
23880 /* Look for the type-id. */
23881 type = cp_parser_type_id (parser);
23882 /* Look for the closing `)'. */
23883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23884 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23887 /* If all went well, then we're done. */
23888 if (cp_parser_parse_definitely (parser))
23890 cp_decl_specifier_seq decl_specs;
23892 /* Build a trivial decl-specifier-seq. */
23893 clear_decl_specs (&decl_specs);
23894 decl_specs.type = type;
23896 /* Call grokdeclarator to figure out what type this is. */
23897 expr = grokdeclarator (NULL,
23898 &decl_specs,
23899 TYPENAME,
23900 /*initialized=*/0,
23901 /*attrlist=*/NULL);
23905 /* If the type-id production did not work out, then we must be
23906 looking at the unary-expression production. */
23907 if (!expr)
23908 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23909 /*cast_p=*/false, NULL);
23911 /* Go back to evaluating expressions. */
23912 --cp_unevaluated_operand;
23913 --c_inhibit_evaluation_warnings;
23915 /* Free the message we created. */
23916 free (tmp);
23917 /* And restore the old one. */
23918 parser->type_definition_forbidden_message = saved_message;
23919 parser->integral_constant_expression_p
23920 = saved_integral_constant_expression_p;
23921 parser->non_integral_constant_expression_p
23922 = saved_non_integral_constant_expression_p;
23924 return expr;
23927 /* If the current declaration has no declarator, return true. */
23929 static bool
23930 cp_parser_declares_only_class_p (cp_parser *parser)
23932 /* If the next token is a `;' or a `,' then there is no
23933 declarator. */
23934 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23935 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23938 /* Update the DECL_SPECS to reflect the storage class indicated by
23939 KEYWORD. */
23941 static void
23942 cp_parser_set_storage_class (cp_parser *parser,
23943 cp_decl_specifier_seq *decl_specs,
23944 enum rid keyword,
23945 cp_token *token)
23947 cp_storage_class storage_class;
23949 if (parser->in_unbraced_linkage_specification_p)
23951 error_at (token->location, "invalid use of %qD in linkage specification",
23952 ridpointers[keyword]);
23953 return;
23955 else if (decl_specs->storage_class != sc_none)
23957 decl_specs->conflicting_specifiers_p = true;
23958 return;
23961 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23962 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23963 && decl_specs->gnu_thread_keyword_p)
23965 pedwarn (decl_specs->locations[ds_thread], 0,
23966 "%<__thread%> before %qD", ridpointers[keyword]);
23969 switch (keyword)
23971 case RID_AUTO:
23972 storage_class = sc_auto;
23973 break;
23974 case RID_REGISTER:
23975 storage_class = sc_register;
23976 break;
23977 case RID_STATIC:
23978 storage_class = sc_static;
23979 break;
23980 case RID_EXTERN:
23981 storage_class = sc_extern;
23982 break;
23983 case RID_MUTABLE:
23984 storage_class = sc_mutable;
23985 break;
23986 default:
23987 gcc_unreachable ();
23989 decl_specs->storage_class = storage_class;
23990 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
23992 /* A storage class specifier cannot be applied alongside a typedef
23993 specifier. If there is a typedef specifier present then set
23994 conflicting_specifiers_p which will trigger an error later
23995 on in grokdeclarator. */
23996 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
23997 decl_specs->conflicting_specifiers_p = true;
24000 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24001 is true, the type is a class or enum definition. */
24003 static void
24004 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24005 tree type_spec,
24006 cp_token *token,
24007 bool type_definition_p)
24009 decl_specs->any_specifiers_p = true;
24011 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24012 (with, for example, in "typedef int wchar_t;") we remember that
24013 this is what happened. In system headers, we ignore these
24014 declarations so that G++ can work with system headers that are not
24015 C++-safe. */
24016 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24017 && !type_definition_p
24018 && (type_spec == boolean_type_node
24019 || type_spec == char16_type_node
24020 || type_spec == char32_type_node
24021 || type_spec == wchar_type_node)
24022 && (decl_specs->type
24023 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24024 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24025 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24026 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24028 decl_specs->redefined_builtin_type = type_spec;
24029 set_and_check_decl_spec_loc (decl_specs,
24030 ds_redefined_builtin_type_spec,
24031 token);
24032 if (!decl_specs->type)
24034 decl_specs->type = type_spec;
24035 decl_specs->type_definition_p = false;
24036 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24039 else if (decl_specs->type)
24040 decl_specs->multiple_types_p = true;
24041 else
24043 decl_specs->type = type_spec;
24044 decl_specs->type_definition_p = type_definition_p;
24045 decl_specs->redefined_builtin_type = NULL_TREE;
24046 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24050 /* True iff TOKEN is the GNU keyword __thread. */
24052 static bool
24053 token_is__thread (cp_token *token)
24055 gcc_assert (token->keyword == RID_THREAD);
24056 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24059 /* Set the location for a declarator specifier and check if it is
24060 duplicated.
24062 DECL_SPECS is the sequence of declarator specifiers onto which to
24063 set the location.
24065 DS is the single declarator specifier to set which location is to
24066 be set onto the existing sequence of declarators.
24068 LOCATION is the location for the declarator specifier to
24069 consider. */
24071 static void
24072 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24073 cp_decl_spec ds, cp_token *token)
24075 gcc_assert (ds < ds_last);
24077 if (decl_specs == NULL)
24078 return;
24080 source_location location = token->location;
24082 if (decl_specs->locations[ds] == 0)
24084 decl_specs->locations[ds] = location;
24085 if (ds == ds_thread)
24086 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24088 else
24090 if (ds == ds_long)
24092 if (decl_specs->locations[ds_long_long] != 0)
24093 error_at (location,
24094 "%<long long long%> is too long for GCC");
24095 else
24097 decl_specs->locations[ds_long_long] = location;
24098 pedwarn_cxx98 (location,
24099 OPT_Wlong_long,
24100 "ISO C++ 1998 does not support %<long long%>");
24103 else if (ds == ds_thread)
24105 bool gnu = token_is__thread (token);
24106 if (gnu != decl_specs->gnu_thread_keyword_p)
24107 error_at (location,
24108 "both %<__thread%> and %<thread_local%> specified");
24109 else
24110 error_at (location, "duplicate %qD", token->u.value);
24112 else
24114 static const char *const decl_spec_names[] = {
24115 "signed",
24116 "unsigned",
24117 "short",
24118 "long",
24119 "const",
24120 "volatile",
24121 "restrict",
24122 "inline",
24123 "virtual",
24124 "explicit",
24125 "friend",
24126 "typedef",
24127 "using",
24128 "constexpr",
24129 "__complex"
24131 error_at (location,
24132 "duplicate %qs", decl_spec_names[ds]);
24137 /* Return true iff the declarator specifier DS is present in the
24138 sequence of declarator specifiers DECL_SPECS. */
24140 bool
24141 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24142 cp_decl_spec ds)
24144 gcc_assert (ds < ds_last);
24146 if (decl_specs == NULL)
24147 return false;
24149 return decl_specs->locations[ds] != 0;
24152 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24153 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24155 static bool
24156 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24158 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24161 /* Issue an error message indicating that TOKEN_DESC was expected.
24162 If KEYWORD is true, it indicated this function is called by
24163 cp_parser_require_keword and the required token can only be
24164 a indicated keyword. */
24166 static void
24167 cp_parser_required_error (cp_parser *parser,
24168 required_token token_desc,
24169 bool keyword)
24171 switch (token_desc)
24173 case RT_NEW:
24174 cp_parser_error (parser, "expected %<new%>");
24175 return;
24176 case RT_DELETE:
24177 cp_parser_error (parser, "expected %<delete%>");
24178 return;
24179 case RT_RETURN:
24180 cp_parser_error (parser, "expected %<return%>");
24181 return;
24182 case RT_WHILE:
24183 cp_parser_error (parser, "expected %<while%>");
24184 return;
24185 case RT_EXTERN:
24186 cp_parser_error (parser, "expected %<extern%>");
24187 return;
24188 case RT_STATIC_ASSERT:
24189 cp_parser_error (parser, "expected %<static_assert%>");
24190 return;
24191 case RT_DECLTYPE:
24192 cp_parser_error (parser, "expected %<decltype%>");
24193 return;
24194 case RT_OPERATOR:
24195 cp_parser_error (parser, "expected %<operator%>");
24196 return;
24197 case RT_CLASS:
24198 cp_parser_error (parser, "expected %<class%>");
24199 return;
24200 case RT_TEMPLATE:
24201 cp_parser_error (parser, "expected %<template%>");
24202 return;
24203 case RT_NAMESPACE:
24204 cp_parser_error (parser, "expected %<namespace%>");
24205 return;
24206 case RT_USING:
24207 cp_parser_error (parser, "expected %<using%>");
24208 return;
24209 case RT_ASM:
24210 cp_parser_error (parser, "expected %<asm%>");
24211 return;
24212 case RT_TRY:
24213 cp_parser_error (parser, "expected %<try%>");
24214 return;
24215 case RT_CATCH:
24216 cp_parser_error (parser, "expected %<catch%>");
24217 return;
24218 case RT_THROW:
24219 cp_parser_error (parser, "expected %<throw%>");
24220 return;
24221 case RT_LABEL:
24222 cp_parser_error (parser, "expected %<__label__%>");
24223 return;
24224 case RT_AT_TRY:
24225 cp_parser_error (parser, "expected %<@try%>");
24226 return;
24227 case RT_AT_SYNCHRONIZED:
24228 cp_parser_error (parser, "expected %<@synchronized%>");
24229 return;
24230 case RT_AT_THROW:
24231 cp_parser_error (parser, "expected %<@throw%>");
24232 return;
24233 case RT_TRANSACTION_ATOMIC:
24234 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24235 return;
24236 case RT_TRANSACTION_RELAXED:
24237 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24238 return;
24239 default:
24240 break;
24242 if (!keyword)
24244 switch (token_desc)
24246 case RT_SEMICOLON:
24247 cp_parser_error (parser, "expected %<;%>");
24248 return;
24249 case RT_OPEN_PAREN:
24250 cp_parser_error (parser, "expected %<(%>");
24251 return;
24252 case RT_CLOSE_BRACE:
24253 cp_parser_error (parser, "expected %<}%>");
24254 return;
24255 case RT_OPEN_BRACE:
24256 cp_parser_error (parser, "expected %<{%>");
24257 return;
24258 case RT_CLOSE_SQUARE:
24259 cp_parser_error (parser, "expected %<]%>");
24260 return;
24261 case RT_OPEN_SQUARE:
24262 cp_parser_error (parser, "expected %<[%>");
24263 return;
24264 case RT_COMMA:
24265 cp_parser_error (parser, "expected %<,%>");
24266 return;
24267 case RT_SCOPE:
24268 cp_parser_error (parser, "expected %<::%>");
24269 return;
24270 case RT_LESS:
24271 cp_parser_error (parser, "expected %<<%>");
24272 return;
24273 case RT_GREATER:
24274 cp_parser_error (parser, "expected %<>%>");
24275 return;
24276 case RT_EQ:
24277 cp_parser_error (parser, "expected %<=%>");
24278 return;
24279 case RT_ELLIPSIS:
24280 cp_parser_error (parser, "expected %<...%>");
24281 return;
24282 case RT_MULT:
24283 cp_parser_error (parser, "expected %<*%>");
24284 return;
24285 case RT_COMPL:
24286 cp_parser_error (parser, "expected %<~%>");
24287 return;
24288 case RT_COLON:
24289 cp_parser_error (parser, "expected %<:%>");
24290 return;
24291 case RT_COLON_SCOPE:
24292 cp_parser_error (parser, "expected %<:%> or %<::%>");
24293 return;
24294 case RT_CLOSE_PAREN:
24295 cp_parser_error (parser, "expected %<)%>");
24296 return;
24297 case RT_COMMA_CLOSE_PAREN:
24298 cp_parser_error (parser, "expected %<,%> or %<)%>");
24299 return;
24300 case RT_PRAGMA_EOL:
24301 cp_parser_error (parser, "expected end of line");
24302 return;
24303 case RT_NAME:
24304 cp_parser_error (parser, "expected identifier");
24305 return;
24306 case RT_SELECT:
24307 cp_parser_error (parser, "expected selection-statement");
24308 return;
24309 case RT_INTERATION:
24310 cp_parser_error (parser, "expected iteration-statement");
24311 return;
24312 case RT_JUMP:
24313 cp_parser_error (parser, "expected jump-statement");
24314 return;
24315 case RT_CLASS_KEY:
24316 cp_parser_error (parser, "expected class-key");
24317 return;
24318 case RT_CLASS_TYPENAME_TEMPLATE:
24319 cp_parser_error (parser,
24320 "expected %<class%>, %<typename%>, or %<template%>");
24321 return;
24322 default:
24323 gcc_unreachable ();
24326 else
24327 gcc_unreachable ();
24332 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24333 issue an error message indicating that TOKEN_DESC was expected.
24335 Returns the token consumed, if the token had the appropriate type.
24336 Otherwise, returns NULL. */
24338 static cp_token *
24339 cp_parser_require (cp_parser* parser,
24340 enum cpp_ttype type,
24341 required_token token_desc)
24343 if (cp_lexer_next_token_is (parser->lexer, type))
24344 return cp_lexer_consume_token (parser->lexer);
24345 else
24347 /* Output the MESSAGE -- unless we're parsing tentatively. */
24348 if (!cp_parser_simulate_error (parser))
24349 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24350 return NULL;
24354 /* An error message is produced if the next token is not '>'.
24355 All further tokens are skipped until the desired token is
24356 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24358 static void
24359 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24361 /* Current level of '< ... >'. */
24362 unsigned level = 0;
24363 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24364 unsigned nesting_depth = 0;
24366 /* Are we ready, yet? If not, issue error message. */
24367 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24368 return;
24370 /* Skip tokens until the desired token is found. */
24371 while (true)
24373 /* Peek at the next token. */
24374 switch (cp_lexer_peek_token (parser->lexer)->type)
24376 case CPP_LESS:
24377 if (!nesting_depth)
24378 ++level;
24379 break;
24381 case CPP_RSHIFT:
24382 if (cxx_dialect == cxx98)
24383 /* C++0x views the `>>' operator as two `>' tokens, but
24384 C++98 does not. */
24385 break;
24386 else if (!nesting_depth && level-- == 0)
24388 /* We've hit a `>>' where the first `>' closes the
24389 template argument list, and the second `>' is
24390 spurious. Just consume the `>>' and stop; we've
24391 already produced at least one error. */
24392 cp_lexer_consume_token (parser->lexer);
24393 return;
24395 /* Fall through for C++0x, so we handle the second `>' in
24396 the `>>'. */
24398 case CPP_GREATER:
24399 if (!nesting_depth && level-- == 0)
24401 /* We've reached the token we want, consume it and stop. */
24402 cp_lexer_consume_token (parser->lexer);
24403 return;
24405 break;
24407 case CPP_OPEN_PAREN:
24408 case CPP_OPEN_SQUARE:
24409 ++nesting_depth;
24410 break;
24412 case CPP_CLOSE_PAREN:
24413 case CPP_CLOSE_SQUARE:
24414 if (nesting_depth-- == 0)
24415 return;
24416 break;
24418 case CPP_EOF:
24419 case CPP_PRAGMA_EOL:
24420 case CPP_SEMICOLON:
24421 case CPP_OPEN_BRACE:
24422 case CPP_CLOSE_BRACE:
24423 /* The '>' was probably forgotten, don't look further. */
24424 return;
24426 default:
24427 break;
24430 /* Consume this token. */
24431 cp_lexer_consume_token (parser->lexer);
24435 /* If the next token is the indicated keyword, consume it. Otherwise,
24436 issue an error message indicating that TOKEN_DESC was expected.
24438 Returns the token consumed, if the token had the appropriate type.
24439 Otherwise, returns NULL. */
24441 static cp_token *
24442 cp_parser_require_keyword (cp_parser* parser,
24443 enum rid keyword,
24444 required_token token_desc)
24446 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24448 if (token && token->keyword != keyword)
24450 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24451 return NULL;
24454 return token;
24457 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24458 function-definition. */
24460 static bool
24461 cp_parser_token_starts_function_definition_p (cp_token* token)
24463 return (/* An ordinary function-body begins with an `{'. */
24464 token->type == CPP_OPEN_BRACE
24465 /* A ctor-initializer begins with a `:'. */
24466 || token->type == CPP_COLON
24467 /* A function-try-block begins with `try'. */
24468 || token->keyword == RID_TRY
24469 /* A function-transaction-block begins with `__transaction_atomic'
24470 or `__transaction_relaxed'. */
24471 || token->keyword == RID_TRANSACTION_ATOMIC
24472 || token->keyword == RID_TRANSACTION_RELAXED
24473 /* The named return value extension begins with `return'. */
24474 || token->keyword == RID_RETURN);
24477 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24478 definition. */
24480 static bool
24481 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24483 cp_token *token;
24485 token = cp_lexer_peek_token (parser->lexer);
24486 return (token->type == CPP_OPEN_BRACE
24487 || (token->type == CPP_COLON
24488 && !parser->colon_doesnt_start_class_def_p));
24491 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24492 C++0x) ending a template-argument. */
24494 static bool
24495 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24497 cp_token *token;
24499 token = cp_lexer_peek_token (parser->lexer);
24500 return (token->type == CPP_COMMA
24501 || token->type == CPP_GREATER
24502 || token->type == CPP_ELLIPSIS
24503 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24506 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24507 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24509 static bool
24510 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24511 size_t n)
24513 cp_token *token;
24515 token = cp_lexer_peek_nth_token (parser->lexer, n);
24516 if (token->type == CPP_LESS)
24517 return true;
24518 /* Check for the sequence `<::' in the original code. It would be lexed as
24519 `[:', where `[' is a digraph, and there is no whitespace before
24520 `:'. */
24521 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24523 cp_token *token2;
24524 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24525 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24526 return true;
24528 return false;
24531 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24532 or none_type otherwise. */
24534 static enum tag_types
24535 cp_parser_token_is_class_key (cp_token* token)
24537 switch (token->keyword)
24539 case RID_CLASS:
24540 return class_type;
24541 case RID_STRUCT:
24542 return record_type;
24543 case RID_UNION:
24544 return union_type;
24546 default:
24547 return none_type;
24551 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24553 static void
24554 cp_parser_check_class_key (enum tag_types class_key, tree type)
24556 if (type == error_mark_node)
24557 return;
24558 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24560 if (permerror (input_location, "%qs tag used in naming %q#T",
24561 class_key == union_type ? "union"
24562 : class_key == record_type ? "struct" : "class",
24563 type))
24564 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24565 "%q#T was previously declared here", type);
24569 /* Issue an error message if DECL is redeclared with different
24570 access than its original declaration [class.access.spec/3].
24571 This applies to nested classes and nested class templates.
24572 [class.mem/1]. */
24574 static void
24575 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24577 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24578 return;
24580 if ((TREE_PRIVATE (decl)
24581 != (current_access_specifier == access_private_node))
24582 || (TREE_PROTECTED (decl)
24583 != (current_access_specifier == access_protected_node)))
24584 error_at (location, "%qD redeclared with different access", decl);
24587 /* Look for the `template' keyword, as a syntactic disambiguator.
24588 Return TRUE iff it is present, in which case it will be
24589 consumed. */
24591 static bool
24592 cp_parser_optional_template_keyword (cp_parser *parser)
24594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24596 /* In C++98 the `template' keyword can only be used within templates;
24597 outside templates the parser can always figure out what is a
24598 template and what is not. In C++11, per the resolution of DR 468,
24599 `template' is allowed in cases where it is not strictly necessary. */
24600 if (!processing_template_decl
24601 && pedantic && cxx_dialect == cxx98)
24603 cp_token *token = cp_lexer_peek_token (parser->lexer);
24604 pedwarn (token->location, OPT_Wpedantic,
24605 "in C++98 %<template%> (as a disambiguator) is only "
24606 "allowed within templates");
24607 /* If this part of the token stream is rescanned, the same
24608 error message would be generated. So, we purge the token
24609 from the stream. */
24610 cp_lexer_purge_token (parser->lexer);
24611 return false;
24613 else
24615 /* Consume the `template' keyword. */
24616 cp_lexer_consume_token (parser->lexer);
24617 return true;
24620 return false;
24623 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24624 set PARSER->SCOPE, and perform other related actions. */
24626 static void
24627 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24629 int i;
24630 struct tree_check *check_value;
24631 deferred_access_check *chk;
24632 vec<deferred_access_check, va_gc> *checks;
24634 /* Get the stored value. */
24635 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24636 /* Perform any access checks that were deferred. */
24637 checks = check_value->checks;
24638 if (checks)
24640 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24641 perform_or_defer_access_check (chk->binfo,
24642 chk->decl,
24643 chk->diag_decl, tf_warning_or_error);
24645 /* Set the scope from the stored value. */
24646 parser->scope = check_value->value;
24647 parser->qualifying_scope = check_value->qualifying_scope;
24648 parser->object_scope = NULL_TREE;
24651 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24652 encounter the end of a block before what we were looking for. */
24654 static bool
24655 cp_parser_cache_group (cp_parser *parser,
24656 enum cpp_ttype end,
24657 unsigned depth)
24659 while (true)
24661 cp_token *token = cp_lexer_peek_token (parser->lexer);
24663 /* Abort a parenthesized expression if we encounter a semicolon. */
24664 if ((end == CPP_CLOSE_PAREN || depth == 0)
24665 && token->type == CPP_SEMICOLON)
24666 return true;
24667 /* If we've reached the end of the file, stop. */
24668 if (token->type == CPP_EOF
24669 || (end != CPP_PRAGMA_EOL
24670 && token->type == CPP_PRAGMA_EOL))
24671 return true;
24672 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24673 /* We've hit the end of an enclosing block, so there's been some
24674 kind of syntax error. */
24675 return true;
24677 /* Consume the token. */
24678 cp_lexer_consume_token (parser->lexer);
24679 /* See if it starts a new group. */
24680 if (token->type == CPP_OPEN_BRACE)
24682 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24683 /* In theory this should probably check end == '}', but
24684 cp_parser_save_member_function_body needs it to exit
24685 after either '}' or ')' when called with ')'. */
24686 if (depth == 0)
24687 return false;
24689 else if (token->type == CPP_OPEN_PAREN)
24691 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24692 if (depth == 0 && end == CPP_CLOSE_PAREN)
24693 return false;
24695 else if (token->type == CPP_PRAGMA)
24696 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24697 else if (token->type == end)
24698 return false;
24702 /* Like above, for caching a default argument or NSDMI. Both of these are
24703 terminated by a non-nested comma, but it can be unclear whether or not a
24704 comma is nested in a template argument list unless we do more parsing.
24705 In order to handle this ambiguity, when we encounter a ',' after a '<'
24706 we try to parse what follows as a parameter-declaration-list (in the
24707 case of a default argument) or a member-declarator (in the case of an
24708 NSDMI). If that succeeds, then we stop caching. */
24710 static tree
24711 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24713 unsigned depth = 0;
24714 int maybe_template_id = 0;
24715 cp_token *first_token;
24716 cp_token *token;
24717 tree default_argument;
24719 /* Add tokens until we have processed the entire default
24720 argument. We add the range [first_token, token). */
24721 first_token = cp_lexer_peek_token (parser->lexer);
24722 if (first_token->type == CPP_OPEN_BRACE)
24724 /* For list-initialization, this is straightforward. */
24725 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24726 token = cp_lexer_peek_token (parser->lexer);
24728 else while (true)
24730 bool done = false;
24732 /* Peek at the next token. */
24733 token = cp_lexer_peek_token (parser->lexer);
24734 /* What we do depends on what token we have. */
24735 switch (token->type)
24737 /* In valid code, a default argument must be
24738 immediately followed by a `,' `)', or `...'. */
24739 case CPP_COMMA:
24740 if (depth == 0 && maybe_template_id)
24742 /* If we've seen a '<', we might be in a
24743 template-argument-list. Until Core issue 325 is
24744 resolved, we don't know how this situation ought
24745 to be handled, so try to DTRT. We check whether
24746 what comes after the comma is a valid parameter
24747 declaration list. If it is, then the comma ends
24748 the default argument; otherwise the default
24749 argument continues. */
24750 bool error = false;
24752 /* Set ITALP so cp_parser_parameter_declaration_list
24753 doesn't decide to commit to this parse. */
24754 bool saved_italp = parser->in_template_argument_list_p;
24755 parser->in_template_argument_list_p = true;
24757 cp_parser_parse_tentatively (parser);
24758 cp_lexer_consume_token (parser->lexer);
24760 if (nsdmi)
24762 int ctor_dtor_or_conv_p;
24763 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24764 &ctor_dtor_or_conv_p,
24765 /*parenthesized_p=*/NULL,
24766 /*member_p=*/true);
24768 else
24770 begin_scope (sk_function_parms, NULL_TREE);
24771 cp_parser_parameter_declaration_list (parser, &error);
24772 pop_bindings_and_leave_scope ();
24774 if (!cp_parser_error_occurred (parser) && !error)
24775 done = true;
24776 cp_parser_abort_tentative_parse (parser);
24778 parser->in_template_argument_list_p = saved_italp;
24779 break;
24781 case CPP_CLOSE_PAREN:
24782 case CPP_ELLIPSIS:
24783 /* If we run into a non-nested `;', `}', or `]',
24784 then the code is invalid -- but the default
24785 argument is certainly over. */
24786 case CPP_SEMICOLON:
24787 case CPP_CLOSE_BRACE:
24788 case CPP_CLOSE_SQUARE:
24789 if (depth == 0
24790 /* Handle correctly int n = sizeof ... ( p ); */
24791 && token->type != CPP_ELLIPSIS)
24792 done = true;
24793 /* Update DEPTH, if necessary. */
24794 else if (token->type == CPP_CLOSE_PAREN
24795 || token->type == CPP_CLOSE_BRACE
24796 || token->type == CPP_CLOSE_SQUARE)
24797 --depth;
24798 break;
24800 case CPP_OPEN_PAREN:
24801 case CPP_OPEN_SQUARE:
24802 case CPP_OPEN_BRACE:
24803 ++depth;
24804 break;
24806 case CPP_LESS:
24807 if (depth == 0)
24808 /* This might be the comparison operator, or it might
24809 start a template argument list. */
24810 ++maybe_template_id;
24811 break;
24813 case CPP_RSHIFT:
24814 if (cxx_dialect == cxx98)
24815 break;
24816 /* Fall through for C++0x, which treats the `>>'
24817 operator like two `>' tokens in certain
24818 cases. */
24820 case CPP_GREATER:
24821 if (depth == 0)
24823 /* This might be an operator, or it might close a
24824 template argument list. But if a previous '<'
24825 started a template argument list, this will have
24826 closed it, so we can't be in one anymore. */
24827 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24828 if (maybe_template_id < 0)
24829 maybe_template_id = 0;
24831 break;
24833 /* If we run out of tokens, issue an error message. */
24834 case CPP_EOF:
24835 case CPP_PRAGMA_EOL:
24836 error_at (token->location, "file ends in default argument");
24837 done = true;
24838 break;
24840 case CPP_NAME:
24841 case CPP_SCOPE:
24842 /* In these cases, we should look for template-ids.
24843 For example, if the default argument is
24844 `X<int, double>()', we need to do name lookup to
24845 figure out whether or not `X' is a template; if
24846 so, the `,' does not end the default argument.
24848 That is not yet done. */
24849 break;
24851 default:
24852 break;
24855 /* If we've reached the end, stop. */
24856 if (done)
24857 break;
24859 /* Add the token to the token block. */
24860 token = cp_lexer_consume_token (parser->lexer);
24863 /* Create a DEFAULT_ARG to represent the unparsed default
24864 argument. */
24865 default_argument = make_node (DEFAULT_ARG);
24866 DEFARG_TOKENS (default_argument)
24867 = cp_token_cache_new (first_token, token);
24868 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24870 return default_argument;
24873 /* Begin parsing tentatively. We always save tokens while parsing
24874 tentatively so that if the tentative parsing fails we can restore the
24875 tokens. */
24877 static void
24878 cp_parser_parse_tentatively (cp_parser* parser)
24880 /* Enter a new parsing context. */
24881 parser->context = cp_parser_context_new (parser->context);
24882 /* Begin saving tokens. */
24883 cp_lexer_save_tokens (parser->lexer);
24884 /* In order to avoid repetitive access control error messages,
24885 access checks are queued up until we are no longer parsing
24886 tentatively. */
24887 push_deferring_access_checks (dk_deferred);
24890 /* Commit to the currently active tentative parse. */
24892 static void
24893 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24895 cp_parser_context *context;
24896 cp_lexer *lexer;
24898 /* Mark all of the levels as committed. */
24899 lexer = parser->lexer;
24900 for (context = parser->context; context->next; context = context->next)
24902 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24903 break;
24904 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24905 while (!cp_lexer_saving_tokens (lexer))
24906 lexer = lexer->next;
24907 cp_lexer_commit_tokens (lexer);
24911 /* Commit to the topmost currently active tentative parse.
24913 Note that this function shouldn't be called when there are
24914 irreversible side-effects while in a tentative state. For
24915 example, we shouldn't create a permanent entry in the symbol
24916 table, or issue an error message that might not apply if the
24917 tentative parse is aborted. */
24919 static void
24920 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24922 cp_parser_context *context = parser->context;
24923 cp_lexer *lexer = parser->lexer;
24925 if (context)
24927 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24928 return;
24929 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24931 while (!cp_lexer_saving_tokens (lexer))
24932 lexer = lexer->next;
24933 cp_lexer_commit_tokens (lexer);
24937 /* Abort the currently active tentative parse. All consumed tokens
24938 will be rolled back, and no diagnostics will be issued. */
24940 static void
24941 cp_parser_abort_tentative_parse (cp_parser* parser)
24943 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24944 || errorcount > 0);
24945 cp_parser_simulate_error (parser);
24946 /* Now, pretend that we want to see if the construct was
24947 successfully parsed. */
24948 cp_parser_parse_definitely (parser);
24951 /* Stop parsing tentatively. If a parse error has occurred, restore the
24952 token stream. Otherwise, commit to the tokens we have consumed.
24953 Returns true if no error occurred; false otherwise. */
24955 static bool
24956 cp_parser_parse_definitely (cp_parser* parser)
24958 bool error_occurred;
24959 cp_parser_context *context;
24961 /* Remember whether or not an error occurred, since we are about to
24962 destroy that information. */
24963 error_occurred = cp_parser_error_occurred (parser);
24964 /* Remove the topmost context from the stack. */
24965 context = parser->context;
24966 parser->context = context->next;
24967 /* If no parse errors occurred, commit to the tentative parse. */
24968 if (!error_occurred)
24970 /* Commit to the tokens read tentatively, unless that was
24971 already done. */
24972 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24973 cp_lexer_commit_tokens (parser->lexer);
24975 pop_to_parent_deferring_access_checks ();
24977 /* Otherwise, if errors occurred, roll back our state so that things
24978 are just as they were before we began the tentative parse. */
24979 else
24981 cp_lexer_rollback_tokens (parser->lexer);
24982 pop_deferring_access_checks ();
24984 /* Add the context to the front of the free list. */
24985 context->next = cp_parser_context_free_list;
24986 cp_parser_context_free_list = context;
24988 return !error_occurred;
24991 /* Returns true if we are parsing tentatively and are not committed to
24992 this tentative parse. */
24994 static bool
24995 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
24997 return (cp_parser_parsing_tentatively (parser)
24998 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25001 /* Returns nonzero iff an error has occurred during the most recent
25002 tentative parse. */
25004 static bool
25005 cp_parser_error_occurred (cp_parser* parser)
25007 return (cp_parser_parsing_tentatively (parser)
25008 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25011 /* Returns nonzero if GNU extensions are allowed. */
25013 static bool
25014 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25016 return parser->allow_gnu_extensions_p;
25019 /* Objective-C++ Productions */
25022 /* Parse an Objective-C expression, which feeds into a primary-expression
25023 above.
25025 objc-expression:
25026 objc-message-expression
25027 objc-string-literal
25028 objc-encode-expression
25029 objc-protocol-expression
25030 objc-selector-expression
25032 Returns a tree representation of the expression. */
25034 static tree
25035 cp_parser_objc_expression (cp_parser* parser)
25037 /* Try to figure out what kind of declaration is present. */
25038 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25040 switch (kwd->type)
25042 case CPP_OPEN_SQUARE:
25043 return cp_parser_objc_message_expression (parser);
25045 case CPP_OBJC_STRING:
25046 kwd = cp_lexer_consume_token (parser->lexer);
25047 return objc_build_string_object (kwd->u.value);
25049 case CPP_KEYWORD:
25050 switch (kwd->keyword)
25052 case RID_AT_ENCODE:
25053 return cp_parser_objc_encode_expression (parser);
25055 case RID_AT_PROTOCOL:
25056 return cp_parser_objc_protocol_expression (parser);
25058 case RID_AT_SELECTOR:
25059 return cp_parser_objc_selector_expression (parser);
25061 default:
25062 break;
25064 default:
25065 error_at (kwd->location,
25066 "misplaced %<@%D%> Objective-C++ construct",
25067 kwd->u.value);
25068 cp_parser_skip_to_end_of_block_or_statement (parser);
25071 return error_mark_node;
25074 /* Parse an Objective-C message expression.
25076 objc-message-expression:
25077 [ objc-message-receiver objc-message-args ]
25079 Returns a representation of an Objective-C message. */
25081 static tree
25082 cp_parser_objc_message_expression (cp_parser* parser)
25084 tree receiver, messageargs;
25086 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25087 receiver = cp_parser_objc_message_receiver (parser);
25088 messageargs = cp_parser_objc_message_args (parser);
25089 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25091 return objc_build_message_expr (receiver, messageargs);
25094 /* Parse an objc-message-receiver.
25096 objc-message-receiver:
25097 expression
25098 simple-type-specifier
25100 Returns a representation of the type or expression. */
25102 static tree
25103 cp_parser_objc_message_receiver (cp_parser* parser)
25105 tree rcv;
25107 /* An Objective-C message receiver may be either (1) a type
25108 or (2) an expression. */
25109 cp_parser_parse_tentatively (parser);
25110 rcv = cp_parser_expression (parser, false, NULL);
25112 if (cp_parser_parse_definitely (parser))
25113 return rcv;
25115 rcv = cp_parser_simple_type_specifier (parser,
25116 /*decl_specs=*/NULL,
25117 CP_PARSER_FLAGS_NONE);
25119 return objc_get_class_reference (rcv);
25122 /* Parse the arguments and selectors comprising an Objective-C message.
25124 objc-message-args:
25125 objc-selector
25126 objc-selector-args
25127 objc-selector-args , objc-comma-args
25129 objc-selector-args:
25130 objc-selector [opt] : assignment-expression
25131 objc-selector-args objc-selector [opt] : assignment-expression
25133 objc-comma-args:
25134 assignment-expression
25135 objc-comma-args , assignment-expression
25137 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25138 selector arguments and TREE_VALUE containing a list of comma
25139 arguments. */
25141 static tree
25142 cp_parser_objc_message_args (cp_parser* parser)
25144 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25145 bool maybe_unary_selector_p = true;
25146 cp_token *token = cp_lexer_peek_token (parser->lexer);
25148 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25150 tree selector = NULL_TREE, arg;
25152 if (token->type != CPP_COLON)
25153 selector = cp_parser_objc_selector (parser);
25155 /* Detect if we have a unary selector. */
25156 if (maybe_unary_selector_p
25157 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25158 return build_tree_list (selector, NULL_TREE);
25160 maybe_unary_selector_p = false;
25161 cp_parser_require (parser, CPP_COLON, RT_COLON);
25162 arg = cp_parser_assignment_expression (parser, false, NULL);
25164 sel_args
25165 = chainon (sel_args,
25166 build_tree_list (selector, arg));
25168 token = cp_lexer_peek_token (parser->lexer);
25171 /* Handle non-selector arguments, if any. */
25172 while (token->type == CPP_COMMA)
25174 tree arg;
25176 cp_lexer_consume_token (parser->lexer);
25177 arg = cp_parser_assignment_expression (parser, false, NULL);
25179 addl_args
25180 = chainon (addl_args,
25181 build_tree_list (NULL_TREE, arg));
25183 token = cp_lexer_peek_token (parser->lexer);
25186 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25188 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25189 return build_tree_list (error_mark_node, error_mark_node);
25192 return build_tree_list (sel_args, addl_args);
25195 /* Parse an Objective-C encode expression.
25197 objc-encode-expression:
25198 @encode objc-typename
25200 Returns an encoded representation of the type argument. */
25202 static tree
25203 cp_parser_objc_encode_expression (cp_parser* parser)
25205 tree type;
25206 cp_token *token;
25208 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25209 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25210 token = cp_lexer_peek_token (parser->lexer);
25211 type = complete_type (cp_parser_type_id (parser));
25212 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25214 if (!type)
25216 error_at (token->location,
25217 "%<@encode%> must specify a type as an argument");
25218 return error_mark_node;
25221 /* This happens if we find @encode(T) (where T is a template
25222 typename or something dependent on a template typename) when
25223 parsing a template. In that case, we can't compile it
25224 immediately, but we rather create an AT_ENCODE_EXPR which will
25225 need to be instantiated when the template is used.
25227 if (dependent_type_p (type))
25229 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25230 TREE_READONLY (value) = 1;
25231 return value;
25234 return objc_build_encode_expr (type);
25237 /* Parse an Objective-C @defs expression. */
25239 static tree
25240 cp_parser_objc_defs_expression (cp_parser *parser)
25242 tree name;
25244 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25245 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25246 name = cp_parser_identifier (parser);
25247 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25249 return objc_get_class_ivars (name);
25252 /* Parse an Objective-C protocol expression.
25254 objc-protocol-expression:
25255 @protocol ( identifier )
25257 Returns a representation of the protocol expression. */
25259 static tree
25260 cp_parser_objc_protocol_expression (cp_parser* parser)
25262 tree proto;
25264 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25265 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25266 proto = cp_parser_identifier (parser);
25267 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25269 return objc_build_protocol_expr (proto);
25272 /* Parse an Objective-C selector expression.
25274 objc-selector-expression:
25275 @selector ( objc-method-signature )
25277 objc-method-signature:
25278 objc-selector
25279 objc-selector-seq
25281 objc-selector-seq:
25282 objc-selector :
25283 objc-selector-seq objc-selector :
25285 Returns a representation of the method selector. */
25287 static tree
25288 cp_parser_objc_selector_expression (cp_parser* parser)
25290 tree sel_seq = NULL_TREE;
25291 bool maybe_unary_selector_p = true;
25292 cp_token *token;
25293 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25295 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25296 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25297 token = cp_lexer_peek_token (parser->lexer);
25299 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25300 || token->type == CPP_SCOPE)
25302 tree selector = NULL_TREE;
25304 if (token->type != CPP_COLON
25305 || token->type == CPP_SCOPE)
25306 selector = cp_parser_objc_selector (parser);
25308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25309 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25311 /* Detect if we have a unary selector. */
25312 if (maybe_unary_selector_p)
25314 sel_seq = selector;
25315 goto finish_selector;
25317 else
25319 cp_parser_error (parser, "expected %<:%>");
25322 maybe_unary_selector_p = false;
25323 token = cp_lexer_consume_token (parser->lexer);
25325 if (token->type == CPP_SCOPE)
25327 sel_seq
25328 = chainon (sel_seq,
25329 build_tree_list (selector, NULL_TREE));
25330 sel_seq
25331 = chainon (sel_seq,
25332 build_tree_list (NULL_TREE, NULL_TREE));
25334 else
25335 sel_seq
25336 = chainon (sel_seq,
25337 build_tree_list (selector, NULL_TREE));
25339 token = cp_lexer_peek_token (parser->lexer);
25342 finish_selector:
25343 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25345 return objc_build_selector_expr (loc, sel_seq);
25348 /* Parse a list of identifiers.
25350 objc-identifier-list:
25351 identifier
25352 objc-identifier-list , identifier
25354 Returns a TREE_LIST of identifier nodes. */
25356 static tree
25357 cp_parser_objc_identifier_list (cp_parser* parser)
25359 tree identifier;
25360 tree list;
25361 cp_token *sep;
25363 identifier = cp_parser_identifier (parser);
25364 if (identifier == error_mark_node)
25365 return error_mark_node;
25367 list = build_tree_list (NULL_TREE, identifier);
25368 sep = cp_lexer_peek_token (parser->lexer);
25370 while (sep->type == CPP_COMMA)
25372 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25373 identifier = cp_parser_identifier (parser);
25374 if (identifier == error_mark_node)
25375 return list;
25377 list = chainon (list, build_tree_list (NULL_TREE,
25378 identifier));
25379 sep = cp_lexer_peek_token (parser->lexer);
25382 return list;
25385 /* Parse an Objective-C alias declaration.
25387 objc-alias-declaration:
25388 @compatibility_alias identifier identifier ;
25390 This function registers the alias mapping with the Objective-C front end.
25391 It returns nothing. */
25393 static void
25394 cp_parser_objc_alias_declaration (cp_parser* parser)
25396 tree alias, orig;
25398 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25399 alias = cp_parser_identifier (parser);
25400 orig = cp_parser_identifier (parser);
25401 objc_declare_alias (alias, orig);
25402 cp_parser_consume_semicolon_at_end_of_statement (parser);
25405 /* Parse an Objective-C class forward-declaration.
25407 objc-class-declaration:
25408 @class objc-identifier-list ;
25410 The function registers the forward declarations with the Objective-C
25411 front end. It returns nothing. */
25413 static void
25414 cp_parser_objc_class_declaration (cp_parser* parser)
25416 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25417 while (true)
25419 tree id;
25421 id = cp_parser_identifier (parser);
25422 if (id == error_mark_node)
25423 break;
25425 objc_declare_class (id);
25427 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25428 cp_lexer_consume_token (parser->lexer);
25429 else
25430 break;
25432 cp_parser_consume_semicolon_at_end_of_statement (parser);
25435 /* Parse a list of Objective-C protocol references.
25437 objc-protocol-refs-opt:
25438 objc-protocol-refs [opt]
25440 objc-protocol-refs:
25441 < objc-identifier-list >
25443 Returns a TREE_LIST of identifiers, if any. */
25445 static tree
25446 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25448 tree protorefs = NULL_TREE;
25450 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25452 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25453 protorefs = cp_parser_objc_identifier_list (parser);
25454 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25457 return protorefs;
25460 /* Parse a Objective-C visibility specification. */
25462 static void
25463 cp_parser_objc_visibility_spec (cp_parser* parser)
25465 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25467 switch (vis->keyword)
25469 case RID_AT_PRIVATE:
25470 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25471 break;
25472 case RID_AT_PROTECTED:
25473 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25474 break;
25475 case RID_AT_PUBLIC:
25476 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25477 break;
25478 case RID_AT_PACKAGE:
25479 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25480 break;
25481 default:
25482 return;
25485 /* Eat '@private'/'@protected'/'@public'. */
25486 cp_lexer_consume_token (parser->lexer);
25489 /* Parse an Objective-C method type. Return 'true' if it is a class
25490 (+) method, and 'false' if it is an instance (-) method. */
25492 static inline bool
25493 cp_parser_objc_method_type (cp_parser* parser)
25495 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25496 return true;
25497 else
25498 return false;
25501 /* Parse an Objective-C protocol qualifier. */
25503 static tree
25504 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25506 tree quals = NULL_TREE, node;
25507 cp_token *token = cp_lexer_peek_token (parser->lexer);
25509 node = token->u.value;
25511 while (node && identifier_p (node)
25512 && (node == ridpointers [(int) RID_IN]
25513 || node == ridpointers [(int) RID_OUT]
25514 || node == ridpointers [(int) RID_INOUT]
25515 || node == ridpointers [(int) RID_BYCOPY]
25516 || node == ridpointers [(int) RID_BYREF]
25517 || node == ridpointers [(int) RID_ONEWAY]))
25519 quals = tree_cons (NULL_TREE, node, quals);
25520 cp_lexer_consume_token (parser->lexer);
25521 token = cp_lexer_peek_token (parser->lexer);
25522 node = token->u.value;
25525 return quals;
25528 /* Parse an Objective-C typename. */
25530 static tree
25531 cp_parser_objc_typename (cp_parser* parser)
25533 tree type_name = NULL_TREE;
25535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25537 tree proto_quals, cp_type = NULL_TREE;
25539 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25540 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25542 /* An ObjC type name may consist of just protocol qualifiers, in which
25543 case the type shall default to 'id'. */
25544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25546 cp_type = cp_parser_type_id (parser);
25548 /* If the type could not be parsed, an error has already
25549 been produced. For error recovery, behave as if it had
25550 not been specified, which will use the default type
25551 'id'. */
25552 if (cp_type == error_mark_node)
25554 cp_type = NULL_TREE;
25555 /* We need to skip to the closing parenthesis as
25556 cp_parser_type_id() does not seem to do it for
25557 us. */
25558 cp_parser_skip_to_closing_parenthesis (parser,
25559 /*recovering=*/true,
25560 /*or_comma=*/false,
25561 /*consume_paren=*/false);
25565 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25566 type_name = build_tree_list (proto_quals, cp_type);
25569 return type_name;
25572 /* Check to see if TYPE refers to an Objective-C selector name. */
25574 static bool
25575 cp_parser_objc_selector_p (enum cpp_ttype type)
25577 return (type == CPP_NAME || type == CPP_KEYWORD
25578 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25579 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25580 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25581 || type == CPP_XOR || type == CPP_XOR_EQ);
25584 /* Parse an Objective-C selector. */
25586 static tree
25587 cp_parser_objc_selector (cp_parser* parser)
25589 cp_token *token = cp_lexer_consume_token (parser->lexer);
25591 if (!cp_parser_objc_selector_p (token->type))
25593 error_at (token->location, "invalid Objective-C++ selector name");
25594 return error_mark_node;
25597 /* C++ operator names are allowed to appear in ObjC selectors. */
25598 switch (token->type)
25600 case CPP_AND_AND: return get_identifier ("and");
25601 case CPP_AND_EQ: return get_identifier ("and_eq");
25602 case CPP_AND: return get_identifier ("bitand");
25603 case CPP_OR: return get_identifier ("bitor");
25604 case CPP_COMPL: return get_identifier ("compl");
25605 case CPP_NOT: return get_identifier ("not");
25606 case CPP_NOT_EQ: return get_identifier ("not_eq");
25607 case CPP_OR_OR: return get_identifier ("or");
25608 case CPP_OR_EQ: return get_identifier ("or_eq");
25609 case CPP_XOR: return get_identifier ("xor");
25610 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25611 default: return token->u.value;
25615 /* Parse an Objective-C params list. */
25617 static tree
25618 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25620 tree params = NULL_TREE;
25621 bool maybe_unary_selector_p = true;
25622 cp_token *token = cp_lexer_peek_token (parser->lexer);
25624 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25626 tree selector = NULL_TREE, type_name, identifier;
25627 tree parm_attr = NULL_TREE;
25629 if (token->keyword == RID_ATTRIBUTE)
25630 break;
25632 if (token->type != CPP_COLON)
25633 selector = cp_parser_objc_selector (parser);
25635 /* Detect if we have a unary selector. */
25636 if (maybe_unary_selector_p
25637 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25639 params = selector; /* Might be followed by attributes. */
25640 break;
25643 maybe_unary_selector_p = false;
25644 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25646 /* Something went quite wrong. There should be a colon
25647 here, but there is not. Stop parsing parameters. */
25648 break;
25650 type_name = cp_parser_objc_typename (parser);
25651 /* New ObjC allows attributes on parameters too. */
25652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25653 parm_attr = cp_parser_attributes_opt (parser);
25654 identifier = cp_parser_identifier (parser);
25656 params
25657 = chainon (params,
25658 objc_build_keyword_decl (selector,
25659 type_name,
25660 identifier,
25661 parm_attr));
25663 token = cp_lexer_peek_token (parser->lexer);
25666 if (params == NULL_TREE)
25668 cp_parser_error (parser, "objective-c++ method declaration is expected");
25669 return error_mark_node;
25672 /* We allow tail attributes for the method. */
25673 if (token->keyword == RID_ATTRIBUTE)
25675 *attributes = cp_parser_attributes_opt (parser);
25676 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25677 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25678 return params;
25679 cp_parser_error (parser,
25680 "method attributes must be specified at the end");
25681 return error_mark_node;
25684 if (params == NULL_TREE)
25686 cp_parser_error (parser, "objective-c++ method declaration is expected");
25687 return error_mark_node;
25689 return params;
25692 /* Parse the non-keyword Objective-C params. */
25694 static tree
25695 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25696 tree* attributes)
25698 tree params = make_node (TREE_LIST);
25699 cp_token *token = cp_lexer_peek_token (parser->lexer);
25700 *ellipsisp = false; /* Initially, assume no ellipsis. */
25702 while (token->type == CPP_COMMA)
25704 cp_parameter_declarator *parmdecl;
25705 tree parm;
25707 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25708 token = cp_lexer_peek_token (parser->lexer);
25710 if (token->type == CPP_ELLIPSIS)
25712 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25713 *ellipsisp = true;
25714 token = cp_lexer_peek_token (parser->lexer);
25715 break;
25718 /* TODO: parse attributes for tail parameters. */
25719 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25720 parm = grokdeclarator (parmdecl->declarator,
25721 &parmdecl->decl_specifiers,
25722 PARM, /*initialized=*/0,
25723 /*attrlist=*/NULL);
25725 chainon (params, build_tree_list (NULL_TREE, parm));
25726 token = cp_lexer_peek_token (parser->lexer);
25729 /* We allow tail attributes for the method. */
25730 if (token->keyword == RID_ATTRIBUTE)
25732 if (*attributes == NULL_TREE)
25734 *attributes = cp_parser_attributes_opt (parser);
25735 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25736 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25737 return params;
25739 else
25740 /* We have an error, but parse the attributes, so that we can
25741 carry on. */
25742 *attributes = cp_parser_attributes_opt (parser);
25744 cp_parser_error (parser,
25745 "method attributes must be specified at the end");
25746 return error_mark_node;
25749 return params;
25752 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25754 static void
25755 cp_parser_objc_interstitial_code (cp_parser* parser)
25757 cp_token *token = cp_lexer_peek_token (parser->lexer);
25759 /* If the next token is `extern' and the following token is a string
25760 literal, then we have a linkage specification. */
25761 if (token->keyword == RID_EXTERN
25762 && cp_parser_is_pure_string_literal
25763 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25764 cp_parser_linkage_specification (parser);
25765 /* Handle #pragma, if any. */
25766 else if (token->type == CPP_PRAGMA)
25767 cp_parser_pragma (parser, pragma_objc_icode);
25768 /* Allow stray semicolons. */
25769 else if (token->type == CPP_SEMICOLON)
25770 cp_lexer_consume_token (parser->lexer);
25771 /* Mark methods as optional or required, when building protocols. */
25772 else if (token->keyword == RID_AT_OPTIONAL)
25774 cp_lexer_consume_token (parser->lexer);
25775 objc_set_method_opt (true);
25777 else if (token->keyword == RID_AT_REQUIRED)
25779 cp_lexer_consume_token (parser->lexer);
25780 objc_set_method_opt (false);
25782 else if (token->keyword == RID_NAMESPACE)
25783 cp_parser_namespace_definition (parser);
25784 /* Other stray characters must generate errors. */
25785 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25787 cp_lexer_consume_token (parser->lexer);
25788 error ("stray %qs between Objective-C++ methods",
25789 token->type == CPP_OPEN_BRACE ? "{" : "}");
25791 /* Finally, try to parse a block-declaration, or a function-definition. */
25792 else
25793 cp_parser_block_declaration (parser, /*statement_p=*/false);
25796 /* Parse a method signature. */
25798 static tree
25799 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25801 tree rettype, kwdparms, optparms;
25802 bool ellipsis = false;
25803 bool is_class_method;
25805 is_class_method = cp_parser_objc_method_type (parser);
25806 rettype = cp_parser_objc_typename (parser);
25807 *attributes = NULL_TREE;
25808 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25809 if (kwdparms == error_mark_node)
25810 return error_mark_node;
25811 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25812 if (optparms == error_mark_node)
25813 return error_mark_node;
25815 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25818 static bool
25819 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25821 tree tattr;
25822 cp_lexer_save_tokens (parser->lexer);
25823 tattr = cp_parser_attributes_opt (parser);
25824 gcc_assert (tattr) ;
25826 /* If the attributes are followed by a method introducer, this is not allowed.
25827 Dump the attributes and flag the situation. */
25828 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25829 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25830 return true;
25832 /* Otherwise, the attributes introduce some interstitial code, possibly so
25833 rewind to allow that check. */
25834 cp_lexer_rollback_tokens (parser->lexer);
25835 return false;
25838 /* Parse an Objective-C method prototype list. */
25840 static void
25841 cp_parser_objc_method_prototype_list (cp_parser* parser)
25843 cp_token *token = cp_lexer_peek_token (parser->lexer);
25845 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25847 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25849 tree attributes, sig;
25850 bool is_class_method;
25851 if (token->type == CPP_PLUS)
25852 is_class_method = true;
25853 else
25854 is_class_method = false;
25855 sig = cp_parser_objc_method_signature (parser, &attributes);
25856 if (sig == error_mark_node)
25858 cp_parser_skip_to_end_of_block_or_statement (parser);
25859 token = cp_lexer_peek_token (parser->lexer);
25860 continue;
25862 objc_add_method_declaration (is_class_method, sig, attributes);
25863 cp_parser_consume_semicolon_at_end_of_statement (parser);
25865 else if (token->keyword == RID_AT_PROPERTY)
25866 cp_parser_objc_at_property_declaration (parser);
25867 else if (token->keyword == RID_ATTRIBUTE
25868 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25869 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25870 OPT_Wattributes,
25871 "prefix attributes are ignored for methods");
25872 else
25873 /* Allow for interspersed non-ObjC++ code. */
25874 cp_parser_objc_interstitial_code (parser);
25876 token = cp_lexer_peek_token (parser->lexer);
25879 if (token->type != CPP_EOF)
25880 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25881 else
25882 cp_parser_error (parser, "expected %<@end%>");
25884 objc_finish_interface ();
25887 /* Parse an Objective-C method definition list. */
25889 static void
25890 cp_parser_objc_method_definition_list (cp_parser* parser)
25892 cp_token *token = cp_lexer_peek_token (parser->lexer);
25894 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25896 tree meth;
25898 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25900 cp_token *ptk;
25901 tree sig, attribute;
25902 bool is_class_method;
25903 if (token->type == CPP_PLUS)
25904 is_class_method = true;
25905 else
25906 is_class_method = false;
25907 push_deferring_access_checks (dk_deferred);
25908 sig = cp_parser_objc_method_signature (parser, &attribute);
25909 if (sig == error_mark_node)
25911 cp_parser_skip_to_end_of_block_or_statement (parser);
25912 token = cp_lexer_peek_token (parser->lexer);
25913 continue;
25915 objc_start_method_definition (is_class_method, sig, attribute,
25916 NULL_TREE);
25918 /* For historical reasons, we accept an optional semicolon. */
25919 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25920 cp_lexer_consume_token (parser->lexer);
25922 ptk = cp_lexer_peek_token (parser->lexer);
25923 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25924 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25926 perform_deferred_access_checks (tf_warning_or_error);
25927 stop_deferring_access_checks ();
25928 meth = cp_parser_function_definition_after_declarator (parser,
25929 false);
25930 pop_deferring_access_checks ();
25931 objc_finish_method_definition (meth);
25934 /* The following case will be removed once @synthesize is
25935 completely implemented. */
25936 else if (token->keyword == RID_AT_PROPERTY)
25937 cp_parser_objc_at_property_declaration (parser);
25938 else if (token->keyword == RID_AT_SYNTHESIZE)
25939 cp_parser_objc_at_synthesize_declaration (parser);
25940 else if (token->keyword == RID_AT_DYNAMIC)
25941 cp_parser_objc_at_dynamic_declaration (parser);
25942 else if (token->keyword == RID_ATTRIBUTE
25943 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25944 warning_at (token->location, OPT_Wattributes,
25945 "prefix attributes are ignored for methods");
25946 else
25947 /* Allow for interspersed non-ObjC++ code. */
25948 cp_parser_objc_interstitial_code (parser);
25950 token = cp_lexer_peek_token (parser->lexer);
25953 if (token->type != CPP_EOF)
25954 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25955 else
25956 cp_parser_error (parser, "expected %<@end%>");
25958 objc_finish_implementation ();
25961 /* Parse Objective-C ivars. */
25963 static void
25964 cp_parser_objc_class_ivars (cp_parser* parser)
25966 cp_token *token = cp_lexer_peek_token (parser->lexer);
25968 if (token->type != CPP_OPEN_BRACE)
25969 return; /* No ivars specified. */
25971 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25972 token = cp_lexer_peek_token (parser->lexer);
25974 while (token->type != CPP_CLOSE_BRACE
25975 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25977 cp_decl_specifier_seq declspecs;
25978 int decl_class_or_enum_p;
25979 tree prefix_attributes;
25981 cp_parser_objc_visibility_spec (parser);
25983 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25984 break;
25986 cp_parser_decl_specifier_seq (parser,
25987 CP_PARSER_FLAGS_OPTIONAL,
25988 &declspecs,
25989 &decl_class_or_enum_p);
25991 /* auto, register, static, extern, mutable. */
25992 if (declspecs.storage_class != sc_none)
25994 cp_parser_error (parser, "invalid type for instance variable");
25995 declspecs.storage_class = sc_none;
25998 /* thread_local. */
25999 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26001 cp_parser_error (parser, "invalid type for instance variable");
26002 declspecs.locations[ds_thread] = 0;
26005 /* typedef. */
26006 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26008 cp_parser_error (parser, "invalid type for instance variable");
26009 declspecs.locations[ds_typedef] = 0;
26012 prefix_attributes = declspecs.attributes;
26013 declspecs.attributes = NULL_TREE;
26015 /* Keep going until we hit the `;' at the end of the
26016 declaration. */
26017 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26019 tree width = NULL_TREE, attributes, first_attribute, decl;
26020 cp_declarator *declarator = NULL;
26021 int ctor_dtor_or_conv_p;
26023 /* Check for a (possibly unnamed) bitfield declaration. */
26024 token = cp_lexer_peek_token (parser->lexer);
26025 if (token->type == CPP_COLON)
26026 goto eat_colon;
26028 if (token->type == CPP_NAME
26029 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26030 == CPP_COLON))
26032 /* Get the name of the bitfield. */
26033 declarator = make_id_declarator (NULL_TREE,
26034 cp_parser_identifier (parser),
26035 sfk_none);
26037 eat_colon:
26038 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26039 /* Get the width of the bitfield. */
26040 width
26041 = cp_parser_constant_expression (parser,
26042 /*allow_non_constant=*/false,
26043 NULL);
26045 else
26047 /* Parse the declarator. */
26048 declarator
26049 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26050 &ctor_dtor_or_conv_p,
26051 /*parenthesized_p=*/NULL,
26052 /*member_p=*/false);
26055 /* Look for attributes that apply to the ivar. */
26056 attributes = cp_parser_attributes_opt (parser);
26057 /* Remember which attributes are prefix attributes and
26058 which are not. */
26059 first_attribute = attributes;
26060 /* Combine the attributes. */
26061 attributes = chainon (prefix_attributes, attributes);
26063 if (width)
26064 /* Create the bitfield declaration. */
26065 decl = grokbitfield (declarator, &declspecs,
26066 width,
26067 attributes);
26068 else
26069 decl = grokfield (declarator, &declspecs,
26070 NULL_TREE, /*init_const_expr_p=*/false,
26071 NULL_TREE, attributes);
26073 /* Add the instance variable. */
26074 if (decl != error_mark_node && decl != NULL_TREE)
26075 objc_add_instance_variable (decl);
26077 /* Reset PREFIX_ATTRIBUTES. */
26078 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26079 attributes = TREE_CHAIN (attributes);
26080 if (attributes)
26081 TREE_CHAIN (attributes) = NULL_TREE;
26083 token = cp_lexer_peek_token (parser->lexer);
26085 if (token->type == CPP_COMMA)
26087 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26088 continue;
26090 break;
26093 cp_parser_consume_semicolon_at_end_of_statement (parser);
26094 token = cp_lexer_peek_token (parser->lexer);
26097 if (token->keyword == RID_AT_END)
26098 cp_parser_error (parser, "expected %<}%>");
26100 /* Do not consume the RID_AT_END, so it will be read again as terminating
26101 the @interface of @implementation. */
26102 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26103 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26105 /* For historical reasons, we accept an optional semicolon. */
26106 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26107 cp_lexer_consume_token (parser->lexer);
26110 /* Parse an Objective-C protocol declaration. */
26112 static void
26113 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26115 tree proto, protorefs;
26116 cp_token *tok;
26118 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26121 tok = cp_lexer_peek_token (parser->lexer);
26122 error_at (tok->location, "identifier expected after %<@protocol%>");
26123 cp_parser_consume_semicolon_at_end_of_statement (parser);
26124 return;
26127 /* See if we have a forward declaration or a definition. */
26128 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26130 /* Try a forward declaration first. */
26131 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26133 while (true)
26135 tree id;
26137 id = cp_parser_identifier (parser);
26138 if (id == error_mark_node)
26139 break;
26141 objc_declare_protocol (id, attributes);
26143 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26144 cp_lexer_consume_token (parser->lexer);
26145 else
26146 break;
26148 cp_parser_consume_semicolon_at_end_of_statement (parser);
26151 /* Ok, we got a full-fledged definition (or at least should). */
26152 else
26154 proto = cp_parser_identifier (parser);
26155 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26156 objc_start_protocol (proto, protorefs, attributes);
26157 cp_parser_objc_method_prototype_list (parser);
26161 /* Parse an Objective-C superclass or category. */
26163 static void
26164 cp_parser_objc_superclass_or_category (cp_parser *parser,
26165 bool iface_p,
26166 tree *super,
26167 tree *categ, bool *is_class_extension)
26169 cp_token *next = cp_lexer_peek_token (parser->lexer);
26171 *super = *categ = NULL_TREE;
26172 *is_class_extension = false;
26173 if (next->type == CPP_COLON)
26175 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26176 *super = cp_parser_identifier (parser);
26178 else if (next->type == CPP_OPEN_PAREN)
26180 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26182 /* If there is no category name, and this is an @interface, we
26183 have a class extension. */
26184 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26186 *categ = NULL_TREE;
26187 *is_class_extension = true;
26189 else
26190 *categ = cp_parser_identifier (parser);
26192 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26196 /* Parse an Objective-C class interface. */
26198 static void
26199 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26201 tree name, super, categ, protos;
26202 bool is_class_extension;
26204 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26205 name = cp_parser_identifier (parser);
26206 if (name == error_mark_node)
26208 /* It's hard to recover because even if valid @interface stuff
26209 is to follow, we can't compile it (or validate it) if we
26210 don't even know which class it refers to. Let's assume this
26211 was a stray '@interface' token in the stream and skip it.
26213 return;
26215 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26216 &is_class_extension);
26217 protos = cp_parser_objc_protocol_refs_opt (parser);
26219 /* We have either a class or a category on our hands. */
26220 if (categ || is_class_extension)
26221 objc_start_category_interface (name, categ, protos, attributes);
26222 else
26224 objc_start_class_interface (name, super, protos, attributes);
26225 /* Handle instance variable declarations, if any. */
26226 cp_parser_objc_class_ivars (parser);
26227 objc_continue_interface ();
26230 cp_parser_objc_method_prototype_list (parser);
26233 /* Parse an Objective-C class implementation. */
26235 static void
26236 cp_parser_objc_class_implementation (cp_parser* parser)
26238 tree name, super, categ;
26239 bool is_class_extension;
26241 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26242 name = cp_parser_identifier (parser);
26243 if (name == error_mark_node)
26245 /* It's hard to recover because even if valid @implementation
26246 stuff is to follow, we can't compile it (or validate it) if
26247 we don't even know which class it refers to. Let's assume
26248 this was a stray '@implementation' token in the stream and
26249 skip it.
26251 return;
26253 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26254 &is_class_extension);
26256 /* We have either a class or a category on our hands. */
26257 if (categ)
26258 objc_start_category_implementation (name, categ);
26259 else
26261 objc_start_class_implementation (name, super);
26262 /* Handle instance variable declarations, if any. */
26263 cp_parser_objc_class_ivars (parser);
26264 objc_continue_implementation ();
26267 cp_parser_objc_method_definition_list (parser);
26270 /* Consume the @end token and finish off the implementation. */
26272 static void
26273 cp_parser_objc_end_implementation (cp_parser* parser)
26275 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26276 objc_finish_implementation ();
26279 /* Parse an Objective-C declaration. */
26281 static void
26282 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26284 /* Try to figure out what kind of declaration is present. */
26285 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26287 if (attributes)
26288 switch (kwd->keyword)
26290 case RID_AT_ALIAS:
26291 case RID_AT_CLASS:
26292 case RID_AT_END:
26293 error_at (kwd->location, "attributes may not be specified before"
26294 " the %<@%D%> Objective-C++ keyword",
26295 kwd->u.value);
26296 attributes = NULL;
26297 break;
26298 case RID_AT_IMPLEMENTATION:
26299 warning_at (kwd->location, OPT_Wattributes,
26300 "prefix attributes are ignored before %<@%D%>",
26301 kwd->u.value);
26302 attributes = NULL;
26303 default:
26304 break;
26307 switch (kwd->keyword)
26309 case RID_AT_ALIAS:
26310 cp_parser_objc_alias_declaration (parser);
26311 break;
26312 case RID_AT_CLASS:
26313 cp_parser_objc_class_declaration (parser);
26314 break;
26315 case RID_AT_PROTOCOL:
26316 cp_parser_objc_protocol_declaration (parser, attributes);
26317 break;
26318 case RID_AT_INTERFACE:
26319 cp_parser_objc_class_interface (parser, attributes);
26320 break;
26321 case RID_AT_IMPLEMENTATION:
26322 cp_parser_objc_class_implementation (parser);
26323 break;
26324 case RID_AT_END:
26325 cp_parser_objc_end_implementation (parser);
26326 break;
26327 default:
26328 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26329 kwd->u.value);
26330 cp_parser_skip_to_end_of_block_or_statement (parser);
26334 /* Parse an Objective-C try-catch-finally statement.
26336 objc-try-catch-finally-stmt:
26337 @try compound-statement objc-catch-clause-seq [opt]
26338 objc-finally-clause [opt]
26340 objc-catch-clause-seq:
26341 objc-catch-clause objc-catch-clause-seq [opt]
26343 objc-catch-clause:
26344 @catch ( objc-exception-declaration ) compound-statement
26346 objc-finally-clause:
26347 @finally compound-statement
26349 objc-exception-declaration:
26350 parameter-declaration
26351 '...'
26353 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26355 Returns NULL_TREE.
26357 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26358 for C. Keep them in sync. */
26360 static tree
26361 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26363 location_t location;
26364 tree stmt;
26366 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26367 location = cp_lexer_peek_token (parser->lexer)->location;
26368 objc_maybe_warn_exceptions (location);
26369 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26370 node, lest it get absorbed into the surrounding block. */
26371 stmt = push_stmt_list ();
26372 cp_parser_compound_statement (parser, NULL, false, false);
26373 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26375 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26377 cp_parameter_declarator *parm;
26378 tree parameter_declaration = error_mark_node;
26379 bool seen_open_paren = false;
26381 cp_lexer_consume_token (parser->lexer);
26382 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26383 seen_open_paren = true;
26384 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26386 /* We have "@catch (...)" (where the '...' are literally
26387 what is in the code). Skip the '...'.
26388 parameter_declaration is set to NULL_TREE, and
26389 objc_being_catch_clauses() knows that that means
26390 '...'. */
26391 cp_lexer_consume_token (parser->lexer);
26392 parameter_declaration = NULL_TREE;
26394 else
26396 /* We have "@catch (NSException *exception)" or something
26397 like that. Parse the parameter declaration. */
26398 parm = cp_parser_parameter_declaration (parser, false, NULL);
26399 if (parm == NULL)
26400 parameter_declaration = error_mark_node;
26401 else
26402 parameter_declaration = grokdeclarator (parm->declarator,
26403 &parm->decl_specifiers,
26404 PARM, /*initialized=*/0,
26405 /*attrlist=*/NULL);
26407 if (seen_open_paren)
26408 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26409 else
26411 /* If there was no open parenthesis, we are recovering from
26412 an error, and we are trying to figure out what mistake
26413 the user has made. */
26415 /* If there is an immediate closing parenthesis, the user
26416 probably forgot the opening one (ie, they typed "@catch
26417 NSException *e)". Parse the closing parenthesis and keep
26418 going. */
26419 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26420 cp_lexer_consume_token (parser->lexer);
26422 /* If these is no immediate closing parenthesis, the user
26423 probably doesn't know that parenthesis are required at
26424 all (ie, they typed "@catch NSException *e"). So, just
26425 forget about the closing parenthesis and keep going. */
26427 objc_begin_catch_clause (parameter_declaration);
26428 cp_parser_compound_statement (parser, NULL, false, false);
26429 objc_finish_catch_clause ();
26431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26433 cp_lexer_consume_token (parser->lexer);
26434 location = cp_lexer_peek_token (parser->lexer)->location;
26435 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26436 node, lest it get absorbed into the surrounding block. */
26437 stmt = push_stmt_list ();
26438 cp_parser_compound_statement (parser, NULL, false, false);
26439 objc_build_finally_clause (location, pop_stmt_list (stmt));
26442 return objc_finish_try_stmt ();
26445 /* Parse an Objective-C synchronized statement.
26447 objc-synchronized-stmt:
26448 @synchronized ( expression ) compound-statement
26450 Returns NULL_TREE. */
26452 static tree
26453 cp_parser_objc_synchronized_statement (cp_parser *parser)
26455 location_t location;
26456 tree lock, stmt;
26458 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26460 location = cp_lexer_peek_token (parser->lexer)->location;
26461 objc_maybe_warn_exceptions (location);
26462 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26463 lock = cp_parser_expression (parser, false, NULL);
26464 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26466 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26467 node, lest it get absorbed into the surrounding block. */
26468 stmt = push_stmt_list ();
26469 cp_parser_compound_statement (parser, NULL, false, false);
26471 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26474 /* Parse an Objective-C throw statement.
26476 objc-throw-stmt:
26477 @throw assignment-expression [opt] ;
26479 Returns a constructed '@throw' statement. */
26481 static tree
26482 cp_parser_objc_throw_statement (cp_parser *parser)
26484 tree expr = NULL_TREE;
26485 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26487 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26490 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26492 cp_parser_consume_semicolon_at_end_of_statement (parser);
26494 return objc_build_throw_stmt (loc, expr);
26497 /* Parse an Objective-C statement. */
26499 static tree
26500 cp_parser_objc_statement (cp_parser * parser)
26502 /* Try to figure out what kind of declaration is present. */
26503 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26505 switch (kwd->keyword)
26507 case RID_AT_TRY:
26508 return cp_parser_objc_try_catch_finally_statement (parser);
26509 case RID_AT_SYNCHRONIZED:
26510 return cp_parser_objc_synchronized_statement (parser);
26511 case RID_AT_THROW:
26512 return cp_parser_objc_throw_statement (parser);
26513 default:
26514 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26515 kwd->u.value);
26516 cp_parser_skip_to_end_of_block_or_statement (parser);
26519 return error_mark_node;
26522 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26523 look ahead to see if an objc keyword follows the attributes. This
26524 is to detect the use of prefix attributes on ObjC @interface and
26525 @protocol. */
26527 static bool
26528 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26530 cp_lexer_save_tokens (parser->lexer);
26531 *attrib = cp_parser_attributes_opt (parser);
26532 gcc_assert (*attrib);
26533 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26535 cp_lexer_commit_tokens (parser->lexer);
26536 return true;
26538 cp_lexer_rollback_tokens (parser->lexer);
26539 return false;
26542 /* This routine is a minimal replacement for
26543 c_parser_struct_declaration () used when parsing the list of
26544 types/names or ObjC++ properties. For example, when parsing the
26545 code
26547 @property (readonly) int a, b, c;
26549 this function is responsible for parsing "int a, int b, int c" and
26550 returning the declarations as CHAIN of DECLs.
26552 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26553 similar parsing. */
26554 static tree
26555 cp_parser_objc_struct_declaration (cp_parser *parser)
26557 tree decls = NULL_TREE;
26558 cp_decl_specifier_seq declspecs;
26559 int decl_class_or_enum_p;
26560 tree prefix_attributes;
26562 cp_parser_decl_specifier_seq (parser,
26563 CP_PARSER_FLAGS_NONE,
26564 &declspecs,
26565 &decl_class_or_enum_p);
26567 if (declspecs.type == error_mark_node)
26568 return error_mark_node;
26570 /* auto, register, static, extern, mutable. */
26571 if (declspecs.storage_class != sc_none)
26573 cp_parser_error (parser, "invalid type for property");
26574 declspecs.storage_class = sc_none;
26577 /* thread_local. */
26578 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26580 cp_parser_error (parser, "invalid type for property");
26581 declspecs.locations[ds_thread] = 0;
26584 /* typedef. */
26585 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26587 cp_parser_error (parser, "invalid type for property");
26588 declspecs.locations[ds_typedef] = 0;
26591 prefix_attributes = declspecs.attributes;
26592 declspecs.attributes = NULL_TREE;
26594 /* Keep going until we hit the `;' at the end of the declaration. */
26595 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26597 tree attributes, first_attribute, decl;
26598 cp_declarator *declarator;
26599 cp_token *token;
26601 /* Parse the declarator. */
26602 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26603 NULL, NULL, false);
26605 /* Look for attributes that apply to the ivar. */
26606 attributes = cp_parser_attributes_opt (parser);
26607 /* Remember which attributes are prefix attributes and
26608 which are not. */
26609 first_attribute = attributes;
26610 /* Combine the attributes. */
26611 attributes = chainon (prefix_attributes, attributes);
26613 decl = grokfield (declarator, &declspecs,
26614 NULL_TREE, /*init_const_expr_p=*/false,
26615 NULL_TREE, attributes);
26617 if (decl == error_mark_node || decl == NULL_TREE)
26618 return error_mark_node;
26620 /* Reset PREFIX_ATTRIBUTES. */
26621 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26622 attributes = TREE_CHAIN (attributes);
26623 if (attributes)
26624 TREE_CHAIN (attributes) = NULL_TREE;
26626 DECL_CHAIN (decl) = decls;
26627 decls = decl;
26629 token = cp_lexer_peek_token (parser->lexer);
26630 if (token->type == CPP_COMMA)
26632 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26633 continue;
26635 else
26636 break;
26638 return decls;
26641 /* Parse an Objective-C @property declaration. The syntax is:
26643 objc-property-declaration:
26644 '@property' objc-property-attributes[opt] struct-declaration ;
26646 objc-property-attributes:
26647 '(' objc-property-attribute-list ')'
26649 objc-property-attribute-list:
26650 objc-property-attribute
26651 objc-property-attribute-list, objc-property-attribute
26653 objc-property-attribute
26654 'getter' = identifier
26655 'setter' = identifier
26656 'readonly'
26657 'readwrite'
26658 'assign'
26659 'retain'
26660 'copy'
26661 'nonatomic'
26663 For example:
26664 @property NSString *name;
26665 @property (readonly) id object;
26666 @property (retain, nonatomic, getter=getTheName) id name;
26667 @property int a, b, c;
26669 PS: This function is identical to
26670 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26671 static void
26672 cp_parser_objc_at_property_declaration (cp_parser *parser)
26674 /* The following variables hold the attributes of the properties as
26675 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26676 seen. When we see an attribute, we set them to 'true' (if they
26677 are boolean properties) or to the identifier (if they have an
26678 argument, ie, for getter and setter). Note that here we only
26679 parse the list of attributes, check the syntax and accumulate the
26680 attributes that we find. objc_add_property_declaration() will
26681 then process the information. */
26682 bool property_assign = false;
26683 bool property_copy = false;
26684 tree property_getter_ident = NULL_TREE;
26685 bool property_nonatomic = false;
26686 bool property_readonly = false;
26687 bool property_readwrite = false;
26688 bool property_retain = false;
26689 tree property_setter_ident = NULL_TREE;
26691 /* 'properties' is the list of properties that we read. Usually a
26692 single one, but maybe more (eg, in "@property int a, b, c;" there
26693 are three). */
26694 tree properties;
26695 location_t loc;
26697 loc = cp_lexer_peek_token (parser->lexer)->location;
26699 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26701 /* Parse the optional attribute list... */
26702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26704 /* Eat the '('. */
26705 cp_lexer_consume_token (parser->lexer);
26707 while (true)
26709 bool syntax_error = false;
26710 cp_token *token = cp_lexer_peek_token (parser->lexer);
26711 enum rid keyword;
26713 if (token->type != CPP_NAME)
26715 cp_parser_error (parser, "expected identifier");
26716 break;
26718 keyword = C_RID_CODE (token->u.value);
26719 cp_lexer_consume_token (parser->lexer);
26720 switch (keyword)
26722 case RID_ASSIGN: property_assign = true; break;
26723 case RID_COPY: property_copy = true; break;
26724 case RID_NONATOMIC: property_nonatomic = true; break;
26725 case RID_READONLY: property_readonly = true; break;
26726 case RID_READWRITE: property_readwrite = true; break;
26727 case RID_RETAIN: property_retain = true; break;
26729 case RID_GETTER:
26730 case RID_SETTER:
26731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26733 if (keyword == RID_GETTER)
26734 cp_parser_error (parser,
26735 "missing %<=%> (after %<getter%> attribute)");
26736 else
26737 cp_parser_error (parser,
26738 "missing %<=%> (after %<setter%> attribute)");
26739 syntax_error = true;
26740 break;
26742 cp_lexer_consume_token (parser->lexer); /* eat the = */
26743 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26745 cp_parser_error (parser, "expected identifier");
26746 syntax_error = true;
26747 break;
26749 if (keyword == RID_SETTER)
26751 if (property_setter_ident != NULL_TREE)
26753 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26754 cp_lexer_consume_token (parser->lexer);
26756 else
26757 property_setter_ident = cp_parser_objc_selector (parser);
26758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26759 cp_parser_error (parser, "setter name must terminate with %<:%>");
26760 else
26761 cp_lexer_consume_token (parser->lexer);
26763 else
26765 if (property_getter_ident != NULL_TREE)
26767 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26768 cp_lexer_consume_token (parser->lexer);
26770 else
26771 property_getter_ident = cp_parser_objc_selector (parser);
26773 break;
26774 default:
26775 cp_parser_error (parser, "unknown property attribute");
26776 syntax_error = true;
26777 break;
26780 if (syntax_error)
26781 break;
26783 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26784 cp_lexer_consume_token (parser->lexer);
26785 else
26786 break;
26789 /* FIXME: "@property (setter, assign);" will generate a spurious
26790 "error: expected ‘)’ before ‘,’ token". This is because
26791 cp_parser_require, unlike the C counterpart, will produce an
26792 error even if we are in error recovery. */
26793 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26795 cp_parser_skip_to_closing_parenthesis (parser,
26796 /*recovering=*/true,
26797 /*or_comma=*/false,
26798 /*consume_paren=*/true);
26802 /* ... and the property declaration(s). */
26803 properties = cp_parser_objc_struct_declaration (parser);
26805 if (properties == error_mark_node)
26807 cp_parser_skip_to_end_of_statement (parser);
26808 /* If the next token is now a `;', consume it. */
26809 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26810 cp_lexer_consume_token (parser->lexer);
26811 return;
26814 if (properties == NULL_TREE)
26815 cp_parser_error (parser, "expected identifier");
26816 else
26818 /* Comma-separated properties are chained together in
26819 reverse order; add them one by one. */
26820 properties = nreverse (properties);
26822 for (; properties; properties = TREE_CHAIN (properties))
26823 objc_add_property_declaration (loc, copy_node (properties),
26824 property_readonly, property_readwrite,
26825 property_assign, property_retain,
26826 property_copy, property_nonatomic,
26827 property_getter_ident, property_setter_ident);
26830 cp_parser_consume_semicolon_at_end_of_statement (parser);
26833 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26835 objc-synthesize-declaration:
26836 @synthesize objc-synthesize-identifier-list ;
26838 objc-synthesize-identifier-list:
26839 objc-synthesize-identifier
26840 objc-synthesize-identifier-list, objc-synthesize-identifier
26842 objc-synthesize-identifier
26843 identifier
26844 identifier = identifier
26846 For example:
26847 @synthesize MyProperty;
26848 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26850 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26851 for C. Keep them in sync.
26853 static void
26854 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26856 tree list = NULL_TREE;
26857 location_t loc;
26858 loc = cp_lexer_peek_token (parser->lexer)->location;
26860 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26861 while (true)
26863 tree property, ivar;
26864 property = cp_parser_identifier (parser);
26865 if (property == error_mark_node)
26867 cp_parser_consume_semicolon_at_end_of_statement (parser);
26868 return;
26870 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26872 cp_lexer_consume_token (parser->lexer);
26873 ivar = cp_parser_identifier (parser);
26874 if (ivar == error_mark_node)
26876 cp_parser_consume_semicolon_at_end_of_statement (parser);
26877 return;
26880 else
26881 ivar = NULL_TREE;
26882 list = chainon (list, build_tree_list (ivar, property));
26883 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26884 cp_lexer_consume_token (parser->lexer);
26885 else
26886 break;
26888 cp_parser_consume_semicolon_at_end_of_statement (parser);
26889 objc_add_synthesize_declaration (loc, list);
26892 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26894 objc-dynamic-declaration:
26895 @dynamic identifier-list ;
26897 For example:
26898 @dynamic MyProperty;
26899 @dynamic MyProperty, AnotherProperty;
26901 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26902 for C. Keep them in sync.
26904 static void
26905 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26907 tree list = NULL_TREE;
26908 location_t loc;
26909 loc = cp_lexer_peek_token (parser->lexer)->location;
26911 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26912 while (true)
26914 tree property;
26915 property = cp_parser_identifier (parser);
26916 if (property == error_mark_node)
26918 cp_parser_consume_semicolon_at_end_of_statement (parser);
26919 return;
26921 list = chainon (list, build_tree_list (NULL, property));
26922 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26923 cp_lexer_consume_token (parser->lexer);
26924 else
26925 break;
26927 cp_parser_consume_semicolon_at_end_of_statement (parser);
26928 objc_add_dynamic_declaration (loc, list);
26932 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26934 /* Returns name of the next clause.
26935 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26936 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26937 returned and the token is consumed. */
26939 static pragma_omp_clause
26940 cp_parser_omp_clause_name (cp_parser *parser)
26942 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26945 result = PRAGMA_OMP_CLAUSE_IF;
26946 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26947 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26948 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26949 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26950 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26951 result = PRAGMA_OMP_CLAUSE_FOR;
26952 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26955 const char *p = IDENTIFIER_POINTER (id);
26957 switch (p[0])
26959 case 'a':
26960 if (!strcmp ("aligned", p))
26961 result = PRAGMA_OMP_CLAUSE_ALIGNED;
26962 break;
26963 case 'c':
26964 if (!strcmp ("collapse", p))
26965 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26966 else if (!strcmp ("copyin", p))
26967 result = PRAGMA_OMP_CLAUSE_COPYIN;
26968 else if (!strcmp ("copyprivate", p))
26969 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26970 break;
26971 case 'd':
26972 if (!strcmp ("depend", p))
26973 result = PRAGMA_OMP_CLAUSE_DEPEND;
26974 else if (!strcmp ("device", p))
26975 result = PRAGMA_OMP_CLAUSE_DEVICE;
26976 else if (!strcmp ("dist_schedule", p))
26977 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
26978 break;
26979 case 'f':
26980 if (!strcmp ("final", p))
26981 result = PRAGMA_OMP_CLAUSE_FINAL;
26982 else if (!strcmp ("firstprivate", p))
26983 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
26984 else if (!strcmp ("from", p))
26985 result = PRAGMA_OMP_CLAUSE_FROM;
26986 break;
26987 case 'i':
26988 if (!strcmp ("inbranch", p))
26989 result = PRAGMA_OMP_CLAUSE_INBRANCH;
26990 break;
26991 case 'l':
26992 if (!strcmp ("lastprivate", p))
26993 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
26994 else if (!strcmp ("linear", p))
26995 result = PRAGMA_OMP_CLAUSE_LINEAR;
26996 break;
26997 case 'm':
26998 if (!strcmp ("map", p))
26999 result = PRAGMA_OMP_CLAUSE_MAP;
27000 else if (!strcmp ("mergeable", p))
27001 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27002 else if (flag_cilkplus && !strcmp ("mask", p))
27003 result = PRAGMA_CILK_CLAUSE_MASK;
27004 break;
27005 case 'n':
27006 if (!strcmp ("notinbranch", p))
27007 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27008 else if (!strcmp ("nowait", p))
27009 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27010 else if (flag_cilkplus && !strcmp ("nomask", p))
27011 result = PRAGMA_CILK_CLAUSE_NOMASK;
27012 else if (!strcmp ("num_teams", p))
27013 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27014 else if (!strcmp ("num_threads", p))
27015 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27016 break;
27017 case 'o':
27018 if (!strcmp ("ordered", p))
27019 result = PRAGMA_OMP_CLAUSE_ORDERED;
27020 break;
27021 case 'p':
27022 if (!strcmp ("parallel", p))
27023 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27024 else if (!strcmp ("proc_bind", p))
27025 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27026 break;
27027 case 'r':
27028 if (!strcmp ("reduction", p))
27029 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27030 break;
27031 case 's':
27032 if (!strcmp ("safelen", p))
27033 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27034 else if (!strcmp ("schedule", p))
27035 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27036 else if (!strcmp ("sections", p))
27037 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27038 else if (!strcmp ("shared", p))
27039 result = PRAGMA_OMP_CLAUSE_SHARED;
27040 else if (!strcmp ("simdlen", p))
27041 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27042 break;
27043 case 't':
27044 if (!strcmp ("taskgroup", p))
27045 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27046 else if (!strcmp ("thread_limit", p))
27047 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27048 else if (!strcmp ("to", p))
27049 result = PRAGMA_OMP_CLAUSE_TO;
27050 break;
27051 case 'u':
27052 if (!strcmp ("uniform", p))
27053 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27054 else if (!strcmp ("untied", p))
27055 result = PRAGMA_OMP_CLAUSE_UNTIED;
27056 break;
27057 case 'v':
27058 if (flag_cilkplus && !strcmp ("vectorlength", p))
27059 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27060 break;
27064 if (result != PRAGMA_OMP_CLAUSE_NONE)
27065 cp_lexer_consume_token (parser->lexer);
27067 return result;
27070 /* Validate that a clause of the given type does not already exist. */
27072 static void
27073 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27074 const char *name, location_t location)
27076 tree c;
27078 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27079 if (OMP_CLAUSE_CODE (c) == code)
27081 error_at (location, "too many %qs clauses", name);
27082 break;
27086 /* OpenMP 2.5:
27087 variable-list:
27088 identifier
27089 variable-list , identifier
27091 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27092 colon). An opening parenthesis will have been consumed by the caller.
27094 If KIND is nonzero, create the appropriate node and install the decl
27095 in OMP_CLAUSE_DECL and add the node to the head of the list.
27097 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27098 return the list created.
27100 COLON can be NULL if only closing parenthesis should end the list,
27101 or pointer to bool which will receive false if the list is terminated
27102 by closing parenthesis or true if the list is terminated by colon. */
27104 static tree
27105 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27106 tree list, bool *colon)
27108 cp_token *token;
27109 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27110 if (colon)
27112 parser->colon_corrects_to_scope_p = false;
27113 *colon = false;
27115 while (1)
27117 tree name, decl;
27119 token = cp_lexer_peek_token (parser->lexer);
27120 name = cp_parser_id_expression (parser, /*template_p=*/false,
27121 /*check_dependency_p=*/true,
27122 /*template_p=*/NULL,
27123 /*declarator_p=*/false,
27124 /*optional_p=*/false);
27125 if (name == error_mark_node)
27126 goto skip_comma;
27128 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27129 if (decl == error_mark_node)
27130 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27131 token->location);
27132 else if (kind != 0)
27134 switch (kind)
27136 case OMP_CLAUSE_MAP:
27137 case OMP_CLAUSE_FROM:
27138 case OMP_CLAUSE_TO:
27139 case OMP_CLAUSE_DEPEND:
27140 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27142 tree low_bound = NULL_TREE, length = NULL_TREE;
27144 parser->colon_corrects_to_scope_p = false;
27145 cp_lexer_consume_token (parser->lexer);
27146 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27147 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27148 NULL);
27149 if (!colon)
27150 parser->colon_corrects_to_scope_p
27151 = saved_colon_corrects_to_scope_p;
27152 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27153 length = integer_one_node;
27154 else
27156 /* Look for `:'. */
27157 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27158 goto skip_comma;
27159 if (!cp_lexer_next_token_is (parser->lexer,
27160 CPP_CLOSE_SQUARE))
27161 length = cp_parser_expression (parser,
27162 /*cast_p=*/false,
27163 NULL);
27165 /* Look for the closing `]'. */
27166 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27167 RT_CLOSE_SQUARE))
27168 goto skip_comma;
27169 decl = tree_cons (low_bound, length, decl);
27171 break;
27172 default:
27173 break;
27176 tree u = build_omp_clause (token->location, kind);
27177 OMP_CLAUSE_DECL (u) = decl;
27178 OMP_CLAUSE_CHAIN (u) = list;
27179 list = u;
27181 else
27182 list = tree_cons (decl, NULL_TREE, list);
27184 get_comma:
27185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27186 break;
27187 cp_lexer_consume_token (parser->lexer);
27190 if (colon)
27191 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27193 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27195 *colon = true;
27196 cp_parser_require (parser, CPP_COLON, RT_COLON);
27197 return list;
27200 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27202 int ending;
27204 /* Try to resync to an unnested comma. Copied from
27205 cp_parser_parenthesized_expression_list. */
27206 skip_comma:
27207 if (colon)
27208 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27209 ending = cp_parser_skip_to_closing_parenthesis (parser,
27210 /*recovering=*/true,
27211 /*or_comma=*/true,
27212 /*consume_paren=*/true);
27213 if (ending < 0)
27214 goto get_comma;
27217 return list;
27220 /* Similarly, but expect leading and trailing parenthesis. This is a very
27221 common case for omp clauses. */
27223 static tree
27224 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27226 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27227 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27228 return list;
27231 /* OpenMP 3.0:
27232 collapse ( constant-expression ) */
27234 static tree
27235 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27237 tree c, num;
27238 location_t loc;
27239 HOST_WIDE_INT n;
27241 loc = cp_lexer_peek_token (parser->lexer)->location;
27242 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27243 return list;
27245 num = cp_parser_constant_expression (parser, false, NULL);
27247 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27249 /*or_comma=*/false,
27250 /*consume_paren=*/true);
27252 if (num == error_mark_node)
27253 return list;
27254 num = fold_non_dependent_expr (num);
27255 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27256 || !tree_fits_shwi_p (num)
27257 || (n = tree_to_shwi (num)) <= 0
27258 || (int) n != n)
27260 error_at (loc, "collapse argument needs positive constant integer expression");
27261 return list;
27264 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27265 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27266 OMP_CLAUSE_CHAIN (c) = list;
27267 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27269 return c;
27272 /* OpenMP 2.5:
27273 default ( shared | none ) */
27275 static tree
27276 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27278 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27279 tree c;
27281 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27282 return list;
27283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27285 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27286 const char *p = IDENTIFIER_POINTER (id);
27288 switch (p[0])
27290 case 'n':
27291 if (strcmp ("none", p) != 0)
27292 goto invalid_kind;
27293 kind = OMP_CLAUSE_DEFAULT_NONE;
27294 break;
27296 case 's':
27297 if (strcmp ("shared", p) != 0)
27298 goto invalid_kind;
27299 kind = OMP_CLAUSE_DEFAULT_SHARED;
27300 break;
27302 default:
27303 goto invalid_kind;
27306 cp_lexer_consume_token (parser->lexer);
27308 else
27310 invalid_kind:
27311 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27314 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27315 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27316 /*or_comma=*/false,
27317 /*consume_paren=*/true);
27319 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27320 return list;
27322 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27323 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27324 OMP_CLAUSE_CHAIN (c) = list;
27325 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27327 return c;
27330 /* OpenMP 3.1:
27331 final ( expression ) */
27333 static tree
27334 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27336 tree t, c;
27338 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27339 return list;
27341 t = cp_parser_condition (parser);
27343 if (t == error_mark_node
27344 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27345 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27346 /*or_comma=*/false,
27347 /*consume_paren=*/true);
27349 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27351 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27352 OMP_CLAUSE_FINAL_EXPR (c) = t;
27353 OMP_CLAUSE_CHAIN (c) = list;
27355 return c;
27358 /* OpenMP 2.5:
27359 if ( expression ) */
27361 static tree
27362 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27364 tree t, c;
27366 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27367 return list;
27369 t = cp_parser_condition (parser);
27371 if (t == error_mark_node
27372 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27373 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27374 /*or_comma=*/false,
27375 /*consume_paren=*/true);
27377 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27379 c = build_omp_clause (location, OMP_CLAUSE_IF);
27380 OMP_CLAUSE_IF_EXPR (c) = t;
27381 OMP_CLAUSE_CHAIN (c) = list;
27383 return c;
27386 /* OpenMP 3.1:
27387 mergeable */
27389 static tree
27390 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27391 tree list, location_t location)
27393 tree c;
27395 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27396 location);
27398 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27399 OMP_CLAUSE_CHAIN (c) = list;
27400 return c;
27403 /* OpenMP 2.5:
27404 nowait */
27406 static tree
27407 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27408 tree list, location_t location)
27410 tree c;
27412 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27414 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27415 OMP_CLAUSE_CHAIN (c) = list;
27416 return c;
27419 /* OpenMP 2.5:
27420 num_threads ( expression ) */
27422 static tree
27423 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27424 location_t location)
27426 tree t, c;
27428 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27429 return list;
27431 t = cp_parser_expression (parser, false, NULL);
27433 if (t == error_mark_node
27434 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27436 /*or_comma=*/false,
27437 /*consume_paren=*/true);
27439 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27440 "num_threads", location);
27442 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27443 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27444 OMP_CLAUSE_CHAIN (c) = list;
27446 return c;
27449 /* OpenMP 2.5:
27450 ordered */
27452 static tree
27453 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27454 tree list, location_t location)
27456 tree c;
27458 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27459 "ordered", location);
27461 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27462 OMP_CLAUSE_CHAIN (c) = list;
27463 return c;
27466 /* OpenMP 2.5:
27467 reduction ( reduction-operator : variable-list )
27469 reduction-operator:
27470 One of: + * - & ^ | && ||
27472 OpenMP 3.1:
27474 reduction-operator:
27475 One of: + * - & ^ | && || min max
27477 OpenMP 4.0:
27479 reduction-operator:
27480 One of: + * - & ^ | && ||
27481 id-expression */
27483 static tree
27484 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27486 enum tree_code code = ERROR_MARK;
27487 tree nlist, c, id = NULL_TREE;
27489 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27490 return list;
27492 switch (cp_lexer_peek_token (parser->lexer)->type)
27494 case CPP_PLUS: code = PLUS_EXPR; break;
27495 case CPP_MULT: code = MULT_EXPR; break;
27496 case CPP_MINUS: code = MINUS_EXPR; break;
27497 case CPP_AND: code = BIT_AND_EXPR; break;
27498 case CPP_XOR: code = BIT_XOR_EXPR; break;
27499 case CPP_OR: code = BIT_IOR_EXPR; break;
27500 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27501 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27502 default: break;
27505 if (code != ERROR_MARK)
27506 cp_lexer_consume_token (parser->lexer);
27507 else
27509 bool saved_colon_corrects_to_scope_p;
27510 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27511 parser->colon_corrects_to_scope_p = false;
27512 id = cp_parser_id_expression (parser, /*template_p=*/false,
27513 /*check_dependency_p=*/true,
27514 /*template_p=*/NULL,
27515 /*declarator_p=*/false,
27516 /*optional_p=*/false);
27517 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27518 if (identifier_p (id))
27520 const char *p = IDENTIFIER_POINTER (id);
27522 if (strcmp (p, "min") == 0)
27523 code = MIN_EXPR;
27524 else if (strcmp (p, "max") == 0)
27525 code = MAX_EXPR;
27526 else if (id == ansi_opname (PLUS_EXPR))
27527 code = PLUS_EXPR;
27528 else if (id == ansi_opname (MULT_EXPR))
27529 code = MULT_EXPR;
27530 else if (id == ansi_opname (MINUS_EXPR))
27531 code = MINUS_EXPR;
27532 else if (id == ansi_opname (BIT_AND_EXPR))
27533 code = BIT_AND_EXPR;
27534 else if (id == ansi_opname (BIT_IOR_EXPR))
27535 code = BIT_IOR_EXPR;
27536 else if (id == ansi_opname (BIT_XOR_EXPR))
27537 code = BIT_XOR_EXPR;
27538 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27539 code = TRUTH_ANDIF_EXPR;
27540 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27541 code = TRUTH_ORIF_EXPR;
27542 id = omp_reduction_id (code, id, NULL_TREE);
27543 tree scope = parser->scope;
27544 if (scope)
27545 id = build_qualified_name (NULL_TREE, scope, id, false);
27546 parser->scope = NULL_TREE;
27547 parser->qualifying_scope = NULL_TREE;
27548 parser->object_scope = NULL_TREE;
27550 else
27552 error ("invalid reduction-identifier");
27553 resync_fail:
27554 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27555 /*or_comma=*/false,
27556 /*consume_paren=*/true);
27557 return list;
27561 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27562 goto resync_fail;
27564 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27565 NULL);
27566 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27568 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27569 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27572 return nlist;
27575 /* OpenMP 2.5:
27576 schedule ( schedule-kind )
27577 schedule ( schedule-kind , expression )
27579 schedule-kind:
27580 static | dynamic | guided | runtime | auto */
27582 static tree
27583 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27585 tree c, t;
27587 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27588 return list;
27590 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27595 const char *p = IDENTIFIER_POINTER (id);
27597 switch (p[0])
27599 case 'd':
27600 if (strcmp ("dynamic", p) != 0)
27601 goto invalid_kind;
27602 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27603 break;
27605 case 'g':
27606 if (strcmp ("guided", p) != 0)
27607 goto invalid_kind;
27608 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27609 break;
27611 case 'r':
27612 if (strcmp ("runtime", p) != 0)
27613 goto invalid_kind;
27614 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27615 break;
27617 default:
27618 goto invalid_kind;
27621 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27622 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27623 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27624 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27625 else
27626 goto invalid_kind;
27627 cp_lexer_consume_token (parser->lexer);
27629 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27631 cp_token *token;
27632 cp_lexer_consume_token (parser->lexer);
27634 token = cp_lexer_peek_token (parser->lexer);
27635 t = cp_parser_assignment_expression (parser, false, NULL);
27637 if (t == error_mark_node)
27638 goto resync_fail;
27639 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27640 error_at (token->location, "schedule %<runtime%> does not take "
27641 "a %<chunk_size%> parameter");
27642 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27643 error_at (token->location, "schedule %<auto%> does not take "
27644 "a %<chunk_size%> parameter");
27645 else
27646 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27648 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27649 goto resync_fail;
27651 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27652 goto resync_fail;
27654 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27655 OMP_CLAUSE_CHAIN (c) = list;
27656 return c;
27658 invalid_kind:
27659 cp_parser_error (parser, "invalid schedule kind");
27660 resync_fail:
27661 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27662 /*or_comma=*/false,
27663 /*consume_paren=*/true);
27664 return list;
27667 /* OpenMP 3.0:
27668 untied */
27670 static tree
27671 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27672 tree list, location_t location)
27674 tree c;
27676 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27678 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27679 OMP_CLAUSE_CHAIN (c) = list;
27680 return c;
27683 /* OpenMP 4.0:
27684 inbranch
27685 notinbranch */
27687 static tree
27688 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27689 tree list, location_t location)
27691 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27692 tree c = build_omp_clause (location, code);
27693 OMP_CLAUSE_CHAIN (c) = list;
27694 return c;
27697 /* OpenMP 4.0:
27698 parallel
27700 sections
27701 taskgroup */
27703 static tree
27704 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27705 enum omp_clause_code code,
27706 tree list, location_t location)
27708 tree c = build_omp_clause (location, code);
27709 OMP_CLAUSE_CHAIN (c) = list;
27710 return c;
27713 /* OpenMP 4.0:
27714 num_teams ( expression ) */
27716 static tree
27717 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27718 location_t location)
27720 tree t, c;
27722 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27723 return list;
27725 t = cp_parser_expression (parser, false, NULL);
27727 if (t == error_mark_node
27728 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27730 /*or_comma=*/false,
27731 /*consume_paren=*/true);
27733 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27734 "num_teams", location);
27736 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27737 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27738 OMP_CLAUSE_CHAIN (c) = list;
27740 return c;
27743 /* OpenMP 4.0:
27744 thread_limit ( expression ) */
27746 static tree
27747 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27748 location_t location)
27750 tree t, c;
27752 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27753 return list;
27755 t = cp_parser_expression (parser, false, NULL);
27757 if (t == error_mark_node
27758 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27760 /*or_comma=*/false,
27761 /*consume_paren=*/true);
27763 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27764 "thread_limit", location);
27766 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27767 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27768 OMP_CLAUSE_CHAIN (c) = list;
27770 return c;
27773 /* OpenMP 4.0:
27774 aligned ( variable-list )
27775 aligned ( variable-list : constant-expression ) */
27777 static tree
27778 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27780 tree nlist, c, alignment = NULL_TREE;
27781 bool colon;
27783 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27784 return list;
27786 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27787 &colon);
27789 if (colon)
27791 alignment = cp_parser_constant_expression (parser, false, NULL);
27793 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27794 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27795 /*or_comma=*/false,
27796 /*consume_paren=*/true);
27798 if (alignment == error_mark_node)
27799 alignment = NULL_TREE;
27802 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27803 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27805 return nlist;
27808 /* OpenMP 4.0:
27809 linear ( variable-list )
27810 linear ( variable-list : expression ) */
27812 static tree
27813 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
27814 bool is_cilk_simd_fn)
27816 tree nlist, c, step = integer_one_node;
27817 bool colon;
27819 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27820 return list;
27822 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27823 &colon);
27825 if (colon)
27827 step = cp_parser_expression (parser, false, NULL);
27829 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
27831 sorry ("using parameters for %<linear%> step is not supported yet");
27832 step = integer_one_node;
27834 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27835 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27836 /*or_comma=*/false,
27837 /*consume_paren=*/true);
27839 if (step == error_mark_node)
27840 return list;
27843 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27844 OMP_CLAUSE_LINEAR_STEP (c) = step;
27846 return nlist;
27849 /* OpenMP 4.0:
27850 safelen ( constant-expression ) */
27852 static tree
27853 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27854 location_t location)
27856 tree t, c;
27858 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27859 return list;
27861 t = cp_parser_constant_expression (parser, false, NULL);
27863 if (t == error_mark_node
27864 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27865 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27866 /*or_comma=*/false,
27867 /*consume_paren=*/true);
27869 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27871 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27872 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27873 OMP_CLAUSE_CHAIN (c) = list;
27875 return c;
27878 /* OpenMP 4.0:
27879 simdlen ( constant-expression ) */
27881 static tree
27882 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27883 location_t location)
27885 tree t, c;
27887 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27888 return list;
27890 t = cp_parser_constant_expression (parser, false, NULL);
27892 if (t == error_mark_node
27893 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27895 /*or_comma=*/false,
27896 /*consume_paren=*/true);
27898 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27900 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27901 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27902 OMP_CLAUSE_CHAIN (c) = list;
27904 return c;
27907 /* OpenMP 4.0:
27908 depend ( depend-kind : variable-list )
27910 depend-kind:
27911 in | out | inout */
27913 static tree
27914 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27916 tree nlist, c;
27917 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27920 return list;
27922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27924 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27925 const char *p = IDENTIFIER_POINTER (id);
27927 if (strcmp ("in", p) == 0)
27928 kind = OMP_CLAUSE_DEPEND_IN;
27929 else if (strcmp ("inout", p) == 0)
27930 kind = OMP_CLAUSE_DEPEND_INOUT;
27931 else if (strcmp ("out", p) == 0)
27932 kind = OMP_CLAUSE_DEPEND_OUT;
27933 else
27934 goto invalid_kind;
27936 else
27937 goto invalid_kind;
27939 cp_lexer_consume_token (parser->lexer);
27940 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27941 goto resync_fail;
27943 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27944 NULL);
27946 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27947 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27949 return nlist;
27951 invalid_kind:
27952 cp_parser_error (parser, "invalid depend kind");
27953 resync_fail:
27954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27955 /*or_comma=*/false,
27956 /*consume_paren=*/true);
27957 return list;
27960 /* OpenMP 4.0:
27961 map ( map-kind : variable-list )
27962 map ( variable-list )
27964 map-kind:
27965 alloc | to | from | tofrom */
27967 static tree
27968 cp_parser_omp_clause_map (cp_parser *parser, tree list)
27970 tree nlist, c;
27971 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
27973 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27974 return list;
27976 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
27977 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
27979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27980 const char *p = IDENTIFIER_POINTER (id);
27982 if (strcmp ("alloc", p) == 0)
27983 kind = OMP_CLAUSE_MAP_ALLOC;
27984 else if (strcmp ("to", p) == 0)
27985 kind = OMP_CLAUSE_MAP_TO;
27986 else if (strcmp ("from", p) == 0)
27987 kind = OMP_CLAUSE_MAP_FROM;
27988 else if (strcmp ("tofrom", p) == 0)
27989 kind = OMP_CLAUSE_MAP_TOFROM;
27990 else
27992 cp_parser_error (parser, "invalid map kind");
27993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27994 /*or_comma=*/false,
27995 /*consume_paren=*/true);
27996 return list;
27998 cp_lexer_consume_token (parser->lexer);
27999 cp_lexer_consume_token (parser->lexer);
28002 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28003 NULL);
28005 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28006 OMP_CLAUSE_MAP_KIND (c) = kind;
28008 return nlist;
28011 /* OpenMP 4.0:
28012 device ( expression ) */
28014 static tree
28015 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28016 location_t location)
28018 tree t, c;
28020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28021 return list;
28023 t = cp_parser_expression (parser, false, NULL);
28025 if (t == error_mark_node
28026 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28027 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28028 /*or_comma=*/false,
28029 /*consume_paren=*/true);
28031 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28032 "device", location);
28034 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28035 OMP_CLAUSE_DEVICE_ID (c) = t;
28036 OMP_CLAUSE_CHAIN (c) = list;
28038 return c;
28041 /* OpenMP 4.0:
28042 dist_schedule ( static )
28043 dist_schedule ( static , expression ) */
28045 static tree
28046 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28047 location_t location)
28049 tree c, t;
28051 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28052 return list;
28054 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28056 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28057 goto invalid_kind;
28058 cp_lexer_consume_token (parser->lexer);
28060 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28062 cp_lexer_consume_token (parser->lexer);
28064 t = cp_parser_assignment_expression (parser, false, NULL);
28066 if (t == error_mark_node)
28067 goto resync_fail;
28068 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28071 goto resync_fail;
28073 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28074 goto resync_fail;
28076 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28077 location);
28078 OMP_CLAUSE_CHAIN (c) = list;
28079 return c;
28081 invalid_kind:
28082 cp_parser_error (parser, "invalid dist_schedule kind");
28083 resync_fail:
28084 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28085 /*or_comma=*/false,
28086 /*consume_paren=*/true);
28087 return list;
28090 /* OpenMP 4.0:
28091 proc_bind ( proc-bind-kind )
28093 proc-bind-kind:
28094 master | close | spread */
28096 static tree
28097 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28098 location_t location)
28100 tree c;
28101 enum omp_clause_proc_bind_kind kind;
28103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28104 return list;
28106 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28108 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28109 const char *p = IDENTIFIER_POINTER (id);
28111 if (strcmp ("master", p) == 0)
28112 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28113 else if (strcmp ("close", p) == 0)
28114 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28115 else if (strcmp ("spread", p) == 0)
28116 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28117 else
28118 goto invalid_kind;
28120 else
28121 goto invalid_kind;
28123 cp_lexer_consume_token (parser->lexer);
28124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28125 goto resync_fail;
28127 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28128 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28129 location);
28130 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28131 OMP_CLAUSE_CHAIN (c) = list;
28132 return c;
28134 invalid_kind:
28135 cp_parser_error (parser, "invalid depend kind");
28136 resync_fail:
28137 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28138 /*or_comma=*/false,
28139 /*consume_paren=*/true);
28140 return list;
28143 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28144 is a bitmask in MASK. Return the list of clauses found; the result
28145 of clause default goes in *pdefault. */
28147 static tree
28148 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28149 const char *where, cp_token *pragma_tok,
28150 bool finish_p = true)
28152 tree clauses = NULL;
28153 bool first = true;
28154 cp_token *token = NULL;
28155 bool cilk_simd_fn = false;
28157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28159 pragma_omp_clause c_kind;
28160 const char *c_name;
28161 tree prev = clauses;
28163 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28164 cp_lexer_consume_token (parser->lexer);
28166 token = cp_lexer_peek_token (parser->lexer);
28167 c_kind = cp_parser_omp_clause_name (parser);
28169 switch (c_kind)
28171 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28172 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28173 token->location);
28174 c_name = "collapse";
28175 break;
28176 case PRAGMA_OMP_CLAUSE_COPYIN:
28177 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28178 c_name = "copyin";
28179 break;
28180 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28181 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28182 clauses);
28183 c_name = "copyprivate";
28184 break;
28185 case PRAGMA_OMP_CLAUSE_DEFAULT:
28186 clauses = cp_parser_omp_clause_default (parser, clauses,
28187 token->location);
28188 c_name = "default";
28189 break;
28190 case PRAGMA_OMP_CLAUSE_FINAL:
28191 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28192 c_name = "final";
28193 break;
28194 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28195 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28196 clauses);
28197 c_name = "firstprivate";
28198 break;
28199 case PRAGMA_OMP_CLAUSE_IF:
28200 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28201 c_name = "if";
28202 break;
28203 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28204 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28205 clauses);
28206 c_name = "lastprivate";
28207 break;
28208 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28209 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28210 token->location);
28211 c_name = "mergeable";
28212 break;
28213 case PRAGMA_OMP_CLAUSE_NOWAIT:
28214 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28215 c_name = "nowait";
28216 break;
28217 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28218 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28219 token->location);
28220 c_name = "num_threads";
28221 break;
28222 case PRAGMA_OMP_CLAUSE_ORDERED:
28223 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28224 token->location);
28225 c_name = "ordered";
28226 break;
28227 case PRAGMA_OMP_CLAUSE_PRIVATE:
28228 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28229 clauses);
28230 c_name = "private";
28231 break;
28232 case PRAGMA_OMP_CLAUSE_REDUCTION:
28233 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28234 c_name = "reduction";
28235 break;
28236 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28237 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28238 token->location);
28239 c_name = "schedule";
28240 break;
28241 case PRAGMA_OMP_CLAUSE_SHARED:
28242 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28243 clauses);
28244 c_name = "shared";
28245 break;
28246 case PRAGMA_OMP_CLAUSE_UNTIED:
28247 clauses = cp_parser_omp_clause_untied (parser, clauses,
28248 token->location);
28249 c_name = "untied";
28250 break;
28251 case PRAGMA_OMP_CLAUSE_INBRANCH:
28252 case PRAGMA_CILK_CLAUSE_MASK:
28253 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28254 clauses, token->location);
28255 c_name = "inbranch";
28256 break;
28257 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28258 case PRAGMA_CILK_CLAUSE_NOMASK:
28259 clauses = cp_parser_omp_clause_branch (parser,
28260 OMP_CLAUSE_NOTINBRANCH,
28261 clauses, token->location);
28262 c_name = "notinbranch";
28263 break;
28264 case PRAGMA_OMP_CLAUSE_PARALLEL:
28265 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28266 clauses, token->location);
28267 c_name = "parallel";
28268 if (!first)
28270 clause_not_first:
28271 error_at (token->location, "%qs must be the first clause of %qs",
28272 c_name, where);
28273 clauses = prev;
28275 break;
28276 case PRAGMA_OMP_CLAUSE_FOR:
28277 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28278 clauses, token->location);
28279 c_name = "for";
28280 if (!first)
28281 goto clause_not_first;
28282 break;
28283 case PRAGMA_OMP_CLAUSE_SECTIONS:
28284 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28285 clauses, token->location);
28286 c_name = "sections";
28287 if (!first)
28288 goto clause_not_first;
28289 break;
28290 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28291 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28292 clauses, token->location);
28293 c_name = "taskgroup";
28294 if (!first)
28295 goto clause_not_first;
28296 break;
28297 case PRAGMA_OMP_CLAUSE_TO:
28298 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28299 clauses);
28300 c_name = "to";
28301 break;
28302 case PRAGMA_OMP_CLAUSE_FROM:
28303 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28304 clauses);
28305 c_name = "from";
28306 break;
28307 case PRAGMA_OMP_CLAUSE_UNIFORM:
28308 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28309 clauses);
28310 c_name = "uniform";
28311 break;
28312 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28313 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28314 token->location);
28315 c_name = "num_teams";
28316 break;
28317 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28318 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28319 token->location);
28320 c_name = "thread_limit";
28321 break;
28322 case PRAGMA_OMP_CLAUSE_ALIGNED:
28323 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28324 c_name = "aligned";
28325 break;
28326 case PRAGMA_OMP_CLAUSE_LINEAR:
28327 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28328 cilk_simd_fn = true;
28329 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28330 c_name = "linear";
28331 break;
28332 case PRAGMA_OMP_CLAUSE_DEPEND:
28333 clauses = cp_parser_omp_clause_depend (parser, clauses);
28334 c_name = "depend";
28335 break;
28336 case PRAGMA_OMP_CLAUSE_MAP:
28337 clauses = cp_parser_omp_clause_map (parser, clauses);
28338 c_name = "map";
28339 break;
28340 case PRAGMA_OMP_CLAUSE_DEVICE:
28341 clauses = cp_parser_omp_clause_device (parser, clauses,
28342 token->location);
28343 c_name = "device";
28344 break;
28345 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28346 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28347 token->location);
28348 c_name = "dist_schedule";
28349 break;
28350 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28351 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28352 token->location);
28353 c_name = "proc_bind";
28354 break;
28355 case PRAGMA_OMP_CLAUSE_SAFELEN:
28356 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28357 token->location);
28358 c_name = "safelen";
28359 break;
28360 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28361 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28362 token->location);
28363 c_name = "simdlen";
28364 break;
28365 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28366 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28367 c_name = "simdlen";
28368 break;
28369 default:
28370 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28371 goto saw_error;
28374 first = false;
28376 if (((mask >> c_kind) & 1) == 0)
28378 /* Remove the invalid clause(s) from the list to avoid
28379 confusing the rest of the compiler. */
28380 clauses = prev;
28381 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28384 saw_error:
28385 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28386 no reason to skip to the end. */
28387 if (!(flag_cilkplus && pragma_tok == NULL))
28388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28389 if (finish_p)
28390 return finish_omp_clauses (clauses);
28391 return clauses;
28394 /* OpenMP 2.5:
28395 structured-block:
28396 statement
28398 In practice, we're also interested in adding the statement to an
28399 outer node. So it is convenient if we work around the fact that
28400 cp_parser_statement calls add_stmt. */
28402 static unsigned
28403 cp_parser_begin_omp_structured_block (cp_parser *parser)
28405 unsigned save = parser->in_statement;
28407 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28408 This preserves the "not within loop or switch" style error messages
28409 for nonsense cases like
28410 void foo() {
28411 #pragma omp single
28412 break;
28415 if (parser->in_statement)
28416 parser->in_statement = IN_OMP_BLOCK;
28418 return save;
28421 static void
28422 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28424 parser->in_statement = save;
28427 static tree
28428 cp_parser_omp_structured_block (cp_parser *parser)
28430 tree stmt = begin_omp_structured_block ();
28431 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28433 cp_parser_statement (parser, NULL_TREE, false, NULL);
28435 cp_parser_end_omp_structured_block (parser, save);
28436 return finish_omp_structured_block (stmt);
28439 /* OpenMP 2.5:
28440 # pragma omp atomic new-line
28441 expression-stmt
28443 expression-stmt:
28444 x binop= expr | x++ | ++x | x-- | --x
28445 binop:
28446 +, *, -, /, &, ^, |, <<, >>
28448 where x is an lvalue expression with scalar type.
28450 OpenMP 3.1:
28451 # pragma omp atomic new-line
28452 update-stmt
28454 # pragma omp atomic read new-line
28455 read-stmt
28457 # pragma omp atomic write new-line
28458 write-stmt
28460 # pragma omp atomic update new-line
28461 update-stmt
28463 # pragma omp atomic capture new-line
28464 capture-stmt
28466 # pragma omp atomic capture new-line
28467 capture-block
28469 read-stmt:
28470 v = x
28471 write-stmt:
28472 x = expr
28473 update-stmt:
28474 expression-stmt | x = x binop expr
28475 capture-stmt:
28476 v = expression-stmt
28477 capture-block:
28478 { v = x; update-stmt; } | { update-stmt; v = x; }
28480 OpenMP 4.0:
28481 update-stmt:
28482 expression-stmt | x = x binop expr | x = expr binop x
28483 capture-stmt:
28484 v = update-stmt
28485 capture-block:
28486 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28488 where x and v are lvalue expressions with scalar type. */
28490 static void
28491 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28493 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28494 tree rhs1 = NULL_TREE, orig_lhs;
28495 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28496 bool structured_block = false;
28497 bool seq_cst = false;
28499 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28501 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28502 const char *p = IDENTIFIER_POINTER (id);
28504 if (!strcmp (p, "read"))
28505 code = OMP_ATOMIC_READ;
28506 else if (!strcmp (p, "write"))
28507 code = NOP_EXPR;
28508 else if (!strcmp (p, "update"))
28509 code = OMP_ATOMIC;
28510 else if (!strcmp (p, "capture"))
28511 code = OMP_ATOMIC_CAPTURE_NEW;
28512 else
28513 p = NULL;
28514 if (p)
28515 cp_lexer_consume_token (parser->lexer);
28518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28521 const char *p = IDENTIFIER_POINTER (id);
28523 if (!strcmp (p, "seq_cst"))
28525 seq_cst = true;
28526 cp_lexer_consume_token (parser->lexer);
28529 cp_parser_require_pragma_eol (parser, pragma_tok);
28531 switch (code)
28533 case OMP_ATOMIC_READ:
28534 case NOP_EXPR: /* atomic write */
28535 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28536 /*cast_p=*/false, NULL);
28537 if (v == error_mark_node)
28538 goto saw_error;
28539 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28540 goto saw_error;
28541 if (code == NOP_EXPR)
28542 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28543 else
28544 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28545 /*cast_p=*/false, NULL);
28546 if (lhs == error_mark_node)
28547 goto saw_error;
28548 if (code == NOP_EXPR)
28550 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28551 opcode. */
28552 code = OMP_ATOMIC;
28553 rhs = lhs;
28554 lhs = v;
28555 v = NULL_TREE;
28557 goto done;
28558 case OMP_ATOMIC_CAPTURE_NEW:
28559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28561 cp_lexer_consume_token (parser->lexer);
28562 structured_block = true;
28564 else
28566 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28567 /*cast_p=*/false, NULL);
28568 if (v == error_mark_node)
28569 goto saw_error;
28570 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28571 goto saw_error;
28573 default:
28574 break;
28577 restart:
28578 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28579 /*cast_p=*/false, NULL);
28580 orig_lhs = lhs;
28581 switch (TREE_CODE (lhs))
28583 case ERROR_MARK:
28584 goto saw_error;
28586 case POSTINCREMENT_EXPR:
28587 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28588 code = OMP_ATOMIC_CAPTURE_OLD;
28589 /* FALLTHROUGH */
28590 case PREINCREMENT_EXPR:
28591 lhs = TREE_OPERAND (lhs, 0);
28592 opcode = PLUS_EXPR;
28593 rhs = integer_one_node;
28594 break;
28596 case POSTDECREMENT_EXPR:
28597 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28598 code = OMP_ATOMIC_CAPTURE_OLD;
28599 /* FALLTHROUGH */
28600 case PREDECREMENT_EXPR:
28601 lhs = TREE_OPERAND (lhs, 0);
28602 opcode = MINUS_EXPR;
28603 rhs = integer_one_node;
28604 break;
28606 case COMPOUND_EXPR:
28607 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28608 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28609 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28610 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28611 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28612 (TREE_OPERAND (lhs, 1), 0), 0)))
28613 == BOOLEAN_TYPE)
28614 /* Undo effects of boolean_increment for post {in,de}crement. */
28615 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28616 /* FALLTHRU */
28617 case MODIFY_EXPR:
28618 if (TREE_CODE (lhs) == MODIFY_EXPR
28619 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28621 /* Undo effects of boolean_increment. */
28622 if (integer_onep (TREE_OPERAND (lhs, 1)))
28624 /* This is pre or post increment. */
28625 rhs = TREE_OPERAND (lhs, 1);
28626 lhs = TREE_OPERAND (lhs, 0);
28627 opcode = NOP_EXPR;
28628 if (code == OMP_ATOMIC_CAPTURE_NEW
28629 && !structured_block
28630 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28631 code = OMP_ATOMIC_CAPTURE_OLD;
28632 break;
28635 /* FALLTHRU */
28636 default:
28637 switch (cp_lexer_peek_token (parser->lexer)->type)
28639 case CPP_MULT_EQ:
28640 opcode = MULT_EXPR;
28641 break;
28642 case CPP_DIV_EQ:
28643 opcode = TRUNC_DIV_EXPR;
28644 break;
28645 case CPP_PLUS_EQ:
28646 opcode = PLUS_EXPR;
28647 break;
28648 case CPP_MINUS_EQ:
28649 opcode = MINUS_EXPR;
28650 break;
28651 case CPP_LSHIFT_EQ:
28652 opcode = LSHIFT_EXPR;
28653 break;
28654 case CPP_RSHIFT_EQ:
28655 opcode = RSHIFT_EXPR;
28656 break;
28657 case CPP_AND_EQ:
28658 opcode = BIT_AND_EXPR;
28659 break;
28660 case CPP_OR_EQ:
28661 opcode = BIT_IOR_EXPR;
28662 break;
28663 case CPP_XOR_EQ:
28664 opcode = BIT_XOR_EXPR;
28665 break;
28666 case CPP_EQ:
28667 enum cp_parser_prec oprec;
28668 cp_token *token;
28669 cp_lexer_consume_token (parser->lexer);
28670 cp_parser_parse_tentatively (parser);
28671 rhs1 = cp_parser_simple_cast_expression (parser);
28672 if (rhs1 == error_mark_node)
28674 cp_parser_abort_tentative_parse (parser);
28675 cp_parser_simple_cast_expression (parser);
28676 goto saw_error;
28678 token = cp_lexer_peek_token (parser->lexer);
28679 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28681 cp_parser_abort_tentative_parse (parser);
28682 cp_parser_parse_tentatively (parser);
28683 rhs = cp_parser_binary_expression (parser, false, true,
28684 PREC_NOT_OPERATOR, NULL);
28685 if (rhs == error_mark_node)
28687 cp_parser_abort_tentative_parse (parser);
28688 cp_parser_binary_expression (parser, false, true,
28689 PREC_NOT_OPERATOR, NULL);
28690 goto saw_error;
28692 switch (TREE_CODE (rhs))
28694 case MULT_EXPR:
28695 case TRUNC_DIV_EXPR:
28696 case PLUS_EXPR:
28697 case MINUS_EXPR:
28698 case LSHIFT_EXPR:
28699 case RSHIFT_EXPR:
28700 case BIT_AND_EXPR:
28701 case BIT_IOR_EXPR:
28702 case BIT_XOR_EXPR:
28703 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28705 if (cp_parser_parse_definitely (parser))
28707 opcode = TREE_CODE (rhs);
28708 rhs1 = TREE_OPERAND (rhs, 0);
28709 rhs = TREE_OPERAND (rhs, 1);
28710 goto stmt_done;
28712 else
28713 goto saw_error;
28715 break;
28716 default:
28717 break;
28719 cp_parser_abort_tentative_parse (parser);
28720 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28722 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28723 if (rhs == error_mark_node)
28724 goto saw_error;
28725 opcode = NOP_EXPR;
28726 rhs1 = NULL_TREE;
28727 goto stmt_done;
28729 cp_parser_error (parser,
28730 "invalid form of %<#pragma omp atomic%>");
28731 goto saw_error;
28733 if (!cp_parser_parse_definitely (parser))
28734 goto saw_error;
28735 switch (token->type)
28737 case CPP_SEMICOLON:
28738 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28740 code = OMP_ATOMIC_CAPTURE_OLD;
28741 v = lhs;
28742 lhs = NULL_TREE;
28743 lhs1 = rhs1;
28744 rhs1 = NULL_TREE;
28745 cp_lexer_consume_token (parser->lexer);
28746 goto restart;
28748 else if (structured_block)
28750 opcode = NOP_EXPR;
28751 rhs = rhs1;
28752 rhs1 = NULL_TREE;
28753 goto stmt_done;
28755 cp_parser_error (parser,
28756 "invalid form of %<#pragma omp atomic%>");
28757 goto saw_error;
28758 case CPP_MULT:
28759 opcode = MULT_EXPR;
28760 break;
28761 case CPP_DIV:
28762 opcode = TRUNC_DIV_EXPR;
28763 break;
28764 case CPP_PLUS:
28765 opcode = PLUS_EXPR;
28766 break;
28767 case CPP_MINUS:
28768 opcode = MINUS_EXPR;
28769 break;
28770 case CPP_LSHIFT:
28771 opcode = LSHIFT_EXPR;
28772 break;
28773 case CPP_RSHIFT:
28774 opcode = RSHIFT_EXPR;
28775 break;
28776 case CPP_AND:
28777 opcode = BIT_AND_EXPR;
28778 break;
28779 case CPP_OR:
28780 opcode = BIT_IOR_EXPR;
28781 break;
28782 case CPP_XOR:
28783 opcode = BIT_XOR_EXPR;
28784 break;
28785 default:
28786 cp_parser_error (parser,
28787 "invalid operator for %<#pragma omp atomic%>");
28788 goto saw_error;
28790 oprec = TOKEN_PRECEDENCE (token);
28791 gcc_assert (oprec != PREC_NOT_OPERATOR);
28792 if (commutative_tree_code (opcode))
28793 oprec = (enum cp_parser_prec) (oprec - 1);
28794 cp_lexer_consume_token (parser->lexer);
28795 rhs = cp_parser_binary_expression (parser, false, false,
28796 oprec, NULL);
28797 if (rhs == error_mark_node)
28798 goto saw_error;
28799 goto stmt_done;
28800 /* FALLTHROUGH */
28801 default:
28802 cp_parser_error (parser,
28803 "invalid operator for %<#pragma omp atomic%>");
28804 goto saw_error;
28806 cp_lexer_consume_token (parser->lexer);
28808 rhs = cp_parser_expression (parser, false, NULL);
28809 if (rhs == error_mark_node)
28810 goto saw_error;
28811 break;
28813 stmt_done:
28814 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28816 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28817 goto saw_error;
28818 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28819 /*cast_p=*/false, NULL);
28820 if (v == error_mark_node)
28821 goto saw_error;
28822 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28823 goto saw_error;
28824 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28825 /*cast_p=*/false, NULL);
28826 if (lhs1 == error_mark_node)
28827 goto saw_error;
28829 if (structured_block)
28831 cp_parser_consume_semicolon_at_end_of_statement (parser);
28832 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28834 done:
28835 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28836 if (!structured_block)
28837 cp_parser_consume_semicolon_at_end_of_statement (parser);
28838 return;
28840 saw_error:
28841 cp_parser_skip_to_end_of_block_or_statement (parser);
28842 if (structured_block)
28844 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28845 cp_lexer_consume_token (parser->lexer);
28846 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28848 cp_parser_skip_to_end_of_block_or_statement (parser);
28849 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28850 cp_lexer_consume_token (parser->lexer);
28856 /* OpenMP 2.5:
28857 # pragma omp barrier new-line */
28859 static void
28860 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28862 cp_parser_require_pragma_eol (parser, pragma_tok);
28863 finish_omp_barrier ();
28866 /* OpenMP 2.5:
28867 # pragma omp critical [(name)] new-line
28868 structured-block */
28870 static tree
28871 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28873 tree stmt, name = NULL;
28875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28877 cp_lexer_consume_token (parser->lexer);
28879 name = cp_parser_identifier (parser);
28881 if (name == error_mark_node
28882 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28884 /*or_comma=*/false,
28885 /*consume_paren=*/true);
28886 if (name == error_mark_node)
28887 name = NULL;
28889 cp_parser_require_pragma_eol (parser, pragma_tok);
28891 stmt = cp_parser_omp_structured_block (parser);
28892 return c_finish_omp_critical (input_location, stmt, name);
28895 /* OpenMP 2.5:
28896 # pragma omp flush flush-vars[opt] new-line
28898 flush-vars:
28899 ( variable-list ) */
28901 static void
28902 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28904 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28905 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28906 cp_parser_require_pragma_eol (parser, pragma_tok);
28908 finish_omp_flush ();
28911 /* Helper function, to parse omp for increment expression. */
28913 static tree
28914 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28916 tree cond = cp_parser_binary_expression (parser, false, true,
28917 PREC_NOT_OPERATOR, NULL);
28918 if (cond == error_mark_node
28919 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28921 cp_parser_skip_to_end_of_statement (parser);
28922 return error_mark_node;
28925 switch (TREE_CODE (cond))
28927 case GT_EXPR:
28928 case GE_EXPR:
28929 case LT_EXPR:
28930 case LE_EXPR:
28931 break;
28932 case NE_EXPR:
28933 if (code == CILK_SIMD)
28934 break;
28935 /* Fall through: OpenMP disallows NE_EXPR. */
28936 default:
28937 return error_mark_node;
28940 /* If decl is an iterator, preserve LHS and RHS of the relational
28941 expr until finish_omp_for. */
28942 if (decl
28943 && (type_dependent_expression_p (decl)
28944 || CLASS_TYPE_P (TREE_TYPE (decl))))
28945 return cond;
28947 return build_x_binary_op (input_location, TREE_CODE (cond),
28948 TREE_OPERAND (cond, 0), ERROR_MARK,
28949 TREE_OPERAND (cond, 1), ERROR_MARK,
28950 /*overload=*/NULL, tf_warning_or_error);
28953 /* Helper function, to parse omp for increment expression. */
28955 static tree
28956 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
28958 cp_token *token = cp_lexer_peek_token (parser->lexer);
28959 enum tree_code op;
28960 tree lhs, rhs;
28961 cp_id_kind idk;
28962 bool decl_first;
28964 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28966 op = (token->type == CPP_PLUS_PLUS
28967 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
28968 cp_lexer_consume_token (parser->lexer);
28969 lhs = cp_parser_simple_cast_expression (parser);
28970 if (lhs != decl)
28971 return error_mark_node;
28972 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28975 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
28976 if (lhs != decl)
28977 return error_mark_node;
28979 token = cp_lexer_peek_token (parser->lexer);
28980 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28982 op = (token->type == CPP_PLUS_PLUS
28983 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
28984 cp_lexer_consume_token (parser->lexer);
28985 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28988 op = cp_parser_assignment_operator_opt (parser);
28989 if (op == ERROR_MARK)
28990 return error_mark_node;
28992 if (op != NOP_EXPR)
28994 rhs = cp_parser_assignment_expression (parser, false, NULL);
28995 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
28996 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28999 lhs = cp_parser_binary_expression (parser, false, false,
29000 PREC_ADDITIVE_EXPRESSION, NULL);
29001 token = cp_lexer_peek_token (parser->lexer);
29002 decl_first = lhs == decl;
29003 if (decl_first)
29004 lhs = NULL_TREE;
29005 if (token->type != CPP_PLUS
29006 && token->type != CPP_MINUS)
29007 return error_mark_node;
29011 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29012 cp_lexer_consume_token (parser->lexer);
29013 rhs = cp_parser_binary_expression (parser, false, false,
29014 PREC_ADDITIVE_EXPRESSION, NULL);
29015 token = cp_lexer_peek_token (parser->lexer);
29016 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29018 if (lhs == NULL_TREE)
29020 if (op == PLUS_EXPR)
29021 lhs = rhs;
29022 else
29023 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29024 tf_warning_or_error);
29026 else
29027 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29028 ERROR_MARK, NULL, tf_warning_or_error);
29031 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29033 if (!decl_first)
29035 if (rhs != decl || op == MINUS_EXPR)
29036 return error_mark_node;
29037 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29039 else
29040 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29042 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29045 /* Parse the initialization statement of either an OpenMP for loop or
29046 a Cilk Plus for loop.
29048 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29049 Plus.
29051 Return true if the resulting construct should have an
29052 OMP_CLAUSE_PRIVATE added to it. */
29054 static bool
29055 cp_parser_omp_for_loop_init (cp_parser *parser,
29056 bool parsing_openmp,
29057 tree &this_pre_body,
29058 vec<tree, va_gc> *for_block,
29059 tree &init,
29060 tree &decl,
29061 tree &real_decl)
29063 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29064 return false;
29066 bool add_private_clause = false;
29068 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29070 init-expr:
29071 var = lb
29072 integer-type var = lb
29073 random-access-iterator-type var = lb
29074 pointer-type var = lb
29076 cp_decl_specifier_seq type_specifiers;
29078 /* First, try to parse as an initialized declaration. See
29079 cp_parser_condition, from whence the bulk of this is copied. */
29081 cp_parser_parse_tentatively (parser);
29082 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29083 /*is_trailing_return=*/false,
29084 &type_specifiers);
29085 if (cp_parser_parse_definitely (parser))
29087 /* If parsing a type specifier seq succeeded, then this
29088 MUST be a initialized declaration. */
29089 tree asm_specification, attributes;
29090 cp_declarator *declarator;
29092 declarator = cp_parser_declarator (parser,
29093 CP_PARSER_DECLARATOR_NAMED,
29094 /*ctor_dtor_or_conv_p=*/NULL,
29095 /*parenthesized_p=*/NULL,
29096 /*member_p=*/false);
29097 attributes = cp_parser_attributes_opt (parser);
29098 asm_specification = cp_parser_asm_specification_opt (parser);
29100 if (declarator == cp_error_declarator)
29101 cp_parser_skip_to_end_of_statement (parser);
29103 else
29105 tree pushed_scope, auto_node;
29107 decl = start_decl (declarator, &type_specifiers,
29108 SD_INITIALIZED, attributes,
29109 /*prefix_attributes=*/NULL_TREE,
29110 &pushed_scope);
29112 auto_node = type_uses_auto (TREE_TYPE (decl));
29113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29115 if (cp_lexer_next_token_is (parser->lexer,
29116 CPP_OPEN_PAREN))
29118 if (parsing_openmp)
29119 error ("parenthesized initialization is not allowed in "
29120 "OpenMP %<for%> loop");
29121 else
29122 error ("parenthesized initialization is "
29123 "not allowed in for-loop");
29125 else
29126 /* Trigger an error. */
29127 cp_parser_require (parser, CPP_EQ, RT_EQ);
29129 init = error_mark_node;
29130 cp_parser_skip_to_end_of_statement (parser);
29132 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29133 || type_dependent_expression_p (decl)
29134 || auto_node)
29136 bool is_direct_init, is_non_constant_init;
29138 init = cp_parser_initializer (parser,
29139 &is_direct_init,
29140 &is_non_constant_init);
29142 if (auto_node)
29144 TREE_TYPE (decl)
29145 = do_auto_deduction (TREE_TYPE (decl), init,
29146 auto_node);
29148 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29149 && !type_dependent_expression_p (decl))
29150 goto non_class;
29153 cp_finish_decl (decl, init, !is_non_constant_init,
29154 asm_specification,
29155 LOOKUP_ONLYCONVERTING);
29156 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29158 vec_safe_push (for_block, this_pre_body);
29159 init = NULL_TREE;
29161 else
29162 init = pop_stmt_list (this_pre_body);
29163 this_pre_body = NULL_TREE;
29165 else
29167 /* Consume '='. */
29168 cp_lexer_consume_token (parser->lexer);
29169 init = cp_parser_assignment_expression (parser, false, NULL);
29171 non_class:
29172 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29173 init = error_mark_node;
29174 else
29175 cp_finish_decl (decl, NULL_TREE,
29176 /*init_const_expr_p=*/false,
29177 asm_specification,
29178 LOOKUP_ONLYCONVERTING);
29181 if (pushed_scope)
29182 pop_scope (pushed_scope);
29185 else
29187 cp_id_kind idk;
29188 /* If parsing a type specifier sequence failed, then
29189 this MUST be a simple expression. */
29190 cp_parser_parse_tentatively (parser);
29191 decl = cp_parser_primary_expression (parser, false, false,
29192 false, &idk);
29193 if (!cp_parser_error_occurred (parser)
29194 && decl
29195 && DECL_P (decl)
29196 && CLASS_TYPE_P (TREE_TYPE (decl)))
29198 tree rhs;
29200 cp_parser_parse_definitely (parser);
29201 cp_parser_require (parser, CPP_EQ, RT_EQ);
29202 rhs = cp_parser_assignment_expression (parser, false, NULL);
29203 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29204 decl, NOP_EXPR,
29205 rhs,
29206 tf_warning_or_error));
29207 add_private_clause = true;
29209 else
29211 decl = NULL;
29212 cp_parser_abort_tentative_parse (parser);
29213 init = cp_parser_expression (parser, false, NULL);
29214 if (init)
29216 if (TREE_CODE (init) == MODIFY_EXPR
29217 || TREE_CODE (init) == MODOP_EXPR)
29218 real_decl = TREE_OPERAND (init, 0);
29222 return add_private_clause;
29225 /* Parse the restricted form of the for statement allowed by OpenMP. */
29227 static tree
29228 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29229 tree *cclauses)
29231 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29232 tree real_decl, initv, condv, incrv, declv;
29233 tree this_pre_body, cl;
29234 location_t loc_first;
29235 bool collapse_err = false;
29236 int i, collapse = 1, nbraces = 0;
29237 vec<tree, va_gc> *for_block = make_tree_vector ();
29239 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29240 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29241 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29243 gcc_assert (collapse >= 1);
29245 declv = make_tree_vec (collapse);
29246 initv = make_tree_vec (collapse);
29247 condv = make_tree_vec (collapse);
29248 incrv = make_tree_vec (collapse);
29250 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29252 for (i = 0; i < collapse; i++)
29254 int bracecount = 0;
29255 bool add_private_clause = false;
29256 location_t loc;
29258 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29260 cp_parser_error (parser, "for statement expected");
29261 return NULL;
29263 loc = cp_lexer_consume_token (parser->lexer)->location;
29265 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29266 return NULL;
29268 init = decl = real_decl = NULL;
29269 this_pre_body = push_stmt_list ();
29271 add_private_clause
29272 |= cp_parser_omp_for_loop_init (parser,
29273 /*parsing_openmp=*/code != CILK_SIMD,
29274 this_pre_body, for_block,
29275 init, decl, real_decl);
29277 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29278 if (this_pre_body)
29280 this_pre_body = pop_stmt_list (this_pre_body);
29281 if (pre_body)
29283 tree t = pre_body;
29284 pre_body = push_stmt_list ();
29285 add_stmt (t);
29286 add_stmt (this_pre_body);
29287 pre_body = pop_stmt_list (pre_body);
29289 else
29290 pre_body = this_pre_body;
29293 if (decl)
29294 real_decl = decl;
29295 if (cclauses != NULL
29296 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29297 && real_decl != NULL_TREE)
29299 tree *c;
29300 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29301 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29302 && OMP_CLAUSE_DECL (*c) == real_decl)
29304 error_at (loc, "iteration variable %qD"
29305 " should not be firstprivate", real_decl);
29306 *c = OMP_CLAUSE_CHAIN (*c);
29308 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29309 && OMP_CLAUSE_DECL (*c) == real_decl)
29311 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29312 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29313 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29314 OMP_CLAUSE_DECL (l) = real_decl;
29315 OMP_CLAUSE_CHAIN (l) = clauses;
29316 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29317 clauses = l;
29318 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29319 CP_OMP_CLAUSE_INFO (*c) = NULL;
29320 add_private_clause = false;
29322 else
29324 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29325 && OMP_CLAUSE_DECL (*c) == real_decl)
29326 add_private_clause = false;
29327 c = &OMP_CLAUSE_CHAIN (*c);
29331 if (add_private_clause)
29333 tree c;
29334 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29336 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29338 && OMP_CLAUSE_DECL (c) == decl)
29339 break;
29340 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29341 && OMP_CLAUSE_DECL (c) == decl)
29342 error_at (loc, "iteration variable %qD "
29343 "should not be firstprivate",
29344 decl);
29345 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29346 && OMP_CLAUSE_DECL (c) == decl)
29347 error_at (loc, "iteration variable %qD should not be reduction",
29348 decl);
29350 if (c == NULL)
29352 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29353 OMP_CLAUSE_DECL (c) = decl;
29354 c = finish_omp_clauses (c);
29355 if (c)
29357 OMP_CLAUSE_CHAIN (c) = clauses;
29358 clauses = c;
29363 cond = NULL;
29364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29365 cond = cp_parser_omp_for_cond (parser, decl, code);
29366 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29368 incr = NULL;
29369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29371 /* If decl is an iterator, preserve the operator on decl
29372 until finish_omp_for. */
29373 if (real_decl
29374 && ((processing_template_decl
29375 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29376 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29377 incr = cp_parser_omp_for_incr (parser, real_decl);
29378 else
29379 incr = cp_parser_expression (parser, false, NULL);
29380 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29381 SET_EXPR_LOCATION (incr, input_location);
29384 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29385 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29386 /*or_comma=*/false,
29387 /*consume_paren=*/true);
29389 TREE_VEC_ELT (declv, i) = decl;
29390 TREE_VEC_ELT (initv, i) = init;
29391 TREE_VEC_ELT (condv, i) = cond;
29392 TREE_VEC_ELT (incrv, i) = incr;
29394 if (i == collapse - 1)
29395 break;
29397 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29398 in between the collapsed for loops to be still considered perfectly
29399 nested. Hopefully the final version clarifies this.
29400 For now handle (multiple) {'s and empty statements. */
29401 cp_parser_parse_tentatively (parser);
29404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29405 break;
29406 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29408 cp_lexer_consume_token (parser->lexer);
29409 bracecount++;
29411 else if (bracecount
29412 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29413 cp_lexer_consume_token (parser->lexer);
29414 else
29416 loc = cp_lexer_peek_token (parser->lexer)->location;
29417 error_at (loc, "not enough collapsed for loops");
29418 collapse_err = true;
29419 cp_parser_abort_tentative_parse (parser);
29420 declv = NULL_TREE;
29421 break;
29424 while (1);
29426 if (declv)
29428 cp_parser_parse_definitely (parser);
29429 nbraces += bracecount;
29433 /* Note that we saved the original contents of this flag when we entered
29434 the structured block, and so we don't need to re-save it here. */
29435 if (code == CILK_SIMD)
29436 parser->in_statement = IN_CILK_SIMD_FOR;
29437 else
29438 parser->in_statement = IN_OMP_FOR;
29440 /* Note that the grammar doesn't call for a structured block here,
29441 though the loop as a whole is a structured block. */
29442 body = push_stmt_list ();
29443 cp_parser_statement (parser, NULL_TREE, false, NULL);
29444 body = pop_stmt_list (body);
29446 if (declv == NULL_TREE)
29447 ret = NULL_TREE;
29448 else
29449 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29450 pre_body, clauses);
29452 while (nbraces)
29454 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29456 cp_lexer_consume_token (parser->lexer);
29457 nbraces--;
29459 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29460 cp_lexer_consume_token (parser->lexer);
29461 else
29463 if (!collapse_err)
29465 error_at (cp_lexer_peek_token (parser->lexer)->location,
29466 "collapsed loops not perfectly nested");
29468 collapse_err = true;
29469 cp_parser_statement_seq_opt (parser, NULL);
29470 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29471 break;
29475 while (!for_block->is_empty ())
29476 add_stmt (pop_stmt_list (for_block->pop ()));
29477 release_tree_vector (for_block);
29479 return ret;
29482 /* Helper function for OpenMP parsing, split clauses and call
29483 finish_omp_clauses on each of the set of clauses afterwards. */
29485 static void
29486 cp_omp_split_clauses (location_t loc, enum tree_code code,
29487 omp_clause_mask mask, tree clauses, tree *cclauses)
29489 int i;
29490 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29491 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29492 if (cclauses[i])
29493 cclauses[i] = finish_omp_clauses (cclauses[i]);
29496 /* OpenMP 4.0:
29497 #pragma omp simd simd-clause[optseq] new-line
29498 for-loop */
29500 #define OMP_SIMD_CLAUSE_MASK \
29501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29509 static tree
29510 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29511 char *p_name, omp_clause_mask mask, tree *cclauses)
29513 tree clauses, sb, ret;
29514 unsigned int save;
29515 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29517 strcat (p_name, " simd");
29518 mask |= OMP_SIMD_CLAUSE_MASK;
29519 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29521 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29522 cclauses == NULL);
29523 if (cclauses)
29525 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29526 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29529 sb = begin_omp_structured_block ();
29530 save = cp_parser_begin_omp_structured_block (parser);
29532 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29534 cp_parser_end_omp_structured_block (parser, save);
29535 add_stmt (finish_omp_structured_block (sb));
29537 return ret;
29540 /* OpenMP 2.5:
29541 #pragma omp for for-clause[optseq] new-line
29542 for-loop
29544 OpenMP 4.0:
29545 #pragma omp for simd for-simd-clause[optseq] new-line
29546 for-loop */
29548 #define OMP_FOR_CLAUSE_MASK \
29549 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29558 static tree
29559 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29560 char *p_name, omp_clause_mask mask, tree *cclauses)
29562 tree clauses, sb, ret;
29563 unsigned int save;
29564 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29566 strcat (p_name, " for");
29567 mask |= OMP_FOR_CLAUSE_MASK;
29568 if (cclauses)
29569 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29574 const char *p = IDENTIFIER_POINTER (id);
29576 if (strcmp (p, "simd") == 0)
29578 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29579 if (cclauses == NULL)
29580 cclauses = cclauses_buf;
29582 cp_lexer_consume_token (parser->lexer);
29583 if (!flag_openmp) /* flag_openmp_simd */
29584 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29585 cclauses);
29586 sb = begin_omp_structured_block ();
29587 save = cp_parser_begin_omp_structured_block (parser);
29588 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29589 cclauses);
29590 cp_parser_end_omp_structured_block (parser, save);
29591 tree body = finish_omp_structured_block (sb);
29592 if (ret == NULL)
29593 return ret;
29594 ret = make_node (OMP_FOR);
29595 TREE_TYPE (ret) = void_type_node;
29596 OMP_FOR_BODY (ret) = body;
29597 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29598 SET_EXPR_LOCATION (ret, loc);
29599 add_stmt (ret);
29600 return ret;
29603 if (!flag_openmp) /* flag_openmp_simd */
29605 cp_parser_require_pragma_eol (parser, pragma_tok);
29606 return NULL_TREE;
29609 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29610 cclauses == NULL);
29611 if (cclauses)
29613 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29614 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29617 sb = begin_omp_structured_block ();
29618 save = cp_parser_begin_omp_structured_block (parser);
29620 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29622 cp_parser_end_omp_structured_block (parser, save);
29623 add_stmt (finish_omp_structured_block (sb));
29625 return ret;
29628 /* OpenMP 2.5:
29629 # pragma omp master new-line
29630 structured-block */
29632 static tree
29633 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29635 cp_parser_require_pragma_eol (parser, pragma_tok);
29636 return c_finish_omp_master (input_location,
29637 cp_parser_omp_structured_block (parser));
29640 /* OpenMP 2.5:
29641 # pragma omp ordered new-line
29642 structured-block */
29644 static tree
29645 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29647 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29648 cp_parser_require_pragma_eol (parser, pragma_tok);
29649 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29652 /* OpenMP 2.5:
29654 section-scope:
29655 { section-sequence }
29657 section-sequence:
29658 section-directive[opt] structured-block
29659 section-sequence section-directive structured-block */
29661 static tree
29662 cp_parser_omp_sections_scope (cp_parser *parser)
29664 tree stmt, substmt;
29665 bool error_suppress = false;
29666 cp_token *tok;
29668 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29669 return NULL_TREE;
29671 stmt = push_stmt_list ();
29673 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29675 substmt = cp_parser_omp_structured_block (parser);
29676 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29677 add_stmt (substmt);
29680 while (1)
29682 tok = cp_lexer_peek_token (parser->lexer);
29683 if (tok->type == CPP_CLOSE_BRACE)
29684 break;
29685 if (tok->type == CPP_EOF)
29686 break;
29688 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29690 cp_lexer_consume_token (parser->lexer);
29691 cp_parser_require_pragma_eol (parser, tok);
29692 error_suppress = false;
29694 else if (!error_suppress)
29696 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29697 error_suppress = true;
29700 substmt = cp_parser_omp_structured_block (parser);
29701 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29702 add_stmt (substmt);
29704 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29706 substmt = pop_stmt_list (stmt);
29708 stmt = make_node (OMP_SECTIONS);
29709 TREE_TYPE (stmt) = void_type_node;
29710 OMP_SECTIONS_BODY (stmt) = substmt;
29712 add_stmt (stmt);
29713 return stmt;
29716 /* OpenMP 2.5:
29717 # pragma omp sections sections-clause[optseq] newline
29718 sections-scope */
29720 #define OMP_SECTIONS_CLAUSE_MASK \
29721 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29727 static tree
29728 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29729 char *p_name, omp_clause_mask mask, tree *cclauses)
29731 tree clauses, ret;
29732 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29734 strcat (p_name, " sections");
29735 mask |= OMP_SECTIONS_CLAUSE_MASK;
29736 if (cclauses)
29737 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29739 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29740 cclauses == NULL);
29741 if (cclauses)
29743 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29744 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29747 ret = cp_parser_omp_sections_scope (parser);
29748 if (ret)
29749 OMP_SECTIONS_CLAUSES (ret) = clauses;
29751 return ret;
29754 /* OpenMP 2.5:
29755 # pragma omp parallel parallel-clause[optseq] new-line
29756 structured-block
29757 # pragma omp parallel for parallel-for-clause[optseq] new-line
29758 structured-block
29759 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29760 structured-block
29762 OpenMP 4.0:
29763 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29764 structured-block */
29766 #define OMP_PARALLEL_CLAUSE_MASK \
29767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29777 static tree
29778 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29779 char *p_name, omp_clause_mask mask, tree *cclauses)
29781 tree stmt, clauses, block;
29782 unsigned int save;
29783 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29785 strcat (p_name, " parallel");
29786 mask |= OMP_PARALLEL_CLAUSE_MASK;
29788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29790 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29791 if (cclauses == NULL)
29792 cclauses = cclauses_buf;
29794 cp_lexer_consume_token (parser->lexer);
29795 if (!flag_openmp) /* flag_openmp_simd */
29796 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29797 block = begin_omp_parallel ();
29798 save = cp_parser_begin_omp_structured_block (parser);
29799 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29800 cp_parser_end_omp_structured_block (parser, save);
29801 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29802 block);
29803 OMP_PARALLEL_COMBINED (stmt) = 1;
29804 return stmt;
29806 else if (cclauses)
29808 error_at (loc, "expected %<for%> after %qs", p_name);
29809 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29810 return NULL_TREE;
29812 else if (!flag_openmp) /* flag_openmp_simd */
29814 cp_parser_require_pragma_eol (parser, pragma_tok);
29815 return NULL_TREE;
29817 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29819 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29820 const char *p = IDENTIFIER_POINTER (id);
29821 if (strcmp (p, "sections") == 0)
29823 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29824 cclauses = cclauses_buf;
29826 cp_lexer_consume_token (parser->lexer);
29827 block = begin_omp_parallel ();
29828 save = cp_parser_begin_omp_structured_block (parser);
29829 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29830 cp_parser_end_omp_structured_block (parser, save);
29831 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29832 block);
29833 OMP_PARALLEL_COMBINED (stmt) = 1;
29834 return stmt;
29838 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29840 block = begin_omp_parallel ();
29841 save = cp_parser_begin_omp_structured_block (parser);
29842 cp_parser_statement (parser, NULL_TREE, false, NULL);
29843 cp_parser_end_omp_structured_block (parser, save);
29844 stmt = finish_omp_parallel (clauses, block);
29845 return stmt;
29848 /* OpenMP 2.5:
29849 # pragma omp single single-clause[optseq] new-line
29850 structured-block */
29852 #define OMP_SINGLE_CLAUSE_MASK \
29853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29858 static tree
29859 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29861 tree stmt = make_node (OMP_SINGLE);
29862 TREE_TYPE (stmt) = void_type_node;
29864 OMP_SINGLE_CLAUSES (stmt)
29865 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29866 "#pragma omp single", pragma_tok);
29867 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29869 return add_stmt (stmt);
29872 /* OpenMP 3.0:
29873 # pragma omp task task-clause[optseq] new-line
29874 structured-block */
29876 #define OMP_TASK_CLAUSE_MASK \
29877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29887 static tree
29888 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29890 tree clauses, block;
29891 unsigned int save;
29893 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29894 "#pragma omp task", pragma_tok);
29895 block = begin_omp_task ();
29896 save = cp_parser_begin_omp_structured_block (parser);
29897 cp_parser_statement (parser, NULL_TREE, false, NULL);
29898 cp_parser_end_omp_structured_block (parser, save);
29899 return finish_omp_task (clauses, block);
29902 /* OpenMP 3.0:
29903 # pragma omp taskwait new-line */
29905 static void
29906 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29908 cp_parser_require_pragma_eol (parser, pragma_tok);
29909 finish_omp_taskwait ();
29912 /* OpenMP 3.1:
29913 # pragma omp taskyield new-line */
29915 static void
29916 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29918 cp_parser_require_pragma_eol (parser, pragma_tok);
29919 finish_omp_taskyield ();
29922 /* OpenMP 4.0:
29923 # pragma omp taskgroup new-line
29924 structured-block */
29926 static tree
29927 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29929 cp_parser_require_pragma_eol (parser, pragma_tok);
29930 return c_finish_omp_taskgroup (input_location,
29931 cp_parser_omp_structured_block (parser));
29935 /* OpenMP 2.5:
29936 # pragma omp threadprivate (variable-list) */
29938 static void
29939 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29941 tree vars;
29943 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29944 cp_parser_require_pragma_eol (parser, pragma_tok);
29946 finish_omp_threadprivate (vars);
29949 /* OpenMP 4.0:
29950 # pragma omp cancel cancel-clause[optseq] new-line */
29952 #define OMP_CANCEL_CLAUSE_MASK \
29953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29959 static void
29960 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
29962 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
29963 "#pragma omp cancel", pragma_tok);
29964 finish_omp_cancel (clauses);
29967 /* OpenMP 4.0:
29968 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29970 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29976 static void
29977 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
29979 tree clauses;
29980 bool point_seen = false;
29982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29985 const char *p = IDENTIFIER_POINTER (id);
29987 if (strcmp (p, "point") == 0)
29989 cp_lexer_consume_token (parser->lexer);
29990 point_seen = true;
29993 if (!point_seen)
29995 cp_parser_error (parser, "expected %<point%>");
29996 cp_parser_require_pragma_eol (parser, pragma_tok);
29997 return;
30000 clauses = cp_parser_omp_all_clauses (parser,
30001 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30002 "#pragma omp cancellation point",
30003 pragma_tok);
30004 finish_omp_cancellation_point (clauses);
30007 /* OpenMP 4.0:
30008 #pragma omp distribute distribute-clause[optseq] new-line
30009 for-loop */
30011 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30017 static tree
30018 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30019 char *p_name, omp_clause_mask mask, tree *cclauses)
30021 tree clauses, sb, ret;
30022 unsigned int save;
30023 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30025 strcat (p_name, " distribute");
30026 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30030 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30031 const char *p = IDENTIFIER_POINTER (id);
30032 bool simd = false;
30033 bool parallel = false;
30035 if (strcmp (p, "simd") == 0)
30036 simd = true;
30037 else
30038 parallel = strcmp (p, "parallel") == 0;
30039 if (parallel || simd)
30041 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30042 if (cclauses == NULL)
30043 cclauses = cclauses_buf;
30044 cp_lexer_consume_token (parser->lexer);
30045 if (!flag_openmp) /* flag_openmp_simd */
30047 if (simd)
30048 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30049 cclauses);
30050 else
30051 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30052 cclauses);
30054 sb = begin_omp_structured_block ();
30055 save = cp_parser_begin_omp_structured_block (parser);
30056 if (simd)
30057 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30058 cclauses);
30059 else
30060 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30061 cclauses);
30062 cp_parser_end_omp_structured_block (parser, save);
30063 tree body = finish_omp_structured_block (sb);
30064 if (ret == NULL)
30065 return ret;
30066 ret = make_node (OMP_DISTRIBUTE);
30067 TREE_TYPE (ret) = void_type_node;
30068 OMP_FOR_BODY (ret) = body;
30069 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30070 SET_EXPR_LOCATION (ret, loc);
30071 add_stmt (ret);
30072 return ret;
30075 if (!flag_openmp) /* flag_openmp_simd */
30077 cp_parser_require_pragma_eol (parser, pragma_tok);
30078 return NULL_TREE;
30081 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30082 cclauses == NULL);
30083 if (cclauses)
30085 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30086 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30089 sb = begin_omp_structured_block ();
30090 save = cp_parser_begin_omp_structured_block (parser);
30092 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30094 cp_parser_end_omp_structured_block (parser, save);
30095 add_stmt (finish_omp_structured_block (sb));
30097 return ret;
30100 /* OpenMP 4.0:
30101 # pragma omp teams teams-clause[optseq] new-line
30102 structured-block */
30104 #define OMP_TEAMS_CLAUSE_MASK \
30105 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30113 static tree
30114 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30115 char *p_name, omp_clause_mask mask, tree *cclauses)
30117 tree clauses, sb, ret;
30118 unsigned int save;
30119 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30121 strcat (p_name, " teams");
30122 mask |= OMP_TEAMS_CLAUSE_MASK;
30124 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30126 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30127 const char *p = IDENTIFIER_POINTER (id);
30128 if (strcmp (p, "distribute") == 0)
30130 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30131 if (cclauses == NULL)
30132 cclauses = cclauses_buf;
30134 cp_lexer_consume_token (parser->lexer);
30135 if (!flag_openmp) /* flag_openmp_simd */
30136 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30137 cclauses);
30138 sb = begin_omp_structured_block ();
30139 save = cp_parser_begin_omp_structured_block (parser);
30140 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30141 cclauses);
30142 cp_parser_end_omp_structured_block (parser, save);
30143 tree body = finish_omp_structured_block (sb);
30144 if (ret == NULL)
30145 return ret;
30146 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30147 ret = make_node (OMP_TEAMS);
30148 TREE_TYPE (ret) = void_type_node;
30149 OMP_TEAMS_CLAUSES (ret) = clauses;
30150 OMP_TEAMS_BODY (ret) = body;
30151 return add_stmt (ret);
30154 if (!flag_openmp) /* flag_openmp_simd */
30156 cp_parser_require_pragma_eol (parser, pragma_tok);
30157 return NULL_TREE;
30160 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30161 cclauses == NULL);
30162 if (cclauses)
30164 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30165 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30168 tree stmt = make_node (OMP_TEAMS);
30169 TREE_TYPE (stmt) = void_type_node;
30170 OMP_TEAMS_CLAUSES (stmt) = clauses;
30171 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30173 return add_stmt (stmt);
30176 /* OpenMP 4.0:
30177 # pragma omp target data target-data-clause[optseq] new-line
30178 structured-block */
30180 #define OMP_TARGET_DATA_CLAUSE_MASK \
30181 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30185 static tree
30186 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30188 tree stmt = make_node (OMP_TARGET_DATA);
30189 TREE_TYPE (stmt) = void_type_node;
30191 OMP_TARGET_DATA_CLAUSES (stmt)
30192 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30193 "#pragma omp target data", pragma_tok);
30194 keep_next_level (true);
30195 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30197 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30198 return add_stmt (stmt);
30201 /* OpenMP 4.0:
30202 # pragma omp target update target-update-clause[optseq] new-line */
30204 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30205 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30210 static bool
30211 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30212 enum pragma_context context)
30214 if (context == pragma_stmt)
30216 error_at (pragma_tok->location,
30217 "%<#pragma omp target update%> may only be "
30218 "used in compound statements");
30219 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30220 return false;
30223 tree clauses
30224 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30225 "#pragma omp target update", pragma_tok);
30226 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30227 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30229 error_at (pragma_tok->location,
30230 "%<#pragma omp target update must contain at least one "
30231 "%<from%> or %<to%> clauses");
30232 return false;
30235 tree stmt = make_node (OMP_TARGET_UPDATE);
30236 TREE_TYPE (stmt) = void_type_node;
30237 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30238 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30239 add_stmt (stmt);
30240 return false;
30243 /* OpenMP 4.0:
30244 # pragma omp target target-clause[optseq] new-line
30245 structured-block */
30247 #define OMP_TARGET_CLAUSE_MASK \
30248 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30252 static bool
30253 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30254 enum pragma_context context)
30256 if (context != pragma_stmt && context != pragma_compound)
30258 cp_parser_error (parser, "expected declaration specifiers");
30259 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30260 return false;
30263 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30266 const char *p = IDENTIFIER_POINTER (id);
30268 if (strcmp (p, "teams") == 0)
30270 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30271 char p_name[sizeof ("#pragma omp target teams distribute "
30272 "parallel for simd")];
30274 cp_lexer_consume_token (parser->lexer);
30275 strcpy (p_name, "#pragma omp target");
30276 if (!flag_openmp) /* flag_openmp_simd */
30277 return cp_parser_omp_teams (parser, pragma_tok, p_name,
30278 OMP_TARGET_CLAUSE_MASK, cclauses);
30279 keep_next_level (true);
30280 tree sb = begin_omp_structured_block ();
30281 unsigned save = cp_parser_begin_omp_structured_block (parser);
30282 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30283 OMP_TARGET_CLAUSE_MASK, cclauses);
30284 cp_parser_end_omp_structured_block (parser, save);
30285 tree body = finish_omp_structured_block (sb);
30286 if (ret == NULL)
30287 return ret;
30288 tree stmt = make_node (OMP_TARGET);
30289 TREE_TYPE (stmt) = void_type_node;
30290 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30291 OMP_TARGET_BODY (stmt) = body;
30292 add_stmt (stmt);
30293 return true;
30295 else if (!flag_openmp) /* flag_openmp_simd */
30297 cp_parser_require_pragma_eol (parser, pragma_tok);
30298 return NULL_TREE;
30300 else if (strcmp (p, "data") == 0)
30302 cp_lexer_consume_token (parser->lexer);
30303 cp_parser_omp_target_data (parser, pragma_tok);
30304 return true;
30306 else if (strcmp (p, "update") == 0)
30308 cp_lexer_consume_token (parser->lexer);
30309 return cp_parser_omp_target_update (parser, pragma_tok, context);
30313 tree stmt = make_node (OMP_TARGET);
30314 TREE_TYPE (stmt) = void_type_node;
30316 OMP_TARGET_CLAUSES (stmt)
30317 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30318 "#pragma omp target", pragma_tok);
30319 keep_next_level (true);
30320 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30322 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30323 add_stmt (stmt);
30324 return true;
30327 /* OpenMP 4.0:
30328 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30330 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30338 static void
30339 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30340 enum pragma_context context)
30342 bool first_p = parser->omp_declare_simd == NULL;
30343 cp_omp_declare_simd_data data;
30344 if (first_p)
30346 data.error_seen = false;
30347 data.fndecl_seen = false;
30348 data.tokens = vNULL;
30349 parser->omp_declare_simd = &data;
30351 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30352 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30353 cp_lexer_consume_token (parser->lexer);
30354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30355 parser->omp_declare_simd->error_seen = true;
30356 cp_parser_require_pragma_eol (parser, pragma_tok);
30357 struct cp_token_cache *cp
30358 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30359 parser->omp_declare_simd->tokens.safe_push (cp);
30360 if (first_p)
30362 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30363 cp_parser_pragma (parser, context);
30364 switch (context)
30366 case pragma_external:
30367 cp_parser_declaration (parser);
30368 break;
30369 case pragma_member:
30370 cp_parser_member_declaration (parser);
30371 break;
30372 case pragma_objc_icode:
30373 cp_parser_block_declaration (parser, /*statement_p=*/false);
30374 break;
30375 default:
30376 cp_parser_declaration_statement (parser);
30377 break;
30379 if (parser->omp_declare_simd
30380 && !parser->omp_declare_simd->error_seen
30381 && !parser->omp_declare_simd->fndecl_seen)
30382 error_at (pragma_tok->location,
30383 "%<#pragma omp declare simd%> not immediately followed by "
30384 "function declaration or definition");
30385 data.tokens.release ();
30386 parser->omp_declare_simd = NULL;
30390 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30391 This function is modelled similar to the late parsing of omp declare
30392 simd. */
30394 static tree
30395 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30397 struct cp_token_cache *ce;
30398 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30399 int ii = 0;
30401 if (parser->omp_declare_simd != NULL)
30403 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30404 " marked as a Cilk Plus SIMD-enabled function");
30405 XDELETE (parser->cilk_simd_fn_info);
30406 parser->cilk_simd_fn_info = NULL;
30407 return attrs;
30409 if (!info->error_seen && info->fndecl_seen)
30411 error ("vector attribute not immediately followed by a single function"
30412 " declaration or definition");
30413 info->error_seen = true;
30415 if (info->error_seen)
30416 return attrs;
30418 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30420 tree c, cl;
30422 cp_parser_push_lexer_for_tokens (parser, ce);
30423 parser->lexer->in_pragma = true;
30424 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30425 "SIMD-enabled functions attribute",
30426 NULL);
30427 cp_parser_pop_lexer (parser);
30428 if (cl)
30429 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30431 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30432 TREE_CHAIN (c) = attrs;
30433 attrs = c;
30435 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30436 TREE_CHAIN (c) = attrs;
30437 if (processing_template_decl)
30438 ATTR_IS_DEPENDENT (c) = 1;
30439 attrs = c;
30441 info->fndecl_seen = true;
30442 XDELETE (parser->cilk_simd_fn_info);
30443 parser->cilk_simd_fn_info = NULL;
30444 return attrs;
30447 /* Finalize #pragma omp declare simd clauses after direct declarator has
30448 been parsed, and put that into "omp declare simd" attribute. */
30450 static tree
30451 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30453 struct cp_token_cache *ce;
30454 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30455 int i;
30457 if (!data->error_seen && data->fndecl_seen)
30459 error ("%<#pragma omp declare simd%> not immediately followed by "
30460 "a single function declaration or definition");
30461 data->error_seen = true;
30462 return attrs;
30464 if (data->error_seen)
30465 return attrs;
30467 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30469 tree c, cl;
30471 cp_parser_push_lexer_for_tokens (parser, ce);
30472 parser->lexer->in_pragma = true;
30473 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30474 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30475 cp_lexer_consume_token (parser->lexer);
30476 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30477 "#pragma omp declare simd", pragma_tok);
30478 cp_parser_pop_lexer (parser);
30479 if (cl)
30480 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30481 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30482 TREE_CHAIN (c) = attrs;
30483 if (processing_template_decl)
30484 ATTR_IS_DEPENDENT (c) = 1;
30485 attrs = c;
30488 data->fndecl_seen = true;
30489 return attrs;
30493 /* OpenMP 4.0:
30494 # pragma omp declare target new-line
30495 declarations and definitions
30496 # pragma omp end declare target new-line */
30498 static void
30499 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30502 scope_chain->omp_declare_target_attribute++;
30505 static void
30506 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30508 const char *p = "";
30509 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30511 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30512 p = IDENTIFIER_POINTER (id);
30514 if (strcmp (p, "declare") == 0)
30516 cp_lexer_consume_token (parser->lexer);
30517 p = "";
30518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30521 p = IDENTIFIER_POINTER (id);
30523 if (strcmp (p, "target") == 0)
30524 cp_lexer_consume_token (parser->lexer);
30525 else
30527 cp_parser_error (parser, "expected %<target%>");
30528 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30529 return;
30532 else
30534 cp_parser_error (parser, "expected %<declare%>");
30535 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30536 return;
30538 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30539 if (!scope_chain->omp_declare_target_attribute)
30540 error_at (pragma_tok->location,
30541 "%<#pragma omp end declare target%> without corresponding "
30542 "%<#pragma omp declare target%>");
30543 else
30544 scope_chain->omp_declare_target_attribute--;
30547 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30548 expression and optional initializer clause of
30549 #pragma omp declare reduction. We store the expression(s) as
30550 either 3, 6 or 7 special statements inside of the artificial function's
30551 body. The first two statements are DECL_EXPRs for the artificial
30552 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30553 expression that uses those variables.
30554 If there was any INITIALIZER clause, this is followed by further statements,
30555 the fourth and fifth statements are DECL_EXPRs for the artificial
30556 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30557 constructor variant (first token after open paren is not omp_priv),
30558 then the sixth statement is a statement with the function call expression
30559 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30560 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30561 to initialize the OMP_PRIV artificial variable and there is seventh
30562 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30564 static bool
30565 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30567 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30568 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30569 type = TREE_TYPE (type);
30570 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30571 DECL_ARTIFICIAL (omp_out) = 1;
30572 pushdecl (omp_out);
30573 add_decl_expr (omp_out);
30574 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30575 DECL_ARTIFICIAL (omp_in) = 1;
30576 pushdecl (omp_in);
30577 add_decl_expr (omp_in);
30578 tree combiner;
30579 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30581 keep_next_level (true);
30582 tree block = begin_omp_structured_block ();
30583 combiner = cp_parser_expression (parser, false, NULL);
30584 finish_expr_stmt (combiner);
30585 block = finish_omp_structured_block (block);
30586 add_stmt (block);
30588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30589 return false;
30591 const char *p = "";
30592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30595 p = IDENTIFIER_POINTER (id);
30598 if (strcmp (p, "initializer") == 0)
30600 cp_lexer_consume_token (parser->lexer);
30601 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30602 return false;
30604 p = "";
30605 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30607 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30608 p = IDENTIFIER_POINTER (id);
30611 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30612 DECL_ARTIFICIAL (omp_priv) = 1;
30613 pushdecl (omp_priv);
30614 add_decl_expr (omp_priv);
30615 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30616 DECL_ARTIFICIAL (omp_orig) = 1;
30617 pushdecl (omp_orig);
30618 add_decl_expr (omp_orig);
30620 keep_next_level (true);
30621 block = begin_omp_structured_block ();
30623 bool ctor = false;
30624 if (strcmp (p, "omp_priv") == 0)
30626 bool is_direct_init, is_non_constant_init;
30627 ctor = true;
30628 cp_lexer_consume_token (parser->lexer);
30629 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30630 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30631 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30632 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30633 == CPP_CLOSE_PAREN
30634 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30635 == CPP_CLOSE_PAREN))
30637 finish_omp_structured_block (block);
30638 error ("invalid initializer clause");
30639 return false;
30641 initializer = cp_parser_initializer (parser, &is_direct_init,
30642 &is_non_constant_init);
30643 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30644 NULL_TREE, LOOKUP_ONLYCONVERTING);
30646 else
30648 cp_parser_parse_tentatively (parser);
30649 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30650 /*check_dependency_p=*/true,
30651 /*template_p=*/NULL,
30652 /*declarator_p=*/false,
30653 /*optional_p=*/false);
30654 vec<tree, va_gc> *args;
30655 if (fn_name == error_mark_node
30656 || cp_parser_error_occurred (parser)
30657 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30658 || ((args = cp_parser_parenthesized_expression_list
30659 (parser, non_attr, /*cast_p=*/false,
30660 /*allow_expansion_p=*/true,
30661 /*non_constant_p=*/NULL)),
30662 cp_parser_error_occurred (parser)))
30664 finish_omp_structured_block (block);
30665 cp_parser_abort_tentative_parse (parser);
30666 cp_parser_error (parser, "expected id-expression (arguments)");
30667 return false;
30669 unsigned int i;
30670 tree arg;
30671 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30672 if (arg == omp_priv
30673 || (TREE_CODE (arg) == ADDR_EXPR
30674 && TREE_OPERAND (arg, 0) == omp_priv))
30675 break;
30676 cp_parser_abort_tentative_parse (parser);
30677 if (arg == NULL_TREE)
30678 error ("one of the initializer call arguments should be %<omp_priv%>"
30679 " or %<&omp_priv%>");
30680 initializer = cp_parser_postfix_expression (parser, false, false, false,
30681 false, NULL);
30682 finish_expr_stmt (initializer);
30685 block = finish_omp_structured_block (block);
30686 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30687 finish_expr_stmt (block);
30689 if (ctor)
30690 add_decl_expr (omp_orig);
30692 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30693 return false;
30696 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30697 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30699 return true;
30702 /* OpenMP 4.0
30703 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30704 initializer-clause[opt] new-line
30706 initializer-clause:
30707 initializer (omp_priv initializer)
30708 initializer (function-name (argument-list)) */
30710 static void
30711 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30712 enum pragma_context)
30714 auto_vec<tree> types;
30715 enum tree_code reduc_code = ERROR_MARK;
30716 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30717 unsigned int i;
30718 cp_token *first_token;
30719 cp_token_cache *cp;
30720 int errs;
30721 void *p;
30723 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30724 p = obstack_alloc (&declarator_obstack, 0);
30726 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30727 goto fail;
30729 switch (cp_lexer_peek_token (parser->lexer)->type)
30731 case CPP_PLUS:
30732 reduc_code = PLUS_EXPR;
30733 break;
30734 case CPP_MULT:
30735 reduc_code = MULT_EXPR;
30736 break;
30737 case CPP_MINUS:
30738 reduc_code = MINUS_EXPR;
30739 break;
30740 case CPP_AND:
30741 reduc_code = BIT_AND_EXPR;
30742 break;
30743 case CPP_XOR:
30744 reduc_code = BIT_XOR_EXPR;
30745 break;
30746 case CPP_OR:
30747 reduc_code = BIT_IOR_EXPR;
30748 break;
30749 case CPP_AND_AND:
30750 reduc_code = TRUTH_ANDIF_EXPR;
30751 break;
30752 case CPP_OR_OR:
30753 reduc_code = TRUTH_ORIF_EXPR;
30754 break;
30755 case CPP_NAME:
30756 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30757 break;
30758 default:
30759 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30760 "%<|%>, %<&&%>, %<||%> or identifier");
30761 goto fail;
30764 if (reduc_code != ERROR_MARK)
30765 cp_lexer_consume_token (parser->lexer);
30767 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30768 if (reduc_id == error_mark_node)
30769 goto fail;
30771 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30772 goto fail;
30774 /* Types may not be defined in declare reduction type list. */
30775 const char *saved_message;
30776 saved_message = parser->type_definition_forbidden_message;
30777 parser->type_definition_forbidden_message
30778 = G_("types may not be defined in declare reduction type list");
30779 bool saved_colon_corrects_to_scope_p;
30780 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30781 parser->colon_corrects_to_scope_p = false;
30782 bool saved_colon_doesnt_start_class_def_p;
30783 saved_colon_doesnt_start_class_def_p
30784 = parser->colon_doesnt_start_class_def_p;
30785 parser->colon_doesnt_start_class_def_p = true;
30787 while (true)
30789 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30790 type = cp_parser_type_id (parser);
30791 if (type == error_mark_node)
30793 else if (ARITHMETIC_TYPE_P (type)
30794 && (orig_reduc_id == NULL_TREE
30795 || (TREE_CODE (type) != COMPLEX_TYPE
30796 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30797 "min") == 0
30798 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30799 "max") == 0))))
30800 error_at (loc, "predeclared arithmetic type %qT in "
30801 "%<#pragma omp declare reduction%>", type);
30802 else if (TREE_CODE (type) == FUNCTION_TYPE
30803 || TREE_CODE (type) == METHOD_TYPE
30804 || TREE_CODE (type) == ARRAY_TYPE)
30805 error_at (loc, "function or array type %qT in "
30806 "%<#pragma omp declare reduction%>", type);
30807 else if (TREE_CODE (type) == REFERENCE_TYPE)
30808 error_at (loc, "reference type %qT in "
30809 "%<#pragma omp declare reduction%>", type);
30810 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30811 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30812 "%<#pragma omp declare reduction%>", type);
30813 else
30814 types.safe_push (type);
30816 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30817 cp_lexer_consume_token (parser->lexer);
30818 else
30819 break;
30822 /* Restore the saved message. */
30823 parser->type_definition_forbidden_message = saved_message;
30824 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30825 parser->colon_doesnt_start_class_def_p
30826 = saved_colon_doesnt_start_class_def_p;
30828 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30829 || types.is_empty ())
30831 fail:
30832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30833 goto done;
30836 first_token = cp_lexer_peek_token (parser->lexer);
30837 cp = NULL;
30838 errs = errorcount;
30839 FOR_EACH_VEC_ELT (types, i, type)
30841 tree fntype
30842 = build_function_type_list (void_type_node,
30843 cp_build_reference_type (type, false),
30844 NULL_TREE);
30845 tree this_reduc_id = reduc_id;
30846 if (!dependent_type_p (type))
30847 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30848 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30849 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30850 DECL_ARTIFICIAL (fndecl) = 1;
30851 DECL_EXTERNAL (fndecl) = 1;
30852 DECL_DECLARED_INLINE_P (fndecl) = 1;
30853 DECL_IGNORED_P (fndecl) = 1;
30854 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30855 DECL_ATTRIBUTES (fndecl)
30856 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30857 DECL_ATTRIBUTES (fndecl));
30858 if (processing_template_decl)
30859 fndecl = push_template_decl (fndecl);
30860 bool block_scope = false;
30861 tree block = NULL_TREE;
30862 if (current_function_decl)
30864 block_scope = true;
30865 DECL_CONTEXT (fndecl) = global_namespace;
30866 if (!processing_template_decl)
30867 pushdecl (fndecl);
30869 else if (current_class_type)
30871 if (cp == NULL)
30873 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30874 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30875 cp_lexer_consume_token (parser->lexer);
30876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30877 goto fail;
30878 cp = cp_token_cache_new (first_token,
30879 cp_lexer_peek_nth_token (parser->lexer,
30880 2));
30882 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30883 finish_member_declaration (fndecl);
30884 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30885 DECL_PENDING_INLINE_P (fndecl) = 1;
30886 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30887 continue;
30889 else
30891 DECL_CONTEXT (fndecl) = current_namespace;
30892 pushdecl (fndecl);
30894 if (!block_scope)
30895 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30896 else
30897 block = begin_omp_structured_block ();
30898 if (cp)
30900 cp_parser_push_lexer_for_tokens (parser, cp);
30901 parser->lexer->in_pragma = true;
30903 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30905 if (!block_scope)
30906 finish_function (0);
30907 else
30908 DECL_CONTEXT (fndecl) = current_function_decl;
30909 if (cp)
30910 cp_parser_pop_lexer (parser);
30911 goto fail;
30913 if (cp)
30914 cp_parser_pop_lexer (parser);
30915 if (!block_scope)
30916 finish_function (0);
30917 else
30919 DECL_CONTEXT (fndecl) = current_function_decl;
30920 block = finish_omp_structured_block (block);
30921 if (TREE_CODE (block) == BIND_EXPR)
30922 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30923 else if (TREE_CODE (block) == STATEMENT_LIST)
30924 DECL_SAVED_TREE (fndecl) = block;
30925 if (processing_template_decl)
30926 add_decl_expr (fndecl);
30928 cp_check_omp_declare_reduction (fndecl);
30929 if (cp == NULL && types.length () > 1)
30930 cp = cp_token_cache_new (first_token,
30931 cp_lexer_peek_nth_token (parser->lexer, 2));
30932 if (errs != errorcount)
30933 break;
30936 cp_parser_require_pragma_eol (parser, pragma_tok);
30938 done:
30939 /* Free any declarators allocated. */
30940 obstack_free (&declarator_obstack, p);
30943 /* OpenMP 4.0
30944 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30945 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30946 initializer-clause[opt] new-line
30947 #pragma omp declare target new-line */
30949 static void
30950 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
30951 enum pragma_context context)
30953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30956 const char *p = IDENTIFIER_POINTER (id);
30958 if (strcmp (p, "simd") == 0)
30960 cp_lexer_consume_token (parser->lexer);
30961 cp_parser_omp_declare_simd (parser, pragma_tok,
30962 context);
30963 return;
30965 cp_ensure_no_omp_declare_simd (parser);
30966 if (strcmp (p, "reduction") == 0)
30968 cp_lexer_consume_token (parser->lexer);
30969 cp_parser_omp_declare_reduction (parser, pragma_tok,
30970 context);
30971 return;
30973 if (!flag_openmp) /* flag_openmp_simd */
30975 cp_parser_require_pragma_eol (parser, pragma_tok);
30976 return;
30978 if (strcmp (p, "target") == 0)
30980 cp_lexer_consume_token (parser->lexer);
30981 cp_parser_omp_declare_target (parser, pragma_tok);
30982 return;
30985 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
30986 "or %<target%>");
30987 cp_parser_require_pragma_eol (parser, pragma_tok);
30990 /* Main entry point to OpenMP statement pragmas. */
30992 static void
30993 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
30995 tree stmt;
30996 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
30997 omp_clause_mask mask (0);
30999 switch (pragma_tok->pragma_kind)
31001 case PRAGMA_OMP_ATOMIC:
31002 cp_parser_omp_atomic (parser, pragma_tok);
31003 return;
31004 case PRAGMA_OMP_CRITICAL:
31005 stmt = cp_parser_omp_critical (parser, pragma_tok);
31006 break;
31007 case PRAGMA_OMP_DISTRIBUTE:
31008 strcpy (p_name, "#pragma omp");
31009 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31010 break;
31011 case PRAGMA_OMP_FOR:
31012 strcpy (p_name, "#pragma omp");
31013 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31014 break;
31015 case PRAGMA_OMP_MASTER:
31016 stmt = cp_parser_omp_master (parser, pragma_tok);
31017 break;
31018 case PRAGMA_OMP_ORDERED:
31019 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31020 break;
31021 case PRAGMA_OMP_PARALLEL:
31022 strcpy (p_name, "#pragma omp");
31023 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31024 break;
31025 case PRAGMA_OMP_SECTIONS:
31026 strcpy (p_name, "#pragma omp");
31027 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31028 break;
31029 case PRAGMA_OMP_SIMD:
31030 strcpy (p_name, "#pragma omp");
31031 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31032 break;
31033 case PRAGMA_OMP_SINGLE:
31034 stmt = cp_parser_omp_single (parser, pragma_tok);
31035 break;
31036 case PRAGMA_OMP_TASK:
31037 stmt = cp_parser_omp_task (parser, pragma_tok);
31038 break;
31039 case PRAGMA_OMP_TASKGROUP:
31040 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31041 break;
31042 case PRAGMA_OMP_TEAMS:
31043 strcpy (p_name, "#pragma omp");
31044 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31045 break;
31046 default:
31047 gcc_unreachable ();
31050 if (stmt)
31051 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31054 /* Transactional Memory parsing routines. */
31056 /* Parse a transaction attribute.
31058 txn-attribute:
31059 attribute
31060 [ [ identifier ] ]
31062 ??? Simplify this when C++0x bracket attributes are
31063 implemented properly. */
31065 static tree
31066 cp_parser_txn_attribute_opt (cp_parser *parser)
31068 cp_token *token;
31069 tree attr_name, attr = NULL;
31071 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31072 return cp_parser_attributes_opt (parser);
31074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31075 return NULL_TREE;
31076 cp_lexer_consume_token (parser->lexer);
31077 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31078 goto error1;
31080 token = cp_lexer_peek_token (parser->lexer);
31081 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31083 token = cp_lexer_consume_token (parser->lexer);
31085 attr_name = (token->type == CPP_KEYWORD
31086 /* For keywords, use the canonical spelling,
31087 not the parsed identifier. */
31088 ? ridpointers[(int) token->keyword]
31089 : token->u.value);
31090 attr = build_tree_list (attr_name, NULL_TREE);
31092 else
31093 cp_parser_error (parser, "expected identifier");
31095 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31096 error1:
31097 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31098 return attr;
31101 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31103 transaction-statement:
31104 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31105 compound-statement
31106 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31109 static tree
31110 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31112 unsigned char old_in = parser->in_transaction;
31113 unsigned char this_in = 1, new_in;
31114 cp_token *token;
31115 tree stmt, attrs, noex;
31117 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31118 || keyword == RID_TRANSACTION_RELAXED);
31119 token = cp_parser_require_keyword (parser, keyword,
31120 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31121 : RT_TRANSACTION_RELAXED));
31122 gcc_assert (token != NULL);
31124 if (keyword == RID_TRANSACTION_RELAXED)
31125 this_in |= TM_STMT_ATTR_RELAXED;
31126 else
31128 attrs = cp_parser_txn_attribute_opt (parser);
31129 if (attrs)
31130 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31133 /* Parse a noexcept specification. */
31134 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31136 /* Keep track if we're in the lexical scope of an outer transaction. */
31137 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31139 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31141 parser->in_transaction = new_in;
31142 cp_parser_compound_statement (parser, NULL, false, false);
31143 parser->in_transaction = old_in;
31145 finish_transaction_stmt (stmt, NULL, this_in, noex);
31147 return stmt;
31150 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31152 transaction-expression:
31153 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31154 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31157 static tree
31158 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31160 unsigned char old_in = parser->in_transaction;
31161 unsigned char this_in = 1;
31162 cp_token *token;
31163 tree expr, noex;
31164 bool noex_expr;
31166 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31167 || keyword == RID_TRANSACTION_RELAXED);
31169 if (!flag_tm)
31170 error (keyword == RID_TRANSACTION_RELAXED
31171 ? G_("%<__transaction_relaxed%> without transactional memory "
31172 "support enabled")
31173 : G_("%<__transaction_atomic%> without transactional memory "
31174 "support enabled"));
31176 token = cp_parser_require_keyword (parser, keyword,
31177 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31178 : RT_TRANSACTION_RELAXED));
31179 gcc_assert (token != NULL);
31181 if (keyword == RID_TRANSACTION_RELAXED)
31182 this_in |= TM_STMT_ATTR_RELAXED;
31184 /* Set this early. This might mean that we allow transaction_cancel in
31185 an expression that we find out later actually has to be a constexpr.
31186 However, we expect that cxx_constant_value will be able to deal with
31187 this; also, if the noexcept has no constexpr, then what we parse next
31188 really is a transaction's body. */
31189 parser->in_transaction = this_in;
31191 /* Parse a noexcept specification. */
31192 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31193 true);
31195 if (!noex || !noex_expr
31196 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31198 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31200 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31201 expr = finish_parenthesized_expr (expr);
31203 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31205 else
31207 /* The only expression that is available got parsed for the noexcept
31208 already. noexcept is true then. */
31209 expr = noex;
31210 noex = boolean_true_node;
31213 expr = build_transaction_expr (token->location, expr, this_in, noex);
31214 parser->in_transaction = old_in;
31216 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31217 return error_mark_node;
31219 return (flag_tm ? expr : error_mark_node);
31222 /* Parse a function-transaction-block.
31224 function-transaction-block:
31225 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31226 function-body
31227 __transaction_atomic txn-attribute[opt] function-try-block
31228 __transaction_relaxed ctor-initializer[opt] function-body
31229 __transaction_relaxed function-try-block
31232 static bool
31233 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31235 unsigned char old_in = parser->in_transaction;
31236 unsigned char new_in = 1;
31237 tree compound_stmt, stmt, attrs;
31238 bool ctor_initializer_p;
31239 cp_token *token;
31241 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31242 || keyword == RID_TRANSACTION_RELAXED);
31243 token = cp_parser_require_keyword (parser, keyword,
31244 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31245 : RT_TRANSACTION_RELAXED));
31246 gcc_assert (token != NULL);
31248 if (keyword == RID_TRANSACTION_RELAXED)
31249 new_in |= TM_STMT_ATTR_RELAXED;
31250 else
31252 attrs = cp_parser_txn_attribute_opt (parser);
31253 if (attrs)
31254 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31257 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31259 parser->in_transaction = new_in;
31261 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31262 ctor_initializer_p = cp_parser_function_try_block (parser);
31263 else
31264 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31265 (parser, /*in_function_try_block=*/false);
31267 parser->in_transaction = old_in;
31269 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31271 return ctor_initializer_p;
31274 /* Parse a __transaction_cancel statement.
31276 cancel-statement:
31277 __transaction_cancel txn-attribute[opt] ;
31278 __transaction_cancel txn-attribute[opt] throw-expression ;
31280 ??? Cancel and throw is not yet implemented. */
31282 static tree
31283 cp_parser_transaction_cancel (cp_parser *parser)
31285 cp_token *token;
31286 bool is_outer = false;
31287 tree stmt, attrs;
31289 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31290 RT_TRANSACTION_CANCEL);
31291 gcc_assert (token != NULL);
31293 attrs = cp_parser_txn_attribute_opt (parser);
31294 if (attrs)
31295 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31297 /* ??? Parse cancel-and-throw here. */
31299 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31301 if (!flag_tm)
31303 error_at (token->location, "%<__transaction_cancel%> without "
31304 "transactional memory support enabled");
31305 return error_mark_node;
31307 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31309 error_at (token->location, "%<__transaction_cancel%> within a "
31310 "%<__transaction_relaxed%>");
31311 return error_mark_node;
31313 else if (is_outer)
31315 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31316 && !is_tm_may_cancel_outer (current_function_decl))
31318 error_at (token->location, "outer %<__transaction_cancel%> not "
31319 "within outer %<__transaction_atomic%>");
31320 error_at (token->location,
31321 " or a %<transaction_may_cancel_outer%> function");
31322 return error_mark_node;
31325 else if (parser->in_transaction == 0)
31327 error_at (token->location, "%<__transaction_cancel%> not within "
31328 "%<__transaction_atomic%>");
31329 return error_mark_node;
31332 stmt = build_tm_abort_call (token->location, is_outer);
31333 add_stmt (stmt);
31335 return stmt;
31338 /* The parser. */
31340 static GTY (()) cp_parser *the_parser;
31343 /* Special handling for the first token or line in the file. The first
31344 thing in the file might be #pragma GCC pch_preprocess, which loads a
31345 PCH file, which is a GC collection point. So we need to handle this
31346 first pragma without benefit of an existing lexer structure.
31348 Always returns one token to the caller in *FIRST_TOKEN. This is
31349 either the true first token of the file, or the first token after
31350 the initial pragma. */
31352 static void
31353 cp_parser_initial_pragma (cp_token *first_token)
31355 tree name = NULL;
31357 cp_lexer_get_preprocessor_token (NULL, first_token);
31358 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31359 return;
31361 cp_lexer_get_preprocessor_token (NULL, first_token);
31362 if (first_token->type == CPP_STRING)
31364 name = first_token->u.value;
31366 cp_lexer_get_preprocessor_token (NULL, first_token);
31367 if (first_token->type != CPP_PRAGMA_EOL)
31368 error_at (first_token->location,
31369 "junk at end of %<#pragma GCC pch_preprocess%>");
31371 else
31372 error_at (first_token->location, "expected string literal");
31374 /* Skip to the end of the pragma. */
31375 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31376 cp_lexer_get_preprocessor_token (NULL, first_token);
31378 /* Now actually load the PCH file. */
31379 if (name)
31380 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31382 /* Read one more token to return to our caller. We have to do this
31383 after reading the PCH file in, since its pointers have to be
31384 live. */
31385 cp_lexer_get_preprocessor_token (NULL, first_token);
31388 /* Normal parsing of a pragma token. Here we can (and must) use the
31389 regular lexer. */
31391 static bool
31392 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31394 cp_token *pragma_tok;
31395 unsigned int id;
31397 pragma_tok = cp_lexer_consume_token (parser->lexer);
31398 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31399 parser->lexer->in_pragma = true;
31401 id = pragma_tok->pragma_kind;
31402 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31403 cp_ensure_no_omp_declare_simd (parser);
31404 switch (id)
31406 case PRAGMA_GCC_PCH_PREPROCESS:
31407 error_at (pragma_tok->location,
31408 "%<#pragma GCC pch_preprocess%> must be first");
31409 break;
31411 case PRAGMA_OMP_BARRIER:
31412 switch (context)
31414 case pragma_compound:
31415 cp_parser_omp_barrier (parser, pragma_tok);
31416 return false;
31417 case pragma_stmt:
31418 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31419 "used in compound statements");
31420 break;
31421 default:
31422 goto bad_stmt;
31424 break;
31426 case PRAGMA_OMP_FLUSH:
31427 switch (context)
31429 case pragma_compound:
31430 cp_parser_omp_flush (parser, pragma_tok);
31431 return false;
31432 case pragma_stmt:
31433 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31434 "used in compound statements");
31435 break;
31436 default:
31437 goto bad_stmt;
31439 break;
31441 case PRAGMA_OMP_TASKWAIT:
31442 switch (context)
31444 case pragma_compound:
31445 cp_parser_omp_taskwait (parser, pragma_tok);
31446 return false;
31447 case pragma_stmt:
31448 error_at (pragma_tok->location,
31449 "%<#pragma omp taskwait%> may only be "
31450 "used in compound statements");
31451 break;
31452 default:
31453 goto bad_stmt;
31455 break;
31457 case PRAGMA_OMP_TASKYIELD:
31458 switch (context)
31460 case pragma_compound:
31461 cp_parser_omp_taskyield (parser, pragma_tok);
31462 return false;
31463 case pragma_stmt:
31464 error_at (pragma_tok->location,
31465 "%<#pragma omp taskyield%> may only be "
31466 "used in compound statements");
31467 break;
31468 default:
31469 goto bad_stmt;
31471 break;
31473 case PRAGMA_OMP_CANCEL:
31474 switch (context)
31476 case pragma_compound:
31477 cp_parser_omp_cancel (parser, pragma_tok);
31478 return false;
31479 case pragma_stmt:
31480 error_at (pragma_tok->location,
31481 "%<#pragma omp cancel%> may only be "
31482 "used in compound statements");
31483 break;
31484 default:
31485 goto bad_stmt;
31487 break;
31489 case PRAGMA_OMP_CANCELLATION_POINT:
31490 switch (context)
31492 case pragma_compound:
31493 cp_parser_omp_cancellation_point (parser, pragma_tok);
31494 return false;
31495 case pragma_stmt:
31496 error_at (pragma_tok->location,
31497 "%<#pragma omp cancellation point%> may only be "
31498 "used in compound statements");
31499 break;
31500 default:
31501 goto bad_stmt;
31503 break;
31505 case PRAGMA_OMP_THREADPRIVATE:
31506 cp_parser_omp_threadprivate (parser, pragma_tok);
31507 return false;
31509 case PRAGMA_OMP_DECLARE_REDUCTION:
31510 cp_parser_omp_declare (parser, pragma_tok, context);
31511 return false;
31513 case PRAGMA_OMP_ATOMIC:
31514 case PRAGMA_OMP_CRITICAL:
31515 case PRAGMA_OMP_DISTRIBUTE:
31516 case PRAGMA_OMP_FOR:
31517 case PRAGMA_OMP_MASTER:
31518 case PRAGMA_OMP_ORDERED:
31519 case PRAGMA_OMP_PARALLEL:
31520 case PRAGMA_OMP_SECTIONS:
31521 case PRAGMA_OMP_SIMD:
31522 case PRAGMA_OMP_SINGLE:
31523 case PRAGMA_OMP_TASK:
31524 case PRAGMA_OMP_TASKGROUP:
31525 case PRAGMA_OMP_TEAMS:
31526 if (context != pragma_stmt && context != pragma_compound)
31527 goto bad_stmt;
31528 cp_parser_omp_construct (parser, pragma_tok);
31529 return true;
31531 case PRAGMA_OMP_TARGET:
31532 return cp_parser_omp_target (parser, pragma_tok, context);
31534 case PRAGMA_OMP_END_DECLARE_TARGET:
31535 cp_parser_omp_end_declare_target (parser, pragma_tok);
31536 return false;
31538 case PRAGMA_OMP_SECTION:
31539 error_at (pragma_tok->location,
31540 "%<#pragma omp section%> may only be used in "
31541 "%<#pragma omp sections%> construct");
31542 break;
31544 case PRAGMA_IVDEP:
31546 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31547 cp_token *tok;
31548 tok = cp_lexer_peek_token (the_parser->lexer);
31549 if (tok->type != CPP_KEYWORD
31550 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31551 && tok->keyword != RID_DO))
31553 cp_parser_error (parser, "for, while or do statement expected");
31554 return false;
31556 cp_parser_iteration_statement (parser, true);
31557 return true;
31560 case PRAGMA_CILK_SIMD:
31561 if (context == pragma_external)
31563 error_at (pragma_tok->location,
31564 "%<#pragma simd%> must be inside a function");
31565 break;
31567 cp_parser_cilk_simd (parser, pragma_tok);
31568 return true;
31570 default:
31571 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31572 c_invoke_pragma_handler (id);
31573 break;
31575 bad_stmt:
31576 cp_parser_error (parser, "expected declaration specifiers");
31577 break;
31580 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31581 return false;
31584 /* The interface the pragma parsers have to the lexer. */
31586 enum cpp_ttype
31587 pragma_lex (tree *value)
31589 cp_token *tok;
31590 enum cpp_ttype ret;
31592 tok = cp_lexer_peek_token (the_parser->lexer);
31594 ret = tok->type;
31595 *value = tok->u.value;
31597 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31598 ret = CPP_EOF;
31599 else if (ret == CPP_STRING)
31600 *value = cp_parser_string_literal (the_parser, false, false);
31601 else
31603 cp_lexer_consume_token (the_parser->lexer);
31604 if (ret == CPP_KEYWORD)
31605 ret = CPP_NAME;
31608 return ret;
31612 /* External interface. */
31614 /* Parse one entire translation unit. */
31616 void
31617 c_parse_file (void)
31619 static bool already_called = false;
31621 if (already_called)
31623 sorry ("inter-module optimizations not implemented for C++");
31624 return;
31626 already_called = true;
31628 the_parser = cp_parser_new ();
31629 push_deferring_access_checks (flag_access_control
31630 ? dk_no_deferred : dk_no_check);
31631 cp_parser_translation_unit (the_parser);
31632 the_parser = NULL;
31635 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31636 vectorlength clause:
31637 Syntax:
31638 vectorlength ( constant-expression ) */
31640 static tree
31641 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31642 bool is_simd_fn)
31644 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31645 tree expr;
31646 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31647 safelen clause. Thus, vectorlength is represented as OMP 4.0
31648 safelen. For SIMD-enabled function it is represented by OMP 4.0
31649 simdlen. */
31650 if (!is_simd_fn)
31651 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31652 loc);
31653 else
31654 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31655 loc);
31657 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31658 return error_mark_node;
31660 expr = cp_parser_constant_expression (parser, false, NULL);
31661 expr = maybe_constant_value (expr);
31663 /* If expr == error_mark_node, then don't emit any errors nor
31664 create a clause. if any of the above functions returns
31665 error mark node then they would have emitted an error message. */
31666 if (expr == error_mark_node)
31668 else if (!TREE_TYPE (expr)
31669 || !TREE_CONSTANT (expr)
31670 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31671 error_at (loc, "vectorlength must be an integer constant");
31672 else if (TREE_CONSTANT (expr)
31673 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31674 error_at (loc, "vectorlength must be a power of 2");
31675 else
31677 tree c;
31678 if (!is_simd_fn)
31680 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31681 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31682 OMP_CLAUSE_CHAIN (c) = clauses;
31683 clauses = c;
31685 else
31687 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31688 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31689 OMP_CLAUSE_CHAIN (c) = clauses;
31690 clauses = c;
31694 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31695 return error_mark_node;
31696 return clauses;
31699 /* Handles the Cilk Plus #pragma simd linear clause.
31700 Syntax:
31701 linear ( simd-linear-variable-list )
31703 simd-linear-variable-list:
31704 simd-linear-variable
31705 simd-linear-variable-list , simd-linear-variable
31707 simd-linear-variable:
31708 id-expression
31709 id-expression : simd-linear-step
31711 simd-linear-step:
31712 conditional-expression */
31714 static tree
31715 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31717 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31719 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31720 return clauses;
31721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31723 cp_parser_error (parser, "expected identifier");
31724 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31725 return error_mark_node;
31728 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31729 parser->colon_corrects_to_scope_p = false;
31730 while (1)
31732 cp_token *token = cp_lexer_peek_token (parser->lexer);
31733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31735 cp_parser_error (parser, "expected variable-name");
31736 clauses = error_mark_node;
31737 break;
31740 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31741 false, false);
31742 tree decl = cp_parser_lookup_name_simple (parser, var_name,
31743 token->location);
31744 if (decl == error_mark_node)
31746 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31747 token->location);
31748 clauses = error_mark_node;
31750 else
31752 tree e = NULL_TREE;
31753 tree step_size = integer_one_node;
31755 /* If present, parse the linear step. Otherwise, assume the default
31756 value of 1. */
31757 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31759 cp_lexer_consume_token (parser->lexer);
31761 e = cp_parser_assignment_expression (parser, false, NULL);
31762 e = maybe_constant_value (e);
31764 if (e == error_mark_node)
31766 /* If an error has occurred, then the whole pragma is
31767 considered ill-formed. Thus, no reason to keep
31768 parsing. */
31769 clauses = error_mark_node;
31770 break;
31772 else if (type_dependent_expression_p (e)
31773 || value_dependent_expression_p (e)
31774 || (TREE_TYPE (e)
31775 && INTEGRAL_TYPE_P (TREE_TYPE (e))
31776 && (TREE_CONSTANT (e)
31777 || DECL_P (e))))
31778 step_size = e;
31779 else
31780 cp_parser_error (parser,
31781 "step size must be an integer constant "
31782 "expression or an integer variable");
31785 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31786 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31787 OMP_CLAUSE_DECL (l) = decl;
31788 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31789 OMP_CLAUSE_CHAIN (l) = clauses;
31790 clauses = l;
31792 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31793 cp_lexer_consume_token (parser->lexer);
31794 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31795 break;
31796 else
31798 error_at (cp_lexer_peek_token (parser->lexer)->location,
31799 "expected %<,%> or %<)%> after %qE", decl);
31800 clauses = error_mark_node;
31801 break;
31804 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31805 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31806 return clauses;
31809 /* Returns the name of the next clause. If the clause is not
31810 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31811 token is not consumed. Otherwise, the appropriate enum from the
31812 pragma_simd_clause is returned and the token is consumed. */
31814 static pragma_omp_clause
31815 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31817 pragma_omp_clause clause_type;
31818 cp_token *token = cp_lexer_peek_token (parser->lexer);
31820 if (token->keyword == RID_PRIVATE)
31821 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31822 else if (!token->u.value || token->type != CPP_NAME)
31823 return PRAGMA_CILK_CLAUSE_NONE;
31824 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31825 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31826 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31827 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31828 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31829 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31830 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31831 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31832 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31833 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31834 else
31835 return PRAGMA_CILK_CLAUSE_NONE;
31837 cp_lexer_consume_token (parser->lexer);
31838 return clause_type;
31841 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31843 static tree
31844 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31846 tree clauses = NULL_TREE;
31848 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31849 && clauses != error_mark_node)
31851 pragma_omp_clause c_kind;
31852 c_kind = cp_parser_cilk_simd_clause_name (parser);
31853 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31854 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
31855 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31856 clauses = cp_parser_cilk_simd_linear (parser, clauses);
31857 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31858 /* Use the OpenMP 4.0 equivalent function. */
31859 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31860 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31861 /* Use the OpenMP 4.0 equivalent function. */
31862 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31863 clauses);
31864 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31865 /* Use the OMP 4.0 equivalent function. */
31866 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31867 clauses);
31868 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31869 /* Use the OMP 4.0 equivalent function. */
31870 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31871 else
31873 clauses = error_mark_node;
31874 cp_parser_error (parser, "expected %<#pragma simd%> clause");
31875 break;
31879 cp_parser_skip_to_pragma_eol (parser, pragma_token);
31881 if (clauses == error_mark_node)
31882 return error_mark_node;
31883 else
31884 return c_finish_cilk_clauses (clauses);
31887 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31889 static void
31890 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31892 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31894 if (clauses == error_mark_node)
31895 return;
31897 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31899 error_at (cp_lexer_peek_token (parser->lexer)->location,
31900 "for statement expected");
31901 return;
31904 tree sb = begin_omp_structured_block ();
31905 int save = cp_parser_begin_omp_structured_block (parser);
31906 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31907 if (ret)
31908 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31909 cp_parser_end_omp_structured_block (parser, save);
31910 add_stmt (finish_omp_structured_block (sb));
31911 return;
31914 /* Create an identifier for a generic parameter type (a synthesized
31915 template parameter implied by `auto' or a concept identifier). */
31917 static GTY(()) int generic_parm_count;
31918 static tree
31919 make_generic_type_name ()
31921 char buf[32];
31922 sprintf (buf, "auto:%d", ++generic_parm_count);
31923 return get_identifier (buf);
31926 /* Predicate that behaves as is_auto_or_concept but matches the parent
31927 node of the generic type rather than the generic type itself. This
31928 allows for type transformation in add_implicit_template_parms. */
31930 static inline bool
31931 tree_type_is_auto_or_concept (const_tree t)
31933 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31936 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
31937 (creating a new template parameter list if necessary). Returns the newly
31938 created template type parm. */
31940 tree
31941 synthesize_implicit_template_parm (cp_parser *parser)
31943 gcc_assert (current_binding_level->kind == sk_function_parms);
31945 /* We are either continuing a function template that already contains implicit
31946 template parameters, creating a new fully-implicit function template, or
31947 extending an existing explicit function template with implicit template
31948 parameters. */
31950 cp_binding_level *const entry_scope = current_binding_level;
31952 bool become_template = false;
31953 cp_binding_level *parent_scope = 0;
31955 if (parser->implicit_template_scope)
31957 gcc_assert (parser->implicit_template_parms);
31959 current_binding_level = parser->implicit_template_scope;
31961 else
31963 /* Roll back to the existing template parameter scope (in the case of
31964 extending an explicit function template) or introduce a new template
31965 parameter scope ahead of the function parameter scope (or class scope
31966 in the case of out-of-line member definitions). The function scope is
31967 added back after template parameter synthesis below. */
31969 cp_binding_level *scope = entry_scope;
31971 while (scope->kind == sk_function_parms)
31973 parent_scope = scope;
31974 scope = scope->level_chain;
31976 if (current_class_type && !LAMBDA_TYPE_P (current_class_type)
31977 && parser->num_classes_being_defined == 0)
31978 while (scope->kind == sk_class)
31980 parent_scope = scope;
31981 scope = scope->level_chain;
31984 current_binding_level = scope;
31986 if (scope->kind != sk_template_parms
31987 || !function_being_declared_is_template_p (parser))
31989 /* Introduce a new template parameter list for implicit template
31990 parameters. */
31992 become_template = true;
31994 parser->implicit_template_scope
31995 = begin_scope (sk_template_parms, NULL);
31997 ++processing_template_decl;
31999 parser->fully_implicit_function_template_p = true;
32000 ++parser->num_template_parameter_lists;
32002 else
32004 /* Synthesize implicit template parameters at the end of the explicit
32005 template parameter list. */
32007 gcc_assert (current_template_parms);
32009 parser->implicit_template_scope = scope;
32011 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32012 parser->implicit_template_parms
32013 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32017 /* Synthesize a new template parameter and track the current template
32018 parameter chain with implicit_template_parms. */
32020 tree synth_id = make_generic_type_name ();
32021 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32022 synth_id);
32023 tree new_parm
32024 = process_template_parm (parser->implicit_template_parms,
32025 input_location,
32026 build_tree_list (NULL_TREE, synth_tmpl_parm),
32027 /*non_type=*/false,
32028 /*param_pack=*/false);
32031 if (parser->implicit_template_parms)
32032 parser->implicit_template_parms
32033 = TREE_CHAIN (parser->implicit_template_parms);
32034 else
32035 parser->implicit_template_parms = new_parm;
32037 tree new_type = TREE_TYPE (getdecls ());
32039 /* If creating a fully implicit function template, start the new implicit
32040 template parameter list with this synthesized type, otherwise grow the
32041 current template parameter list. */
32043 if (become_template)
32045 parent_scope->level_chain = current_binding_level;
32047 tree new_parms = make_tree_vec (1);
32048 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32049 current_template_parms = tree_cons (size_int (processing_template_decl),
32050 new_parms, current_template_parms);
32052 else
32054 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32055 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32056 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32057 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32060 current_binding_level = entry_scope;
32062 return new_type;
32065 /* Finish the declaration of a fully implicit function template. Such a
32066 template has no explicit template parameter list so has not been through the
32067 normal template head and tail processing. synthesize_implicit_template_parm
32068 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32069 provided if the declaration is a class member such that its template
32070 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32071 form is returned. Otherwise NULL_TREE is returned. */
32073 tree
32074 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32076 gcc_assert (parser->fully_implicit_function_template_p);
32078 if (member_decl_opt && member_decl_opt != error_mark_node
32079 && DECL_VIRTUAL_P (member_decl_opt))
32081 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32082 "implicit templates may not be %<virtual%>");
32083 DECL_VIRTUAL_P (member_decl_opt) = false;
32086 if (member_decl_opt)
32087 member_decl_opt = finish_member_template_decl (member_decl_opt);
32088 end_template_decl ();
32090 parser->fully_implicit_function_template_p = false;
32091 --parser->num_template_parameter_lists;
32093 return member_decl_opt;
32096 #include "gt-cp-parser.h"