PR c/50347
[official-gcc.git] / gcc / cp / parser.c
blob2e117a53155c0feeec84154e91bb627ddffe1e1b
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 /* Unwind generic function template scope if necessary. */
3140 if (parser->fully_implicit_function_template_p)
3141 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3143 while (true)
3145 cp_token *token = cp_lexer_peek_token (parser->lexer);
3147 switch (token->type)
3149 case CPP_EOF:
3150 case CPP_PRAGMA_EOL:
3151 /* If we've run out of tokens, stop. */
3152 return;
3154 case CPP_SEMICOLON:
3155 /* If the next token is a `;', we have reached the end of the
3156 statement. */
3157 if (!nesting_depth)
3158 return;
3159 break;
3161 case CPP_CLOSE_BRACE:
3162 /* If this is a non-nested '}', stop before consuming it.
3163 That way, when confronted with something like:
3165 { 3 + }
3167 we stop before consuming the closing '}', even though we
3168 have not yet reached a `;'. */
3169 if (nesting_depth == 0)
3170 return;
3172 /* If it is the closing '}' for a block that we have
3173 scanned, stop -- but only after consuming the token.
3174 That way given:
3176 void f g () { ... }
3177 typedef int I;
3179 we will stop after the body of the erroneously declared
3180 function, but before consuming the following `typedef'
3181 declaration. */
3182 if (--nesting_depth == 0)
3184 cp_lexer_consume_token (parser->lexer);
3185 return;
3188 case CPP_OPEN_BRACE:
3189 ++nesting_depth;
3190 break;
3192 default:
3193 break;
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser->lexer);
3201 /* This function is called at the end of a statement or declaration.
3202 If the next token is a semicolon, it is consumed; otherwise, error
3203 recovery is attempted. */
3205 static void
3206 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3208 /* Look for the trailing `;'. */
3209 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3211 /* If there is additional (erroneous) input, skip to the end of
3212 the statement. */
3213 cp_parser_skip_to_end_of_statement (parser);
3214 /* If the next token is now a `;', consume it. */
3215 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3216 cp_lexer_consume_token (parser->lexer);
3220 /* Skip tokens until we have consumed an entire block, or until we
3221 have consumed a non-nested `;'. */
3223 static void
3224 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3226 int nesting_depth = 0;
3228 /* Unwind generic function template scope if necessary. */
3229 if (parser->fully_implicit_function_template_p)
3230 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3232 while (nesting_depth >= 0)
3234 cp_token *token = cp_lexer_peek_token (parser->lexer);
3236 switch (token->type)
3238 case CPP_EOF:
3239 case CPP_PRAGMA_EOL:
3240 /* If we've run out of tokens, stop. */
3241 return;
3243 case CPP_SEMICOLON:
3244 /* Stop if this is an unnested ';'. */
3245 if (!nesting_depth)
3246 nesting_depth = -1;
3247 break;
3249 case CPP_CLOSE_BRACE:
3250 /* Stop if this is an unnested '}', or closes the outermost
3251 nesting level. */
3252 nesting_depth--;
3253 if (nesting_depth < 0)
3254 return;
3255 if (!nesting_depth)
3256 nesting_depth = -1;
3257 break;
3259 case CPP_OPEN_BRACE:
3260 /* Nest. */
3261 nesting_depth++;
3262 break;
3264 default:
3265 break;
3268 /* Consume the token. */
3269 cp_lexer_consume_token (parser->lexer);
3273 /* Skip tokens until a non-nested closing curly brace is the next
3274 token, or there are no more tokens. Return true in the first case,
3275 false otherwise. */
3277 static bool
3278 cp_parser_skip_to_closing_brace (cp_parser *parser)
3280 unsigned nesting_depth = 0;
3282 while (true)
3284 cp_token *token = cp_lexer_peek_token (parser->lexer);
3286 switch (token->type)
3288 case CPP_EOF:
3289 case CPP_PRAGMA_EOL:
3290 /* If we've run out of tokens, stop. */
3291 return false;
3293 case CPP_CLOSE_BRACE:
3294 /* If the next token is a non-nested `}', then we have reached
3295 the end of the current block. */
3296 if (nesting_depth-- == 0)
3297 return true;
3298 break;
3300 case CPP_OPEN_BRACE:
3301 /* If it the next token is a `{', then we are entering a new
3302 block. Consume the entire block. */
3303 ++nesting_depth;
3304 break;
3306 default:
3307 break;
3310 /* Consume the token. */
3311 cp_lexer_consume_token (parser->lexer);
3315 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3316 parameter is the PRAGMA token, allowing us to purge the entire pragma
3317 sequence. */
3319 static void
3320 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3322 cp_token *token;
3324 parser->lexer->in_pragma = false;
3327 token = cp_lexer_consume_token (parser->lexer);
3328 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3330 /* Ensure that the pragma is not parsed again. */
3331 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3334 /* Require pragma end of line, resyncing with it as necessary. The
3335 arguments are as for cp_parser_skip_to_pragma_eol. */
3337 static void
3338 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3340 parser->lexer->in_pragma = false;
3341 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3342 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3345 /* This is a simple wrapper around make_typename_type. When the id is
3346 an unresolved identifier node, we can provide a superior diagnostic
3347 using cp_parser_diagnose_invalid_type_name. */
3349 static tree
3350 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3351 tree id, location_t id_location)
3353 tree result;
3354 if (identifier_p (id))
3356 result = make_typename_type (scope, id, typename_type,
3357 /*complain=*/tf_none);
3358 if (result == error_mark_node)
3359 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3360 return result;
3362 return make_typename_type (scope, id, typename_type, tf_error);
3365 /* This is a wrapper around the
3366 make_{pointer,ptrmem,reference}_declarator functions that decides
3367 which one to call based on the CODE and CLASS_TYPE arguments. The
3368 CODE argument should be one of the values returned by
3369 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3370 appertain to the pointer or reference. */
3372 static cp_declarator *
3373 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3374 cp_cv_quals cv_qualifiers,
3375 cp_declarator *target,
3376 tree attributes)
3378 if (code == ERROR_MARK)
3379 return cp_error_declarator;
3381 if (code == INDIRECT_REF)
3382 if (class_type == NULL_TREE)
3383 return make_pointer_declarator (cv_qualifiers, target, attributes);
3384 else
3385 return make_ptrmem_declarator (cv_qualifiers, class_type,
3386 target, attributes);
3387 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3388 return make_reference_declarator (cv_qualifiers, target,
3389 false, attributes);
3390 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3391 return make_reference_declarator (cv_qualifiers, target,
3392 true, attributes);
3393 gcc_unreachable ();
3396 /* Create a new C++ parser. */
3398 static cp_parser *
3399 cp_parser_new (void)
3401 cp_parser *parser;
3402 cp_lexer *lexer;
3403 unsigned i;
3405 /* cp_lexer_new_main is called before doing GC allocation because
3406 cp_lexer_new_main might load a PCH file. */
3407 lexer = cp_lexer_new_main ();
3409 /* Initialize the binops_by_token so that we can get the tree
3410 directly from the token. */
3411 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3412 binops_by_token[binops[i].token_type] = binops[i];
3414 parser = ggc_alloc_cleared_cp_parser ();
3415 parser->lexer = lexer;
3416 parser->context = cp_parser_context_new (NULL);
3418 /* For now, we always accept GNU extensions. */
3419 parser->allow_gnu_extensions_p = 1;
3421 /* The `>' token is a greater-than operator, not the end of a
3422 template-id. */
3423 parser->greater_than_is_operator_p = true;
3425 parser->default_arg_ok_p = true;
3427 /* We are not parsing a constant-expression. */
3428 parser->integral_constant_expression_p = false;
3429 parser->allow_non_integral_constant_expression_p = false;
3430 parser->non_integral_constant_expression_p = false;
3432 /* Local variable names are not forbidden. */
3433 parser->local_variables_forbidden_p = false;
3435 /* We are not processing an `extern "C"' declaration. */
3436 parser->in_unbraced_linkage_specification_p = false;
3438 /* We are not processing a declarator. */
3439 parser->in_declarator_p = false;
3441 /* We are not processing a template-argument-list. */
3442 parser->in_template_argument_list_p = false;
3444 /* We are not in an iteration statement. */
3445 parser->in_statement = 0;
3447 /* We are not in a switch statement. */
3448 parser->in_switch_statement_p = false;
3450 /* We are not parsing a type-id inside an expression. */
3451 parser->in_type_id_in_expr_p = false;
3453 /* Declarations aren't implicitly extern "C". */
3454 parser->implicit_extern_c = false;
3456 /* String literals should be translated to the execution character set. */
3457 parser->translate_strings_p = true;
3459 /* We are not parsing a function body. */
3460 parser->in_function_body = false;
3462 /* We can correct until told otherwise. */
3463 parser->colon_corrects_to_scope_p = true;
3465 /* The unparsed function queue is empty. */
3466 push_unparsed_function_queues (parser);
3468 /* There are no classes being defined. */
3469 parser->num_classes_being_defined = 0;
3471 /* No template parameters apply. */
3472 parser->num_template_parameter_lists = 0;
3474 /* Not declaring an implicit function template. */
3475 parser->auto_is_implicit_function_template_parm_p = false;
3476 parser->fully_implicit_function_template_p = false;
3477 parser->implicit_template_parms = 0;
3478 parser->implicit_template_scope = 0;
3480 return parser;
3483 /* Create a cp_lexer structure which will emit the tokens in CACHE
3484 and push it onto the parser's lexer stack. This is used for delayed
3485 parsing of in-class method bodies and default arguments, and should
3486 not be confused with tentative parsing. */
3487 static void
3488 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3490 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3491 lexer->next = parser->lexer;
3492 parser->lexer = lexer;
3494 /* Move the current source position to that of the first token in the
3495 new lexer. */
3496 cp_lexer_set_source_position_from_token (lexer->next_token);
3499 /* Pop the top lexer off the parser stack. This is never used for the
3500 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3501 static void
3502 cp_parser_pop_lexer (cp_parser *parser)
3504 cp_lexer *lexer = parser->lexer;
3505 parser->lexer = lexer->next;
3506 cp_lexer_destroy (lexer);
3508 /* Put the current source position back where it was before this
3509 lexer was pushed. */
3510 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3513 /* Lexical conventions [gram.lex] */
3515 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3516 identifier. */
3518 static tree
3519 cp_parser_identifier (cp_parser* parser)
3521 cp_token *token;
3523 /* Look for the identifier. */
3524 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3525 /* Return the value. */
3526 return token ? token->u.value : error_mark_node;
3529 /* Parse a sequence of adjacent string constants. Returns a
3530 TREE_STRING representing the combined, nul-terminated string
3531 constant. If TRANSLATE is true, translate the string to the
3532 execution character set. If WIDE_OK is true, a wide string is
3533 invalid here.
3535 C++98 [lex.string] says that if a narrow string literal token is
3536 adjacent to a wide string literal token, the behavior is undefined.
3537 However, C99 6.4.5p4 says that this results in a wide string literal.
3538 We follow C99 here, for consistency with the C front end.
3540 This code is largely lifted from lex_string() in c-lex.c.
3542 FUTURE: ObjC++ will need to handle @-strings here. */
3543 static tree
3544 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3546 tree value;
3547 size_t count;
3548 struct obstack str_ob;
3549 cpp_string str, istr, *strs;
3550 cp_token *tok;
3551 enum cpp_ttype type, curr_type;
3552 int have_suffix_p = 0;
3553 tree string_tree;
3554 tree suffix_id = NULL_TREE;
3555 bool curr_tok_is_userdef_p = false;
3557 tok = cp_lexer_peek_token (parser->lexer);
3558 if (!cp_parser_is_string_literal (tok))
3560 cp_parser_error (parser, "expected string-literal");
3561 return error_mark_node;
3564 if (cpp_userdef_string_p (tok->type))
3566 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3567 curr_type = cpp_userdef_string_remove_type (tok->type);
3568 curr_tok_is_userdef_p = true;
3570 else
3572 string_tree = tok->u.value;
3573 curr_type = tok->type;
3575 type = curr_type;
3577 /* Try to avoid the overhead of creating and destroying an obstack
3578 for the common case of just one string. */
3579 if (!cp_parser_is_string_literal
3580 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3582 cp_lexer_consume_token (parser->lexer);
3584 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3585 str.len = TREE_STRING_LENGTH (string_tree);
3586 count = 1;
3588 if (curr_tok_is_userdef_p)
3590 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3591 have_suffix_p = 1;
3592 curr_type = cpp_userdef_string_remove_type (tok->type);
3594 else
3595 curr_type = tok->type;
3597 strs = &str;
3599 else
3601 gcc_obstack_init (&str_ob);
3602 count = 0;
3606 cp_lexer_consume_token (parser->lexer);
3607 count++;
3608 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3609 str.len = TREE_STRING_LENGTH (string_tree);
3611 if (curr_tok_is_userdef_p)
3613 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3614 if (have_suffix_p == 0)
3616 suffix_id = curr_suffix_id;
3617 have_suffix_p = 1;
3619 else if (have_suffix_p == 1
3620 && curr_suffix_id != suffix_id)
3622 error ("inconsistent user-defined literal suffixes"
3623 " %qD and %qD in string literal",
3624 suffix_id, curr_suffix_id);
3625 have_suffix_p = -1;
3627 curr_type = cpp_userdef_string_remove_type (tok->type);
3629 else
3630 curr_type = tok->type;
3632 if (type != curr_type)
3634 if (type == CPP_STRING)
3635 type = curr_type;
3636 else if (curr_type != CPP_STRING)
3637 error_at (tok->location,
3638 "unsupported non-standard concatenation "
3639 "of string literals");
3642 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3644 tok = cp_lexer_peek_token (parser->lexer);
3645 if (cpp_userdef_string_p (tok->type))
3647 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3648 curr_type = cpp_userdef_string_remove_type (tok->type);
3649 curr_tok_is_userdef_p = true;
3651 else
3653 string_tree = tok->u.value;
3654 curr_type = tok->type;
3655 curr_tok_is_userdef_p = false;
3658 while (cp_parser_is_string_literal (tok));
3660 strs = (cpp_string *) obstack_finish (&str_ob);
3663 if (type != CPP_STRING && !wide_ok)
3665 cp_parser_error (parser, "a wide string is invalid in this context");
3666 type = CPP_STRING;
3669 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3670 (parse_in, strs, count, &istr, type))
3672 value = build_string (istr.len, (const char *)istr.text);
3673 free (CONST_CAST (unsigned char *, istr.text));
3675 switch (type)
3677 default:
3678 case CPP_STRING:
3679 case CPP_UTF8STRING:
3680 TREE_TYPE (value) = char_array_type_node;
3681 break;
3682 case CPP_STRING16:
3683 TREE_TYPE (value) = char16_array_type_node;
3684 break;
3685 case CPP_STRING32:
3686 TREE_TYPE (value) = char32_array_type_node;
3687 break;
3688 case CPP_WSTRING:
3689 TREE_TYPE (value) = wchar_array_type_node;
3690 break;
3693 value = fix_string_type (value);
3695 if (have_suffix_p)
3697 tree literal = build_userdef_literal (suffix_id, value,
3698 OT_NONE, NULL_TREE);
3699 tok->u.value = literal;
3700 return cp_parser_userdef_string_literal (tok);
3703 else
3704 /* cpp_interpret_string has issued an error. */
3705 value = error_mark_node;
3707 if (count > 1)
3708 obstack_free (&str_ob, 0);
3710 return value;
3713 /* Look up a literal operator with the name and the exact arguments. */
3715 static tree
3716 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3718 tree decl, fns;
3719 decl = lookup_name (name);
3720 if (!decl || !is_overloaded_fn (decl))
3721 return error_mark_node;
3723 for (fns = decl; fns; fns = OVL_NEXT (fns))
3725 unsigned int ix;
3726 bool found = true;
3727 tree fn = OVL_CURRENT (fns);
3728 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3729 if (parmtypes != NULL_TREE)
3731 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3732 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3734 tree tparm = TREE_VALUE (parmtypes);
3735 tree targ = TREE_TYPE ((*args)[ix]);
3736 bool ptr = TYPE_PTR_P (tparm);
3737 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3738 if ((ptr || arr || !same_type_p (tparm, targ))
3739 && (!ptr || !arr
3740 || !same_type_p (TREE_TYPE (tparm),
3741 TREE_TYPE (targ))))
3742 found = false;
3744 if (found
3745 && ix == vec_safe_length (args)
3746 /* May be this should be sufficient_parms_p instead,
3747 depending on how exactly should user-defined literals
3748 work in presence of default arguments on the literal
3749 operator parameters. */
3750 && parmtypes == void_list_node)
3751 return fn;
3755 return error_mark_node;
3758 /* Parse a user-defined char constant. Returns a call to a user-defined
3759 literal operator taking the character as an argument. */
3761 static tree
3762 cp_parser_userdef_char_literal (cp_parser *parser)
3764 cp_token *token = cp_lexer_consume_token (parser->lexer);
3765 tree literal = token->u.value;
3766 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3767 tree value = USERDEF_LITERAL_VALUE (literal);
3768 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3769 tree decl, result;
3771 /* Build up a call to the user-defined operator */
3772 /* Lookup the name we got back from the id-expression. */
3773 vec<tree, va_gc> *args = make_tree_vector ();
3774 vec_safe_push (args, value);
3775 decl = lookup_literal_operator (name, args);
3776 if (!decl || decl == error_mark_node)
3778 error ("unable to find character literal operator %qD with %qT argument",
3779 name, TREE_TYPE (value));
3780 release_tree_vector (args);
3781 return error_mark_node;
3783 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3784 release_tree_vector (args);
3785 if (result != error_mark_node)
3786 return result;
3788 error ("unable to find character literal operator %qD with %qT argument",
3789 name, TREE_TYPE (value));
3790 return error_mark_node;
3793 /* A subroutine of cp_parser_userdef_numeric_literal to
3794 create a char... template parameter pack from a string node. */
3796 static tree
3797 make_char_string_pack (tree value)
3799 tree charvec;
3800 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3801 const char *str = TREE_STRING_POINTER (value);
3802 int i, len = TREE_STRING_LENGTH (value) - 1;
3803 tree argvec = make_tree_vec (1);
3805 /* Fill in CHARVEC with all of the parameters. */
3806 charvec = make_tree_vec (len);
3807 for (i = 0; i < len; ++i)
3808 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3810 /* Build the argument packs. */
3811 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3812 TREE_TYPE (argpack) = char_type_node;
3814 TREE_VEC_ELT (argvec, 0) = argpack;
3816 return argvec;
3819 /* A subroutine of cp_parser_userdef_numeric_literal to
3820 create a char... template parameter pack from a string node. */
3822 static tree
3823 make_string_pack (tree value)
3825 tree charvec;
3826 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3827 const unsigned char *str
3828 = (const unsigned char *) TREE_STRING_POINTER (value);
3829 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3830 int len = TREE_STRING_LENGTH (value) / sz - 1;
3831 tree argvec = make_tree_vec (2);
3833 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3834 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3836 /* First template parm is character type. */
3837 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3839 /* Fill in CHARVEC with all of the parameters. */
3840 charvec = make_tree_vec (len);
3841 for (int i = 0; i < len; ++i)
3842 TREE_VEC_ELT (charvec, i)
3843 = double_int_to_tree (str_char_type_node,
3844 double_int::from_buffer (str + i * sz, sz));
3846 /* Build the argument packs. */
3847 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3848 TREE_TYPE (argpack) = str_char_type_node;
3850 TREE_VEC_ELT (argvec, 1) = argpack;
3852 return argvec;
3855 /* Parse a user-defined numeric constant. returns a call to a user-defined
3856 literal operator. */
3858 static tree
3859 cp_parser_userdef_numeric_literal (cp_parser *parser)
3861 cp_token *token = cp_lexer_consume_token (parser->lexer);
3862 tree literal = token->u.value;
3863 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3864 tree value = USERDEF_LITERAL_VALUE (literal);
3865 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3866 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3867 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3868 tree decl, result;
3869 vec<tree, va_gc> *args;
3871 /* Look for a literal operator taking the exact type of numeric argument
3872 as the literal value. */
3873 args = make_tree_vector ();
3874 vec_safe_push (args, value);
3875 decl = lookup_literal_operator (name, args);
3876 if (decl && decl != error_mark_node)
3878 result = finish_call_expr (decl, &args, false, true, tf_none);
3879 if (result != error_mark_node)
3881 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3882 warning_at (token->location, OPT_Woverflow,
3883 "integer literal exceeds range of %qT type",
3884 long_long_unsigned_type_node);
3885 else
3887 if (overflow > 0)
3888 warning_at (token->location, OPT_Woverflow,
3889 "floating literal exceeds range of %qT type",
3890 long_double_type_node);
3891 else if (overflow < 0)
3892 warning_at (token->location, OPT_Woverflow,
3893 "floating literal truncated to zero");
3895 release_tree_vector (args);
3896 return result;
3899 release_tree_vector (args);
3901 /* If the numeric argument didn't work, look for a raw literal
3902 operator taking a const char* argument consisting of the number
3903 in string format. */
3904 args = make_tree_vector ();
3905 vec_safe_push (args, num_string);
3906 decl = lookup_literal_operator (name, args);
3907 if (decl && decl != error_mark_node)
3909 result = finish_call_expr (decl, &args, false, true, tf_none);
3910 if (result != error_mark_node)
3912 release_tree_vector (args);
3913 return result;
3916 release_tree_vector (args);
3918 /* If the raw literal didn't work, look for a non-type template
3919 function with parameter pack char.... Call the function with
3920 template parameter characters representing the number. */
3921 args = make_tree_vector ();
3922 decl = lookup_literal_operator (name, args);
3923 if (decl && decl != error_mark_node)
3925 tree tmpl_args = make_char_string_pack (num_string);
3926 decl = lookup_template_function (decl, tmpl_args);
3927 result = finish_call_expr (decl, &args, false, true, tf_none);
3928 if (result != error_mark_node)
3930 release_tree_vector (args);
3931 return result;
3934 release_tree_vector (args);
3936 error ("unable to find numeric literal operator %qD", name);
3937 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3938 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3939 "to enable more built-in suffixes");
3940 return error_mark_node;
3943 /* Parse a user-defined string constant. Returns a call to a user-defined
3944 literal operator taking a character pointer and the length of the string
3945 as arguments. */
3947 static tree
3948 cp_parser_userdef_string_literal (cp_token *token)
3950 tree literal = token->u.value;
3951 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3952 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3953 tree value = USERDEF_LITERAL_VALUE (literal);
3954 int len = TREE_STRING_LENGTH (value)
3955 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3956 tree decl, result;
3957 vec<tree, va_gc> *args;
3959 /* Look for a template function with typename parameter CharT
3960 and parameter pack CharT... Call the function with
3961 template parameter characters representing the string. */
3962 args = make_tree_vector ();
3963 decl = lookup_literal_operator (name, args);
3964 if (decl && decl != error_mark_node)
3966 tree tmpl_args = make_string_pack (value);
3967 decl = lookup_template_function (decl, tmpl_args);
3968 result = finish_call_expr (decl, &args, false, true, tf_none);
3969 if (result != error_mark_node)
3971 release_tree_vector (args);
3972 return result;
3975 release_tree_vector (args);
3977 /* Build up a call to the user-defined operator */
3978 /* Lookup the name we got back from the id-expression. */
3979 args = make_tree_vector ();
3980 vec_safe_push (args, value);
3981 vec_safe_push (args, build_int_cst (size_type_node, len));
3982 decl = lookup_name (name);
3983 if (!decl || decl == error_mark_node)
3985 error ("unable to find string literal operator %qD", name);
3986 release_tree_vector (args);
3987 return error_mark_node;
3989 result = finish_call_expr (decl, &args, false, true, tf_none);
3990 release_tree_vector (args);
3991 if (result != error_mark_node)
3992 return result;
3994 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3995 name, TREE_TYPE (value), size_type_node);
3996 return error_mark_node;
4000 /* Basic concepts [gram.basic] */
4002 /* Parse a translation-unit.
4004 translation-unit:
4005 declaration-seq [opt]
4007 Returns TRUE if all went well. */
4009 static bool
4010 cp_parser_translation_unit (cp_parser* parser)
4012 /* The address of the first non-permanent object on the declarator
4013 obstack. */
4014 static void *declarator_obstack_base;
4016 bool success;
4018 /* Create the declarator obstack, if necessary. */
4019 if (!cp_error_declarator)
4021 gcc_obstack_init (&declarator_obstack);
4022 /* Create the error declarator. */
4023 cp_error_declarator = make_declarator (cdk_error);
4024 /* Create the empty parameter list. */
4025 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4026 /* Remember where the base of the declarator obstack lies. */
4027 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4030 cp_parser_declaration_seq_opt (parser);
4032 /* If there are no tokens left then all went well. */
4033 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4035 /* Get rid of the token array; we don't need it any more. */
4036 cp_lexer_destroy (parser->lexer);
4037 parser->lexer = NULL;
4039 /* This file might have been a context that's implicitly extern
4040 "C". If so, pop the lang context. (Only relevant for PCH.) */
4041 if (parser->implicit_extern_c)
4043 pop_lang_context ();
4044 parser->implicit_extern_c = false;
4047 /* Finish up. */
4048 finish_translation_unit ();
4050 success = true;
4052 else
4054 cp_parser_error (parser, "expected declaration");
4055 success = false;
4058 /* Make sure the declarator obstack was fully cleaned up. */
4059 gcc_assert (obstack_next_free (&declarator_obstack)
4060 == declarator_obstack_base);
4062 /* All went well. */
4063 return success;
4066 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4067 decltype context. */
4069 static inline tsubst_flags_t
4070 complain_flags (bool decltype_p)
4072 tsubst_flags_t complain = tf_warning_or_error;
4073 if (decltype_p)
4074 complain |= tf_decltype;
4075 return complain;
4078 /* Expressions [gram.expr] */
4080 /* Parse a primary-expression.
4082 primary-expression:
4083 literal
4084 this
4085 ( expression )
4086 id-expression
4088 GNU Extensions:
4090 primary-expression:
4091 ( compound-statement )
4092 __builtin_va_arg ( assignment-expression , type-id )
4093 __builtin_offsetof ( type-id , offsetof-expression )
4095 C++ Extensions:
4096 __has_nothrow_assign ( type-id )
4097 __has_nothrow_constructor ( type-id )
4098 __has_nothrow_copy ( type-id )
4099 __has_trivial_assign ( type-id )
4100 __has_trivial_constructor ( type-id )
4101 __has_trivial_copy ( type-id )
4102 __has_trivial_destructor ( type-id )
4103 __has_virtual_destructor ( type-id )
4104 __is_abstract ( type-id )
4105 __is_base_of ( type-id , type-id )
4106 __is_class ( type-id )
4107 __is_convertible_to ( type-id , type-id )
4108 __is_empty ( type-id )
4109 __is_enum ( type-id )
4110 __is_final ( type-id )
4111 __is_literal_type ( type-id )
4112 __is_pod ( type-id )
4113 __is_polymorphic ( type-id )
4114 __is_std_layout ( type-id )
4115 __is_trivial ( type-id )
4116 __is_union ( type-id )
4118 Objective-C++ Extension:
4120 primary-expression:
4121 objc-expression
4123 literal:
4124 __null
4126 ADDRESS_P is true iff this expression was immediately preceded by
4127 "&" and therefore might denote a pointer-to-member. CAST_P is true
4128 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4129 true iff this expression is a template argument.
4131 Returns a representation of the expression. Upon return, *IDK
4132 indicates what kind of id-expression (if any) was present. */
4134 static tree
4135 cp_parser_primary_expression (cp_parser *parser,
4136 bool address_p,
4137 bool cast_p,
4138 bool template_arg_p,
4139 bool decltype_p,
4140 cp_id_kind *idk)
4142 cp_token *token = NULL;
4144 /* Assume the primary expression is not an id-expression. */
4145 *idk = CP_ID_KIND_NONE;
4147 /* Peek at the next token. */
4148 token = cp_lexer_peek_token (parser->lexer);
4149 switch (token->type)
4151 /* literal:
4152 integer-literal
4153 character-literal
4154 floating-literal
4155 string-literal
4156 boolean-literal
4157 pointer-literal
4158 user-defined-literal */
4159 case CPP_CHAR:
4160 case CPP_CHAR16:
4161 case CPP_CHAR32:
4162 case CPP_WCHAR:
4163 case CPP_NUMBER:
4164 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4165 return cp_parser_userdef_numeric_literal (parser);
4166 token = cp_lexer_consume_token (parser->lexer);
4167 if (TREE_CODE (token->u.value) == FIXED_CST)
4169 error_at (token->location,
4170 "fixed-point types not supported in C++");
4171 return error_mark_node;
4173 /* Floating-point literals are only allowed in an integral
4174 constant expression if they are cast to an integral or
4175 enumeration type. */
4176 if (TREE_CODE (token->u.value) == REAL_CST
4177 && parser->integral_constant_expression_p
4178 && pedantic)
4180 /* CAST_P will be set even in invalid code like "int(2.7 +
4181 ...)". Therefore, we have to check that the next token
4182 is sure to end the cast. */
4183 if (cast_p)
4185 cp_token *next_token;
4187 next_token = cp_lexer_peek_token (parser->lexer);
4188 if (/* The comma at the end of an
4189 enumerator-definition. */
4190 next_token->type != CPP_COMMA
4191 /* The curly brace at the end of an enum-specifier. */
4192 && next_token->type != CPP_CLOSE_BRACE
4193 /* The end of a statement. */
4194 && next_token->type != CPP_SEMICOLON
4195 /* The end of the cast-expression. */
4196 && next_token->type != CPP_CLOSE_PAREN
4197 /* The end of an array bound. */
4198 && next_token->type != CPP_CLOSE_SQUARE
4199 /* The closing ">" in a template-argument-list. */
4200 && (next_token->type != CPP_GREATER
4201 || parser->greater_than_is_operator_p)
4202 /* C++0x only: A ">>" treated like two ">" tokens,
4203 in a template-argument-list. */
4204 && (next_token->type != CPP_RSHIFT
4205 || (cxx_dialect == cxx98)
4206 || parser->greater_than_is_operator_p))
4207 cast_p = false;
4210 /* If we are within a cast, then the constraint that the
4211 cast is to an integral or enumeration type will be
4212 checked at that point. If we are not within a cast, then
4213 this code is invalid. */
4214 if (!cast_p)
4215 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4217 return token->u.value;
4219 case CPP_CHAR_USERDEF:
4220 case CPP_CHAR16_USERDEF:
4221 case CPP_CHAR32_USERDEF:
4222 case CPP_WCHAR_USERDEF:
4223 return cp_parser_userdef_char_literal (parser);
4225 case CPP_STRING:
4226 case CPP_STRING16:
4227 case CPP_STRING32:
4228 case CPP_WSTRING:
4229 case CPP_UTF8STRING:
4230 case CPP_STRING_USERDEF:
4231 case CPP_STRING16_USERDEF:
4232 case CPP_STRING32_USERDEF:
4233 case CPP_WSTRING_USERDEF:
4234 case CPP_UTF8STRING_USERDEF:
4235 /* ??? Should wide strings be allowed when parser->translate_strings_p
4236 is false (i.e. in attributes)? If not, we can kill the third
4237 argument to cp_parser_string_literal. */
4238 return cp_parser_string_literal (parser,
4239 parser->translate_strings_p,
4240 true);
4242 case CPP_OPEN_PAREN:
4244 tree expr;
4245 bool saved_greater_than_is_operator_p;
4247 /* Consume the `('. */
4248 cp_lexer_consume_token (parser->lexer);
4249 /* Within a parenthesized expression, a `>' token is always
4250 the greater-than operator. */
4251 saved_greater_than_is_operator_p
4252 = parser->greater_than_is_operator_p;
4253 parser->greater_than_is_operator_p = true;
4254 /* If we see `( { ' then we are looking at the beginning of
4255 a GNU statement-expression. */
4256 if (cp_parser_allow_gnu_extensions_p (parser)
4257 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4259 /* Statement-expressions are not allowed by the standard. */
4260 pedwarn (token->location, OPT_Wpedantic,
4261 "ISO C++ forbids braced-groups within expressions");
4263 /* And they're not allowed outside of a function-body; you
4264 cannot, for example, write:
4266 int i = ({ int j = 3; j + 1; });
4268 at class or namespace scope. */
4269 if (!parser->in_function_body
4270 || parser->in_template_argument_list_p)
4272 error_at (token->location,
4273 "statement-expressions are not allowed outside "
4274 "functions nor in template-argument lists");
4275 cp_parser_skip_to_end_of_block_or_statement (parser);
4276 expr = error_mark_node;
4278 else
4280 /* Start the statement-expression. */
4281 expr = begin_stmt_expr ();
4282 /* Parse the compound-statement. */
4283 cp_parser_compound_statement (parser, expr, false, false);
4284 /* Finish up. */
4285 expr = finish_stmt_expr (expr, false);
4288 else
4290 /* Parse the parenthesized expression. */
4291 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4292 /* Let the front end know that this expression was
4293 enclosed in parentheses. This matters in case, for
4294 example, the expression is of the form `A::B', since
4295 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4296 not. */
4297 expr = finish_parenthesized_expr (expr);
4298 /* DR 705: Wrapping an unqualified name in parentheses
4299 suppresses arg-dependent lookup. We want to pass back
4300 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4301 (c++/37862), but none of the others. */
4302 if (*idk != CP_ID_KIND_QUALIFIED)
4303 *idk = CP_ID_KIND_NONE;
4305 /* The `>' token might be the end of a template-id or
4306 template-parameter-list now. */
4307 parser->greater_than_is_operator_p
4308 = saved_greater_than_is_operator_p;
4309 /* Consume the `)'. */
4310 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4311 cp_parser_skip_to_end_of_statement (parser);
4313 return expr;
4316 case CPP_OPEN_SQUARE:
4317 if (c_dialect_objc ())
4318 /* We have an Objective-C++ message. */
4319 return cp_parser_objc_expression (parser);
4321 tree lam = cp_parser_lambda_expression (parser);
4322 /* Don't warn about a failed tentative parse. */
4323 if (cp_parser_error_occurred (parser))
4324 return error_mark_node;
4325 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4326 return lam;
4329 case CPP_OBJC_STRING:
4330 if (c_dialect_objc ())
4331 /* We have an Objective-C++ string literal. */
4332 return cp_parser_objc_expression (parser);
4333 cp_parser_error (parser, "expected primary-expression");
4334 return error_mark_node;
4336 case CPP_KEYWORD:
4337 switch (token->keyword)
4339 /* These two are the boolean literals. */
4340 case RID_TRUE:
4341 cp_lexer_consume_token (parser->lexer);
4342 return boolean_true_node;
4343 case RID_FALSE:
4344 cp_lexer_consume_token (parser->lexer);
4345 return boolean_false_node;
4347 /* The `__null' literal. */
4348 case RID_NULL:
4349 cp_lexer_consume_token (parser->lexer);
4350 return null_node;
4352 /* The `nullptr' literal. */
4353 case RID_NULLPTR:
4354 cp_lexer_consume_token (parser->lexer);
4355 return nullptr_node;
4357 /* Recognize the `this' keyword. */
4358 case RID_THIS:
4359 cp_lexer_consume_token (parser->lexer);
4360 if (parser->local_variables_forbidden_p)
4362 error_at (token->location,
4363 "%<this%> may not be used in this context");
4364 return error_mark_node;
4366 /* Pointers cannot appear in constant-expressions. */
4367 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4368 return error_mark_node;
4369 return finish_this_expr ();
4371 /* The `operator' keyword can be the beginning of an
4372 id-expression. */
4373 case RID_OPERATOR:
4374 goto id_expression;
4376 case RID_FUNCTION_NAME:
4377 case RID_PRETTY_FUNCTION_NAME:
4378 case RID_C99_FUNCTION_NAME:
4380 non_integral_constant name;
4382 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4383 __func__ are the names of variables -- but they are
4384 treated specially. Therefore, they are handled here,
4385 rather than relying on the generic id-expression logic
4386 below. Grammatically, these names are id-expressions.
4388 Consume the token. */
4389 token = cp_lexer_consume_token (parser->lexer);
4391 switch (token->keyword)
4393 case RID_FUNCTION_NAME:
4394 name = NIC_FUNC_NAME;
4395 break;
4396 case RID_PRETTY_FUNCTION_NAME:
4397 name = NIC_PRETTY_FUNC;
4398 break;
4399 case RID_C99_FUNCTION_NAME:
4400 name = NIC_C99_FUNC;
4401 break;
4402 default:
4403 gcc_unreachable ();
4406 if (cp_parser_non_integral_constant_expression (parser, name))
4407 return error_mark_node;
4409 /* Look up the name. */
4410 return finish_fname (token->u.value);
4413 case RID_VA_ARG:
4415 tree expression;
4416 tree type;
4417 source_location type_location;
4419 /* The `__builtin_va_arg' construct is used to handle
4420 `va_arg'. Consume the `__builtin_va_arg' token. */
4421 cp_lexer_consume_token (parser->lexer);
4422 /* Look for the opening `('. */
4423 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4424 /* Now, parse the assignment-expression. */
4425 expression = cp_parser_assignment_expression (parser,
4426 /*cast_p=*/false, NULL);
4427 /* Look for the `,'. */
4428 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4429 type_location = cp_lexer_peek_token (parser->lexer)->location;
4430 /* Parse the type-id. */
4431 type = cp_parser_type_id (parser);
4432 /* Look for the closing `)'. */
4433 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4434 /* Using `va_arg' in a constant-expression is not
4435 allowed. */
4436 if (cp_parser_non_integral_constant_expression (parser,
4437 NIC_VA_ARG))
4438 return error_mark_node;
4439 return build_x_va_arg (type_location, expression, type);
4442 case RID_OFFSETOF:
4443 return cp_parser_builtin_offsetof (parser);
4445 case RID_HAS_NOTHROW_ASSIGN:
4446 case RID_HAS_NOTHROW_CONSTRUCTOR:
4447 case RID_HAS_NOTHROW_COPY:
4448 case RID_HAS_TRIVIAL_ASSIGN:
4449 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4450 case RID_HAS_TRIVIAL_COPY:
4451 case RID_HAS_TRIVIAL_DESTRUCTOR:
4452 case RID_HAS_VIRTUAL_DESTRUCTOR:
4453 case RID_IS_ABSTRACT:
4454 case RID_IS_BASE_OF:
4455 case RID_IS_CLASS:
4456 case RID_IS_CONVERTIBLE_TO:
4457 case RID_IS_EMPTY:
4458 case RID_IS_ENUM:
4459 case RID_IS_FINAL:
4460 case RID_IS_LITERAL_TYPE:
4461 case RID_IS_POD:
4462 case RID_IS_POLYMORPHIC:
4463 case RID_IS_STD_LAYOUT:
4464 case RID_IS_TRIVIAL:
4465 case RID_IS_UNION:
4466 return cp_parser_trait_expr (parser, token->keyword);
4468 /* Objective-C++ expressions. */
4469 case RID_AT_ENCODE:
4470 case RID_AT_PROTOCOL:
4471 case RID_AT_SELECTOR:
4472 return cp_parser_objc_expression (parser);
4474 case RID_TEMPLATE:
4475 if (parser->in_function_body
4476 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4477 == CPP_LESS))
4479 error_at (token->location,
4480 "a template declaration cannot appear at block scope");
4481 cp_parser_skip_to_end_of_block_or_statement (parser);
4482 return error_mark_node;
4484 default:
4485 cp_parser_error (parser, "expected primary-expression");
4486 return error_mark_node;
4489 /* An id-expression can start with either an identifier, a
4490 `::' as the beginning of a qualified-id, or the "operator"
4491 keyword. */
4492 case CPP_NAME:
4493 case CPP_SCOPE:
4494 case CPP_TEMPLATE_ID:
4495 case CPP_NESTED_NAME_SPECIFIER:
4497 tree id_expression;
4498 tree decl;
4499 const char *error_msg;
4500 bool template_p;
4501 bool done;
4502 cp_token *id_expr_token;
4504 id_expression:
4505 /* Parse the id-expression. */
4506 id_expression
4507 = cp_parser_id_expression (parser,
4508 /*template_keyword_p=*/false,
4509 /*check_dependency_p=*/true,
4510 &template_p,
4511 /*declarator_p=*/false,
4512 /*optional_p=*/false);
4513 if (id_expression == error_mark_node)
4514 return error_mark_node;
4515 id_expr_token = token;
4516 token = cp_lexer_peek_token (parser->lexer);
4517 done = (token->type != CPP_OPEN_SQUARE
4518 && token->type != CPP_OPEN_PAREN
4519 && token->type != CPP_DOT
4520 && token->type != CPP_DEREF
4521 && token->type != CPP_PLUS_PLUS
4522 && token->type != CPP_MINUS_MINUS);
4523 /* If we have a template-id, then no further lookup is
4524 required. If the template-id was for a template-class, we
4525 will sometimes have a TYPE_DECL at this point. */
4526 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4527 || TREE_CODE (id_expression) == TYPE_DECL)
4528 decl = id_expression;
4529 /* Look up the name. */
4530 else
4532 tree ambiguous_decls;
4534 /* If we already know that this lookup is ambiguous, then
4535 we've already issued an error message; there's no reason
4536 to check again. */
4537 if (id_expr_token->type == CPP_NAME
4538 && id_expr_token->ambiguous_p)
4540 cp_parser_simulate_error (parser);
4541 return error_mark_node;
4544 decl = cp_parser_lookup_name (parser, id_expression,
4545 none_type,
4546 template_p,
4547 /*is_namespace=*/false,
4548 /*check_dependency=*/true,
4549 &ambiguous_decls,
4550 id_expr_token->location);
4551 /* If the lookup was ambiguous, an error will already have
4552 been issued. */
4553 if (ambiguous_decls)
4554 return error_mark_node;
4556 /* In Objective-C++, we may have an Objective-C 2.0
4557 dot-syntax for classes here. */
4558 if (c_dialect_objc ()
4559 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4560 && TREE_CODE (decl) == TYPE_DECL
4561 && objc_is_class_name (decl))
4563 tree component;
4564 cp_lexer_consume_token (parser->lexer);
4565 component = cp_parser_identifier (parser);
4566 if (component == error_mark_node)
4567 return error_mark_node;
4569 return objc_build_class_component_ref (id_expression, component);
4572 /* In Objective-C++, an instance variable (ivar) may be preferred
4573 to whatever cp_parser_lookup_name() found. */
4574 decl = objc_lookup_ivar (decl, id_expression);
4576 /* If name lookup gives us a SCOPE_REF, then the
4577 qualifying scope was dependent. */
4578 if (TREE_CODE (decl) == SCOPE_REF)
4580 /* At this point, we do not know if DECL is a valid
4581 integral constant expression. We assume that it is
4582 in fact such an expression, so that code like:
4584 template <int N> struct A {
4585 int a[B<N>::i];
4588 is accepted. At template-instantiation time, we
4589 will check that B<N>::i is actually a constant. */
4590 return decl;
4592 /* Check to see if DECL is a local variable in a context
4593 where that is forbidden. */
4594 if (parser->local_variables_forbidden_p
4595 && local_variable_p (decl))
4597 /* It might be that we only found DECL because we are
4598 trying to be generous with pre-ISO scoping rules.
4599 For example, consider:
4601 int i;
4602 void g() {
4603 for (int i = 0; i < 10; ++i) {}
4604 extern void f(int j = i);
4607 Here, name look up will originally find the out
4608 of scope `i'. We need to issue a warning message,
4609 but then use the global `i'. */
4610 decl = check_for_out_of_scope_variable (decl);
4611 if (local_variable_p (decl))
4613 error_at (id_expr_token->location,
4614 "local variable %qD may not appear in this context",
4615 decl);
4616 return error_mark_node;
4621 decl = (finish_id_expression
4622 (id_expression, decl, parser->scope,
4623 idk,
4624 parser->integral_constant_expression_p,
4625 parser->allow_non_integral_constant_expression_p,
4626 &parser->non_integral_constant_expression_p,
4627 template_p, done, address_p,
4628 template_arg_p,
4629 &error_msg,
4630 id_expr_token->location));
4631 if (error_msg)
4632 cp_parser_error (parser, error_msg);
4633 return decl;
4636 /* Anything else is an error. */
4637 default:
4638 cp_parser_error (parser, "expected primary-expression");
4639 return error_mark_node;
4643 static inline tree
4644 cp_parser_primary_expression (cp_parser *parser,
4645 bool address_p,
4646 bool cast_p,
4647 bool template_arg_p,
4648 cp_id_kind *idk)
4650 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4651 /*decltype*/false, idk);
4654 /* Parse an id-expression.
4656 id-expression:
4657 unqualified-id
4658 qualified-id
4660 qualified-id:
4661 :: [opt] nested-name-specifier template [opt] unqualified-id
4662 :: identifier
4663 :: operator-function-id
4664 :: template-id
4666 Return a representation of the unqualified portion of the
4667 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4668 a `::' or nested-name-specifier.
4670 Often, if the id-expression was a qualified-id, the caller will
4671 want to make a SCOPE_REF to represent the qualified-id. This
4672 function does not do this in order to avoid wastefully creating
4673 SCOPE_REFs when they are not required.
4675 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4676 `template' keyword.
4678 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4679 uninstantiated templates.
4681 If *TEMPLATE_P is non-NULL, it is set to true iff the
4682 `template' keyword is used to explicitly indicate that the entity
4683 named is a template.
4685 If DECLARATOR_P is true, the id-expression is appearing as part of
4686 a declarator, rather than as part of an expression. */
4688 static tree
4689 cp_parser_id_expression (cp_parser *parser,
4690 bool template_keyword_p,
4691 bool check_dependency_p,
4692 bool *template_p,
4693 bool declarator_p,
4694 bool optional_p)
4696 bool global_scope_p;
4697 bool nested_name_specifier_p;
4699 /* Assume the `template' keyword was not used. */
4700 if (template_p)
4701 *template_p = template_keyword_p;
4703 /* Look for the optional `::' operator. */
4704 global_scope_p
4705 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4706 != NULL_TREE);
4707 /* Look for the optional nested-name-specifier. */
4708 nested_name_specifier_p
4709 = (cp_parser_nested_name_specifier_opt (parser,
4710 /*typename_keyword_p=*/false,
4711 check_dependency_p,
4712 /*type_p=*/false,
4713 declarator_p)
4714 != NULL_TREE);
4715 /* If there is a nested-name-specifier, then we are looking at
4716 the first qualified-id production. */
4717 if (nested_name_specifier_p)
4719 tree saved_scope;
4720 tree saved_object_scope;
4721 tree saved_qualifying_scope;
4722 tree unqualified_id;
4723 bool is_template;
4725 /* See if the next token is the `template' keyword. */
4726 if (!template_p)
4727 template_p = &is_template;
4728 *template_p = cp_parser_optional_template_keyword (parser);
4729 /* Name lookup we do during the processing of the
4730 unqualified-id might obliterate SCOPE. */
4731 saved_scope = parser->scope;
4732 saved_object_scope = parser->object_scope;
4733 saved_qualifying_scope = parser->qualifying_scope;
4734 /* Process the final unqualified-id. */
4735 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4736 check_dependency_p,
4737 declarator_p,
4738 /*optional_p=*/false);
4739 /* Restore the SAVED_SCOPE for our caller. */
4740 parser->scope = saved_scope;
4741 parser->object_scope = saved_object_scope;
4742 parser->qualifying_scope = saved_qualifying_scope;
4744 return unqualified_id;
4746 /* Otherwise, if we are in global scope, then we are looking at one
4747 of the other qualified-id productions. */
4748 else if (global_scope_p)
4750 cp_token *token;
4751 tree id;
4753 /* Peek at the next token. */
4754 token = cp_lexer_peek_token (parser->lexer);
4756 /* If it's an identifier, and the next token is not a "<", then
4757 we can avoid the template-id case. This is an optimization
4758 for this common case. */
4759 if (token->type == CPP_NAME
4760 && !cp_parser_nth_token_starts_template_argument_list_p
4761 (parser, 2))
4762 return cp_parser_identifier (parser);
4764 cp_parser_parse_tentatively (parser);
4765 /* Try a template-id. */
4766 id = cp_parser_template_id (parser,
4767 /*template_keyword_p=*/false,
4768 /*check_dependency_p=*/true,
4769 none_type,
4770 declarator_p);
4771 /* If that worked, we're done. */
4772 if (cp_parser_parse_definitely (parser))
4773 return id;
4775 /* Peek at the next token. (Changes in the token buffer may
4776 have invalidated the pointer obtained above.) */
4777 token = cp_lexer_peek_token (parser->lexer);
4779 switch (token->type)
4781 case CPP_NAME:
4782 return cp_parser_identifier (parser);
4784 case CPP_KEYWORD:
4785 if (token->keyword == RID_OPERATOR)
4786 return cp_parser_operator_function_id (parser);
4787 /* Fall through. */
4789 default:
4790 cp_parser_error (parser, "expected id-expression");
4791 return error_mark_node;
4794 else
4795 return cp_parser_unqualified_id (parser, template_keyword_p,
4796 /*check_dependency_p=*/true,
4797 declarator_p,
4798 optional_p);
4801 /* Parse an unqualified-id.
4803 unqualified-id:
4804 identifier
4805 operator-function-id
4806 conversion-function-id
4807 ~ class-name
4808 template-id
4810 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4811 keyword, in a construct like `A::template ...'.
4813 Returns a representation of unqualified-id. For the `identifier'
4814 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4815 production a BIT_NOT_EXPR is returned; the operand of the
4816 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4817 other productions, see the documentation accompanying the
4818 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4819 names are looked up in uninstantiated templates. If DECLARATOR_P
4820 is true, the unqualified-id is appearing as part of a declarator,
4821 rather than as part of an expression. */
4823 static tree
4824 cp_parser_unqualified_id (cp_parser* parser,
4825 bool template_keyword_p,
4826 bool check_dependency_p,
4827 bool declarator_p,
4828 bool optional_p)
4830 cp_token *token;
4832 /* Peek at the next token. */
4833 token = cp_lexer_peek_token (parser->lexer);
4835 switch (token->type)
4837 case CPP_NAME:
4839 tree id;
4841 /* We don't know yet whether or not this will be a
4842 template-id. */
4843 cp_parser_parse_tentatively (parser);
4844 /* Try a template-id. */
4845 id = cp_parser_template_id (parser, template_keyword_p,
4846 check_dependency_p,
4847 none_type,
4848 declarator_p);
4849 /* If it worked, we're done. */
4850 if (cp_parser_parse_definitely (parser))
4851 return id;
4852 /* Otherwise, it's an ordinary identifier. */
4853 return cp_parser_identifier (parser);
4856 case CPP_TEMPLATE_ID:
4857 return cp_parser_template_id (parser, template_keyword_p,
4858 check_dependency_p,
4859 none_type,
4860 declarator_p);
4862 case CPP_COMPL:
4864 tree type_decl;
4865 tree qualifying_scope;
4866 tree object_scope;
4867 tree scope;
4868 bool done;
4870 /* Consume the `~' token. */
4871 cp_lexer_consume_token (parser->lexer);
4872 /* Parse the class-name. The standard, as written, seems to
4873 say that:
4875 template <typename T> struct S { ~S (); };
4876 template <typename T> S<T>::~S() {}
4878 is invalid, since `~' must be followed by a class-name, but
4879 `S<T>' is dependent, and so not known to be a class.
4880 That's not right; we need to look in uninstantiated
4881 templates. A further complication arises from:
4883 template <typename T> void f(T t) {
4884 t.T::~T();
4887 Here, it is not possible to look up `T' in the scope of `T'
4888 itself. We must look in both the current scope, and the
4889 scope of the containing complete expression.
4891 Yet another issue is:
4893 struct S {
4894 int S;
4895 ~S();
4898 S::~S() {}
4900 The standard does not seem to say that the `S' in `~S'
4901 should refer to the type `S' and not the data member
4902 `S::S'. */
4904 /* DR 244 says that we look up the name after the "~" in the
4905 same scope as we looked up the qualifying name. That idea
4906 isn't fully worked out; it's more complicated than that. */
4907 scope = parser->scope;
4908 object_scope = parser->object_scope;
4909 qualifying_scope = parser->qualifying_scope;
4911 /* Check for invalid scopes. */
4912 if (scope == error_mark_node)
4914 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4915 cp_lexer_consume_token (parser->lexer);
4916 return error_mark_node;
4918 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4921 error_at (token->location,
4922 "scope %qT before %<~%> is not a class-name",
4923 scope);
4924 cp_parser_simulate_error (parser);
4925 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4926 cp_lexer_consume_token (parser->lexer);
4927 return error_mark_node;
4929 gcc_assert (!scope || TYPE_P (scope));
4931 /* If the name is of the form "X::~X" it's OK even if X is a
4932 typedef. */
4933 token = cp_lexer_peek_token (parser->lexer);
4934 if (scope
4935 && token->type == CPP_NAME
4936 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4937 != CPP_LESS)
4938 && (token->u.value == TYPE_IDENTIFIER (scope)
4939 || (CLASS_TYPE_P (scope)
4940 && constructor_name_p (token->u.value, scope))))
4942 cp_lexer_consume_token (parser->lexer);
4943 return build_nt (BIT_NOT_EXPR, scope);
4946 /* ~auto means the destructor of whatever the object is. */
4947 if (cp_parser_is_keyword (token, RID_AUTO))
4949 if (cxx_dialect < cxx1y)
4950 pedwarn (input_location, 0,
4951 "%<~auto%> only available with "
4952 "-std=c++1y or -std=gnu++1y");
4953 cp_lexer_consume_token (parser->lexer);
4954 return build_nt (BIT_NOT_EXPR, make_auto ());
4957 /* If there was an explicit qualification (S::~T), first look
4958 in the scope given by the qualification (i.e., S).
4960 Note: in the calls to cp_parser_class_name below we pass
4961 typename_type so that lookup finds the injected-class-name
4962 rather than the constructor. */
4963 done = false;
4964 type_decl = NULL_TREE;
4965 if (scope)
4967 cp_parser_parse_tentatively (parser);
4968 type_decl = cp_parser_class_name (parser,
4969 /*typename_keyword_p=*/false,
4970 /*template_keyword_p=*/false,
4971 typename_type,
4972 /*check_dependency=*/false,
4973 /*class_head_p=*/false,
4974 declarator_p);
4975 if (cp_parser_parse_definitely (parser))
4976 done = true;
4978 /* In "N::S::~S", look in "N" as well. */
4979 if (!done && scope && qualifying_scope)
4981 cp_parser_parse_tentatively (parser);
4982 parser->scope = qualifying_scope;
4983 parser->object_scope = NULL_TREE;
4984 parser->qualifying_scope = NULL_TREE;
4985 type_decl
4986 = cp_parser_class_name (parser,
4987 /*typename_keyword_p=*/false,
4988 /*template_keyword_p=*/false,
4989 typename_type,
4990 /*check_dependency=*/false,
4991 /*class_head_p=*/false,
4992 declarator_p);
4993 if (cp_parser_parse_definitely (parser))
4994 done = true;
4996 /* In "p->S::~T", look in the scope given by "*p" as well. */
4997 else if (!done && object_scope)
4999 cp_parser_parse_tentatively (parser);
5000 parser->scope = object_scope;
5001 parser->object_scope = NULL_TREE;
5002 parser->qualifying_scope = NULL_TREE;
5003 type_decl
5004 = cp_parser_class_name (parser,
5005 /*typename_keyword_p=*/false,
5006 /*template_keyword_p=*/false,
5007 typename_type,
5008 /*check_dependency=*/false,
5009 /*class_head_p=*/false,
5010 declarator_p);
5011 if (cp_parser_parse_definitely (parser))
5012 done = true;
5014 /* Look in the surrounding context. */
5015 if (!done)
5017 parser->scope = NULL_TREE;
5018 parser->object_scope = NULL_TREE;
5019 parser->qualifying_scope = NULL_TREE;
5020 if (processing_template_decl)
5021 cp_parser_parse_tentatively (parser);
5022 type_decl
5023 = cp_parser_class_name (parser,
5024 /*typename_keyword_p=*/false,
5025 /*template_keyword_p=*/false,
5026 typename_type,
5027 /*check_dependency=*/false,
5028 /*class_head_p=*/false,
5029 declarator_p);
5030 if (processing_template_decl
5031 && ! cp_parser_parse_definitely (parser))
5033 /* We couldn't find a type with this name, so just accept
5034 it and check for a match at instantiation time. */
5035 type_decl = cp_parser_identifier (parser);
5036 if (type_decl != error_mark_node)
5037 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5038 return type_decl;
5041 /* If an error occurred, assume that the name of the
5042 destructor is the same as the name of the qualifying
5043 class. That allows us to keep parsing after running
5044 into ill-formed destructor names. */
5045 if (type_decl == error_mark_node && scope)
5046 return build_nt (BIT_NOT_EXPR, scope);
5047 else if (type_decl == error_mark_node)
5048 return error_mark_node;
5050 /* Check that destructor name and scope match. */
5051 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5054 error_at (token->location,
5055 "declaration of %<~%T%> as member of %qT",
5056 type_decl, scope);
5057 cp_parser_simulate_error (parser);
5058 return error_mark_node;
5061 /* [class.dtor]
5063 A typedef-name that names a class shall not be used as the
5064 identifier in the declarator for a destructor declaration. */
5065 if (declarator_p
5066 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5067 && !DECL_SELF_REFERENCE_P (type_decl)
5068 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5069 error_at (token->location,
5070 "typedef-name %qD used as destructor declarator",
5071 type_decl);
5073 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5076 case CPP_KEYWORD:
5077 if (token->keyword == RID_OPERATOR)
5079 tree id;
5081 /* This could be a template-id, so we try that first. */
5082 cp_parser_parse_tentatively (parser);
5083 /* Try a template-id. */
5084 id = cp_parser_template_id (parser, template_keyword_p,
5085 /*check_dependency_p=*/true,
5086 none_type,
5087 declarator_p);
5088 /* If that worked, we're done. */
5089 if (cp_parser_parse_definitely (parser))
5090 return id;
5091 /* We still don't know whether we're looking at an
5092 operator-function-id or a conversion-function-id. */
5093 cp_parser_parse_tentatively (parser);
5094 /* Try an operator-function-id. */
5095 id = cp_parser_operator_function_id (parser);
5096 /* If that didn't work, try a conversion-function-id. */
5097 if (!cp_parser_parse_definitely (parser))
5098 id = cp_parser_conversion_function_id (parser);
5099 else if (UDLIT_OPER_P (id))
5101 /* 17.6.3.3.5 */
5102 const char *name = UDLIT_OP_SUFFIX (id);
5103 if (name[0] != '_' && !in_system_header_at (input_location)
5104 && declarator_p)
5105 warning (0, "literal operator suffixes not preceded by %<_%>"
5106 " are reserved for future standardization");
5109 return id;
5111 /* Fall through. */
5113 default:
5114 if (optional_p)
5115 return NULL_TREE;
5116 cp_parser_error (parser, "expected unqualified-id");
5117 return error_mark_node;
5121 /* Parse an (optional) nested-name-specifier.
5123 nested-name-specifier: [C++98]
5124 class-or-namespace-name :: nested-name-specifier [opt]
5125 class-or-namespace-name :: template nested-name-specifier [opt]
5127 nested-name-specifier: [C++0x]
5128 type-name ::
5129 namespace-name ::
5130 nested-name-specifier identifier ::
5131 nested-name-specifier template [opt] simple-template-id ::
5133 PARSER->SCOPE should be set appropriately before this function is
5134 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5135 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5136 in name lookups.
5138 Sets PARSER->SCOPE to the class (TYPE) or namespace
5139 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5140 it unchanged if there is no nested-name-specifier. Returns the new
5141 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5143 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5144 part of a declaration and/or decl-specifier. */
5146 static tree
5147 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5148 bool typename_keyword_p,
5149 bool check_dependency_p,
5150 bool type_p,
5151 bool is_declaration)
5153 bool success = false;
5154 cp_token_position start = 0;
5155 cp_token *token;
5157 /* Remember where the nested-name-specifier starts. */
5158 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5160 start = cp_lexer_token_position (parser->lexer, false);
5161 push_deferring_access_checks (dk_deferred);
5164 while (true)
5166 tree new_scope;
5167 tree old_scope;
5168 tree saved_qualifying_scope;
5169 bool template_keyword_p;
5171 /* Spot cases that cannot be the beginning of a
5172 nested-name-specifier. */
5173 token = cp_lexer_peek_token (parser->lexer);
5175 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5176 the already parsed nested-name-specifier. */
5177 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5179 /* Grab the nested-name-specifier and continue the loop. */
5180 cp_parser_pre_parsed_nested_name_specifier (parser);
5181 /* If we originally encountered this nested-name-specifier
5182 with IS_DECLARATION set to false, we will not have
5183 resolved TYPENAME_TYPEs, so we must do so here. */
5184 if (is_declaration
5185 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5187 new_scope = resolve_typename_type (parser->scope,
5188 /*only_current_p=*/false);
5189 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5190 parser->scope = new_scope;
5192 success = true;
5193 continue;
5196 /* Spot cases that cannot be the beginning of a
5197 nested-name-specifier. On the second and subsequent times
5198 through the loop, we look for the `template' keyword. */
5199 if (success && token->keyword == RID_TEMPLATE)
5201 /* A template-id can start a nested-name-specifier. */
5202 else if (token->type == CPP_TEMPLATE_ID)
5204 /* DR 743: decltype can be used in a nested-name-specifier. */
5205 else if (token_is_decltype (token))
5207 else
5209 /* If the next token is not an identifier, then it is
5210 definitely not a type-name or namespace-name. */
5211 if (token->type != CPP_NAME)
5212 break;
5213 /* If the following token is neither a `<' (to begin a
5214 template-id), nor a `::', then we are not looking at a
5215 nested-name-specifier. */
5216 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5218 if (token->type == CPP_COLON
5219 && parser->colon_corrects_to_scope_p
5220 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5222 error_at (token->location,
5223 "found %<:%> in nested-name-specifier, expected %<::%>");
5224 token->type = CPP_SCOPE;
5227 if (token->type != CPP_SCOPE
5228 && !cp_parser_nth_token_starts_template_argument_list_p
5229 (parser, 2))
5230 break;
5233 /* The nested-name-specifier is optional, so we parse
5234 tentatively. */
5235 cp_parser_parse_tentatively (parser);
5237 /* Look for the optional `template' keyword, if this isn't the
5238 first time through the loop. */
5239 if (success)
5240 template_keyword_p = cp_parser_optional_template_keyword (parser);
5241 else
5242 template_keyword_p = false;
5244 /* Save the old scope since the name lookup we are about to do
5245 might destroy it. */
5246 old_scope = parser->scope;
5247 saved_qualifying_scope = parser->qualifying_scope;
5248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5249 look up names in "X<T>::I" in order to determine that "Y" is
5250 a template. So, if we have a typename at this point, we make
5251 an effort to look through it. */
5252 if (is_declaration
5253 && !typename_keyword_p
5254 && parser->scope
5255 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5256 parser->scope = resolve_typename_type (parser->scope,
5257 /*only_current_p=*/false);
5258 /* Parse the qualifying entity. */
5259 new_scope
5260 = cp_parser_qualifying_entity (parser,
5261 typename_keyword_p,
5262 template_keyword_p,
5263 check_dependency_p,
5264 type_p,
5265 is_declaration);
5266 /* Look for the `::' token. */
5267 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5269 /* If we found what we wanted, we keep going; otherwise, we're
5270 done. */
5271 if (!cp_parser_parse_definitely (parser))
5273 bool error_p = false;
5275 /* Restore the OLD_SCOPE since it was valid before the
5276 failed attempt at finding the last
5277 class-or-namespace-name. */
5278 parser->scope = old_scope;
5279 parser->qualifying_scope = saved_qualifying_scope;
5281 /* If the next token is a decltype, and the one after that is a
5282 `::', then the decltype has failed to resolve to a class or
5283 enumeration type. Give this error even when parsing
5284 tentatively since it can't possibly be valid--and we're going
5285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5286 won't get another chance.*/
5287 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5288 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5289 == CPP_SCOPE))
5291 token = cp_lexer_consume_token (parser->lexer);
5292 error_at (token->location, "decltype evaluates to %qT, "
5293 "which is not a class or enumeration type",
5294 token->u.value);
5295 parser->scope = error_mark_node;
5296 error_p = true;
5297 /* As below. */
5298 success = true;
5299 cp_lexer_consume_token (parser->lexer);
5302 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5303 break;
5304 /* If the next token is an identifier, and the one after
5305 that is a `::', then any valid interpretation would have
5306 found a class-or-namespace-name. */
5307 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5308 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5309 == CPP_SCOPE)
5310 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5311 != CPP_COMPL))
5313 token = cp_lexer_consume_token (parser->lexer);
5314 if (!error_p)
5316 if (!token->ambiguous_p)
5318 tree decl;
5319 tree ambiguous_decls;
5321 decl = cp_parser_lookup_name (parser, token->u.value,
5322 none_type,
5323 /*is_template=*/false,
5324 /*is_namespace=*/false,
5325 /*check_dependency=*/true,
5326 &ambiguous_decls,
5327 token->location);
5328 if (TREE_CODE (decl) == TEMPLATE_DECL)
5329 error_at (token->location,
5330 "%qD used without template parameters",
5331 decl);
5332 else if (ambiguous_decls)
5334 // cp_parser_lookup_name has the same diagnostic,
5335 // thus make sure to emit it at most once.
5336 if (cp_parser_uncommitted_to_tentative_parse_p
5337 (parser))
5339 error_at (token->location,
5340 "reference to %qD is ambiguous",
5341 token->u.value);
5342 print_candidates (ambiguous_decls);
5344 decl = error_mark_node;
5346 else
5348 if (cxx_dialect != cxx98)
5349 cp_parser_name_lookup_error
5350 (parser, token->u.value, decl, NLE_NOT_CXX98,
5351 token->location);
5352 else
5353 cp_parser_name_lookup_error
5354 (parser, token->u.value, decl, NLE_CXX98,
5355 token->location);
5358 parser->scope = error_mark_node;
5359 error_p = true;
5360 /* Treat this as a successful nested-name-specifier
5361 due to:
5363 [basic.lookup.qual]
5365 If the name found is not a class-name (clause
5366 _class_) or namespace-name (_namespace.def_), the
5367 program is ill-formed. */
5368 success = true;
5370 cp_lexer_consume_token (parser->lexer);
5372 break;
5374 /* We've found one valid nested-name-specifier. */
5375 success = true;
5376 /* Name lookup always gives us a DECL. */
5377 if (TREE_CODE (new_scope) == TYPE_DECL)
5378 new_scope = TREE_TYPE (new_scope);
5379 /* Uses of "template" must be followed by actual templates. */
5380 if (template_keyword_p
5381 && !(CLASS_TYPE_P (new_scope)
5382 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5383 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5384 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5385 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5386 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5387 == TEMPLATE_ID_EXPR)))
5388 permerror (input_location, TYPE_P (new_scope)
5389 ? G_("%qT is not a template")
5390 : G_("%qD is not a template"),
5391 new_scope);
5392 /* If it is a class scope, try to complete it; we are about to
5393 be looking up names inside the class. */
5394 if (TYPE_P (new_scope)
5395 /* Since checking types for dependency can be expensive,
5396 avoid doing it if the type is already complete. */
5397 && !COMPLETE_TYPE_P (new_scope)
5398 /* Do not try to complete dependent types. */
5399 && !dependent_type_p (new_scope))
5401 new_scope = complete_type (new_scope);
5402 /* If it is a typedef to current class, use the current
5403 class instead, as the typedef won't have any names inside
5404 it yet. */
5405 if (!COMPLETE_TYPE_P (new_scope)
5406 && currently_open_class (new_scope))
5407 new_scope = TYPE_MAIN_VARIANT (new_scope);
5409 /* Make sure we look in the right scope the next time through
5410 the loop. */
5411 parser->scope = new_scope;
5414 /* If parsing tentatively, replace the sequence of tokens that makes
5415 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5416 token. That way, should we re-parse the token stream, we will
5417 not have to repeat the effort required to do the parse, nor will
5418 we issue duplicate error messages. */
5419 if (success && start)
5421 cp_token *token;
5423 token = cp_lexer_token_at (parser->lexer, start);
5424 /* Reset the contents of the START token. */
5425 token->type = CPP_NESTED_NAME_SPECIFIER;
5426 /* Retrieve any deferred checks. Do not pop this access checks yet
5427 so the memory will not be reclaimed during token replacing below. */
5428 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5429 token->u.tree_check_value->value = parser->scope;
5430 token->u.tree_check_value->checks = get_deferred_access_checks ();
5431 token->u.tree_check_value->qualifying_scope =
5432 parser->qualifying_scope;
5433 token->keyword = RID_MAX;
5435 /* Purge all subsequent tokens. */
5436 cp_lexer_purge_tokens_after (parser->lexer, start);
5439 if (start)
5440 pop_to_parent_deferring_access_checks ();
5442 return success ? parser->scope : NULL_TREE;
5445 /* Parse a nested-name-specifier. See
5446 cp_parser_nested_name_specifier_opt for details. This function
5447 behaves identically, except that it will an issue an error if no
5448 nested-name-specifier is present. */
5450 static tree
5451 cp_parser_nested_name_specifier (cp_parser *parser,
5452 bool typename_keyword_p,
5453 bool check_dependency_p,
5454 bool type_p,
5455 bool is_declaration)
5457 tree scope;
5459 /* Look for the nested-name-specifier. */
5460 scope = cp_parser_nested_name_specifier_opt (parser,
5461 typename_keyword_p,
5462 check_dependency_p,
5463 type_p,
5464 is_declaration);
5465 /* If it was not present, issue an error message. */
5466 if (!scope)
5468 cp_parser_error (parser, "expected nested-name-specifier");
5469 parser->scope = NULL_TREE;
5472 return scope;
5475 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5476 this is either a class-name or a namespace-name (which corresponds
5477 to the class-or-namespace-name production in the grammar). For
5478 C++0x, it can also be a type-name that refers to an enumeration
5479 type or a simple-template-id.
5481 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5482 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5483 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5484 TYPE_P is TRUE iff the next name should be taken as a class-name,
5485 even the same name is declared to be another entity in the same
5486 scope.
5488 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5489 specified by the class-or-namespace-name. If neither is found the
5490 ERROR_MARK_NODE is returned. */
5492 static tree
5493 cp_parser_qualifying_entity (cp_parser *parser,
5494 bool typename_keyword_p,
5495 bool template_keyword_p,
5496 bool check_dependency_p,
5497 bool type_p,
5498 bool is_declaration)
5500 tree saved_scope;
5501 tree saved_qualifying_scope;
5502 tree saved_object_scope;
5503 tree scope;
5504 bool only_class_p;
5505 bool successful_parse_p;
5507 /* DR 743: decltype can appear in a nested-name-specifier. */
5508 if (cp_lexer_next_token_is_decltype (parser->lexer))
5510 scope = cp_parser_decltype (parser);
5511 if (TREE_CODE (scope) != ENUMERAL_TYPE
5512 && !MAYBE_CLASS_TYPE_P (scope))
5514 cp_parser_simulate_error (parser);
5515 return error_mark_node;
5517 if (TYPE_NAME (scope))
5518 scope = TYPE_NAME (scope);
5519 return scope;
5522 /* Before we try to parse the class-name, we must save away the
5523 current PARSER->SCOPE since cp_parser_class_name will destroy
5524 it. */
5525 saved_scope = parser->scope;
5526 saved_qualifying_scope = parser->qualifying_scope;
5527 saved_object_scope = parser->object_scope;
5528 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5529 there is no need to look for a namespace-name. */
5530 only_class_p = template_keyword_p
5531 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5532 if (!only_class_p)
5533 cp_parser_parse_tentatively (parser);
5534 scope = cp_parser_class_name (parser,
5535 typename_keyword_p,
5536 template_keyword_p,
5537 type_p ? class_type : none_type,
5538 check_dependency_p,
5539 /*class_head_p=*/false,
5540 is_declaration);
5541 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5542 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5543 if (!only_class_p
5544 && cxx_dialect != cxx98
5545 && !successful_parse_p)
5547 /* Restore the saved scope. */
5548 parser->scope = saved_scope;
5549 parser->qualifying_scope = saved_qualifying_scope;
5550 parser->object_scope = saved_object_scope;
5552 /* Parse tentatively. */
5553 cp_parser_parse_tentatively (parser);
5555 /* Parse a type-name */
5556 scope = cp_parser_type_name (parser);
5558 /* "If the name found does not designate a namespace or a class,
5559 enumeration, or dependent type, the program is ill-formed."
5561 We cover classes and dependent types above and namespaces below,
5562 so this code is only looking for enums. */
5563 if (!scope || TREE_CODE (scope) != TYPE_DECL
5564 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5565 cp_parser_simulate_error (parser);
5567 successful_parse_p = cp_parser_parse_definitely (parser);
5569 /* If that didn't work, try for a namespace-name. */
5570 if (!only_class_p && !successful_parse_p)
5572 /* Restore the saved scope. */
5573 parser->scope = saved_scope;
5574 parser->qualifying_scope = saved_qualifying_scope;
5575 parser->object_scope = saved_object_scope;
5576 /* If we are not looking at an identifier followed by the scope
5577 resolution operator, then this is not part of a
5578 nested-name-specifier. (Note that this function is only used
5579 to parse the components of a nested-name-specifier.) */
5580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5581 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5582 return error_mark_node;
5583 scope = cp_parser_namespace_name (parser);
5586 return scope;
5589 /* Parse a postfix-expression.
5591 postfix-expression:
5592 primary-expression
5593 postfix-expression [ expression ]
5594 postfix-expression ( expression-list [opt] )
5595 simple-type-specifier ( expression-list [opt] )
5596 typename :: [opt] nested-name-specifier identifier
5597 ( expression-list [opt] )
5598 typename :: [opt] nested-name-specifier template [opt] template-id
5599 ( expression-list [opt] )
5600 postfix-expression . template [opt] id-expression
5601 postfix-expression -> template [opt] id-expression
5602 postfix-expression . pseudo-destructor-name
5603 postfix-expression -> pseudo-destructor-name
5604 postfix-expression ++
5605 postfix-expression --
5606 dynamic_cast < type-id > ( expression )
5607 static_cast < type-id > ( expression )
5608 reinterpret_cast < type-id > ( expression )
5609 const_cast < type-id > ( expression )
5610 typeid ( expression )
5611 typeid ( type-id )
5613 GNU Extension:
5615 postfix-expression:
5616 ( type-id ) { initializer-list , [opt] }
5618 This extension is a GNU version of the C99 compound-literal
5619 construct. (The C99 grammar uses `type-name' instead of `type-id',
5620 but they are essentially the same concept.)
5622 If ADDRESS_P is true, the postfix expression is the operand of the
5623 `&' operator. CAST_P is true if this expression is the target of a
5624 cast.
5626 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5627 class member access expressions [expr.ref].
5629 Returns a representation of the expression. */
5631 static tree
5632 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5633 bool member_access_only_p, bool decltype_p,
5634 cp_id_kind * pidk_return)
5636 cp_token *token;
5637 location_t loc;
5638 enum rid keyword;
5639 cp_id_kind idk = CP_ID_KIND_NONE;
5640 tree postfix_expression = NULL_TREE;
5641 bool is_member_access = false;
5642 int saved_in_statement = -1;
5644 /* Peek at the next token. */
5645 token = cp_lexer_peek_token (parser->lexer);
5646 loc = token->location;
5647 /* Some of the productions are determined by keywords. */
5648 keyword = token->keyword;
5649 switch (keyword)
5651 case RID_DYNCAST:
5652 case RID_STATCAST:
5653 case RID_REINTCAST:
5654 case RID_CONSTCAST:
5656 tree type;
5657 tree expression;
5658 const char *saved_message;
5659 bool saved_in_type_id_in_expr_p;
5661 /* All of these can be handled in the same way from the point
5662 of view of parsing. Begin by consuming the token
5663 identifying the cast. */
5664 cp_lexer_consume_token (parser->lexer);
5666 /* New types cannot be defined in the cast. */
5667 saved_message = parser->type_definition_forbidden_message;
5668 parser->type_definition_forbidden_message
5669 = G_("types may not be defined in casts");
5671 /* Look for the opening `<'. */
5672 cp_parser_require (parser, CPP_LESS, RT_LESS);
5673 /* Parse the type to which we are casting. */
5674 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5675 parser->in_type_id_in_expr_p = true;
5676 type = cp_parser_type_id (parser);
5677 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5678 /* Look for the closing `>'. */
5679 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5680 /* Restore the old message. */
5681 parser->type_definition_forbidden_message = saved_message;
5683 bool saved_greater_than_is_operator_p
5684 = parser->greater_than_is_operator_p;
5685 parser->greater_than_is_operator_p = true;
5687 /* And the expression which is being cast. */
5688 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5689 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5690 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5692 parser->greater_than_is_operator_p
5693 = saved_greater_than_is_operator_p;
5695 /* Only type conversions to integral or enumeration types
5696 can be used in constant-expressions. */
5697 if (!cast_valid_in_integral_constant_expression_p (type)
5698 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5699 return error_mark_node;
5701 switch (keyword)
5703 case RID_DYNCAST:
5704 postfix_expression
5705 = build_dynamic_cast (type, expression, tf_warning_or_error);
5706 break;
5707 case RID_STATCAST:
5708 postfix_expression
5709 = build_static_cast (type, expression, tf_warning_or_error);
5710 break;
5711 case RID_REINTCAST:
5712 postfix_expression
5713 = build_reinterpret_cast (type, expression,
5714 tf_warning_or_error);
5715 break;
5716 case RID_CONSTCAST:
5717 postfix_expression
5718 = build_const_cast (type, expression, tf_warning_or_error);
5719 break;
5720 default:
5721 gcc_unreachable ();
5724 break;
5726 case RID_TYPEID:
5728 tree type;
5729 const char *saved_message;
5730 bool saved_in_type_id_in_expr_p;
5732 /* Consume the `typeid' token. */
5733 cp_lexer_consume_token (parser->lexer);
5734 /* Look for the `(' token. */
5735 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5736 /* Types cannot be defined in a `typeid' expression. */
5737 saved_message = parser->type_definition_forbidden_message;
5738 parser->type_definition_forbidden_message
5739 = G_("types may not be defined in a %<typeid%> expression");
5740 /* We can't be sure yet whether we're looking at a type-id or an
5741 expression. */
5742 cp_parser_parse_tentatively (parser);
5743 /* Try a type-id first. */
5744 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5745 parser->in_type_id_in_expr_p = true;
5746 type = cp_parser_type_id (parser);
5747 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5748 /* Look for the `)' token. Otherwise, we can't be sure that
5749 we're not looking at an expression: consider `typeid (int
5750 (3))', for example. */
5751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5752 /* If all went well, simply lookup the type-id. */
5753 if (cp_parser_parse_definitely (parser))
5754 postfix_expression = get_typeid (type, tf_warning_or_error);
5755 /* Otherwise, fall back to the expression variant. */
5756 else
5758 tree expression;
5760 /* Look for an expression. */
5761 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5762 /* Compute its typeid. */
5763 postfix_expression = build_typeid (expression, tf_warning_or_error);
5764 /* Look for the `)' token. */
5765 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5767 /* Restore the saved message. */
5768 parser->type_definition_forbidden_message = saved_message;
5769 /* `typeid' may not appear in an integral constant expression. */
5770 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5771 return error_mark_node;
5773 break;
5775 case RID_TYPENAME:
5777 tree type;
5778 /* The syntax permitted here is the same permitted for an
5779 elaborated-type-specifier. */
5780 type = cp_parser_elaborated_type_specifier (parser,
5781 /*is_friend=*/false,
5782 /*is_declaration=*/false);
5783 postfix_expression = cp_parser_functional_cast (parser, type);
5785 break;
5787 case RID_CILK_SPAWN:
5789 cp_lexer_consume_token (parser->lexer);
5790 token = cp_lexer_peek_token (parser->lexer);
5791 if (token->type == CPP_SEMICOLON)
5793 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5794 "an expression");
5795 postfix_expression = error_mark_node;
5796 break;
5798 else if (!current_function_decl)
5800 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5801 "inside a function");
5802 postfix_expression = error_mark_node;
5803 break;
5805 else
5807 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5808 saved_in_statement = parser->in_statement;
5809 parser->in_statement |= IN_CILK_SPAWN;
5811 cfun->calls_cilk_spawn = 1;
5812 postfix_expression =
5813 cp_parser_postfix_expression (parser, false, false,
5814 false, false, &idk);
5815 if (!flag_cilkplus)
5817 error_at (token->location, "-fcilkplus must be enabled to use"
5818 " %<_Cilk_spawn%>");
5819 cfun->calls_cilk_spawn = 0;
5821 else if (saved_in_statement & IN_CILK_SPAWN)
5823 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5824 "are not permitted");
5825 postfix_expression = error_mark_node;
5826 cfun->calls_cilk_spawn = 0;
5828 else
5830 postfix_expression = build_cilk_spawn (token->location,
5831 postfix_expression);
5832 if (postfix_expression != error_mark_node)
5833 SET_EXPR_LOCATION (postfix_expression, input_location);
5834 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5836 break;
5839 case RID_CILK_SYNC:
5840 if (flag_cilkplus)
5842 tree sync_expr = build_cilk_sync ();
5843 SET_EXPR_LOCATION (sync_expr,
5844 cp_lexer_peek_token (parser->lexer)->location);
5845 finish_expr_stmt (sync_expr);
5847 else
5848 error_at (token->location, "-fcilkplus must be enabled to use"
5849 " %<_Cilk_sync%>");
5850 cp_lexer_consume_token (parser->lexer);
5851 break;
5853 case RID_BUILTIN_SHUFFLE:
5855 vec<tree, va_gc> *vec;
5856 unsigned int i;
5857 tree p;
5859 cp_lexer_consume_token (parser->lexer);
5860 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5861 /*cast_p=*/false, /*allow_expansion_p=*/true,
5862 /*non_constant_p=*/NULL);
5863 if (vec == NULL)
5864 return error_mark_node;
5866 FOR_EACH_VEC_ELT (*vec, i, p)
5867 mark_exp_read (p);
5869 if (vec->length () == 2)
5870 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5871 tf_warning_or_error);
5872 else if (vec->length () == 3)
5873 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5874 tf_warning_or_error);
5875 else
5877 error_at (loc, "wrong number of arguments to "
5878 "%<__builtin_shuffle%>");
5879 return error_mark_node;
5881 break;
5884 default:
5886 tree type;
5888 /* If the next thing is a simple-type-specifier, we may be
5889 looking at a functional cast. We could also be looking at
5890 an id-expression. So, we try the functional cast, and if
5891 that doesn't work we fall back to the primary-expression. */
5892 cp_parser_parse_tentatively (parser);
5893 /* Look for the simple-type-specifier. */
5894 type = cp_parser_simple_type_specifier (parser,
5895 /*decl_specs=*/NULL,
5896 CP_PARSER_FLAGS_NONE);
5897 /* Parse the cast itself. */
5898 if (!cp_parser_error_occurred (parser))
5899 postfix_expression
5900 = cp_parser_functional_cast (parser, type);
5901 /* If that worked, we're done. */
5902 if (cp_parser_parse_definitely (parser))
5903 break;
5905 /* If the functional-cast didn't work out, try a
5906 compound-literal. */
5907 if (cp_parser_allow_gnu_extensions_p (parser)
5908 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5910 tree initializer = NULL_TREE;
5911 bool compound_literal_p;
5913 cp_parser_parse_tentatively (parser);
5914 /* Consume the `('. */
5915 cp_lexer_consume_token (parser->lexer);
5917 /* Avoid calling cp_parser_type_id pointlessly, see comment
5918 in cp_parser_cast_expression about c++/29234. */
5919 cp_lexer_save_tokens (parser->lexer);
5921 compound_literal_p
5922 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5923 /*consume_paren=*/true)
5924 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5926 /* Roll back the tokens we skipped. */
5927 cp_lexer_rollback_tokens (parser->lexer);
5929 if (!compound_literal_p)
5930 cp_parser_simulate_error (parser);
5931 else
5933 /* Parse the type. */
5934 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5935 parser->in_type_id_in_expr_p = true;
5936 type = cp_parser_type_id (parser);
5937 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5938 /* Look for the `)'. */
5939 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5942 /* If things aren't going well, there's no need to
5943 keep going. */
5944 if (!cp_parser_error_occurred (parser))
5946 bool non_constant_p;
5947 /* Parse the brace-enclosed initializer list. */
5948 initializer = cp_parser_braced_list (parser,
5949 &non_constant_p);
5951 /* If that worked, we're definitely looking at a
5952 compound-literal expression. */
5953 if (cp_parser_parse_definitely (parser))
5955 /* Warn the user that a compound literal is not
5956 allowed in standard C++. */
5957 pedwarn (input_location, OPT_Wpedantic,
5958 "ISO C++ forbids compound-literals");
5959 /* For simplicity, we disallow compound literals in
5960 constant-expressions. We could
5961 allow compound literals of integer type, whose
5962 initializer was a constant, in constant
5963 expressions. Permitting that usage, as a further
5964 extension, would not change the meaning of any
5965 currently accepted programs. (Of course, as
5966 compound literals are not part of ISO C++, the
5967 standard has nothing to say.) */
5968 if (cp_parser_non_integral_constant_expression (parser,
5969 NIC_NCC))
5971 postfix_expression = error_mark_node;
5972 break;
5974 /* Form the representation of the compound-literal. */
5975 postfix_expression
5976 = finish_compound_literal (type, initializer,
5977 tf_warning_or_error);
5978 break;
5982 /* It must be a primary-expression. */
5983 postfix_expression
5984 = cp_parser_primary_expression (parser, address_p, cast_p,
5985 /*template_arg_p=*/false,
5986 decltype_p,
5987 &idk);
5989 break;
5992 /* Note that we don't need to worry about calling build_cplus_new on a
5993 class-valued CALL_EXPR in decltype when it isn't the end of the
5994 postfix-expression; unary_complex_lvalue will take care of that for
5995 all these cases. */
5997 /* Keep looping until the postfix-expression is complete. */
5998 while (true)
6000 if (idk == CP_ID_KIND_UNQUALIFIED
6001 && identifier_p (postfix_expression)
6002 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6003 /* It is not a Koenig lookup function call. */
6004 postfix_expression
6005 = unqualified_name_lookup_error (postfix_expression);
6007 /* Peek at the next token. */
6008 token = cp_lexer_peek_token (parser->lexer);
6010 switch (token->type)
6012 case CPP_OPEN_SQUARE:
6013 if (cp_next_tokens_can_be_std_attribute_p (parser))
6015 cp_parser_error (parser,
6016 "two consecutive %<[%> shall "
6017 "only introduce an attribute");
6018 return error_mark_node;
6020 postfix_expression
6021 = cp_parser_postfix_open_square_expression (parser,
6022 postfix_expression,
6023 false,
6024 decltype_p);
6025 idk = CP_ID_KIND_NONE;
6026 is_member_access = false;
6027 break;
6029 case CPP_OPEN_PAREN:
6030 /* postfix-expression ( expression-list [opt] ) */
6032 bool koenig_p;
6033 bool is_builtin_constant_p;
6034 bool saved_integral_constant_expression_p = false;
6035 bool saved_non_integral_constant_expression_p = false;
6036 tsubst_flags_t complain = complain_flags (decltype_p);
6037 vec<tree, va_gc> *args;
6039 is_member_access = false;
6041 is_builtin_constant_p
6042 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6043 if (is_builtin_constant_p)
6045 /* The whole point of __builtin_constant_p is to allow
6046 non-constant expressions to appear as arguments. */
6047 saved_integral_constant_expression_p
6048 = parser->integral_constant_expression_p;
6049 saved_non_integral_constant_expression_p
6050 = parser->non_integral_constant_expression_p;
6051 parser->integral_constant_expression_p = false;
6053 args = (cp_parser_parenthesized_expression_list
6054 (parser, non_attr,
6055 /*cast_p=*/false, /*allow_expansion_p=*/true,
6056 /*non_constant_p=*/NULL));
6057 if (is_builtin_constant_p)
6059 parser->integral_constant_expression_p
6060 = saved_integral_constant_expression_p;
6061 parser->non_integral_constant_expression_p
6062 = saved_non_integral_constant_expression_p;
6065 if (args == NULL)
6067 postfix_expression = error_mark_node;
6068 break;
6071 /* Function calls are not permitted in
6072 constant-expressions. */
6073 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6074 && cp_parser_non_integral_constant_expression (parser,
6075 NIC_FUNC_CALL))
6077 postfix_expression = error_mark_node;
6078 release_tree_vector (args);
6079 break;
6082 koenig_p = false;
6083 if (idk == CP_ID_KIND_UNQUALIFIED
6084 || idk == CP_ID_KIND_TEMPLATE_ID)
6086 if (identifier_p (postfix_expression))
6088 if (!args->is_empty ())
6090 koenig_p = true;
6091 if (!any_type_dependent_arguments_p (args))
6092 postfix_expression
6093 = perform_koenig_lookup (postfix_expression, args,
6094 complain);
6096 else
6097 postfix_expression
6098 = unqualified_fn_lookup_error (postfix_expression);
6100 /* We do not perform argument-dependent lookup if
6101 normal lookup finds a non-function, in accordance
6102 with the expected resolution of DR 218. */
6103 else if (!args->is_empty ()
6104 && is_overloaded_fn (postfix_expression))
6106 tree fn = get_first_fn (postfix_expression);
6107 fn = STRIP_TEMPLATE (fn);
6109 /* Do not do argument dependent lookup if regular
6110 lookup finds a member function or a block-scope
6111 function declaration. [basic.lookup.argdep]/3 */
6112 if (!DECL_FUNCTION_MEMBER_P (fn)
6113 && !DECL_LOCAL_FUNCTION_P (fn))
6115 koenig_p = true;
6116 if (!any_type_dependent_arguments_p (args))
6117 postfix_expression
6118 = perform_koenig_lookup (postfix_expression, args,
6119 complain);
6124 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6126 tree instance = TREE_OPERAND (postfix_expression, 0);
6127 tree fn = TREE_OPERAND (postfix_expression, 1);
6129 if (processing_template_decl
6130 && (type_dependent_expression_p (instance)
6131 || (!BASELINK_P (fn)
6132 && TREE_CODE (fn) != FIELD_DECL)
6133 || type_dependent_expression_p (fn)
6134 || any_type_dependent_arguments_p (args)))
6136 postfix_expression
6137 = build_nt_call_vec (postfix_expression, args);
6138 release_tree_vector (args);
6139 break;
6142 if (BASELINK_P (fn))
6144 postfix_expression
6145 = (build_new_method_call
6146 (instance, fn, &args, NULL_TREE,
6147 (idk == CP_ID_KIND_QUALIFIED
6148 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6149 : LOOKUP_NORMAL),
6150 /*fn_p=*/NULL,
6151 complain));
6153 else
6154 postfix_expression
6155 = finish_call_expr (postfix_expression, &args,
6156 /*disallow_virtual=*/false,
6157 /*koenig_p=*/false,
6158 complain);
6160 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6161 || TREE_CODE (postfix_expression) == MEMBER_REF
6162 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6163 postfix_expression = (build_offset_ref_call_from_tree
6164 (postfix_expression, &args,
6165 complain));
6166 else if (idk == CP_ID_KIND_QUALIFIED)
6167 /* A call to a static class member, or a namespace-scope
6168 function. */
6169 postfix_expression
6170 = finish_call_expr (postfix_expression, &args,
6171 /*disallow_virtual=*/true,
6172 koenig_p,
6173 complain);
6174 else
6175 /* All other function calls. */
6176 postfix_expression
6177 = finish_call_expr (postfix_expression, &args,
6178 /*disallow_virtual=*/false,
6179 koenig_p,
6180 complain);
6182 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6183 idk = CP_ID_KIND_NONE;
6185 release_tree_vector (args);
6187 break;
6189 case CPP_DOT:
6190 case CPP_DEREF:
6191 /* postfix-expression . template [opt] id-expression
6192 postfix-expression . pseudo-destructor-name
6193 postfix-expression -> template [opt] id-expression
6194 postfix-expression -> pseudo-destructor-name */
6196 /* Consume the `.' or `->' operator. */
6197 cp_lexer_consume_token (parser->lexer);
6199 postfix_expression
6200 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6201 postfix_expression,
6202 false, &idk, loc);
6204 is_member_access = true;
6205 break;
6207 case CPP_PLUS_PLUS:
6208 /* postfix-expression ++ */
6209 /* Consume the `++' token. */
6210 cp_lexer_consume_token (parser->lexer);
6211 /* Generate a representation for the complete expression. */
6212 postfix_expression
6213 = finish_increment_expr (postfix_expression,
6214 POSTINCREMENT_EXPR);
6215 /* Increments may not appear in constant-expressions. */
6216 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6217 postfix_expression = error_mark_node;
6218 idk = CP_ID_KIND_NONE;
6219 is_member_access = false;
6220 break;
6222 case CPP_MINUS_MINUS:
6223 /* postfix-expression -- */
6224 /* Consume the `--' token. */
6225 cp_lexer_consume_token (parser->lexer);
6226 /* Generate a representation for the complete expression. */
6227 postfix_expression
6228 = finish_increment_expr (postfix_expression,
6229 POSTDECREMENT_EXPR);
6230 /* Decrements may not appear in constant-expressions. */
6231 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6232 postfix_expression = error_mark_node;
6233 idk = CP_ID_KIND_NONE;
6234 is_member_access = false;
6235 break;
6237 default:
6238 if (pidk_return != NULL)
6239 * pidk_return = idk;
6240 if (member_access_only_p)
6241 return is_member_access? postfix_expression : error_mark_node;
6242 else
6243 return postfix_expression;
6247 /* We should never get here. */
6248 gcc_unreachable ();
6249 return error_mark_node;
6252 /* This function parses Cilk Plus array notations. If a normal array expr. is
6253 parsed then the array index is passed back to the caller through *INIT_INDEX
6254 and the function returns a NULL_TREE. If array notation expr. is parsed,
6255 then *INIT_INDEX is ignored by the caller and the function returns
6256 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6257 error_mark_node. */
6259 static tree
6260 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6261 tree array_value)
6263 cp_token *token = NULL;
6264 tree length_index, stride = NULL_TREE, value_tree, array_type;
6265 if (!array_value || array_value == error_mark_node)
6267 cp_parser_skip_to_end_of_statement (parser);
6268 return error_mark_node;
6271 array_type = TREE_TYPE (array_value);
6273 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6274 parser->colon_corrects_to_scope_p = false;
6275 token = cp_lexer_peek_token (parser->lexer);
6277 if (!token)
6279 cp_parser_error (parser, "expected %<:%> or numeral");
6280 return error_mark_node;
6282 else if (token->type == CPP_COLON)
6284 /* Consume the ':'. */
6285 cp_lexer_consume_token (parser->lexer);
6287 /* If we are here, then we have a case like this A[:]. */
6288 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6290 cp_parser_error (parser, "expected %<]%>");
6291 cp_parser_skip_to_end_of_statement (parser);
6292 return error_mark_node;
6294 *init_index = NULL_TREE;
6295 stride = NULL_TREE;
6296 length_index = NULL_TREE;
6298 else
6300 /* If we are here, then there are three valid possibilities:
6301 1. ARRAY [ EXP ]
6302 2. ARRAY [ EXP : EXP ]
6303 3. ARRAY [ EXP : EXP : EXP ] */
6305 *init_index = cp_parser_expression (parser, false, NULL);
6306 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6308 /* This indicates that we have a normal array expression. */
6309 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6310 return NULL_TREE;
6313 /* Consume the ':'. */
6314 cp_lexer_consume_token (parser->lexer);
6315 length_index = cp_parser_expression (parser, false, NULL);
6316 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6318 cp_lexer_consume_token (parser->lexer);
6319 stride = cp_parser_expression (parser, false, NULL);
6322 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6324 if (*init_index == error_mark_node || length_index == error_mark_node
6325 || stride == error_mark_node)
6327 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6328 cp_lexer_consume_token (parser->lexer);
6329 return error_mark_node;
6331 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6333 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6334 length_index, stride, array_type);
6335 return value_tree;
6338 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6339 by cp_parser_builtin_offsetof. We're looking for
6341 postfix-expression [ expression ]
6342 postfix-expression [ braced-init-list ] (C++11)
6344 FOR_OFFSETOF is set if we're being called in that context, which
6345 changes how we deal with integer constant expressions. */
6347 static tree
6348 cp_parser_postfix_open_square_expression (cp_parser *parser,
6349 tree postfix_expression,
6350 bool for_offsetof,
6351 bool decltype_p)
6353 tree index = NULL_TREE;
6354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6355 bool saved_greater_than_is_operator_p;
6357 /* Consume the `[' token. */
6358 cp_lexer_consume_token (parser->lexer);
6360 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6361 parser->greater_than_is_operator_p = true;
6363 /* Parse the index expression. */
6364 /* ??? For offsetof, there is a question of what to allow here. If
6365 offsetof is not being used in an integral constant expression context,
6366 then we *could* get the right answer by computing the value at runtime.
6367 If we are in an integral constant expression context, then we might
6368 could accept any constant expression; hard to say without analysis.
6369 Rather than open the barn door too wide right away, allow only integer
6370 constant expressions here. */
6371 if (for_offsetof)
6372 index = cp_parser_constant_expression (parser, false, NULL);
6373 else
6375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6377 bool expr_nonconst_p;
6378 cp_lexer_set_source_position (parser->lexer);
6379 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6380 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6381 if (flag_cilkplus
6382 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6384 error_at (cp_lexer_peek_token (parser->lexer)->location,
6385 "braced list index is not allowed with array "
6386 "notation");
6387 cp_parser_skip_to_end_of_statement (parser);
6388 return error_mark_node;
6391 else if (flag_cilkplus)
6393 /* Here are have these two options:
6394 ARRAY[EXP : EXP] - Array notation expr with default
6395 stride of 1.
6396 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6397 stride. */
6398 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6399 postfix_expression);
6400 if (an_exp)
6401 return an_exp;
6403 else
6404 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6407 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6409 /* Look for the closing `]'. */
6410 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6412 /* Build the ARRAY_REF. */
6413 postfix_expression = grok_array_decl (loc, postfix_expression,
6414 index, decltype_p);
6416 /* When not doing offsetof, array references are not permitted in
6417 constant-expressions. */
6418 if (!for_offsetof
6419 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6420 postfix_expression = error_mark_node;
6422 return postfix_expression;
6425 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6426 by cp_parser_builtin_offsetof. We're looking for
6428 postfix-expression . template [opt] id-expression
6429 postfix-expression . pseudo-destructor-name
6430 postfix-expression -> template [opt] id-expression
6431 postfix-expression -> pseudo-destructor-name
6433 FOR_OFFSETOF is set if we're being called in that context. That sorta
6434 limits what of the above we'll actually accept, but nevermind.
6435 TOKEN_TYPE is the "." or "->" token, which will already have been
6436 removed from the stream. */
6438 static tree
6439 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6440 enum cpp_ttype token_type,
6441 tree postfix_expression,
6442 bool for_offsetof, cp_id_kind *idk,
6443 location_t location)
6445 tree name;
6446 bool dependent_p;
6447 bool pseudo_destructor_p;
6448 tree scope = NULL_TREE;
6450 /* If this is a `->' operator, dereference the pointer. */
6451 if (token_type == CPP_DEREF)
6452 postfix_expression = build_x_arrow (location, postfix_expression,
6453 tf_warning_or_error);
6454 /* Check to see whether or not the expression is type-dependent. */
6455 dependent_p = type_dependent_expression_p (postfix_expression);
6456 /* The identifier following the `->' or `.' is not qualified. */
6457 parser->scope = NULL_TREE;
6458 parser->qualifying_scope = NULL_TREE;
6459 parser->object_scope = NULL_TREE;
6460 *idk = CP_ID_KIND_NONE;
6462 /* Enter the scope corresponding to the type of the object
6463 given by the POSTFIX_EXPRESSION. */
6464 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6466 scope = TREE_TYPE (postfix_expression);
6467 /* According to the standard, no expression should ever have
6468 reference type. Unfortunately, we do not currently match
6469 the standard in this respect in that our internal representation
6470 of an expression may have reference type even when the standard
6471 says it does not. Therefore, we have to manually obtain the
6472 underlying type here. */
6473 scope = non_reference (scope);
6474 /* The type of the POSTFIX_EXPRESSION must be complete. */
6475 if (scope == unknown_type_node)
6477 error_at (location, "%qE does not have class type",
6478 postfix_expression);
6479 scope = NULL_TREE;
6481 /* Unlike the object expression in other contexts, *this is not
6482 required to be of complete type for purposes of class member
6483 access (5.2.5) outside the member function body. */
6484 else if (postfix_expression != current_class_ref
6485 && !(processing_template_decl && scope == current_class_type))
6486 scope = complete_type_or_else (scope, NULL_TREE);
6487 /* Let the name lookup machinery know that we are processing a
6488 class member access expression. */
6489 parser->context->object_type = scope;
6490 /* If something went wrong, we want to be able to discern that case,
6491 as opposed to the case where there was no SCOPE due to the type
6492 of expression being dependent. */
6493 if (!scope)
6494 scope = error_mark_node;
6495 /* If the SCOPE was erroneous, make the various semantic analysis
6496 functions exit quickly -- and without issuing additional error
6497 messages. */
6498 if (scope == error_mark_node)
6499 postfix_expression = error_mark_node;
6502 /* Assume this expression is not a pseudo-destructor access. */
6503 pseudo_destructor_p = false;
6505 /* If the SCOPE is a scalar type, then, if this is a valid program,
6506 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6507 is type dependent, it can be pseudo-destructor-name or something else.
6508 Try to parse it as pseudo-destructor-name first. */
6509 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6511 tree s;
6512 tree type;
6514 cp_parser_parse_tentatively (parser);
6515 /* Parse the pseudo-destructor-name. */
6516 s = NULL_TREE;
6517 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6518 &s, &type);
6519 if (dependent_p
6520 && (cp_parser_error_occurred (parser)
6521 || !SCALAR_TYPE_P (type)))
6522 cp_parser_abort_tentative_parse (parser);
6523 else if (cp_parser_parse_definitely (parser))
6525 pseudo_destructor_p = true;
6526 postfix_expression
6527 = finish_pseudo_destructor_expr (postfix_expression,
6528 s, type, location);
6532 if (!pseudo_destructor_p)
6534 /* If the SCOPE is not a scalar type, we are looking at an
6535 ordinary class member access expression, rather than a
6536 pseudo-destructor-name. */
6537 bool template_p;
6538 cp_token *token = cp_lexer_peek_token (parser->lexer);
6539 /* Parse the id-expression. */
6540 name = (cp_parser_id_expression
6541 (parser,
6542 cp_parser_optional_template_keyword (parser),
6543 /*check_dependency_p=*/true,
6544 &template_p,
6545 /*declarator_p=*/false,
6546 /*optional_p=*/false));
6547 /* In general, build a SCOPE_REF if the member name is qualified.
6548 However, if the name was not dependent and has already been
6549 resolved; there is no need to build the SCOPE_REF. For example;
6551 struct X { void f(); };
6552 template <typename T> void f(T* t) { t->X::f(); }
6554 Even though "t" is dependent, "X::f" is not and has been resolved
6555 to a BASELINK; there is no need to include scope information. */
6557 /* But we do need to remember that there was an explicit scope for
6558 virtual function calls. */
6559 if (parser->scope)
6560 *idk = CP_ID_KIND_QUALIFIED;
6562 /* If the name is a template-id that names a type, we will get a
6563 TYPE_DECL here. That is invalid code. */
6564 if (TREE_CODE (name) == TYPE_DECL)
6566 error_at (token->location, "invalid use of %qD", name);
6567 postfix_expression = error_mark_node;
6569 else
6571 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6573 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6575 error_at (token->location, "%<%D::%D%> is not a class member",
6576 parser->scope, name);
6577 postfix_expression = error_mark_node;
6579 else
6580 name = build_qualified_name (/*type=*/NULL_TREE,
6581 parser->scope,
6582 name,
6583 template_p);
6584 parser->scope = NULL_TREE;
6585 parser->qualifying_scope = NULL_TREE;
6586 parser->object_scope = NULL_TREE;
6588 if (parser->scope && name && BASELINK_P (name))
6589 adjust_result_of_qualified_name_lookup
6590 (name, parser->scope, scope);
6591 postfix_expression
6592 = finish_class_member_access_expr (postfix_expression, name,
6593 template_p,
6594 tf_warning_or_error);
6598 /* We no longer need to look up names in the scope of the object on
6599 the left-hand side of the `.' or `->' operator. */
6600 parser->context->object_type = NULL_TREE;
6602 /* Outside of offsetof, these operators may not appear in
6603 constant-expressions. */
6604 if (!for_offsetof
6605 && (cp_parser_non_integral_constant_expression
6606 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6607 postfix_expression = error_mark_node;
6609 return postfix_expression;
6612 /* Parse a parenthesized expression-list.
6614 expression-list:
6615 assignment-expression
6616 expression-list, assignment-expression
6618 attribute-list:
6619 expression-list
6620 identifier
6621 identifier, expression-list
6623 CAST_P is true if this expression is the target of a cast.
6625 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6626 argument pack.
6628 Returns a vector of trees. Each element is a representation of an
6629 assignment-expression. NULL is returned if the ( and or ) are
6630 missing. An empty, but allocated, vector is returned on no
6631 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6632 if we are parsing an attribute list for an attribute that wants a
6633 plain identifier argument, normal_attr for an attribute that wants
6634 an expression, or non_attr if we aren't parsing an attribute list. If
6635 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6636 not all of the expressions in the list were constant. */
6638 static vec<tree, va_gc> *
6639 cp_parser_parenthesized_expression_list (cp_parser* parser,
6640 int is_attribute_list,
6641 bool cast_p,
6642 bool allow_expansion_p,
6643 bool *non_constant_p)
6645 vec<tree, va_gc> *expression_list;
6646 bool fold_expr_p = is_attribute_list != non_attr;
6647 tree identifier = NULL_TREE;
6648 bool saved_greater_than_is_operator_p;
6650 /* Assume all the expressions will be constant. */
6651 if (non_constant_p)
6652 *non_constant_p = false;
6654 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6655 return NULL;
6657 expression_list = make_tree_vector ();
6659 /* Within a parenthesized expression, a `>' token is always
6660 the greater-than operator. */
6661 saved_greater_than_is_operator_p
6662 = parser->greater_than_is_operator_p;
6663 parser->greater_than_is_operator_p = true;
6665 /* Consume expressions until there are no more. */
6666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6667 while (true)
6669 tree expr;
6671 /* At the beginning of attribute lists, check to see if the
6672 next token is an identifier. */
6673 if (is_attribute_list == id_attr
6674 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6676 cp_token *token;
6678 /* Consume the identifier. */
6679 token = cp_lexer_consume_token (parser->lexer);
6680 /* Save the identifier. */
6681 identifier = token->u.value;
6683 else
6685 bool expr_non_constant_p;
6687 /* Parse the next assignment-expression. */
6688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6690 /* A braced-init-list. */
6691 cp_lexer_set_source_position (parser->lexer);
6692 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6693 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6694 if (non_constant_p && expr_non_constant_p)
6695 *non_constant_p = true;
6697 else if (non_constant_p)
6699 expr = (cp_parser_constant_expression
6700 (parser, /*allow_non_constant_p=*/true,
6701 &expr_non_constant_p));
6702 if (expr_non_constant_p)
6703 *non_constant_p = true;
6705 else
6706 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6708 if (fold_expr_p)
6709 expr = fold_non_dependent_expr (expr);
6711 /* If we have an ellipsis, then this is an expression
6712 expansion. */
6713 if (allow_expansion_p
6714 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6716 /* Consume the `...'. */
6717 cp_lexer_consume_token (parser->lexer);
6719 /* Build the argument pack. */
6720 expr = make_pack_expansion (expr);
6723 /* Add it to the list. We add error_mark_node
6724 expressions to the list, so that we can still tell if
6725 the correct form for a parenthesized expression-list
6726 is found. That gives better errors. */
6727 vec_safe_push (expression_list, expr);
6729 if (expr == error_mark_node)
6730 goto skip_comma;
6733 /* After the first item, attribute lists look the same as
6734 expression lists. */
6735 is_attribute_list = non_attr;
6737 get_comma:;
6738 /* If the next token isn't a `,', then we are done. */
6739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6740 break;
6742 /* Otherwise, consume the `,' and keep going. */
6743 cp_lexer_consume_token (parser->lexer);
6746 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6748 int ending;
6750 skip_comma:;
6751 /* We try and resync to an unnested comma, as that will give the
6752 user better diagnostics. */
6753 ending = cp_parser_skip_to_closing_parenthesis (parser,
6754 /*recovering=*/true,
6755 /*or_comma=*/true,
6756 /*consume_paren=*/true);
6757 if (ending < 0)
6758 goto get_comma;
6759 if (!ending)
6761 parser->greater_than_is_operator_p
6762 = saved_greater_than_is_operator_p;
6763 return NULL;
6767 parser->greater_than_is_operator_p
6768 = saved_greater_than_is_operator_p;
6770 if (identifier)
6771 vec_safe_insert (expression_list, 0, identifier);
6773 return expression_list;
6776 /* Parse a pseudo-destructor-name.
6778 pseudo-destructor-name:
6779 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6780 :: [opt] nested-name-specifier template template-id :: ~ type-name
6781 :: [opt] nested-name-specifier [opt] ~ type-name
6783 If either of the first two productions is used, sets *SCOPE to the
6784 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6785 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6786 or ERROR_MARK_NODE if the parse fails. */
6788 static void
6789 cp_parser_pseudo_destructor_name (cp_parser* parser,
6790 tree object,
6791 tree* scope,
6792 tree* type)
6794 bool nested_name_specifier_p;
6796 /* Handle ~auto. */
6797 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6798 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6799 && !type_dependent_expression_p (object))
6801 if (cxx_dialect < cxx1y)
6802 pedwarn (input_location, 0,
6803 "%<~auto%> only available with "
6804 "-std=c++1y or -std=gnu++1y");
6805 cp_lexer_consume_token (parser->lexer);
6806 cp_lexer_consume_token (parser->lexer);
6807 *scope = NULL_TREE;
6808 *type = TREE_TYPE (object);
6809 return;
6812 /* Assume that things will not work out. */
6813 *type = error_mark_node;
6815 /* Look for the optional `::' operator. */
6816 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6817 /* Look for the optional nested-name-specifier. */
6818 nested_name_specifier_p
6819 = (cp_parser_nested_name_specifier_opt (parser,
6820 /*typename_keyword_p=*/false,
6821 /*check_dependency_p=*/true,
6822 /*type_p=*/false,
6823 /*is_declaration=*/false)
6824 != NULL_TREE);
6825 /* Now, if we saw a nested-name-specifier, we might be doing the
6826 second production. */
6827 if (nested_name_specifier_p
6828 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6830 /* Consume the `template' keyword. */
6831 cp_lexer_consume_token (parser->lexer);
6832 /* Parse the template-id. */
6833 cp_parser_template_id (parser,
6834 /*template_keyword_p=*/true,
6835 /*check_dependency_p=*/false,
6836 class_type,
6837 /*is_declaration=*/true);
6838 /* Look for the `::' token. */
6839 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6841 /* If the next token is not a `~', then there might be some
6842 additional qualification. */
6843 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6845 /* At this point, we're looking for "type-name :: ~". The type-name
6846 must not be a class-name, since this is a pseudo-destructor. So,
6847 it must be either an enum-name, or a typedef-name -- both of which
6848 are just identifiers. So, we peek ahead to check that the "::"
6849 and "~" tokens are present; if they are not, then we can avoid
6850 calling type_name. */
6851 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6852 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6853 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6855 cp_parser_error (parser, "non-scalar type");
6856 return;
6859 /* Look for the type-name. */
6860 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6861 if (*scope == error_mark_node)
6862 return;
6864 /* Look for the `::' token. */
6865 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6867 else
6868 *scope = NULL_TREE;
6870 /* Look for the `~'. */
6871 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6873 /* Once we see the ~, this has to be a pseudo-destructor. */
6874 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6875 cp_parser_commit_to_topmost_tentative_parse (parser);
6877 /* Look for the type-name again. We are not responsible for
6878 checking that it matches the first type-name. */
6879 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6882 /* Parse a unary-expression.
6884 unary-expression:
6885 postfix-expression
6886 ++ cast-expression
6887 -- cast-expression
6888 unary-operator cast-expression
6889 sizeof unary-expression
6890 sizeof ( type-id )
6891 alignof ( type-id ) [C++0x]
6892 new-expression
6893 delete-expression
6895 GNU Extensions:
6897 unary-expression:
6898 __extension__ cast-expression
6899 __alignof__ unary-expression
6900 __alignof__ ( type-id )
6901 alignof unary-expression [C++0x]
6902 __real__ cast-expression
6903 __imag__ cast-expression
6904 && identifier
6905 sizeof ( type-id ) { initializer-list , [opt] }
6906 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6907 __alignof__ ( type-id ) { initializer-list , [opt] }
6909 ADDRESS_P is true iff the unary-expression is appearing as the
6910 operand of the `&' operator. CAST_P is true if this expression is
6911 the target of a cast.
6913 Returns a representation of the expression. */
6915 static tree
6916 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6917 bool decltype_p, cp_id_kind * pidk)
6919 cp_token *token;
6920 enum tree_code unary_operator;
6922 /* Peek at the next token. */
6923 token = cp_lexer_peek_token (parser->lexer);
6924 /* Some keywords give away the kind of expression. */
6925 if (token->type == CPP_KEYWORD)
6927 enum rid keyword = token->keyword;
6929 switch (keyword)
6931 case RID_ALIGNOF:
6932 case RID_SIZEOF:
6934 tree operand, ret;
6935 enum tree_code op;
6936 location_t first_loc;
6938 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6939 /* Consume the token. */
6940 cp_lexer_consume_token (parser->lexer);
6941 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6942 /* Parse the operand. */
6943 operand = cp_parser_sizeof_operand (parser, keyword);
6945 if (TYPE_P (operand))
6946 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6947 else
6949 /* ISO C++ defines alignof only with types, not with
6950 expressions. So pedwarn if alignof is used with a non-
6951 type expression. However, __alignof__ is ok. */
6952 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6953 pedwarn (token->location, OPT_Wpedantic,
6954 "ISO C++ does not allow %<alignof%> "
6955 "with a non-type");
6957 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6959 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6960 SIZEOF_EXPR with the original operand. */
6961 if (op == SIZEOF_EXPR && ret != error_mark_node)
6963 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6965 if (!processing_template_decl && TYPE_P (operand))
6967 ret = build_min (SIZEOF_EXPR, size_type_node,
6968 build1 (NOP_EXPR, operand,
6969 error_mark_node));
6970 SIZEOF_EXPR_TYPE_P (ret) = 1;
6972 else
6973 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6974 TREE_SIDE_EFFECTS (ret) = 0;
6975 TREE_READONLY (ret) = 1;
6977 SET_EXPR_LOCATION (ret, first_loc);
6979 return ret;
6982 case RID_NEW:
6983 return cp_parser_new_expression (parser);
6985 case RID_DELETE:
6986 return cp_parser_delete_expression (parser);
6988 case RID_EXTENSION:
6990 /* The saved value of the PEDANTIC flag. */
6991 int saved_pedantic;
6992 tree expr;
6994 /* Save away the PEDANTIC flag. */
6995 cp_parser_extension_opt (parser, &saved_pedantic);
6996 /* Parse the cast-expression. */
6997 expr = cp_parser_simple_cast_expression (parser);
6998 /* Restore the PEDANTIC flag. */
6999 pedantic = saved_pedantic;
7001 return expr;
7004 case RID_REALPART:
7005 case RID_IMAGPART:
7007 tree expression;
7009 /* Consume the `__real__' or `__imag__' token. */
7010 cp_lexer_consume_token (parser->lexer);
7011 /* Parse the cast-expression. */
7012 expression = cp_parser_simple_cast_expression (parser);
7013 /* Create the complete representation. */
7014 return build_x_unary_op (token->location,
7015 (keyword == RID_REALPART
7016 ? REALPART_EXPR : IMAGPART_EXPR),
7017 expression,
7018 tf_warning_or_error);
7020 break;
7022 case RID_TRANSACTION_ATOMIC:
7023 case RID_TRANSACTION_RELAXED:
7024 return cp_parser_transaction_expression (parser, keyword);
7026 case RID_NOEXCEPT:
7028 tree expr;
7029 const char *saved_message;
7030 bool saved_integral_constant_expression_p;
7031 bool saved_non_integral_constant_expression_p;
7032 bool saved_greater_than_is_operator_p;
7034 cp_lexer_consume_token (parser->lexer);
7035 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7037 saved_message = parser->type_definition_forbidden_message;
7038 parser->type_definition_forbidden_message
7039 = G_("types may not be defined in %<noexcept%> expressions");
7041 saved_integral_constant_expression_p
7042 = parser->integral_constant_expression_p;
7043 saved_non_integral_constant_expression_p
7044 = parser->non_integral_constant_expression_p;
7045 parser->integral_constant_expression_p = false;
7047 saved_greater_than_is_operator_p
7048 = parser->greater_than_is_operator_p;
7049 parser->greater_than_is_operator_p = true;
7051 ++cp_unevaluated_operand;
7052 ++c_inhibit_evaluation_warnings;
7053 expr = cp_parser_expression (parser, false, NULL);
7054 --c_inhibit_evaluation_warnings;
7055 --cp_unevaluated_operand;
7057 parser->greater_than_is_operator_p
7058 = saved_greater_than_is_operator_p;
7060 parser->integral_constant_expression_p
7061 = saved_integral_constant_expression_p;
7062 parser->non_integral_constant_expression_p
7063 = saved_non_integral_constant_expression_p;
7065 parser->type_definition_forbidden_message = saved_message;
7067 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7068 return finish_noexcept_expr (expr, tf_warning_or_error);
7071 default:
7072 break;
7076 /* Look for the `:: new' and `:: delete', which also signal the
7077 beginning of a new-expression, or delete-expression,
7078 respectively. If the next token is `::', then it might be one of
7079 these. */
7080 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7082 enum rid keyword;
7084 /* See if the token after the `::' is one of the keywords in
7085 which we're interested. */
7086 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7087 /* If it's `new', we have a new-expression. */
7088 if (keyword == RID_NEW)
7089 return cp_parser_new_expression (parser);
7090 /* Similarly, for `delete'. */
7091 else if (keyword == RID_DELETE)
7092 return cp_parser_delete_expression (parser);
7095 /* Look for a unary operator. */
7096 unary_operator = cp_parser_unary_operator (token);
7097 /* The `++' and `--' operators can be handled similarly, even though
7098 they are not technically unary-operators in the grammar. */
7099 if (unary_operator == ERROR_MARK)
7101 if (token->type == CPP_PLUS_PLUS)
7102 unary_operator = PREINCREMENT_EXPR;
7103 else if (token->type == CPP_MINUS_MINUS)
7104 unary_operator = PREDECREMENT_EXPR;
7105 /* Handle the GNU address-of-label extension. */
7106 else if (cp_parser_allow_gnu_extensions_p (parser)
7107 && token->type == CPP_AND_AND)
7109 tree identifier;
7110 tree expression;
7111 location_t loc = token->location;
7113 /* Consume the '&&' token. */
7114 cp_lexer_consume_token (parser->lexer);
7115 /* Look for the identifier. */
7116 identifier = cp_parser_identifier (parser);
7117 /* Create an expression representing the address. */
7118 expression = finish_label_address_expr (identifier, loc);
7119 if (cp_parser_non_integral_constant_expression (parser,
7120 NIC_ADDR_LABEL))
7121 expression = error_mark_node;
7122 return expression;
7125 if (unary_operator != ERROR_MARK)
7127 tree cast_expression;
7128 tree expression = error_mark_node;
7129 non_integral_constant non_constant_p = NIC_NONE;
7130 location_t loc = token->location;
7131 tsubst_flags_t complain = complain_flags (decltype_p);
7133 /* Consume the operator token. */
7134 token = cp_lexer_consume_token (parser->lexer);
7135 /* Parse the cast-expression. */
7136 cast_expression
7137 = cp_parser_cast_expression (parser,
7138 unary_operator == ADDR_EXPR,
7139 /*cast_p=*/false,
7140 /*decltype*/false,
7141 pidk);
7142 /* Now, build an appropriate representation. */
7143 switch (unary_operator)
7145 case INDIRECT_REF:
7146 non_constant_p = NIC_STAR;
7147 expression = build_x_indirect_ref (loc, cast_expression,
7148 RO_UNARY_STAR,
7149 complain);
7150 break;
7152 case ADDR_EXPR:
7153 non_constant_p = NIC_ADDR;
7154 /* Fall through. */
7155 case BIT_NOT_EXPR:
7156 expression = build_x_unary_op (loc, unary_operator,
7157 cast_expression,
7158 complain);
7159 break;
7161 case PREINCREMENT_EXPR:
7162 case PREDECREMENT_EXPR:
7163 non_constant_p = unary_operator == PREINCREMENT_EXPR
7164 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7165 /* Fall through. */
7166 case UNARY_PLUS_EXPR:
7167 case NEGATE_EXPR:
7168 case TRUTH_NOT_EXPR:
7169 expression = finish_unary_op_expr (loc, unary_operator,
7170 cast_expression, complain);
7171 break;
7173 default:
7174 gcc_unreachable ();
7177 if (non_constant_p != NIC_NONE
7178 && cp_parser_non_integral_constant_expression (parser,
7179 non_constant_p))
7180 expression = error_mark_node;
7182 return expression;
7185 return cp_parser_postfix_expression (parser, address_p, cast_p,
7186 /*member_access_only_p=*/false,
7187 decltype_p,
7188 pidk);
7191 static inline tree
7192 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7193 cp_id_kind * pidk)
7195 return cp_parser_unary_expression (parser, address_p, cast_p,
7196 /*decltype*/false, pidk);
7199 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7200 unary-operator, the corresponding tree code is returned. */
7202 static enum tree_code
7203 cp_parser_unary_operator (cp_token* token)
7205 switch (token->type)
7207 case CPP_MULT:
7208 return INDIRECT_REF;
7210 case CPP_AND:
7211 return ADDR_EXPR;
7213 case CPP_PLUS:
7214 return UNARY_PLUS_EXPR;
7216 case CPP_MINUS:
7217 return NEGATE_EXPR;
7219 case CPP_NOT:
7220 return TRUTH_NOT_EXPR;
7222 case CPP_COMPL:
7223 return BIT_NOT_EXPR;
7225 default:
7226 return ERROR_MARK;
7230 /* Parse a new-expression.
7232 new-expression:
7233 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7234 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7236 Returns a representation of the expression. */
7238 static tree
7239 cp_parser_new_expression (cp_parser* parser)
7241 bool global_scope_p;
7242 vec<tree, va_gc> *placement;
7243 tree type;
7244 vec<tree, va_gc> *initializer;
7245 tree nelts = NULL_TREE;
7246 tree ret;
7248 /* Look for the optional `::' operator. */
7249 global_scope_p
7250 = (cp_parser_global_scope_opt (parser,
7251 /*current_scope_valid_p=*/false)
7252 != NULL_TREE);
7253 /* Look for the `new' operator. */
7254 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7255 /* There's no easy way to tell a new-placement from the
7256 `( type-id )' construct. */
7257 cp_parser_parse_tentatively (parser);
7258 /* Look for a new-placement. */
7259 placement = cp_parser_new_placement (parser);
7260 /* If that didn't work out, there's no new-placement. */
7261 if (!cp_parser_parse_definitely (parser))
7263 if (placement != NULL)
7264 release_tree_vector (placement);
7265 placement = NULL;
7268 /* If the next token is a `(', then we have a parenthesized
7269 type-id. */
7270 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7272 cp_token *token;
7273 const char *saved_message = parser->type_definition_forbidden_message;
7275 /* Consume the `('. */
7276 cp_lexer_consume_token (parser->lexer);
7278 /* Parse the type-id. */
7279 parser->type_definition_forbidden_message
7280 = G_("types may not be defined in a new-expression");
7281 type = cp_parser_type_id (parser);
7282 parser->type_definition_forbidden_message = saved_message;
7284 /* Look for the closing `)'. */
7285 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7286 token = cp_lexer_peek_token (parser->lexer);
7287 /* There should not be a direct-new-declarator in this production,
7288 but GCC used to allowed this, so we check and emit a sensible error
7289 message for this case. */
7290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7292 error_at (token->location,
7293 "array bound forbidden after parenthesized type-id");
7294 inform (token->location,
7295 "try removing the parentheses around the type-id");
7296 cp_parser_direct_new_declarator (parser);
7299 /* Otherwise, there must be a new-type-id. */
7300 else
7301 type = cp_parser_new_type_id (parser, &nelts);
7303 /* If the next token is a `(' or '{', then we have a new-initializer. */
7304 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7305 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7306 initializer = cp_parser_new_initializer (parser);
7307 else
7308 initializer = NULL;
7310 /* A new-expression may not appear in an integral constant
7311 expression. */
7312 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7313 ret = error_mark_node;
7314 else
7316 /* Create a representation of the new-expression. */
7317 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7318 tf_warning_or_error);
7321 if (placement != NULL)
7322 release_tree_vector (placement);
7323 if (initializer != NULL)
7324 release_tree_vector (initializer);
7326 return ret;
7329 /* Parse a new-placement.
7331 new-placement:
7332 ( expression-list )
7334 Returns the same representation as for an expression-list. */
7336 static vec<tree, va_gc> *
7337 cp_parser_new_placement (cp_parser* parser)
7339 vec<tree, va_gc> *expression_list;
7341 /* Parse the expression-list. */
7342 expression_list = (cp_parser_parenthesized_expression_list
7343 (parser, non_attr, /*cast_p=*/false,
7344 /*allow_expansion_p=*/true,
7345 /*non_constant_p=*/NULL));
7347 return expression_list;
7350 /* Parse a new-type-id.
7352 new-type-id:
7353 type-specifier-seq new-declarator [opt]
7355 Returns the TYPE allocated. If the new-type-id indicates an array
7356 type, *NELTS is set to the number of elements in the last array
7357 bound; the TYPE will not include the last array bound. */
7359 static tree
7360 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7362 cp_decl_specifier_seq type_specifier_seq;
7363 cp_declarator *new_declarator;
7364 cp_declarator *declarator;
7365 cp_declarator *outer_declarator;
7366 const char *saved_message;
7368 /* The type-specifier sequence must not contain type definitions.
7369 (It cannot contain declarations of new types either, but if they
7370 are not definitions we will catch that because they are not
7371 complete.) */
7372 saved_message = parser->type_definition_forbidden_message;
7373 parser->type_definition_forbidden_message
7374 = G_("types may not be defined in a new-type-id");
7375 /* Parse the type-specifier-seq. */
7376 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7377 /*is_trailing_return=*/false,
7378 &type_specifier_seq);
7379 /* Restore the old message. */
7380 parser->type_definition_forbidden_message = saved_message;
7382 if (type_specifier_seq.type == error_mark_node)
7383 return error_mark_node;
7385 /* Parse the new-declarator. */
7386 new_declarator = cp_parser_new_declarator_opt (parser);
7388 /* Determine the number of elements in the last array dimension, if
7389 any. */
7390 *nelts = NULL_TREE;
7391 /* Skip down to the last array dimension. */
7392 declarator = new_declarator;
7393 outer_declarator = NULL;
7394 while (declarator && (declarator->kind == cdk_pointer
7395 || declarator->kind == cdk_ptrmem))
7397 outer_declarator = declarator;
7398 declarator = declarator->declarator;
7400 while (declarator
7401 && declarator->kind == cdk_array
7402 && declarator->declarator
7403 && declarator->declarator->kind == cdk_array)
7405 outer_declarator = declarator;
7406 declarator = declarator->declarator;
7409 if (declarator && declarator->kind == cdk_array)
7411 *nelts = declarator->u.array.bounds;
7412 if (*nelts == error_mark_node)
7413 *nelts = integer_one_node;
7415 if (outer_declarator)
7416 outer_declarator->declarator = declarator->declarator;
7417 else
7418 new_declarator = NULL;
7421 return groktypename (&type_specifier_seq, new_declarator, false);
7424 /* Parse an (optional) new-declarator.
7426 new-declarator:
7427 ptr-operator new-declarator [opt]
7428 direct-new-declarator
7430 Returns the declarator. */
7432 static cp_declarator *
7433 cp_parser_new_declarator_opt (cp_parser* parser)
7435 enum tree_code code;
7436 tree type, std_attributes = NULL_TREE;
7437 cp_cv_quals cv_quals;
7439 /* We don't know if there's a ptr-operator next, or not. */
7440 cp_parser_parse_tentatively (parser);
7441 /* Look for a ptr-operator. */
7442 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7443 /* If that worked, look for more new-declarators. */
7444 if (cp_parser_parse_definitely (parser))
7446 cp_declarator *declarator;
7448 /* Parse another optional declarator. */
7449 declarator = cp_parser_new_declarator_opt (parser);
7451 declarator = cp_parser_make_indirect_declarator
7452 (code, type, cv_quals, declarator, std_attributes);
7454 return declarator;
7457 /* If the next token is a `[', there is a direct-new-declarator. */
7458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7459 return cp_parser_direct_new_declarator (parser);
7461 return NULL;
7464 /* Parse a direct-new-declarator.
7466 direct-new-declarator:
7467 [ expression ]
7468 direct-new-declarator [constant-expression]
7472 static cp_declarator *
7473 cp_parser_direct_new_declarator (cp_parser* parser)
7475 cp_declarator *declarator = NULL;
7477 while (true)
7479 tree expression;
7480 cp_token *token;
7482 /* Look for the opening `['. */
7483 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7485 token = cp_lexer_peek_token (parser->lexer);
7486 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7487 /* The standard requires that the expression have integral
7488 type. DR 74 adds enumeration types. We believe that the
7489 real intent is that these expressions be handled like the
7490 expression in a `switch' condition, which also allows
7491 classes with a single conversion to integral or
7492 enumeration type. */
7493 if (!processing_template_decl)
7495 expression
7496 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7497 expression,
7498 /*complain=*/true);
7499 if (!expression)
7501 error_at (token->location,
7502 "expression in new-declarator must have integral "
7503 "or enumeration type");
7504 expression = error_mark_node;
7508 /* Look for the closing `]'. */
7509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7511 /* Add this bound to the declarator. */
7512 declarator = make_array_declarator (declarator, expression);
7514 /* If the next token is not a `[', then there are no more
7515 bounds. */
7516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7517 break;
7520 return declarator;
7523 /* Parse a new-initializer.
7525 new-initializer:
7526 ( expression-list [opt] )
7527 braced-init-list
7529 Returns a representation of the expression-list. */
7531 static vec<tree, va_gc> *
7532 cp_parser_new_initializer (cp_parser* parser)
7534 vec<tree, va_gc> *expression_list;
7536 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7538 tree t;
7539 bool expr_non_constant_p;
7540 cp_lexer_set_source_position (parser->lexer);
7541 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7542 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7543 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7544 expression_list = make_tree_vector_single (t);
7546 else
7547 expression_list = (cp_parser_parenthesized_expression_list
7548 (parser, non_attr, /*cast_p=*/false,
7549 /*allow_expansion_p=*/true,
7550 /*non_constant_p=*/NULL));
7552 return expression_list;
7555 /* Parse a delete-expression.
7557 delete-expression:
7558 :: [opt] delete cast-expression
7559 :: [opt] delete [ ] cast-expression
7561 Returns a representation of the expression. */
7563 static tree
7564 cp_parser_delete_expression (cp_parser* parser)
7566 bool global_scope_p;
7567 bool array_p;
7568 tree expression;
7570 /* Look for the optional `::' operator. */
7571 global_scope_p
7572 = (cp_parser_global_scope_opt (parser,
7573 /*current_scope_valid_p=*/false)
7574 != NULL_TREE);
7575 /* Look for the `delete' keyword. */
7576 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7577 /* See if the array syntax is in use. */
7578 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7580 /* Consume the `[' token. */
7581 cp_lexer_consume_token (parser->lexer);
7582 /* Look for the `]' token. */
7583 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7584 /* Remember that this is the `[]' construct. */
7585 array_p = true;
7587 else
7588 array_p = false;
7590 /* Parse the cast-expression. */
7591 expression = cp_parser_simple_cast_expression (parser);
7593 /* A delete-expression may not appear in an integral constant
7594 expression. */
7595 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7596 return error_mark_node;
7598 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7599 tf_warning_or_error);
7602 /* Returns true if TOKEN may start a cast-expression and false
7603 otherwise. */
7605 static bool
7606 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7608 cp_token *token = cp_lexer_peek_token (parser->lexer);
7609 switch (token->type)
7611 case CPP_COMMA:
7612 case CPP_SEMICOLON:
7613 case CPP_QUERY:
7614 case CPP_COLON:
7615 case CPP_CLOSE_SQUARE:
7616 case CPP_CLOSE_PAREN:
7617 case CPP_CLOSE_BRACE:
7618 case CPP_OPEN_BRACE:
7619 case CPP_DOT:
7620 case CPP_DOT_STAR:
7621 case CPP_DEREF:
7622 case CPP_DEREF_STAR:
7623 case CPP_DIV:
7624 case CPP_MOD:
7625 case CPP_LSHIFT:
7626 case CPP_RSHIFT:
7627 case CPP_LESS:
7628 case CPP_GREATER:
7629 case CPP_LESS_EQ:
7630 case CPP_GREATER_EQ:
7631 case CPP_EQ_EQ:
7632 case CPP_NOT_EQ:
7633 case CPP_EQ:
7634 case CPP_MULT_EQ:
7635 case CPP_DIV_EQ:
7636 case CPP_MOD_EQ:
7637 case CPP_PLUS_EQ:
7638 case CPP_MINUS_EQ:
7639 case CPP_RSHIFT_EQ:
7640 case CPP_LSHIFT_EQ:
7641 case CPP_AND_EQ:
7642 case CPP_XOR_EQ:
7643 case CPP_OR_EQ:
7644 case CPP_XOR:
7645 case CPP_OR:
7646 case CPP_OR_OR:
7647 case CPP_EOF:
7648 return false;
7650 case CPP_OPEN_PAREN:
7651 /* In ((type ()) () the last () isn't a valid cast-expression,
7652 so the whole must be parsed as postfix-expression. */
7653 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7654 != CPP_CLOSE_PAREN;
7656 /* '[' may start a primary-expression in obj-c++. */
7657 case CPP_OPEN_SQUARE:
7658 return c_dialect_objc ();
7660 default:
7661 return true;
7665 /* Parse a cast-expression.
7667 cast-expression:
7668 unary-expression
7669 ( type-id ) cast-expression
7671 ADDRESS_P is true iff the unary-expression is appearing as the
7672 operand of the `&' operator. CAST_P is true if this expression is
7673 the target of a cast.
7675 Returns a representation of the expression. */
7677 static tree
7678 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7679 bool decltype_p, cp_id_kind * pidk)
7681 /* If it's a `(', then we might be looking at a cast. */
7682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7684 tree type = NULL_TREE;
7685 tree expr = NULL_TREE;
7686 bool cast_expression_p;
7687 const char *saved_message;
7689 /* There's no way to know yet whether or not this is a cast.
7690 For example, `(int (3))' is a unary-expression, while `(int)
7691 3' is a cast. So, we resort to parsing tentatively. */
7692 cp_parser_parse_tentatively (parser);
7693 /* Types may not be defined in a cast. */
7694 saved_message = parser->type_definition_forbidden_message;
7695 parser->type_definition_forbidden_message
7696 = G_("types may not be defined in casts");
7697 /* Consume the `('. */
7698 cp_lexer_consume_token (parser->lexer);
7699 /* A very tricky bit is that `(struct S) { 3 }' is a
7700 compound-literal (which we permit in C++ as an extension).
7701 But, that construct is not a cast-expression -- it is a
7702 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7703 is legal; if the compound-literal were a cast-expression,
7704 you'd need an extra set of parentheses.) But, if we parse
7705 the type-id, and it happens to be a class-specifier, then we
7706 will commit to the parse at that point, because we cannot
7707 undo the action that is done when creating a new class. So,
7708 then we cannot back up and do a postfix-expression.
7709 Another tricky case is the following (c++/29234):
7711 struct S { void operator () (); };
7713 void foo ()
7715 ( S()() );
7718 As a type-id we parse the parenthesized S()() as a function
7719 returning a function, groktypename complains and we cannot
7720 back up in this case either.
7722 Therefore, we scan ahead to the closing `)', and check to see
7723 if the tokens after the `)' can start a cast-expression. Otherwise
7724 we are dealing with an unary-expression, a postfix-expression
7725 or something else.
7727 Save tokens so that we can put them back. */
7728 cp_lexer_save_tokens (parser->lexer);
7730 /* We may be looking at a cast-expression. */
7731 cast_expression_p
7732 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7733 /*consume_paren=*/true)
7734 && cp_parser_tokens_start_cast_expression (parser));
7736 /* Roll back the tokens we skipped. */
7737 cp_lexer_rollback_tokens (parser->lexer);
7738 /* If we aren't looking at a cast-expression, simulate an error so
7739 that the call to cp_parser_parse_definitely below will fail. */
7740 if (!cast_expression_p)
7741 cp_parser_simulate_error (parser);
7742 else
7744 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7745 parser->in_type_id_in_expr_p = true;
7746 /* Look for the type-id. */
7747 type = cp_parser_type_id (parser);
7748 /* Look for the closing `)'. */
7749 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7750 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7753 /* Restore the saved message. */
7754 parser->type_definition_forbidden_message = saved_message;
7756 /* At this point this can only be either a cast or a
7757 parenthesized ctor such as `(T ())' that looks like a cast to
7758 function returning T. */
7759 if (!cp_parser_error_occurred (parser))
7761 cp_parser_parse_definitely (parser);
7762 expr = cp_parser_cast_expression (parser,
7763 /*address_p=*/false,
7764 /*cast_p=*/true,
7765 /*decltype_p=*/false,
7766 pidk);
7768 /* Warn about old-style casts, if so requested. */
7769 if (warn_old_style_cast
7770 && !in_system_header_at (input_location)
7771 && !VOID_TYPE_P (type)
7772 && current_lang_name != lang_name_c)
7773 warning (OPT_Wold_style_cast, "use of old-style cast");
7775 /* Only type conversions to integral or enumeration types
7776 can be used in constant-expressions. */
7777 if (!cast_valid_in_integral_constant_expression_p (type)
7778 && cp_parser_non_integral_constant_expression (parser,
7779 NIC_CAST))
7780 return error_mark_node;
7782 /* Perform the cast. */
7783 expr = build_c_cast (input_location, type, expr);
7784 return expr;
7786 else
7787 cp_parser_abort_tentative_parse (parser);
7790 /* If we get here, then it's not a cast, so it must be a
7791 unary-expression. */
7792 return cp_parser_unary_expression (parser, address_p, cast_p,
7793 decltype_p, pidk);
7796 /* Parse a binary expression of the general form:
7798 pm-expression:
7799 cast-expression
7800 pm-expression .* cast-expression
7801 pm-expression ->* cast-expression
7803 multiplicative-expression:
7804 pm-expression
7805 multiplicative-expression * pm-expression
7806 multiplicative-expression / pm-expression
7807 multiplicative-expression % pm-expression
7809 additive-expression:
7810 multiplicative-expression
7811 additive-expression + multiplicative-expression
7812 additive-expression - multiplicative-expression
7814 shift-expression:
7815 additive-expression
7816 shift-expression << additive-expression
7817 shift-expression >> additive-expression
7819 relational-expression:
7820 shift-expression
7821 relational-expression < shift-expression
7822 relational-expression > shift-expression
7823 relational-expression <= shift-expression
7824 relational-expression >= shift-expression
7826 GNU Extension:
7828 relational-expression:
7829 relational-expression <? shift-expression
7830 relational-expression >? shift-expression
7832 equality-expression:
7833 relational-expression
7834 equality-expression == relational-expression
7835 equality-expression != relational-expression
7837 and-expression:
7838 equality-expression
7839 and-expression & equality-expression
7841 exclusive-or-expression:
7842 and-expression
7843 exclusive-or-expression ^ and-expression
7845 inclusive-or-expression:
7846 exclusive-or-expression
7847 inclusive-or-expression | exclusive-or-expression
7849 logical-and-expression:
7850 inclusive-or-expression
7851 logical-and-expression && inclusive-or-expression
7853 logical-or-expression:
7854 logical-and-expression
7855 logical-or-expression || logical-and-expression
7857 All these are implemented with a single function like:
7859 binary-expression:
7860 simple-cast-expression
7861 binary-expression <token> binary-expression
7863 CAST_P is true if this expression is the target of a cast.
7865 The binops_by_token map is used to get the tree codes for each <token> type.
7866 binary-expressions are associated according to a precedence table. */
7868 #define TOKEN_PRECEDENCE(token) \
7869 (((token->type == CPP_GREATER \
7870 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7871 && !parser->greater_than_is_operator_p) \
7872 ? PREC_NOT_OPERATOR \
7873 : binops_by_token[token->type].prec)
7875 static tree
7876 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7877 bool no_toplevel_fold_p,
7878 bool decltype_p,
7879 enum cp_parser_prec prec,
7880 cp_id_kind * pidk)
7882 cp_parser_expression_stack stack;
7883 cp_parser_expression_stack_entry *sp = &stack[0];
7884 cp_parser_expression_stack_entry current;
7885 tree rhs;
7886 cp_token *token;
7887 enum tree_code rhs_type;
7888 enum cp_parser_prec new_prec, lookahead_prec;
7889 tree overload;
7891 /* Parse the first expression. */
7892 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7893 cast_p, decltype_p, pidk);
7894 current.lhs_type = ERROR_MARK;
7895 current.prec = prec;
7897 if (cp_parser_error_occurred (parser))
7898 return error_mark_node;
7900 for (;;)
7902 /* Get an operator token. */
7903 token = cp_lexer_peek_token (parser->lexer);
7905 if (warn_cxx0x_compat
7906 && token->type == CPP_RSHIFT
7907 && !parser->greater_than_is_operator_p)
7909 if (warning_at (token->location, OPT_Wc__0x_compat,
7910 "%<>>%> operator is treated"
7911 " as two right angle brackets in C++11"))
7912 inform (token->location,
7913 "suggest parentheses around %<>>%> expression");
7916 new_prec = TOKEN_PRECEDENCE (token);
7918 /* Popping an entry off the stack means we completed a subexpression:
7919 - either we found a token which is not an operator (`>' where it is not
7920 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7921 will happen repeatedly;
7922 - or, we found an operator which has lower priority. This is the case
7923 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7924 parsing `3 * 4'. */
7925 if (new_prec <= current.prec)
7927 if (sp == stack)
7928 break;
7929 else
7930 goto pop;
7933 get_rhs:
7934 current.tree_type = binops_by_token[token->type].tree_type;
7935 current.loc = token->location;
7937 /* We used the operator token. */
7938 cp_lexer_consume_token (parser->lexer);
7940 /* For "false && x" or "true || x", x will never be executed;
7941 disable warnings while evaluating it. */
7942 if (current.tree_type == TRUTH_ANDIF_EXPR)
7943 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7944 else if (current.tree_type == TRUTH_ORIF_EXPR)
7945 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7947 /* Extract another operand. It may be the RHS of this expression
7948 or the LHS of a new, higher priority expression. */
7949 rhs = cp_parser_simple_cast_expression (parser);
7950 rhs_type = ERROR_MARK;
7952 /* Get another operator token. Look up its precedence to avoid
7953 building a useless (immediately popped) stack entry for common
7954 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7955 token = cp_lexer_peek_token (parser->lexer);
7956 lookahead_prec = TOKEN_PRECEDENCE (token);
7957 if (lookahead_prec > new_prec)
7959 /* ... and prepare to parse the RHS of the new, higher priority
7960 expression. Since precedence levels on the stack are
7961 monotonically increasing, we do not have to care about
7962 stack overflows. */
7963 *sp = current;
7964 ++sp;
7965 current.lhs = rhs;
7966 current.lhs_type = rhs_type;
7967 current.prec = new_prec;
7968 new_prec = lookahead_prec;
7969 goto get_rhs;
7971 pop:
7972 lookahead_prec = new_prec;
7973 /* If the stack is not empty, we have parsed into LHS the right side
7974 (`4' in the example above) of an expression we had suspended.
7975 We can use the information on the stack to recover the LHS (`3')
7976 from the stack together with the tree code (`MULT_EXPR'), and
7977 the precedence of the higher level subexpression
7978 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7979 which will be used to actually build the additive expression. */
7980 rhs = current.lhs;
7981 rhs_type = current.lhs_type;
7982 --sp;
7983 current = *sp;
7986 /* Undo the disabling of warnings done above. */
7987 if (current.tree_type == TRUTH_ANDIF_EXPR)
7988 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7989 else if (current.tree_type == TRUTH_ORIF_EXPR)
7990 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7992 overload = NULL;
7993 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7994 ERROR_MARK for everything that is not a binary expression.
7995 This makes warn_about_parentheses miss some warnings that
7996 involve unary operators. For unary expressions we should
7997 pass the correct tree_code unless the unary expression was
7998 surrounded by parentheses.
8000 if (no_toplevel_fold_p
8001 && lookahead_prec <= current.prec
8002 && sp == stack)
8003 current.lhs = build2 (current.tree_type,
8004 TREE_CODE_CLASS (current.tree_type)
8005 == tcc_comparison
8006 ? boolean_type_node : TREE_TYPE (current.lhs),
8007 current.lhs, rhs);
8008 else
8009 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8010 current.lhs, current.lhs_type,
8011 rhs, rhs_type, &overload,
8012 complain_flags (decltype_p));
8013 current.lhs_type = current.tree_type;
8014 if (EXPR_P (current.lhs))
8015 SET_EXPR_LOCATION (current.lhs, current.loc);
8017 /* If the binary operator required the use of an overloaded operator,
8018 then this expression cannot be an integral constant-expression.
8019 An overloaded operator can be used even if both operands are
8020 otherwise permissible in an integral constant-expression if at
8021 least one of the operands is of enumeration type. */
8023 if (overload
8024 && cp_parser_non_integral_constant_expression (parser,
8025 NIC_OVERLOADED))
8026 return error_mark_node;
8029 return current.lhs;
8032 static tree
8033 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8034 bool no_toplevel_fold_p,
8035 enum cp_parser_prec prec,
8036 cp_id_kind * pidk)
8038 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8039 /*decltype*/false, prec, pidk);
8042 /* Parse the `? expression : assignment-expression' part of a
8043 conditional-expression. The LOGICAL_OR_EXPR is the
8044 logical-or-expression that started the conditional-expression.
8045 Returns a representation of the entire conditional-expression.
8047 This routine is used by cp_parser_assignment_expression.
8049 ? expression : assignment-expression
8051 GNU Extensions:
8053 ? : assignment-expression */
8055 static tree
8056 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8058 tree expr;
8059 tree assignment_expr;
8060 struct cp_token *token;
8061 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8063 /* Consume the `?' token. */
8064 cp_lexer_consume_token (parser->lexer);
8065 token = cp_lexer_peek_token (parser->lexer);
8066 if (cp_parser_allow_gnu_extensions_p (parser)
8067 && token->type == CPP_COLON)
8069 pedwarn (token->location, OPT_Wpedantic,
8070 "ISO C++ does not allow ?: with omitted middle operand");
8071 /* Implicit true clause. */
8072 expr = NULL_TREE;
8073 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8074 warn_for_omitted_condop (token->location, logical_or_expr);
8076 else
8078 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8079 parser->colon_corrects_to_scope_p = false;
8080 /* Parse the expression. */
8081 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8082 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8083 c_inhibit_evaluation_warnings +=
8084 ((logical_or_expr == truthvalue_true_node)
8085 - (logical_or_expr == truthvalue_false_node));
8086 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8089 /* The next token should be a `:'. */
8090 cp_parser_require (parser, CPP_COLON, RT_COLON);
8091 /* Parse the assignment-expression. */
8092 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8093 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8095 /* Build the conditional-expression. */
8096 return build_x_conditional_expr (loc, logical_or_expr,
8097 expr,
8098 assignment_expr,
8099 tf_warning_or_error);
8102 /* Parse an assignment-expression.
8104 assignment-expression:
8105 conditional-expression
8106 logical-or-expression assignment-operator assignment_expression
8107 throw-expression
8109 CAST_P is true if this expression is the target of a cast.
8110 DECLTYPE_P is true if this expression is the operand of decltype.
8112 Returns a representation for the expression. */
8114 static tree
8115 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8116 bool decltype_p, cp_id_kind * pidk)
8118 tree expr;
8120 /* If the next token is the `throw' keyword, then we're looking at
8121 a throw-expression. */
8122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8123 expr = cp_parser_throw_expression (parser);
8124 /* Otherwise, it must be that we are looking at a
8125 logical-or-expression. */
8126 else
8128 /* Parse the binary expressions (logical-or-expression). */
8129 expr = cp_parser_binary_expression (parser, cast_p, false,
8130 decltype_p,
8131 PREC_NOT_OPERATOR, pidk);
8132 /* If the next token is a `?' then we're actually looking at a
8133 conditional-expression. */
8134 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8135 return cp_parser_question_colon_clause (parser, expr);
8136 else
8138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8140 /* If it's an assignment-operator, we're using the second
8141 production. */
8142 enum tree_code assignment_operator
8143 = cp_parser_assignment_operator_opt (parser);
8144 if (assignment_operator != ERROR_MARK)
8146 bool non_constant_p;
8147 location_t saved_input_location;
8149 /* Parse the right-hand side of the assignment. */
8150 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8152 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8153 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8155 /* An assignment may not appear in a
8156 constant-expression. */
8157 if (cp_parser_non_integral_constant_expression (parser,
8158 NIC_ASSIGNMENT))
8159 return error_mark_node;
8160 /* Build the assignment expression. Its default
8161 location is the location of the '=' token. */
8162 saved_input_location = input_location;
8163 input_location = loc;
8164 expr = build_x_modify_expr (loc, expr,
8165 assignment_operator,
8166 rhs,
8167 complain_flags (decltype_p));
8168 input_location = saved_input_location;
8173 return expr;
8176 static tree
8177 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8178 cp_id_kind * pidk)
8180 return cp_parser_assignment_expression (parser, cast_p,
8181 /*decltype*/false, pidk);
8184 /* Parse an (optional) assignment-operator.
8186 assignment-operator: one of
8187 = *= /= %= += -= >>= <<= &= ^= |=
8189 GNU Extension:
8191 assignment-operator: one of
8192 <?= >?=
8194 If the next token is an assignment operator, the corresponding tree
8195 code is returned, and the token is consumed. For example, for
8196 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8197 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8198 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8199 operator, ERROR_MARK is returned. */
8201 static enum tree_code
8202 cp_parser_assignment_operator_opt (cp_parser* parser)
8204 enum tree_code op;
8205 cp_token *token;
8207 /* Peek at the next token. */
8208 token = cp_lexer_peek_token (parser->lexer);
8210 switch (token->type)
8212 case CPP_EQ:
8213 op = NOP_EXPR;
8214 break;
8216 case CPP_MULT_EQ:
8217 op = MULT_EXPR;
8218 break;
8220 case CPP_DIV_EQ:
8221 op = TRUNC_DIV_EXPR;
8222 break;
8224 case CPP_MOD_EQ:
8225 op = TRUNC_MOD_EXPR;
8226 break;
8228 case CPP_PLUS_EQ:
8229 op = PLUS_EXPR;
8230 break;
8232 case CPP_MINUS_EQ:
8233 op = MINUS_EXPR;
8234 break;
8236 case CPP_RSHIFT_EQ:
8237 op = RSHIFT_EXPR;
8238 break;
8240 case CPP_LSHIFT_EQ:
8241 op = LSHIFT_EXPR;
8242 break;
8244 case CPP_AND_EQ:
8245 op = BIT_AND_EXPR;
8246 break;
8248 case CPP_XOR_EQ:
8249 op = BIT_XOR_EXPR;
8250 break;
8252 case CPP_OR_EQ:
8253 op = BIT_IOR_EXPR;
8254 break;
8256 default:
8257 /* Nothing else is an assignment operator. */
8258 op = ERROR_MARK;
8261 /* If it was an assignment operator, consume it. */
8262 if (op != ERROR_MARK)
8263 cp_lexer_consume_token (parser->lexer);
8265 return op;
8268 /* Parse an expression.
8270 expression:
8271 assignment-expression
8272 expression , assignment-expression
8274 CAST_P is true if this expression is the target of a cast.
8275 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8276 except possibly parenthesized or on the RHS of a comma (N3276).
8278 Returns a representation of the expression. */
8280 static tree
8281 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8282 cp_id_kind * pidk)
8284 tree expression = NULL_TREE;
8285 location_t loc = UNKNOWN_LOCATION;
8287 while (true)
8289 tree assignment_expression;
8291 /* Parse the next assignment-expression. */
8292 assignment_expression
8293 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8295 /* We don't create a temporary for a call that is the immediate operand
8296 of decltype or on the RHS of a comma. But when we see a comma, we
8297 need to create a temporary for a call on the LHS. */
8298 if (decltype_p && !processing_template_decl
8299 && TREE_CODE (assignment_expression) == CALL_EXPR
8300 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8301 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8302 assignment_expression
8303 = build_cplus_new (TREE_TYPE (assignment_expression),
8304 assignment_expression, tf_warning_or_error);
8306 /* If this is the first assignment-expression, we can just
8307 save it away. */
8308 if (!expression)
8309 expression = assignment_expression;
8310 else
8311 expression = build_x_compound_expr (loc, expression,
8312 assignment_expression,
8313 complain_flags (decltype_p));
8314 /* If the next token is not a comma, then we are done with the
8315 expression. */
8316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8317 break;
8318 /* Consume the `,'. */
8319 loc = cp_lexer_peek_token (parser->lexer)->location;
8320 cp_lexer_consume_token (parser->lexer);
8321 /* A comma operator cannot appear in a constant-expression. */
8322 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8323 expression = error_mark_node;
8326 return expression;
8329 static inline tree
8330 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8332 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8335 /* Parse a constant-expression.
8337 constant-expression:
8338 conditional-expression
8340 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8341 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8342 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8343 is false, NON_CONSTANT_P should be NULL. */
8345 static tree
8346 cp_parser_constant_expression (cp_parser* parser,
8347 bool allow_non_constant_p,
8348 bool *non_constant_p)
8350 bool saved_integral_constant_expression_p;
8351 bool saved_allow_non_integral_constant_expression_p;
8352 bool saved_non_integral_constant_expression_p;
8353 tree expression;
8355 /* It might seem that we could simply parse the
8356 conditional-expression, and then check to see if it were
8357 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8358 one that the compiler can figure out is constant, possibly after
8359 doing some simplifications or optimizations. The standard has a
8360 precise definition of constant-expression, and we must honor
8361 that, even though it is somewhat more restrictive.
8363 For example:
8365 int i[(2, 3)];
8367 is not a legal declaration, because `(2, 3)' is not a
8368 constant-expression. The `,' operator is forbidden in a
8369 constant-expression. However, GCC's constant-folding machinery
8370 will fold this operation to an INTEGER_CST for `3'. */
8372 /* Save the old settings. */
8373 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8374 saved_allow_non_integral_constant_expression_p
8375 = parser->allow_non_integral_constant_expression_p;
8376 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8377 /* We are now parsing a constant-expression. */
8378 parser->integral_constant_expression_p = true;
8379 parser->allow_non_integral_constant_expression_p
8380 = (allow_non_constant_p || cxx_dialect >= cxx11);
8381 parser->non_integral_constant_expression_p = false;
8382 /* Although the grammar says "conditional-expression", we parse an
8383 "assignment-expression", which also permits "throw-expression"
8384 and the use of assignment operators. In the case that
8385 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8386 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8387 actually essential that we look for an assignment-expression.
8388 For example, cp_parser_initializer_clauses uses this function to
8389 determine whether a particular assignment-expression is in fact
8390 constant. */
8391 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8392 /* Restore the old settings. */
8393 parser->integral_constant_expression_p
8394 = saved_integral_constant_expression_p;
8395 parser->allow_non_integral_constant_expression_p
8396 = saved_allow_non_integral_constant_expression_p;
8397 if (cxx_dialect >= cxx11)
8399 /* Require an rvalue constant expression here; that's what our
8400 callers expect. Reference constant expressions are handled
8401 separately in e.g. cp_parser_template_argument. */
8402 bool is_const = potential_rvalue_constant_expression (expression);
8403 parser->non_integral_constant_expression_p = !is_const;
8404 if (!is_const && !allow_non_constant_p)
8405 require_potential_rvalue_constant_expression (expression);
8407 if (allow_non_constant_p)
8408 *non_constant_p = parser->non_integral_constant_expression_p;
8409 parser->non_integral_constant_expression_p
8410 = saved_non_integral_constant_expression_p;
8412 return expression;
8415 /* Parse __builtin_offsetof.
8417 offsetof-expression:
8418 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8420 offsetof-member-designator:
8421 id-expression
8422 | offsetof-member-designator "." id-expression
8423 | offsetof-member-designator "[" expression "]"
8424 | offsetof-member-designator "->" id-expression */
8426 static tree
8427 cp_parser_builtin_offsetof (cp_parser *parser)
8429 int save_ice_p, save_non_ice_p;
8430 tree type, expr;
8431 cp_id_kind dummy;
8432 cp_token *token;
8434 /* We're about to accept non-integral-constant things, but will
8435 definitely yield an integral constant expression. Save and
8436 restore these values around our local parsing. */
8437 save_ice_p = parser->integral_constant_expression_p;
8438 save_non_ice_p = parser->non_integral_constant_expression_p;
8440 /* Consume the "__builtin_offsetof" token. */
8441 cp_lexer_consume_token (parser->lexer);
8442 /* Consume the opening `('. */
8443 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8444 /* Parse the type-id. */
8445 type = cp_parser_type_id (parser);
8446 /* Look for the `,'. */
8447 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8448 token = cp_lexer_peek_token (parser->lexer);
8450 /* Build the (type *)null that begins the traditional offsetof macro. */
8451 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8452 tf_warning_or_error);
8454 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8455 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8456 true, &dummy, token->location);
8457 while (true)
8459 token = cp_lexer_peek_token (parser->lexer);
8460 switch (token->type)
8462 case CPP_OPEN_SQUARE:
8463 /* offsetof-member-designator "[" expression "]" */
8464 expr = cp_parser_postfix_open_square_expression (parser, expr,
8465 true, false);
8466 break;
8468 case CPP_DEREF:
8469 /* offsetof-member-designator "->" identifier */
8470 expr = grok_array_decl (token->location, expr,
8471 integer_zero_node, false);
8472 /* FALLTHRU */
8474 case CPP_DOT:
8475 /* offsetof-member-designator "." identifier */
8476 cp_lexer_consume_token (parser->lexer);
8477 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8478 expr, true, &dummy,
8479 token->location);
8480 break;
8482 case CPP_CLOSE_PAREN:
8483 /* Consume the ")" token. */
8484 cp_lexer_consume_token (parser->lexer);
8485 goto success;
8487 default:
8488 /* Error. We know the following require will fail, but
8489 that gives the proper error message. */
8490 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8491 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8492 expr = error_mark_node;
8493 goto failure;
8497 success:
8498 /* If we're processing a template, we can't finish the semantics yet.
8499 Otherwise we can fold the entire expression now. */
8500 if (processing_template_decl)
8501 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8502 else
8503 expr = finish_offsetof (expr);
8505 failure:
8506 parser->integral_constant_expression_p = save_ice_p;
8507 parser->non_integral_constant_expression_p = save_non_ice_p;
8509 return expr;
8512 /* Parse a trait expression.
8514 Returns a representation of the expression, the underlying type
8515 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8517 static tree
8518 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8520 cp_trait_kind kind;
8521 tree type1, type2 = NULL_TREE;
8522 bool binary = false;
8523 cp_decl_specifier_seq decl_specs;
8525 switch (keyword)
8527 case RID_HAS_NOTHROW_ASSIGN:
8528 kind = CPTK_HAS_NOTHROW_ASSIGN;
8529 break;
8530 case RID_HAS_NOTHROW_CONSTRUCTOR:
8531 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8532 break;
8533 case RID_HAS_NOTHROW_COPY:
8534 kind = CPTK_HAS_NOTHROW_COPY;
8535 break;
8536 case RID_HAS_TRIVIAL_ASSIGN:
8537 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8538 break;
8539 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8540 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8541 break;
8542 case RID_HAS_TRIVIAL_COPY:
8543 kind = CPTK_HAS_TRIVIAL_COPY;
8544 break;
8545 case RID_HAS_TRIVIAL_DESTRUCTOR:
8546 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8547 break;
8548 case RID_HAS_VIRTUAL_DESTRUCTOR:
8549 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8550 break;
8551 case RID_IS_ABSTRACT:
8552 kind = CPTK_IS_ABSTRACT;
8553 break;
8554 case RID_IS_BASE_OF:
8555 kind = CPTK_IS_BASE_OF;
8556 binary = true;
8557 break;
8558 case RID_IS_CLASS:
8559 kind = CPTK_IS_CLASS;
8560 break;
8561 case RID_IS_CONVERTIBLE_TO:
8562 kind = CPTK_IS_CONVERTIBLE_TO;
8563 binary = true;
8564 break;
8565 case RID_IS_EMPTY:
8566 kind = CPTK_IS_EMPTY;
8567 break;
8568 case RID_IS_ENUM:
8569 kind = CPTK_IS_ENUM;
8570 break;
8571 case RID_IS_FINAL:
8572 kind = CPTK_IS_FINAL;
8573 break;
8574 case RID_IS_LITERAL_TYPE:
8575 kind = CPTK_IS_LITERAL_TYPE;
8576 break;
8577 case RID_IS_POD:
8578 kind = CPTK_IS_POD;
8579 break;
8580 case RID_IS_POLYMORPHIC:
8581 kind = CPTK_IS_POLYMORPHIC;
8582 break;
8583 case RID_IS_STD_LAYOUT:
8584 kind = CPTK_IS_STD_LAYOUT;
8585 break;
8586 case RID_IS_TRIVIAL:
8587 kind = CPTK_IS_TRIVIAL;
8588 break;
8589 case RID_IS_UNION:
8590 kind = CPTK_IS_UNION;
8591 break;
8592 case RID_UNDERLYING_TYPE:
8593 kind = CPTK_UNDERLYING_TYPE;
8594 break;
8595 case RID_BASES:
8596 kind = CPTK_BASES;
8597 break;
8598 case RID_DIRECT_BASES:
8599 kind = CPTK_DIRECT_BASES;
8600 break;
8601 default:
8602 gcc_unreachable ();
8605 /* Consume the token. */
8606 cp_lexer_consume_token (parser->lexer);
8608 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8610 type1 = cp_parser_type_id (parser);
8612 if (type1 == error_mark_node)
8613 return error_mark_node;
8615 /* Build a trivial decl-specifier-seq. */
8616 clear_decl_specs (&decl_specs);
8617 decl_specs.type = type1;
8619 /* Call grokdeclarator to figure out what type this is. */
8620 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8621 /*initialized=*/0, /*attrlist=*/NULL);
8623 if (binary)
8625 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8627 type2 = cp_parser_type_id (parser);
8629 if (type2 == error_mark_node)
8630 return error_mark_node;
8632 /* Build a trivial decl-specifier-seq. */
8633 clear_decl_specs (&decl_specs);
8634 decl_specs.type = type2;
8636 /* Call grokdeclarator to figure out what type this is. */
8637 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8638 /*initialized=*/0, /*attrlist=*/NULL);
8641 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8643 /* Complete the trait expression, which may mean either processing
8644 the trait expr now or saving it for template instantiation. */
8645 switch(kind)
8647 case CPTK_UNDERLYING_TYPE:
8648 return finish_underlying_type (type1);
8649 case CPTK_BASES:
8650 return finish_bases (type1, false);
8651 case CPTK_DIRECT_BASES:
8652 return finish_bases (type1, true);
8653 default:
8654 return finish_trait_expr (kind, type1, type2);
8658 /* Lambdas that appear in variable initializer or default argument scope
8659 get that in their mangling, so we need to record it. We might as well
8660 use the count for function and namespace scopes as well. */
8661 static GTY(()) tree lambda_scope;
8662 static GTY(()) int lambda_count;
8663 typedef struct GTY(()) tree_int
8665 tree t;
8666 int i;
8667 } tree_int;
8668 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8670 static void
8671 start_lambda_scope (tree decl)
8673 tree_int ti;
8674 gcc_assert (decl);
8675 /* Once we're inside a function, we ignore other scopes and just push
8676 the function again so that popping works properly. */
8677 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8678 decl = current_function_decl;
8679 ti.t = lambda_scope;
8680 ti.i = lambda_count;
8681 vec_safe_push (lambda_scope_stack, ti);
8682 if (lambda_scope != decl)
8684 /* Don't reset the count if we're still in the same function. */
8685 lambda_scope = decl;
8686 lambda_count = 0;
8690 static void
8691 record_lambda_scope (tree lambda)
8693 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8694 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8697 static void
8698 finish_lambda_scope (void)
8700 tree_int *p = &lambda_scope_stack->last ();
8701 if (lambda_scope != p->t)
8703 lambda_scope = p->t;
8704 lambda_count = p->i;
8706 lambda_scope_stack->pop ();
8709 /* Parse a lambda expression.
8711 lambda-expression:
8712 lambda-introducer lambda-declarator [opt] compound-statement
8714 Returns a representation of the expression. */
8716 static tree
8717 cp_parser_lambda_expression (cp_parser* parser)
8719 tree lambda_expr = build_lambda_expr ();
8720 tree type;
8721 bool ok = true;
8723 LAMBDA_EXPR_LOCATION (lambda_expr)
8724 = cp_lexer_peek_token (parser->lexer)->location;
8726 if (cp_unevaluated_operand)
8728 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8729 "lambda-expression in unevaluated context");
8730 ok = false;
8733 /* We may be in the middle of deferred access check. Disable
8734 it now. */
8735 push_deferring_access_checks (dk_no_deferred);
8737 cp_parser_lambda_introducer (parser, lambda_expr);
8739 type = begin_lambda_type (lambda_expr);
8740 if (type == error_mark_node)
8741 return error_mark_node;
8743 record_lambda_scope (lambda_expr);
8745 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8746 determine_visibility (TYPE_NAME (type));
8748 /* Now that we've started the type, add the capture fields for any
8749 explicit captures. */
8750 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8753 /* Inside the class, surrounding template-parameter-lists do not apply. */
8754 unsigned int saved_num_template_parameter_lists
8755 = parser->num_template_parameter_lists;
8756 unsigned char in_statement = parser->in_statement;
8757 bool in_switch_statement_p = parser->in_switch_statement_p;
8758 bool fully_implicit_function_template_p
8759 = parser->fully_implicit_function_template_p;
8760 tree implicit_template_parms = parser->implicit_template_parms;
8761 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8762 bool auto_is_implicit_function_template_parm_p
8763 = parser->auto_is_implicit_function_template_parm_p;
8765 parser->num_template_parameter_lists = 0;
8766 parser->in_statement = 0;
8767 parser->in_switch_statement_p = false;
8768 parser->fully_implicit_function_template_p = false;
8769 parser->implicit_template_parms = 0;
8770 parser->implicit_template_scope = 0;
8771 parser->auto_is_implicit_function_template_parm_p = false;
8773 /* By virtue of defining a local class, a lambda expression has access to
8774 the private variables of enclosing classes. */
8776 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8778 if (ok)
8779 cp_parser_lambda_body (parser, lambda_expr);
8780 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8782 if (cp_parser_skip_to_closing_brace (parser))
8783 cp_lexer_consume_token (parser->lexer);
8786 /* The capture list was built up in reverse order; fix that now. */
8787 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8788 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8790 if (ok)
8791 maybe_add_lambda_conv_op (type);
8793 type = finish_struct (type, /*attributes=*/NULL_TREE);
8795 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8796 parser->in_statement = in_statement;
8797 parser->in_switch_statement_p = in_switch_statement_p;
8798 parser->fully_implicit_function_template_p
8799 = fully_implicit_function_template_p;
8800 parser->implicit_template_parms = implicit_template_parms;
8801 parser->implicit_template_scope = implicit_template_scope;
8802 parser->auto_is_implicit_function_template_parm_p
8803 = auto_is_implicit_function_template_parm_p;
8806 pop_deferring_access_checks ();
8808 /* This field is only used during parsing of the lambda. */
8809 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8811 /* This lambda shouldn't have any proxies left at this point. */
8812 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8813 /* And now that we're done, push proxies for an enclosing lambda. */
8814 insert_pending_capture_proxies ();
8816 if (ok)
8817 return build_lambda_object (lambda_expr);
8818 else
8819 return error_mark_node;
8822 /* Parse the beginning of a lambda expression.
8824 lambda-introducer:
8825 [ lambda-capture [opt] ]
8827 LAMBDA_EXPR is the current representation of the lambda expression. */
8829 static void
8830 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8832 /* Need commas after the first capture. */
8833 bool first = true;
8835 /* Eat the leading `['. */
8836 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8838 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8839 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8840 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8841 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8842 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8843 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8845 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8847 cp_lexer_consume_token (parser->lexer);
8848 first = false;
8851 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8853 cp_token* capture_token;
8854 tree capture_id;
8855 tree capture_init_expr;
8856 cp_id_kind idk = CP_ID_KIND_NONE;
8857 bool explicit_init_p = false;
8859 enum capture_kind_type
8861 BY_COPY,
8862 BY_REFERENCE
8864 enum capture_kind_type capture_kind = BY_COPY;
8866 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8868 error ("expected end of capture-list");
8869 return;
8872 if (first)
8873 first = false;
8874 else
8875 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8877 /* Possibly capture `this'. */
8878 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8880 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8881 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8882 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8883 "with by-copy capture default");
8884 cp_lexer_consume_token (parser->lexer);
8885 add_capture (lambda_expr,
8886 /*id=*/this_identifier,
8887 /*initializer=*/finish_this_expr(),
8888 /*by_reference_p=*/false,
8889 explicit_init_p);
8890 continue;
8893 /* Remember whether we want to capture as a reference or not. */
8894 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8896 capture_kind = BY_REFERENCE;
8897 cp_lexer_consume_token (parser->lexer);
8900 /* Get the identifier. */
8901 capture_token = cp_lexer_peek_token (parser->lexer);
8902 capture_id = cp_parser_identifier (parser);
8904 if (capture_id == error_mark_node)
8905 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8906 delimiters, but I modified this to stop on unnested ']' as well. It
8907 was already changed to stop on unnested '}', so the
8908 "closing_parenthesis" name is no more misleading with my change. */
8910 cp_parser_skip_to_closing_parenthesis (parser,
8911 /*recovering=*/true,
8912 /*or_comma=*/true,
8913 /*consume_paren=*/true);
8914 break;
8917 /* Find the initializer for this capture. */
8918 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8919 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8920 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8922 bool direct, non_constant;
8923 /* An explicit initializer exists. */
8924 if (cxx_dialect < cxx1y)
8925 pedwarn (input_location, 0,
8926 "lambda capture initializers "
8927 "only available with -std=c++1y or -std=gnu++1y");
8928 capture_init_expr = cp_parser_initializer (parser, &direct,
8929 &non_constant);
8930 explicit_init_p = true;
8931 if (capture_init_expr == NULL_TREE)
8933 error ("empty initializer for lambda init-capture");
8934 capture_init_expr = error_mark_node;
8937 else
8939 const char* error_msg;
8941 /* Turn the identifier into an id-expression. */
8942 capture_init_expr
8943 = cp_parser_lookup_name_simple (parser, capture_id,
8944 capture_token->location);
8946 if (capture_init_expr == error_mark_node)
8948 unqualified_name_lookup_error (capture_id);
8949 continue;
8951 else if (DECL_P (capture_init_expr)
8952 && (!VAR_P (capture_init_expr)
8953 && TREE_CODE (capture_init_expr) != PARM_DECL))
8955 error_at (capture_token->location,
8956 "capture of non-variable %qD ",
8957 capture_init_expr);
8958 inform (0, "%q+#D declared here", capture_init_expr);
8959 continue;
8961 if (VAR_P (capture_init_expr)
8962 && decl_storage_duration (capture_init_expr) != dk_auto)
8964 pedwarn (capture_token->location, 0, "capture of variable "
8965 "%qD with non-automatic storage duration",
8966 capture_init_expr);
8967 inform (0, "%q+#D declared here", capture_init_expr);
8968 continue;
8971 capture_init_expr
8972 = finish_id_expression
8973 (capture_id,
8974 capture_init_expr,
8975 parser->scope,
8976 &idk,
8977 /*integral_constant_expression_p=*/false,
8978 /*allow_non_integral_constant_expression_p=*/false,
8979 /*non_integral_constant_expression_p=*/NULL,
8980 /*template_p=*/false,
8981 /*done=*/true,
8982 /*address_p=*/false,
8983 /*template_arg_p=*/false,
8984 &error_msg,
8985 capture_token->location);
8987 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8989 cp_lexer_consume_token (parser->lexer);
8990 capture_init_expr = make_pack_expansion (capture_init_expr);
8992 else
8993 check_for_bare_parameter_packs (capture_init_expr);
8996 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8997 && !explicit_init_p)
8999 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9000 && capture_kind == BY_COPY)
9001 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9002 "of %qD redundant with by-copy capture default",
9003 capture_id);
9004 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9005 && capture_kind == BY_REFERENCE)
9006 pedwarn (capture_token->location, 0, "explicit by-reference "
9007 "capture of %qD redundant with by-reference capture "
9008 "default", capture_id);
9011 add_capture (lambda_expr,
9012 capture_id,
9013 capture_init_expr,
9014 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9015 explicit_init_p);
9018 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9021 /* Parse the (optional) middle of a lambda expression.
9023 lambda-declarator:
9024 < template-parameter-list [opt] >
9025 ( parameter-declaration-clause [opt] )
9026 attribute-specifier [opt]
9027 mutable [opt]
9028 exception-specification [opt]
9029 lambda-return-type-clause [opt]
9031 LAMBDA_EXPR is the current representation of the lambda expression. */
9033 static bool
9034 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9036 /* 5.1.1.4 of the standard says:
9037 If a lambda-expression does not include a lambda-declarator, it is as if
9038 the lambda-declarator were ().
9039 This means an empty parameter list, no attributes, and no exception
9040 specification. */
9041 tree param_list = void_list_node;
9042 tree attributes = NULL_TREE;
9043 tree exception_spec = NULL_TREE;
9044 tree template_param_list = NULL_TREE;
9046 /* The template-parameter-list is optional, but must begin with
9047 an opening angle if present. */
9048 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9050 if (cxx_dialect < cxx1y)
9051 pedwarn (parser->lexer->next_token->location, 0,
9052 "lambda templates are only available with "
9053 "-std=c++1y or -std=gnu++1y");
9055 cp_lexer_consume_token (parser->lexer);
9057 template_param_list = cp_parser_template_parameter_list (parser);
9059 cp_parser_skip_to_end_of_template_parameter_list (parser);
9061 /* We just processed one more parameter list. */
9062 ++parser->num_template_parameter_lists;
9065 /* The parameter-declaration-clause is optional (unless
9066 template-parameter-list was given), but must begin with an
9067 opening parenthesis if present. */
9068 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9070 cp_lexer_consume_token (parser->lexer);
9072 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9074 /* Parse parameters. */
9075 param_list = cp_parser_parameter_declaration_clause (parser);
9077 /* Default arguments shall not be specified in the
9078 parameter-declaration-clause of a lambda-declarator. */
9079 for (tree t = param_list; t; t = TREE_CHAIN (t))
9080 if (TREE_PURPOSE (t))
9081 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9082 "default argument specified for lambda parameter");
9084 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9086 attributes = cp_parser_attributes_opt (parser);
9088 /* Parse optional `mutable' keyword. */
9089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9091 cp_lexer_consume_token (parser->lexer);
9092 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9095 /* Parse optional exception specification. */
9096 exception_spec = cp_parser_exception_specification_opt (parser);
9098 /* Parse optional trailing return type. */
9099 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9101 cp_lexer_consume_token (parser->lexer);
9102 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9103 = cp_parser_trailing_type_id (parser);
9106 /* The function parameters must be in scope all the way until after the
9107 trailing-return-type in case of decltype. */
9108 pop_bindings_and_leave_scope ();
9110 else if (template_param_list != NULL_TREE) // generate diagnostic
9111 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9113 /* Create the function call operator.
9115 Messing with declarators like this is no uglier than building up the
9116 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9117 other code. */
9119 cp_decl_specifier_seq return_type_specs;
9120 cp_declarator* declarator;
9121 tree fco;
9122 int quals;
9123 void *p;
9125 clear_decl_specs (&return_type_specs);
9126 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9127 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9128 else
9129 /* Maybe we will deduce the return type later. */
9130 return_type_specs.type = make_auto ();
9132 p = obstack_alloc (&declarator_obstack, 0);
9134 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9135 sfk_none);
9137 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9138 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9139 declarator = make_call_declarator (declarator, param_list, quals,
9140 VIRT_SPEC_UNSPECIFIED,
9141 REF_QUAL_NONE,
9142 exception_spec,
9143 /*late_return_type=*/NULL_TREE);
9144 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9146 fco = grokmethod (&return_type_specs,
9147 declarator,
9148 attributes);
9149 if (fco != error_mark_node)
9151 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9152 DECL_ARTIFICIAL (fco) = 1;
9153 /* Give the object parameter a different name. */
9154 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9156 if (template_param_list)
9158 fco = finish_member_template_decl (fco);
9159 finish_template_decl (template_param_list);
9160 --parser->num_template_parameter_lists;
9162 else if (parser->fully_implicit_function_template_p)
9163 fco = finish_fully_implicit_template (parser, fco);
9165 finish_member_declaration (fco);
9167 obstack_free (&declarator_obstack, p);
9169 return (fco != error_mark_node);
9173 /* Parse the body of a lambda expression, which is simply
9175 compound-statement
9177 but which requires special handling.
9178 LAMBDA_EXPR is the current representation of the lambda expression. */
9180 static void
9181 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9183 bool nested = (current_function_decl != NULL_TREE);
9184 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9185 if (nested)
9186 push_function_context ();
9187 else
9188 /* Still increment function_depth so that we don't GC in the
9189 middle of an expression. */
9190 ++function_depth;
9191 /* Clear this in case we're in the middle of a default argument. */
9192 parser->local_variables_forbidden_p = false;
9194 /* Finish the function call operator
9195 - class_specifier
9196 + late_parsing_for_member
9197 + function_definition_after_declarator
9198 + ctor_initializer_opt_and_function_body */
9200 tree fco = lambda_function (lambda_expr);
9201 tree body;
9202 bool done = false;
9203 tree compound_stmt;
9204 tree cap;
9206 /* Let the front end know that we are going to be defining this
9207 function. */
9208 start_preparsed_function (fco,
9209 NULL_TREE,
9210 SF_PRE_PARSED | SF_INCLASS_INLINE);
9212 start_lambda_scope (fco);
9213 body = begin_function_body ();
9215 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9216 goto out;
9218 /* Push the proxies for any explicit captures. */
9219 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9220 cap = TREE_CHAIN (cap))
9221 build_capture_proxy (TREE_PURPOSE (cap));
9223 compound_stmt = begin_compound_stmt (0);
9225 /* 5.1.1.4 of the standard says:
9226 If a lambda-expression does not include a trailing-return-type, it
9227 is as if the trailing-return-type denotes the following type:
9228 * if the compound-statement is of the form
9229 { return attribute-specifier [opt] expression ; }
9230 the type of the returned expression after lvalue-to-rvalue
9231 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9232 (_conv.array_ 4.2), and function-to-pointer conversion
9233 (_conv.func_ 4.3);
9234 * otherwise, void. */
9236 /* In a lambda that has neither a lambda-return-type-clause
9237 nor a deducible form, errors should be reported for return statements
9238 in the body. Since we used void as the placeholder return type, parsing
9239 the body as usual will give such desired behavior. */
9240 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9241 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9242 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9244 tree expr = NULL_TREE;
9245 cp_id_kind idk = CP_ID_KIND_NONE;
9247 /* Parse tentatively in case there's more after the initial return
9248 statement. */
9249 cp_parser_parse_tentatively (parser);
9251 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9253 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9255 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9256 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9258 if (cp_parser_parse_definitely (parser))
9260 if (!processing_template_decl)
9261 apply_deduced_return_type (fco, lambda_return_type (expr));
9263 /* Will get error here if type not deduced yet. */
9264 finish_return_stmt (expr);
9266 done = true;
9270 if (!done)
9272 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9273 cp_parser_label_declaration (parser);
9274 cp_parser_statement_seq_opt (parser, NULL_TREE);
9275 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9278 finish_compound_stmt (compound_stmt);
9280 out:
9281 finish_function_body (body);
9282 finish_lambda_scope ();
9284 /* Finish the function and generate code for it if necessary. */
9285 tree fn = finish_function (/*inline*/2);
9287 /* Only expand if the call op is not a template. */
9288 if (!DECL_TEMPLATE_INFO (fco))
9289 expand_or_defer_fn (fn);
9292 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9293 if (nested)
9294 pop_function_context();
9295 else
9296 --function_depth;
9299 /* Statements [gram.stmt.stmt] */
9301 /* Parse a statement.
9303 statement:
9304 labeled-statement
9305 expression-statement
9306 compound-statement
9307 selection-statement
9308 iteration-statement
9309 jump-statement
9310 declaration-statement
9311 try-block
9313 C++11:
9315 statement:
9316 labeled-statement
9317 attribute-specifier-seq (opt) expression-statement
9318 attribute-specifier-seq (opt) compound-statement
9319 attribute-specifier-seq (opt) selection-statement
9320 attribute-specifier-seq (opt) iteration-statement
9321 attribute-specifier-seq (opt) jump-statement
9322 declaration-statement
9323 attribute-specifier-seq (opt) try-block
9325 TM Extension:
9327 statement:
9328 atomic-statement
9330 IN_COMPOUND is true when the statement is nested inside a
9331 cp_parser_compound_statement; this matters for certain pragmas.
9333 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9334 is a (possibly labeled) if statement which is not enclosed in braces
9335 and has an else clause. This is used to implement -Wparentheses. */
9337 static void
9338 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9339 bool in_compound, bool *if_p)
9341 tree statement, std_attrs = NULL_TREE;
9342 cp_token *token;
9343 location_t statement_location, attrs_location;
9345 restart:
9346 if (if_p != NULL)
9347 *if_p = false;
9348 /* There is no statement yet. */
9349 statement = NULL_TREE;
9351 cp_lexer_save_tokens (parser->lexer);
9352 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9353 if (c_dialect_objc ())
9354 /* In obj-c++, seeing '[[' might be the either the beginning of
9355 c++11 attributes, or a nested objc-message-expression. So
9356 let's parse the c++11 attributes tentatively. */
9357 cp_parser_parse_tentatively (parser);
9358 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9359 if (c_dialect_objc ())
9361 if (!cp_parser_parse_definitely (parser))
9362 std_attrs = NULL_TREE;
9365 /* Peek at the next token. */
9366 token = cp_lexer_peek_token (parser->lexer);
9367 /* Remember the location of the first token in the statement. */
9368 statement_location = token->location;
9369 /* If this is a keyword, then that will often determine what kind of
9370 statement we have. */
9371 if (token->type == CPP_KEYWORD)
9373 enum rid keyword = token->keyword;
9375 switch (keyword)
9377 case RID_CASE:
9378 case RID_DEFAULT:
9379 /* Looks like a labeled-statement with a case label.
9380 Parse the label, and then use tail recursion to parse
9381 the statement. */
9382 cp_parser_label_for_labeled_statement (parser, std_attrs);
9383 goto restart;
9385 case RID_IF:
9386 case RID_SWITCH:
9387 statement = cp_parser_selection_statement (parser, if_p);
9388 break;
9390 case RID_WHILE:
9391 case RID_DO:
9392 case RID_FOR:
9393 statement = cp_parser_iteration_statement (parser, false);
9394 break;
9396 case RID_BREAK:
9397 case RID_CONTINUE:
9398 case RID_RETURN:
9399 case RID_GOTO:
9400 statement = cp_parser_jump_statement (parser);
9401 break;
9403 /* Objective-C++ exception-handling constructs. */
9404 case RID_AT_TRY:
9405 case RID_AT_CATCH:
9406 case RID_AT_FINALLY:
9407 case RID_AT_SYNCHRONIZED:
9408 case RID_AT_THROW:
9409 statement = cp_parser_objc_statement (parser);
9410 break;
9412 case RID_TRY:
9413 statement = cp_parser_try_block (parser);
9414 break;
9416 case RID_NAMESPACE:
9417 /* This must be a namespace alias definition. */
9418 cp_parser_declaration_statement (parser);
9419 return;
9421 case RID_TRANSACTION_ATOMIC:
9422 case RID_TRANSACTION_RELAXED:
9423 statement = cp_parser_transaction (parser, keyword);
9424 break;
9425 case RID_TRANSACTION_CANCEL:
9426 statement = cp_parser_transaction_cancel (parser);
9427 break;
9429 default:
9430 /* It might be a keyword like `int' that can start a
9431 declaration-statement. */
9432 break;
9435 else if (token->type == CPP_NAME)
9437 /* If the next token is a `:', then we are looking at a
9438 labeled-statement. */
9439 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9440 if (token->type == CPP_COLON)
9442 /* Looks like a labeled-statement with an ordinary label.
9443 Parse the label, and then use tail recursion to parse
9444 the statement. */
9446 cp_parser_label_for_labeled_statement (parser, std_attrs);
9447 goto restart;
9450 /* Anything that starts with a `{' must be a compound-statement. */
9451 else if (token->type == CPP_OPEN_BRACE)
9452 statement = cp_parser_compound_statement (parser, NULL, false, false);
9453 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9454 a statement all its own. */
9455 else if (token->type == CPP_PRAGMA)
9457 /* Only certain OpenMP pragmas are attached to statements, and thus
9458 are considered statements themselves. All others are not. In
9459 the context of a compound, accept the pragma as a "statement" and
9460 return so that we can check for a close brace. Otherwise we
9461 require a real statement and must go back and read one. */
9462 if (in_compound)
9463 cp_parser_pragma (parser, pragma_compound);
9464 else if (!cp_parser_pragma (parser, pragma_stmt))
9465 goto restart;
9466 return;
9468 else if (token->type == CPP_EOF)
9470 cp_parser_error (parser, "expected statement");
9471 return;
9474 /* Everything else must be a declaration-statement or an
9475 expression-statement. Try for the declaration-statement
9476 first, unless we are looking at a `;', in which case we know that
9477 we have an expression-statement. */
9478 if (!statement)
9480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9482 if (std_attrs != NULL_TREE)
9484 /* Attributes should be parsed as part of the the
9485 declaration, so let's un-parse them. */
9486 cp_lexer_rollback_tokens (parser->lexer);
9487 std_attrs = NULL_TREE;
9490 cp_parser_parse_tentatively (parser);
9491 /* Try to parse the declaration-statement. */
9492 cp_parser_declaration_statement (parser);
9493 /* If that worked, we're done. */
9494 if (cp_parser_parse_definitely (parser))
9495 return;
9497 /* Look for an expression-statement instead. */
9498 statement = cp_parser_expression_statement (parser, in_statement_expr);
9501 /* Set the line number for the statement. */
9502 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9503 SET_EXPR_LOCATION (statement, statement_location);
9505 /* Note that for now, we don't do anything with c++11 statements
9506 parsed at this level. */
9507 if (std_attrs != NULL_TREE)
9508 warning_at (attrs_location,
9509 OPT_Wattributes,
9510 "attributes at the beginning of statement are ignored");
9513 /* Parse the label for a labeled-statement, i.e.
9515 identifier :
9516 case constant-expression :
9517 default :
9519 GNU Extension:
9520 case constant-expression ... constant-expression : statement
9522 When a label is parsed without errors, the label is added to the
9523 parse tree by the finish_* functions, so this function doesn't
9524 have to return the label. */
9526 static void
9527 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9529 cp_token *token;
9530 tree label = NULL_TREE;
9531 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9533 /* The next token should be an identifier. */
9534 token = cp_lexer_peek_token (parser->lexer);
9535 if (token->type != CPP_NAME
9536 && token->type != CPP_KEYWORD)
9538 cp_parser_error (parser, "expected labeled-statement");
9539 return;
9542 parser->colon_corrects_to_scope_p = false;
9543 switch (token->keyword)
9545 case RID_CASE:
9547 tree expr, expr_hi;
9548 cp_token *ellipsis;
9550 /* Consume the `case' token. */
9551 cp_lexer_consume_token (parser->lexer);
9552 /* Parse the constant-expression. */
9553 expr = cp_parser_constant_expression (parser,
9554 /*allow_non_constant_p=*/false,
9555 NULL);
9557 ellipsis = cp_lexer_peek_token (parser->lexer);
9558 if (ellipsis->type == CPP_ELLIPSIS)
9560 /* Consume the `...' token. */
9561 cp_lexer_consume_token (parser->lexer);
9562 expr_hi =
9563 cp_parser_constant_expression (parser,
9564 /*allow_non_constant_p=*/false,
9565 NULL);
9566 /* We don't need to emit warnings here, as the common code
9567 will do this for us. */
9569 else
9570 expr_hi = NULL_TREE;
9572 if (parser->in_switch_statement_p)
9573 finish_case_label (token->location, expr, expr_hi);
9574 else
9575 error_at (token->location,
9576 "case label %qE not within a switch statement",
9577 expr);
9579 break;
9581 case RID_DEFAULT:
9582 /* Consume the `default' token. */
9583 cp_lexer_consume_token (parser->lexer);
9585 if (parser->in_switch_statement_p)
9586 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9587 else
9588 error_at (token->location, "case label not within a switch statement");
9589 break;
9591 default:
9592 /* Anything else must be an ordinary label. */
9593 label = finish_label_stmt (cp_parser_identifier (parser));
9594 break;
9597 /* Require the `:' token. */
9598 cp_parser_require (parser, CPP_COLON, RT_COLON);
9600 /* An ordinary label may optionally be followed by attributes.
9601 However, this is only permitted if the attributes are then
9602 followed by a semicolon. This is because, for backward
9603 compatibility, when parsing
9604 lab: __attribute__ ((unused)) int i;
9605 we want the attribute to attach to "i", not "lab". */
9606 if (label != NULL_TREE
9607 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9609 tree attrs;
9610 cp_parser_parse_tentatively (parser);
9611 attrs = cp_parser_gnu_attributes_opt (parser);
9612 if (attrs == NULL_TREE
9613 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9614 cp_parser_abort_tentative_parse (parser);
9615 else if (!cp_parser_parse_definitely (parser))
9617 else
9618 attributes = chainon (attributes, attrs);
9621 if (attributes != NULL_TREE)
9622 cplus_decl_attributes (&label, attributes, 0);
9624 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9627 /* Parse an expression-statement.
9629 expression-statement:
9630 expression [opt] ;
9632 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9633 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9634 indicates whether this expression-statement is part of an
9635 expression statement. */
9637 static tree
9638 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9640 tree statement = NULL_TREE;
9641 cp_token *token = cp_lexer_peek_token (parser->lexer);
9643 /* If the next token is a ';', then there is no expression
9644 statement. */
9645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9647 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9648 if (statement == error_mark_node
9649 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9651 cp_parser_skip_to_end_of_block_or_statement (parser);
9652 return error_mark_node;
9656 /* Give a helpful message for "A<T>::type t;" and the like. */
9657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9658 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9660 if (TREE_CODE (statement) == SCOPE_REF)
9661 error_at (token->location, "need %<typename%> before %qE because "
9662 "%qT is a dependent scope",
9663 statement, TREE_OPERAND (statement, 0));
9664 else if (is_overloaded_fn (statement)
9665 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9667 /* A::A a; */
9668 tree fn = get_first_fn (statement);
9669 error_at (token->location,
9670 "%<%T::%D%> names the constructor, not the type",
9671 DECL_CONTEXT (fn), DECL_NAME (fn));
9675 /* Consume the final `;'. */
9676 cp_parser_consume_semicolon_at_end_of_statement (parser);
9678 if (in_statement_expr
9679 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9680 /* This is the final expression statement of a statement
9681 expression. */
9682 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9683 else if (statement)
9684 statement = finish_expr_stmt (statement);
9686 return statement;
9689 /* Parse a compound-statement.
9691 compound-statement:
9692 { statement-seq [opt] }
9694 GNU extension:
9696 compound-statement:
9697 { label-declaration-seq [opt] statement-seq [opt] }
9699 label-declaration-seq:
9700 label-declaration
9701 label-declaration-seq label-declaration
9703 Returns a tree representing the statement. */
9705 static tree
9706 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9707 bool in_try, bool function_body)
9709 tree compound_stmt;
9711 /* Consume the `{'. */
9712 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9713 return error_mark_node;
9714 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9715 && !function_body)
9716 pedwarn (input_location, OPT_Wpedantic,
9717 "compound-statement in constexpr function");
9718 /* Begin the compound-statement. */
9719 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9720 /* If the next keyword is `__label__' we have a label declaration. */
9721 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9722 cp_parser_label_declaration (parser);
9723 /* Parse an (optional) statement-seq. */
9724 cp_parser_statement_seq_opt (parser, in_statement_expr);
9725 /* Finish the compound-statement. */
9726 finish_compound_stmt (compound_stmt);
9727 /* Consume the `}'. */
9728 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9730 return compound_stmt;
9733 /* Parse an (optional) statement-seq.
9735 statement-seq:
9736 statement
9737 statement-seq [opt] statement */
9739 static void
9740 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9742 /* Scan statements until there aren't any more. */
9743 while (true)
9745 cp_token *token = cp_lexer_peek_token (parser->lexer);
9747 /* If we are looking at a `}', then we have run out of
9748 statements; the same is true if we have reached the end
9749 of file, or have stumbled upon a stray '@end'. */
9750 if (token->type == CPP_CLOSE_BRACE
9751 || token->type == CPP_EOF
9752 || token->type == CPP_PRAGMA_EOL
9753 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9754 break;
9756 /* If we are in a compound statement and find 'else' then
9757 something went wrong. */
9758 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9760 if (parser->in_statement & IN_IF_STMT)
9761 break;
9762 else
9764 token = cp_lexer_consume_token (parser->lexer);
9765 error_at (token->location, "%<else%> without a previous %<if%>");
9769 /* Parse the statement. */
9770 cp_parser_statement (parser, in_statement_expr, true, NULL);
9774 /* Parse a selection-statement.
9776 selection-statement:
9777 if ( condition ) statement
9778 if ( condition ) statement else statement
9779 switch ( condition ) statement
9781 Returns the new IF_STMT or SWITCH_STMT.
9783 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9784 is a (possibly labeled) if statement which is not enclosed in
9785 braces and has an else clause. This is used to implement
9786 -Wparentheses. */
9788 static tree
9789 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9791 cp_token *token;
9792 enum rid keyword;
9794 if (if_p != NULL)
9795 *if_p = false;
9797 /* Peek at the next token. */
9798 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9800 /* See what kind of keyword it is. */
9801 keyword = token->keyword;
9802 switch (keyword)
9804 case RID_IF:
9805 case RID_SWITCH:
9807 tree statement;
9808 tree condition;
9810 /* Look for the `('. */
9811 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9813 cp_parser_skip_to_end_of_statement (parser);
9814 return error_mark_node;
9817 /* Begin the selection-statement. */
9818 if (keyword == RID_IF)
9819 statement = begin_if_stmt ();
9820 else
9821 statement = begin_switch_stmt ();
9823 /* Parse the condition. */
9824 condition = cp_parser_condition (parser);
9825 /* Look for the `)'. */
9826 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9827 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9828 /*consume_paren=*/true);
9830 if (keyword == RID_IF)
9832 bool nested_if;
9833 unsigned char in_statement;
9835 /* Add the condition. */
9836 finish_if_stmt_cond (condition, statement);
9838 /* Parse the then-clause. */
9839 in_statement = parser->in_statement;
9840 parser->in_statement |= IN_IF_STMT;
9841 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9844 add_stmt (build_empty_stmt (loc));
9845 cp_lexer_consume_token (parser->lexer);
9846 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9847 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9848 "empty body in an %<if%> statement");
9849 nested_if = false;
9851 else
9852 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9853 parser->in_statement = in_statement;
9855 finish_then_clause (statement);
9857 /* If the next token is `else', parse the else-clause. */
9858 if (cp_lexer_next_token_is_keyword (parser->lexer,
9859 RID_ELSE))
9861 /* Consume the `else' keyword. */
9862 cp_lexer_consume_token (parser->lexer);
9863 begin_else_clause (statement);
9864 /* Parse the else-clause. */
9865 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9867 location_t loc;
9868 loc = cp_lexer_peek_token (parser->lexer)->location;
9869 warning_at (loc,
9870 OPT_Wempty_body, "suggest braces around "
9871 "empty body in an %<else%> statement");
9872 add_stmt (build_empty_stmt (loc));
9873 cp_lexer_consume_token (parser->lexer);
9875 else
9876 cp_parser_implicitly_scoped_statement (parser, NULL);
9878 finish_else_clause (statement);
9880 /* If we are currently parsing a then-clause, then
9881 IF_P will not be NULL. We set it to true to
9882 indicate that this if statement has an else clause.
9883 This may trigger the Wparentheses warning below
9884 when we get back up to the parent if statement. */
9885 if (if_p != NULL)
9886 *if_p = true;
9888 else
9890 /* This if statement does not have an else clause. If
9891 NESTED_IF is true, then the then-clause is an if
9892 statement which does have an else clause. We warn
9893 about the potential ambiguity. */
9894 if (nested_if)
9895 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9896 "suggest explicit braces to avoid ambiguous"
9897 " %<else%>");
9900 /* Now we're all done with the if-statement. */
9901 finish_if_stmt (statement);
9903 else
9905 bool in_switch_statement_p;
9906 unsigned char in_statement;
9908 /* Add the condition. */
9909 finish_switch_cond (condition, statement);
9911 /* Parse the body of the switch-statement. */
9912 in_switch_statement_p = parser->in_switch_statement_p;
9913 in_statement = parser->in_statement;
9914 parser->in_switch_statement_p = true;
9915 parser->in_statement |= IN_SWITCH_STMT;
9916 cp_parser_implicitly_scoped_statement (parser, NULL);
9917 parser->in_switch_statement_p = in_switch_statement_p;
9918 parser->in_statement = in_statement;
9920 /* Now we're all done with the switch-statement. */
9921 finish_switch_stmt (statement);
9924 return statement;
9926 break;
9928 default:
9929 cp_parser_error (parser, "expected selection-statement");
9930 return error_mark_node;
9934 /* Parse a condition.
9936 condition:
9937 expression
9938 type-specifier-seq declarator = initializer-clause
9939 type-specifier-seq declarator braced-init-list
9941 GNU Extension:
9943 condition:
9944 type-specifier-seq declarator asm-specification [opt]
9945 attributes [opt] = assignment-expression
9947 Returns the expression that should be tested. */
9949 static tree
9950 cp_parser_condition (cp_parser* parser)
9952 cp_decl_specifier_seq type_specifiers;
9953 const char *saved_message;
9954 int declares_class_or_enum;
9956 /* Try the declaration first. */
9957 cp_parser_parse_tentatively (parser);
9958 /* New types are not allowed in the type-specifier-seq for a
9959 condition. */
9960 saved_message = parser->type_definition_forbidden_message;
9961 parser->type_definition_forbidden_message
9962 = G_("types may not be defined in conditions");
9963 /* Parse the type-specifier-seq. */
9964 cp_parser_decl_specifier_seq (parser,
9965 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9966 &type_specifiers,
9967 &declares_class_or_enum);
9968 /* Restore the saved message. */
9969 parser->type_definition_forbidden_message = saved_message;
9970 /* If all is well, we might be looking at a declaration. */
9971 if (!cp_parser_error_occurred (parser))
9973 tree decl;
9974 tree asm_specification;
9975 tree attributes;
9976 cp_declarator *declarator;
9977 tree initializer = NULL_TREE;
9979 /* Parse the declarator. */
9980 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9981 /*ctor_dtor_or_conv_p=*/NULL,
9982 /*parenthesized_p=*/NULL,
9983 /*member_p=*/false);
9984 /* Parse the attributes. */
9985 attributes = cp_parser_attributes_opt (parser);
9986 /* Parse the asm-specification. */
9987 asm_specification = cp_parser_asm_specification_opt (parser);
9988 /* If the next token is not an `=' or '{', then we might still be
9989 looking at an expression. For example:
9991 if (A(a).x)
9993 looks like a decl-specifier-seq and a declarator -- but then
9994 there is no `=', so this is an expression. */
9995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9996 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9997 cp_parser_simulate_error (parser);
9999 /* If we did see an `=' or '{', then we are looking at a declaration
10000 for sure. */
10001 if (cp_parser_parse_definitely (parser))
10003 tree pushed_scope;
10004 bool non_constant_p;
10005 bool flags = LOOKUP_ONLYCONVERTING;
10007 /* Create the declaration. */
10008 decl = start_decl (declarator, &type_specifiers,
10009 /*initialized_p=*/true,
10010 attributes, /*prefix_attributes=*/NULL_TREE,
10011 &pushed_scope);
10013 /* Parse the initializer. */
10014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10016 initializer = cp_parser_braced_list (parser, &non_constant_p);
10017 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10018 flags = 0;
10020 else
10022 /* Consume the `='. */
10023 cp_parser_require (parser, CPP_EQ, RT_EQ);
10024 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10026 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10027 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10029 /* Process the initializer. */
10030 cp_finish_decl (decl,
10031 initializer, !non_constant_p,
10032 asm_specification,
10033 flags);
10035 if (pushed_scope)
10036 pop_scope (pushed_scope);
10038 return convert_from_reference (decl);
10041 /* If we didn't even get past the declarator successfully, we are
10042 definitely not looking at a declaration. */
10043 else
10044 cp_parser_abort_tentative_parse (parser);
10046 /* Otherwise, we are looking at an expression. */
10047 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10050 /* Parses a for-statement or range-for-statement until the closing ')',
10051 not included. */
10053 static tree
10054 cp_parser_for (cp_parser *parser, bool ivdep)
10056 tree init, scope, decl;
10057 bool is_range_for;
10059 /* Begin the for-statement. */
10060 scope = begin_for_scope (&init);
10062 /* Parse the initialization. */
10063 is_range_for = cp_parser_for_init_statement (parser, &decl);
10065 if (is_range_for)
10066 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10067 else
10068 return cp_parser_c_for (parser, scope, init, ivdep);
10071 static tree
10072 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10074 /* Normal for loop */
10075 tree condition = NULL_TREE;
10076 tree expression = NULL_TREE;
10077 tree stmt;
10079 stmt = begin_for_stmt (scope, init);
10080 /* The for-init-statement has already been parsed in
10081 cp_parser_for_init_statement, so no work is needed here. */
10082 finish_for_init_stmt (stmt);
10084 /* If there's a condition, process it. */
10085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10086 condition = cp_parser_condition (parser);
10087 else if (ivdep)
10089 cp_parser_error (parser, "missing loop condition in loop with "
10090 "%<GCC ivdep%> pragma");
10091 condition = error_mark_node;
10093 finish_for_cond (condition, stmt, ivdep);
10094 /* Look for the `;'. */
10095 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10097 /* If there's an expression, process it. */
10098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10099 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10100 finish_for_expr (expression, stmt);
10102 return stmt;
10105 /* Tries to parse a range-based for-statement:
10107 range-based-for:
10108 decl-specifier-seq declarator : expression
10110 The decl-specifier-seq declarator and the `:' are already parsed by
10111 cp_parser_for_init_statement. If processing_template_decl it returns a
10112 newly created RANGE_FOR_STMT; if not, it is converted to a
10113 regular FOR_STMT. */
10115 static tree
10116 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10117 bool ivdep)
10119 tree stmt, range_expr;
10121 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10123 bool expr_non_constant_p;
10124 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10126 else
10127 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10129 /* If in template, STMT is converted to a normal for-statement
10130 at instantiation. If not, it is done just ahead. */
10131 if (processing_template_decl)
10133 if (check_for_bare_parameter_packs (range_expr))
10134 range_expr = error_mark_node;
10135 stmt = begin_range_for_stmt (scope, init);
10136 if (ivdep)
10137 RANGE_FOR_IVDEP (stmt) = 1;
10138 finish_range_for_decl (stmt, range_decl, range_expr);
10139 if (!type_dependent_expression_p (range_expr)
10140 /* do_auto_deduction doesn't mess with template init-lists. */
10141 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10142 do_range_for_auto_deduction (range_decl, range_expr);
10144 else
10146 stmt = begin_for_stmt (scope, init);
10147 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10149 return stmt;
10152 /* Subroutine of cp_convert_range_for: given the initializer expression,
10153 builds up the range temporary. */
10155 static tree
10156 build_range_temp (tree range_expr)
10158 tree range_type, range_temp;
10160 /* Find out the type deduced by the declaration
10161 `auto &&__range = range_expr'. */
10162 range_type = cp_build_reference_type (make_auto (), true);
10163 range_type = do_auto_deduction (range_type, range_expr,
10164 type_uses_auto (range_type));
10166 /* Create the __range variable. */
10167 range_temp = build_decl (input_location, VAR_DECL,
10168 get_identifier ("__for_range"), range_type);
10169 TREE_USED (range_temp) = 1;
10170 DECL_ARTIFICIAL (range_temp) = 1;
10172 return range_temp;
10175 /* Used by cp_parser_range_for in template context: we aren't going to
10176 do a full conversion yet, but we still need to resolve auto in the
10177 type of the for-range-declaration if present. This is basically
10178 a shortcut version of cp_convert_range_for. */
10180 static void
10181 do_range_for_auto_deduction (tree decl, tree range_expr)
10183 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10184 if (auto_node)
10186 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10187 range_temp = convert_from_reference (build_range_temp (range_expr));
10188 iter_type = (cp_parser_perform_range_for_lookup
10189 (range_temp, &begin_dummy, &end_dummy));
10190 if (iter_type)
10192 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10193 iter_type);
10194 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10195 tf_warning_or_error);
10196 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10197 iter_decl, auto_node);
10202 /* Converts a range-based for-statement into a normal
10203 for-statement, as per the definition.
10205 for (RANGE_DECL : RANGE_EXPR)
10206 BLOCK
10208 should be equivalent to:
10211 auto &&__range = RANGE_EXPR;
10212 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10213 __begin != __end;
10214 ++__begin)
10216 RANGE_DECL = *__begin;
10217 BLOCK
10221 If RANGE_EXPR is an array:
10222 BEGIN_EXPR = __range
10223 END_EXPR = __range + ARRAY_SIZE(__range)
10224 Else if RANGE_EXPR has a member 'begin' or 'end':
10225 BEGIN_EXPR = __range.begin()
10226 END_EXPR = __range.end()
10227 Else:
10228 BEGIN_EXPR = begin(__range)
10229 END_EXPR = end(__range);
10231 If __range has a member 'begin' but not 'end', or vice versa, we must
10232 still use the second alternative (it will surely fail, however).
10233 When calling begin()/end() in the third alternative we must use
10234 argument dependent lookup, but always considering 'std' as an associated
10235 namespace. */
10237 tree
10238 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10239 bool ivdep)
10241 tree begin, end;
10242 tree iter_type, begin_expr, end_expr;
10243 tree condition, expression;
10245 if (range_decl == error_mark_node || range_expr == error_mark_node)
10246 /* If an error happened previously do nothing or else a lot of
10247 unhelpful errors would be issued. */
10248 begin_expr = end_expr = iter_type = error_mark_node;
10249 else
10251 tree range_temp;
10253 if (TREE_CODE (range_expr) == VAR_DECL
10254 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10255 /* Can't bind a reference to an array of runtime bound. */
10256 range_temp = range_expr;
10257 else
10259 range_temp = build_range_temp (range_expr);
10260 pushdecl (range_temp);
10261 cp_finish_decl (range_temp, range_expr,
10262 /*is_constant_init*/false, NULL_TREE,
10263 LOOKUP_ONLYCONVERTING);
10264 range_temp = convert_from_reference (range_temp);
10266 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10267 &begin_expr, &end_expr);
10270 /* The new for initialization statement. */
10271 begin = build_decl (input_location, VAR_DECL,
10272 get_identifier ("__for_begin"), iter_type);
10273 TREE_USED (begin) = 1;
10274 DECL_ARTIFICIAL (begin) = 1;
10275 pushdecl (begin);
10276 cp_finish_decl (begin, begin_expr,
10277 /*is_constant_init*/false, NULL_TREE,
10278 LOOKUP_ONLYCONVERTING);
10280 end = build_decl (input_location, VAR_DECL,
10281 get_identifier ("__for_end"), iter_type);
10282 TREE_USED (end) = 1;
10283 DECL_ARTIFICIAL (end) = 1;
10284 pushdecl (end);
10285 cp_finish_decl (end, end_expr,
10286 /*is_constant_init*/false, NULL_TREE,
10287 LOOKUP_ONLYCONVERTING);
10289 finish_for_init_stmt (statement);
10291 /* The new for condition. */
10292 condition = build_x_binary_op (input_location, NE_EXPR,
10293 begin, ERROR_MARK,
10294 end, ERROR_MARK,
10295 NULL, tf_warning_or_error);
10296 finish_for_cond (condition, statement, ivdep);
10298 /* The new increment expression. */
10299 expression = finish_unary_op_expr (input_location,
10300 PREINCREMENT_EXPR, begin,
10301 tf_warning_or_error);
10302 finish_for_expr (expression, statement);
10304 /* The declaration is initialized with *__begin inside the loop body. */
10305 cp_finish_decl (range_decl,
10306 build_x_indirect_ref (input_location, begin, RO_NULL,
10307 tf_warning_or_error),
10308 /*is_constant_init*/false, NULL_TREE,
10309 LOOKUP_ONLYCONVERTING);
10311 return statement;
10314 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10315 We need to solve both at the same time because the method used
10316 depends on the existence of members begin or end.
10317 Returns the type deduced for the iterator expression. */
10319 static tree
10320 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10322 if (error_operand_p (range))
10324 *begin = *end = error_mark_node;
10325 return error_mark_node;
10328 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10330 error ("range-based %<for%> expression of type %qT "
10331 "has incomplete type", TREE_TYPE (range));
10332 *begin = *end = error_mark_node;
10333 return error_mark_node;
10335 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10337 /* If RANGE is an array, we will use pointer arithmetic. */
10338 *begin = range;
10339 *end = build_binary_op (input_location, PLUS_EXPR,
10340 range,
10341 array_type_nelts_top (TREE_TYPE (range)),
10343 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10345 else
10347 /* If it is not an array, we must do a bit of magic. */
10348 tree id_begin, id_end;
10349 tree member_begin, member_end;
10351 *begin = *end = error_mark_node;
10353 id_begin = get_identifier ("begin");
10354 id_end = get_identifier ("end");
10355 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10356 /*protect=*/2, /*want_type=*/false,
10357 tf_warning_or_error);
10358 member_end = lookup_member (TREE_TYPE (range), id_end,
10359 /*protect=*/2, /*want_type=*/false,
10360 tf_warning_or_error);
10362 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10364 /* Use the member functions. */
10365 if (member_begin != NULL_TREE)
10366 *begin = cp_parser_range_for_member_function (range, id_begin);
10367 else
10368 error ("range-based %<for%> expression of type %qT has an "
10369 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10371 if (member_end != NULL_TREE)
10372 *end = cp_parser_range_for_member_function (range, id_end);
10373 else
10374 error ("range-based %<for%> expression of type %qT has a "
10375 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10377 else
10379 /* Use global functions with ADL. */
10380 vec<tree, va_gc> *vec;
10381 vec = make_tree_vector ();
10383 vec_safe_push (vec, range);
10385 member_begin = perform_koenig_lookup (id_begin, vec,
10386 tf_warning_or_error);
10387 *begin = finish_call_expr (member_begin, &vec, false, true,
10388 tf_warning_or_error);
10389 member_end = perform_koenig_lookup (id_end, vec,
10390 tf_warning_or_error);
10391 *end = finish_call_expr (member_end, &vec, false, true,
10392 tf_warning_or_error);
10394 release_tree_vector (vec);
10397 /* Last common checks. */
10398 if (*begin == error_mark_node || *end == error_mark_node)
10400 /* If one of the expressions is an error do no more checks. */
10401 *begin = *end = error_mark_node;
10402 return error_mark_node;
10404 else if (type_dependent_expression_p (*begin)
10405 || type_dependent_expression_p (*end))
10406 /* Can happen, when, eg, in a template context, Koenig lookup
10407 can't resolve begin/end (c++/58503). */
10408 return NULL_TREE;
10409 else
10411 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10412 /* The unqualified type of the __begin and __end temporaries should
10413 be the same, as required by the multiple auto declaration. */
10414 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10415 error ("inconsistent begin/end types in range-based %<for%> "
10416 "statement: %qT and %qT",
10417 TREE_TYPE (*begin), TREE_TYPE (*end));
10418 return iter_type;
10423 /* Helper function for cp_parser_perform_range_for_lookup.
10424 Builds a tree for RANGE.IDENTIFIER(). */
10426 static tree
10427 cp_parser_range_for_member_function (tree range, tree identifier)
10429 tree member, res;
10430 vec<tree, va_gc> *vec;
10432 member = finish_class_member_access_expr (range, identifier,
10433 false, tf_warning_or_error);
10434 if (member == error_mark_node)
10435 return error_mark_node;
10437 vec = make_tree_vector ();
10438 res = finish_call_expr (member, &vec,
10439 /*disallow_virtual=*/false,
10440 /*koenig_p=*/false,
10441 tf_warning_or_error);
10442 release_tree_vector (vec);
10443 return res;
10446 /* Parse an iteration-statement.
10448 iteration-statement:
10449 while ( condition ) statement
10450 do statement while ( expression ) ;
10451 for ( for-init-statement condition [opt] ; expression [opt] )
10452 statement
10454 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10456 static tree
10457 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10459 cp_token *token;
10460 enum rid keyword;
10461 tree statement;
10462 unsigned char in_statement;
10464 /* Peek at the next token. */
10465 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10466 if (!token)
10467 return error_mark_node;
10469 /* Remember whether or not we are already within an iteration
10470 statement. */
10471 in_statement = parser->in_statement;
10473 /* See what kind of keyword it is. */
10474 keyword = token->keyword;
10475 switch (keyword)
10477 case RID_WHILE:
10479 tree condition;
10481 /* Begin the while-statement. */
10482 statement = begin_while_stmt ();
10483 /* Look for the `('. */
10484 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10485 /* Parse the condition. */
10486 condition = cp_parser_condition (parser);
10487 finish_while_stmt_cond (condition, statement, ivdep);
10488 /* Look for the `)'. */
10489 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10490 /* Parse the dependent statement. */
10491 parser->in_statement = IN_ITERATION_STMT;
10492 cp_parser_already_scoped_statement (parser);
10493 parser->in_statement = in_statement;
10494 /* We're done with the while-statement. */
10495 finish_while_stmt (statement);
10497 break;
10499 case RID_DO:
10501 tree expression;
10503 /* Begin the do-statement. */
10504 statement = begin_do_stmt ();
10505 /* Parse the body of the do-statement. */
10506 parser->in_statement = IN_ITERATION_STMT;
10507 cp_parser_implicitly_scoped_statement (parser, NULL);
10508 parser->in_statement = in_statement;
10509 finish_do_body (statement);
10510 /* Look for the `while' keyword. */
10511 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10512 /* Look for the `('. */
10513 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10514 /* Parse the expression. */
10515 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10516 /* We're done with the do-statement. */
10517 finish_do_stmt (expression, statement, ivdep);
10518 /* Look for the `)'. */
10519 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10520 /* Look for the `;'. */
10521 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10523 break;
10525 case RID_FOR:
10527 /* Look for the `('. */
10528 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10530 statement = cp_parser_for (parser, ivdep);
10532 /* Look for the `)'. */
10533 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10535 /* Parse the body of the for-statement. */
10536 parser->in_statement = IN_ITERATION_STMT;
10537 cp_parser_already_scoped_statement (parser);
10538 parser->in_statement = in_statement;
10540 /* We're done with the for-statement. */
10541 finish_for_stmt (statement);
10543 break;
10545 default:
10546 cp_parser_error (parser, "expected iteration-statement");
10547 statement = error_mark_node;
10548 break;
10551 return statement;
10554 /* Parse a for-init-statement or the declarator of a range-based-for.
10555 Returns true if a range-based-for declaration is seen.
10557 for-init-statement:
10558 expression-statement
10559 simple-declaration */
10561 static bool
10562 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10564 /* If the next token is a `;', then we have an empty
10565 expression-statement. Grammatically, this is also a
10566 simple-declaration, but an invalid one, because it does not
10567 declare anything. Therefore, if we did not handle this case
10568 specially, we would issue an error message about an invalid
10569 declaration. */
10570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10572 bool is_range_for = false;
10573 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10575 parser->colon_corrects_to_scope_p = false;
10577 /* We're going to speculatively look for a declaration, falling back
10578 to an expression, if necessary. */
10579 cp_parser_parse_tentatively (parser);
10580 /* Parse the declaration. */
10581 cp_parser_simple_declaration (parser,
10582 /*function_definition_allowed_p=*/false,
10583 decl);
10584 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10585 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10587 /* It is a range-for, consume the ':' */
10588 cp_lexer_consume_token (parser->lexer);
10589 is_range_for = true;
10590 if (cxx_dialect < cxx11)
10592 error_at (cp_lexer_peek_token (parser->lexer)->location,
10593 "range-based %<for%> loops are not allowed "
10594 "in C++98 mode");
10595 *decl = error_mark_node;
10598 else
10599 /* The ';' is not consumed yet because we told
10600 cp_parser_simple_declaration not to. */
10601 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10603 if (cp_parser_parse_definitely (parser))
10604 return is_range_for;
10605 /* If the tentative parse failed, then we shall need to look for an
10606 expression-statement. */
10608 /* If we are here, it is an expression-statement. */
10609 cp_parser_expression_statement (parser, NULL_TREE);
10610 return false;
10613 /* Parse a jump-statement.
10615 jump-statement:
10616 break ;
10617 continue ;
10618 return expression [opt] ;
10619 return braced-init-list ;
10620 goto identifier ;
10622 GNU extension:
10624 jump-statement:
10625 goto * expression ;
10627 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10629 static tree
10630 cp_parser_jump_statement (cp_parser* parser)
10632 tree statement = error_mark_node;
10633 cp_token *token;
10634 enum rid keyword;
10635 unsigned char in_statement;
10637 /* Peek at the next token. */
10638 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10639 if (!token)
10640 return error_mark_node;
10642 /* See what kind of keyword it is. */
10643 keyword = token->keyword;
10644 switch (keyword)
10646 case RID_BREAK:
10647 in_statement = parser->in_statement & ~IN_IF_STMT;
10648 switch (in_statement)
10650 case 0:
10651 error_at (token->location, "break statement not within loop or switch");
10652 break;
10653 default:
10654 gcc_assert ((in_statement & IN_SWITCH_STMT)
10655 || in_statement == IN_ITERATION_STMT);
10656 statement = finish_break_stmt ();
10657 if (in_statement == IN_ITERATION_STMT)
10658 break_maybe_infinite_loop ();
10659 break;
10660 case IN_OMP_BLOCK:
10661 error_at (token->location, "invalid exit from OpenMP structured block");
10662 break;
10663 case IN_OMP_FOR:
10664 error_at (token->location, "break statement used with OpenMP for loop");
10665 break;
10666 case IN_CILK_SIMD_FOR:
10667 error_at (token->location, "break statement used with Cilk Plus for loop");
10668 break;
10670 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10671 break;
10673 case RID_CONTINUE:
10674 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10676 case 0:
10677 error_at (token->location, "continue statement not within a loop");
10678 break;
10679 case IN_CILK_SIMD_FOR:
10680 error_at (token->location,
10681 "continue statement within %<#pragma simd%> loop body");
10682 /* Fall through. */
10683 case IN_ITERATION_STMT:
10684 case IN_OMP_FOR:
10685 statement = finish_continue_stmt ();
10686 break;
10687 case IN_OMP_BLOCK:
10688 error_at (token->location, "invalid exit from OpenMP structured block");
10689 break;
10690 default:
10691 gcc_unreachable ();
10693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10694 break;
10696 case RID_RETURN:
10698 tree expr;
10699 bool expr_non_constant_p;
10701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10703 cp_lexer_set_source_position (parser->lexer);
10704 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10705 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10707 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10708 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10709 else
10710 /* If the next token is a `;', then there is no
10711 expression. */
10712 expr = NULL_TREE;
10713 /* Build the return-statement. */
10714 statement = finish_return_stmt (expr);
10715 /* Look for the final `;'. */
10716 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10718 break;
10720 case RID_GOTO:
10721 /* Create the goto-statement. */
10722 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10724 /* Issue a warning about this use of a GNU extension. */
10725 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10726 /* Consume the '*' token. */
10727 cp_lexer_consume_token (parser->lexer);
10728 /* Parse the dependent expression. */
10729 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10731 else
10732 finish_goto_stmt (cp_parser_identifier (parser));
10733 /* Look for the final `;'. */
10734 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10735 break;
10737 default:
10738 cp_parser_error (parser, "expected jump-statement");
10739 break;
10742 return statement;
10745 /* Parse a declaration-statement.
10747 declaration-statement:
10748 block-declaration */
10750 static void
10751 cp_parser_declaration_statement (cp_parser* parser)
10753 void *p;
10755 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10756 p = obstack_alloc (&declarator_obstack, 0);
10758 /* Parse the block-declaration. */
10759 cp_parser_block_declaration (parser, /*statement_p=*/true);
10761 /* Free any declarators allocated. */
10762 obstack_free (&declarator_obstack, p);
10765 /* Some dependent statements (like `if (cond) statement'), are
10766 implicitly in their own scope. In other words, if the statement is
10767 a single statement (as opposed to a compound-statement), it is
10768 none-the-less treated as if it were enclosed in braces. Any
10769 declarations appearing in the dependent statement are out of scope
10770 after control passes that point. This function parses a statement,
10771 but ensures that is in its own scope, even if it is not a
10772 compound-statement.
10774 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10775 is a (possibly labeled) if statement which is not enclosed in
10776 braces and has an else clause. This is used to implement
10777 -Wparentheses.
10779 Returns the new statement. */
10781 static tree
10782 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10784 tree statement;
10786 if (if_p != NULL)
10787 *if_p = false;
10789 /* Mark if () ; with a special NOP_EXPR. */
10790 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10792 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10793 cp_lexer_consume_token (parser->lexer);
10794 statement = add_stmt (build_empty_stmt (loc));
10796 /* if a compound is opened, we simply parse the statement directly. */
10797 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10798 statement = cp_parser_compound_statement (parser, NULL, false, false);
10799 /* If the token is not a `{', then we must take special action. */
10800 else
10802 /* Create a compound-statement. */
10803 statement = begin_compound_stmt (0);
10804 /* Parse the dependent-statement. */
10805 cp_parser_statement (parser, NULL_TREE, false, if_p);
10806 /* Finish the dummy compound-statement. */
10807 finish_compound_stmt (statement);
10810 /* Return the statement. */
10811 return statement;
10814 /* For some dependent statements (like `while (cond) statement'), we
10815 have already created a scope. Therefore, even if the dependent
10816 statement is a compound-statement, we do not want to create another
10817 scope. */
10819 static void
10820 cp_parser_already_scoped_statement (cp_parser* parser)
10822 /* If the token is a `{', then we must take special action. */
10823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10824 cp_parser_statement (parser, NULL_TREE, false, NULL);
10825 else
10827 /* Avoid calling cp_parser_compound_statement, so that we
10828 don't create a new scope. Do everything else by hand. */
10829 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10830 /* If the next keyword is `__label__' we have a label declaration. */
10831 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10832 cp_parser_label_declaration (parser);
10833 /* Parse an (optional) statement-seq. */
10834 cp_parser_statement_seq_opt (parser, NULL_TREE);
10835 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10839 /* Declarations [gram.dcl.dcl] */
10841 /* Parse an optional declaration-sequence.
10843 declaration-seq:
10844 declaration
10845 declaration-seq declaration */
10847 static void
10848 cp_parser_declaration_seq_opt (cp_parser* parser)
10850 while (true)
10852 cp_token *token;
10854 token = cp_lexer_peek_token (parser->lexer);
10856 if (token->type == CPP_CLOSE_BRACE
10857 || token->type == CPP_EOF
10858 || token->type == CPP_PRAGMA_EOL)
10859 break;
10861 if (token->type == CPP_SEMICOLON)
10863 /* A declaration consisting of a single semicolon is
10864 invalid. Allow it unless we're being pedantic. */
10865 cp_lexer_consume_token (parser->lexer);
10866 if (!in_system_header_at (input_location))
10867 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10868 continue;
10871 /* If we're entering or exiting a region that's implicitly
10872 extern "C", modify the lang context appropriately. */
10873 if (!parser->implicit_extern_c && token->implicit_extern_c)
10875 push_lang_context (lang_name_c);
10876 parser->implicit_extern_c = true;
10878 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10880 pop_lang_context ();
10881 parser->implicit_extern_c = false;
10884 if (token->type == CPP_PRAGMA)
10886 /* A top-level declaration can consist solely of a #pragma.
10887 A nested declaration cannot, so this is done here and not
10888 in cp_parser_declaration. (A #pragma at block scope is
10889 handled in cp_parser_statement.) */
10890 cp_parser_pragma (parser, pragma_external);
10891 continue;
10894 /* Parse the declaration itself. */
10895 cp_parser_declaration (parser);
10899 /* Parse a declaration.
10901 declaration:
10902 block-declaration
10903 function-definition
10904 template-declaration
10905 explicit-instantiation
10906 explicit-specialization
10907 linkage-specification
10908 namespace-definition
10910 GNU extension:
10912 declaration:
10913 __extension__ declaration */
10915 static void
10916 cp_parser_declaration (cp_parser* parser)
10918 cp_token token1;
10919 cp_token token2;
10920 int saved_pedantic;
10921 void *p;
10922 tree attributes = NULL_TREE;
10924 /* Check for the `__extension__' keyword. */
10925 if (cp_parser_extension_opt (parser, &saved_pedantic))
10927 /* Parse the qualified declaration. */
10928 cp_parser_declaration (parser);
10929 /* Restore the PEDANTIC flag. */
10930 pedantic = saved_pedantic;
10932 return;
10935 /* Try to figure out what kind of declaration is present. */
10936 token1 = *cp_lexer_peek_token (parser->lexer);
10938 if (token1.type != CPP_EOF)
10939 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10940 else
10942 token2.type = CPP_EOF;
10943 token2.keyword = RID_MAX;
10946 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10947 p = obstack_alloc (&declarator_obstack, 0);
10949 /* If the next token is `extern' and the following token is a string
10950 literal, then we have a linkage specification. */
10951 if (token1.keyword == RID_EXTERN
10952 && cp_parser_is_pure_string_literal (&token2))
10953 cp_parser_linkage_specification (parser);
10954 /* If the next token is `template', then we have either a template
10955 declaration, an explicit instantiation, or an explicit
10956 specialization. */
10957 else if (token1.keyword == RID_TEMPLATE)
10959 /* `template <>' indicates a template specialization. */
10960 if (token2.type == CPP_LESS
10961 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10962 cp_parser_explicit_specialization (parser);
10963 /* `template <' indicates a template declaration. */
10964 else if (token2.type == CPP_LESS)
10965 cp_parser_template_declaration (parser, /*member_p=*/false);
10966 /* Anything else must be an explicit instantiation. */
10967 else
10968 cp_parser_explicit_instantiation (parser);
10970 /* If the next token is `export', then we have a template
10971 declaration. */
10972 else if (token1.keyword == RID_EXPORT)
10973 cp_parser_template_declaration (parser, /*member_p=*/false);
10974 /* If the next token is `extern', 'static' or 'inline' and the one
10975 after that is `template', we have a GNU extended explicit
10976 instantiation directive. */
10977 else if (cp_parser_allow_gnu_extensions_p (parser)
10978 && (token1.keyword == RID_EXTERN
10979 || token1.keyword == RID_STATIC
10980 || token1.keyword == RID_INLINE)
10981 && token2.keyword == RID_TEMPLATE)
10982 cp_parser_explicit_instantiation (parser);
10983 /* If the next token is `namespace', check for a named or unnamed
10984 namespace definition. */
10985 else if (token1.keyword == RID_NAMESPACE
10986 && (/* A named namespace definition. */
10987 (token2.type == CPP_NAME
10988 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10989 != CPP_EQ))
10990 /* An unnamed namespace definition. */
10991 || token2.type == CPP_OPEN_BRACE
10992 || token2.keyword == RID_ATTRIBUTE))
10993 cp_parser_namespace_definition (parser);
10994 /* An inline (associated) namespace definition. */
10995 else if (token1.keyword == RID_INLINE
10996 && token2.keyword == RID_NAMESPACE)
10997 cp_parser_namespace_definition (parser);
10998 /* Objective-C++ declaration/definition. */
10999 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11000 cp_parser_objc_declaration (parser, NULL_TREE);
11001 else if (c_dialect_objc ()
11002 && token1.keyword == RID_ATTRIBUTE
11003 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11004 cp_parser_objc_declaration (parser, attributes);
11005 /* We must have either a block declaration or a function
11006 definition. */
11007 else
11008 /* Try to parse a block-declaration, or a function-definition. */
11009 cp_parser_block_declaration (parser, /*statement_p=*/false);
11011 /* Free any declarators allocated. */
11012 obstack_free (&declarator_obstack, p);
11015 /* Parse a block-declaration.
11017 block-declaration:
11018 simple-declaration
11019 asm-definition
11020 namespace-alias-definition
11021 using-declaration
11022 using-directive
11024 GNU Extension:
11026 block-declaration:
11027 __extension__ block-declaration
11029 C++0x Extension:
11031 block-declaration:
11032 static_assert-declaration
11034 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11035 part of a declaration-statement. */
11037 static void
11038 cp_parser_block_declaration (cp_parser *parser,
11039 bool statement_p)
11041 cp_token *token1;
11042 int saved_pedantic;
11044 /* Check for the `__extension__' keyword. */
11045 if (cp_parser_extension_opt (parser, &saved_pedantic))
11047 /* Parse the qualified declaration. */
11048 cp_parser_block_declaration (parser, statement_p);
11049 /* Restore the PEDANTIC flag. */
11050 pedantic = saved_pedantic;
11052 return;
11055 /* Peek at the next token to figure out which kind of declaration is
11056 present. */
11057 token1 = cp_lexer_peek_token (parser->lexer);
11059 /* If the next keyword is `asm', we have an asm-definition. */
11060 if (token1->keyword == RID_ASM)
11062 if (statement_p)
11063 cp_parser_commit_to_tentative_parse (parser);
11064 cp_parser_asm_definition (parser);
11066 /* If the next keyword is `namespace', we have a
11067 namespace-alias-definition. */
11068 else if (token1->keyword == RID_NAMESPACE)
11069 cp_parser_namespace_alias_definition (parser);
11070 /* If the next keyword is `using', we have a
11071 using-declaration, a using-directive, or an alias-declaration. */
11072 else if (token1->keyword == RID_USING)
11074 cp_token *token2;
11076 if (statement_p)
11077 cp_parser_commit_to_tentative_parse (parser);
11078 /* If the token after `using' is `namespace', then we have a
11079 using-directive. */
11080 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11081 if (token2->keyword == RID_NAMESPACE)
11082 cp_parser_using_directive (parser);
11083 /* If the second token after 'using' is '=', then we have an
11084 alias-declaration. */
11085 else if (cxx_dialect >= cxx11
11086 && token2->type == CPP_NAME
11087 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11088 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11089 cp_parser_alias_declaration (parser);
11090 /* Otherwise, it's a using-declaration. */
11091 else
11092 cp_parser_using_declaration (parser,
11093 /*access_declaration_p=*/false);
11095 /* If the next keyword is `__label__' we have a misplaced label
11096 declaration. */
11097 else if (token1->keyword == RID_LABEL)
11099 cp_lexer_consume_token (parser->lexer);
11100 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11101 cp_parser_skip_to_end_of_statement (parser);
11102 /* If the next token is now a `;', consume it. */
11103 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11104 cp_lexer_consume_token (parser->lexer);
11106 /* If the next token is `static_assert' we have a static assertion. */
11107 else if (token1->keyword == RID_STATIC_ASSERT)
11108 cp_parser_static_assert (parser, /*member_p=*/false);
11109 /* Anything else must be a simple-declaration. */
11110 else
11111 cp_parser_simple_declaration (parser, !statement_p,
11112 /*maybe_range_for_decl*/NULL);
11115 /* Parse a simple-declaration.
11117 simple-declaration:
11118 decl-specifier-seq [opt] init-declarator-list [opt] ;
11120 init-declarator-list:
11121 init-declarator
11122 init-declarator-list , init-declarator
11124 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11125 function-definition as a simple-declaration.
11127 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11128 parsed declaration if it is an uninitialized single declarator not followed
11129 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11130 if present, will not be consumed. */
11132 static void
11133 cp_parser_simple_declaration (cp_parser* parser,
11134 bool function_definition_allowed_p,
11135 tree *maybe_range_for_decl)
11137 cp_decl_specifier_seq decl_specifiers;
11138 int declares_class_or_enum;
11139 bool saw_declarator;
11141 if (maybe_range_for_decl)
11142 *maybe_range_for_decl = NULL_TREE;
11144 /* Defer access checks until we know what is being declared; the
11145 checks for names appearing in the decl-specifier-seq should be
11146 done as if we were in the scope of the thing being declared. */
11147 push_deferring_access_checks (dk_deferred);
11149 /* Parse the decl-specifier-seq. We have to keep track of whether
11150 or not the decl-specifier-seq declares a named class or
11151 enumeration type, since that is the only case in which the
11152 init-declarator-list is allowed to be empty.
11154 [dcl.dcl]
11156 In a simple-declaration, the optional init-declarator-list can be
11157 omitted only when declaring a class or enumeration, that is when
11158 the decl-specifier-seq contains either a class-specifier, an
11159 elaborated-type-specifier, or an enum-specifier. */
11160 cp_parser_decl_specifier_seq (parser,
11161 CP_PARSER_FLAGS_OPTIONAL,
11162 &decl_specifiers,
11163 &declares_class_or_enum);
11164 /* We no longer need to defer access checks. */
11165 stop_deferring_access_checks ();
11167 /* In a block scope, a valid declaration must always have a
11168 decl-specifier-seq. By not trying to parse declarators, we can
11169 resolve the declaration/expression ambiguity more quickly. */
11170 if (!function_definition_allowed_p
11171 && !decl_specifiers.any_specifiers_p)
11173 cp_parser_error (parser, "expected declaration");
11174 goto done;
11177 /* If the next two tokens are both identifiers, the code is
11178 erroneous. The usual cause of this situation is code like:
11180 T t;
11182 where "T" should name a type -- but does not. */
11183 if (!decl_specifiers.any_type_specifiers_p
11184 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11186 /* If parsing tentatively, we should commit; we really are
11187 looking at a declaration. */
11188 cp_parser_commit_to_tentative_parse (parser);
11189 /* Give up. */
11190 goto done;
11193 /* If we have seen at least one decl-specifier, and the next token
11194 is not a parenthesis, then we must be looking at a declaration.
11195 (After "int (" we might be looking at a functional cast.) */
11196 if (decl_specifiers.any_specifiers_p
11197 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11198 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11199 && !cp_parser_error_occurred (parser))
11200 cp_parser_commit_to_tentative_parse (parser);
11202 /* Keep going until we hit the `;' at the end of the simple
11203 declaration. */
11204 saw_declarator = false;
11205 while (cp_lexer_next_token_is_not (parser->lexer,
11206 CPP_SEMICOLON))
11208 cp_token *token;
11209 bool function_definition_p;
11210 tree decl;
11212 if (saw_declarator)
11214 /* If we are processing next declarator, coma is expected */
11215 token = cp_lexer_peek_token (parser->lexer);
11216 gcc_assert (token->type == CPP_COMMA);
11217 cp_lexer_consume_token (parser->lexer);
11218 if (maybe_range_for_decl)
11219 *maybe_range_for_decl = error_mark_node;
11221 else
11222 saw_declarator = true;
11224 /* Parse the init-declarator. */
11225 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11226 /*checks=*/NULL,
11227 function_definition_allowed_p,
11228 /*member_p=*/false,
11229 declares_class_or_enum,
11230 &function_definition_p,
11231 maybe_range_for_decl);
11232 /* If an error occurred while parsing tentatively, exit quickly.
11233 (That usually happens when in the body of a function; each
11234 statement is treated as a declaration-statement until proven
11235 otherwise.) */
11236 if (cp_parser_error_occurred (parser))
11237 goto done;
11238 /* Handle function definitions specially. */
11239 if (function_definition_p)
11241 /* If the next token is a `,', then we are probably
11242 processing something like:
11244 void f() {}, *p;
11246 which is erroneous. */
11247 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11249 cp_token *token = cp_lexer_peek_token (parser->lexer);
11250 error_at (token->location,
11251 "mixing"
11252 " declarations and function-definitions is forbidden");
11254 /* Otherwise, we're done with the list of declarators. */
11255 else
11257 pop_deferring_access_checks ();
11258 return;
11261 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11262 *maybe_range_for_decl = decl;
11263 /* The next token should be either a `,' or a `;'. */
11264 token = cp_lexer_peek_token (parser->lexer);
11265 /* If it's a `,', there are more declarators to come. */
11266 if (token->type == CPP_COMMA)
11267 /* will be consumed next time around */;
11268 /* If it's a `;', we are done. */
11269 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11270 break;
11271 /* Anything else is an error. */
11272 else
11274 /* If we have already issued an error message we don't need
11275 to issue another one. */
11276 if (decl != error_mark_node
11277 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11278 cp_parser_error (parser, "expected %<,%> or %<;%>");
11279 /* Skip tokens until we reach the end of the statement. */
11280 cp_parser_skip_to_end_of_statement (parser);
11281 /* If the next token is now a `;', consume it. */
11282 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11283 cp_lexer_consume_token (parser->lexer);
11284 goto done;
11286 /* After the first time around, a function-definition is not
11287 allowed -- even if it was OK at first. For example:
11289 int i, f() {}
11291 is not valid. */
11292 function_definition_allowed_p = false;
11295 /* Issue an error message if no declarators are present, and the
11296 decl-specifier-seq does not itself declare a class or
11297 enumeration: [dcl.dcl]/3. */
11298 if (!saw_declarator)
11300 if (cp_parser_declares_only_class_p (parser))
11302 if (!declares_class_or_enum
11303 && decl_specifiers.type
11304 && OVERLOAD_TYPE_P (decl_specifiers.type))
11305 /* Ensure an error is issued anyway when finish_decltype_type,
11306 called via cp_parser_decl_specifier_seq, returns a class or
11307 an enumeration (c++/51786). */
11308 decl_specifiers.type = NULL_TREE;
11309 shadow_tag (&decl_specifiers);
11311 /* Perform any deferred access checks. */
11312 perform_deferred_access_checks (tf_warning_or_error);
11315 /* Consume the `;'. */
11316 if (!maybe_range_for_decl)
11317 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11319 done:
11320 pop_deferring_access_checks ();
11323 /* Parse a decl-specifier-seq.
11325 decl-specifier-seq:
11326 decl-specifier-seq [opt] decl-specifier
11327 decl-specifier attribute-specifier-seq [opt] (C++11)
11329 decl-specifier:
11330 storage-class-specifier
11331 type-specifier
11332 function-specifier
11333 friend
11334 typedef
11336 GNU Extension:
11338 decl-specifier:
11339 attributes
11341 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11343 The parser flags FLAGS is used to control type-specifier parsing.
11345 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11346 flags:
11348 1: one of the decl-specifiers is an elaborated-type-specifier
11349 (i.e., a type declaration)
11350 2: one of the decl-specifiers is an enum-specifier or a
11351 class-specifier (i.e., a type definition)
11355 static void
11356 cp_parser_decl_specifier_seq (cp_parser* parser,
11357 cp_parser_flags flags,
11358 cp_decl_specifier_seq *decl_specs,
11359 int* declares_class_or_enum)
11361 bool constructor_possible_p = !parser->in_declarator_p;
11362 bool found_decl_spec = false;
11363 cp_token *start_token = NULL;
11364 cp_decl_spec ds;
11366 /* Clear DECL_SPECS. */
11367 clear_decl_specs (decl_specs);
11369 /* Assume no class or enumeration type is declared. */
11370 *declares_class_or_enum = 0;
11372 /* Keep reading specifiers until there are no more to read. */
11373 while (true)
11375 bool constructor_p;
11376 cp_token *token;
11377 ds = ds_last;
11379 /* Peek at the next token. */
11380 token = cp_lexer_peek_token (parser->lexer);
11382 /* Save the first token of the decl spec list for error
11383 reporting. */
11384 if (!start_token)
11385 start_token = token;
11386 /* Handle attributes. */
11387 if (cp_next_tokens_can_be_attribute_p (parser))
11389 /* Parse the attributes. */
11390 tree attrs = cp_parser_attributes_opt (parser);
11392 /* In a sequence of declaration specifiers, c++11 attributes
11393 appertain to the type that precede them. In that case
11394 [dcl.spec]/1 says:
11396 The attribute-specifier-seq affects the type only for
11397 the declaration it appears in, not other declarations
11398 involving the same type.
11400 But for now let's force the user to position the
11401 attribute either at the beginning of the declaration or
11402 after the declarator-id, which would clearly mean that it
11403 applies to the declarator. */
11404 if (cxx11_attribute_p (attrs))
11406 if (!found_decl_spec)
11407 /* The c++11 attribute is at the beginning of the
11408 declaration. It appertains to the entity being
11409 declared. */;
11410 else
11412 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11414 /* This is an attribute following a
11415 class-specifier. */
11416 if (decl_specs->type_definition_p)
11417 warn_misplaced_attr_for_class_type (token->location,
11418 decl_specs->type);
11419 attrs = NULL_TREE;
11421 else
11423 decl_specs->std_attributes
11424 = chainon (decl_specs->std_attributes,
11425 attrs);
11426 if (decl_specs->locations[ds_std_attribute] == 0)
11427 decl_specs->locations[ds_std_attribute] = token->location;
11429 continue;
11433 decl_specs->attributes
11434 = chainon (decl_specs->attributes,
11435 attrs);
11436 if (decl_specs->locations[ds_attribute] == 0)
11437 decl_specs->locations[ds_attribute] = token->location;
11438 continue;
11440 /* Assume we will find a decl-specifier keyword. */
11441 found_decl_spec = true;
11442 /* If the next token is an appropriate keyword, we can simply
11443 add it to the list. */
11444 switch (token->keyword)
11446 /* decl-specifier:
11447 friend
11448 constexpr */
11449 case RID_FRIEND:
11450 if (!at_class_scope_p ())
11452 error_at (token->location, "%<friend%> used outside of class");
11453 cp_lexer_purge_token (parser->lexer);
11455 else
11457 ds = ds_friend;
11458 /* Consume the token. */
11459 cp_lexer_consume_token (parser->lexer);
11461 break;
11463 case RID_CONSTEXPR:
11464 ds = ds_constexpr;
11465 cp_lexer_consume_token (parser->lexer);
11466 break;
11468 /* function-specifier:
11469 inline
11470 virtual
11471 explicit */
11472 case RID_INLINE:
11473 case RID_VIRTUAL:
11474 case RID_EXPLICIT:
11475 cp_parser_function_specifier_opt (parser, decl_specs);
11476 break;
11478 /* decl-specifier:
11479 typedef */
11480 case RID_TYPEDEF:
11481 ds = ds_typedef;
11482 /* Consume the token. */
11483 cp_lexer_consume_token (parser->lexer);
11484 /* A constructor declarator cannot appear in a typedef. */
11485 constructor_possible_p = false;
11486 /* The "typedef" keyword can only occur in a declaration; we
11487 may as well commit at this point. */
11488 cp_parser_commit_to_tentative_parse (parser);
11490 if (decl_specs->storage_class != sc_none)
11491 decl_specs->conflicting_specifiers_p = true;
11492 break;
11494 /* storage-class-specifier:
11495 auto
11496 register
11497 static
11498 extern
11499 mutable
11501 GNU Extension:
11502 thread */
11503 case RID_AUTO:
11504 if (cxx_dialect == cxx98)
11506 /* Consume the token. */
11507 cp_lexer_consume_token (parser->lexer);
11509 /* Complain about `auto' as a storage specifier, if
11510 we're complaining about C++0x compatibility. */
11511 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11512 " changes meaning in C++11; please remove it");
11514 /* Set the storage class anyway. */
11515 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11516 token);
11518 else
11519 /* C++0x auto type-specifier. */
11520 found_decl_spec = false;
11521 break;
11523 case RID_REGISTER:
11524 case RID_STATIC:
11525 case RID_EXTERN:
11526 case RID_MUTABLE:
11527 /* Consume the token. */
11528 cp_lexer_consume_token (parser->lexer);
11529 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11530 token);
11531 break;
11532 case RID_THREAD:
11533 /* Consume the token. */
11534 ds = ds_thread;
11535 cp_lexer_consume_token (parser->lexer);
11536 break;
11538 default:
11539 /* We did not yet find a decl-specifier yet. */
11540 found_decl_spec = false;
11541 break;
11544 if (found_decl_spec
11545 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11546 && token->keyword != RID_CONSTEXPR)
11547 error ("decl-specifier invalid in condition");
11549 if (ds != ds_last)
11550 set_and_check_decl_spec_loc (decl_specs, ds, token);
11552 /* Constructors are a special case. The `S' in `S()' is not a
11553 decl-specifier; it is the beginning of the declarator. */
11554 constructor_p
11555 = (!found_decl_spec
11556 && constructor_possible_p
11557 && (cp_parser_constructor_declarator_p
11558 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11560 /* If we don't have a DECL_SPEC yet, then we must be looking at
11561 a type-specifier. */
11562 if (!found_decl_spec && !constructor_p)
11564 int decl_spec_declares_class_or_enum;
11565 bool is_cv_qualifier;
11566 tree type_spec;
11568 type_spec
11569 = cp_parser_type_specifier (parser, flags,
11570 decl_specs,
11571 /*is_declaration=*/true,
11572 &decl_spec_declares_class_or_enum,
11573 &is_cv_qualifier);
11574 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11576 /* If this type-specifier referenced a user-defined type
11577 (a typedef, class-name, etc.), then we can't allow any
11578 more such type-specifiers henceforth.
11580 [dcl.spec]
11582 The longest sequence of decl-specifiers that could
11583 possibly be a type name is taken as the
11584 decl-specifier-seq of a declaration. The sequence shall
11585 be self-consistent as described below.
11587 [dcl.type]
11589 As a general rule, at most one type-specifier is allowed
11590 in the complete decl-specifier-seq of a declaration. The
11591 only exceptions are the following:
11593 -- const or volatile can be combined with any other
11594 type-specifier.
11596 -- signed or unsigned can be combined with char, long,
11597 short, or int.
11599 -- ..
11601 Example:
11603 typedef char* Pc;
11604 void g (const int Pc);
11606 Here, Pc is *not* part of the decl-specifier seq; it's
11607 the declarator. Therefore, once we see a type-specifier
11608 (other than a cv-qualifier), we forbid any additional
11609 user-defined types. We *do* still allow things like `int
11610 int' to be considered a decl-specifier-seq, and issue the
11611 error message later. */
11612 if (type_spec && !is_cv_qualifier)
11613 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11614 /* A constructor declarator cannot follow a type-specifier. */
11615 if (type_spec)
11617 constructor_possible_p = false;
11618 found_decl_spec = true;
11619 if (!is_cv_qualifier)
11620 decl_specs->any_type_specifiers_p = true;
11624 /* If we still do not have a DECL_SPEC, then there are no more
11625 decl-specifiers. */
11626 if (!found_decl_spec)
11627 break;
11629 decl_specs->any_specifiers_p = true;
11630 /* After we see one decl-specifier, further decl-specifiers are
11631 always optional. */
11632 flags |= CP_PARSER_FLAGS_OPTIONAL;
11635 /* Don't allow a friend specifier with a class definition. */
11636 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11637 && (*declares_class_or_enum & 2))
11638 error_at (decl_specs->locations[ds_friend],
11639 "class definition may not be declared a friend");
11642 /* Parse an (optional) storage-class-specifier.
11644 storage-class-specifier:
11645 auto
11646 register
11647 static
11648 extern
11649 mutable
11651 GNU Extension:
11653 storage-class-specifier:
11654 thread
11656 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11658 static tree
11659 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11661 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11663 case RID_AUTO:
11664 if (cxx_dialect != cxx98)
11665 return NULL_TREE;
11666 /* Fall through for C++98. */
11668 case RID_REGISTER:
11669 case RID_STATIC:
11670 case RID_EXTERN:
11671 case RID_MUTABLE:
11672 case RID_THREAD:
11673 /* Consume the token. */
11674 return cp_lexer_consume_token (parser->lexer)->u.value;
11676 default:
11677 return NULL_TREE;
11681 /* Parse an (optional) function-specifier.
11683 function-specifier:
11684 inline
11685 virtual
11686 explicit
11688 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11689 Updates DECL_SPECS, if it is non-NULL. */
11691 static tree
11692 cp_parser_function_specifier_opt (cp_parser* parser,
11693 cp_decl_specifier_seq *decl_specs)
11695 cp_token *token = cp_lexer_peek_token (parser->lexer);
11696 switch (token->keyword)
11698 case RID_INLINE:
11699 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11700 break;
11702 case RID_VIRTUAL:
11703 /* 14.5.2.3 [temp.mem]
11705 A member function template shall not be virtual. */
11706 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11707 error_at (token->location, "templates may not be %<virtual%>");
11708 else
11709 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11710 break;
11712 case RID_EXPLICIT:
11713 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11714 break;
11716 default:
11717 return NULL_TREE;
11720 /* Consume the token. */
11721 return cp_lexer_consume_token (parser->lexer)->u.value;
11724 /* Parse a linkage-specification.
11726 linkage-specification:
11727 extern string-literal { declaration-seq [opt] }
11728 extern string-literal declaration */
11730 static void
11731 cp_parser_linkage_specification (cp_parser* parser)
11733 tree linkage;
11735 /* Look for the `extern' keyword. */
11736 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11738 /* Look for the string-literal. */
11739 linkage = cp_parser_string_literal (parser, false, false);
11741 /* Transform the literal into an identifier. If the literal is a
11742 wide-character string, or contains embedded NULs, then we can't
11743 handle it as the user wants. */
11744 if (strlen (TREE_STRING_POINTER (linkage))
11745 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11747 cp_parser_error (parser, "invalid linkage-specification");
11748 /* Assume C++ linkage. */
11749 linkage = lang_name_cplusplus;
11751 else
11752 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11754 /* We're now using the new linkage. */
11755 push_lang_context (linkage);
11757 /* If the next token is a `{', then we're using the first
11758 production. */
11759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11761 cp_ensure_no_omp_declare_simd (parser);
11763 /* Consume the `{' token. */
11764 cp_lexer_consume_token (parser->lexer);
11765 /* Parse the declarations. */
11766 cp_parser_declaration_seq_opt (parser);
11767 /* Look for the closing `}'. */
11768 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11770 /* Otherwise, there's just one declaration. */
11771 else
11773 bool saved_in_unbraced_linkage_specification_p;
11775 saved_in_unbraced_linkage_specification_p
11776 = parser->in_unbraced_linkage_specification_p;
11777 parser->in_unbraced_linkage_specification_p = true;
11778 cp_parser_declaration (parser);
11779 parser->in_unbraced_linkage_specification_p
11780 = saved_in_unbraced_linkage_specification_p;
11783 /* We're done with the linkage-specification. */
11784 pop_lang_context ();
11787 /* Parse a static_assert-declaration.
11789 static_assert-declaration:
11790 static_assert ( constant-expression , string-literal ) ;
11792 If MEMBER_P, this static_assert is a class member. */
11794 static void
11795 cp_parser_static_assert(cp_parser *parser, bool member_p)
11797 tree condition;
11798 tree message;
11799 cp_token *token;
11800 location_t saved_loc;
11801 bool dummy;
11803 /* Peek at the `static_assert' token so we can keep track of exactly
11804 where the static assertion started. */
11805 token = cp_lexer_peek_token (parser->lexer);
11806 saved_loc = token->location;
11808 /* Look for the `static_assert' keyword. */
11809 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11810 RT_STATIC_ASSERT))
11811 return;
11813 /* We know we are in a static assertion; commit to any tentative
11814 parse. */
11815 if (cp_parser_parsing_tentatively (parser))
11816 cp_parser_commit_to_tentative_parse (parser);
11818 /* Parse the `(' starting the static assertion condition. */
11819 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11821 /* Parse the constant-expression. Allow a non-constant expression
11822 here in order to give better diagnostics in finish_static_assert. */
11823 condition =
11824 cp_parser_constant_expression (parser,
11825 /*allow_non_constant_p=*/true,
11826 /*non_constant_p=*/&dummy);
11828 /* Parse the separating `,'. */
11829 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11831 /* Parse the string-literal message. */
11832 message = cp_parser_string_literal (parser,
11833 /*translate=*/false,
11834 /*wide_ok=*/true);
11836 /* A `)' completes the static assertion. */
11837 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11838 cp_parser_skip_to_closing_parenthesis (parser,
11839 /*recovering=*/true,
11840 /*or_comma=*/false,
11841 /*consume_paren=*/true);
11843 /* A semicolon terminates the declaration. */
11844 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11846 /* Complete the static assertion, which may mean either processing
11847 the static assert now or saving it for template instantiation. */
11848 finish_static_assert (condition, message, saved_loc, member_p);
11851 /* Parse the expression in decltype ( expression ). */
11853 static tree
11854 cp_parser_decltype_expr (cp_parser *parser,
11855 bool &id_expression_or_member_access_p)
11857 cp_token *id_expr_start_token;
11858 tree expr;
11860 /* First, try parsing an id-expression. */
11861 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11862 cp_parser_parse_tentatively (parser);
11863 expr = cp_parser_id_expression (parser,
11864 /*template_keyword_p=*/false,
11865 /*check_dependency_p=*/true,
11866 /*template_p=*/NULL,
11867 /*declarator_p=*/false,
11868 /*optional_p=*/false);
11870 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11872 bool non_integral_constant_expression_p = false;
11873 tree id_expression = expr;
11874 cp_id_kind idk;
11875 const char *error_msg;
11877 if (identifier_p (expr))
11878 /* Lookup the name we got back from the id-expression. */
11879 expr = cp_parser_lookup_name_simple (parser, expr,
11880 id_expr_start_token->location);
11882 if (expr
11883 && expr != error_mark_node
11884 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11885 && TREE_CODE (expr) != TYPE_DECL
11886 && (TREE_CODE (expr) != BIT_NOT_EXPR
11887 || !TYPE_P (TREE_OPERAND (expr, 0)))
11888 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11890 /* Complete lookup of the id-expression. */
11891 expr = (finish_id_expression
11892 (id_expression, expr, parser->scope, &idk,
11893 /*integral_constant_expression_p=*/false,
11894 /*allow_non_integral_constant_expression_p=*/true,
11895 &non_integral_constant_expression_p,
11896 /*template_p=*/false,
11897 /*done=*/true,
11898 /*address_p=*/false,
11899 /*template_arg_p=*/false,
11900 &error_msg,
11901 id_expr_start_token->location));
11903 if (expr == error_mark_node)
11904 /* We found an id-expression, but it was something that we
11905 should not have found. This is an error, not something
11906 we can recover from, so note that we found an
11907 id-expression and we'll recover as gracefully as
11908 possible. */
11909 id_expression_or_member_access_p = true;
11912 if (expr
11913 && expr != error_mark_node
11914 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11915 /* We have an id-expression. */
11916 id_expression_or_member_access_p = true;
11919 if (!id_expression_or_member_access_p)
11921 /* Abort the id-expression parse. */
11922 cp_parser_abort_tentative_parse (parser);
11924 /* Parsing tentatively, again. */
11925 cp_parser_parse_tentatively (parser);
11927 /* Parse a class member access. */
11928 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11929 /*cast_p=*/false, /*decltype*/true,
11930 /*member_access_only_p=*/true, NULL);
11932 if (expr
11933 && expr != error_mark_node
11934 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11935 /* We have an id-expression. */
11936 id_expression_or_member_access_p = true;
11939 if (id_expression_or_member_access_p)
11940 /* We have parsed the complete id-expression or member access. */
11941 cp_parser_parse_definitely (parser);
11942 else
11944 /* Abort our attempt to parse an id-expression or member access
11945 expression. */
11946 cp_parser_abort_tentative_parse (parser);
11948 /* Parse a full expression. */
11949 expr = cp_parser_expression (parser, /*cast_p=*/false,
11950 /*decltype*/true, NULL);
11953 return expr;
11956 /* Parse a `decltype' type. Returns the type.
11958 simple-type-specifier:
11959 decltype ( expression )
11960 C++14 proposal:
11961 decltype ( auto ) */
11963 static tree
11964 cp_parser_decltype (cp_parser *parser)
11966 tree expr;
11967 bool id_expression_or_member_access_p = false;
11968 const char *saved_message;
11969 bool saved_integral_constant_expression_p;
11970 bool saved_non_integral_constant_expression_p;
11971 bool saved_greater_than_is_operator_p;
11972 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11974 if (start_token->type == CPP_DECLTYPE)
11976 /* Already parsed. */
11977 cp_lexer_consume_token (parser->lexer);
11978 return start_token->u.value;
11981 /* Look for the `decltype' token. */
11982 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11983 return error_mark_node;
11985 /* Parse the opening `('. */
11986 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11987 return error_mark_node;
11989 /* decltype (auto) */
11990 if (cxx_dialect >= cxx1y
11991 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11993 cp_lexer_consume_token (parser->lexer);
11994 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11995 return error_mark_node;
11996 expr = make_decltype_auto ();
11997 AUTO_IS_DECLTYPE (expr) = true;
11998 goto rewrite;
12001 /* Types cannot be defined in a `decltype' expression. Save away the
12002 old message. */
12003 saved_message = parser->type_definition_forbidden_message;
12005 /* And create the new one. */
12006 parser->type_definition_forbidden_message
12007 = G_("types may not be defined in %<decltype%> expressions");
12009 /* The restrictions on constant-expressions do not apply inside
12010 decltype expressions. */
12011 saved_integral_constant_expression_p
12012 = parser->integral_constant_expression_p;
12013 saved_non_integral_constant_expression_p
12014 = parser->non_integral_constant_expression_p;
12015 parser->integral_constant_expression_p = false;
12017 /* Within a parenthesized expression, a `>' token is always
12018 the greater-than operator. */
12019 saved_greater_than_is_operator_p
12020 = parser->greater_than_is_operator_p;
12021 parser->greater_than_is_operator_p = true;
12023 /* Do not actually evaluate the expression. */
12024 ++cp_unevaluated_operand;
12026 /* Do not warn about problems with the expression. */
12027 ++c_inhibit_evaluation_warnings;
12029 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12031 /* Go back to evaluating expressions. */
12032 --cp_unevaluated_operand;
12033 --c_inhibit_evaluation_warnings;
12035 /* The `>' token might be the end of a template-id or
12036 template-parameter-list now. */
12037 parser->greater_than_is_operator_p
12038 = saved_greater_than_is_operator_p;
12040 /* Restore the old message and the integral constant expression
12041 flags. */
12042 parser->type_definition_forbidden_message = saved_message;
12043 parser->integral_constant_expression_p
12044 = saved_integral_constant_expression_p;
12045 parser->non_integral_constant_expression_p
12046 = saved_non_integral_constant_expression_p;
12048 /* Parse to the closing `)'. */
12049 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12051 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12052 /*consume_paren=*/true);
12053 return error_mark_node;
12056 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12057 tf_warning_or_error);
12059 rewrite:
12060 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12061 it again. */
12062 start_token->type = CPP_DECLTYPE;
12063 start_token->u.value = expr;
12064 start_token->keyword = RID_MAX;
12065 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12067 return expr;
12070 /* Special member functions [gram.special] */
12072 /* Parse a conversion-function-id.
12074 conversion-function-id:
12075 operator conversion-type-id
12077 Returns an IDENTIFIER_NODE representing the operator. */
12079 static tree
12080 cp_parser_conversion_function_id (cp_parser* parser)
12082 tree type;
12083 tree saved_scope;
12084 tree saved_qualifying_scope;
12085 tree saved_object_scope;
12086 tree pushed_scope = NULL_TREE;
12088 /* Look for the `operator' token. */
12089 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12090 return error_mark_node;
12091 /* When we parse the conversion-type-id, the current scope will be
12092 reset. However, we need that information in able to look up the
12093 conversion function later, so we save it here. */
12094 saved_scope = parser->scope;
12095 saved_qualifying_scope = parser->qualifying_scope;
12096 saved_object_scope = parser->object_scope;
12097 /* We must enter the scope of the class so that the names of
12098 entities declared within the class are available in the
12099 conversion-type-id. For example, consider:
12101 struct S {
12102 typedef int I;
12103 operator I();
12106 S::operator I() { ... }
12108 In order to see that `I' is a type-name in the definition, we
12109 must be in the scope of `S'. */
12110 if (saved_scope)
12111 pushed_scope = push_scope (saved_scope);
12112 /* Parse the conversion-type-id. */
12113 type = cp_parser_conversion_type_id (parser);
12114 /* Leave the scope of the class, if any. */
12115 if (pushed_scope)
12116 pop_scope (pushed_scope);
12117 /* Restore the saved scope. */
12118 parser->scope = saved_scope;
12119 parser->qualifying_scope = saved_qualifying_scope;
12120 parser->object_scope = saved_object_scope;
12121 /* If the TYPE is invalid, indicate failure. */
12122 if (type == error_mark_node)
12123 return error_mark_node;
12124 return mangle_conv_op_name_for_type (type);
12127 /* Parse a conversion-type-id:
12129 conversion-type-id:
12130 type-specifier-seq conversion-declarator [opt]
12132 Returns the TYPE specified. */
12134 static tree
12135 cp_parser_conversion_type_id (cp_parser* parser)
12137 tree attributes;
12138 cp_decl_specifier_seq type_specifiers;
12139 cp_declarator *declarator;
12140 tree type_specified;
12141 const char *saved_message;
12143 /* Parse the attributes. */
12144 attributes = cp_parser_attributes_opt (parser);
12146 saved_message = parser->type_definition_forbidden_message;
12147 parser->type_definition_forbidden_message
12148 = G_("types may not be defined in a conversion-type-id");
12150 /* Parse the type-specifiers. */
12151 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12152 /*is_trailing_return=*/false,
12153 &type_specifiers);
12155 parser->type_definition_forbidden_message = saved_message;
12157 /* If that didn't work, stop. */
12158 if (type_specifiers.type == error_mark_node)
12159 return error_mark_node;
12160 /* Parse the conversion-declarator. */
12161 declarator = cp_parser_conversion_declarator_opt (parser);
12163 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12164 /*initialized=*/0, &attributes);
12165 if (attributes)
12166 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12168 /* Don't give this error when parsing tentatively. This happens to
12169 work because we always parse this definitively once. */
12170 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12171 && type_uses_auto (type_specified))
12173 if (cxx_dialect < cxx1y)
12175 error ("invalid use of %<auto%> in conversion operator");
12176 return error_mark_node;
12178 else if (template_parm_scope_p ())
12179 warning (0, "use of %<auto%> in member template "
12180 "conversion operator can never be deduced");
12183 return type_specified;
12186 /* Parse an (optional) conversion-declarator.
12188 conversion-declarator:
12189 ptr-operator conversion-declarator [opt]
12193 static cp_declarator *
12194 cp_parser_conversion_declarator_opt (cp_parser* parser)
12196 enum tree_code code;
12197 tree class_type, std_attributes = NULL_TREE;
12198 cp_cv_quals cv_quals;
12200 /* We don't know if there's a ptr-operator next, or not. */
12201 cp_parser_parse_tentatively (parser);
12202 /* Try the ptr-operator. */
12203 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12204 &std_attributes);
12205 /* If it worked, look for more conversion-declarators. */
12206 if (cp_parser_parse_definitely (parser))
12208 cp_declarator *declarator;
12210 /* Parse another optional declarator. */
12211 declarator = cp_parser_conversion_declarator_opt (parser);
12213 declarator = cp_parser_make_indirect_declarator
12214 (code, class_type, cv_quals, declarator, std_attributes);
12216 return declarator;
12219 return NULL;
12222 /* Parse an (optional) ctor-initializer.
12224 ctor-initializer:
12225 : mem-initializer-list
12227 Returns TRUE iff the ctor-initializer was actually present. */
12229 static bool
12230 cp_parser_ctor_initializer_opt (cp_parser* parser)
12232 /* If the next token is not a `:', then there is no
12233 ctor-initializer. */
12234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12236 /* Do default initialization of any bases and members. */
12237 if (DECL_CONSTRUCTOR_P (current_function_decl))
12238 finish_mem_initializers (NULL_TREE);
12240 return false;
12243 /* Consume the `:' token. */
12244 cp_lexer_consume_token (parser->lexer);
12245 /* And the mem-initializer-list. */
12246 cp_parser_mem_initializer_list (parser);
12248 return true;
12251 /* Parse a mem-initializer-list.
12253 mem-initializer-list:
12254 mem-initializer ... [opt]
12255 mem-initializer ... [opt] , mem-initializer-list */
12257 static void
12258 cp_parser_mem_initializer_list (cp_parser* parser)
12260 tree mem_initializer_list = NULL_TREE;
12261 tree target_ctor = error_mark_node;
12262 cp_token *token = cp_lexer_peek_token (parser->lexer);
12264 /* Let the semantic analysis code know that we are starting the
12265 mem-initializer-list. */
12266 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12267 error_at (token->location,
12268 "only constructors take member initializers");
12270 /* Loop through the list. */
12271 while (true)
12273 tree mem_initializer;
12275 token = cp_lexer_peek_token (parser->lexer);
12276 /* Parse the mem-initializer. */
12277 mem_initializer = cp_parser_mem_initializer (parser);
12278 /* If the next token is a `...', we're expanding member initializers. */
12279 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12281 /* Consume the `...'. */
12282 cp_lexer_consume_token (parser->lexer);
12284 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12285 can be expanded but members cannot. */
12286 if (mem_initializer != error_mark_node
12287 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12289 error_at (token->location,
12290 "cannot expand initializer for member %<%D%>",
12291 TREE_PURPOSE (mem_initializer));
12292 mem_initializer = error_mark_node;
12295 /* Construct the pack expansion type. */
12296 if (mem_initializer != error_mark_node)
12297 mem_initializer = make_pack_expansion (mem_initializer);
12299 if (target_ctor != error_mark_node
12300 && mem_initializer != error_mark_node)
12302 error ("mem-initializer for %qD follows constructor delegation",
12303 TREE_PURPOSE (mem_initializer));
12304 mem_initializer = error_mark_node;
12306 /* Look for a target constructor. */
12307 if (mem_initializer != error_mark_node
12308 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12309 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12311 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12312 if (mem_initializer_list)
12314 error ("constructor delegation follows mem-initializer for %qD",
12315 TREE_PURPOSE (mem_initializer_list));
12316 mem_initializer = error_mark_node;
12318 target_ctor = mem_initializer;
12320 /* Add it to the list, unless it was erroneous. */
12321 if (mem_initializer != error_mark_node)
12323 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12324 mem_initializer_list = mem_initializer;
12326 /* If the next token is not a `,', we're done. */
12327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12328 break;
12329 /* Consume the `,' token. */
12330 cp_lexer_consume_token (parser->lexer);
12333 /* Perform semantic analysis. */
12334 if (DECL_CONSTRUCTOR_P (current_function_decl))
12335 finish_mem_initializers (mem_initializer_list);
12338 /* Parse a mem-initializer.
12340 mem-initializer:
12341 mem-initializer-id ( expression-list [opt] )
12342 mem-initializer-id braced-init-list
12344 GNU extension:
12346 mem-initializer:
12347 ( expression-list [opt] )
12349 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12350 class) or FIELD_DECL (for a non-static data member) to initialize;
12351 the TREE_VALUE is the expression-list. An empty initialization
12352 list is represented by void_list_node. */
12354 static tree
12355 cp_parser_mem_initializer (cp_parser* parser)
12357 tree mem_initializer_id;
12358 tree expression_list;
12359 tree member;
12360 cp_token *token = cp_lexer_peek_token (parser->lexer);
12362 /* Find out what is being initialized. */
12363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12365 permerror (token->location,
12366 "anachronistic old-style base class initializer");
12367 mem_initializer_id = NULL_TREE;
12369 else
12371 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12372 if (mem_initializer_id == error_mark_node)
12373 return mem_initializer_id;
12375 member = expand_member_init (mem_initializer_id);
12376 if (member && !DECL_P (member))
12377 in_base_initializer = 1;
12379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12381 bool expr_non_constant_p;
12382 cp_lexer_set_source_position (parser->lexer);
12383 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12384 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12385 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12386 expression_list = build_tree_list (NULL_TREE, expression_list);
12388 else
12390 vec<tree, va_gc> *vec;
12391 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12392 /*cast_p=*/false,
12393 /*allow_expansion_p=*/true,
12394 /*non_constant_p=*/NULL);
12395 if (vec == NULL)
12396 return error_mark_node;
12397 expression_list = build_tree_list_vec (vec);
12398 release_tree_vector (vec);
12401 if (expression_list == error_mark_node)
12402 return error_mark_node;
12403 if (!expression_list)
12404 expression_list = void_type_node;
12406 in_base_initializer = 0;
12408 return member ? build_tree_list (member, expression_list) : error_mark_node;
12411 /* Parse a mem-initializer-id.
12413 mem-initializer-id:
12414 :: [opt] nested-name-specifier [opt] class-name
12415 identifier
12417 Returns a TYPE indicating the class to be initializer for the first
12418 production. Returns an IDENTIFIER_NODE indicating the data member
12419 to be initialized for the second production. */
12421 static tree
12422 cp_parser_mem_initializer_id (cp_parser* parser)
12424 bool global_scope_p;
12425 bool nested_name_specifier_p;
12426 bool template_p = false;
12427 tree id;
12429 cp_token *token = cp_lexer_peek_token (parser->lexer);
12431 /* `typename' is not allowed in this context ([temp.res]). */
12432 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12434 error_at (token->location,
12435 "keyword %<typename%> not allowed in this context (a qualified "
12436 "member initializer is implicitly a type)");
12437 cp_lexer_consume_token (parser->lexer);
12439 /* Look for the optional `::' operator. */
12440 global_scope_p
12441 = (cp_parser_global_scope_opt (parser,
12442 /*current_scope_valid_p=*/false)
12443 != NULL_TREE);
12444 /* Look for the optional nested-name-specifier. The simplest way to
12445 implement:
12447 [temp.res]
12449 The keyword `typename' is not permitted in a base-specifier or
12450 mem-initializer; in these contexts a qualified name that
12451 depends on a template-parameter is implicitly assumed to be a
12452 type name.
12454 is to assume that we have seen the `typename' keyword at this
12455 point. */
12456 nested_name_specifier_p
12457 = (cp_parser_nested_name_specifier_opt (parser,
12458 /*typename_keyword_p=*/true,
12459 /*check_dependency_p=*/true,
12460 /*type_p=*/true,
12461 /*is_declaration=*/true)
12462 != NULL_TREE);
12463 if (nested_name_specifier_p)
12464 template_p = cp_parser_optional_template_keyword (parser);
12465 /* If there is a `::' operator or a nested-name-specifier, then we
12466 are definitely looking for a class-name. */
12467 if (global_scope_p || nested_name_specifier_p)
12468 return cp_parser_class_name (parser,
12469 /*typename_keyword_p=*/true,
12470 /*template_keyword_p=*/template_p,
12471 typename_type,
12472 /*check_dependency_p=*/true,
12473 /*class_head_p=*/false,
12474 /*is_declaration=*/true);
12475 /* Otherwise, we could also be looking for an ordinary identifier. */
12476 cp_parser_parse_tentatively (parser);
12477 /* Try a class-name. */
12478 id = cp_parser_class_name (parser,
12479 /*typename_keyword_p=*/true,
12480 /*template_keyword_p=*/false,
12481 none_type,
12482 /*check_dependency_p=*/true,
12483 /*class_head_p=*/false,
12484 /*is_declaration=*/true);
12485 /* If we found one, we're done. */
12486 if (cp_parser_parse_definitely (parser))
12487 return id;
12488 /* Otherwise, look for an ordinary identifier. */
12489 return cp_parser_identifier (parser);
12492 /* Overloading [gram.over] */
12494 /* Parse an operator-function-id.
12496 operator-function-id:
12497 operator operator
12499 Returns an IDENTIFIER_NODE for the operator which is a
12500 human-readable spelling of the identifier, e.g., `operator +'. */
12502 static tree
12503 cp_parser_operator_function_id (cp_parser* parser)
12505 /* Look for the `operator' keyword. */
12506 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12507 return error_mark_node;
12508 /* And then the name of the operator itself. */
12509 return cp_parser_operator (parser);
12512 /* Return an identifier node for a user-defined literal operator.
12513 The suffix identifier is chained to the operator name identifier. */
12515 static tree
12516 cp_literal_operator_id (const char* name)
12518 tree identifier;
12519 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12520 + strlen (name) + 10);
12521 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12522 identifier = get_identifier (buffer);
12524 return identifier;
12527 /* Parse an operator.
12529 operator:
12530 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12531 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12532 || ++ -- , ->* -> () []
12534 GNU Extensions:
12536 operator:
12537 <? >? <?= >?=
12539 Returns an IDENTIFIER_NODE for the operator which is a
12540 human-readable spelling of the identifier, e.g., `operator +'. */
12542 static tree
12543 cp_parser_operator (cp_parser* parser)
12545 tree id = NULL_TREE;
12546 cp_token *token;
12547 bool bad_encoding_prefix = false;
12549 /* Peek at the next token. */
12550 token = cp_lexer_peek_token (parser->lexer);
12551 /* Figure out which operator we have. */
12552 switch (token->type)
12554 case CPP_KEYWORD:
12556 enum tree_code op;
12558 /* The keyword should be either `new' or `delete'. */
12559 if (token->keyword == RID_NEW)
12560 op = NEW_EXPR;
12561 else if (token->keyword == RID_DELETE)
12562 op = DELETE_EXPR;
12563 else
12564 break;
12566 /* Consume the `new' or `delete' token. */
12567 cp_lexer_consume_token (parser->lexer);
12569 /* Peek at the next token. */
12570 token = cp_lexer_peek_token (parser->lexer);
12571 /* If it's a `[' token then this is the array variant of the
12572 operator. */
12573 if (token->type == CPP_OPEN_SQUARE)
12575 /* Consume the `[' token. */
12576 cp_lexer_consume_token (parser->lexer);
12577 /* Look for the `]' token. */
12578 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12579 id = ansi_opname (op == NEW_EXPR
12580 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12582 /* Otherwise, we have the non-array variant. */
12583 else
12584 id = ansi_opname (op);
12586 return id;
12589 case CPP_PLUS:
12590 id = ansi_opname (PLUS_EXPR);
12591 break;
12593 case CPP_MINUS:
12594 id = ansi_opname (MINUS_EXPR);
12595 break;
12597 case CPP_MULT:
12598 id = ansi_opname (MULT_EXPR);
12599 break;
12601 case CPP_DIV:
12602 id = ansi_opname (TRUNC_DIV_EXPR);
12603 break;
12605 case CPP_MOD:
12606 id = ansi_opname (TRUNC_MOD_EXPR);
12607 break;
12609 case CPP_XOR:
12610 id = ansi_opname (BIT_XOR_EXPR);
12611 break;
12613 case CPP_AND:
12614 id = ansi_opname (BIT_AND_EXPR);
12615 break;
12617 case CPP_OR:
12618 id = ansi_opname (BIT_IOR_EXPR);
12619 break;
12621 case CPP_COMPL:
12622 id = ansi_opname (BIT_NOT_EXPR);
12623 break;
12625 case CPP_NOT:
12626 id = ansi_opname (TRUTH_NOT_EXPR);
12627 break;
12629 case CPP_EQ:
12630 id = ansi_assopname (NOP_EXPR);
12631 break;
12633 case CPP_LESS:
12634 id = ansi_opname (LT_EXPR);
12635 break;
12637 case CPP_GREATER:
12638 id = ansi_opname (GT_EXPR);
12639 break;
12641 case CPP_PLUS_EQ:
12642 id = ansi_assopname (PLUS_EXPR);
12643 break;
12645 case CPP_MINUS_EQ:
12646 id = ansi_assopname (MINUS_EXPR);
12647 break;
12649 case CPP_MULT_EQ:
12650 id = ansi_assopname (MULT_EXPR);
12651 break;
12653 case CPP_DIV_EQ:
12654 id = ansi_assopname (TRUNC_DIV_EXPR);
12655 break;
12657 case CPP_MOD_EQ:
12658 id = ansi_assopname (TRUNC_MOD_EXPR);
12659 break;
12661 case CPP_XOR_EQ:
12662 id = ansi_assopname (BIT_XOR_EXPR);
12663 break;
12665 case CPP_AND_EQ:
12666 id = ansi_assopname (BIT_AND_EXPR);
12667 break;
12669 case CPP_OR_EQ:
12670 id = ansi_assopname (BIT_IOR_EXPR);
12671 break;
12673 case CPP_LSHIFT:
12674 id = ansi_opname (LSHIFT_EXPR);
12675 break;
12677 case CPP_RSHIFT:
12678 id = ansi_opname (RSHIFT_EXPR);
12679 break;
12681 case CPP_LSHIFT_EQ:
12682 id = ansi_assopname (LSHIFT_EXPR);
12683 break;
12685 case CPP_RSHIFT_EQ:
12686 id = ansi_assopname (RSHIFT_EXPR);
12687 break;
12689 case CPP_EQ_EQ:
12690 id = ansi_opname (EQ_EXPR);
12691 break;
12693 case CPP_NOT_EQ:
12694 id = ansi_opname (NE_EXPR);
12695 break;
12697 case CPP_LESS_EQ:
12698 id = ansi_opname (LE_EXPR);
12699 break;
12701 case CPP_GREATER_EQ:
12702 id = ansi_opname (GE_EXPR);
12703 break;
12705 case CPP_AND_AND:
12706 id = ansi_opname (TRUTH_ANDIF_EXPR);
12707 break;
12709 case CPP_OR_OR:
12710 id = ansi_opname (TRUTH_ORIF_EXPR);
12711 break;
12713 case CPP_PLUS_PLUS:
12714 id = ansi_opname (POSTINCREMENT_EXPR);
12715 break;
12717 case CPP_MINUS_MINUS:
12718 id = ansi_opname (PREDECREMENT_EXPR);
12719 break;
12721 case CPP_COMMA:
12722 id = ansi_opname (COMPOUND_EXPR);
12723 break;
12725 case CPP_DEREF_STAR:
12726 id = ansi_opname (MEMBER_REF);
12727 break;
12729 case CPP_DEREF:
12730 id = ansi_opname (COMPONENT_REF);
12731 break;
12733 case CPP_OPEN_PAREN:
12734 /* Consume the `('. */
12735 cp_lexer_consume_token (parser->lexer);
12736 /* Look for the matching `)'. */
12737 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12738 return ansi_opname (CALL_EXPR);
12740 case CPP_OPEN_SQUARE:
12741 /* Consume the `['. */
12742 cp_lexer_consume_token (parser->lexer);
12743 /* Look for the matching `]'. */
12744 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12745 return ansi_opname (ARRAY_REF);
12747 case CPP_WSTRING:
12748 case CPP_STRING16:
12749 case CPP_STRING32:
12750 case CPP_UTF8STRING:
12751 bad_encoding_prefix = true;
12752 /* Fall through. */
12754 case CPP_STRING:
12755 if (cxx_dialect == cxx98)
12756 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12757 if (bad_encoding_prefix)
12759 error ("invalid encoding prefix in literal operator");
12760 return error_mark_node;
12762 if (TREE_STRING_LENGTH (token->u.value) > 2)
12764 error ("expected empty string after %<operator%> keyword");
12765 return error_mark_node;
12767 /* Consume the string. */
12768 cp_lexer_consume_token (parser->lexer);
12769 /* Look for the suffix identifier. */
12770 token = cp_lexer_peek_token (parser->lexer);
12771 if (token->type == CPP_NAME)
12773 id = cp_parser_identifier (parser);
12774 if (id != error_mark_node)
12776 const char *name = IDENTIFIER_POINTER (id);
12777 return cp_literal_operator_id (name);
12780 else if (token->type == CPP_KEYWORD)
12782 error ("unexpected keyword;"
12783 " remove space between quotes and suffix identifier");
12784 return error_mark_node;
12786 else
12788 error ("expected suffix identifier");
12789 return error_mark_node;
12792 case CPP_WSTRING_USERDEF:
12793 case CPP_STRING16_USERDEF:
12794 case CPP_STRING32_USERDEF:
12795 case CPP_UTF8STRING_USERDEF:
12796 bad_encoding_prefix = true;
12797 /* Fall through. */
12799 case CPP_STRING_USERDEF:
12800 if (cxx_dialect == cxx98)
12801 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12802 if (bad_encoding_prefix)
12804 error ("invalid encoding prefix in literal operator");
12805 return error_mark_node;
12808 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12809 if (TREE_STRING_LENGTH (string_tree) > 2)
12811 error ("expected empty string after %<operator%> keyword");
12812 return error_mark_node;
12814 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12815 /* Consume the user-defined string literal. */
12816 cp_lexer_consume_token (parser->lexer);
12817 if (id != error_mark_node)
12819 const char *name = IDENTIFIER_POINTER (id);
12820 return cp_literal_operator_id (name);
12822 else
12823 return error_mark_node;
12826 default:
12827 /* Anything else is an error. */
12828 break;
12831 /* If we have selected an identifier, we need to consume the
12832 operator token. */
12833 if (id)
12834 cp_lexer_consume_token (parser->lexer);
12835 /* Otherwise, no valid operator name was present. */
12836 else
12838 cp_parser_error (parser, "expected operator");
12839 id = error_mark_node;
12842 return id;
12845 /* Parse a template-declaration.
12847 template-declaration:
12848 export [opt] template < template-parameter-list > declaration
12850 If MEMBER_P is TRUE, this template-declaration occurs within a
12851 class-specifier.
12853 The grammar rule given by the standard isn't correct. What
12854 is really meant is:
12856 template-declaration:
12857 export [opt] template-parameter-list-seq
12858 decl-specifier-seq [opt] init-declarator [opt] ;
12859 export [opt] template-parameter-list-seq
12860 function-definition
12862 template-parameter-list-seq:
12863 template-parameter-list-seq [opt]
12864 template < template-parameter-list > */
12866 static void
12867 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12869 /* Check for `export'. */
12870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12872 /* Consume the `export' token. */
12873 cp_lexer_consume_token (parser->lexer);
12874 /* Warn that we do not support `export'. */
12875 warning (0, "keyword %<export%> not implemented, and will be ignored");
12878 cp_parser_template_declaration_after_export (parser, member_p);
12881 /* Parse a template-parameter-list.
12883 template-parameter-list:
12884 template-parameter
12885 template-parameter-list , template-parameter
12887 Returns a TREE_LIST. Each node represents a template parameter.
12888 The nodes are connected via their TREE_CHAINs. */
12890 static tree
12891 cp_parser_template_parameter_list (cp_parser* parser)
12893 tree parameter_list = NULL_TREE;
12895 begin_template_parm_list ();
12897 /* The loop below parses the template parms. We first need to know
12898 the total number of template parms to be able to compute proper
12899 canonical types of each dependent type. So after the loop, when
12900 we know the total number of template parms,
12901 end_template_parm_list computes the proper canonical types and
12902 fixes up the dependent types accordingly. */
12903 while (true)
12905 tree parameter;
12906 bool is_non_type;
12907 bool is_parameter_pack;
12908 location_t parm_loc;
12910 /* Parse the template-parameter. */
12911 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12912 parameter = cp_parser_template_parameter (parser,
12913 &is_non_type,
12914 &is_parameter_pack);
12915 /* Add it to the list. */
12916 if (parameter != error_mark_node)
12917 parameter_list = process_template_parm (parameter_list,
12918 parm_loc,
12919 parameter,
12920 is_non_type,
12921 is_parameter_pack);
12922 else
12924 tree err_parm = build_tree_list (parameter, parameter);
12925 parameter_list = chainon (parameter_list, err_parm);
12928 /* If the next token is not a `,', we're done. */
12929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12930 break;
12931 /* Otherwise, consume the `,' token. */
12932 cp_lexer_consume_token (parser->lexer);
12935 return end_template_parm_list (parameter_list);
12938 /* Parse a template-parameter.
12940 template-parameter:
12941 type-parameter
12942 parameter-declaration
12944 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12945 the parameter. The TREE_PURPOSE is the default value, if any.
12946 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12947 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12948 set to true iff this parameter is a parameter pack. */
12950 static tree
12951 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12952 bool *is_parameter_pack)
12954 cp_token *token;
12955 cp_parameter_declarator *parameter_declarator;
12956 cp_declarator *id_declarator;
12957 tree parm;
12959 /* Assume it is a type parameter or a template parameter. */
12960 *is_non_type = false;
12961 /* Assume it not a parameter pack. */
12962 *is_parameter_pack = false;
12963 /* Peek at the next token. */
12964 token = cp_lexer_peek_token (parser->lexer);
12965 /* If it is `class' or `template', we have a type-parameter. */
12966 if (token->keyword == RID_TEMPLATE)
12967 return cp_parser_type_parameter (parser, is_parameter_pack);
12968 /* If it is `class' or `typename' we do not know yet whether it is a
12969 type parameter or a non-type parameter. Consider:
12971 template <typename T, typename T::X X> ...
12975 template <class C, class D*> ...
12977 Here, the first parameter is a type parameter, and the second is
12978 a non-type parameter. We can tell by looking at the token after
12979 the identifier -- if it is a `,', `=', or `>' then we have a type
12980 parameter. */
12981 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12983 /* Peek at the token after `class' or `typename'. */
12984 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12985 /* If it's an ellipsis, we have a template type parameter
12986 pack. */
12987 if (token->type == CPP_ELLIPSIS)
12988 return cp_parser_type_parameter (parser, is_parameter_pack);
12989 /* If it's an identifier, skip it. */
12990 if (token->type == CPP_NAME)
12991 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12992 /* Now, see if the token looks like the end of a template
12993 parameter. */
12994 if (token->type == CPP_COMMA
12995 || token->type == CPP_EQ
12996 || token->type == CPP_GREATER)
12997 return cp_parser_type_parameter (parser, is_parameter_pack);
13000 /* Otherwise, it is a non-type parameter.
13002 [temp.param]
13004 When parsing a default template-argument for a non-type
13005 template-parameter, the first non-nested `>' is taken as the end
13006 of the template parameter-list rather than a greater-than
13007 operator. */
13008 *is_non_type = true;
13009 parameter_declarator
13010 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13011 /*parenthesized_p=*/NULL);
13013 if (!parameter_declarator)
13014 return error_mark_node;
13016 /* If the parameter declaration is marked as a parameter pack, set
13017 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13018 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13019 grokdeclarator. */
13020 if (parameter_declarator->declarator
13021 && parameter_declarator->declarator->parameter_pack_p)
13023 *is_parameter_pack = true;
13024 parameter_declarator->declarator->parameter_pack_p = false;
13027 if (parameter_declarator->default_argument)
13029 /* Can happen in some cases of erroneous input (c++/34892). */
13030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13031 /* Consume the `...' for better error recovery. */
13032 cp_lexer_consume_token (parser->lexer);
13034 /* If the next token is an ellipsis, and we don't already have it
13035 marked as a parameter pack, then we have a parameter pack (that
13036 has no declarator). */
13037 else if (!*is_parameter_pack
13038 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13039 && (declarator_can_be_parameter_pack
13040 (parameter_declarator->declarator)))
13042 /* Consume the `...'. */
13043 cp_lexer_consume_token (parser->lexer);
13044 maybe_warn_variadic_templates ();
13046 *is_parameter_pack = true;
13048 /* We might end up with a pack expansion as the type of the non-type
13049 template parameter, in which case this is a non-type template
13050 parameter pack. */
13051 else if (parameter_declarator->decl_specifiers.type
13052 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13054 *is_parameter_pack = true;
13055 parameter_declarator->decl_specifiers.type =
13056 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13059 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13061 /* Parameter packs cannot have default arguments. However, a
13062 user may try to do so, so we'll parse them and give an
13063 appropriate diagnostic here. */
13065 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13067 /* Find the name of the parameter pack. */
13068 id_declarator = parameter_declarator->declarator;
13069 while (id_declarator && id_declarator->kind != cdk_id)
13070 id_declarator = id_declarator->declarator;
13072 if (id_declarator && id_declarator->kind == cdk_id)
13073 error_at (start_token->location,
13074 "template parameter pack %qD cannot have a default argument",
13075 id_declarator->u.id.unqualified_name);
13076 else
13077 error_at (start_token->location,
13078 "template parameter pack cannot have a default argument");
13080 /* Parse the default argument, but throw away the result. */
13081 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13084 parm = grokdeclarator (parameter_declarator->declarator,
13085 &parameter_declarator->decl_specifiers,
13086 TPARM, /*initialized=*/0,
13087 /*attrlist=*/NULL);
13088 if (parm == error_mark_node)
13089 return error_mark_node;
13091 return build_tree_list (parameter_declarator->default_argument, parm);
13094 /* Parse a type-parameter.
13096 type-parameter:
13097 class identifier [opt]
13098 class identifier [opt] = type-id
13099 typename identifier [opt]
13100 typename identifier [opt] = type-id
13101 template < template-parameter-list > class identifier [opt]
13102 template < template-parameter-list > class identifier [opt]
13103 = id-expression
13105 GNU Extension (variadic templates):
13107 type-parameter:
13108 class ... identifier [opt]
13109 typename ... identifier [opt]
13111 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13112 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13113 the declaration of the parameter.
13115 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13117 static tree
13118 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13120 cp_token *token;
13121 tree parameter;
13123 /* Look for a keyword to tell us what kind of parameter this is. */
13124 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13125 if (!token)
13126 return error_mark_node;
13128 switch (token->keyword)
13130 case RID_CLASS:
13131 case RID_TYPENAME:
13133 tree identifier;
13134 tree default_argument;
13136 /* If the next token is an ellipsis, we have a template
13137 argument pack. */
13138 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13140 /* Consume the `...' token. */
13141 cp_lexer_consume_token (parser->lexer);
13142 maybe_warn_variadic_templates ();
13144 *is_parameter_pack = true;
13147 /* If the next token is an identifier, then it names the
13148 parameter. */
13149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13150 identifier = cp_parser_identifier (parser);
13151 else
13152 identifier = NULL_TREE;
13154 /* Create the parameter. */
13155 parameter = finish_template_type_parm (class_type_node, identifier);
13157 /* If the next token is an `=', we have a default argument. */
13158 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13160 /* Consume the `=' token. */
13161 cp_lexer_consume_token (parser->lexer);
13162 /* Parse the default-argument. */
13163 push_deferring_access_checks (dk_no_deferred);
13164 default_argument = cp_parser_type_id (parser);
13166 /* Template parameter packs cannot have default
13167 arguments. */
13168 if (*is_parameter_pack)
13170 if (identifier)
13171 error_at (token->location,
13172 "template parameter pack %qD cannot have a "
13173 "default argument", identifier);
13174 else
13175 error_at (token->location,
13176 "template parameter packs cannot have "
13177 "default arguments");
13178 default_argument = NULL_TREE;
13180 pop_deferring_access_checks ();
13182 else
13183 default_argument = NULL_TREE;
13185 /* Create the combined representation of the parameter and the
13186 default argument. */
13187 parameter = build_tree_list (default_argument, parameter);
13189 break;
13191 case RID_TEMPLATE:
13193 tree identifier;
13194 tree default_argument;
13196 /* Look for the `<'. */
13197 cp_parser_require (parser, CPP_LESS, RT_LESS);
13198 /* Parse the template-parameter-list. */
13199 cp_parser_template_parameter_list (parser);
13200 /* Look for the `>'. */
13201 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13202 /* Look for the `class' keyword. */
13203 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13204 /* If the next token is an ellipsis, we have a template
13205 argument pack. */
13206 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13208 /* Consume the `...' token. */
13209 cp_lexer_consume_token (parser->lexer);
13210 maybe_warn_variadic_templates ();
13212 *is_parameter_pack = true;
13214 /* If the next token is an `=', then there is a
13215 default-argument. If the next token is a `>', we are at
13216 the end of the parameter-list. If the next token is a `,',
13217 then we are at the end of this parameter. */
13218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13219 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13220 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13222 identifier = cp_parser_identifier (parser);
13223 /* Treat invalid names as if the parameter were nameless. */
13224 if (identifier == error_mark_node)
13225 identifier = NULL_TREE;
13227 else
13228 identifier = NULL_TREE;
13230 /* Create the template parameter. */
13231 parameter = finish_template_template_parm (class_type_node,
13232 identifier);
13234 /* If the next token is an `=', then there is a
13235 default-argument. */
13236 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13238 bool is_template;
13240 /* Consume the `='. */
13241 cp_lexer_consume_token (parser->lexer);
13242 /* Parse the id-expression. */
13243 push_deferring_access_checks (dk_no_deferred);
13244 /* save token before parsing the id-expression, for error
13245 reporting */
13246 token = cp_lexer_peek_token (parser->lexer);
13247 default_argument
13248 = cp_parser_id_expression (parser,
13249 /*template_keyword_p=*/false,
13250 /*check_dependency_p=*/true,
13251 /*template_p=*/&is_template,
13252 /*declarator_p=*/false,
13253 /*optional_p=*/false);
13254 if (TREE_CODE (default_argument) == TYPE_DECL)
13255 /* If the id-expression was a template-id that refers to
13256 a template-class, we already have the declaration here,
13257 so no further lookup is needed. */
13259 else
13260 /* Look up the name. */
13261 default_argument
13262 = cp_parser_lookup_name (parser, default_argument,
13263 none_type,
13264 /*is_template=*/is_template,
13265 /*is_namespace=*/false,
13266 /*check_dependency=*/true,
13267 /*ambiguous_decls=*/NULL,
13268 token->location);
13269 /* See if the default argument is valid. */
13270 default_argument
13271 = check_template_template_default_arg (default_argument);
13273 /* Template parameter packs cannot have default
13274 arguments. */
13275 if (*is_parameter_pack)
13277 if (identifier)
13278 error_at (token->location,
13279 "template parameter pack %qD cannot "
13280 "have a default argument",
13281 identifier);
13282 else
13283 error_at (token->location, "template parameter packs cannot "
13284 "have default arguments");
13285 default_argument = NULL_TREE;
13287 pop_deferring_access_checks ();
13289 else
13290 default_argument = NULL_TREE;
13292 /* Create the combined representation of the parameter and the
13293 default argument. */
13294 parameter = build_tree_list (default_argument, parameter);
13296 break;
13298 default:
13299 gcc_unreachable ();
13300 break;
13303 return parameter;
13306 /* Parse a template-id.
13308 template-id:
13309 template-name < template-argument-list [opt] >
13311 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13312 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13313 returned. Otherwise, if the template-name names a function, or set
13314 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13315 names a class, returns a TYPE_DECL for the specialization.
13317 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13318 uninstantiated templates. */
13320 static tree
13321 cp_parser_template_id (cp_parser *parser,
13322 bool template_keyword_p,
13323 bool check_dependency_p,
13324 enum tag_types tag_type,
13325 bool is_declaration)
13327 int i;
13328 tree templ;
13329 tree arguments;
13330 tree template_id;
13331 cp_token_position start_of_id = 0;
13332 deferred_access_check *chk;
13333 vec<deferred_access_check, va_gc> *access_check;
13334 cp_token *next_token = NULL, *next_token_2 = NULL;
13335 bool is_identifier;
13337 /* If the next token corresponds to a template-id, there is no need
13338 to reparse it. */
13339 next_token = cp_lexer_peek_token (parser->lexer);
13340 if (next_token->type == CPP_TEMPLATE_ID)
13342 struct tree_check *check_value;
13344 /* Get the stored value. */
13345 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13346 /* Perform any access checks that were deferred. */
13347 access_check = check_value->checks;
13348 if (access_check)
13350 FOR_EACH_VEC_ELT (*access_check, i, chk)
13351 perform_or_defer_access_check (chk->binfo,
13352 chk->decl,
13353 chk->diag_decl,
13354 tf_warning_or_error);
13356 /* Return the stored value. */
13357 return check_value->value;
13360 /* Avoid performing name lookup if there is no possibility of
13361 finding a template-id. */
13362 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13363 || (next_token->type == CPP_NAME
13364 && !cp_parser_nth_token_starts_template_argument_list_p
13365 (parser, 2)))
13367 cp_parser_error (parser, "expected template-id");
13368 return error_mark_node;
13371 /* Remember where the template-id starts. */
13372 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13373 start_of_id = cp_lexer_token_position (parser->lexer, false);
13375 push_deferring_access_checks (dk_deferred);
13377 /* Parse the template-name. */
13378 is_identifier = false;
13379 templ = cp_parser_template_name (parser, template_keyword_p,
13380 check_dependency_p,
13381 is_declaration,
13382 tag_type,
13383 &is_identifier);
13384 if (templ == error_mark_node || is_identifier)
13386 pop_deferring_access_checks ();
13387 return templ;
13390 /* If we find the sequence `[:' after a template-name, it's probably
13391 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13392 parse correctly the argument list. */
13393 next_token = cp_lexer_peek_token (parser->lexer);
13394 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13395 if (next_token->type == CPP_OPEN_SQUARE
13396 && next_token->flags & DIGRAPH
13397 && next_token_2->type == CPP_COLON
13398 && !(next_token_2->flags & PREV_WHITE))
13400 cp_parser_parse_tentatively (parser);
13401 /* Change `:' into `::'. */
13402 next_token_2->type = CPP_SCOPE;
13403 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13404 CPP_LESS. */
13405 cp_lexer_consume_token (parser->lexer);
13407 /* Parse the arguments. */
13408 arguments = cp_parser_enclosed_template_argument_list (parser);
13409 if (!cp_parser_parse_definitely (parser))
13411 /* If we couldn't parse an argument list, then we revert our changes
13412 and return simply an error. Maybe this is not a template-id
13413 after all. */
13414 next_token_2->type = CPP_COLON;
13415 cp_parser_error (parser, "expected %<<%>");
13416 pop_deferring_access_checks ();
13417 return error_mark_node;
13419 /* Otherwise, emit an error about the invalid digraph, but continue
13420 parsing because we got our argument list. */
13421 if (permerror (next_token->location,
13422 "%<<::%> cannot begin a template-argument list"))
13424 static bool hint = false;
13425 inform (next_token->location,
13426 "%<<:%> is an alternate spelling for %<[%>."
13427 " Insert whitespace between %<<%> and %<::%>");
13428 if (!hint && !flag_permissive)
13430 inform (next_token->location, "(if you use %<-fpermissive%> "
13431 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13432 "accept your code)");
13433 hint = true;
13437 else
13439 /* Look for the `<' that starts the template-argument-list. */
13440 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13442 pop_deferring_access_checks ();
13443 return error_mark_node;
13445 /* Parse the arguments. */
13446 arguments = cp_parser_enclosed_template_argument_list (parser);
13449 /* Build a representation of the specialization. */
13450 if (identifier_p (templ))
13451 template_id = build_min_nt_loc (next_token->location,
13452 TEMPLATE_ID_EXPR,
13453 templ, arguments);
13454 else if (DECL_TYPE_TEMPLATE_P (templ)
13455 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13457 bool entering_scope;
13458 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13459 template (rather than some instantiation thereof) only if
13460 is not nested within some other construct. For example, in
13461 "template <typename T> void f(T) { A<T>::", A<T> is just an
13462 instantiation of A. */
13463 entering_scope = (template_parm_scope_p ()
13464 && cp_lexer_next_token_is (parser->lexer,
13465 CPP_SCOPE));
13466 template_id
13467 = finish_template_type (templ, arguments, entering_scope);
13469 else
13471 /* If it's not a class-template or a template-template, it should be
13472 a function-template. */
13473 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13474 || TREE_CODE (templ) == OVERLOAD
13475 || BASELINK_P (templ)));
13477 template_id = lookup_template_function (templ, arguments);
13480 /* If parsing tentatively, replace the sequence of tokens that makes
13481 up the template-id with a CPP_TEMPLATE_ID token. That way,
13482 should we re-parse the token stream, we will not have to repeat
13483 the effort required to do the parse, nor will we issue duplicate
13484 error messages about problems during instantiation of the
13485 template. */
13486 if (start_of_id
13487 /* Don't do this if we had a parse error in a declarator; re-parsing
13488 might succeed if a name changes meaning (60361). */
13489 && !(cp_parser_error_occurred (parser)
13490 && cp_parser_parsing_tentatively (parser)
13491 && parser->in_declarator_p))
13493 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13495 /* Reset the contents of the START_OF_ID token. */
13496 token->type = CPP_TEMPLATE_ID;
13497 /* Retrieve any deferred checks. Do not pop this access checks yet
13498 so the memory will not be reclaimed during token replacing below. */
13499 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13500 token->u.tree_check_value->value = template_id;
13501 token->u.tree_check_value->checks = get_deferred_access_checks ();
13502 token->keyword = RID_MAX;
13504 /* Purge all subsequent tokens. */
13505 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13507 /* ??? Can we actually assume that, if template_id ==
13508 error_mark_node, we will have issued a diagnostic to the
13509 user, as opposed to simply marking the tentative parse as
13510 failed? */
13511 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13512 error_at (token->location, "parse error in template argument list");
13515 pop_to_parent_deferring_access_checks ();
13516 return template_id;
13519 /* Parse a template-name.
13521 template-name:
13522 identifier
13524 The standard should actually say:
13526 template-name:
13527 identifier
13528 operator-function-id
13530 A defect report has been filed about this issue.
13532 A conversion-function-id cannot be a template name because they cannot
13533 be part of a template-id. In fact, looking at this code:
13535 a.operator K<int>()
13537 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13538 It is impossible to call a templated conversion-function-id with an
13539 explicit argument list, since the only allowed template parameter is
13540 the type to which it is converting.
13542 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13543 `template' keyword, in a construction like:
13545 T::template f<3>()
13547 In that case `f' is taken to be a template-name, even though there
13548 is no way of knowing for sure.
13550 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13551 name refers to a set of overloaded functions, at least one of which
13552 is a template, or an IDENTIFIER_NODE with the name of the template,
13553 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13554 names are looked up inside uninstantiated templates. */
13556 static tree
13557 cp_parser_template_name (cp_parser* parser,
13558 bool template_keyword_p,
13559 bool check_dependency_p,
13560 bool is_declaration,
13561 enum tag_types tag_type,
13562 bool *is_identifier)
13564 tree identifier;
13565 tree decl;
13566 tree fns;
13567 cp_token *token = cp_lexer_peek_token (parser->lexer);
13569 /* If the next token is `operator', then we have either an
13570 operator-function-id or a conversion-function-id. */
13571 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13573 /* We don't know whether we're looking at an
13574 operator-function-id or a conversion-function-id. */
13575 cp_parser_parse_tentatively (parser);
13576 /* Try an operator-function-id. */
13577 identifier = cp_parser_operator_function_id (parser);
13578 /* If that didn't work, try a conversion-function-id. */
13579 if (!cp_parser_parse_definitely (parser))
13581 cp_parser_error (parser, "expected template-name");
13582 return error_mark_node;
13585 /* Look for the identifier. */
13586 else
13587 identifier = cp_parser_identifier (parser);
13589 /* If we didn't find an identifier, we don't have a template-id. */
13590 if (identifier == error_mark_node)
13591 return error_mark_node;
13593 /* If the name immediately followed the `template' keyword, then it
13594 is a template-name. However, if the next token is not `<', then
13595 we do not treat it as a template-name, since it is not being used
13596 as part of a template-id. This enables us to handle constructs
13597 like:
13599 template <typename T> struct S { S(); };
13600 template <typename T> S<T>::S();
13602 correctly. We would treat `S' as a template -- if it were `S<T>'
13603 -- but we do not if there is no `<'. */
13605 if (processing_template_decl
13606 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13608 /* In a declaration, in a dependent context, we pretend that the
13609 "template" keyword was present in order to improve error
13610 recovery. For example, given:
13612 template <typename T> void f(T::X<int>);
13614 we want to treat "X<int>" as a template-id. */
13615 if (is_declaration
13616 && !template_keyword_p
13617 && parser->scope && TYPE_P (parser->scope)
13618 && check_dependency_p
13619 && dependent_scope_p (parser->scope)
13620 /* Do not do this for dtors (or ctors), since they never
13621 need the template keyword before their name. */
13622 && !constructor_name_p (identifier, parser->scope))
13624 cp_token_position start = 0;
13626 /* Explain what went wrong. */
13627 error_at (token->location, "non-template %qD used as template",
13628 identifier);
13629 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13630 parser->scope, identifier);
13631 /* If parsing tentatively, find the location of the "<" token. */
13632 if (cp_parser_simulate_error (parser))
13633 start = cp_lexer_token_position (parser->lexer, true);
13634 /* Parse the template arguments so that we can issue error
13635 messages about them. */
13636 cp_lexer_consume_token (parser->lexer);
13637 cp_parser_enclosed_template_argument_list (parser);
13638 /* Skip tokens until we find a good place from which to
13639 continue parsing. */
13640 cp_parser_skip_to_closing_parenthesis (parser,
13641 /*recovering=*/true,
13642 /*or_comma=*/true,
13643 /*consume_paren=*/false);
13644 /* If parsing tentatively, permanently remove the
13645 template argument list. That will prevent duplicate
13646 error messages from being issued about the missing
13647 "template" keyword. */
13648 if (start)
13649 cp_lexer_purge_tokens_after (parser->lexer, start);
13650 if (is_identifier)
13651 *is_identifier = true;
13652 return identifier;
13655 /* If the "template" keyword is present, then there is generally
13656 no point in doing name-lookup, so we just return IDENTIFIER.
13657 But, if the qualifying scope is non-dependent then we can
13658 (and must) do name-lookup normally. */
13659 if (template_keyword_p
13660 && (!parser->scope
13661 || (TYPE_P (parser->scope)
13662 && dependent_type_p (parser->scope))))
13663 return identifier;
13666 /* Look up the name. */
13667 decl = cp_parser_lookup_name (parser, identifier,
13668 tag_type,
13669 /*is_template=*/true,
13670 /*is_namespace=*/false,
13671 check_dependency_p,
13672 /*ambiguous_decls=*/NULL,
13673 token->location);
13675 /* If DECL is a template, then the name was a template-name. */
13676 if (TREE_CODE (decl) == TEMPLATE_DECL)
13678 else
13680 tree fn = NULL_TREE;
13682 /* The standard does not explicitly indicate whether a name that
13683 names a set of overloaded declarations, some of which are
13684 templates, is a template-name. However, such a name should
13685 be a template-name; otherwise, there is no way to form a
13686 template-id for the overloaded templates. */
13687 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13688 if (TREE_CODE (fns) == OVERLOAD)
13689 for (fn = fns; fn; fn = OVL_NEXT (fn))
13690 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13691 break;
13693 if (!fn)
13695 /* The name does not name a template. */
13696 cp_parser_error (parser, "expected template-name");
13697 return error_mark_node;
13701 /* If DECL is dependent, and refers to a function, then just return
13702 its name; we will look it up again during template instantiation. */
13703 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13705 tree scope = ovl_scope (decl);
13706 if (TYPE_P (scope) && dependent_type_p (scope))
13707 return identifier;
13710 return decl;
13713 /* Parse a template-argument-list.
13715 template-argument-list:
13716 template-argument ... [opt]
13717 template-argument-list , template-argument ... [opt]
13719 Returns a TREE_VEC containing the arguments. */
13721 static tree
13722 cp_parser_template_argument_list (cp_parser* parser)
13724 tree fixed_args[10];
13725 unsigned n_args = 0;
13726 unsigned alloced = 10;
13727 tree *arg_ary = fixed_args;
13728 tree vec;
13729 bool saved_in_template_argument_list_p;
13730 bool saved_ice_p;
13731 bool saved_non_ice_p;
13733 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13734 parser->in_template_argument_list_p = true;
13735 /* Even if the template-id appears in an integral
13736 constant-expression, the contents of the argument list do
13737 not. */
13738 saved_ice_p = parser->integral_constant_expression_p;
13739 parser->integral_constant_expression_p = false;
13740 saved_non_ice_p = parser->non_integral_constant_expression_p;
13741 parser->non_integral_constant_expression_p = false;
13743 /* Parse the arguments. */
13746 tree argument;
13748 if (n_args)
13749 /* Consume the comma. */
13750 cp_lexer_consume_token (parser->lexer);
13752 /* Parse the template-argument. */
13753 argument = cp_parser_template_argument (parser);
13755 /* If the next token is an ellipsis, we're expanding a template
13756 argument pack. */
13757 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13759 if (argument == error_mark_node)
13761 cp_token *token = cp_lexer_peek_token (parser->lexer);
13762 error_at (token->location,
13763 "expected parameter pack before %<...%>");
13765 /* Consume the `...' token. */
13766 cp_lexer_consume_token (parser->lexer);
13768 /* Make the argument into a TYPE_PACK_EXPANSION or
13769 EXPR_PACK_EXPANSION. */
13770 argument = make_pack_expansion (argument);
13773 if (n_args == alloced)
13775 alloced *= 2;
13777 if (arg_ary == fixed_args)
13779 arg_ary = XNEWVEC (tree, alloced);
13780 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13782 else
13783 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13785 arg_ary[n_args++] = argument;
13787 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13789 vec = make_tree_vec (n_args);
13791 while (n_args--)
13792 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13794 if (arg_ary != fixed_args)
13795 free (arg_ary);
13796 parser->non_integral_constant_expression_p = saved_non_ice_p;
13797 parser->integral_constant_expression_p = saved_ice_p;
13798 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13799 #ifdef ENABLE_CHECKING
13800 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13801 #endif
13802 return vec;
13805 /* Parse a template-argument.
13807 template-argument:
13808 assignment-expression
13809 type-id
13810 id-expression
13812 The representation is that of an assignment-expression, type-id, or
13813 id-expression -- except that the qualified id-expression is
13814 evaluated, so that the value returned is either a DECL or an
13815 OVERLOAD.
13817 Although the standard says "assignment-expression", it forbids
13818 throw-expressions or assignments in the template argument.
13819 Therefore, we use "conditional-expression" instead. */
13821 static tree
13822 cp_parser_template_argument (cp_parser* parser)
13824 tree argument;
13825 bool template_p;
13826 bool address_p;
13827 bool maybe_type_id = false;
13828 cp_token *token = NULL, *argument_start_token = NULL;
13829 location_t loc = 0;
13830 cp_id_kind idk;
13832 /* There's really no way to know what we're looking at, so we just
13833 try each alternative in order.
13835 [temp.arg]
13837 In a template-argument, an ambiguity between a type-id and an
13838 expression is resolved to a type-id, regardless of the form of
13839 the corresponding template-parameter.
13841 Therefore, we try a type-id first. */
13842 cp_parser_parse_tentatively (parser);
13843 argument = cp_parser_template_type_arg (parser);
13844 /* If there was no error parsing the type-id but the next token is a
13845 '>>', our behavior depends on which dialect of C++ we're
13846 parsing. In C++98, we probably found a typo for '> >'. But there
13847 are type-id which are also valid expressions. For instance:
13849 struct X { int operator >> (int); };
13850 template <int V> struct Foo {};
13851 Foo<X () >> 5> r;
13853 Here 'X()' is a valid type-id of a function type, but the user just
13854 wanted to write the expression "X() >> 5". Thus, we remember that we
13855 found a valid type-id, but we still try to parse the argument as an
13856 expression to see what happens.
13858 In C++0x, the '>>' will be considered two separate '>'
13859 tokens. */
13860 if (!cp_parser_error_occurred (parser)
13861 && cxx_dialect == cxx98
13862 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13864 maybe_type_id = true;
13865 cp_parser_abort_tentative_parse (parser);
13867 else
13869 /* If the next token isn't a `,' or a `>', then this argument wasn't
13870 really finished. This means that the argument is not a valid
13871 type-id. */
13872 if (!cp_parser_next_token_ends_template_argument_p (parser))
13873 cp_parser_error (parser, "expected template-argument");
13874 /* If that worked, we're done. */
13875 if (cp_parser_parse_definitely (parser))
13876 return argument;
13878 /* We're still not sure what the argument will be. */
13879 cp_parser_parse_tentatively (parser);
13880 /* Try a template. */
13881 argument_start_token = cp_lexer_peek_token (parser->lexer);
13882 argument = cp_parser_id_expression (parser,
13883 /*template_keyword_p=*/false,
13884 /*check_dependency_p=*/true,
13885 &template_p,
13886 /*declarator_p=*/false,
13887 /*optional_p=*/false);
13888 /* If the next token isn't a `,' or a `>', then this argument wasn't
13889 really finished. */
13890 if (!cp_parser_next_token_ends_template_argument_p (parser))
13891 cp_parser_error (parser, "expected template-argument");
13892 if (!cp_parser_error_occurred (parser))
13894 /* Figure out what is being referred to. If the id-expression
13895 was for a class template specialization, then we will have a
13896 TYPE_DECL at this point. There is no need to do name lookup
13897 at this point in that case. */
13898 if (TREE_CODE (argument) != TYPE_DECL)
13899 argument = cp_parser_lookup_name (parser, argument,
13900 none_type,
13901 /*is_template=*/template_p,
13902 /*is_namespace=*/false,
13903 /*check_dependency=*/true,
13904 /*ambiguous_decls=*/NULL,
13905 argument_start_token->location);
13906 if (TREE_CODE (argument) != TEMPLATE_DECL
13907 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13908 cp_parser_error (parser, "expected template-name");
13910 if (cp_parser_parse_definitely (parser))
13911 return argument;
13912 /* It must be a non-type argument. There permitted cases are given
13913 in [temp.arg.nontype]:
13915 -- an integral constant-expression of integral or enumeration
13916 type; or
13918 -- the name of a non-type template-parameter; or
13920 -- the name of an object or function with external linkage...
13922 -- the address of an object or function with external linkage...
13924 -- a pointer to member... */
13925 /* Look for a non-type template parameter. */
13926 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13928 cp_parser_parse_tentatively (parser);
13929 argument = cp_parser_primary_expression (parser,
13930 /*address_p=*/false,
13931 /*cast_p=*/false,
13932 /*template_arg_p=*/true,
13933 &idk);
13934 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13935 || !cp_parser_next_token_ends_template_argument_p (parser))
13936 cp_parser_simulate_error (parser);
13937 if (cp_parser_parse_definitely (parser))
13938 return argument;
13941 /* If the next token is "&", the argument must be the address of an
13942 object or function with external linkage. */
13943 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13944 if (address_p)
13946 loc = cp_lexer_peek_token (parser->lexer)->location;
13947 cp_lexer_consume_token (parser->lexer);
13949 /* See if we might have an id-expression. */
13950 token = cp_lexer_peek_token (parser->lexer);
13951 if (token->type == CPP_NAME
13952 || token->keyword == RID_OPERATOR
13953 || token->type == CPP_SCOPE
13954 || token->type == CPP_TEMPLATE_ID
13955 || token->type == CPP_NESTED_NAME_SPECIFIER)
13957 cp_parser_parse_tentatively (parser);
13958 argument = cp_parser_primary_expression (parser,
13959 address_p,
13960 /*cast_p=*/false,
13961 /*template_arg_p=*/true,
13962 &idk);
13963 if (cp_parser_error_occurred (parser)
13964 || !cp_parser_next_token_ends_template_argument_p (parser))
13965 cp_parser_abort_tentative_parse (parser);
13966 else
13968 tree probe;
13970 if (INDIRECT_REF_P (argument))
13972 /* Strip the dereference temporarily. */
13973 gcc_assert (REFERENCE_REF_P (argument));
13974 argument = TREE_OPERAND (argument, 0);
13977 /* If we're in a template, we represent a qualified-id referring
13978 to a static data member as a SCOPE_REF even if the scope isn't
13979 dependent so that we can check access control later. */
13980 probe = argument;
13981 if (TREE_CODE (probe) == SCOPE_REF)
13982 probe = TREE_OPERAND (probe, 1);
13983 if (VAR_P (probe))
13985 /* A variable without external linkage might still be a
13986 valid constant-expression, so no error is issued here
13987 if the external-linkage check fails. */
13988 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13989 cp_parser_simulate_error (parser);
13991 else if (is_overloaded_fn (argument))
13992 /* All overloaded functions are allowed; if the external
13993 linkage test does not pass, an error will be issued
13994 later. */
13996 else if (address_p
13997 && (TREE_CODE (argument) == OFFSET_REF
13998 || TREE_CODE (argument) == SCOPE_REF))
13999 /* A pointer-to-member. */
14001 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14003 else
14004 cp_parser_simulate_error (parser);
14006 if (cp_parser_parse_definitely (parser))
14008 if (address_p)
14009 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14010 tf_warning_or_error);
14011 else
14012 argument = convert_from_reference (argument);
14013 return argument;
14017 /* If the argument started with "&", there are no other valid
14018 alternatives at this point. */
14019 if (address_p)
14021 cp_parser_error (parser, "invalid non-type template argument");
14022 return error_mark_node;
14025 /* If the argument wasn't successfully parsed as a type-id followed
14026 by '>>', the argument can only be a constant expression now.
14027 Otherwise, we try parsing the constant-expression tentatively,
14028 because the argument could really be a type-id. */
14029 if (maybe_type_id)
14030 cp_parser_parse_tentatively (parser);
14031 argument = cp_parser_constant_expression (parser,
14032 /*allow_non_constant_p=*/false,
14033 /*non_constant_p=*/NULL);
14034 if (!maybe_type_id)
14035 return argument;
14036 if (!cp_parser_next_token_ends_template_argument_p (parser))
14037 cp_parser_error (parser, "expected template-argument");
14038 if (cp_parser_parse_definitely (parser))
14039 return argument;
14040 /* We did our best to parse the argument as a non type-id, but that
14041 was the only alternative that matched (albeit with a '>' after
14042 it). We can assume it's just a typo from the user, and a
14043 diagnostic will then be issued. */
14044 return cp_parser_template_type_arg (parser);
14047 /* Parse an explicit-instantiation.
14049 explicit-instantiation:
14050 template declaration
14052 Although the standard says `declaration', what it really means is:
14054 explicit-instantiation:
14055 template decl-specifier-seq [opt] declarator [opt] ;
14057 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14058 supposed to be allowed. A defect report has been filed about this
14059 issue.
14061 GNU Extension:
14063 explicit-instantiation:
14064 storage-class-specifier template
14065 decl-specifier-seq [opt] declarator [opt] ;
14066 function-specifier template
14067 decl-specifier-seq [opt] declarator [opt] ; */
14069 static void
14070 cp_parser_explicit_instantiation (cp_parser* parser)
14072 int declares_class_or_enum;
14073 cp_decl_specifier_seq decl_specifiers;
14074 tree extension_specifier = NULL_TREE;
14076 timevar_push (TV_TEMPLATE_INST);
14078 /* Look for an (optional) storage-class-specifier or
14079 function-specifier. */
14080 if (cp_parser_allow_gnu_extensions_p (parser))
14082 extension_specifier
14083 = cp_parser_storage_class_specifier_opt (parser);
14084 if (!extension_specifier)
14085 extension_specifier
14086 = cp_parser_function_specifier_opt (parser,
14087 /*decl_specs=*/NULL);
14090 /* Look for the `template' keyword. */
14091 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14092 /* Let the front end know that we are processing an explicit
14093 instantiation. */
14094 begin_explicit_instantiation ();
14095 /* [temp.explicit] says that we are supposed to ignore access
14096 control while processing explicit instantiation directives. */
14097 push_deferring_access_checks (dk_no_check);
14098 /* Parse a decl-specifier-seq. */
14099 cp_parser_decl_specifier_seq (parser,
14100 CP_PARSER_FLAGS_OPTIONAL,
14101 &decl_specifiers,
14102 &declares_class_or_enum);
14103 /* If there was exactly one decl-specifier, and it declared a class,
14104 and there's no declarator, then we have an explicit type
14105 instantiation. */
14106 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14108 tree type;
14110 type = check_tag_decl (&decl_specifiers,
14111 /*explicit_type_instantiation_p=*/true);
14112 /* Turn access control back on for names used during
14113 template instantiation. */
14114 pop_deferring_access_checks ();
14115 if (type)
14116 do_type_instantiation (type, extension_specifier,
14117 /*complain=*/tf_error);
14119 else
14121 cp_declarator *declarator;
14122 tree decl;
14124 /* Parse the declarator. */
14125 declarator
14126 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14127 /*ctor_dtor_or_conv_p=*/NULL,
14128 /*parenthesized_p=*/NULL,
14129 /*member_p=*/false);
14130 if (declares_class_or_enum & 2)
14131 cp_parser_check_for_definition_in_return_type (declarator,
14132 decl_specifiers.type,
14133 decl_specifiers.locations[ds_type_spec]);
14134 if (declarator != cp_error_declarator)
14136 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14137 permerror (decl_specifiers.locations[ds_inline],
14138 "explicit instantiation shall not use"
14139 " %<inline%> specifier");
14140 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14141 permerror (decl_specifiers.locations[ds_constexpr],
14142 "explicit instantiation shall not use"
14143 " %<constexpr%> specifier");
14145 decl = grokdeclarator (declarator, &decl_specifiers,
14146 NORMAL, 0, &decl_specifiers.attributes);
14147 /* Turn access control back on for names used during
14148 template instantiation. */
14149 pop_deferring_access_checks ();
14150 /* Do the explicit instantiation. */
14151 do_decl_instantiation (decl, extension_specifier);
14153 else
14155 pop_deferring_access_checks ();
14156 /* Skip the body of the explicit instantiation. */
14157 cp_parser_skip_to_end_of_statement (parser);
14160 /* We're done with the instantiation. */
14161 end_explicit_instantiation ();
14163 cp_parser_consume_semicolon_at_end_of_statement (parser);
14165 timevar_pop (TV_TEMPLATE_INST);
14168 /* Parse an explicit-specialization.
14170 explicit-specialization:
14171 template < > declaration
14173 Although the standard says `declaration', what it really means is:
14175 explicit-specialization:
14176 template <> decl-specifier [opt] init-declarator [opt] ;
14177 template <> function-definition
14178 template <> explicit-specialization
14179 template <> template-declaration */
14181 static void
14182 cp_parser_explicit_specialization (cp_parser* parser)
14184 bool need_lang_pop;
14185 cp_token *token = cp_lexer_peek_token (parser->lexer);
14187 /* Look for the `template' keyword. */
14188 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14189 /* Look for the `<'. */
14190 cp_parser_require (parser, CPP_LESS, RT_LESS);
14191 /* Look for the `>'. */
14192 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14193 /* We have processed another parameter list. */
14194 ++parser->num_template_parameter_lists;
14195 /* [temp]
14197 A template ... explicit specialization ... shall not have C
14198 linkage. */
14199 if (current_lang_name == lang_name_c)
14201 error_at (token->location, "template specialization with C linkage");
14202 /* Give it C++ linkage to avoid confusing other parts of the
14203 front end. */
14204 push_lang_context (lang_name_cplusplus);
14205 need_lang_pop = true;
14207 else
14208 need_lang_pop = false;
14209 /* Let the front end know that we are beginning a specialization. */
14210 if (!begin_specialization ())
14212 end_specialization ();
14213 return;
14216 /* If the next keyword is `template', we need to figure out whether
14217 or not we're looking a template-declaration. */
14218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14220 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14221 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14222 cp_parser_template_declaration_after_export (parser,
14223 /*member_p=*/false);
14224 else
14225 cp_parser_explicit_specialization (parser);
14227 else
14228 /* Parse the dependent declaration. */
14229 cp_parser_single_declaration (parser,
14230 /*checks=*/NULL,
14231 /*member_p=*/false,
14232 /*explicit_specialization_p=*/true,
14233 /*friend_p=*/NULL);
14234 /* We're done with the specialization. */
14235 end_specialization ();
14236 /* For the erroneous case of a template with C linkage, we pushed an
14237 implicit C++ linkage scope; exit that scope now. */
14238 if (need_lang_pop)
14239 pop_lang_context ();
14240 /* We're done with this parameter list. */
14241 --parser->num_template_parameter_lists;
14244 /* Parse a type-specifier.
14246 type-specifier:
14247 simple-type-specifier
14248 class-specifier
14249 enum-specifier
14250 elaborated-type-specifier
14251 cv-qualifier
14253 GNU Extension:
14255 type-specifier:
14256 __complex__
14258 Returns a representation of the type-specifier. For a
14259 class-specifier, enum-specifier, or elaborated-type-specifier, a
14260 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14262 The parser flags FLAGS is used to control type-specifier parsing.
14264 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14265 in a decl-specifier-seq.
14267 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14268 class-specifier, enum-specifier, or elaborated-type-specifier, then
14269 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14270 if a type is declared; 2 if it is defined. Otherwise, it is set to
14271 zero.
14273 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14274 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14275 is set to FALSE. */
14277 static tree
14278 cp_parser_type_specifier (cp_parser* parser,
14279 cp_parser_flags flags,
14280 cp_decl_specifier_seq *decl_specs,
14281 bool is_declaration,
14282 int* declares_class_or_enum,
14283 bool* is_cv_qualifier)
14285 tree type_spec = NULL_TREE;
14286 cp_token *token;
14287 enum rid keyword;
14288 cp_decl_spec ds = ds_last;
14290 /* Assume this type-specifier does not declare a new type. */
14291 if (declares_class_or_enum)
14292 *declares_class_or_enum = 0;
14293 /* And that it does not specify a cv-qualifier. */
14294 if (is_cv_qualifier)
14295 *is_cv_qualifier = false;
14296 /* Peek at the next token. */
14297 token = cp_lexer_peek_token (parser->lexer);
14299 /* If we're looking at a keyword, we can use that to guide the
14300 production we choose. */
14301 keyword = token->keyword;
14302 switch (keyword)
14304 case RID_ENUM:
14305 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14306 goto elaborated_type_specifier;
14308 /* Look for the enum-specifier. */
14309 type_spec = cp_parser_enum_specifier (parser);
14310 /* If that worked, we're done. */
14311 if (type_spec)
14313 if (declares_class_or_enum)
14314 *declares_class_or_enum = 2;
14315 if (decl_specs)
14316 cp_parser_set_decl_spec_type (decl_specs,
14317 type_spec,
14318 token,
14319 /*type_definition_p=*/true);
14320 return type_spec;
14322 else
14323 goto elaborated_type_specifier;
14325 /* Any of these indicate either a class-specifier, or an
14326 elaborated-type-specifier. */
14327 case RID_CLASS:
14328 case RID_STRUCT:
14329 case RID_UNION:
14330 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14331 goto elaborated_type_specifier;
14333 /* Parse tentatively so that we can back up if we don't find a
14334 class-specifier. */
14335 cp_parser_parse_tentatively (parser);
14336 /* Look for the class-specifier. */
14337 type_spec = cp_parser_class_specifier (parser);
14338 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14339 /* If that worked, we're done. */
14340 if (cp_parser_parse_definitely (parser))
14342 if (declares_class_or_enum)
14343 *declares_class_or_enum = 2;
14344 if (decl_specs)
14345 cp_parser_set_decl_spec_type (decl_specs,
14346 type_spec,
14347 token,
14348 /*type_definition_p=*/true);
14349 return type_spec;
14352 /* Fall through. */
14353 elaborated_type_specifier:
14354 /* We're declaring (not defining) a class or enum. */
14355 if (declares_class_or_enum)
14356 *declares_class_or_enum = 1;
14358 /* Fall through. */
14359 case RID_TYPENAME:
14360 /* Look for an elaborated-type-specifier. */
14361 type_spec
14362 = (cp_parser_elaborated_type_specifier
14363 (parser,
14364 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14365 is_declaration));
14366 if (decl_specs)
14367 cp_parser_set_decl_spec_type (decl_specs,
14368 type_spec,
14369 token,
14370 /*type_definition_p=*/false);
14371 return type_spec;
14373 case RID_CONST:
14374 ds = ds_const;
14375 if (is_cv_qualifier)
14376 *is_cv_qualifier = true;
14377 break;
14379 case RID_VOLATILE:
14380 ds = ds_volatile;
14381 if (is_cv_qualifier)
14382 *is_cv_qualifier = true;
14383 break;
14385 case RID_RESTRICT:
14386 ds = ds_restrict;
14387 if (is_cv_qualifier)
14388 *is_cv_qualifier = true;
14389 break;
14391 case RID_COMPLEX:
14392 /* The `__complex__' keyword is a GNU extension. */
14393 ds = ds_complex;
14394 break;
14396 default:
14397 break;
14400 /* Handle simple keywords. */
14401 if (ds != ds_last)
14403 if (decl_specs)
14405 set_and_check_decl_spec_loc (decl_specs, ds, token);
14406 decl_specs->any_specifiers_p = true;
14408 return cp_lexer_consume_token (parser->lexer)->u.value;
14411 /* If we do not already have a type-specifier, assume we are looking
14412 at a simple-type-specifier. */
14413 type_spec = cp_parser_simple_type_specifier (parser,
14414 decl_specs,
14415 flags);
14417 /* If we didn't find a type-specifier, and a type-specifier was not
14418 optional in this context, issue an error message. */
14419 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14421 cp_parser_error (parser, "expected type specifier");
14422 return error_mark_node;
14425 return type_spec;
14428 /* Parse a simple-type-specifier.
14430 simple-type-specifier:
14431 :: [opt] nested-name-specifier [opt] type-name
14432 :: [opt] nested-name-specifier template template-id
14433 char
14434 wchar_t
14435 bool
14436 short
14438 long
14439 signed
14440 unsigned
14441 float
14442 double
14443 void
14445 C++0x Extension:
14447 simple-type-specifier:
14448 auto
14449 decltype ( expression )
14450 char16_t
14451 char32_t
14452 __underlying_type ( type-id )
14454 GNU Extension:
14456 simple-type-specifier:
14457 __int128
14458 __typeof__ unary-expression
14459 __typeof__ ( type-id )
14460 __typeof__ ( type-id ) { initializer-list , [opt] }
14462 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14463 appropriately updated. */
14465 static tree
14466 cp_parser_simple_type_specifier (cp_parser* parser,
14467 cp_decl_specifier_seq *decl_specs,
14468 cp_parser_flags flags)
14470 tree type = NULL_TREE;
14471 cp_token *token;
14473 /* Peek at the next token. */
14474 token = cp_lexer_peek_token (parser->lexer);
14476 /* If we're looking at a keyword, things are easy. */
14477 switch (token->keyword)
14479 case RID_CHAR:
14480 if (decl_specs)
14481 decl_specs->explicit_char_p = true;
14482 type = char_type_node;
14483 break;
14484 case RID_CHAR16:
14485 type = char16_type_node;
14486 break;
14487 case RID_CHAR32:
14488 type = char32_type_node;
14489 break;
14490 case RID_WCHAR:
14491 type = wchar_type_node;
14492 break;
14493 case RID_BOOL:
14494 type = boolean_type_node;
14495 break;
14496 case RID_SHORT:
14497 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14498 type = short_integer_type_node;
14499 break;
14500 case RID_INT:
14501 if (decl_specs)
14502 decl_specs->explicit_int_p = true;
14503 type = integer_type_node;
14504 break;
14505 case RID_INT128:
14506 if (!int128_integer_type_node)
14507 break;
14508 if (decl_specs)
14509 decl_specs->explicit_int128_p = true;
14510 type = int128_integer_type_node;
14511 break;
14512 case RID_LONG:
14513 if (decl_specs)
14514 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14515 type = long_integer_type_node;
14516 break;
14517 case RID_SIGNED:
14518 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14519 type = integer_type_node;
14520 break;
14521 case RID_UNSIGNED:
14522 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14523 type = unsigned_type_node;
14524 break;
14525 case RID_FLOAT:
14526 type = float_type_node;
14527 break;
14528 case RID_DOUBLE:
14529 type = double_type_node;
14530 break;
14531 case RID_VOID:
14532 type = void_type_node;
14533 break;
14535 case RID_AUTO:
14536 maybe_warn_cpp0x (CPP0X_AUTO);
14537 if (parser->auto_is_implicit_function_template_parm_p)
14539 type = synthesize_implicit_template_parm (parser);
14541 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14543 if (cxx_dialect < cxx1y)
14544 pedwarn (location_of (type), 0,
14545 "use of %<auto%> in lambda parameter declaration "
14546 "only available with "
14547 "-std=c++1y or -std=gnu++1y");
14549 else if (cxx_dialect < cxx1y)
14550 pedwarn (location_of (type), 0,
14551 "use of %<auto%> in parameter declaration "
14552 "only available with "
14553 "-std=c++1y or -std=gnu++1y");
14554 else
14555 pedwarn (location_of (type), OPT_Wpedantic,
14556 "ISO C++ forbids use of %<auto%> in parameter "
14557 "declaration");
14559 else
14560 type = make_auto ();
14561 break;
14563 case RID_DECLTYPE:
14564 /* Since DR 743, decltype can either be a simple-type-specifier by
14565 itself or begin a nested-name-specifier. Parsing it will replace
14566 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14567 handling below decide what to do. */
14568 cp_parser_decltype (parser);
14569 cp_lexer_set_token_position (parser->lexer, token);
14570 break;
14572 case RID_TYPEOF:
14573 /* Consume the `typeof' token. */
14574 cp_lexer_consume_token (parser->lexer);
14575 /* Parse the operand to `typeof'. */
14576 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14577 /* If it is not already a TYPE, take its type. */
14578 if (!TYPE_P (type))
14579 type = finish_typeof (type);
14581 if (decl_specs)
14582 cp_parser_set_decl_spec_type (decl_specs, type,
14583 token,
14584 /*type_definition_p=*/false);
14586 return type;
14588 case RID_UNDERLYING_TYPE:
14589 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14590 if (decl_specs)
14591 cp_parser_set_decl_spec_type (decl_specs, type,
14592 token,
14593 /*type_definition_p=*/false);
14595 return type;
14597 case RID_BASES:
14598 case RID_DIRECT_BASES:
14599 type = cp_parser_trait_expr (parser, token->keyword);
14600 if (decl_specs)
14601 cp_parser_set_decl_spec_type (decl_specs, type,
14602 token,
14603 /*type_definition_p=*/false);
14604 return type;
14605 default:
14606 break;
14609 /* If token is an already-parsed decltype not followed by ::,
14610 it's a simple-type-specifier. */
14611 if (token->type == CPP_DECLTYPE
14612 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14614 type = token->u.value;
14615 if (decl_specs)
14616 cp_parser_set_decl_spec_type (decl_specs, type,
14617 token,
14618 /*type_definition_p=*/false);
14619 cp_lexer_consume_token (parser->lexer);
14620 return type;
14623 /* If the type-specifier was for a built-in type, we're done. */
14624 if (type)
14626 /* Record the type. */
14627 if (decl_specs
14628 && (token->keyword != RID_SIGNED
14629 && token->keyword != RID_UNSIGNED
14630 && token->keyword != RID_SHORT
14631 && token->keyword != RID_LONG))
14632 cp_parser_set_decl_spec_type (decl_specs,
14633 type,
14634 token,
14635 /*type_definition_p=*/false);
14636 if (decl_specs)
14637 decl_specs->any_specifiers_p = true;
14639 /* Consume the token. */
14640 cp_lexer_consume_token (parser->lexer);
14642 /* There is no valid C++ program where a non-template type is
14643 followed by a "<". That usually indicates that the user thought
14644 that the type was a template. */
14645 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14646 token->location);
14648 return TYPE_NAME (type);
14651 /* The type-specifier must be a user-defined type. */
14652 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14654 bool qualified_p;
14655 bool global_p;
14657 /* Don't gobble tokens or issue error messages if this is an
14658 optional type-specifier. */
14659 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14660 cp_parser_parse_tentatively (parser);
14662 /* Look for the optional `::' operator. */
14663 global_p
14664 = (cp_parser_global_scope_opt (parser,
14665 /*current_scope_valid_p=*/false)
14666 != NULL_TREE);
14667 /* Look for the nested-name specifier. */
14668 qualified_p
14669 = (cp_parser_nested_name_specifier_opt (parser,
14670 /*typename_keyword_p=*/false,
14671 /*check_dependency_p=*/true,
14672 /*type_p=*/false,
14673 /*is_declaration=*/false)
14674 != NULL_TREE);
14675 token = cp_lexer_peek_token (parser->lexer);
14676 /* If we have seen a nested-name-specifier, and the next token
14677 is `template', then we are using the template-id production. */
14678 if (parser->scope
14679 && cp_parser_optional_template_keyword (parser))
14681 /* Look for the template-id. */
14682 type = cp_parser_template_id (parser,
14683 /*template_keyword_p=*/true,
14684 /*check_dependency_p=*/true,
14685 none_type,
14686 /*is_declaration=*/false);
14687 /* If the template-id did not name a type, we are out of
14688 luck. */
14689 if (TREE_CODE (type) != TYPE_DECL)
14691 cp_parser_error (parser, "expected template-id for type");
14692 type = NULL_TREE;
14695 /* Otherwise, look for a type-name. */
14696 else
14697 type = cp_parser_type_name (parser);
14698 /* Keep track of all name-lookups performed in class scopes. */
14699 if (type
14700 && !global_p
14701 && !qualified_p
14702 && TREE_CODE (type) == TYPE_DECL
14703 && identifier_p (DECL_NAME (type)))
14704 maybe_note_name_used_in_class (DECL_NAME (type), type);
14705 /* If it didn't work out, we don't have a TYPE. */
14706 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14707 && !cp_parser_parse_definitely (parser))
14708 type = NULL_TREE;
14709 if (type && decl_specs)
14710 cp_parser_set_decl_spec_type (decl_specs, type,
14711 token,
14712 /*type_definition_p=*/false);
14715 /* If we didn't get a type-name, issue an error message. */
14716 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14718 cp_parser_error (parser, "expected type-name");
14719 return error_mark_node;
14722 if (type && type != error_mark_node)
14724 /* See if TYPE is an Objective-C type, and if so, parse and
14725 accept any protocol references following it. Do this before
14726 the cp_parser_check_for_invalid_template_id() call, because
14727 Objective-C types can be followed by '<...>' which would
14728 enclose protocol names rather than template arguments, and so
14729 everything is fine. */
14730 if (c_dialect_objc () && !parser->scope
14731 && (objc_is_id (type) || objc_is_class_name (type)))
14733 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14734 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14736 /* Clobber the "unqualified" type previously entered into
14737 DECL_SPECS with the new, improved protocol-qualified version. */
14738 if (decl_specs)
14739 decl_specs->type = qual_type;
14741 return qual_type;
14744 /* There is no valid C++ program where a non-template type is
14745 followed by a "<". That usually indicates that the user
14746 thought that the type was a template. */
14747 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14748 none_type,
14749 token->location);
14752 return type;
14755 /* Parse a type-name.
14757 type-name:
14758 class-name
14759 enum-name
14760 typedef-name
14761 simple-template-id [in c++0x]
14763 enum-name:
14764 identifier
14766 typedef-name:
14767 identifier
14769 Returns a TYPE_DECL for the type. */
14771 static tree
14772 cp_parser_type_name (cp_parser* parser)
14774 tree type_decl;
14776 /* We can't know yet whether it is a class-name or not. */
14777 cp_parser_parse_tentatively (parser);
14778 /* Try a class-name. */
14779 type_decl = cp_parser_class_name (parser,
14780 /*typename_keyword_p=*/false,
14781 /*template_keyword_p=*/false,
14782 none_type,
14783 /*check_dependency_p=*/true,
14784 /*class_head_p=*/false,
14785 /*is_declaration=*/false);
14786 /* If it's not a class-name, keep looking. */
14787 if (!cp_parser_parse_definitely (parser))
14789 if (cxx_dialect < cxx11)
14790 /* It must be a typedef-name or an enum-name. */
14791 return cp_parser_nonclass_name (parser);
14793 cp_parser_parse_tentatively (parser);
14794 /* It is either a simple-template-id representing an
14795 instantiation of an alias template... */
14796 type_decl = cp_parser_template_id (parser,
14797 /*template_keyword_p=*/false,
14798 /*check_dependency_p=*/true,
14799 none_type,
14800 /*is_declaration=*/false);
14801 /* Note that this must be an instantiation of an alias template
14802 because [temp.names]/6 says:
14804 A template-id that names an alias template specialization
14805 is a type-name.
14807 Whereas [temp.names]/7 says:
14809 A simple-template-id that names a class template
14810 specialization is a class-name. */
14811 if (type_decl != NULL_TREE
14812 && TREE_CODE (type_decl) == TYPE_DECL
14813 && TYPE_DECL_ALIAS_P (type_decl))
14814 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14815 else
14816 cp_parser_simulate_error (parser);
14818 if (!cp_parser_parse_definitely (parser))
14819 /* ... Or a typedef-name or an enum-name. */
14820 return cp_parser_nonclass_name (parser);
14823 return type_decl;
14826 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14828 enum-name:
14829 identifier
14831 typedef-name:
14832 identifier
14834 Returns a TYPE_DECL for the type. */
14836 static tree
14837 cp_parser_nonclass_name (cp_parser* parser)
14839 tree type_decl;
14840 tree identifier;
14842 cp_token *token = cp_lexer_peek_token (parser->lexer);
14843 identifier = cp_parser_identifier (parser);
14844 if (identifier == error_mark_node)
14845 return error_mark_node;
14847 /* Look up the type-name. */
14848 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14850 type_decl = strip_using_decl (type_decl);
14852 if (TREE_CODE (type_decl) != TYPE_DECL
14853 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14855 /* See if this is an Objective-C type. */
14856 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14857 tree type = objc_get_protocol_qualified_type (identifier, protos);
14858 if (type)
14859 type_decl = TYPE_NAME (type);
14862 /* Issue an error if we did not find a type-name. */
14863 if (TREE_CODE (type_decl) != TYPE_DECL
14864 /* In Objective-C, we have the complication that class names are
14865 normally type names and start declarations (eg, the
14866 "NSObject" in "NSObject *object;"), but can be used in an
14867 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14868 is an expression. So, a classname followed by a dot is not a
14869 valid type-name. */
14870 || (objc_is_class_name (TREE_TYPE (type_decl))
14871 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14873 if (!cp_parser_simulate_error (parser))
14874 cp_parser_name_lookup_error (parser, identifier, type_decl,
14875 NLE_TYPE, token->location);
14876 return error_mark_node;
14878 /* Remember that the name was used in the definition of the
14879 current class so that we can check later to see if the
14880 meaning would have been different after the class was
14881 entirely defined. */
14882 else if (type_decl != error_mark_node
14883 && !parser->scope)
14884 maybe_note_name_used_in_class (identifier, type_decl);
14886 return type_decl;
14889 /* Parse an elaborated-type-specifier. Note that the grammar given
14890 here incorporates the resolution to DR68.
14892 elaborated-type-specifier:
14893 class-key :: [opt] nested-name-specifier [opt] identifier
14894 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14895 enum-key :: [opt] nested-name-specifier [opt] identifier
14896 typename :: [opt] nested-name-specifier identifier
14897 typename :: [opt] nested-name-specifier template [opt]
14898 template-id
14900 GNU extension:
14902 elaborated-type-specifier:
14903 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14904 class-key attributes :: [opt] nested-name-specifier [opt]
14905 template [opt] template-id
14906 enum attributes :: [opt] nested-name-specifier [opt] identifier
14908 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14909 declared `friend'. If IS_DECLARATION is TRUE, then this
14910 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14911 something is being declared.
14913 Returns the TYPE specified. */
14915 static tree
14916 cp_parser_elaborated_type_specifier (cp_parser* parser,
14917 bool is_friend,
14918 bool is_declaration)
14920 enum tag_types tag_type;
14921 tree identifier;
14922 tree type = NULL_TREE;
14923 tree attributes = NULL_TREE;
14924 tree globalscope;
14925 cp_token *token = NULL;
14927 /* See if we're looking at the `enum' keyword. */
14928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14930 /* Consume the `enum' token. */
14931 cp_lexer_consume_token (parser->lexer);
14932 /* Remember that it's an enumeration type. */
14933 tag_type = enum_type;
14934 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14935 enums) is used here. */
14936 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14937 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14939 pedwarn (input_location, 0, "elaborated-type-specifier "
14940 "for a scoped enum must not use the %<%D%> keyword",
14941 cp_lexer_peek_token (parser->lexer)->u.value);
14942 /* Consume the `struct' or `class' and parse it anyway. */
14943 cp_lexer_consume_token (parser->lexer);
14945 /* Parse the attributes. */
14946 attributes = cp_parser_attributes_opt (parser);
14948 /* Or, it might be `typename'. */
14949 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14950 RID_TYPENAME))
14952 /* Consume the `typename' token. */
14953 cp_lexer_consume_token (parser->lexer);
14954 /* Remember that it's a `typename' type. */
14955 tag_type = typename_type;
14957 /* Otherwise it must be a class-key. */
14958 else
14960 tag_type = cp_parser_class_key (parser);
14961 if (tag_type == none_type)
14962 return error_mark_node;
14963 /* Parse the attributes. */
14964 attributes = cp_parser_attributes_opt (parser);
14967 /* Look for the `::' operator. */
14968 globalscope = cp_parser_global_scope_opt (parser,
14969 /*current_scope_valid_p=*/false);
14970 /* Look for the nested-name-specifier. */
14971 if (tag_type == typename_type && !globalscope)
14973 if (!cp_parser_nested_name_specifier (parser,
14974 /*typename_keyword_p=*/true,
14975 /*check_dependency_p=*/true,
14976 /*type_p=*/true,
14977 is_declaration))
14978 return error_mark_node;
14980 else
14981 /* Even though `typename' is not present, the proposed resolution
14982 to Core Issue 180 says that in `class A<T>::B', `B' should be
14983 considered a type-name, even if `A<T>' is dependent. */
14984 cp_parser_nested_name_specifier_opt (parser,
14985 /*typename_keyword_p=*/true,
14986 /*check_dependency_p=*/true,
14987 /*type_p=*/true,
14988 is_declaration);
14989 /* For everything but enumeration types, consider a template-id.
14990 For an enumeration type, consider only a plain identifier. */
14991 if (tag_type != enum_type)
14993 bool template_p = false;
14994 tree decl;
14996 /* Allow the `template' keyword. */
14997 template_p = cp_parser_optional_template_keyword (parser);
14998 /* If we didn't see `template', we don't know if there's a
14999 template-id or not. */
15000 if (!template_p)
15001 cp_parser_parse_tentatively (parser);
15002 /* Parse the template-id. */
15003 token = cp_lexer_peek_token (parser->lexer);
15004 decl = cp_parser_template_id (parser, template_p,
15005 /*check_dependency_p=*/true,
15006 tag_type,
15007 is_declaration);
15008 /* If we didn't find a template-id, look for an ordinary
15009 identifier. */
15010 if (!template_p && !cp_parser_parse_definitely (parser))
15012 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15013 in effect, then we must assume that, upon instantiation, the
15014 template will correspond to a class. */
15015 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15016 && tag_type == typename_type)
15017 type = make_typename_type (parser->scope, decl,
15018 typename_type,
15019 /*complain=*/tf_error);
15020 /* If the `typename' keyword is in effect and DECL is not a type
15021 decl, then type is non existent. */
15022 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15024 else if (TREE_CODE (decl) == TYPE_DECL)
15025 type = check_elaborated_type_specifier (tag_type, decl,
15026 /*allow_template_p=*/true);
15027 else if (decl == error_mark_node)
15028 type = error_mark_node;
15031 if (!type)
15033 token = cp_lexer_peek_token (parser->lexer);
15034 identifier = cp_parser_identifier (parser);
15036 if (identifier == error_mark_node)
15038 parser->scope = NULL_TREE;
15039 return error_mark_node;
15042 /* For a `typename', we needn't call xref_tag. */
15043 if (tag_type == typename_type
15044 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15045 return cp_parser_make_typename_type (parser, parser->scope,
15046 identifier,
15047 token->location);
15048 /* Look up a qualified name in the usual way. */
15049 if (parser->scope)
15051 tree decl;
15052 tree ambiguous_decls;
15054 decl = cp_parser_lookup_name (parser, identifier,
15055 tag_type,
15056 /*is_template=*/false,
15057 /*is_namespace=*/false,
15058 /*check_dependency=*/true,
15059 &ambiguous_decls,
15060 token->location);
15062 /* If the lookup was ambiguous, an error will already have been
15063 issued. */
15064 if (ambiguous_decls)
15065 return error_mark_node;
15067 /* If we are parsing friend declaration, DECL may be a
15068 TEMPLATE_DECL tree node here. However, we need to check
15069 whether this TEMPLATE_DECL results in valid code. Consider
15070 the following example:
15072 namespace N {
15073 template <class T> class C {};
15075 class X {
15076 template <class T> friend class N::C; // #1, valid code
15078 template <class T> class Y {
15079 friend class N::C; // #2, invalid code
15082 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15083 name lookup of `N::C'. We see that friend declaration must
15084 be template for the code to be valid. Note that
15085 processing_template_decl does not work here since it is
15086 always 1 for the above two cases. */
15088 decl = (cp_parser_maybe_treat_template_as_class
15089 (decl, /*tag_name_p=*/is_friend
15090 && parser->num_template_parameter_lists));
15092 if (TREE_CODE (decl) != TYPE_DECL)
15094 cp_parser_diagnose_invalid_type_name (parser,
15095 parser->scope,
15096 identifier,
15097 token->location);
15098 return error_mark_node;
15101 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15103 bool allow_template = (parser->num_template_parameter_lists
15104 || DECL_SELF_REFERENCE_P (decl));
15105 type = check_elaborated_type_specifier (tag_type, decl,
15106 allow_template);
15108 if (type == error_mark_node)
15109 return error_mark_node;
15112 /* Forward declarations of nested types, such as
15114 class C1::C2;
15115 class C1::C2::C3;
15117 are invalid unless all components preceding the final '::'
15118 are complete. If all enclosing types are complete, these
15119 declarations become merely pointless.
15121 Invalid forward declarations of nested types are errors
15122 caught elsewhere in parsing. Those that are pointless arrive
15123 here. */
15125 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15126 && !is_friend && !processing_explicit_instantiation)
15127 warning (0, "declaration %qD does not declare anything", decl);
15129 type = TREE_TYPE (decl);
15131 else
15133 /* An elaborated-type-specifier sometimes introduces a new type and
15134 sometimes names an existing type. Normally, the rule is that it
15135 introduces a new type only if there is not an existing type of
15136 the same name already in scope. For example, given:
15138 struct S {};
15139 void f() { struct S s; }
15141 the `struct S' in the body of `f' is the same `struct S' as in
15142 the global scope; the existing definition is used. However, if
15143 there were no global declaration, this would introduce a new
15144 local class named `S'.
15146 An exception to this rule applies to the following code:
15148 namespace N { struct S; }
15150 Here, the elaborated-type-specifier names a new type
15151 unconditionally; even if there is already an `S' in the
15152 containing scope this declaration names a new type.
15153 This exception only applies if the elaborated-type-specifier
15154 forms the complete declaration:
15156 [class.name]
15158 A declaration consisting solely of `class-key identifier ;' is
15159 either a redeclaration of the name in the current scope or a
15160 forward declaration of the identifier as a class name. It
15161 introduces the name into the current scope.
15163 We are in this situation precisely when the next token is a `;'.
15165 An exception to the exception is that a `friend' declaration does
15166 *not* name a new type; i.e., given:
15168 struct S { friend struct T; };
15170 `T' is not a new type in the scope of `S'.
15172 Also, `new struct S' or `sizeof (struct S)' never results in the
15173 definition of a new type; a new type can only be declared in a
15174 declaration context. */
15176 tag_scope ts;
15177 bool template_p;
15179 if (is_friend)
15180 /* Friends have special name lookup rules. */
15181 ts = ts_within_enclosing_non_class;
15182 else if (is_declaration
15183 && cp_lexer_next_token_is (parser->lexer,
15184 CPP_SEMICOLON))
15185 /* This is a `class-key identifier ;' */
15186 ts = ts_current;
15187 else
15188 ts = ts_global;
15190 template_p =
15191 (parser->num_template_parameter_lists
15192 && (cp_parser_next_token_starts_class_definition_p (parser)
15193 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15194 /* An unqualified name was used to reference this type, so
15195 there were no qualifying templates. */
15196 if (!cp_parser_check_template_parameters (parser,
15197 /*num_templates=*/0,
15198 token->location,
15199 /*declarator=*/NULL))
15200 return error_mark_node;
15201 type = xref_tag (tag_type, identifier, ts, template_p);
15205 if (type == error_mark_node)
15206 return error_mark_node;
15208 /* Allow attributes on forward declarations of classes. */
15209 if (attributes)
15211 if (TREE_CODE (type) == TYPENAME_TYPE)
15212 warning (OPT_Wattributes,
15213 "attributes ignored on uninstantiated type");
15214 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15215 && ! processing_explicit_instantiation)
15216 warning (OPT_Wattributes,
15217 "attributes ignored on template instantiation");
15218 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15219 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15220 else
15221 warning (OPT_Wattributes,
15222 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15225 if (tag_type != enum_type)
15227 /* Indicate whether this class was declared as a `class' or as a
15228 `struct'. */
15229 if (TREE_CODE (type) == RECORD_TYPE)
15230 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15231 cp_parser_check_class_key (tag_type, type);
15234 /* A "<" cannot follow an elaborated type specifier. If that
15235 happens, the user was probably trying to form a template-id. */
15236 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15237 token->location);
15239 return type;
15242 /* Parse an enum-specifier.
15244 enum-specifier:
15245 enum-head { enumerator-list [opt] }
15246 enum-head { enumerator-list , } [C++0x]
15248 enum-head:
15249 enum-key identifier [opt] enum-base [opt]
15250 enum-key nested-name-specifier identifier enum-base [opt]
15252 enum-key:
15253 enum
15254 enum class [C++0x]
15255 enum struct [C++0x]
15257 enum-base: [C++0x]
15258 : type-specifier-seq
15260 opaque-enum-specifier:
15261 enum-key identifier enum-base [opt] ;
15263 GNU Extensions:
15264 enum-key attributes[opt] identifier [opt] enum-base [opt]
15265 { enumerator-list [opt] }attributes[opt]
15266 enum-key attributes[opt] identifier [opt] enum-base [opt]
15267 { enumerator-list, }attributes[opt] [C++0x]
15269 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15270 if the token stream isn't an enum-specifier after all. */
15272 static tree
15273 cp_parser_enum_specifier (cp_parser* parser)
15275 tree identifier;
15276 tree type = NULL_TREE;
15277 tree prev_scope;
15278 tree nested_name_specifier = NULL_TREE;
15279 tree attributes;
15280 bool scoped_enum_p = false;
15281 bool has_underlying_type = false;
15282 bool nested_being_defined = false;
15283 bool new_value_list = false;
15284 bool is_new_type = false;
15285 bool is_anonymous = false;
15286 tree underlying_type = NULL_TREE;
15287 cp_token *type_start_token = NULL;
15288 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15290 parser->colon_corrects_to_scope_p = false;
15292 /* Parse tentatively so that we can back up if we don't find a
15293 enum-specifier. */
15294 cp_parser_parse_tentatively (parser);
15296 /* Caller guarantees that the current token is 'enum', an identifier
15297 possibly follows, and the token after that is an opening brace.
15298 If we don't have an identifier, fabricate an anonymous name for
15299 the enumeration being defined. */
15300 cp_lexer_consume_token (parser->lexer);
15302 /* Parse the "class" or "struct", which indicates a scoped
15303 enumeration type in C++0x. */
15304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15305 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15307 if (cxx_dialect < cxx11)
15308 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15310 /* Consume the `struct' or `class' token. */
15311 cp_lexer_consume_token (parser->lexer);
15313 scoped_enum_p = true;
15316 attributes = cp_parser_attributes_opt (parser);
15318 /* Clear the qualification. */
15319 parser->scope = NULL_TREE;
15320 parser->qualifying_scope = NULL_TREE;
15321 parser->object_scope = NULL_TREE;
15323 /* Figure out in what scope the declaration is being placed. */
15324 prev_scope = current_scope ();
15326 type_start_token = cp_lexer_peek_token (parser->lexer);
15328 push_deferring_access_checks (dk_no_check);
15329 nested_name_specifier
15330 = cp_parser_nested_name_specifier_opt (parser,
15331 /*typename_keyword_p=*/true,
15332 /*check_dependency_p=*/false,
15333 /*type_p=*/false,
15334 /*is_declaration=*/false);
15336 if (nested_name_specifier)
15338 tree name;
15340 identifier = cp_parser_identifier (parser);
15341 name = cp_parser_lookup_name (parser, identifier,
15342 enum_type,
15343 /*is_template=*/false,
15344 /*is_namespace=*/false,
15345 /*check_dependency=*/true,
15346 /*ambiguous_decls=*/NULL,
15347 input_location);
15348 if (name && name != error_mark_node)
15350 type = TREE_TYPE (name);
15351 if (TREE_CODE (type) == TYPENAME_TYPE)
15353 /* Are template enums allowed in ISO? */
15354 if (template_parm_scope_p ())
15355 pedwarn (type_start_token->location, OPT_Wpedantic,
15356 "%qD is an enumeration template", name);
15357 /* ignore a typename reference, for it will be solved by name
15358 in start_enum. */
15359 type = NULL_TREE;
15362 else if (nested_name_specifier == error_mark_node)
15363 /* We already issued an error. */;
15364 else
15365 error_at (type_start_token->location,
15366 "%qD is not an enumerator-name", identifier);
15368 else
15370 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15371 identifier = cp_parser_identifier (parser);
15372 else
15374 identifier = make_anon_name ();
15375 is_anonymous = true;
15376 if (scoped_enum_p)
15377 error_at (type_start_token->location,
15378 "anonymous scoped enum is not allowed");
15381 pop_deferring_access_checks ();
15383 /* Check for the `:' that denotes a specified underlying type in C++0x.
15384 Note that a ':' could also indicate a bitfield width, however. */
15385 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15387 cp_decl_specifier_seq type_specifiers;
15389 /* Consume the `:'. */
15390 cp_lexer_consume_token (parser->lexer);
15392 /* Parse the type-specifier-seq. */
15393 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15394 /*is_trailing_return=*/false,
15395 &type_specifiers);
15397 /* At this point this is surely not elaborated type specifier. */
15398 if (!cp_parser_parse_definitely (parser))
15399 return NULL_TREE;
15401 if (cxx_dialect < cxx11)
15402 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15404 has_underlying_type = true;
15406 /* If that didn't work, stop. */
15407 if (type_specifiers.type != error_mark_node)
15409 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15410 /*initialized=*/0, NULL);
15411 if (underlying_type == error_mark_node
15412 || check_for_bare_parameter_packs (underlying_type))
15413 underlying_type = NULL_TREE;
15417 /* Look for the `{' but don't consume it yet. */
15418 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15420 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15422 cp_parser_error (parser, "expected %<{%>");
15423 if (has_underlying_type)
15425 type = NULL_TREE;
15426 goto out;
15429 /* An opaque-enum-specifier must have a ';' here. */
15430 if ((scoped_enum_p || underlying_type)
15431 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15433 cp_parser_error (parser, "expected %<;%> or %<{%>");
15434 if (has_underlying_type)
15436 type = NULL_TREE;
15437 goto out;
15442 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15443 return NULL_TREE;
15445 if (nested_name_specifier)
15447 if (CLASS_TYPE_P (nested_name_specifier))
15449 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15450 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15451 push_scope (nested_name_specifier);
15453 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15455 push_nested_namespace (nested_name_specifier);
15459 /* Issue an error message if type-definitions are forbidden here. */
15460 if (!cp_parser_check_type_definition (parser))
15461 type = error_mark_node;
15462 else
15463 /* Create the new type. We do this before consuming the opening
15464 brace so the enum will be recorded as being on the line of its
15465 tag (or the 'enum' keyword, if there is no tag). */
15466 type = start_enum (identifier, type, underlying_type,
15467 scoped_enum_p, &is_new_type);
15469 /* If the next token is not '{' it is an opaque-enum-specifier or an
15470 elaborated-type-specifier. */
15471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15473 timevar_push (TV_PARSE_ENUM);
15474 if (nested_name_specifier
15475 && nested_name_specifier != error_mark_node)
15477 /* The following catches invalid code such as:
15478 enum class S<int>::E { A, B, C }; */
15479 if (!processing_specialization
15480 && CLASS_TYPE_P (nested_name_specifier)
15481 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15482 error_at (type_start_token->location, "cannot add an enumerator "
15483 "list to a template instantiation");
15485 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15487 error_at (type_start_token->location,
15488 "%<%T::%E%> has not been declared",
15489 TYPE_CONTEXT (nested_name_specifier),
15490 nested_name_specifier);
15491 type = error_mark_node;
15493 /* If that scope does not contain the scope in which the
15494 class was originally declared, the program is invalid. */
15495 else if (prev_scope && !is_ancestor (prev_scope,
15496 nested_name_specifier))
15498 if (at_namespace_scope_p ())
15499 error_at (type_start_token->location,
15500 "declaration of %qD in namespace %qD which does not "
15501 "enclose %qD",
15502 type, prev_scope, nested_name_specifier);
15503 else
15504 error_at (type_start_token->location,
15505 "declaration of %qD in %qD which does not "
15506 "enclose %qD",
15507 type, prev_scope, nested_name_specifier);
15508 type = error_mark_node;
15512 if (scoped_enum_p)
15513 begin_scope (sk_scoped_enum, type);
15515 /* Consume the opening brace. */
15516 cp_lexer_consume_token (parser->lexer);
15518 if (type == error_mark_node)
15519 ; /* Nothing to add */
15520 else if (OPAQUE_ENUM_P (type)
15521 || (cxx_dialect > cxx98 && processing_specialization))
15523 new_value_list = true;
15524 SET_OPAQUE_ENUM_P (type, false);
15525 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15527 else
15529 error_at (type_start_token->location, "multiple definition of %q#T", type);
15530 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15531 "previous definition here");
15532 type = error_mark_node;
15535 if (type == error_mark_node)
15536 cp_parser_skip_to_end_of_block_or_statement (parser);
15537 /* If the next token is not '}', then there are some enumerators. */
15538 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15540 if (is_anonymous && !scoped_enum_p)
15541 pedwarn (type_start_token->location, OPT_Wpedantic,
15542 "ISO C++ forbids empty anonymous enum");
15544 else
15545 cp_parser_enumerator_list (parser, type);
15547 /* Consume the final '}'. */
15548 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15550 if (scoped_enum_p)
15551 finish_scope ();
15552 timevar_pop (TV_PARSE_ENUM);
15554 else
15556 /* If a ';' follows, then it is an opaque-enum-specifier
15557 and additional restrictions apply. */
15558 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15560 if (is_anonymous)
15561 error_at (type_start_token->location,
15562 "opaque-enum-specifier without name");
15563 else if (nested_name_specifier)
15564 error_at (type_start_token->location,
15565 "opaque-enum-specifier must use a simple identifier");
15569 /* Look for trailing attributes to apply to this enumeration, and
15570 apply them if appropriate. */
15571 if (cp_parser_allow_gnu_extensions_p (parser))
15573 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15574 trailing_attr = chainon (trailing_attr, attributes);
15575 cplus_decl_attributes (&type,
15576 trailing_attr,
15577 (int) ATTR_FLAG_TYPE_IN_PLACE);
15580 /* Finish up the enumeration. */
15581 if (type != error_mark_node)
15583 if (new_value_list)
15584 finish_enum_value_list (type);
15585 if (is_new_type)
15586 finish_enum (type);
15589 if (nested_name_specifier)
15591 if (CLASS_TYPE_P (nested_name_specifier))
15593 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15594 pop_scope (nested_name_specifier);
15596 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15598 pop_nested_namespace (nested_name_specifier);
15601 out:
15602 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15603 return type;
15606 /* Parse an enumerator-list. The enumerators all have the indicated
15607 TYPE.
15609 enumerator-list:
15610 enumerator-definition
15611 enumerator-list , enumerator-definition */
15613 static void
15614 cp_parser_enumerator_list (cp_parser* parser, tree type)
15616 while (true)
15618 /* Parse an enumerator-definition. */
15619 cp_parser_enumerator_definition (parser, type);
15621 /* If the next token is not a ',', we've reached the end of
15622 the list. */
15623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15624 break;
15625 /* Otherwise, consume the `,' and keep going. */
15626 cp_lexer_consume_token (parser->lexer);
15627 /* If the next token is a `}', there is a trailing comma. */
15628 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15630 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15631 pedwarn (input_location, OPT_Wpedantic,
15632 "comma at end of enumerator list");
15633 break;
15638 /* Parse an enumerator-definition. The enumerator has the indicated
15639 TYPE.
15641 enumerator-definition:
15642 enumerator
15643 enumerator = constant-expression
15645 enumerator:
15646 identifier */
15648 static void
15649 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15651 tree identifier;
15652 tree value;
15653 location_t loc;
15655 /* Save the input location because we are interested in the location
15656 of the identifier and not the location of the explicit value. */
15657 loc = cp_lexer_peek_token (parser->lexer)->location;
15659 /* Look for the identifier. */
15660 identifier = cp_parser_identifier (parser);
15661 if (identifier == error_mark_node)
15662 return;
15664 /* If the next token is an '=', then there is an explicit value. */
15665 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15667 /* Consume the `=' token. */
15668 cp_lexer_consume_token (parser->lexer);
15669 /* Parse the value. */
15670 value = cp_parser_constant_expression (parser,
15671 /*allow_non_constant_p=*/false,
15672 NULL);
15674 else
15675 value = NULL_TREE;
15677 /* If we are processing a template, make sure the initializer of the
15678 enumerator doesn't contain any bare template parameter pack. */
15679 if (check_for_bare_parameter_packs (value))
15680 value = error_mark_node;
15682 /* integral_constant_value will pull out this expression, so make sure
15683 it's folded as appropriate. */
15684 value = fold_non_dependent_expr (value);
15686 /* Create the enumerator. */
15687 build_enumerator (identifier, value, type, loc);
15690 /* Parse a namespace-name.
15692 namespace-name:
15693 original-namespace-name
15694 namespace-alias
15696 Returns the NAMESPACE_DECL for the namespace. */
15698 static tree
15699 cp_parser_namespace_name (cp_parser* parser)
15701 tree identifier;
15702 tree namespace_decl;
15704 cp_token *token = cp_lexer_peek_token (parser->lexer);
15706 /* Get the name of the namespace. */
15707 identifier = cp_parser_identifier (parser);
15708 if (identifier == error_mark_node)
15709 return error_mark_node;
15711 /* Look up the identifier in the currently active scope. Look only
15712 for namespaces, due to:
15714 [basic.lookup.udir]
15716 When looking up a namespace-name in a using-directive or alias
15717 definition, only namespace names are considered.
15719 And:
15721 [basic.lookup.qual]
15723 During the lookup of a name preceding the :: scope resolution
15724 operator, object, function, and enumerator names are ignored.
15726 (Note that cp_parser_qualifying_entity only calls this
15727 function if the token after the name is the scope resolution
15728 operator.) */
15729 namespace_decl = cp_parser_lookup_name (parser, identifier,
15730 none_type,
15731 /*is_template=*/false,
15732 /*is_namespace=*/true,
15733 /*check_dependency=*/true,
15734 /*ambiguous_decls=*/NULL,
15735 token->location);
15736 /* If it's not a namespace, issue an error. */
15737 if (namespace_decl == error_mark_node
15738 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15740 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15741 error_at (token->location, "%qD is not a namespace-name", identifier);
15742 cp_parser_error (parser, "expected namespace-name");
15743 namespace_decl = error_mark_node;
15746 return namespace_decl;
15749 /* Parse a namespace-definition.
15751 namespace-definition:
15752 named-namespace-definition
15753 unnamed-namespace-definition
15755 named-namespace-definition:
15756 original-namespace-definition
15757 extension-namespace-definition
15759 original-namespace-definition:
15760 namespace identifier { namespace-body }
15762 extension-namespace-definition:
15763 namespace original-namespace-name { namespace-body }
15765 unnamed-namespace-definition:
15766 namespace { namespace-body } */
15768 static void
15769 cp_parser_namespace_definition (cp_parser* parser)
15771 tree identifier, attribs;
15772 bool has_visibility;
15773 bool is_inline;
15775 cp_ensure_no_omp_declare_simd (parser);
15776 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15778 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15779 is_inline = true;
15780 cp_lexer_consume_token (parser->lexer);
15782 else
15783 is_inline = false;
15785 /* Look for the `namespace' keyword. */
15786 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15788 /* Get the name of the namespace. We do not attempt to distinguish
15789 between an original-namespace-definition and an
15790 extension-namespace-definition at this point. The semantic
15791 analysis routines are responsible for that. */
15792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15793 identifier = cp_parser_identifier (parser);
15794 else
15795 identifier = NULL_TREE;
15797 /* Parse any specified attributes. */
15798 attribs = cp_parser_attributes_opt (parser);
15800 /* Look for the `{' to start the namespace. */
15801 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15802 /* Start the namespace. */
15803 push_namespace (identifier);
15805 /* "inline namespace" is equivalent to a stub namespace definition
15806 followed by a strong using directive. */
15807 if (is_inline)
15809 tree name_space = current_namespace;
15810 /* Set up namespace association. */
15811 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15812 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15813 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15814 /* Import the contents of the inline namespace. */
15815 pop_namespace ();
15816 do_using_directive (name_space);
15817 push_namespace (identifier);
15820 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15822 /* Parse the body of the namespace. */
15823 cp_parser_namespace_body (parser);
15825 if (has_visibility)
15826 pop_visibility (1);
15828 /* Finish the namespace. */
15829 pop_namespace ();
15830 /* Look for the final `}'. */
15831 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15834 /* Parse a namespace-body.
15836 namespace-body:
15837 declaration-seq [opt] */
15839 static void
15840 cp_parser_namespace_body (cp_parser* parser)
15842 cp_parser_declaration_seq_opt (parser);
15845 /* Parse a namespace-alias-definition.
15847 namespace-alias-definition:
15848 namespace identifier = qualified-namespace-specifier ; */
15850 static void
15851 cp_parser_namespace_alias_definition (cp_parser* parser)
15853 tree identifier;
15854 tree namespace_specifier;
15856 cp_token *token = cp_lexer_peek_token (parser->lexer);
15858 /* Look for the `namespace' keyword. */
15859 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15860 /* Look for the identifier. */
15861 identifier = cp_parser_identifier (parser);
15862 if (identifier == error_mark_node)
15863 return;
15864 /* Look for the `=' token. */
15865 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15866 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15868 error_at (token->location, "%<namespace%> definition is not allowed here");
15869 /* Skip the definition. */
15870 cp_lexer_consume_token (parser->lexer);
15871 if (cp_parser_skip_to_closing_brace (parser))
15872 cp_lexer_consume_token (parser->lexer);
15873 return;
15875 cp_parser_require (parser, CPP_EQ, RT_EQ);
15876 /* Look for the qualified-namespace-specifier. */
15877 namespace_specifier
15878 = cp_parser_qualified_namespace_specifier (parser);
15879 /* Look for the `;' token. */
15880 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15882 /* Register the alias in the symbol table. */
15883 do_namespace_alias (identifier, namespace_specifier);
15886 /* Parse a qualified-namespace-specifier.
15888 qualified-namespace-specifier:
15889 :: [opt] nested-name-specifier [opt] namespace-name
15891 Returns a NAMESPACE_DECL corresponding to the specified
15892 namespace. */
15894 static tree
15895 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15897 /* Look for the optional `::'. */
15898 cp_parser_global_scope_opt (parser,
15899 /*current_scope_valid_p=*/false);
15901 /* Look for the optional nested-name-specifier. */
15902 cp_parser_nested_name_specifier_opt (parser,
15903 /*typename_keyword_p=*/false,
15904 /*check_dependency_p=*/true,
15905 /*type_p=*/false,
15906 /*is_declaration=*/true);
15908 return cp_parser_namespace_name (parser);
15911 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15912 access declaration.
15914 using-declaration:
15915 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15916 using :: unqualified-id ;
15918 access-declaration:
15919 qualified-id ;
15923 static bool
15924 cp_parser_using_declaration (cp_parser* parser,
15925 bool access_declaration_p)
15927 cp_token *token;
15928 bool typename_p = false;
15929 bool global_scope_p;
15930 tree decl;
15931 tree identifier;
15932 tree qscope;
15933 int oldcount = errorcount;
15934 cp_token *diag_token = NULL;
15936 if (access_declaration_p)
15938 diag_token = cp_lexer_peek_token (parser->lexer);
15939 cp_parser_parse_tentatively (parser);
15941 else
15943 /* Look for the `using' keyword. */
15944 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15946 /* Peek at the next token. */
15947 token = cp_lexer_peek_token (parser->lexer);
15948 /* See if it's `typename'. */
15949 if (token->keyword == RID_TYPENAME)
15951 /* Remember that we've seen it. */
15952 typename_p = true;
15953 /* Consume the `typename' token. */
15954 cp_lexer_consume_token (parser->lexer);
15958 /* Look for the optional global scope qualification. */
15959 global_scope_p
15960 = (cp_parser_global_scope_opt (parser,
15961 /*current_scope_valid_p=*/false)
15962 != NULL_TREE);
15964 /* If we saw `typename', or didn't see `::', then there must be a
15965 nested-name-specifier present. */
15966 if (typename_p || !global_scope_p)
15968 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15969 /*check_dependency_p=*/true,
15970 /*type_p=*/false,
15971 /*is_declaration=*/true);
15972 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
15974 cp_parser_skip_to_end_of_block_or_statement (parser);
15975 return false;
15978 /* Otherwise, we could be in either of the two productions. In that
15979 case, treat the nested-name-specifier as optional. */
15980 else
15981 qscope = cp_parser_nested_name_specifier_opt (parser,
15982 /*typename_keyword_p=*/false,
15983 /*check_dependency_p=*/true,
15984 /*type_p=*/false,
15985 /*is_declaration=*/true);
15986 if (!qscope)
15987 qscope = global_namespace;
15989 if (access_declaration_p && cp_parser_error_occurred (parser))
15990 /* Something has already gone wrong; there's no need to parse
15991 further. Since an error has occurred, the return value of
15992 cp_parser_parse_definitely will be false, as required. */
15993 return cp_parser_parse_definitely (parser);
15995 token = cp_lexer_peek_token (parser->lexer);
15996 /* Parse the unqualified-id. */
15997 identifier = cp_parser_unqualified_id (parser,
15998 /*template_keyword_p=*/false,
15999 /*check_dependency_p=*/true,
16000 /*declarator_p=*/true,
16001 /*optional_p=*/false);
16003 if (access_declaration_p)
16005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16006 cp_parser_simulate_error (parser);
16007 if (!cp_parser_parse_definitely (parser))
16008 return false;
16011 /* The function we call to handle a using-declaration is different
16012 depending on what scope we are in. */
16013 if (qscope == error_mark_node || identifier == error_mark_node)
16015 else if (!identifier_p (identifier)
16016 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16017 /* [namespace.udecl]
16019 A using declaration shall not name a template-id. */
16020 error_at (token->location,
16021 "a template-id may not appear in a using-declaration");
16022 else
16024 if (at_class_scope_p ())
16026 /* Create the USING_DECL. */
16027 decl = do_class_using_decl (parser->scope, identifier);
16029 if (decl && typename_p)
16030 USING_DECL_TYPENAME_P (decl) = 1;
16032 if (check_for_bare_parameter_packs (decl))
16034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16035 return false;
16037 else
16038 /* Add it to the list of members in this class. */
16039 finish_member_declaration (decl);
16041 else
16043 decl = cp_parser_lookup_name_simple (parser,
16044 identifier,
16045 token->location);
16046 if (decl == error_mark_node)
16047 cp_parser_name_lookup_error (parser, identifier,
16048 decl, NLE_NULL,
16049 token->location);
16050 else if (check_for_bare_parameter_packs (decl))
16052 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16053 return false;
16055 else if (!at_namespace_scope_p ())
16056 do_local_using_decl (decl, qscope, identifier);
16057 else
16058 do_toplevel_using_decl (decl, qscope, identifier);
16062 /* Look for the final `;'. */
16063 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16065 if (access_declaration_p && errorcount == oldcount)
16066 warning_at (diag_token->location, OPT_Wdeprecated,
16067 "access declarations are deprecated "
16068 "in favour of using-declarations; "
16069 "suggestion: add the %<using%> keyword");
16071 return true;
16074 /* Parse an alias-declaration.
16076 alias-declaration:
16077 using identifier attribute-specifier-seq [opt] = type-id */
16079 static tree
16080 cp_parser_alias_declaration (cp_parser* parser)
16082 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16083 location_t id_location;
16084 cp_declarator *declarator;
16085 cp_decl_specifier_seq decl_specs;
16086 bool member_p;
16087 const char *saved_message = NULL;
16089 /* Look for the `using' keyword. */
16090 cp_token *using_token
16091 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16092 if (using_token == NULL)
16093 return error_mark_node;
16095 id_location = cp_lexer_peek_token (parser->lexer)->location;
16096 id = cp_parser_identifier (parser);
16097 if (id == error_mark_node)
16098 return error_mark_node;
16100 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16101 attributes = cp_parser_attributes_opt (parser);
16102 if (attributes == error_mark_node)
16103 return error_mark_node;
16105 cp_parser_require (parser, CPP_EQ, RT_EQ);
16107 if (cp_parser_error_occurred (parser))
16108 return error_mark_node;
16110 cp_parser_commit_to_tentative_parse (parser);
16112 /* Now we are going to parse the type-id of the declaration. */
16115 [dcl.type]/3 says:
16117 "A type-specifier-seq shall not define a class or enumeration
16118 unless it appears in the type-id of an alias-declaration (7.1.3) that
16119 is not the declaration of a template-declaration."
16121 In other words, if we currently are in an alias template, the
16122 type-id should not define a type.
16124 So let's set parser->type_definition_forbidden_message in that
16125 case; cp_parser_check_type_definition (called by
16126 cp_parser_class_specifier) will then emit an error if a type is
16127 defined in the type-id. */
16128 if (parser->num_template_parameter_lists)
16130 saved_message = parser->type_definition_forbidden_message;
16131 parser->type_definition_forbidden_message =
16132 G_("types may not be defined in alias template declarations");
16135 type = cp_parser_type_id (parser);
16137 /* Restore the error message if need be. */
16138 if (parser->num_template_parameter_lists)
16139 parser->type_definition_forbidden_message = saved_message;
16141 if (type == error_mark_node)
16143 cp_parser_skip_to_end_of_block_or_statement (parser);
16144 return error_mark_node;
16147 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16149 if (cp_parser_error_occurred (parser))
16151 cp_parser_skip_to_end_of_block_or_statement (parser);
16152 return error_mark_node;
16155 /* A typedef-name can also be introduced by an alias-declaration. The
16156 identifier following the using keyword becomes a typedef-name. It has
16157 the same semantics as if it were introduced by the typedef
16158 specifier. In particular, it does not define a new type and it shall
16159 not appear in the type-id. */
16161 clear_decl_specs (&decl_specs);
16162 decl_specs.type = type;
16163 if (attributes != NULL_TREE)
16165 decl_specs.attributes = attributes;
16166 set_and_check_decl_spec_loc (&decl_specs,
16167 ds_attribute,
16168 attrs_token);
16170 set_and_check_decl_spec_loc (&decl_specs,
16171 ds_typedef,
16172 using_token);
16173 set_and_check_decl_spec_loc (&decl_specs,
16174 ds_alias,
16175 using_token);
16177 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16178 declarator->id_loc = id_location;
16180 member_p = at_class_scope_p ();
16181 if (member_p)
16182 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16183 NULL_TREE, attributes);
16184 else
16185 decl = start_decl (declarator, &decl_specs, 0,
16186 attributes, NULL_TREE, &pushed_scope);
16187 if (decl == error_mark_node)
16188 return decl;
16190 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16192 if (pushed_scope)
16193 pop_scope (pushed_scope);
16195 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16196 added into the symbol table; otherwise, return the TYPE_DECL. */
16197 if (DECL_LANG_SPECIFIC (decl)
16198 && DECL_TEMPLATE_INFO (decl)
16199 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16201 decl = DECL_TI_TEMPLATE (decl);
16202 if (member_p)
16203 check_member_template (decl);
16206 return decl;
16209 /* Parse a using-directive.
16211 using-directive:
16212 using namespace :: [opt] nested-name-specifier [opt]
16213 namespace-name ; */
16215 static void
16216 cp_parser_using_directive (cp_parser* parser)
16218 tree namespace_decl;
16219 tree attribs;
16221 /* Look for the `using' keyword. */
16222 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16223 /* And the `namespace' keyword. */
16224 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16225 /* Look for the optional `::' operator. */
16226 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16227 /* And the optional nested-name-specifier. */
16228 cp_parser_nested_name_specifier_opt (parser,
16229 /*typename_keyword_p=*/false,
16230 /*check_dependency_p=*/true,
16231 /*type_p=*/false,
16232 /*is_declaration=*/true);
16233 /* Get the namespace being used. */
16234 namespace_decl = cp_parser_namespace_name (parser);
16235 /* And any specified attributes. */
16236 attribs = cp_parser_attributes_opt (parser);
16237 /* Update the symbol table. */
16238 parse_using_directive (namespace_decl, attribs);
16239 /* Look for the final `;'. */
16240 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16243 /* Parse an asm-definition.
16245 asm-definition:
16246 asm ( string-literal ) ;
16248 GNU Extension:
16250 asm-definition:
16251 asm volatile [opt] ( string-literal ) ;
16252 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16253 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16254 : asm-operand-list [opt] ) ;
16255 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16256 : asm-operand-list [opt]
16257 : asm-clobber-list [opt] ) ;
16258 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16259 : asm-clobber-list [opt]
16260 : asm-goto-list ) ; */
16262 static void
16263 cp_parser_asm_definition (cp_parser* parser)
16265 tree string;
16266 tree outputs = NULL_TREE;
16267 tree inputs = NULL_TREE;
16268 tree clobbers = NULL_TREE;
16269 tree labels = NULL_TREE;
16270 tree asm_stmt;
16271 bool volatile_p = false;
16272 bool extended_p = false;
16273 bool invalid_inputs_p = false;
16274 bool invalid_outputs_p = false;
16275 bool goto_p = false;
16276 required_token missing = RT_NONE;
16278 /* Look for the `asm' keyword. */
16279 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16280 /* See if the next token is `volatile'. */
16281 if (cp_parser_allow_gnu_extensions_p (parser)
16282 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16284 /* Remember that we saw the `volatile' keyword. */
16285 volatile_p = true;
16286 /* Consume the token. */
16287 cp_lexer_consume_token (parser->lexer);
16289 if (cp_parser_allow_gnu_extensions_p (parser)
16290 && parser->in_function_body
16291 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16293 /* Remember that we saw the `goto' keyword. */
16294 goto_p = true;
16295 /* Consume the token. */
16296 cp_lexer_consume_token (parser->lexer);
16298 /* Look for the opening `('. */
16299 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16300 return;
16301 /* Look for the string. */
16302 string = cp_parser_string_literal (parser, false, false);
16303 if (string == error_mark_node)
16305 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16306 /*consume_paren=*/true);
16307 return;
16310 /* If we're allowing GNU extensions, check for the extended assembly
16311 syntax. Unfortunately, the `:' tokens need not be separated by
16312 a space in C, and so, for compatibility, we tolerate that here
16313 too. Doing that means that we have to treat the `::' operator as
16314 two `:' tokens. */
16315 if (cp_parser_allow_gnu_extensions_p (parser)
16316 && parser->in_function_body
16317 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16318 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16320 bool inputs_p = false;
16321 bool clobbers_p = false;
16322 bool labels_p = false;
16324 /* The extended syntax was used. */
16325 extended_p = true;
16327 /* Look for outputs. */
16328 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16330 /* Consume the `:'. */
16331 cp_lexer_consume_token (parser->lexer);
16332 /* Parse the output-operands. */
16333 if (cp_lexer_next_token_is_not (parser->lexer,
16334 CPP_COLON)
16335 && cp_lexer_next_token_is_not (parser->lexer,
16336 CPP_SCOPE)
16337 && cp_lexer_next_token_is_not (parser->lexer,
16338 CPP_CLOSE_PAREN)
16339 && !goto_p)
16340 outputs = cp_parser_asm_operand_list (parser);
16342 if (outputs == error_mark_node)
16343 invalid_outputs_p = true;
16345 /* If the next token is `::', there are no outputs, and the
16346 next token is the beginning of the inputs. */
16347 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16348 /* The inputs are coming next. */
16349 inputs_p = true;
16351 /* Look for inputs. */
16352 if (inputs_p
16353 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16355 /* Consume the `:' or `::'. */
16356 cp_lexer_consume_token (parser->lexer);
16357 /* Parse the output-operands. */
16358 if (cp_lexer_next_token_is_not (parser->lexer,
16359 CPP_COLON)
16360 && cp_lexer_next_token_is_not (parser->lexer,
16361 CPP_SCOPE)
16362 && cp_lexer_next_token_is_not (parser->lexer,
16363 CPP_CLOSE_PAREN))
16364 inputs = cp_parser_asm_operand_list (parser);
16366 if (inputs == error_mark_node)
16367 invalid_inputs_p = true;
16369 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16370 /* The clobbers are coming next. */
16371 clobbers_p = true;
16373 /* Look for clobbers. */
16374 if (clobbers_p
16375 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16377 clobbers_p = true;
16378 /* Consume the `:' or `::'. */
16379 cp_lexer_consume_token (parser->lexer);
16380 /* Parse the clobbers. */
16381 if (cp_lexer_next_token_is_not (parser->lexer,
16382 CPP_COLON)
16383 && cp_lexer_next_token_is_not (parser->lexer,
16384 CPP_CLOSE_PAREN))
16385 clobbers = cp_parser_asm_clobber_list (parser);
16387 else if (goto_p
16388 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16389 /* The labels are coming next. */
16390 labels_p = true;
16392 /* Look for labels. */
16393 if (labels_p
16394 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16396 labels_p = true;
16397 /* Consume the `:' or `::'. */
16398 cp_lexer_consume_token (parser->lexer);
16399 /* Parse the labels. */
16400 labels = cp_parser_asm_label_list (parser);
16403 if (goto_p && !labels_p)
16404 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16406 else if (goto_p)
16407 missing = RT_COLON_SCOPE;
16409 /* Look for the closing `)'. */
16410 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16411 missing ? missing : RT_CLOSE_PAREN))
16412 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16413 /*consume_paren=*/true);
16414 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16416 if (!invalid_inputs_p && !invalid_outputs_p)
16418 /* Create the ASM_EXPR. */
16419 if (parser->in_function_body)
16421 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16422 inputs, clobbers, labels);
16423 /* If the extended syntax was not used, mark the ASM_EXPR. */
16424 if (!extended_p)
16426 tree temp = asm_stmt;
16427 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16428 temp = TREE_OPERAND (temp, 0);
16430 ASM_INPUT_P (temp) = 1;
16433 else
16434 add_asm_node (string);
16438 /* Declarators [gram.dcl.decl] */
16440 /* Parse an init-declarator.
16442 init-declarator:
16443 declarator initializer [opt]
16445 GNU Extension:
16447 init-declarator:
16448 declarator asm-specification [opt] attributes [opt] initializer [opt]
16450 function-definition:
16451 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16452 function-body
16453 decl-specifier-seq [opt] declarator function-try-block
16455 GNU Extension:
16457 function-definition:
16458 __extension__ function-definition
16460 TM Extension:
16462 function-definition:
16463 decl-specifier-seq [opt] declarator function-transaction-block
16465 The DECL_SPECIFIERS apply to this declarator. Returns a
16466 representation of the entity declared. If MEMBER_P is TRUE, then
16467 this declarator appears in a class scope. The new DECL created by
16468 this declarator is returned.
16470 The CHECKS are access checks that should be performed once we know
16471 what entity is being declared (and, therefore, what classes have
16472 befriended it).
16474 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16475 for a function-definition here as well. If the declarator is a
16476 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16477 be TRUE upon return. By that point, the function-definition will
16478 have been completely parsed.
16480 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16481 is FALSE.
16483 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16484 parsed declaration if it is an uninitialized single declarator not followed
16485 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16486 if present, will not be consumed. If returned, this declarator will be
16487 created with SD_INITIALIZED but will not call cp_finish_decl. */
16489 static tree
16490 cp_parser_init_declarator (cp_parser* parser,
16491 cp_decl_specifier_seq *decl_specifiers,
16492 vec<deferred_access_check, va_gc> *checks,
16493 bool function_definition_allowed_p,
16494 bool member_p,
16495 int declares_class_or_enum,
16496 bool* function_definition_p,
16497 tree* maybe_range_for_decl)
16499 cp_token *token = NULL, *asm_spec_start_token = NULL,
16500 *attributes_start_token = NULL;
16501 cp_declarator *declarator;
16502 tree prefix_attributes;
16503 tree attributes = NULL;
16504 tree asm_specification;
16505 tree initializer;
16506 tree decl = NULL_TREE;
16507 tree scope;
16508 int is_initialized;
16509 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16510 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16511 "(...)". */
16512 enum cpp_ttype initialization_kind;
16513 bool is_direct_init = false;
16514 bool is_non_constant_init;
16515 int ctor_dtor_or_conv_p;
16516 bool friend_p;
16517 tree pushed_scope = NULL_TREE;
16518 bool range_for_decl_p = false;
16519 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16521 /* Gather the attributes that were provided with the
16522 decl-specifiers. */
16523 prefix_attributes = decl_specifiers->attributes;
16525 /* Assume that this is not the declarator for a function
16526 definition. */
16527 if (function_definition_p)
16528 *function_definition_p = false;
16530 /* Default arguments are only permitted for function parameters. */
16531 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16532 parser->default_arg_ok_p = false;
16534 /* Defer access checks while parsing the declarator; we cannot know
16535 what names are accessible until we know what is being
16536 declared. */
16537 resume_deferring_access_checks ();
16539 /* Parse the declarator. */
16540 token = cp_lexer_peek_token (parser->lexer);
16541 declarator
16542 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16543 &ctor_dtor_or_conv_p,
16544 /*parenthesized_p=*/NULL,
16545 member_p);
16546 /* Gather up the deferred checks. */
16547 stop_deferring_access_checks ();
16549 parser->default_arg_ok_p = saved_default_arg_ok_p;
16551 /* If the DECLARATOR was erroneous, there's no need to go
16552 further. */
16553 if (declarator == cp_error_declarator)
16554 return error_mark_node;
16556 /* Check that the number of template-parameter-lists is OK. */
16557 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16558 token->location))
16559 return error_mark_node;
16561 if (declares_class_or_enum & 2)
16562 cp_parser_check_for_definition_in_return_type (declarator,
16563 decl_specifiers->type,
16564 decl_specifiers->locations[ds_type_spec]);
16566 /* Figure out what scope the entity declared by the DECLARATOR is
16567 located in. `grokdeclarator' sometimes changes the scope, so
16568 we compute it now. */
16569 scope = get_scope_of_declarator (declarator);
16571 /* Perform any lookups in the declared type which were thought to be
16572 dependent, but are not in the scope of the declarator. */
16573 decl_specifiers->type
16574 = maybe_update_decl_type (decl_specifiers->type, scope);
16576 /* If we're allowing GNU extensions, look for an
16577 asm-specification. */
16578 if (cp_parser_allow_gnu_extensions_p (parser))
16580 /* Look for an asm-specification. */
16581 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16582 asm_specification = cp_parser_asm_specification_opt (parser);
16584 else
16585 asm_specification = NULL_TREE;
16587 /* Look for attributes. */
16588 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16589 attributes = cp_parser_attributes_opt (parser);
16591 /* Peek at the next token. */
16592 token = cp_lexer_peek_token (parser->lexer);
16594 if (function_declarator_p (declarator))
16596 /* Check to see if the token indicates the start of a
16597 function-definition. */
16598 if (cp_parser_token_starts_function_definition_p (token))
16600 if (!function_definition_allowed_p)
16602 /* If a function-definition should not appear here, issue an
16603 error message. */
16604 cp_parser_error (parser,
16605 "a function-definition is not allowed here");
16606 return error_mark_node;
16609 location_t func_brace_location
16610 = cp_lexer_peek_token (parser->lexer)->location;
16612 /* Neither attributes nor an asm-specification are allowed
16613 on a function-definition. */
16614 if (asm_specification)
16615 error_at (asm_spec_start_token->location,
16616 "an asm-specification is not allowed "
16617 "on a function-definition");
16618 if (attributes)
16619 error_at (attributes_start_token->location,
16620 "attributes are not allowed "
16621 "on a function-definition");
16622 /* This is a function-definition. */
16623 *function_definition_p = true;
16625 /* Parse the function definition. */
16626 if (member_p)
16627 decl = cp_parser_save_member_function_body (parser,
16628 decl_specifiers,
16629 declarator,
16630 prefix_attributes);
16631 else
16632 decl =
16633 (cp_parser_function_definition_from_specifiers_and_declarator
16634 (parser, decl_specifiers, prefix_attributes, declarator));
16636 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16638 /* This is where the prologue starts... */
16639 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16640 = func_brace_location;
16643 return decl;
16647 /* [dcl.dcl]
16649 Only in function declarations for constructors, destructors, and
16650 type conversions can the decl-specifier-seq be omitted.
16652 We explicitly postpone this check past the point where we handle
16653 function-definitions because we tolerate function-definitions
16654 that are missing their return types in some modes. */
16655 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16657 cp_parser_error (parser,
16658 "expected constructor, destructor, or type conversion");
16659 return error_mark_node;
16662 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16663 if (token->type == CPP_EQ
16664 || token->type == CPP_OPEN_PAREN
16665 || token->type == CPP_OPEN_BRACE)
16667 is_initialized = SD_INITIALIZED;
16668 initialization_kind = token->type;
16669 if (maybe_range_for_decl)
16670 *maybe_range_for_decl = error_mark_node;
16672 if (token->type == CPP_EQ
16673 && function_declarator_p (declarator))
16675 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16676 if (t2->keyword == RID_DEFAULT)
16677 is_initialized = SD_DEFAULTED;
16678 else if (t2->keyword == RID_DELETE)
16679 is_initialized = SD_DELETED;
16682 else
16684 /* If the init-declarator isn't initialized and isn't followed by a
16685 `,' or `;', it's not a valid init-declarator. */
16686 if (token->type != CPP_COMMA
16687 && token->type != CPP_SEMICOLON)
16689 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16690 range_for_decl_p = true;
16691 else
16693 cp_parser_error (parser, "expected initializer");
16694 return error_mark_node;
16697 is_initialized = SD_UNINITIALIZED;
16698 initialization_kind = CPP_EOF;
16701 /* Because start_decl has side-effects, we should only call it if we
16702 know we're going ahead. By this point, we know that we cannot
16703 possibly be looking at any other construct. */
16704 cp_parser_commit_to_tentative_parse (parser);
16706 /* If the decl specifiers were bad, issue an error now that we're
16707 sure this was intended to be a declarator. Then continue
16708 declaring the variable(s), as int, to try to cut down on further
16709 errors. */
16710 if (decl_specifiers->any_specifiers_p
16711 && decl_specifiers->type == error_mark_node)
16713 cp_parser_error (parser, "invalid type in declaration");
16714 decl_specifiers->type = integer_type_node;
16717 /* Check to see whether or not this declaration is a friend. */
16718 friend_p = cp_parser_friend_p (decl_specifiers);
16720 /* Enter the newly declared entry in the symbol table. If we're
16721 processing a declaration in a class-specifier, we wait until
16722 after processing the initializer. */
16723 if (!member_p)
16725 if (parser->in_unbraced_linkage_specification_p)
16726 decl_specifiers->storage_class = sc_extern;
16727 decl = start_decl (declarator, decl_specifiers,
16728 range_for_decl_p? SD_INITIALIZED : is_initialized,
16729 attributes, prefix_attributes, &pushed_scope);
16730 cp_finalize_omp_declare_simd (parser, decl);
16731 /* Adjust location of decl if declarator->id_loc is more appropriate:
16732 set, and decl wasn't merged with another decl, in which case its
16733 location would be different from input_location, and more accurate. */
16734 if (DECL_P (decl)
16735 && declarator->id_loc != UNKNOWN_LOCATION
16736 && DECL_SOURCE_LOCATION (decl) == input_location)
16737 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16739 else if (scope)
16740 /* Enter the SCOPE. That way unqualified names appearing in the
16741 initializer will be looked up in SCOPE. */
16742 pushed_scope = push_scope (scope);
16744 /* Perform deferred access control checks, now that we know in which
16745 SCOPE the declared entity resides. */
16746 if (!member_p && decl)
16748 tree saved_current_function_decl = NULL_TREE;
16750 /* If the entity being declared is a function, pretend that we
16751 are in its scope. If it is a `friend', it may have access to
16752 things that would not otherwise be accessible. */
16753 if (TREE_CODE (decl) == FUNCTION_DECL)
16755 saved_current_function_decl = current_function_decl;
16756 current_function_decl = decl;
16759 /* Perform access checks for template parameters. */
16760 cp_parser_perform_template_parameter_access_checks (checks);
16762 /* Perform the access control checks for the declarator and the
16763 decl-specifiers. */
16764 perform_deferred_access_checks (tf_warning_or_error);
16766 /* Restore the saved value. */
16767 if (TREE_CODE (decl) == FUNCTION_DECL)
16768 current_function_decl = saved_current_function_decl;
16771 /* Parse the initializer. */
16772 initializer = NULL_TREE;
16773 is_direct_init = false;
16774 is_non_constant_init = true;
16775 if (is_initialized)
16777 if (function_declarator_p (declarator))
16779 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16780 if (initialization_kind == CPP_EQ)
16781 initializer = cp_parser_pure_specifier (parser);
16782 else
16784 /* If the declaration was erroneous, we don't really
16785 know what the user intended, so just silently
16786 consume the initializer. */
16787 if (decl != error_mark_node)
16788 error_at (initializer_start_token->location,
16789 "initializer provided for function");
16790 cp_parser_skip_to_closing_parenthesis (parser,
16791 /*recovering=*/true,
16792 /*or_comma=*/false,
16793 /*consume_paren=*/true);
16796 else
16798 /* We want to record the extra mangling scope for in-class
16799 initializers of class members and initializers of static data
16800 member templates. The former involves deferring
16801 parsing of the initializer until end of class as with default
16802 arguments. So right here we only handle the latter. */
16803 if (!member_p && processing_template_decl)
16804 start_lambda_scope (decl);
16805 initializer = cp_parser_initializer (parser,
16806 &is_direct_init,
16807 &is_non_constant_init);
16808 if (!member_p && processing_template_decl)
16809 finish_lambda_scope ();
16810 if (initializer == error_mark_node)
16811 cp_parser_skip_to_end_of_statement (parser);
16815 /* The old parser allows attributes to appear after a parenthesized
16816 initializer. Mark Mitchell proposed removing this functionality
16817 on the GCC mailing lists on 2002-08-13. This parser accepts the
16818 attributes -- but ignores them. */
16819 if (cp_parser_allow_gnu_extensions_p (parser)
16820 && initialization_kind == CPP_OPEN_PAREN)
16821 if (cp_parser_attributes_opt (parser))
16822 warning (OPT_Wattributes,
16823 "attributes after parenthesized initializer ignored");
16825 /* A non-template declaration involving a function parameter list containing
16826 an implicit template parameter will have been made into a template. If it
16827 turns out that the resulting declaration is not an actual function then
16828 finish the template declaration here. An error message will already have
16829 been issued. */
16830 if (parser->fully_implicit_function_template_p)
16831 if (!function_declarator_p (declarator))
16832 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16834 /* For an in-class declaration, use `grokfield' to create the
16835 declaration. */
16836 if (member_p)
16838 if (pushed_scope)
16840 pop_scope (pushed_scope);
16841 pushed_scope = NULL_TREE;
16843 decl = grokfield (declarator, decl_specifiers,
16844 initializer, !is_non_constant_init,
16845 /*asmspec=*/NULL_TREE,
16846 chainon (attributes, prefix_attributes));
16847 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16848 cp_parser_save_default_args (parser, decl);
16849 cp_finalize_omp_declare_simd (parser, decl);
16852 /* Finish processing the declaration. But, skip member
16853 declarations. */
16854 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16856 cp_finish_decl (decl,
16857 initializer, !is_non_constant_init,
16858 asm_specification,
16859 /* If the initializer is in parentheses, then this is
16860 a direct-initialization, which means that an
16861 `explicit' constructor is OK. Otherwise, an
16862 `explicit' constructor cannot be used. */
16863 ((is_direct_init || !is_initialized)
16864 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16866 else if ((cxx_dialect != cxx98) && friend_p
16867 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16868 /* Core issue #226 (C++0x only): A default template-argument
16869 shall not be specified in a friend class template
16870 declaration. */
16871 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16872 /*is_partial=*/false, /*is_friend_decl=*/1);
16874 if (!friend_p && pushed_scope)
16875 pop_scope (pushed_scope);
16877 if (function_declarator_p (declarator)
16878 && parser->fully_implicit_function_template_p)
16880 if (member_p)
16881 decl = finish_fully_implicit_template (parser, decl);
16882 else
16883 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16886 return decl;
16889 /* Parse a declarator.
16891 declarator:
16892 direct-declarator
16893 ptr-operator declarator
16895 abstract-declarator:
16896 ptr-operator abstract-declarator [opt]
16897 direct-abstract-declarator
16899 GNU Extensions:
16901 declarator:
16902 attributes [opt] direct-declarator
16903 attributes [opt] ptr-operator declarator
16905 abstract-declarator:
16906 attributes [opt] ptr-operator abstract-declarator [opt]
16907 attributes [opt] direct-abstract-declarator
16909 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16910 detect constructor, destructor or conversion operators. It is set
16911 to -1 if the declarator is a name, and +1 if it is a
16912 function. Otherwise it is set to zero. Usually you just want to
16913 test for >0, but internally the negative value is used.
16915 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16916 a decl-specifier-seq unless it declares a constructor, destructor,
16917 or conversion. It might seem that we could check this condition in
16918 semantic analysis, rather than parsing, but that makes it difficult
16919 to handle something like `f()'. We want to notice that there are
16920 no decl-specifiers, and therefore realize that this is an
16921 expression, not a declaration.)
16923 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16924 the declarator is a direct-declarator of the form "(...)".
16926 MEMBER_P is true iff this declarator is a member-declarator. */
16928 static cp_declarator *
16929 cp_parser_declarator (cp_parser* parser,
16930 cp_parser_declarator_kind dcl_kind,
16931 int* ctor_dtor_or_conv_p,
16932 bool* parenthesized_p,
16933 bool member_p)
16935 cp_declarator *declarator;
16936 enum tree_code code;
16937 cp_cv_quals cv_quals;
16938 tree class_type;
16939 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16941 /* Assume this is not a constructor, destructor, or type-conversion
16942 operator. */
16943 if (ctor_dtor_or_conv_p)
16944 *ctor_dtor_or_conv_p = 0;
16946 if (cp_parser_allow_gnu_extensions_p (parser))
16947 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16949 /* Check for the ptr-operator production. */
16950 cp_parser_parse_tentatively (parser);
16951 /* Parse the ptr-operator. */
16952 code = cp_parser_ptr_operator (parser,
16953 &class_type,
16954 &cv_quals,
16955 &std_attributes);
16957 /* If that worked, then we have a ptr-operator. */
16958 if (cp_parser_parse_definitely (parser))
16960 /* If a ptr-operator was found, then this declarator was not
16961 parenthesized. */
16962 if (parenthesized_p)
16963 *parenthesized_p = true;
16964 /* The dependent declarator is optional if we are parsing an
16965 abstract-declarator. */
16966 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16967 cp_parser_parse_tentatively (parser);
16969 /* Parse the dependent declarator. */
16970 declarator = cp_parser_declarator (parser, dcl_kind,
16971 /*ctor_dtor_or_conv_p=*/NULL,
16972 /*parenthesized_p=*/NULL,
16973 /*member_p=*/false);
16975 /* If we are parsing an abstract-declarator, we must handle the
16976 case where the dependent declarator is absent. */
16977 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16978 && !cp_parser_parse_definitely (parser))
16979 declarator = NULL;
16981 declarator = cp_parser_make_indirect_declarator
16982 (code, class_type, cv_quals, declarator, std_attributes);
16984 /* Everything else is a direct-declarator. */
16985 else
16987 if (parenthesized_p)
16988 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16989 CPP_OPEN_PAREN);
16990 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16991 ctor_dtor_or_conv_p,
16992 member_p);
16995 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16996 declarator->attributes = gnu_attributes;
16997 return declarator;
17000 /* Parse a direct-declarator or direct-abstract-declarator.
17002 direct-declarator:
17003 declarator-id
17004 direct-declarator ( parameter-declaration-clause )
17005 cv-qualifier-seq [opt]
17006 ref-qualifier [opt]
17007 exception-specification [opt]
17008 direct-declarator [ constant-expression [opt] ]
17009 ( declarator )
17011 direct-abstract-declarator:
17012 direct-abstract-declarator [opt]
17013 ( parameter-declaration-clause )
17014 cv-qualifier-seq [opt]
17015 ref-qualifier [opt]
17016 exception-specification [opt]
17017 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17018 ( abstract-declarator )
17020 Returns a representation of the declarator. DCL_KIND is
17021 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17022 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17023 we are parsing a direct-declarator. It is
17024 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17025 of ambiguity we prefer an abstract declarator, as per
17026 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17027 cp_parser_declarator. */
17029 static cp_declarator *
17030 cp_parser_direct_declarator (cp_parser* parser,
17031 cp_parser_declarator_kind dcl_kind,
17032 int* ctor_dtor_or_conv_p,
17033 bool member_p)
17035 cp_token *token;
17036 cp_declarator *declarator = NULL;
17037 tree scope = NULL_TREE;
17038 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17039 bool saved_in_declarator_p = parser->in_declarator_p;
17040 bool first = true;
17041 tree pushed_scope = NULL_TREE;
17043 while (true)
17045 /* Peek at the next token. */
17046 token = cp_lexer_peek_token (parser->lexer);
17047 if (token->type == CPP_OPEN_PAREN)
17049 /* This is either a parameter-declaration-clause, or a
17050 parenthesized declarator. When we know we are parsing a
17051 named declarator, it must be a parenthesized declarator
17052 if FIRST is true. For instance, `(int)' is a
17053 parameter-declaration-clause, with an omitted
17054 direct-abstract-declarator. But `((*))', is a
17055 parenthesized abstract declarator. Finally, when T is a
17056 template parameter `(T)' is a
17057 parameter-declaration-clause, and not a parenthesized
17058 named declarator.
17060 We first try and parse a parameter-declaration-clause,
17061 and then try a nested declarator (if FIRST is true).
17063 It is not an error for it not to be a
17064 parameter-declaration-clause, even when FIRST is
17065 false. Consider,
17067 int i (int);
17068 int i (3);
17070 The first is the declaration of a function while the
17071 second is the definition of a variable, including its
17072 initializer.
17074 Having seen only the parenthesis, we cannot know which of
17075 these two alternatives should be selected. Even more
17076 complex are examples like:
17078 int i (int (a));
17079 int i (int (3));
17081 The former is a function-declaration; the latter is a
17082 variable initialization.
17084 Thus again, we try a parameter-declaration-clause, and if
17085 that fails, we back out and return. */
17087 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17089 tree params;
17090 bool is_declarator = false;
17092 /* In a member-declarator, the only valid interpretation
17093 of a parenthesis is the start of a
17094 parameter-declaration-clause. (It is invalid to
17095 initialize a static data member with a parenthesized
17096 initializer; only the "=" form of initialization is
17097 permitted.) */
17098 if (!member_p)
17099 cp_parser_parse_tentatively (parser);
17101 /* Consume the `('. */
17102 cp_lexer_consume_token (parser->lexer);
17103 if (first)
17105 /* If this is going to be an abstract declarator, we're
17106 in a declarator and we can't have default args. */
17107 parser->default_arg_ok_p = false;
17108 parser->in_declarator_p = true;
17111 begin_scope (sk_function_parms, NULL_TREE);
17113 /* Parse the parameter-declaration-clause. */
17114 params = cp_parser_parameter_declaration_clause (parser);
17116 /* Consume the `)'. */
17117 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17119 /* If all went well, parse the cv-qualifier-seq,
17120 ref-qualifier and the exception-specification. */
17121 if (member_p || cp_parser_parse_definitely (parser))
17123 cp_cv_quals cv_quals;
17124 cp_virt_specifiers virt_specifiers;
17125 cp_ref_qualifier ref_qual;
17126 tree exception_specification;
17127 tree late_return;
17128 tree attrs;
17129 bool memfn = (member_p || (pushed_scope
17130 && CLASS_TYPE_P (pushed_scope)));
17132 is_declarator = true;
17134 if (ctor_dtor_or_conv_p)
17135 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17136 first = false;
17138 /* Parse the cv-qualifier-seq. */
17139 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17140 /* Parse the ref-qualifier. */
17141 ref_qual = cp_parser_ref_qualifier_opt (parser);
17142 /* And the exception-specification. */
17143 exception_specification
17144 = cp_parser_exception_specification_opt (parser);
17146 attrs = cp_parser_std_attribute_spec_seq (parser);
17148 /* In here, we handle cases where attribute is used after
17149 the function declaration. For example:
17150 void func (int x) __attribute__((vector(..))); */
17151 if (flag_cilkplus
17152 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17154 cp_parser_parse_tentatively (parser);
17155 tree attr = cp_parser_gnu_attributes_opt (parser);
17156 if (cp_lexer_next_token_is_not (parser->lexer,
17157 CPP_SEMICOLON)
17158 && cp_lexer_next_token_is_not (parser->lexer,
17159 CPP_OPEN_BRACE))
17160 cp_parser_abort_tentative_parse (parser);
17161 else if (!cp_parser_parse_definitely (parser))
17163 else
17164 attrs = chainon (attr, attrs);
17166 late_return = (cp_parser_late_return_type_opt
17167 (parser, declarator,
17168 memfn ? cv_quals : -1));
17171 /* Parse the virt-specifier-seq. */
17172 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17174 /* Create the function-declarator. */
17175 declarator = make_call_declarator (declarator,
17176 params,
17177 cv_quals,
17178 virt_specifiers,
17179 ref_qual,
17180 exception_specification,
17181 late_return);
17182 declarator->std_attributes = attrs;
17183 /* Any subsequent parameter lists are to do with
17184 return type, so are not those of the declared
17185 function. */
17186 parser->default_arg_ok_p = false;
17189 /* Remove the function parms from scope. */
17190 pop_bindings_and_leave_scope ();
17192 if (is_declarator)
17193 /* Repeat the main loop. */
17194 continue;
17197 /* If this is the first, we can try a parenthesized
17198 declarator. */
17199 if (first)
17201 bool saved_in_type_id_in_expr_p;
17203 parser->default_arg_ok_p = saved_default_arg_ok_p;
17204 parser->in_declarator_p = saved_in_declarator_p;
17206 /* Consume the `('. */
17207 cp_lexer_consume_token (parser->lexer);
17208 /* Parse the nested declarator. */
17209 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17210 parser->in_type_id_in_expr_p = true;
17211 declarator
17212 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17213 /*parenthesized_p=*/NULL,
17214 member_p);
17215 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17216 first = false;
17217 /* Expect a `)'. */
17218 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17219 declarator = cp_error_declarator;
17220 if (declarator == cp_error_declarator)
17221 break;
17223 goto handle_declarator;
17225 /* Otherwise, we must be done. */
17226 else
17227 break;
17229 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17230 && token->type == CPP_OPEN_SQUARE
17231 && !cp_next_tokens_can_be_attribute_p (parser))
17233 /* Parse an array-declarator. */
17234 tree bounds, attrs;
17236 if (ctor_dtor_or_conv_p)
17237 *ctor_dtor_or_conv_p = 0;
17239 first = false;
17240 parser->default_arg_ok_p = false;
17241 parser->in_declarator_p = true;
17242 /* Consume the `['. */
17243 cp_lexer_consume_token (parser->lexer);
17244 /* Peek at the next token. */
17245 token = cp_lexer_peek_token (parser->lexer);
17246 /* If the next token is `]', then there is no
17247 constant-expression. */
17248 if (token->type != CPP_CLOSE_SQUARE)
17250 bool non_constant_p;
17251 bounds
17252 = cp_parser_constant_expression (parser,
17253 /*allow_non_constant=*/true,
17254 &non_constant_p);
17255 if (!non_constant_p)
17256 /* OK */;
17257 else if (error_operand_p (bounds))
17258 /* Already gave an error. */;
17259 else if (!parser->in_function_body
17260 || current_binding_level->kind == sk_function_parms)
17262 /* Normally, the array bound must be an integral constant
17263 expression. However, as an extension, we allow VLAs
17264 in function scopes as long as they aren't part of a
17265 parameter declaration. */
17266 cp_parser_error (parser,
17267 "array bound is not an integer constant");
17268 bounds = error_mark_node;
17270 else if (processing_template_decl
17271 && !type_dependent_expression_p (bounds))
17273 /* Remember this wasn't a constant-expression. */
17274 bounds = build_nop (TREE_TYPE (bounds), bounds);
17275 TREE_SIDE_EFFECTS (bounds) = 1;
17278 else
17279 bounds = NULL_TREE;
17280 /* Look for the closing `]'. */
17281 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17283 declarator = cp_error_declarator;
17284 break;
17287 attrs = cp_parser_std_attribute_spec_seq (parser);
17288 declarator = make_array_declarator (declarator, bounds);
17289 declarator->std_attributes = attrs;
17291 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17294 tree qualifying_scope;
17295 tree unqualified_name;
17296 tree attrs;
17297 special_function_kind sfk;
17298 bool abstract_ok;
17299 bool pack_expansion_p = false;
17300 cp_token *declarator_id_start_token;
17302 /* Parse a declarator-id */
17303 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17304 if (abstract_ok)
17306 cp_parser_parse_tentatively (parser);
17308 /* If we see an ellipsis, we should be looking at a
17309 parameter pack. */
17310 if (token->type == CPP_ELLIPSIS)
17312 /* Consume the `...' */
17313 cp_lexer_consume_token (parser->lexer);
17315 pack_expansion_p = true;
17319 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17320 unqualified_name
17321 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17322 qualifying_scope = parser->scope;
17323 if (abstract_ok)
17325 bool okay = false;
17327 if (!unqualified_name && pack_expansion_p)
17329 /* Check whether an error occurred. */
17330 okay = !cp_parser_error_occurred (parser);
17332 /* We already consumed the ellipsis to mark a
17333 parameter pack, but we have no way to report it,
17334 so abort the tentative parse. We will be exiting
17335 immediately anyway. */
17336 cp_parser_abort_tentative_parse (parser);
17338 else
17339 okay = cp_parser_parse_definitely (parser);
17341 if (!okay)
17342 unqualified_name = error_mark_node;
17343 else if (unqualified_name
17344 && (qualifying_scope
17345 || (!identifier_p (unqualified_name))))
17347 cp_parser_error (parser, "expected unqualified-id");
17348 unqualified_name = error_mark_node;
17352 if (!unqualified_name)
17353 return NULL;
17354 if (unqualified_name == error_mark_node)
17356 declarator = cp_error_declarator;
17357 pack_expansion_p = false;
17358 declarator->parameter_pack_p = false;
17359 break;
17362 attrs = cp_parser_std_attribute_spec_seq (parser);
17364 if (qualifying_scope && at_namespace_scope_p ()
17365 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17367 /* In the declaration of a member of a template class
17368 outside of the class itself, the SCOPE will sometimes
17369 be a TYPENAME_TYPE. For example, given:
17371 template <typename T>
17372 int S<T>::R::i = 3;
17374 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17375 this context, we must resolve S<T>::R to an ordinary
17376 type, rather than a typename type.
17378 The reason we normally avoid resolving TYPENAME_TYPEs
17379 is that a specialization of `S' might render
17380 `S<T>::R' not a type. However, if `S' is
17381 specialized, then this `i' will not be used, so there
17382 is no harm in resolving the types here. */
17383 tree type;
17385 /* Resolve the TYPENAME_TYPE. */
17386 type = resolve_typename_type (qualifying_scope,
17387 /*only_current_p=*/false);
17388 /* If that failed, the declarator is invalid. */
17389 if (TREE_CODE (type) == TYPENAME_TYPE)
17391 if (typedef_variant_p (type))
17392 error_at (declarator_id_start_token->location,
17393 "cannot define member of dependent typedef "
17394 "%qT", type);
17395 else
17396 error_at (declarator_id_start_token->location,
17397 "%<%T::%E%> is not a type",
17398 TYPE_CONTEXT (qualifying_scope),
17399 TYPE_IDENTIFIER (qualifying_scope));
17401 qualifying_scope = type;
17404 sfk = sfk_none;
17406 if (unqualified_name)
17408 tree class_type;
17410 if (qualifying_scope
17411 && CLASS_TYPE_P (qualifying_scope))
17412 class_type = qualifying_scope;
17413 else
17414 class_type = current_class_type;
17416 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17418 tree name_type = TREE_TYPE (unqualified_name);
17419 if (class_type && same_type_p (name_type, class_type))
17421 if (qualifying_scope
17422 && CLASSTYPE_USE_TEMPLATE (name_type))
17424 error_at (declarator_id_start_token->location,
17425 "invalid use of constructor as a template");
17426 inform (declarator_id_start_token->location,
17427 "use %<%T::%D%> instead of %<%T::%D%> to "
17428 "name the constructor in a qualified name",
17429 class_type,
17430 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17431 class_type, name_type);
17432 declarator = cp_error_declarator;
17433 break;
17435 else
17436 unqualified_name = constructor_name (class_type);
17438 else
17440 /* We do not attempt to print the declarator
17441 here because we do not have enough
17442 information about its original syntactic
17443 form. */
17444 cp_parser_error (parser, "invalid declarator");
17445 declarator = cp_error_declarator;
17446 break;
17450 if (class_type)
17452 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17453 sfk = sfk_destructor;
17454 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17455 sfk = sfk_conversion;
17456 else if (/* There's no way to declare a constructor
17457 for an anonymous type, even if the type
17458 got a name for linkage purposes. */
17459 !TYPE_WAS_ANONYMOUS (class_type)
17460 && constructor_name_p (unqualified_name,
17461 class_type))
17463 unqualified_name = constructor_name (class_type);
17464 sfk = sfk_constructor;
17466 else if (is_overloaded_fn (unqualified_name)
17467 && DECL_CONSTRUCTOR_P (get_first_fn
17468 (unqualified_name)))
17469 sfk = sfk_constructor;
17471 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17472 *ctor_dtor_or_conv_p = -1;
17475 declarator = make_id_declarator (qualifying_scope,
17476 unqualified_name,
17477 sfk);
17478 declarator->std_attributes = attrs;
17479 declarator->id_loc = token->location;
17480 declarator->parameter_pack_p = pack_expansion_p;
17482 if (pack_expansion_p)
17483 maybe_warn_variadic_templates ();
17486 handle_declarator:;
17487 scope = get_scope_of_declarator (declarator);
17488 if (scope)
17490 /* Any names that appear after the declarator-id for a
17491 member are looked up in the containing scope. */
17492 if (at_function_scope_p ())
17494 /* But declarations with qualified-ids can't appear in a
17495 function. */
17496 cp_parser_error (parser, "qualified-id in declaration");
17497 declarator = cp_error_declarator;
17498 break;
17500 pushed_scope = push_scope (scope);
17502 parser->in_declarator_p = true;
17503 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17504 || (declarator && declarator->kind == cdk_id))
17505 /* Default args are only allowed on function
17506 declarations. */
17507 parser->default_arg_ok_p = saved_default_arg_ok_p;
17508 else
17509 parser->default_arg_ok_p = false;
17511 first = false;
17513 /* We're done. */
17514 else
17515 break;
17518 /* For an abstract declarator, we might wind up with nothing at this
17519 point. That's an error; the declarator is not optional. */
17520 if (!declarator)
17521 cp_parser_error (parser, "expected declarator");
17523 /* If we entered a scope, we must exit it now. */
17524 if (pushed_scope)
17525 pop_scope (pushed_scope);
17527 parser->default_arg_ok_p = saved_default_arg_ok_p;
17528 parser->in_declarator_p = saved_in_declarator_p;
17530 return declarator;
17533 /* Parse a ptr-operator.
17535 ptr-operator:
17536 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17537 * cv-qualifier-seq [opt]
17539 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17540 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17542 GNU Extension:
17544 ptr-operator:
17545 & cv-qualifier-seq [opt]
17547 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17548 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17549 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17550 filled in with the TYPE containing the member. *CV_QUALS is
17551 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17552 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17553 Note that the tree codes returned by this function have nothing
17554 to do with the types of trees that will be eventually be created
17555 to represent the pointer or reference type being parsed. They are
17556 just constants with suggestive names. */
17557 static enum tree_code
17558 cp_parser_ptr_operator (cp_parser* parser,
17559 tree* type,
17560 cp_cv_quals *cv_quals,
17561 tree *attributes)
17563 enum tree_code code = ERROR_MARK;
17564 cp_token *token;
17565 tree attrs = NULL_TREE;
17567 /* Assume that it's not a pointer-to-member. */
17568 *type = NULL_TREE;
17569 /* And that there are no cv-qualifiers. */
17570 *cv_quals = TYPE_UNQUALIFIED;
17572 /* Peek at the next token. */
17573 token = cp_lexer_peek_token (parser->lexer);
17575 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17576 if (token->type == CPP_MULT)
17577 code = INDIRECT_REF;
17578 else if (token->type == CPP_AND)
17579 code = ADDR_EXPR;
17580 else if ((cxx_dialect != cxx98) &&
17581 token->type == CPP_AND_AND) /* C++0x only */
17582 code = NON_LVALUE_EXPR;
17584 if (code != ERROR_MARK)
17586 /* Consume the `*', `&' or `&&'. */
17587 cp_lexer_consume_token (parser->lexer);
17589 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17590 `&', if we are allowing GNU extensions. (The only qualifier
17591 that can legally appear after `&' is `restrict', but that is
17592 enforced during semantic analysis. */
17593 if (code == INDIRECT_REF
17594 || cp_parser_allow_gnu_extensions_p (parser))
17595 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17597 attrs = cp_parser_std_attribute_spec_seq (parser);
17598 if (attributes != NULL)
17599 *attributes = attrs;
17601 else
17603 /* Try the pointer-to-member case. */
17604 cp_parser_parse_tentatively (parser);
17605 /* Look for the optional `::' operator. */
17606 cp_parser_global_scope_opt (parser,
17607 /*current_scope_valid_p=*/false);
17608 /* Look for the nested-name specifier. */
17609 token = cp_lexer_peek_token (parser->lexer);
17610 cp_parser_nested_name_specifier (parser,
17611 /*typename_keyword_p=*/false,
17612 /*check_dependency_p=*/true,
17613 /*type_p=*/false,
17614 /*is_declaration=*/false);
17615 /* If we found it, and the next token is a `*', then we are
17616 indeed looking at a pointer-to-member operator. */
17617 if (!cp_parser_error_occurred (parser)
17618 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17620 /* Indicate that the `*' operator was used. */
17621 code = INDIRECT_REF;
17623 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17624 error_at (token->location, "%qD is a namespace", parser->scope);
17625 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17626 error_at (token->location, "cannot form pointer to member of "
17627 "non-class %q#T", parser->scope);
17628 else
17630 /* The type of which the member is a member is given by the
17631 current SCOPE. */
17632 *type = parser->scope;
17633 /* The next name will not be qualified. */
17634 parser->scope = NULL_TREE;
17635 parser->qualifying_scope = NULL_TREE;
17636 parser->object_scope = NULL_TREE;
17637 /* Look for optional c++11 attributes. */
17638 attrs = cp_parser_std_attribute_spec_seq (parser);
17639 if (attributes != NULL)
17640 *attributes = attrs;
17641 /* Look for the optional cv-qualifier-seq. */
17642 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17645 /* If that didn't work we don't have a ptr-operator. */
17646 if (!cp_parser_parse_definitely (parser))
17647 cp_parser_error (parser, "expected ptr-operator");
17650 return code;
17653 /* Parse an (optional) cv-qualifier-seq.
17655 cv-qualifier-seq:
17656 cv-qualifier cv-qualifier-seq [opt]
17658 cv-qualifier:
17659 const
17660 volatile
17662 GNU Extension:
17664 cv-qualifier:
17665 __restrict__
17667 Returns a bitmask representing the cv-qualifiers. */
17669 static cp_cv_quals
17670 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17672 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17674 while (true)
17676 cp_token *token;
17677 cp_cv_quals cv_qualifier;
17679 /* Peek at the next token. */
17680 token = cp_lexer_peek_token (parser->lexer);
17681 /* See if it's a cv-qualifier. */
17682 switch (token->keyword)
17684 case RID_CONST:
17685 cv_qualifier = TYPE_QUAL_CONST;
17686 break;
17688 case RID_VOLATILE:
17689 cv_qualifier = TYPE_QUAL_VOLATILE;
17690 break;
17692 case RID_RESTRICT:
17693 cv_qualifier = TYPE_QUAL_RESTRICT;
17694 break;
17696 default:
17697 cv_qualifier = TYPE_UNQUALIFIED;
17698 break;
17701 if (!cv_qualifier)
17702 break;
17704 if (cv_quals & cv_qualifier)
17706 error_at (token->location, "duplicate cv-qualifier");
17707 cp_lexer_purge_token (parser->lexer);
17709 else
17711 cp_lexer_consume_token (parser->lexer);
17712 cv_quals |= cv_qualifier;
17716 return cv_quals;
17719 /* Parse an (optional) ref-qualifier
17721 ref-qualifier:
17725 Returns cp_ref_qualifier representing ref-qualifier. */
17727 static cp_ref_qualifier
17728 cp_parser_ref_qualifier_opt (cp_parser* parser)
17730 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17732 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17733 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17734 return ref_qual;
17736 while (true)
17738 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17739 cp_token *token = cp_lexer_peek_token (parser->lexer);
17741 switch (token->type)
17743 case CPP_AND:
17744 curr_ref_qual = REF_QUAL_LVALUE;
17745 break;
17747 case CPP_AND_AND:
17748 curr_ref_qual = REF_QUAL_RVALUE;
17749 break;
17751 default:
17752 curr_ref_qual = REF_QUAL_NONE;
17753 break;
17756 if (!curr_ref_qual)
17757 break;
17758 else if (ref_qual)
17760 error_at (token->location, "multiple ref-qualifiers");
17761 cp_lexer_purge_token (parser->lexer);
17763 else
17765 ref_qual = curr_ref_qual;
17766 cp_lexer_consume_token (parser->lexer);
17770 return ref_qual;
17773 /* Parse an (optional) virt-specifier-seq.
17775 virt-specifier-seq:
17776 virt-specifier virt-specifier-seq [opt]
17778 virt-specifier:
17779 override
17780 final
17782 Returns a bitmask representing the virt-specifiers. */
17784 static cp_virt_specifiers
17785 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17787 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17789 while (true)
17791 cp_token *token;
17792 cp_virt_specifiers virt_specifier;
17794 /* Peek at the next token. */
17795 token = cp_lexer_peek_token (parser->lexer);
17796 /* See if it's a virt-specifier-qualifier. */
17797 if (token->type != CPP_NAME)
17798 break;
17799 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17801 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17802 virt_specifier = VIRT_SPEC_OVERRIDE;
17804 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17806 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17807 virt_specifier = VIRT_SPEC_FINAL;
17809 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17811 virt_specifier = VIRT_SPEC_FINAL;
17813 else
17814 break;
17816 if (virt_specifiers & virt_specifier)
17818 error_at (token->location, "duplicate virt-specifier");
17819 cp_lexer_purge_token (parser->lexer);
17821 else
17823 cp_lexer_consume_token (parser->lexer);
17824 virt_specifiers |= virt_specifier;
17827 return virt_specifiers;
17830 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17831 is in scope even though it isn't real. */
17833 static void
17834 inject_this_parameter (tree ctype, cp_cv_quals quals)
17836 tree this_parm;
17838 if (current_class_ptr)
17840 /* We don't clear this between NSDMIs. Is it already what we want? */
17841 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17842 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17843 && cp_type_quals (type) == quals)
17844 return;
17847 this_parm = build_this_parm (ctype, quals);
17848 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17849 current_class_ptr = NULL_TREE;
17850 current_class_ref
17851 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17852 current_class_ptr = this_parm;
17855 /* Return true iff our current scope is a non-static data member
17856 initializer. */
17858 bool
17859 parsing_nsdmi (void)
17861 /* We recognize NSDMI context by the context-less 'this' pointer set up
17862 by the function above. */
17863 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17864 return true;
17865 return false;
17868 /* Parse a late-specified return type, if any. This is not a separate
17869 non-terminal, but part of a function declarator, which looks like
17871 -> trailing-type-specifier-seq abstract-declarator(opt)
17873 Returns the type indicated by the type-id.
17875 In addition to this this parses any queued up omp declare simd
17876 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17878 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17879 function. */
17881 static tree
17882 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17883 cp_cv_quals quals)
17885 cp_token *token;
17886 tree type = NULL_TREE;
17887 bool declare_simd_p = (parser->omp_declare_simd
17888 && declarator
17889 && declarator->kind == cdk_id);
17891 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
17892 && declarator && declarator->kind == cdk_id);
17894 /* Peek at the next token. */
17895 token = cp_lexer_peek_token (parser->lexer);
17896 /* A late-specified return type is indicated by an initial '->'. */
17897 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
17898 return NULL_TREE;
17900 tree save_ccp = current_class_ptr;
17901 tree save_ccr = current_class_ref;
17902 if (quals >= 0)
17904 /* DR 1207: 'this' is in scope in the trailing return type. */
17905 inject_this_parameter (current_class_type, quals);
17908 if (token->type == CPP_DEREF)
17910 /* Consume the ->. */
17911 cp_lexer_consume_token (parser->lexer);
17913 type = cp_parser_trailing_type_id (parser);
17916 if (cilk_simd_fn_vector_p)
17917 declarator->std_attributes
17918 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
17919 declarator->std_attributes);
17920 if (declare_simd_p)
17921 declarator->std_attributes
17922 = cp_parser_late_parsing_omp_declare_simd (parser,
17923 declarator->std_attributes);
17925 if (quals >= 0)
17927 current_class_ptr = save_ccp;
17928 current_class_ref = save_ccr;
17931 return type;
17934 /* Parse a declarator-id.
17936 declarator-id:
17937 id-expression
17938 :: [opt] nested-name-specifier [opt] type-name
17940 In the `id-expression' case, the value returned is as for
17941 cp_parser_id_expression if the id-expression was an unqualified-id.
17942 If the id-expression was a qualified-id, then a SCOPE_REF is
17943 returned. The first operand is the scope (either a NAMESPACE_DECL
17944 or TREE_TYPE), but the second is still just a representation of an
17945 unqualified-id. */
17947 static tree
17948 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17950 tree id;
17951 /* The expression must be an id-expression. Assume that qualified
17952 names are the names of types so that:
17954 template <class T>
17955 int S<T>::R::i = 3;
17957 will work; we must treat `S<T>::R' as the name of a type.
17958 Similarly, assume that qualified names are templates, where
17959 required, so that:
17961 template <class T>
17962 int S<T>::R<T>::i = 3;
17964 will work, too. */
17965 id = cp_parser_id_expression (parser,
17966 /*template_keyword_p=*/false,
17967 /*check_dependency_p=*/false,
17968 /*template_p=*/NULL,
17969 /*declarator_p=*/true,
17970 optional_p);
17971 if (id && BASELINK_P (id))
17972 id = BASELINK_FUNCTIONS (id);
17973 return id;
17976 /* Parse a type-id.
17978 type-id:
17979 type-specifier-seq abstract-declarator [opt]
17981 Returns the TYPE specified. */
17983 static tree
17984 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17985 bool is_trailing_return)
17987 cp_decl_specifier_seq type_specifier_seq;
17988 cp_declarator *abstract_declarator;
17990 /* Parse the type-specifier-seq. */
17991 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17992 is_trailing_return,
17993 &type_specifier_seq);
17994 if (type_specifier_seq.type == error_mark_node)
17995 return error_mark_node;
17997 /* There might or might not be an abstract declarator. */
17998 cp_parser_parse_tentatively (parser);
17999 /* Look for the declarator. */
18000 abstract_declarator
18001 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18002 /*parenthesized_p=*/NULL,
18003 /*member_p=*/false);
18004 /* Check to see if there really was a declarator. */
18005 if (!cp_parser_parse_definitely (parser))
18006 abstract_declarator = NULL;
18008 if (type_specifier_seq.type
18009 /* None of the valid uses of 'auto' in C++14 involve the type-id
18010 nonterminal, but it is valid in a trailing-return-type. */
18011 && !(cxx_dialect >= cxx1y && is_trailing_return)
18012 && type_uses_auto (type_specifier_seq.type))
18014 /* A type-id with type 'auto' is only ok if the abstract declarator
18015 is a function declarator with a late-specified return type. */
18016 if (abstract_declarator
18017 && abstract_declarator->kind == cdk_function
18018 && abstract_declarator->u.function.late_return_type)
18019 /* OK */;
18020 else
18022 error ("invalid use of %<auto%>");
18023 return error_mark_node;
18027 return groktypename (&type_specifier_seq, abstract_declarator,
18028 is_template_arg);
18031 static tree cp_parser_type_id (cp_parser *parser)
18033 return cp_parser_type_id_1 (parser, false, false);
18036 static tree cp_parser_template_type_arg (cp_parser *parser)
18038 tree r;
18039 const char *saved_message = parser->type_definition_forbidden_message;
18040 parser->type_definition_forbidden_message
18041 = G_("types may not be defined in template arguments");
18042 r = cp_parser_type_id_1 (parser, true, false);
18043 parser->type_definition_forbidden_message = saved_message;
18044 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18046 error ("invalid use of %<auto%> in template argument");
18047 r = error_mark_node;
18049 return r;
18052 static tree cp_parser_trailing_type_id (cp_parser *parser)
18054 return cp_parser_type_id_1 (parser, false, true);
18057 /* Parse a type-specifier-seq.
18059 type-specifier-seq:
18060 type-specifier type-specifier-seq [opt]
18062 GNU extension:
18064 type-specifier-seq:
18065 attributes type-specifier-seq [opt]
18067 If IS_DECLARATION is true, we are at the start of a "condition" or
18068 exception-declaration, so we might be followed by a declarator-id.
18070 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18071 i.e. we've just seen "->".
18073 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18075 static void
18076 cp_parser_type_specifier_seq (cp_parser* parser,
18077 bool is_declaration,
18078 bool is_trailing_return,
18079 cp_decl_specifier_seq *type_specifier_seq)
18081 bool seen_type_specifier = false;
18082 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18083 cp_token *start_token = NULL;
18085 /* Clear the TYPE_SPECIFIER_SEQ. */
18086 clear_decl_specs (type_specifier_seq);
18088 /* In the context of a trailing return type, enum E { } is an
18089 elaborated-type-specifier followed by a function-body, not an
18090 enum-specifier. */
18091 if (is_trailing_return)
18092 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18094 /* Parse the type-specifiers and attributes. */
18095 while (true)
18097 tree type_specifier;
18098 bool is_cv_qualifier;
18100 /* Check for attributes first. */
18101 if (cp_next_tokens_can_be_attribute_p (parser))
18103 type_specifier_seq->attributes =
18104 chainon (type_specifier_seq->attributes,
18105 cp_parser_attributes_opt (parser));
18106 continue;
18109 /* record the token of the beginning of the type specifier seq,
18110 for error reporting purposes*/
18111 if (!start_token)
18112 start_token = cp_lexer_peek_token (parser->lexer);
18114 /* Look for the type-specifier. */
18115 type_specifier = cp_parser_type_specifier (parser,
18116 flags,
18117 type_specifier_seq,
18118 /*is_declaration=*/false,
18119 NULL,
18120 &is_cv_qualifier);
18121 if (!type_specifier)
18123 /* If the first type-specifier could not be found, this is not a
18124 type-specifier-seq at all. */
18125 if (!seen_type_specifier)
18127 /* Set in_declarator_p to avoid skipping to the semicolon. */
18128 int in_decl = parser->in_declarator_p;
18129 parser->in_declarator_p = true;
18131 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18132 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18133 cp_parser_error (parser, "expected type-specifier");
18135 parser->in_declarator_p = in_decl;
18137 type_specifier_seq->type = error_mark_node;
18138 return;
18140 /* If subsequent type-specifiers could not be found, the
18141 type-specifier-seq is complete. */
18142 break;
18145 seen_type_specifier = true;
18146 /* The standard says that a condition can be:
18148 type-specifier-seq declarator = assignment-expression
18150 However, given:
18152 struct S {};
18153 if (int S = ...)
18155 we should treat the "S" as a declarator, not as a
18156 type-specifier. The standard doesn't say that explicitly for
18157 type-specifier-seq, but it does say that for
18158 decl-specifier-seq in an ordinary declaration. Perhaps it
18159 would be clearer just to allow a decl-specifier-seq here, and
18160 then add a semantic restriction that if any decl-specifiers
18161 that are not type-specifiers appear, the program is invalid. */
18162 if (is_declaration && !is_cv_qualifier)
18163 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18167 /* Return whether the function currently being declared has an associated
18168 template parameter list. */
18170 static bool
18171 function_being_declared_is_template_p (cp_parser* parser)
18173 if (!current_template_parms || processing_template_parmlist)
18174 return false;
18176 if (parser->implicit_template_scope)
18177 return true;
18179 if (at_class_scope_p ()
18180 && TYPE_BEING_DEFINED (current_class_type))
18181 return parser->num_template_parameter_lists != 0;
18183 return ((int) parser->num_template_parameter_lists > template_class_depth
18184 (current_class_type));
18187 /* Parse a parameter-declaration-clause.
18189 parameter-declaration-clause:
18190 parameter-declaration-list [opt] ... [opt]
18191 parameter-declaration-list , ...
18193 Returns a representation for the parameter declarations. A return
18194 value of NULL indicates a parameter-declaration-clause consisting
18195 only of an ellipsis. */
18197 static tree
18198 cp_parser_parameter_declaration_clause (cp_parser* parser)
18200 tree parameters;
18201 cp_token *token;
18202 bool ellipsis_p;
18203 bool is_error;
18205 struct cleanup {
18206 cp_parser* parser;
18207 int auto_is_implicit_function_template_parm_p;
18208 ~cleanup() {
18209 parser->auto_is_implicit_function_template_parm_p
18210 = auto_is_implicit_function_template_parm_p;
18212 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18214 (void) cleanup;
18216 if (!processing_specialization
18217 && !processing_template_parmlist
18218 && !processing_explicit_instantiation)
18219 if (!current_function_decl
18220 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18221 parser->auto_is_implicit_function_template_parm_p = true;
18223 /* Peek at the next token. */
18224 token = cp_lexer_peek_token (parser->lexer);
18225 /* Check for trivial parameter-declaration-clauses. */
18226 if (token->type == CPP_ELLIPSIS)
18228 /* Consume the `...' token. */
18229 cp_lexer_consume_token (parser->lexer);
18230 return NULL_TREE;
18232 else if (token->type == CPP_CLOSE_PAREN)
18233 /* There are no parameters. */
18235 #ifndef NO_IMPLICIT_EXTERN_C
18236 if (in_system_header_at (input_location)
18237 && current_class_type == NULL
18238 && current_lang_name == lang_name_c)
18239 return NULL_TREE;
18240 else
18241 #endif
18242 return void_list_node;
18244 /* Check for `(void)', too, which is a special case. */
18245 else if (token->keyword == RID_VOID
18246 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18247 == CPP_CLOSE_PAREN))
18249 /* Consume the `void' token. */
18250 cp_lexer_consume_token (parser->lexer);
18251 /* There are no parameters. */
18252 return void_list_node;
18255 /* Parse the parameter-declaration-list. */
18256 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18257 /* If a parse error occurred while parsing the
18258 parameter-declaration-list, then the entire
18259 parameter-declaration-clause is erroneous. */
18260 if (is_error)
18261 return NULL;
18263 /* Peek at the next token. */
18264 token = cp_lexer_peek_token (parser->lexer);
18265 /* If it's a `,', the clause should terminate with an ellipsis. */
18266 if (token->type == CPP_COMMA)
18268 /* Consume the `,'. */
18269 cp_lexer_consume_token (parser->lexer);
18270 /* Expect an ellipsis. */
18271 ellipsis_p
18272 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18274 /* It might also be `...' if the optional trailing `,' was
18275 omitted. */
18276 else if (token->type == CPP_ELLIPSIS)
18278 /* Consume the `...' token. */
18279 cp_lexer_consume_token (parser->lexer);
18280 /* And remember that we saw it. */
18281 ellipsis_p = true;
18283 else
18284 ellipsis_p = false;
18286 /* Finish the parameter list. */
18287 if (!ellipsis_p)
18288 parameters = chainon (parameters, void_list_node);
18290 return parameters;
18293 /* Parse a parameter-declaration-list.
18295 parameter-declaration-list:
18296 parameter-declaration
18297 parameter-declaration-list , parameter-declaration
18299 Returns a representation of the parameter-declaration-list, as for
18300 cp_parser_parameter_declaration_clause. However, the
18301 `void_list_node' is never appended to the list. Upon return,
18302 *IS_ERROR will be true iff an error occurred. */
18304 static tree
18305 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18307 tree parameters = NULL_TREE;
18308 tree *tail = &parameters;
18309 bool saved_in_unbraced_linkage_specification_p;
18310 int index = 0;
18312 /* Assume all will go well. */
18313 *is_error = false;
18314 /* The special considerations that apply to a function within an
18315 unbraced linkage specifications do not apply to the parameters
18316 to the function. */
18317 saved_in_unbraced_linkage_specification_p
18318 = parser->in_unbraced_linkage_specification_p;
18319 parser->in_unbraced_linkage_specification_p = false;
18321 /* Look for more parameters. */
18322 while (true)
18324 cp_parameter_declarator *parameter;
18325 tree decl = error_mark_node;
18326 bool parenthesized_p = false;
18327 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18328 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18329 (current_template_parms)) : 0);
18331 /* Parse the parameter. */
18332 parameter
18333 = cp_parser_parameter_declaration (parser,
18334 /*template_parm_p=*/false,
18335 &parenthesized_p);
18337 /* We don't know yet if the enclosing context is deprecated, so wait
18338 and warn in grokparms if appropriate. */
18339 deprecated_state = DEPRECATED_SUPPRESS;
18341 if (parameter)
18343 /* If a function parameter pack was specified and an implicit template
18344 parameter was introduced during cp_parser_parameter_declaration,
18345 change any implicit parameters introduced into packs. */
18346 if (parser->implicit_template_parms
18347 && parameter->declarator
18348 && parameter->declarator->parameter_pack_p)
18350 int latest_template_parm_idx = TREE_VEC_LENGTH
18351 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18353 if (latest_template_parm_idx != template_parm_idx)
18354 parameter->decl_specifiers.type = convert_generic_types_to_packs
18355 (parameter->decl_specifiers.type,
18356 template_parm_idx, latest_template_parm_idx);
18359 decl = grokdeclarator (parameter->declarator,
18360 &parameter->decl_specifiers,
18361 PARM,
18362 parameter->default_argument != NULL_TREE,
18363 &parameter->decl_specifiers.attributes);
18366 deprecated_state = DEPRECATED_NORMAL;
18368 /* If a parse error occurred parsing the parameter declaration,
18369 then the entire parameter-declaration-list is erroneous. */
18370 if (decl == error_mark_node)
18372 *is_error = true;
18373 parameters = error_mark_node;
18374 break;
18377 if (parameter->decl_specifiers.attributes)
18378 cplus_decl_attributes (&decl,
18379 parameter->decl_specifiers.attributes,
18381 if (DECL_NAME (decl))
18382 decl = pushdecl (decl);
18384 if (decl != error_mark_node)
18386 retrofit_lang_decl (decl);
18387 DECL_PARM_INDEX (decl) = ++index;
18388 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18391 /* Add the new parameter to the list. */
18392 *tail = build_tree_list (parameter->default_argument, decl);
18393 tail = &TREE_CHAIN (*tail);
18395 /* Peek at the next token. */
18396 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18397 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18398 /* These are for Objective-C++ */
18399 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18400 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18401 /* The parameter-declaration-list is complete. */
18402 break;
18403 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18405 cp_token *token;
18407 /* Peek at the next token. */
18408 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18409 /* If it's an ellipsis, then the list is complete. */
18410 if (token->type == CPP_ELLIPSIS)
18411 break;
18412 /* Otherwise, there must be more parameters. Consume the
18413 `,'. */
18414 cp_lexer_consume_token (parser->lexer);
18415 /* When parsing something like:
18417 int i(float f, double d)
18419 we can tell after seeing the declaration for "f" that we
18420 are not looking at an initialization of a variable "i",
18421 but rather at the declaration of a function "i".
18423 Due to the fact that the parsing of template arguments
18424 (as specified to a template-id) requires backtracking we
18425 cannot use this technique when inside a template argument
18426 list. */
18427 if (!parser->in_template_argument_list_p
18428 && !parser->in_type_id_in_expr_p
18429 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18430 /* However, a parameter-declaration of the form
18431 "float(f)" (which is a valid declaration of a
18432 parameter "f") can also be interpreted as an
18433 expression (the conversion of "f" to "float"). */
18434 && !parenthesized_p)
18435 cp_parser_commit_to_tentative_parse (parser);
18437 else
18439 cp_parser_error (parser, "expected %<,%> or %<...%>");
18440 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18441 cp_parser_skip_to_closing_parenthesis (parser,
18442 /*recovering=*/true,
18443 /*or_comma=*/false,
18444 /*consume_paren=*/false);
18445 break;
18449 parser->in_unbraced_linkage_specification_p
18450 = saved_in_unbraced_linkage_specification_p;
18452 /* Reset implicit_template_scope if we are about to leave the function
18453 parameter list that introduced it. Note that for out-of-line member
18454 definitions, there will be one or more class scopes before we get to
18455 the template parameter scope. */
18457 if (cp_binding_level *its = parser->implicit_template_scope)
18458 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18460 while (maybe_its->kind == sk_class)
18461 maybe_its = maybe_its->level_chain;
18462 if (maybe_its == its)
18464 parser->implicit_template_parms = 0;
18465 parser->implicit_template_scope = 0;
18469 return parameters;
18472 /* Parse a parameter declaration.
18474 parameter-declaration:
18475 decl-specifier-seq ... [opt] declarator
18476 decl-specifier-seq declarator = assignment-expression
18477 decl-specifier-seq ... [opt] abstract-declarator [opt]
18478 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18480 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18481 declares a template parameter. (In that case, a non-nested `>'
18482 token encountered during the parsing of the assignment-expression
18483 is not interpreted as a greater-than operator.)
18485 Returns a representation of the parameter, or NULL if an error
18486 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18487 true iff the declarator is of the form "(p)". */
18489 static cp_parameter_declarator *
18490 cp_parser_parameter_declaration (cp_parser *parser,
18491 bool template_parm_p,
18492 bool *parenthesized_p)
18494 int declares_class_or_enum;
18495 cp_decl_specifier_seq decl_specifiers;
18496 cp_declarator *declarator;
18497 tree default_argument;
18498 cp_token *token = NULL, *declarator_token_start = NULL;
18499 const char *saved_message;
18501 /* In a template parameter, `>' is not an operator.
18503 [temp.param]
18505 When parsing a default template-argument for a non-type
18506 template-parameter, the first non-nested `>' is taken as the end
18507 of the template parameter-list rather than a greater-than
18508 operator. */
18510 /* Type definitions may not appear in parameter types. */
18511 saved_message = parser->type_definition_forbidden_message;
18512 parser->type_definition_forbidden_message
18513 = G_("types may not be defined in parameter types");
18515 /* Parse the declaration-specifiers. */
18516 cp_parser_decl_specifier_seq (parser,
18517 CP_PARSER_FLAGS_NONE,
18518 &decl_specifiers,
18519 &declares_class_or_enum);
18521 /* Complain about missing 'typename' or other invalid type names. */
18522 if (!decl_specifiers.any_type_specifiers_p
18523 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18524 decl_specifiers.type = error_mark_node;
18526 /* If an error occurred, there's no reason to attempt to parse the
18527 rest of the declaration. */
18528 if (cp_parser_error_occurred (parser))
18530 parser->type_definition_forbidden_message = saved_message;
18531 return NULL;
18534 /* Peek at the next token. */
18535 token = cp_lexer_peek_token (parser->lexer);
18537 /* If the next token is a `)', `,', `=', `>', or `...', then there
18538 is no declarator. However, when variadic templates are enabled,
18539 there may be a declarator following `...'. */
18540 if (token->type == CPP_CLOSE_PAREN
18541 || token->type == CPP_COMMA
18542 || token->type == CPP_EQ
18543 || token->type == CPP_GREATER)
18545 declarator = NULL;
18546 if (parenthesized_p)
18547 *parenthesized_p = false;
18549 /* Otherwise, there should be a declarator. */
18550 else
18552 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18553 parser->default_arg_ok_p = false;
18555 /* After seeing a decl-specifier-seq, if the next token is not a
18556 "(", there is no possibility that the code is a valid
18557 expression. Therefore, if parsing tentatively, we commit at
18558 this point. */
18559 if (!parser->in_template_argument_list_p
18560 /* In an expression context, having seen:
18562 (int((char ...
18564 we cannot be sure whether we are looking at a
18565 function-type (taking a "char" as a parameter) or a cast
18566 of some object of type "char" to "int". */
18567 && !parser->in_type_id_in_expr_p
18568 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18569 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18570 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18571 cp_parser_commit_to_tentative_parse (parser);
18572 /* Parse the declarator. */
18573 declarator_token_start = token;
18574 declarator = cp_parser_declarator (parser,
18575 CP_PARSER_DECLARATOR_EITHER,
18576 /*ctor_dtor_or_conv_p=*/NULL,
18577 parenthesized_p,
18578 /*member_p=*/false);
18579 parser->default_arg_ok_p = saved_default_arg_ok_p;
18580 /* After the declarator, allow more attributes. */
18581 decl_specifiers.attributes
18582 = chainon (decl_specifiers.attributes,
18583 cp_parser_attributes_opt (parser));
18586 /* If the next token is an ellipsis, and we have not seen a
18587 declarator name, and the type of the declarator contains parameter
18588 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18589 a parameter pack expansion expression. Otherwise, leave the
18590 ellipsis for a C-style variadic function. */
18591 token = cp_lexer_peek_token (parser->lexer);
18592 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18594 tree type = decl_specifiers.type;
18596 if (type && DECL_P (type))
18597 type = TREE_TYPE (type);
18599 if (type
18600 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18601 && declarator_can_be_parameter_pack (declarator)
18602 && (!declarator || !declarator->parameter_pack_p)
18603 && uses_parameter_packs (type))
18605 /* Consume the `...'. */
18606 cp_lexer_consume_token (parser->lexer);
18607 maybe_warn_variadic_templates ();
18609 /* Build a pack expansion type */
18610 if (declarator)
18611 declarator->parameter_pack_p = true;
18612 else
18613 decl_specifiers.type = make_pack_expansion (type);
18617 /* The restriction on defining new types applies only to the type
18618 of the parameter, not to the default argument. */
18619 parser->type_definition_forbidden_message = saved_message;
18621 /* If the next token is `=', then process a default argument. */
18622 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18624 token = cp_lexer_peek_token (parser->lexer);
18625 /* If we are defining a class, then the tokens that make up the
18626 default argument must be saved and processed later. */
18627 if (!template_parm_p && at_class_scope_p ()
18628 && TYPE_BEING_DEFINED (current_class_type)
18629 && !LAMBDA_TYPE_P (current_class_type))
18630 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18631 /* Outside of a class definition, we can just parse the
18632 assignment-expression. */
18633 else
18634 default_argument
18635 = cp_parser_default_argument (parser, template_parm_p);
18637 if (!parser->default_arg_ok_p)
18639 if (flag_permissive)
18640 warning (0, "deprecated use of default argument for parameter of non-function");
18641 else
18643 error_at (token->location,
18644 "default arguments are only "
18645 "permitted for function parameters");
18646 default_argument = NULL_TREE;
18649 else if ((declarator && declarator->parameter_pack_p)
18650 || (decl_specifiers.type
18651 && PACK_EXPANSION_P (decl_specifiers.type)))
18653 /* Find the name of the parameter pack. */
18654 cp_declarator *id_declarator = declarator;
18655 while (id_declarator && id_declarator->kind != cdk_id)
18656 id_declarator = id_declarator->declarator;
18658 if (id_declarator && id_declarator->kind == cdk_id)
18659 error_at (declarator_token_start->location,
18660 template_parm_p
18661 ? G_("template parameter pack %qD "
18662 "cannot have a default argument")
18663 : G_("parameter pack %qD cannot have "
18664 "a default argument"),
18665 id_declarator->u.id.unqualified_name);
18666 else
18667 error_at (declarator_token_start->location,
18668 template_parm_p
18669 ? G_("template parameter pack cannot have "
18670 "a default argument")
18671 : G_("parameter pack cannot have a "
18672 "default argument"));
18674 default_argument = NULL_TREE;
18677 else
18678 default_argument = NULL_TREE;
18680 return make_parameter_declarator (&decl_specifiers,
18681 declarator,
18682 default_argument);
18685 /* Parse a default argument and return it.
18687 TEMPLATE_PARM_P is true if this is a default argument for a
18688 non-type template parameter. */
18689 static tree
18690 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18692 tree default_argument = NULL_TREE;
18693 bool saved_greater_than_is_operator_p;
18694 bool saved_local_variables_forbidden_p;
18695 bool non_constant_p, is_direct_init;
18697 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18698 set correctly. */
18699 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18700 parser->greater_than_is_operator_p = !template_parm_p;
18701 /* Local variable names (and the `this' keyword) may not
18702 appear in a default argument. */
18703 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18704 parser->local_variables_forbidden_p = true;
18705 /* Parse the assignment-expression. */
18706 if (template_parm_p)
18707 push_deferring_access_checks (dk_no_deferred);
18708 tree saved_class_ptr = NULL_TREE;
18709 tree saved_class_ref = NULL_TREE;
18710 /* The "this" pointer is not valid in a default argument. */
18711 if (cfun)
18713 saved_class_ptr = current_class_ptr;
18714 cp_function_chain->x_current_class_ptr = NULL_TREE;
18715 saved_class_ref = current_class_ref;
18716 cp_function_chain->x_current_class_ref = NULL_TREE;
18718 default_argument
18719 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18720 /* Restore the "this" pointer. */
18721 if (cfun)
18723 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18724 cp_function_chain->x_current_class_ref = saved_class_ref;
18726 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18727 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18728 if (template_parm_p)
18729 pop_deferring_access_checks ();
18730 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18731 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18733 return default_argument;
18736 /* Parse a function-body.
18738 function-body:
18739 compound_statement */
18741 static void
18742 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18744 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18747 /* Parse a ctor-initializer-opt followed by a function-body. Return
18748 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18749 is true we are parsing a function-try-block. */
18751 static bool
18752 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18753 bool in_function_try_block)
18755 tree body, list;
18756 bool ctor_initializer_p;
18757 const bool check_body_p =
18758 DECL_CONSTRUCTOR_P (current_function_decl)
18759 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18760 tree last = NULL;
18762 /* Begin the function body. */
18763 body = begin_function_body ();
18764 /* Parse the optional ctor-initializer. */
18765 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18767 /* If we're parsing a constexpr constructor definition, we need
18768 to check that the constructor body is indeed empty. However,
18769 before we get to cp_parser_function_body lot of junk has been
18770 generated, so we can't just check that we have an empty block.
18771 Rather we take a snapshot of the outermost block, and check whether
18772 cp_parser_function_body changed its state. */
18773 if (check_body_p)
18775 list = cur_stmt_list;
18776 if (STATEMENT_LIST_TAIL (list))
18777 last = STATEMENT_LIST_TAIL (list)->stmt;
18779 /* Parse the function-body. */
18780 cp_parser_function_body (parser, in_function_try_block);
18781 if (check_body_p)
18782 check_constexpr_ctor_body (last, list);
18783 /* Finish the function body. */
18784 finish_function_body (body);
18786 return ctor_initializer_p;
18789 /* Parse an initializer.
18791 initializer:
18792 = initializer-clause
18793 ( expression-list )
18795 Returns an expression representing the initializer. If no
18796 initializer is present, NULL_TREE is returned.
18798 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18799 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18800 set to TRUE if there is no initializer present. If there is an
18801 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18802 is set to true; otherwise it is set to false. */
18804 static tree
18805 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18806 bool* non_constant_p)
18808 cp_token *token;
18809 tree init;
18811 /* Peek at the next token. */
18812 token = cp_lexer_peek_token (parser->lexer);
18814 /* Let our caller know whether or not this initializer was
18815 parenthesized. */
18816 *is_direct_init = (token->type != CPP_EQ);
18817 /* Assume that the initializer is constant. */
18818 *non_constant_p = false;
18820 if (token->type == CPP_EQ)
18822 /* Consume the `='. */
18823 cp_lexer_consume_token (parser->lexer);
18824 /* Parse the initializer-clause. */
18825 init = cp_parser_initializer_clause (parser, non_constant_p);
18827 else if (token->type == CPP_OPEN_PAREN)
18829 vec<tree, va_gc> *vec;
18830 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18831 /*cast_p=*/false,
18832 /*allow_expansion_p=*/true,
18833 non_constant_p);
18834 if (vec == NULL)
18835 return error_mark_node;
18836 init = build_tree_list_vec (vec);
18837 release_tree_vector (vec);
18839 else if (token->type == CPP_OPEN_BRACE)
18841 cp_lexer_set_source_position (parser->lexer);
18842 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18843 init = cp_parser_braced_list (parser, non_constant_p);
18844 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18846 else
18848 /* Anything else is an error. */
18849 cp_parser_error (parser, "expected initializer");
18850 init = error_mark_node;
18853 return init;
18856 /* Parse an initializer-clause.
18858 initializer-clause:
18859 assignment-expression
18860 braced-init-list
18862 Returns an expression representing the initializer.
18864 If the `assignment-expression' production is used the value
18865 returned is simply a representation for the expression.
18867 Otherwise, calls cp_parser_braced_list. */
18869 static tree
18870 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18872 tree initializer;
18874 /* Assume the expression is constant. */
18875 *non_constant_p = false;
18877 /* If it is not a `{', then we are looking at an
18878 assignment-expression. */
18879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18881 initializer
18882 = cp_parser_constant_expression (parser,
18883 /*allow_non_constant_p=*/true,
18884 non_constant_p);
18886 else
18887 initializer = cp_parser_braced_list (parser, non_constant_p);
18889 return initializer;
18892 /* Parse a brace-enclosed initializer list.
18894 braced-init-list:
18895 { initializer-list , [opt] }
18898 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18899 the elements of the initializer-list (or NULL, if the last
18900 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18901 NULL_TREE. There is no way to detect whether or not the optional
18902 trailing `,' was provided. NON_CONSTANT_P is as for
18903 cp_parser_initializer. */
18905 static tree
18906 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18908 tree initializer;
18910 /* Consume the `{' token. */
18911 cp_lexer_consume_token (parser->lexer);
18912 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18913 initializer = make_node (CONSTRUCTOR);
18914 /* If it's not a `}', then there is a non-trivial initializer. */
18915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18917 /* Parse the initializer list. */
18918 CONSTRUCTOR_ELTS (initializer)
18919 = cp_parser_initializer_list (parser, non_constant_p);
18920 /* A trailing `,' token is allowed. */
18921 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18922 cp_lexer_consume_token (parser->lexer);
18924 else
18925 *non_constant_p = false;
18926 /* Now, there should be a trailing `}'. */
18927 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18928 TREE_TYPE (initializer) = init_list_type_node;
18929 return initializer;
18932 /* Parse an initializer-list.
18934 initializer-list:
18935 initializer-clause ... [opt]
18936 initializer-list , initializer-clause ... [opt]
18938 GNU Extension:
18940 initializer-list:
18941 designation initializer-clause ...[opt]
18942 initializer-list , designation initializer-clause ...[opt]
18944 designation:
18945 . identifier =
18946 identifier :
18947 [ constant-expression ] =
18949 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18950 for the initializer. If the INDEX of the elt is non-NULL, it is the
18951 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18952 as for cp_parser_initializer. */
18954 static vec<constructor_elt, va_gc> *
18955 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18957 vec<constructor_elt, va_gc> *v = NULL;
18959 /* Assume all of the expressions are constant. */
18960 *non_constant_p = false;
18962 /* Parse the rest of the list. */
18963 while (true)
18965 cp_token *token;
18966 tree designator;
18967 tree initializer;
18968 bool clause_non_constant_p;
18970 /* If the next token is an identifier and the following one is a
18971 colon, we are looking at the GNU designated-initializer
18972 syntax. */
18973 if (cp_parser_allow_gnu_extensions_p (parser)
18974 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18975 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18977 /* Warn the user that they are using an extension. */
18978 pedwarn (input_location, OPT_Wpedantic,
18979 "ISO C++ does not allow designated initializers");
18980 /* Consume the identifier. */
18981 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18982 /* Consume the `:'. */
18983 cp_lexer_consume_token (parser->lexer);
18985 /* Also handle the C99 syntax, '. id ='. */
18986 else if (cp_parser_allow_gnu_extensions_p (parser)
18987 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18988 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18989 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18991 /* Warn the user that they are using an extension. */
18992 pedwarn (input_location, OPT_Wpedantic,
18993 "ISO C++ does not allow C99 designated initializers");
18994 /* Consume the `.'. */
18995 cp_lexer_consume_token (parser->lexer);
18996 /* Consume the identifier. */
18997 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18998 /* Consume the `='. */
18999 cp_lexer_consume_token (parser->lexer);
19001 /* Also handle C99 array designators, '[ const ] ='. */
19002 else if (cp_parser_allow_gnu_extensions_p (parser)
19003 && !c_dialect_objc ()
19004 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19006 /* In C++11, [ could start a lambda-introducer. */
19007 bool non_const = false;
19009 cp_parser_parse_tentatively (parser);
19010 cp_lexer_consume_token (parser->lexer);
19011 designator = cp_parser_constant_expression (parser, true, &non_const);
19012 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19013 cp_parser_require (parser, CPP_EQ, RT_EQ);
19014 if (!cp_parser_parse_definitely (parser))
19015 designator = NULL_TREE;
19016 else if (non_const)
19017 require_potential_rvalue_constant_expression (designator);
19019 else
19020 designator = NULL_TREE;
19022 /* Parse the initializer. */
19023 initializer = cp_parser_initializer_clause (parser,
19024 &clause_non_constant_p);
19025 /* If any clause is non-constant, so is the entire initializer. */
19026 if (clause_non_constant_p)
19027 *non_constant_p = true;
19029 /* If we have an ellipsis, this is an initializer pack
19030 expansion. */
19031 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19033 /* Consume the `...'. */
19034 cp_lexer_consume_token (parser->lexer);
19036 /* Turn the initializer into an initializer expansion. */
19037 initializer = make_pack_expansion (initializer);
19040 /* Add it to the vector. */
19041 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19043 /* If the next token is not a comma, we have reached the end of
19044 the list. */
19045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19046 break;
19048 /* Peek at the next token. */
19049 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19050 /* If the next token is a `}', then we're still done. An
19051 initializer-clause can have a trailing `,' after the
19052 initializer-list and before the closing `}'. */
19053 if (token->type == CPP_CLOSE_BRACE)
19054 break;
19056 /* Consume the `,' token. */
19057 cp_lexer_consume_token (parser->lexer);
19060 return v;
19063 /* Classes [gram.class] */
19065 /* Parse a class-name.
19067 class-name:
19068 identifier
19069 template-id
19071 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19072 to indicate that names looked up in dependent types should be
19073 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19074 keyword has been used to indicate that the name that appears next
19075 is a template. TAG_TYPE indicates the explicit tag given before
19076 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19077 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19078 is the class being defined in a class-head.
19080 Returns the TYPE_DECL representing the class. */
19082 static tree
19083 cp_parser_class_name (cp_parser *parser,
19084 bool typename_keyword_p,
19085 bool template_keyword_p,
19086 enum tag_types tag_type,
19087 bool check_dependency_p,
19088 bool class_head_p,
19089 bool is_declaration)
19091 tree decl;
19092 tree scope;
19093 bool typename_p;
19094 cp_token *token;
19095 tree identifier = NULL_TREE;
19097 /* All class-names start with an identifier. */
19098 token = cp_lexer_peek_token (parser->lexer);
19099 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19101 cp_parser_error (parser, "expected class-name");
19102 return error_mark_node;
19105 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19106 to a template-id, so we save it here. */
19107 scope = parser->scope;
19108 if (scope == error_mark_node)
19109 return error_mark_node;
19111 /* Any name names a type if we're following the `typename' keyword
19112 in a qualified name where the enclosing scope is type-dependent. */
19113 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19114 && dependent_type_p (scope));
19115 /* Handle the common case (an identifier, but not a template-id)
19116 efficiently. */
19117 if (token->type == CPP_NAME
19118 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19120 cp_token *identifier_token;
19121 bool ambiguous_p;
19123 /* Look for the identifier. */
19124 identifier_token = cp_lexer_peek_token (parser->lexer);
19125 ambiguous_p = identifier_token->ambiguous_p;
19126 identifier = cp_parser_identifier (parser);
19127 /* If the next token isn't an identifier, we are certainly not
19128 looking at a class-name. */
19129 if (identifier == error_mark_node)
19130 decl = error_mark_node;
19131 /* If we know this is a type-name, there's no need to look it
19132 up. */
19133 else if (typename_p)
19134 decl = identifier;
19135 else
19137 tree ambiguous_decls;
19138 /* If we already know that this lookup is ambiguous, then
19139 we've already issued an error message; there's no reason
19140 to check again. */
19141 if (ambiguous_p)
19143 cp_parser_simulate_error (parser);
19144 return error_mark_node;
19146 /* If the next token is a `::', then the name must be a type
19147 name.
19149 [basic.lookup.qual]
19151 During the lookup for a name preceding the :: scope
19152 resolution operator, object, function, and enumerator
19153 names are ignored. */
19154 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19155 tag_type = typename_type;
19156 /* Look up the name. */
19157 decl = cp_parser_lookup_name (parser, identifier,
19158 tag_type,
19159 /*is_template=*/false,
19160 /*is_namespace=*/false,
19161 check_dependency_p,
19162 &ambiguous_decls,
19163 identifier_token->location);
19164 if (ambiguous_decls)
19166 if (cp_parser_parsing_tentatively (parser))
19167 cp_parser_simulate_error (parser);
19168 return error_mark_node;
19172 else
19174 /* Try a template-id. */
19175 decl = cp_parser_template_id (parser, template_keyword_p,
19176 check_dependency_p,
19177 tag_type,
19178 is_declaration);
19179 if (decl == error_mark_node)
19180 return error_mark_node;
19183 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19185 /* If this is a typename, create a TYPENAME_TYPE. */
19186 if (typename_p && decl != error_mark_node)
19188 decl = make_typename_type (scope, decl, typename_type,
19189 /*complain=*/tf_error);
19190 if (decl != error_mark_node)
19191 decl = TYPE_NAME (decl);
19194 decl = strip_using_decl (decl);
19196 /* Check to see that it is really the name of a class. */
19197 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19198 && identifier_p (TREE_OPERAND (decl, 0))
19199 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19200 /* Situations like this:
19202 template <typename T> struct A {
19203 typename T::template X<int>::I i;
19206 are problematic. Is `T::template X<int>' a class-name? The
19207 standard does not seem to be definitive, but there is no other
19208 valid interpretation of the following `::'. Therefore, those
19209 names are considered class-names. */
19211 decl = make_typename_type (scope, decl, tag_type, tf_error);
19212 if (decl != error_mark_node)
19213 decl = TYPE_NAME (decl);
19215 else if (TREE_CODE (decl) != TYPE_DECL
19216 || TREE_TYPE (decl) == error_mark_node
19217 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19218 /* In Objective-C 2.0, a classname followed by '.' starts a
19219 dot-syntax expression, and it's not a type-name. */
19220 || (c_dialect_objc ()
19221 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19222 && objc_is_class_name (decl)))
19223 decl = error_mark_node;
19225 if (decl == error_mark_node)
19226 cp_parser_error (parser, "expected class-name");
19227 else if (identifier && !parser->scope)
19228 maybe_note_name_used_in_class (identifier, decl);
19230 return decl;
19233 /* Parse a class-specifier.
19235 class-specifier:
19236 class-head { member-specification [opt] }
19238 Returns the TREE_TYPE representing the class. */
19240 static tree
19241 cp_parser_class_specifier_1 (cp_parser* parser)
19243 tree type;
19244 tree attributes = NULL_TREE;
19245 bool nested_name_specifier_p;
19246 unsigned saved_num_template_parameter_lists;
19247 bool saved_in_function_body;
19248 unsigned char in_statement;
19249 bool in_switch_statement_p;
19250 bool saved_in_unbraced_linkage_specification_p;
19251 tree old_scope = NULL_TREE;
19252 tree scope = NULL_TREE;
19253 cp_token *closing_brace;
19255 push_deferring_access_checks (dk_no_deferred);
19257 /* Parse the class-head. */
19258 type = cp_parser_class_head (parser,
19259 &nested_name_specifier_p);
19260 /* If the class-head was a semantic disaster, skip the entire body
19261 of the class. */
19262 if (!type)
19264 cp_parser_skip_to_end_of_block_or_statement (parser);
19265 pop_deferring_access_checks ();
19266 return error_mark_node;
19269 /* Look for the `{'. */
19270 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19272 pop_deferring_access_checks ();
19273 return error_mark_node;
19276 cp_ensure_no_omp_declare_simd (parser);
19278 /* Issue an error message if type-definitions are forbidden here. */
19279 cp_parser_check_type_definition (parser);
19280 /* Remember that we are defining one more class. */
19281 ++parser->num_classes_being_defined;
19282 /* Inside the class, surrounding template-parameter-lists do not
19283 apply. */
19284 saved_num_template_parameter_lists
19285 = parser->num_template_parameter_lists;
19286 parser->num_template_parameter_lists = 0;
19287 /* We are not in a function body. */
19288 saved_in_function_body = parser->in_function_body;
19289 parser->in_function_body = false;
19290 /* Or in a loop. */
19291 in_statement = parser->in_statement;
19292 parser->in_statement = 0;
19293 /* Or in a switch. */
19294 in_switch_statement_p = parser->in_switch_statement_p;
19295 parser->in_switch_statement_p = false;
19296 /* We are not immediately inside an extern "lang" block. */
19297 saved_in_unbraced_linkage_specification_p
19298 = parser->in_unbraced_linkage_specification_p;
19299 parser->in_unbraced_linkage_specification_p = false;
19301 /* Start the class. */
19302 if (nested_name_specifier_p)
19304 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19305 old_scope = push_inner_scope (scope);
19307 type = begin_class_definition (type);
19309 if (type == error_mark_node)
19310 /* If the type is erroneous, skip the entire body of the class. */
19311 cp_parser_skip_to_closing_brace (parser);
19312 else
19313 /* Parse the member-specification. */
19314 cp_parser_member_specification_opt (parser);
19316 /* Look for the trailing `}'. */
19317 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19318 /* Look for trailing attributes to apply to this class. */
19319 if (cp_parser_allow_gnu_extensions_p (parser))
19320 attributes = cp_parser_gnu_attributes_opt (parser);
19321 if (type != error_mark_node)
19322 type = finish_struct (type, attributes);
19323 if (nested_name_specifier_p)
19324 pop_inner_scope (old_scope, scope);
19326 /* We've finished a type definition. Check for the common syntax
19327 error of forgetting a semicolon after the definition. We need to
19328 be careful, as we can't just check for not-a-semicolon and be done
19329 with it; the user might have typed:
19331 class X { } c = ...;
19332 class X { } *p = ...;
19334 and so forth. Instead, enumerate all the possible tokens that
19335 might follow this production; if we don't see one of them, then
19336 complain and silently insert the semicolon. */
19338 cp_token *token = cp_lexer_peek_token (parser->lexer);
19339 bool want_semicolon = true;
19341 if (cp_next_tokens_can_be_std_attribute_p (parser))
19342 /* Don't try to parse c++11 attributes here. As per the
19343 grammar, that should be a task for
19344 cp_parser_decl_specifier_seq. */
19345 want_semicolon = false;
19347 switch (token->type)
19349 case CPP_NAME:
19350 case CPP_SEMICOLON:
19351 case CPP_MULT:
19352 case CPP_AND:
19353 case CPP_OPEN_PAREN:
19354 case CPP_CLOSE_PAREN:
19355 case CPP_COMMA:
19356 want_semicolon = false;
19357 break;
19359 /* While it's legal for type qualifiers and storage class
19360 specifiers to follow type definitions in the grammar, only
19361 compiler testsuites contain code like that. Assume that if
19362 we see such code, then what we're really seeing is a case
19363 like:
19365 class X { }
19366 const <type> var = ...;
19370 class Y { }
19371 static <type> func (...) ...
19373 i.e. the qualifier or specifier applies to the next
19374 declaration. To do so, however, we need to look ahead one
19375 more token to see if *that* token is a type specifier.
19377 This code could be improved to handle:
19379 class Z { }
19380 static const <type> var = ...; */
19381 case CPP_KEYWORD:
19382 if (keyword_is_decl_specifier (token->keyword))
19384 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19386 /* Handling user-defined types here would be nice, but very
19387 tricky. */
19388 want_semicolon
19389 = (lookahead->type == CPP_KEYWORD
19390 && keyword_begins_type_specifier (lookahead->keyword));
19392 break;
19393 default:
19394 break;
19397 /* If we don't have a type, then something is very wrong and we
19398 shouldn't try to do anything clever. Likewise for not seeing the
19399 closing brace. */
19400 if (closing_brace && TYPE_P (type) && want_semicolon)
19402 cp_token_position prev
19403 = cp_lexer_previous_token_position (parser->lexer);
19404 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19405 location_t loc = prev_token->location;
19407 if (CLASSTYPE_DECLARED_CLASS (type))
19408 error_at (loc, "expected %<;%> after class definition");
19409 else if (TREE_CODE (type) == RECORD_TYPE)
19410 error_at (loc, "expected %<;%> after struct definition");
19411 else if (TREE_CODE (type) == UNION_TYPE)
19412 error_at (loc, "expected %<;%> after union definition");
19413 else
19414 gcc_unreachable ();
19416 /* Unget one token and smash it to look as though we encountered
19417 a semicolon in the input stream. */
19418 cp_lexer_set_token_position (parser->lexer, prev);
19419 token = cp_lexer_peek_token (parser->lexer);
19420 token->type = CPP_SEMICOLON;
19421 token->keyword = RID_MAX;
19425 /* If this class is not itself within the scope of another class,
19426 then we need to parse the bodies of all of the queued function
19427 definitions. Note that the queued functions defined in a class
19428 are not always processed immediately following the
19429 class-specifier for that class. Consider:
19431 struct A {
19432 struct B { void f() { sizeof (A); } };
19435 If `f' were processed before the processing of `A' were
19436 completed, there would be no way to compute the size of `A'.
19437 Note that the nesting we are interested in here is lexical --
19438 not the semantic nesting given by TYPE_CONTEXT. In particular,
19439 for:
19441 struct A { struct B; };
19442 struct A::B { void f() { } };
19444 there is no need to delay the parsing of `A::B::f'. */
19445 if (--parser->num_classes_being_defined == 0)
19447 tree decl;
19448 tree class_type = NULL_TREE;
19449 tree pushed_scope = NULL_TREE;
19450 unsigned ix;
19451 cp_default_arg_entry *e;
19452 tree save_ccp, save_ccr;
19454 /* In a first pass, parse default arguments to the functions.
19455 Then, in a second pass, parse the bodies of the functions.
19456 This two-phased approach handles cases like:
19458 struct S {
19459 void f() { g(); }
19460 void g(int i = 3);
19464 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19466 decl = e->decl;
19467 /* If there are default arguments that have not yet been processed,
19468 take care of them now. */
19469 if (class_type != e->class_type)
19471 if (pushed_scope)
19472 pop_scope (pushed_scope);
19473 class_type = e->class_type;
19474 pushed_scope = push_scope (class_type);
19476 /* Make sure that any template parameters are in scope. */
19477 maybe_begin_member_template_processing (decl);
19478 /* Parse the default argument expressions. */
19479 cp_parser_late_parsing_default_args (parser, decl);
19480 /* Remove any template parameters from the symbol table. */
19481 maybe_end_member_template_processing ();
19483 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19484 /* Now parse any NSDMIs. */
19485 save_ccp = current_class_ptr;
19486 save_ccr = current_class_ref;
19487 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19489 if (class_type != DECL_CONTEXT (decl))
19491 if (pushed_scope)
19492 pop_scope (pushed_scope);
19493 class_type = DECL_CONTEXT (decl);
19494 pushed_scope = push_scope (class_type);
19496 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19497 cp_parser_late_parsing_nsdmi (parser, decl);
19499 vec_safe_truncate (unparsed_nsdmis, 0);
19500 current_class_ptr = save_ccp;
19501 current_class_ref = save_ccr;
19502 if (pushed_scope)
19503 pop_scope (pushed_scope);
19504 /* Now parse the body of the functions. */
19505 if (flag_openmp)
19507 /* OpenMP UDRs need to be parsed before all other functions. */
19508 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19509 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19510 cp_parser_late_parsing_for_member (parser, decl);
19511 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19512 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19513 cp_parser_late_parsing_for_member (parser, decl);
19515 else
19516 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19517 cp_parser_late_parsing_for_member (parser, decl);
19518 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19521 /* Put back any saved access checks. */
19522 pop_deferring_access_checks ();
19524 /* Restore saved state. */
19525 parser->in_switch_statement_p = in_switch_statement_p;
19526 parser->in_statement = in_statement;
19527 parser->in_function_body = saved_in_function_body;
19528 parser->num_template_parameter_lists
19529 = saved_num_template_parameter_lists;
19530 parser->in_unbraced_linkage_specification_p
19531 = saved_in_unbraced_linkage_specification_p;
19533 return type;
19536 static tree
19537 cp_parser_class_specifier (cp_parser* parser)
19539 tree ret;
19540 timevar_push (TV_PARSE_STRUCT);
19541 ret = cp_parser_class_specifier_1 (parser);
19542 timevar_pop (TV_PARSE_STRUCT);
19543 return ret;
19546 /* Parse a class-head.
19548 class-head:
19549 class-key identifier [opt] base-clause [opt]
19550 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19551 class-key nested-name-specifier [opt] template-id
19552 base-clause [opt]
19554 class-virt-specifier:
19555 final
19557 GNU Extensions:
19558 class-key attributes identifier [opt] base-clause [opt]
19559 class-key attributes nested-name-specifier identifier base-clause [opt]
19560 class-key attributes nested-name-specifier [opt] template-id
19561 base-clause [opt]
19563 Upon return BASES is initialized to the list of base classes (or
19564 NULL, if there are none) in the same form returned by
19565 cp_parser_base_clause.
19567 Returns the TYPE of the indicated class. Sets
19568 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19569 involving a nested-name-specifier was used, and FALSE otherwise.
19571 Returns error_mark_node if this is not a class-head.
19573 Returns NULL_TREE if the class-head is syntactically valid, but
19574 semantically invalid in a way that means we should skip the entire
19575 body of the class. */
19577 static tree
19578 cp_parser_class_head (cp_parser* parser,
19579 bool* nested_name_specifier_p)
19581 tree nested_name_specifier;
19582 enum tag_types class_key;
19583 tree id = NULL_TREE;
19584 tree type = NULL_TREE;
19585 tree attributes;
19586 tree bases;
19587 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19588 bool template_id_p = false;
19589 bool qualified_p = false;
19590 bool invalid_nested_name_p = false;
19591 bool invalid_explicit_specialization_p = false;
19592 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19593 tree pushed_scope = NULL_TREE;
19594 unsigned num_templates;
19595 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19596 /* Assume no nested-name-specifier will be present. */
19597 *nested_name_specifier_p = false;
19598 /* Assume no template parameter lists will be used in defining the
19599 type. */
19600 num_templates = 0;
19601 parser->colon_corrects_to_scope_p = false;
19603 /* Look for the class-key. */
19604 class_key = cp_parser_class_key (parser);
19605 if (class_key == none_type)
19606 return error_mark_node;
19608 /* Parse the attributes. */
19609 attributes = cp_parser_attributes_opt (parser);
19611 /* If the next token is `::', that is invalid -- but sometimes
19612 people do try to write:
19614 struct ::S {};
19616 Handle this gracefully by accepting the extra qualifier, and then
19617 issuing an error about it later if this really is a
19618 class-head. If it turns out just to be an elaborated type
19619 specifier, remain silent. */
19620 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19621 qualified_p = true;
19623 push_deferring_access_checks (dk_no_check);
19625 /* Determine the name of the class. Begin by looking for an
19626 optional nested-name-specifier. */
19627 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19628 nested_name_specifier
19629 = cp_parser_nested_name_specifier_opt (parser,
19630 /*typename_keyword_p=*/false,
19631 /*check_dependency_p=*/false,
19632 /*type_p=*/true,
19633 /*is_declaration=*/false);
19634 /* If there was a nested-name-specifier, then there *must* be an
19635 identifier. */
19636 if (nested_name_specifier)
19638 type_start_token = cp_lexer_peek_token (parser->lexer);
19639 /* Although the grammar says `identifier', it really means
19640 `class-name' or `template-name'. You are only allowed to
19641 define a class that has already been declared with this
19642 syntax.
19644 The proposed resolution for Core Issue 180 says that wherever
19645 you see `class T::X' you should treat `X' as a type-name.
19647 It is OK to define an inaccessible class; for example:
19649 class A { class B; };
19650 class A::B {};
19652 We do not know if we will see a class-name, or a
19653 template-name. We look for a class-name first, in case the
19654 class-name is a template-id; if we looked for the
19655 template-name first we would stop after the template-name. */
19656 cp_parser_parse_tentatively (parser);
19657 type = cp_parser_class_name (parser,
19658 /*typename_keyword_p=*/false,
19659 /*template_keyword_p=*/false,
19660 class_type,
19661 /*check_dependency_p=*/false,
19662 /*class_head_p=*/true,
19663 /*is_declaration=*/false);
19664 /* If that didn't work, ignore the nested-name-specifier. */
19665 if (!cp_parser_parse_definitely (parser))
19667 invalid_nested_name_p = true;
19668 type_start_token = cp_lexer_peek_token (parser->lexer);
19669 id = cp_parser_identifier (parser);
19670 if (id == error_mark_node)
19671 id = NULL_TREE;
19673 /* If we could not find a corresponding TYPE, treat this
19674 declaration like an unqualified declaration. */
19675 if (type == error_mark_node)
19676 nested_name_specifier = NULL_TREE;
19677 /* Otherwise, count the number of templates used in TYPE and its
19678 containing scopes. */
19679 else
19681 tree scope;
19683 for (scope = TREE_TYPE (type);
19684 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19685 scope = get_containing_scope (scope))
19686 if (TYPE_P (scope)
19687 && CLASS_TYPE_P (scope)
19688 && CLASSTYPE_TEMPLATE_INFO (scope)
19689 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19690 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19691 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19692 ++num_templates;
19695 /* Otherwise, the identifier is optional. */
19696 else
19698 /* We don't know whether what comes next is a template-id,
19699 an identifier, or nothing at all. */
19700 cp_parser_parse_tentatively (parser);
19701 /* Check for a template-id. */
19702 type_start_token = cp_lexer_peek_token (parser->lexer);
19703 id = cp_parser_template_id (parser,
19704 /*template_keyword_p=*/false,
19705 /*check_dependency_p=*/true,
19706 class_key,
19707 /*is_declaration=*/true);
19708 /* If that didn't work, it could still be an identifier. */
19709 if (!cp_parser_parse_definitely (parser))
19711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19713 type_start_token = cp_lexer_peek_token (parser->lexer);
19714 id = cp_parser_identifier (parser);
19716 else
19717 id = NULL_TREE;
19719 else
19721 template_id_p = true;
19722 ++num_templates;
19726 pop_deferring_access_checks ();
19728 if (id)
19730 cp_parser_check_for_invalid_template_id (parser, id,
19731 class_key,
19732 type_start_token->location);
19734 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19736 /* If it's not a `:' or a `{' then we can't really be looking at a
19737 class-head, since a class-head only appears as part of a
19738 class-specifier. We have to detect this situation before calling
19739 xref_tag, since that has irreversible side-effects. */
19740 if (!cp_parser_next_token_starts_class_definition_p (parser))
19742 cp_parser_error (parser, "expected %<{%> or %<:%>");
19743 type = error_mark_node;
19744 goto out;
19747 /* At this point, we're going ahead with the class-specifier, even
19748 if some other problem occurs. */
19749 cp_parser_commit_to_tentative_parse (parser);
19750 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19752 cp_parser_error (parser,
19753 "cannot specify %<override%> for a class");
19754 type = error_mark_node;
19755 goto out;
19757 /* Issue the error about the overly-qualified name now. */
19758 if (qualified_p)
19760 cp_parser_error (parser,
19761 "global qualification of class name is invalid");
19762 type = error_mark_node;
19763 goto out;
19765 else if (invalid_nested_name_p)
19767 cp_parser_error (parser,
19768 "qualified name does not name a class");
19769 type = error_mark_node;
19770 goto out;
19772 else if (nested_name_specifier)
19774 tree scope;
19776 /* Reject typedef-names in class heads. */
19777 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19779 error_at (type_start_token->location,
19780 "invalid class name in declaration of %qD",
19781 type);
19782 type = NULL_TREE;
19783 goto done;
19786 /* Figure out in what scope the declaration is being placed. */
19787 scope = current_scope ();
19788 /* If that scope does not contain the scope in which the
19789 class was originally declared, the program is invalid. */
19790 if (scope && !is_ancestor (scope, nested_name_specifier))
19792 if (at_namespace_scope_p ())
19793 error_at (type_start_token->location,
19794 "declaration of %qD in namespace %qD which does not "
19795 "enclose %qD",
19796 type, scope, nested_name_specifier);
19797 else
19798 error_at (type_start_token->location,
19799 "declaration of %qD in %qD which does not enclose %qD",
19800 type, scope, nested_name_specifier);
19801 type = NULL_TREE;
19802 goto done;
19804 /* [dcl.meaning]
19806 A declarator-id shall not be qualified except for the
19807 definition of a ... nested class outside of its class
19808 ... [or] the definition or explicit instantiation of a
19809 class member of a namespace outside of its namespace. */
19810 if (scope == nested_name_specifier)
19812 permerror (nested_name_specifier_token_start->location,
19813 "extra qualification not allowed");
19814 nested_name_specifier = NULL_TREE;
19815 num_templates = 0;
19818 /* An explicit-specialization must be preceded by "template <>". If
19819 it is not, try to recover gracefully. */
19820 if (at_namespace_scope_p ()
19821 && parser->num_template_parameter_lists == 0
19822 && template_id_p)
19824 error_at (type_start_token->location,
19825 "an explicit specialization must be preceded by %<template <>%>");
19826 invalid_explicit_specialization_p = true;
19827 /* Take the same action that would have been taken by
19828 cp_parser_explicit_specialization. */
19829 ++parser->num_template_parameter_lists;
19830 begin_specialization ();
19832 /* There must be no "return" statements between this point and the
19833 end of this function; set "type "to the correct return value and
19834 use "goto done;" to return. */
19835 /* Make sure that the right number of template parameters were
19836 present. */
19837 if (!cp_parser_check_template_parameters (parser, num_templates,
19838 type_start_token->location,
19839 /*declarator=*/NULL))
19841 /* If something went wrong, there is no point in even trying to
19842 process the class-definition. */
19843 type = NULL_TREE;
19844 goto done;
19847 /* Look up the type. */
19848 if (template_id_p)
19850 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19851 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19852 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19854 error_at (type_start_token->location,
19855 "function template %qD redeclared as a class template", id);
19856 type = error_mark_node;
19858 else
19860 type = TREE_TYPE (id);
19861 type = maybe_process_partial_specialization (type);
19863 if (nested_name_specifier)
19864 pushed_scope = push_scope (nested_name_specifier);
19866 else if (nested_name_specifier)
19868 tree class_type;
19870 /* Given:
19872 template <typename T> struct S { struct T };
19873 template <typename T> struct S<T>::T { };
19875 we will get a TYPENAME_TYPE when processing the definition of
19876 `S::T'. We need to resolve it to the actual type before we
19877 try to define it. */
19878 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19880 class_type = resolve_typename_type (TREE_TYPE (type),
19881 /*only_current_p=*/false);
19882 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19883 type = TYPE_NAME (class_type);
19884 else
19886 cp_parser_error (parser, "could not resolve typename type");
19887 type = error_mark_node;
19891 if (maybe_process_partial_specialization (TREE_TYPE (type))
19892 == error_mark_node)
19894 type = NULL_TREE;
19895 goto done;
19898 class_type = current_class_type;
19899 /* Enter the scope indicated by the nested-name-specifier. */
19900 pushed_scope = push_scope (nested_name_specifier);
19901 /* Get the canonical version of this type. */
19902 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19903 /* Call push_template_decl if it seems like we should be defining a
19904 template either from the template headers or the type we're
19905 defining, so that we diagnose both extra and missing headers. */
19906 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19907 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
19908 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19909 (TREE_TYPE (type)))))
19910 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19912 type = push_template_decl (type);
19913 if (type == error_mark_node)
19915 type = NULL_TREE;
19916 goto done;
19920 type = TREE_TYPE (type);
19921 *nested_name_specifier_p = true;
19923 else /* The name is not a nested name. */
19925 /* If the class was unnamed, create a dummy name. */
19926 if (!id)
19927 id = make_anon_name ();
19928 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19929 parser->num_template_parameter_lists);
19932 /* Indicate whether this class was declared as a `class' or as a
19933 `struct'. */
19934 if (TREE_CODE (type) == RECORD_TYPE)
19935 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19936 cp_parser_check_class_key (class_key, type);
19938 /* If this type was already complete, and we see another definition,
19939 that's an error. */
19940 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19942 error_at (type_start_token->location, "redefinition of %q#T",
19943 type);
19944 error_at (type_start_token->location, "previous definition of %q+#T",
19945 type);
19946 type = NULL_TREE;
19947 goto done;
19949 else if (type == error_mark_node)
19950 type = NULL_TREE;
19952 if (type)
19954 /* Apply attributes now, before any use of the class as a template
19955 argument in its base list. */
19956 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19957 fixup_attribute_variants (type);
19960 /* We will have entered the scope containing the class; the names of
19961 base classes should be looked up in that context. For example:
19963 struct A { struct B {}; struct C; };
19964 struct A::C : B {};
19966 is valid. */
19968 /* Get the list of base-classes, if there is one. */
19969 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19971 /* PR59482: enter the class scope so that base-specifiers are looked
19972 up correctly. */
19973 if (type)
19974 pushclass (type);
19975 bases = cp_parser_base_clause (parser);
19976 /* PR59482: get out of the previously pushed class scope so that the
19977 subsequent pops pop the right thing. */
19978 if (type)
19979 popclass ();
19981 else
19982 bases = NULL_TREE;
19984 /* If we're really defining a class, process the base classes.
19985 If they're invalid, fail. */
19986 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19987 && !xref_basetypes (type, bases))
19988 type = NULL_TREE;
19990 done:
19991 /* Leave the scope given by the nested-name-specifier. We will
19992 enter the class scope itself while processing the members. */
19993 if (pushed_scope)
19994 pop_scope (pushed_scope);
19996 if (invalid_explicit_specialization_p)
19998 end_specialization ();
19999 --parser->num_template_parameter_lists;
20002 if (type)
20003 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20004 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20005 CLASSTYPE_FINAL (type) = 1;
20006 out:
20007 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20008 return type;
20011 /* Parse a class-key.
20013 class-key:
20014 class
20015 struct
20016 union
20018 Returns the kind of class-key specified, or none_type to indicate
20019 error. */
20021 static enum tag_types
20022 cp_parser_class_key (cp_parser* parser)
20024 cp_token *token;
20025 enum tag_types tag_type;
20027 /* Look for the class-key. */
20028 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20029 if (!token)
20030 return none_type;
20032 /* Check to see if the TOKEN is a class-key. */
20033 tag_type = cp_parser_token_is_class_key (token);
20034 if (!tag_type)
20035 cp_parser_error (parser, "expected class-key");
20036 return tag_type;
20039 /* Parse an (optional) member-specification.
20041 member-specification:
20042 member-declaration member-specification [opt]
20043 access-specifier : member-specification [opt] */
20045 static void
20046 cp_parser_member_specification_opt (cp_parser* parser)
20048 while (true)
20050 cp_token *token;
20051 enum rid keyword;
20053 /* Peek at the next token. */
20054 token = cp_lexer_peek_token (parser->lexer);
20055 /* If it's a `}', or EOF then we've seen all the members. */
20056 if (token->type == CPP_CLOSE_BRACE
20057 || token->type == CPP_EOF
20058 || token->type == CPP_PRAGMA_EOL)
20059 break;
20061 /* See if this token is a keyword. */
20062 keyword = token->keyword;
20063 switch (keyword)
20065 case RID_PUBLIC:
20066 case RID_PROTECTED:
20067 case RID_PRIVATE:
20068 /* Consume the access-specifier. */
20069 cp_lexer_consume_token (parser->lexer);
20070 /* Remember which access-specifier is active. */
20071 current_access_specifier = token->u.value;
20072 /* Look for the `:'. */
20073 cp_parser_require (parser, CPP_COLON, RT_COLON);
20074 break;
20076 default:
20077 /* Accept #pragmas at class scope. */
20078 if (token->type == CPP_PRAGMA)
20080 cp_parser_pragma (parser, pragma_member);
20081 break;
20084 /* Otherwise, the next construction must be a
20085 member-declaration. */
20086 cp_parser_member_declaration (parser);
20091 /* Parse a member-declaration.
20093 member-declaration:
20094 decl-specifier-seq [opt] member-declarator-list [opt] ;
20095 function-definition ; [opt]
20096 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20097 using-declaration
20098 template-declaration
20099 alias-declaration
20101 member-declarator-list:
20102 member-declarator
20103 member-declarator-list , member-declarator
20105 member-declarator:
20106 declarator pure-specifier [opt]
20107 declarator constant-initializer [opt]
20108 identifier [opt] : constant-expression
20110 GNU Extensions:
20112 member-declaration:
20113 __extension__ member-declaration
20115 member-declarator:
20116 declarator attributes [opt] pure-specifier [opt]
20117 declarator attributes [opt] constant-initializer [opt]
20118 identifier [opt] attributes [opt] : constant-expression
20120 C++0x Extensions:
20122 member-declaration:
20123 static_assert-declaration */
20125 static void
20126 cp_parser_member_declaration (cp_parser* parser)
20128 cp_decl_specifier_seq decl_specifiers;
20129 tree prefix_attributes;
20130 tree decl;
20131 int declares_class_or_enum;
20132 bool friend_p;
20133 cp_token *token = NULL;
20134 cp_token *decl_spec_token_start = NULL;
20135 cp_token *initializer_token_start = NULL;
20136 int saved_pedantic;
20137 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20139 /* Check for the `__extension__' keyword. */
20140 if (cp_parser_extension_opt (parser, &saved_pedantic))
20142 /* Recurse. */
20143 cp_parser_member_declaration (parser);
20144 /* Restore the old value of the PEDANTIC flag. */
20145 pedantic = saved_pedantic;
20147 return;
20150 /* Check for a template-declaration. */
20151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20153 /* An explicit specialization here is an error condition, and we
20154 expect the specialization handler to detect and report this. */
20155 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20156 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20157 cp_parser_explicit_specialization (parser);
20158 else
20159 cp_parser_template_declaration (parser, /*member_p=*/true);
20161 return;
20164 /* Check for a using-declaration. */
20165 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20167 if (cxx_dialect < cxx11)
20169 /* Parse the using-declaration. */
20170 cp_parser_using_declaration (parser,
20171 /*access_declaration_p=*/false);
20172 return;
20174 else
20176 tree decl;
20177 bool alias_decl_expected;
20178 cp_parser_parse_tentatively (parser);
20179 decl = cp_parser_alias_declaration (parser);
20180 /* Note that if we actually see the '=' token after the
20181 identifier, cp_parser_alias_declaration commits the
20182 tentative parse. In that case, we really expects an
20183 alias-declaration. Otherwise, we expect a using
20184 declaration. */
20185 alias_decl_expected =
20186 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20187 cp_parser_parse_definitely (parser);
20189 if (alias_decl_expected)
20190 finish_member_declaration (decl);
20191 else
20192 cp_parser_using_declaration (parser,
20193 /*access_declaration_p=*/false);
20194 return;
20198 /* Check for @defs. */
20199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20201 tree ivar, member;
20202 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20203 ivar = ivar_chains;
20204 while (ivar)
20206 member = ivar;
20207 ivar = TREE_CHAIN (member);
20208 TREE_CHAIN (member) = NULL_TREE;
20209 finish_member_declaration (member);
20211 return;
20214 /* If the next token is `static_assert' we have a static assertion. */
20215 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20217 cp_parser_static_assert (parser, /*member_p=*/true);
20218 return;
20221 parser->colon_corrects_to_scope_p = false;
20223 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20224 goto out;
20226 /* Parse the decl-specifier-seq. */
20227 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20228 cp_parser_decl_specifier_seq (parser,
20229 CP_PARSER_FLAGS_OPTIONAL,
20230 &decl_specifiers,
20231 &declares_class_or_enum);
20232 /* Check for an invalid type-name. */
20233 if (!decl_specifiers.any_type_specifiers_p
20234 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20235 goto out;
20236 /* If there is no declarator, then the decl-specifier-seq should
20237 specify a type. */
20238 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20240 /* If there was no decl-specifier-seq, and the next token is a
20241 `;', then we have something like:
20243 struct S { ; };
20245 [class.mem]
20247 Each member-declaration shall declare at least one member
20248 name of the class. */
20249 if (!decl_specifiers.any_specifiers_p)
20251 cp_token *token = cp_lexer_peek_token (parser->lexer);
20252 if (!in_system_header_at (token->location))
20253 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20255 else
20257 tree type;
20259 /* See if this declaration is a friend. */
20260 friend_p = cp_parser_friend_p (&decl_specifiers);
20261 /* If there were decl-specifiers, check to see if there was
20262 a class-declaration. */
20263 type = check_tag_decl (&decl_specifiers,
20264 /*explicit_type_instantiation_p=*/false);
20265 /* Nested classes have already been added to the class, but
20266 a `friend' needs to be explicitly registered. */
20267 if (friend_p)
20269 /* If the `friend' keyword was present, the friend must
20270 be introduced with a class-key. */
20271 if (!declares_class_or_enum && cxx_dialect < cxx11)
20272 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20273 "in C++03 a class-key must be used "
20274 "when declaring a friend");
20275 /* In this case:
20277 template <typename T> struct A {
20278 friend struct A<T>::B;
20281 A<T>::B will be represented by a TYPENAME_TYPE, and
20282 therefore not recognized by check_tag_decl. */
20283 if (!type)
20285 type = decl_specifiers.type;
20286 if (type && TREE_CODE (type) == TYPE_DECL)
20287 type = TREE_TYPE (type);
20289 if (!type || !TYPE_P (type))
20290 error_at (decl_spec_token_start->location,
20291 "friend declaration does not name a class or "
20292 "function");
20293 else
20294 make_friend_class (current_class_type, type,
20295 /*complain=*/true);
20297 /* If there is no TYPE, an error message will already have
20298 been issued. */
20299 else if (!type || type == error_mark_node)
20301 /* An anonymous aggregate has to be handled specially; such
20302 a declaration really declares a data member (with a
20303 particular type), as opposed to a nested class. */
20304 else if (ANON_AGGR_TYPE_P (type))
20306 /* C++11 9.5/6. */
20307 if (decl_specifiers.storage_class != sc_none)
20308 error_at (decl_spec_token_start->location,
20309 "a storage class on an anonymous aggregate "
20310 "in class scope is not allowed");
20312 /* Remove constructors and such from TYPE, now that we
20313 know it is an anonymous aggregate. */
20314 fixup_anonymous_aggr (type);
20315 /* And make the corresponding data member. */
20316 decl = build_decl (decl_spec_token_start->location,
20317 FIELD_DECL, NULL_TREE, type);
20318 /* Add it to the class. */
20319 finish_member_declaration (decl);
20321 else
20322 cp_parser_check_access_in_redeclaration
20323 (TYPE_NAME (type),
20324 decl_spec_token_start->location);
20327 else
20329 bool assume_semicolon = false;
20331 /* Clear attributes from the decl_specifiers but keep them
20332 around as prefix attributes that apply them to the entity
20333 being declared. */
20334 prefix_attributes = decl_specifiers.attributes;
20335 decl_specifiers.attributes = NULL_TREE;
20337 /* See if these declarations will be friends. */
20338 friend_p = cp_parser_friend_p (&decl_specifiers);
20340 /* Keep going until we hit the `;' at the end of the
20341 declaration. */
20342 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20344 tree attributes = NULL_TREE;
20345 tree first_attribute;
20347 /* Peek at the next token. */
20348 token = cp_lexer_peek_token (parser->lexer);
20350 /* Check for a bitfield declaration. */
20351 if (token->type == CPP_COLON
20352 || (token->type == CPP_NAME
20353 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20354 == CPP_COLON))
20356 tree identifier;
20357 tree width;
20359 /* Get the name of the bitfield. Note that we cannot just
20360 check TOKEN here because it may have been invalidated by
20361 the call to cp_lexer_peek_nth_token above. */
20362 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20363 identifier = cp_parser_identifier (parser);
20364 else
20365 identifier = NULL_TREE;
20367 /* Consume the `:' token. */
20368 cp_lexer_consume_token (parser->lexer);
20369 /* Get the width of the bitfield. */
20370 width
20371 = cp_parser_constant_expression (parser,
20372 /*allow_non_constant=*/false,
20373 NULL);
20375 /* Look for attributes that apply to the bitfield. */
20376 attributes = cp_parser_attributes_opt (parser);
20377 /* Remember which attributes are prefix attributes and
20378 which are not. */
20379 first_attribute = attributes;
20380 /* Combine the attributes. */
20381 attributes = chainon (prefix_attributes, attributes);
20383 /* Create the bitfield declaration. */
20384 decl = grokbitfield (identifier
20385 ? make_id_declarator (NULL_TREE,
20386 identifier,
20387 sfk_none)
20388 : NULL,
20389 &decl_specifiers,
20390 width,
20391 attributes);
20393 else
20395 cp_declarator *declarator;
20396 tree initializer;
20397 tree asm_specification;
20398 int ctor_dtor_or_conv_p;
20400 /* Parse the declarator. */
20401 declarator
20402 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20403 &ctor_dtor_or_conv_p,
20404 /*parenthesized_p=*/NULL,
20405 /*member_p=*/true);
20407 /* If something went wrong parsing the declarator, make sure
20408 that we at least consume some tokens. */
20409 if (declarator == cp_error_declarator)
20411 /* Skip to the end of the statement. */
20412 cp_parser_skip_to_end_of_statement (parser);
20413 /* If the next token is not a semicolon, that is
20414 probably because we just skipped over the body of
20415 a function. So, we consume a semicolon if
20416 present, but do not issue an error message if it
20417 is not present. */
20418 if (cp_lexer_next_token_is (parser->lexer,
20419 CPP_SEMICOLON))
20420 cp_lexer_consume_token (parser->lexer);
20421 goto out;
20424 if (declares_class_or_enum & 2)
20425 cp_parser_check_for_definition_in_return_type
20426 (declarator, decl_specifiers.type,
20427 decl_specifiers.locations[ds_type_spec]);
20429 /* Look for an asm-specification. */
20430 asm_specification = cp_parser_asm_specification_opt (parser);
20431 /* Look for attributes that apply to the declaration. */
20432 attributes = cp_parser_attributes_opt (parser);
20433 /* Remember which attributes are prefix attributes and
20434 which are not. */
20435 first_attribute = attributes;
20436 /* Combine the attributes. */
20437 attributes = chainon (prefix_attributes, attributes);
20439 /* If it's an `=', then we have a constant-initializer or a
20440 pure-specifier. It is not correct to parse the
20441 initializer before registering the member declaration
20442 since the member declaration should be in scope while
20443 its initializer is processed. However, the rest of the
20444 front end does not yet provide an interface that allows
20445 us to handle this correctly. */
20446 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20448 /* In [class.mem]:
20450 A pure-specifier shall be used only in the declaration of
20451 a virtual function.
20453 A member-declarator can contain a constant-initializer
20454 only if it declares a static member of integral or
20455 enumeration type.
20457 Therefore, if the DECLARATOR is for a function, we look
20458 for a pure-specifier; otherwise, we look for a
20459 constant-initializer. When we call `grokfield', it will
20460 perform more stringent semantics checks. */
20461 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20462 if (function_declarator_p (declarator)
20463 || (decl_specifiers.type
20464 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20465 && declarator->kind == cdk_id
20466 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20467 == FUNCTION_TYPE)))
20468 initializer = cp_parser_pure_specifier (parser);
20469 else if (decl_specifiers.storage_class != sc_static)
20470 initializer = cp_parser_save_nsdmi (parser);
20471 else if (cxx_dialect >= cxx11)
20473 bool nonconst;
20474 /* Don't require a constant rvalue in C++11, since we
20475 might want a reference constant. We'll enforce
20476 constancy later. */
20477 cp_lexer_consume_token (parser->lexer);
20478 /* Parse the initializer. */
20479 initializer = cp_parser_initializer_clause (parser,
20480 &nonconst);
20482 else
20483 /* Parse the initializer. */
20484 initializer = cp_parser_constant_initializer (parser);
20486 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20487 && !function_declarator_p (declarator))
20489 bool x;
20490 if (decl_specifiers.storage_class != sc_static)
20491 initializer = cp_parser_save_nsdmi (parser);
20492 else
20493 initializer = cp_parser_initializer (parser, &x, &x);
20495 /* Otherwise, there is no initializer. */
20496 else
20497 initializer = NULL_TREE;
20499 /* See if we are probably looking at a function
20500 definition. We are certainly not looking at a
20501 member-declarator. Calling `grokfield' has
20502 side-effects, so we must not do it unless we are sure
20503 that we are looking at a member-declarator. */
20504 if (cp_parser_token_starts_function_definition_p
20505 (cp_lexer_peek_token (parser->lexer)))
20507 /* The grammar does not allow a pure-specifier to be
20508 used when a member function is defined. (It is
20509 possible that this fact is an oversight in the
20510 standard, since a pure function may be defined
20511 outside of the class-specifier. */
20512 if (initializer && initializer_token_start)
20513 error_at (initializer_token_start->location,
20514 "pure-specifier on function-definition");
20515 decl = cp_parser_save_member_function_body (parser,
20516 &decl_specifiers,
20517 declarator,
20518 attributes);
20519 if (parser->fully_implicit_function_template_p)
20520 decl = finish_fully_implicit_template (parser, decl);
20521 /* If the member was not a friend, declare it here. */
20522 if (!friend_p)
20523 finish_member_declaration (decl);
20524 /* Peek at the next token. */
20525 token = cp_lexer_peek_token (parser->lexer);
20526 /* If the next token is a semicolon, consume it. */
20527 if (token->type == CPP_SEMICOLON)
20528 cp_lexer_consume_token (parser->lexer);
20529 goto out;
20531 else
20532 if (declarator->kind == cdk_function)
20533 declarator->id_loc = token->location;
20534 /* Create the declaration. */
20535 decl = grokfield (declarator, &decl_specifiers,
20536 initializer, /*init_const_expr_p=*/true,
20537 asm_specification, attributes);
20538 if (parser->fully_implicit_function_template_p)
20540 if (friend_p)
20541 finish_fully_implicit_template (parser, 0);
20542 else
20543 decl = finish_fully_implicit_template (parser, decl);
20547 cp_finalize_omp_declare_simd (parser, decl);
20549 /* Reset PREFIX_ATTRIBUTES. */
20550 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20551 attributes = TREE_CHAIN (attributes);
20552 if (attributes)
20553 TREE_CHAIN (attributes) = NULL_TREE;
20555 /* If there is any qualification still in effect, clear it
20556 now; we will be starting fresh with the next declarator. */
20557 parser->scope = NULL_TREE;
20558 parser->qualifying_scope = NULL_TREE;
20559 parser->object_scope = NULL_TREE;
20560 /* If it's a `,', then there are more declarators. */
20561 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20563 cp_lexer_consume_token (parser->lexer);
20564 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20566 cp_token *token = cp_lexer_previous_token (parser->lexer);
20567 error_at (token->location,
20568 "stray %<,%> at end of member declaration");
20571 /* If the next token isn't a `;', then we have a parse error. */
20572 else if (cp_lexer_next_token_is_not (parser->lexer,
20573 CPP_SEMICOLON))
20575 /* The next token might be a ways away from where the
20576 actual semicolon is missing. Find the previous token
20577 and use that for our error position. */
20578 cp_token *token = cp_lexer_previous_token (parser->lexer);
20579 error_at (token->location,
20580 "expected %<;%> at end of member declaration");
20582 /* Assume that the user meant to provide a semicolon. If
20583 we were to cp_parser_skip_to_end_of_statement, we might
20584 skip to a semicolon inside a member function definition
20585 and issue nonsensical error messages. */
20586 assume_semicolon = true;
20589 if (decl)
20591 /* Add DECL to the list of members. */
20592 if (!friend_p)
20593 finish_member_declaration (decl);
20595 if (TREE_CODE (decl) == FUNCTION_DECL)
20596 cp_parser_save_default_args (parser, decl);
20597 else if (TREE_CODE (decl) == FIELD_DECL
20598 && !DECL_C_BIT_FIELD (decl)
20599 && DECL_INITIAL (decl))
20600 /* Add DECL to the queue of NSDMI to be parsed later. */
20601 vec_safe_push (unparsed_nsdmis, decl);
20604 if (assume_semicolon)
20605 goto out;
20609 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20610 out:
20611 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20614 /* Parse a pure-specifier.
20616 pure-specifier:
20619 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20620 Otherwise, ERROR_MARK_NODE is returned. */
20622 static tree
20623 cp_parser_pure_specifier (cp_parser* parser)
20625 cp_token *token;
20627 /* Look for the `=' token. */
20628 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20629 return error_mark_node;
20630 /* Look for the `0' token. */
20631 token = cp_lexer_peek_token (parser->lexer);
20633 if (token->type == CPP_EOF
20634 || token->type == CPP_PRAGMA_EOL)
20635 return error_mark_node;
20637 cp_lexer_consume_token (parser->lexer);
20639 /* Accept = default or = delete in c++0x mode. */
20640 if (token->keyword == RID_DEFAULT
20641 || token->keyword == RID_DELETE)
20643 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20644 return token->u.value;
20647 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20648 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20650 cp_parser_error (parser,
20651 "invalid pure specifier (only %<= 0%> is allowed)");
20652 cp_parser_skip_to_end_of_statement (parser);
20653 return error_mark_node;
20655 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20657 error_at (token->location, "templates may not be %<virtual%>");
20658 return error_mark_node;
20661 return integer_zero_node;
20664 /* Parse a constant-initializer.
20666 constant-initializer:
20667 = constant-expression
20669 Returns a representation of the constant-expression. */
20671 static tree
20672 cp_parser_constant_initializer (cp_parser* parser)
20674 /* Look for the `=' token. */
20675 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20676 return error_mark_node;
20678 /* It is invalid to write:
20680 struct S { static const int i = { 7 }; };
20683 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20685 cp_parser_error (parser,
20686 "a brace-enclosed initializer is not allowed here");
20687 /* Consume the opening brace. */
20688 cp_lexer_consume_token (parser->lexer);
20689 /* Skip the initializer. */
20690 cp_parser_skip_to_closing_brace (parser);
20691 /* Look for the trailing `}'. */
20692 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20694 return error_mark_node;
20697 return cp_parser_constant_expression (parser,
20698 /*allow_non_constant=*/false,
20699 NULL);
20702 /* Derived classes [gram.class.derived] */
20704 /* Parse a base-clause.
20706 base-clause:
20707 : base-specifier-list
20709 base-specifier-list:
20710 base-specifier ... [opt]
20711 base-specifier-list , base-specifier ... [opt]
20713 Returns a TREE_LIST representing the base-classes, in the order in
20714 which they were declared. The representation of each node is as
20715 described by cp_parser_base_specifier.
20717 In the case that no bases are specified, this function will return
20718 NULL_TREE, not ERROR_MARK_NODE. */
20720 static tree
20721 cp_parser_base_clause (cp_parser* parser)
20723 tree bases = NULL_TREE;
20725 /* Look for the `:' that begins the list. */
20726 cp_parser_require (parser, CPP_COLON, RT_COLON);
20728 /* Scan the base-specifier-list. */
20729 while (true)
20731 cp_token *token;
20732 tree base;
20733 bool pack_expansion_p = false;
20735 /* Look for the base-specifier. */
20736 base = cp_parser_base_specifier (parser);
20737 /* Look for the (optional) ellipsis. */
20738 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20740 /* Consume the `...'. */
20741 cp_lexer_consume_token (parser->lexer);
20743 pack_expansion_p = true;
20746 /* Add BASE to the front of the list. */
20747 if (base && base != error_mark_node)
20749 if (pack_expansion_p)
20750 /* Make this a pack expansion type. */
20751 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20753 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20755 TREE_CHAIN (base) = bases;
20756 bases = base;
20759 /* Peek at the next token. */
20760 token = cp_lexer_peek_token (parser->lexer);
20761 /* If it's not a comma, then the list is complete. */
20762 if (token->type != CPP_COMMA)
20763 break;
20764 /* Consume the `,'. */
20765 cp_lexer_consume_token (parser->lexer);
20768 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20769 base class had a qualified name. However, the next name that
20770 appears is certainly not qualified. */
20771 parser->scope = NULL_TREE;
20772 parser->qualifying_scope = NULL_TREE;
20773 parser->object_scope = NULL_TREE;
20775 return nreverse (bases);
20778 /* Parse a base-specifier.
20780 base-specifier:
20781 :: [opt] nested-name-specifier [opt] class-name
20782 virtual access-specifier [opt] :: [opt] nested-name-specifier
20783 [opt] class-name
20784 access-specifier virtual [opt] :: [opt] nested-name-specifier
20785 [opt] class-name
20787 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20788 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20789 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20790 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20792 static tree
20793 cp_parser_base_specifier (cp_parser* parser)
20795 cp_token *token;
20796 bool done = false;
20797 bool virtual_p = false;
20798 bool duplicate_virtual_error_issued_p = false;
20799 bool duplicate_access_error_issued_p = false;
20800 bool class_scope_p, template_p;
20801 tree access = access_default_node;
20802 tree type;
20804 /* Process the optional `virtual' and `access-specifier'. */
20805 while (!done)
20807 /* Peek at the next token. */
20808 token = cp_lexer_peek_token (parser->lexer);
20809 /* Process `virtual'. */
20810 switch (token->keyword)
20812 case RID_VIRTUAL:
20813 /* If `virtual' appears more than once, issue an error. */
20814 if (virtual_p && !duplicate_virtual_error_issued_p)
20816 cp_parser_error (parser,
20817 "%<virtual%> specified more than once in base-specified");
20818 duplicate_virtual_error_issued_p = true;
20821 virtual_p = true;
20823 /* Consume the `virtual' token. */
20824 cp_lexer_consume_token (parser->lexer);
20826 break;
20828 case RID_PUBLIC:
20829 case RID_PROTECTED:
20830 case RID_PRIVATE:
20831 /* If more than one access specifier appears, issue an
20832 error. */
20833 if (access != access_default_node
20834 && !duplicate_access_error_issued_p)
20836 cp_parser_error (parser,
20837 "more than one access specifier in base-specified");
20838 duplicate_access_error_issued_p = true;
20841 access = ridpointers[(int) token->keyword];
20843 /* Consume the access-specifier. */
20844 cp_lexer_consume_token (parser->lexer);
20846 break;
20848 default:
20849 done = true;
20850 break;
20853 /* It is not uncommon to see programs mechanically, erroneously, use
20854 the 'typename' keyword to denote (dependent) qualified types
20855 as base classes. */
20856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20858 token = cp_lexer_peek_token (parser->lexer);
20859 if (!processing_template_decl)
20860 error_at (token->location,
20861 "keyword %<typename%> not allowed outside of templates");
20862 else
20863 error_at (token->location,
20864 "keyword %<typename%> not allowed in this context "
20865 "(the base class is implicitly a type)");
20866 cp_lexer_consume_token (parser->lexer);
20869 /* Look for the optional `::' operator. */
20870 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20871 /* Look for the nested-name-specifier. The simplest way to
20872 implement:
20874 [temp.res]
20876 The keyword `typename' is not permitted in a base-specifier or
20877 mem-initializer; in these contexts a qualified name that
20878 depends on a template-parameter is implicitly assumed to be a
20879 type name.
20881 is to pretend that we have seen the `typename' keyword at this
20882 point. */
20883 cp_parser_nested_name_specifier_opt (parser,
20884 /*typename_keyword_p=*/true,
20885 /*check_dependency_p=*/true,
20886 typename_type,
20887 /*is_declaration=*/true);
20888 /* If the base class is given by a qualified name, assume that names
20889 we see are type names or templates, as appropriate. */
20890 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20891 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20893 if (!parser->scope
20894 && cp_lexer_next_token_is_decltype (parser->lexer))
20895 /* DR 950 allows decltype as a base-specifier. */
20896 type = cp_parser_decltype (parser);
20897 else
20899 /* Otherwise, look for the class-name. */
20900 type = cp_parser_class_name (parser,
20901 class_scope_p,
20902 template_p,
20903 typename_type,
20904 /*check_dependency_p=*/true,
20905 /*class_head_p=*/false,
20906 /*is_declaration=*/true);
20907 type = TREE_TYPE (type);
20910 if (type == error_mark_node)
20911 return error_mark_node;
20913 return finish_base_specifier (type, access, virtual_p);
20916 /* Exception handling [gram.exception] */
20918 /* Parse an (optional) noexcept-specification.
20920 noexcept-specification:
20921 noexcept ( constant-expression ) [opt]
20923 If no noexcept-specification is present, returns NULL_TREE.
20924 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20925 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20926 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20927 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20928 in which case a boolean condition is returned instead. */
20930 static tree
20931 cp_parser_noexcept_specification_opt (cp_parser* parser,
20932 bool require_constexpr,
20933 bool* consumed_expr,
20934 bool return_cond)
20936 cp_token *token;
20937 const char *saved_message;
20939 /* Peek at the next token. */
20940 token = cp_lexer_peek_token (parser->lexer);
20942 /* Is it a noexcept-specification? */
20943 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20945 tree expr;
20946 cp_lexer_consume_token (parser->lexer);
20948 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20950 cp_lexer_consume_token (parser->lexer);
20952 if (require_constexpr)
20954 /* Types may not be defined in an exception-specification. */
20955 saved_message = parser->type_definition_forbidden_message;
20956 parser->type_definition_forbidden_message
20957 = G_("types may not be defined in an exception-specification");
20959 expr = cp_parser_constant_expression (parser, false, NULL);
20961 /* Restore the saved message. */
20962 parser->type_definition_forbidden_message = saved_message;
20964 else
20966 expr = cp_parser_expression (parser, false, NULL);
20967 *consumed_expr = true;
20970 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20972 else
20974 expr = boolean_true_node;
20975 if (!require_constexpr)
20976 *consumed_expr = false;
20979 /* We cannot build a noexcept-spec right away because this will check
20980 that expr is a constexpr. */
20981 if (!return_cond)
20982 return build_noexcept_spec (expr, tf_warning_or_error);
20983 else
20984 return expr;
20986 else
20987 return NULL_TREE;
20990 /* Parse an (optional) exception-specification.
20992 exception-specification:
20993 throw ( type-id-list [opt] )
20995 Returns a TREE_LIST representing the exception-specification. The
20996 TREE_VALUE of each node is a type. */
20998 static tree
20999 cp_parser_exception_specification_opt (cp_parser* parser)
21001 cp_token *token;
21002 tree type_id_list;
21003 const char *saved_message;
21005 /* Peek at the next token. */
21006 token = cp_lexer_peek_token (parser->lexer);
21008 /* Is it a noexcept-specification? */
21009 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21010 false);
21011 if (type_id_list != NULL_TREE)
21012 return type_id_list;
21014 /* If it's not `throw', then there's no exception-specification. */
21015 if (!cp_parser_is_keyword (token, RID_THROW))
21016 return NULL_TREE;
21018 #if 0
21019 /* Enable this once a lot of code has transitioned to noexcept? */
21020 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21021 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21022 "deprecated in C++0x; use %<noexcept%> instead");
21023 #endif
21025 /* Consume the `throw'. */
21026 cp_lexer_consume_token (parser->lexer);
21028 /* Look for the `('. */
21029 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21031 /* Peek at the next token. */
21032 token = cp_lexer_peek_token (parser->lexer);
21033 /* If it's not a `)', then there is a type-id-list. */
21034 if (token->type != CPP_CLOSE_PAREN)
21036 /* Types may not be defined in an exception-specification. */
21037 saved_message = parser->type_definition_forbidden_message;
21038 parser->type_definition_forbidden_message
21039 = G_("types may not be defined in an exception-specification");
21040 /* Parse the type-id-list. */
21041 type_id_list = cp_parser_type_id_list (parser);
21042 /* Restore the saved message. */
21043 parser->type_definition_forbidden_message = saved_message;
21045 else
21046 type_id_list = empty_except_spec;
21048 /* Look for the `)'. */
21049 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21051 return type_id_list;
21054 /* Parse an (optional) type-id-list.
21056 type-id-list:
21057 type-id ... [opt]
21058 type-id-list , type-id ... [opt]
21060 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21061 in the order that the types were presented. */
21063 static tree
21064 cp_parser_type_id_list (cp_parser* parser)
21066 tree types = NULL_TREE;
21068 while (true)
21070 cp_token *token;
21071 tree type;
21073 /* Get the next type-id. */
21074 type = cp_parser_type_id (parser);
21075 /* Parse the optional ellipsis. */
21076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21078 /* Consume the `...'. */
21079 cp_lexer_consume_token (parser->lexer);
21081 /* Turn the type into a pack expansion expression. */
21082 type = make_pack_expansion (type);
21084 /* Add it to the list. */
21085 types = add_exception_specifier (types, type, /*complain=*/1);
21086 /* Peek at the next token. */
21087 token = cp_lexer_peek_token (parser->lexer);
21088 /* If it is not a `,', we are done. */
21089 if (token->type != CPP_COMMA)
21090 break;
21091 /* Consume the `,'. */
21092 cp_lexer_consume_token (parser->lexer);
21095 return nreverse (types);
21098 /* Parse a try-block.
21100 try-block:
21101 try compound-statement handler-seq */
21103 static tree
21104 cp_parser_try_block (cp_parser* parser)
21106 tree try_block;
21108 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21109 try_block = begin_try_block ();
21110 cp_parser_compound_statement (parser, NULL, true, false);
21111 finish_try_block (try_block);
21112 cp_parser_handler_seq (parser);
21113 finish_handler_sequence (try_block);
21115 return try_block;
21118 /* Parse a function-try-block.
21120 function-try-block:
21121 try ctor-initializer [opt] function-body handler-seq */
21123 static bool
21124 cp_parser_function_try_block (cp_parser* parser)
21126 tree compound_stmt;
21127 tree try_block;
21128 bool ctor_initializer_p;
21130 /* Look for the `try' keyword. */
21131 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21132 return false;
21133 /* Let the rest of the front end know where we are. */
21134 try_block = begin_function_try_block (&compound_stmt);
21135 /* Parse the function-body. */
21136 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21137 (parser, /*in_function_try_block=*/true);
21138 /* We're done with the `try' part. */
21139 finish_function_try_block (try_block);
21140 /* Parse the handlers. */
21141 cp_parser_handler_seq (parser);
21142 /* We're done with the handlers. */
21143 finish_function_handler_sequence (try_block, compound_stmt);
21145 return ctor_initializer_p;
21148 /* Parse a handler-seq.
21150 handler-seq:
21151 handler handler-seq [opt] */
21153 static void
21154 cp_parser_handler_seq (cp_parser* parser)
21156 while (true)
21158 cp_token *token;
21160 /* Parse the handler. */
21161 cp_parser_handler (parser);
21162 /* Peek at the next token. */
21163 token = cp_lexer_peek_token (parser->lexer);
21164 /* If it's not `catch' then there are no more handlers. */
21165 if (!cp_parser_is_keyword (token, RID_CATCH))
21166 break;
21170 /* Parse a handler.
21172 handler:
21173 catch ( exception-declaration ) compound-statement */
21175 static void
21176 cp_parser_handler (cp_parser* parser)
21178 tree handler;
21179 tree declaration;
21181 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21182 handler = begin_handler ();
21183 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21184 declaration = cp_parser_exception_declaration (parser);
21185 finish_handler_parms (declaration, handler);
21186 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21187 cp_parser_compound_statement (parser, NULL, false, false);
21188 finish_handler (handler);
21191 /* Parse an exception-declaration.
21193 exception-declaration:
21194 type-specifier-seq declarator
21195 type-specifier-seq abstract-declarator
21196 type-specifier-seq
21199 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21200 ellipsis variant is used. */
21202 static tree
21203 cp_parser_exception_declaration (cp_parser* parser)
21205 cp_decl_specifier_seq type_specifiers;
21206 cp_declarator *declarator;
21207 const char *saved_message;
21209 /* If it's an ellipsis, it's easy to handle. */
21210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21212 /* Consume the `...' token. */
21213 cp_lexer_consume_token (parser->lexer);
21214 return NULL_TREE;
21217 /* Types may not be defined in exception-declarations. */
21218 saved_message = parser->type_definition_forbidden_message;
21219 parser->type_definition_forbidden_message
21220 = G_("types may not be defined in exception-declarations");
21222 /* Parse the type-specifier-seq. */
21223 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21224 /*is_trailing_return=*/false,
21225 &type_specifiers);
21226 /* If it's a `)', then there is no declarator. */
21227 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21228 declarator = NULL;
21229 else
21230 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21231 /*ctor_dtor_or_conv_p=*/NULL,
21232 /*parenthesized_p=*/NULL,
21233 /*member_p=*/false);
21235 /* Restore the saved message. */
21236 parser->type_definition_forbidden_message = saved_message;
21238 if (!type_specifiers.any_specifiers_p)
21239 return error_mark_node;
21241 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21244 /* Parse a throw-expression.
21246 throw-expression:
21247 throw assignment-expression [opt]
21249 Returns a THROW_EXPR representing the throw-expression. */
21251 static tree
21252 cp_parser_throw_expression (cp_parser* parser)
21254 tree expression;
21255 cp_token* token;
21257 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21258 token = cp_lexer_peek_token (parser->lexer);
21259 /* Figure out whether or not there is an assignment-expression
21260 following the "throw" keyword. */
21261 if (token->type == CPP_COMMA
21262 || token->type == CPP_SEMICOLON
21263 || token->type == CPP_CLOSE_PAREN
21264 || token->type == CPP_CLOSE_SQUARE
21265 || token->type == CPP_CLOSE_BRACE
21266 || token->type == CPP_COLON)
21267 expression = NULL_TREE;
21268 else
21269 expression = cp_parser_assignment_expression (parser,
21270 /*cast_p=*/false, NULL);
21272 return build_throw (expression);
21275 /* GNU Extensions */
21277 /* Parse an (optional) asm-specification.
21279 asm-specification:
21280 asm ( string-literal )
21282 If the asm-specification is present, returns a STRING_CST
21283 corresponding to the string-literal. Otherwise, returns
21284 NULL_TREE. */
21286 static tree
21287 cp_parser_asm_specification_opt (cp_parser* parser)
21289 cp_token *token;
21290 tree asm_specification;
21292 /* Peek at the next token. */
21293 token = cp_lexer_peek_token (parser->lexer);
21294 /* If the next token isn't the `asm' keyword, then there's no
21295 asm-specification. */
21296 if (!cp_parser_is_keyword (token, RID_ASM))
21297 return NULL_TREE;
21299 /* Consume the `asm' token. */
21300 cp_lexer_consume_token (parser->lexer);
21301 /* Look for the `('. */
21302 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21304 /* Look for the string-literal. */
21305 asm_specification = cp_parser_string_literal (parser, false, false);
21307 /* Look for the `)'. */
21308 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21310 return asm_specification;
21313 /* Parse an asm-operand-list.
21315 asm-operand-list:
21316 asm-operand
21317 asm-operand-list , asm-operand
21319 asm-operand:
21320 string-literal ( expression )
21321 [ string-literal ] string-literal ( expression )
21323 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21324 each node is the expression. The TREE_PURPOSE is itself a
21325 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21326 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21327 is a STRING_CST for the string literal before the parenthesis. Returns
21328 ERROR_MARK_NODE if any of the operands are invalid. */
21330 static tree
21331 cp_parser_asm_operand_list (cp_parser* parser)
21333 tree asm_operands = NULL_TREE;
21334 bool invalid_operands = false;
21336 while (true)
21338 tree string_literal;
21339 tree expression;
21340 tree name;
21342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21344 /* Consume the `[' token. */
21345 cp_lexer_consume_token (parser->lexer);
21346 /* Read the operand name. */
21347 name = cp_parser_identifier (parser);
21348 if (name != error_mark_node)
21349 name = build_string (IDENTIFIER_LENGTH (name),
21350 IDENTIFIER_POINTER (name));
21351 /* Look for the closing `]'. */
21352 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21354 else
21355 name = NULL_TREE;
21356 /* Look for the string-literal. */
21357 string_literal = cp_parser_string_literal (parser, false, false);
21359 /* Look for the `('. */
21360 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21361 /* Parse the expression. */
21362 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21363 /* Look for the `)'. */
21364 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21366 if (name == error_mark_node
21367 || string_literal == error_mark_node
21368 || expression == error_mark_node)
21369 invalid_operands = true;
21371 /* Add this operand to the list. */
21372 asm_operands = tree_cons (build_tree_list (name, string_literal),
21373 expression,
21374 asm_operands);
21375 /* If the next token is not a `,', there are no more
21376 operands. */
21377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21378 break;
21379 /* Consume the `,'. */
21380 cp_lexer_consume_token (parser->lexer);
21383 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21386 /* Parse an asm-clobber-list.
21388 asm-clobber-list:
21389 string-literal
21390 asm-clobber-list , string-literal
21392 Returns a TREE_LIST, indicating the clobbers in the order that they
21393 appeared. The TREE_VALUE of each node is a STRING_CST. */
21395 static tree
21396 cp_parser_asm_clobber_list (cp_parser* parser)
21398 tree clobbers = NULL_TREE;
21400 while (true)
21402 tree string_literal;
21404 /* Look for the string literal. */
21405 string_literal = cp_parser_string_literal (parser, false, false);
21406 /* Add it to the list. */
21407 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21408 /* If the next token is not a `,', then the list is
21409 complete. */
21410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21411 break;
21412 /* Consume the `,' token. */
21413 cp_lexer_consume_token (parser->lexer);
21416 return clobbers;
21419 /* Parse an asm-label-list.
21421 asm-label-list:
21422 identifier
21423 asm-label-list , identifier
21425 Returns a TREE_LIST, indicating the labels in the order that they
21426 appeared. The TREE_VALUE of each node is a label. */
21428 static tree
21429 cp_parser_asm_label_list (cp_parser* parser)
21431 tree labels = NULL_TREE;
21433 while (true)
21435 tree identifier, label, name;
21437 /* Look for the identifier. */
21438 identifier = cp_parser_identifier (parser);
21439 if (!error_operand_p (identifier))
21441 label = lookup_label (identifier);
21442 if (TREE_CODE (label) == LABEL_DECL)
21444 TREE_USED (label) = 1;
21445 check_goto (label);
21446 name = build_string (IDENTIFIER_LENGTH (identifier),
21447 IDENTIFIER_POINTER (identifier));
21448 labels = tree_cons (name, label, labels);
21451 /* If the next token is not a `,', then the list is
21452 complete. */
21453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21454 break;
21455 /* Consume the `,' token. */
21456 cp_lexer_consume_token (parser->lexer);
21459 return nreverse (labels);
21462 /* Return TRUE iff the next tokens in the stream are possibly the
21463 beginning of a GNU extension attribute. */
21465 static bool
21466 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21468 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21471 /* Return TRUE iff the next tokens in the stream are possibly the
21472 beginning of a standard C++-11 attribute specifier. */
21474 static bool
21475 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21477 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21480 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21481 beginning of a standard C++-11 attribute specifier. */
21483 static bool
21484 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21486 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21488 return (cxx_dialect >= cxx11
21489 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21490 || (token->type == CPP_OPEN_SQUARE
21491 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21492 && token->type == CPP_OPEN_SQUARE)));
21495 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21496 beginning of a GNU extension attribute. */
21498 static bool
21499 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21501 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21503 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21506 /* Return true iff the next tokens can be the beginning of either a
21507 GNU attribute list, or a standard C++11 attribute sequence. */
21509 static bool
21510 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21512 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21513 || cp_next_tokens_can_be_std_attribute_p (parser));
21516 /* Return true iff the next Nth tokens can be the beginning of either
21517 a GNU attribute list, or a standard C++11 attribute sequence. */
21519 static bool
21520 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21522 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21523 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21526 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21527 of GNU attributes, or return NULL. */
21529 static tree
21530 cp_parser_attributes_opt (cp_parser *parser)
21532 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21533 return cp_parser_gnu_attributes_opt (parser);
21534 return cp_parser_std_attribute_spec_seq (parser);
21537 #define CILK_SIMD_FN_CLAUSE_MASK \
21538 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21539 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21540 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21541 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21542 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21544 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21545 vector [(<clauses>)] */
21547 static void
21548 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21550 bool first_p = parser->cilk_simd_fn_info == NULL;
21551 cp_token *token = v_token;
21552 if (first_p)
21554 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21555 parser->cilk_simd_fn_info->error_seen = false;
21556 parser->cilk_simd_fn_info->fndecl_seen = false;
21557 parser->cilk_simd_fn_info->tokens = vNULL;
21559 int paren_scope = 0;
21560 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21562 cp_lexer_consume_token (parser->lexer);
21563 v_token = cp_lexer_peek_token (parser->lexer);
21564 paren_scope++;
21566 while (paren_scope > 0)
21568 token = cp_lexer_peek_token (parser->lexer);
21569 if (token->type == CPP_OPEN_PAREN)
21570 paren_scope++;
21571 else if (token->type == CPP_CLOSE_PAREN)
21572 paren_scope--;
21573 /* Do not push the last ')' */
21574 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21575 cp_lexer_consume_token (parser->lexer);
21578 token->type = CPP_PRAGMA_EOL;
21579 parser->lexer->next_token = token;
21580 cp_lexer_consume_token (parser->lexer);
21582 struct cp_token_cache *cp
21583 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21584 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21587 /* Parse an (optional) series of attributes.
21589 attributes:
21590 attributes attribute
21592 attribute:
21593 __attribute__ (( attribute-list [opt] ))
21595 The return value is as for cp_parser_gnu_attribute_list. */
21597 static tree
21598 cp_parser_gnu_attributes_opt (cp_parser* parser)
21600 tree attributes = NULL_TREE;
21602 while (true)
21604 cp_token *token;
21605 tree attribute_list;
21606 bool ok = true;
21608 /* Peek at the next token. */
21609 token = cp_lexer_peek_token (parser->lexer);
21610 /* If it's not `__attribute__', then we're done. */
21611 if (token->keyword != RID_ATTRIBUTE)
21612 break;
21614 /* Consume the `__attribute__' keyword. */
21615 cp_lexer_consume_token (parser->lexer);
21616 /* Look for the two `(' tokens. */
21617 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21618 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21620 /* Peek at the next token. */
21621 token = cp_lexer_peek_token (parser->lexer);
21622 if (token->type != CPP_CLOSE_PAREN)
21623 /* Parse the attribute-list. */
21624 attribute_list = cp_parser_gnu_attribute_list (parser);
21625 else
21626 /* If the next token is a `)', then there is no attribute
21627 list. */
21628 attribute_list = NULL;
21630 /* Look for the two `)' tokens. */
21631 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21632 ok = false;
21633 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21634 ok = false;
21635 if (!ok)
21636 cp_parser_skip_to_end_of_statement (parser);
21638 /* Add these new attributes to the list. */
21639 attributes = chainon (attributes, attribute_list);
21642 return attributes;
21645 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21646 "__vector" or "__vector__." */
21648 static inline bool
21649 is_cilkplus_vector_p (tree name)
21651 if (flag_cilkplus && is_attribute_p ("vector", name))
21652 return true;
21653 return false;
21656 /* Parse a GNU attribute-list.
21658 attribute-list:
21659 attribute
21660 attribute-list , attribute
21662 attribute:
21663 identifier
21664 identifier ( identifier )
21665 identifier ( identifier , expression-list )
21666 identifier ( expression-list )
21668 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21669 to an attribute. The TREE_PURPOSE of each node is the identifier
21670 indicating which attribute is in use. The TREE_VALUE represents
21671 the arguments, if any. */
21673 static tree
21674 cp_parser_gnu_attribute_list (cp_parser* parser)
21676 tree attribute_list = NULL_TREE;
21677 bool save_translate_strings_p = parser->translate_strings_p;
21679 parser->translate_strings_p = false;
21680 while (true)
21682 cp_token *token;
21683 tree identifier;
21684 tree attribute;
21686 /* Look for the identifier. We also allow keywords here; for
21687 example `__attribute__ ((const))' is legal. */
21688 token = cp_lexer_peek_token (parser->lexer);
21689 if (token->type == CPP_NAME
21690 || token->type == CPP_KEYWORD)
21692 tree arguments = NULL_TREE;
21694 /* Consume the token, but save it since we need it for the
21695 SIMD enabled function parsing. */
21696 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21698 /* Save away the identifier that indicates which attribute
21699 this is. */
21700 identifier = (token->type == CPP_KEYWORD)
21701 /* For keywords, use the canonical spelling, not the
21702 parsed identifier. */
21703 ? ridpointers[(int) token->keyword]
21704 : id_token->u.value;
21706 attribute = build_tree_list (identifier, NULL_TREE);
21708 /* Peek at the next token. */
21709 token = cp_lexer_peek_token (parser->lexer);
21710 /* If it's an `(', then parse the attribute arguments. */
21711 if (token->type == CPP_OPEN_PAREN)
21713 vec<tree, va_gc> *vec;
21714 int attr_flag = (attribute_takes_identifier_p (identifier)
21715 ? id_attr : normal_attr);
21716 if (is_cilkplus_vector_p (identifier))
21718 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21719 continue;
21721 else
21722 vec = cp_parser_parenthesized_expression_list
21723 (parser, attr_flag, /*cast_p=*/false,
21724 /*allow_expansion_p=*/false,
21725 /*non_constant_p=*/NULL);
21726 if (vec == NULL)
21727 arguments = error_mark_node;
21728 else
21730 arguments = build_tree_list_vec (vec);
21731 release_tree_vector (vec);
21733 /* Save the arguments away. */
21734 TREE_VALUE (attribute) = arguments;
21736 else if (is_cilkplus_vector_p (identifier))
21738 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21739 continue;
21742 if (arguments != error_mark_node)
21744 /* Add this attribute to the list. */
21745 TREE_CHAIN (attribute) = attribute_list;
21746 attribute_list = attribute;
21749 token = cp_lexer_peek_token (parser->lexer);
21751 /* Now, look for more attributes. If the next token isn't a
21752 `,', we're done. */
21753 if (token->type != CPP_COMMA)
21754 break;
21756 /* Consume the comma and keep going. */
21757 cp_lexer_consume_token (parser->lexer);
21759 parser->translate_strings_p = save_translate_strings_p;
21761 /* We built up the list in reverse order. */
21762 return nreverse (attribute_list);
21765 /* Parse a standard C++11 attribute.
21767 The returned representation is a TREE_LIST which TREE_PURPOSE is
21768 the scoped name of the attribute, and the TREE_VALUE is its
21769 arguments list.
21771 Note that the scoped name of the attribute is itself a TREE_LIST
21772 which TREE_PURPOSE is the namespace of the attribute, and
21773 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21774 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21775 and which TREE_PURPOSE is directly the attribute name.
21777 Clients of the attribute code should use get_attribute_namespace
21778 and get_attribute_name to get the actual namespace and name of
21779 attributes, regardless of their being GNU or C++11 attributes.
21781 attribute:
21782 attribute-token attribute-argument-clause [opt]
21784 attribute-token:
21785 identifier
21786 attribute-scoped-token
21788 attribute-scoped-token:
21789 attribute-namespace :: identifier
21791 attribute-namespace:
21792 identifier
21794 attribute-argument-clause:
21795 ( balanced-token-seq )
21797 balanced-token-seq:
21798 balanced-token [opt]
21799 balanced-token-seq balanced-token
21801 balanced-token:
21802 ( balanced-token-seq )
21803 [ balanced-token-seq ]
21804 { balanced-token-seq }. */
21806 static tree
21807 cp_parser_std_attribute (cp_parser *parser)
21809 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21810 cp_token *token;
21812 /* First, parse name of the the attribute, a.k.a
21813 attribute-token. */
21815 token = cp_lexer_peek_token (parser->lexer);
21816 if (token->type == CPP_NAME)
21817 attr_id = token->u.value;
21818 else if (token->type == CPP_KEYWORD)
21819 attr_id = ridpointers[(int) token->keyword];
21820 else if (token->flags & NAMED_OP)
21821 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21823 if (attr_id == NULL_TREE)
21824 return NULL_TREE;
21826 cp_lexer_consume_token (parser->lexer);
21828 token = cp_lexer_peek_token (parser->lexer);
21829 if (token->type == CPP_SCOPE)
21831 /* We are seeing a scoped attribute token. */
21833 cp_lexer_consume_token (parser->lexer);
21834 attr_ns = attr_id;
21836 token = cp_lexer_consume_token (parser->lexer);
21837 if (token->type == CPP_NAME)
21838 attr_id = token->u.value;
21839 else if (token->type == CPP_KEYWORD)
21840 attr_id = ridpointers[(int) token->keyword];
21841 else
21843 error_at (token->location,
21844 "expected an identifier for the attribute name");
21845 return error_mark_node;
21847 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21848 NULL_TREE);
21849 token = cp_lexer_peek_token (parser->lexer);
21851 else
21853 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21854 NULL_TREE);
21855 /* C++11 noreturn attribute is equivalent to GNU's. */
21856 if (is_attribute_p ("noreturn", attr_id))
21857 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21858 /* C++14 deprecated attribute is equivalent to GNU's. */
21859 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21860 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21863 /* Now parse the optional argument clause of the attribute. */
21865 if (token->type != CPP_OPEN_PAREN)
21866 return attribute;
21869 vec<tree, va_gc> *vec;
21870 int attr_flag = normal_attr;
21872 if (attr_ns == get_identifier ("gnu")
21873 && attribute_takes_identifier_p (attr_id))
21874 /* A GNU attribute that takes an identifier in parameter. */
21875 attr_flag = id_attr;
21877 vec = cp_parser_parenthesized_expression_list
21878 (parser, attr_flag, /*cast_p=*/false,
21879 /*allow_expansion_p=*/true,
21880 /*non_constant_p=*/NULL);
21881 if (vec == NULL)
21882 arguments = error_mark_node;
21883 else
21885 arguments = build_tree_list_vec (vec);
21886 release_tree_vector (vec);
21889 if (arguments == error_mark_node)
21890 attribute = error_mark_node;
21891 else
21892 TREE_VALUE (attribute) = arguments;
21895 return attribute;
21898 /* Parse a list of standard C++-11 attributes.
21900 attribute-list:
21901 attribute [opt]
21902 attribute-list , attribute[opt]
21903 attribute ...
21904 attribute-list , attribute ...
21907 static tree
21908 cp_parser_std_attribute_list (cp_parser *parser)
21910 tree attributes = NULL_TREE, attribute = NULL_TREE;
21911 cp_token *token = NULL;
21913 while (true)
21915 attribute = cp_parser_std_attribute (parser);
21916 if (attribute == error_mark_node)
21917 break;
21918 if (attribute != NULL_TREE)
21920 TREE_CHAIN (attribute) = attributes;
21921 attributes = attribute;
21923 token = cp_lexer_peek_token (parser->lexer);
21924 if (token->type != CPP_COMMA)
21925 break;
21926 cp_lexer_consume_token (parser->lexer);
21928 attributes = nreverse (attributes);
21929 return attributes;
21932 /* Parse a standard C++-11 attribute specifier.
21934 attribute-specifier:
21935 [ [ attribute-list ] ]
21936 alignment-specifier
21938 alignment-specifier:
21939 alignas ( type-id ... [opt] )
21940 alignas ( alignment-expression ... [opt] ). */
21942 static tree
21943 cp_parser_std_attribute_spec (cp_parser *parser)
21945 tree attributes = NULL_TREE;
21946 cp_token *token = cp_lexer_peek_token (parser->lexer);
21948 if (token->type == CPP_OPEN_SQUARE
21949 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21951 cp_lexer_consume_token (parser->lexer);
21952 cp_lexer_consume_token (parser->lexer);
21954 attributes = cp_parser_std_attribute_list (parser);
21956 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21957 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21958 cp_parser_skip_to_end_of_statement (parser);
21959 else
21960 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21961 when we are sure that we have actually parsed them. */
21962 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21964 else
21966 tree alignas_expr;
21968 /* Look for an alignment-specifier. */
21970 token = cp_lexer_peek_token (parser->lexer);
21972 if (token->type != CPP_KEYWORD
21973 || token->keyword != RID_ALIGNAS)
21974 return NULL_TREE;
21976 cp_lexer_consume_token (parser->lexer);
21977 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21979 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21981 cp_parser_error (parser, "expected %<(%>");
21982 return error_mark_node;
21985 cp_parser_parse_tentatively (parser);
21986 alignas_expr = cp_parser_type_id (parser);
21988 if (!cp_parser_parse_definitely (parser))
21990 gcc_assert (alignas_expr == error_mark_node
21991 || alignas_expr == NULL_TREE);
21993 alignas_expr =
21994 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21995 /**cp_id_kind=*/NULL);
21996 if (alignas_expr == error_mark_node)
21997 cp_parser_skip_to_end_of_statement (parser);
21998 if (alignas_expr == NULL_TREE
21999 || alignas_expr == error_mark_node)
22000 return alignas_expr;
22003 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22005 cp_parser_error (parser, "expected %<)%>");
22006 return error_mark_node;
22009 alignas_expr = cxx_alignas_expr (alignas_expr);
22011 /* Build the C++-11 representation of an 'aligned'
22012 attribute. */
22013 attributes =
22014 build_tree_list (build_tree_list (get_identifier ("gnu"),
22015 get_identifier ("aligned")),
22016 build_tree_list (NULL_TREE, alignas_expr));
22019 return attributes;
22022 /* Parse a standard C++-11 attribute-specifier-seq.
22024 attribute-specifier-seq:
22025 attribute-specifier-seq [opt] attribute-specifier
22028 static tree
22029 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22031 tree attr_specs = NULL;
22033 while (true)
22035 tree attr_spec = cp_parser_std_attribute_spec (parser);
22036 if (attr_spec == NULL_TREE)
22037 break;
22038 if (attr_spec == error_mark_node)
22039 return error_mark_node;
22041 TREE_CHAIN (attr_spec) = attr_specs;
22042 attr_specs = attr_spec;
22045 attr_specs = nreverse (attr_specs);
22046 return attr_specs;
22049 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22050 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22051 current value of the PEDANTIC flag, regardless of whether or not
22052 the `__extension__' keyword is present. The caller is responsible
22053 for restoring the value of the PEDANTIC flag. */
22055 static bool
22056 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22058 /* Save the old value of the PEDANTIC flag. */
22059 *saved_pedantic = pedantic;
22061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22063 /* Consume the `__extension__' token. */
22064 cp_lexer_consume_token (parser->lexer);
22065 /* We're not being pedantic while the `__extension__' keyword is
22066 in effect. */
22067 pedantic = 0;
22069 return true;
22072 return false;
22075 /* Parse a label declaration.
22077 label-declaration:
22078 __label__ label-declarator-seq ;
22080 label-declarator-seq:
22081 identifier , label-declarator-seq
22082 identifier */
22084 static void
22085 cp_parser_label_declaration (cp_parser* parser)
22087 /* Look for the `__label__' keyword. */
22088 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22090 while (true)
22092 tree identifier;
22094 /* Look for an identifier. */
22095 identifier = cp_parser_identifier (parser);
22096 /* If we failed, stop. */
22097 if (identifier == error_mark_node)
22098 break;
22099 /* Declare it as a label. */
22100 finish_label_decl (identifier);
22101 /* If the next token is a `;', stop. */
22102 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22103 break;
22104 /* Look for the `,' separating the label declarations. */
22105 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22108 /* Look for the final `;'. */
22109 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22112 /* Support Functions */
22114 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22115 NAME should have one of the representations used for an
22116 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22117 is returned. If PARSER->SCOPE is a dependent type, then a
22118 SCOPE_REF is returned.
22120 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22121 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22122 was formed. Abstractly, such entities should not be passed to this
22123 function, because they do not need to be looked up, but it is
22124 simpler to check for this special case here, rather than at the
22125 call-sites.
22127 In cases not explicitly covered above, this function returns a
22128 DECL, OVERLOAD, or baselink representing the result of the lookup.
22129 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22130 is returned.
22132 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22133 (e.g., "struct") that was used. In that case bindings that do not
22134 refer to types are ignored.
22136 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22137 ignored.
22139 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22140 are ignored.
22142 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22143 types.
22145 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22146 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22147 NULL_TREE otherwise. */
22149 static tree
22150 cp_parser_lookup_name (cp_parser *parser, tree name,
22151 enum tag_types tag_type,
22152 bool is_template,
22153 bool is_namespace,
22154 bool check_dependency,
22155 tree *ambiguous_decls,
22156 location_t name_location)
22158 tree decl;
22159 tree object_type = parser->context->object_type;
22161 /* Assume that the lookup will be unambiguous. */
22162 if (ambiguous_decls)
22163 *ambiguous_decls = NULL_TREE;
22165 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22166 no longer valid. Note that if we are parsing tentatively, and
22167 the parse fails, OBJECT_TYPE will be automatically restored. */
22168 parser->context->object_type = NULL_TREE;
22170 if (name == error_mark_node)
22171 return error_mark_node;
22173 /* A template-id has already been resolved; there is no lookup to
22174 do. */
22175 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22176 return name;
22177 if (BASELINK_P (name))
22179 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22180 == TEMPLATE_ID_EXPR);
22181 return name;
22184 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22185 it should already have been checked to make sure that the name
22186 used matches the type being destroyed. */
22187 if (TREE_CODE (name) == BIT_NOT_EXPR)
22189 tree type;
22191 /* Figure out to which type this destructor applies. */
22192 if (parser->scope)
22193 type = parser->scope;
22194 else if (object_type)
22195 type = object_type;
22196 else
22197 type = current_class_type;
22198 /* If that's not a class type, there is no destructor. */
22199 if (!type || !CLASS_TYPE_P (type))
22200 return error_mark_node;
22201 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22202 lazily_declare_fn (sfk_destructor, type);
22203 if (!CLASSTYPE_DESTRUCTORS (type))
22204 return error_mark_node;
22205 /* If it was a class type, return the destructor. */
22206 return CLASSTYPE_DESTRUCTORS (type);
22209 /* By this point, the NAME should be an ordinary identifier. If
22210 the id-expression was a qualified name, the qualifying scope is
22211 stored in PARSER->SCOPE at this point. */
22212 gcc_assert (identifier_p (name));
22214 /* Perform the lookup. */
22215 if (parser->scope)
22217 bool dependent_p;
22219 if (parser->scope == error_mark_node)
22220 return error_mark_node;
22222 /* If the SCOPE is dependent, the lookup must be deferred until
22223 the template is instantiated -- unless we are explicitly
22224 looking up names in uninstantiated templates. Even then, we
22225 cannot look up the name if the scope is not a class type; it
22226 might, for example, be a template type parameter. */
22227 dependent_p = (TYPE_P (parser->scope)
22228 && dependent_scope_p (parser->scope));
22229 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22230 && dependent_p)
22231 /* Defer lookup. */
22232 decl = error_mark_node;
22233 else
22235 tree pushed_scope = NULL_TREE;
22237 /* If PARSER->SCOPE is a dependent type, then it must be a
22238 class type, and we must not be checking dependencies;
22239 otherwise, we would have processed this lookup above. So
22240 that PARSER->SCOPE is not considered a dependent base by
22241 lookup_member, we must enter the scope here. */
22242 if (dependent_p)
22243 pushed_scope = push_scope (parser->scope);
22245 /* If the PARSER->SCOPE is a template specialization, it
22246 may be instantiated during name lookup. In that case,
22247 errors may be issued. Even if we rollback the current
22248 tentative parse, those errors are valid. */
22249 decl = lookup_qualified_name (parser->scope, name,
22250 tag_type != none_type,
22251 /*complain=*/true);
22253 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22254 lookup result and the nested-name-specifier nominates a class C:
22255 * if the name specified after the nested-name-specifier, when
22256 looked up in C, is the injected-class-name of C (Clause 9), or
22257 * if the name specified after the nested-name-specifier is the
22258 same as the identifier or the simple-template-id's template-
22259 name in the last component of the nested-name-specifier,
22260 the name is instead considered to name the constructor of
22261 class C. [ Note: for example, the constructor is not an
22262 acceptable lookup result in an elaborated-type-specifier so
22263 the constructor would not be used in place of the
22264 injected-class-name. --end note ] Such a constructor name
22265 shall be used only in the declarator-id of a declaration that
22266 names a constructor or in a using-declaration. */
22267 if (tag_type == none_type
22268 && DECL_SELF_REFERENCE_P (decl)
22269 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22270 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22271 tag_type != none_type,
22272 /*complain=*/true);
22274 /* If we have a single function from a using decl, pull it out. */
22275 if (TREE_CODE (decl) == OVERLOAD
22276 && !really_overloaded_fn (decl))
22277 decl = OVL_FUNCTION (decl);
22279 if (pushed_scope)
22280 pop_scope (pushed_scope);
22283 /* If the scope is a dependent type and either we deferred lookup or
22284 we did lookup but didn't find the name, rememeber the name. */
22285 if (decl == error_mark_node && TYPE_P (parser->scope)
22286 && dependent_type_p (parser->scope))
22288 if (tag_type)
22290 tree type;
22292 /* The resolution to Core Issue 180 says that `struct
22293 A::B' should be considered a type-name, even if `A'
22294 is dependent. */
22295 type = make_typename_type (parser->scope, name, tag_type,
22296 /*complain=*/tf_error);
22297 if (type != error_mark_node)
22298 decl = TYPE_NAME (type);
22300 else if (is_template
22301 && (cp_parser_next_token_ends_template_argument_p (parser)
22302 || cp_lexer_next_token_is (parser->lexer,
22303 CPP_CLOSE_PAREN)))
22304 decl = make_unbound_class_template (parser->scope,
22305 name, NULL_TREE,
22306 /*complain=*/tf_error);
22307 else
22308 decl = build_qualified_name (/*type=*/NULL_TREE,
22309 parser->scope, name,
22310 is_template);
22312 parser->qualifying_scope = parser->scope;
22313 parser->object_scope = NULL_TREE;
22315 else if (object_type)
22317 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22318 OBJECT_TYPE is not a class. */
22319 if (CLASS_TYPE_P (object_type))
22320 /* If the OBJECT_TYPE is a template specialization, it may
22321 be instantiated during name lookup. In that case, errors
22322 may be issued. Even if we rollback the current tentative
22323 parse, those errors are valid. */
22324 decl = lookup_member (object_type,
22325 name,
22326 /*protect=*/0,
22327 tag_type != none_type,
22328 tf_warning_or_error);
22329 else
22330 decl = NULL_TREE;
22332 if (!decl)
22333 /* Look it up in the enclosing context. */
22334 decl = lookup_name_real (name, tag_type != none_type,
22335 /*nonclass=*/0,
22336 /*block_p=*/true, is_namespace, 0);
22337 parser->object_scope = object_type;
22338 parser->qualifying_scope = NULL_TREE;
22340 else
22342 decl = lookup_name_real (name, tag_type != none_type,
22343 /*nonclass=*/0,
22344 /*block_p=*/true, is_namespace, 0);
22345 parser->qualifying_scope = NULL_TREE;
22346 parser->object_scope = NULL_TREE;
22349 /* If the lookup failed, let our caller know. */
22350 if (!decl || decl == error_mark_node)
22351 return error_mark_node;
22353 /* Pull out the template from an injected-class-name (or multiple). */
22354 if (is_template)
22355 decl = maybe_get_template_decl_from_type_decl (decl);
22357 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22358 if (TREE_CODE (decl) == TREE_LIST)
22360 if (ambiguous_decls)
22361 *ambiguous_decls = decl;
22362 /* The error message we have to print is too complicated for
22363 cp_parser_error, so we incorporate its actions directly. */
22364 if (!cp_parser_simulate_error (parser))
22366 error_at (name_location, "reference to %qD is ambiguous",
22367 name);
22368 print_candidates (decl);
22370 return error_mark_node;
22373 gcc_assert (DECL_P (decl)
22374 || TREE_CODE (decl) == OVERLOAD
22375 || TREE_CODE (decl) == SCOPE_REF
22376 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22377 || BASELINK_P (decl));
22379 /* If we have resolved the name of a member declaration, check to
22380 see if the declaration is accessible. When the name resolves to
22381 set of overloaded functions, accessibility is checked when
22382 overload resolution is done.
22384 During an explicit instantiation, access is not checked at all,
22385 as per [temp.explicit]. */
22386 if (DECL_P (decl))
22387 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22389 maybe_record_typedef_use (decl);
22391 return decl;
22394 /* Like cp_parser_lookup_name, but for use in the typical case where
22395 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22396 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22398 static tree
22399 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22401 return cp_parser_lookup_name (parser, name,
22402 none_type,
22403 /*is_template=*/false,
22404 /*is_namespace=*/false,
22405 /*check_dependency=*/true,
22406 /*ambiguous_decls=*/NULL,
22407 location);
22410 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22411 the current context, return the TYPE_DECL. If TAG_NAME_P is
22412 true, the DECL indicates the class being defined in a class-head,
22413 or declared in an elaborated-type-specifier.
22415 Otherwise, return DECL. */
22417 static tree
22418 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22420 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22421 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22423 struct A {
22424 template <typename T> struct B;
22427 template <typename T> struct A::B {};
22429 Similarly, in an elaborated-type-specifier:
22431 namespace N { struct X{}; }
22433 struct A {
22434 template <typename T> friend struct N::X;
22437 However, if the DECL refers to a class type, and we are in
22438 the scope of the class, then the name lookup automatically
22439 finds the TYPE_DECL created by build_self_reference rather
22440 than a TEMPLATE_DECL. For example, in:
22442 template <class T> struct S {
22443 S s;
22446 there is no need to handle such case. */
22448 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22449 return DECL_TEMPLATE_RESULT (decl);
22451 return decl;
22454 /* If too many, or too few, template-parameter lists apply to the
22455 declarator, issue an error message. Returns TRUE if all went well,
22456 and FALSE otherwise. */
22458 static bool
22459 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22460 cp_declarator *declarator,
22461 location_t declarator_location)
22463 switch (declarator->kind)
22465 case cdk_id:
22467 unsigned num_templates = 0;
22468 tree scope = declarator->u.id.qualifying_scope;
22470 if (scope)
22471 num_templates = num_template_headers_for_class (scope);
22472 else if (TREE_CODE (declarator->u.id.unqualified_name)
22473 == TEMPLATE_ID_EXPR)
22474 /* If the DECLARATOR has the form `X<y>' then it uses one
22475 additional level of template parameters. */
22476 ++num_templates;
22478 return cp_parser_check_template_parameters
22479 (parser, num_templates, declarator_location, declarator);
22482 case cdk_function:
22483 case cdk_array:
22484 case cdk_pointer:
22485 case cdk_reference:
22486 case cdk_ptrmem:
22487 return (cp_parser_check_declarator_template_parameters
22488 (parser, declarator->declarator, declarator_location));
22490 case cdk_error:
22491 return true;
22493 default:
22494 gcc_unreachable ();
22496 return false;
22499 /* NUM_TEMPLATES were used in the current declaration. If that is
22500 invalid, return FALSE and issue an error messages. Otherwise,
22501 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22502 declarator and we can print more accurate diagnostics. */
22504 static bool
22505 cp_parser_check_template_parameters (cp_parser* parser,
22506 unsigned num_templates,
22507 location_t location,
22508 cp_declarator *declarator)
22510 /* If there are the same number of template classes and parameter
22511 lists, that's OK. */
22512 if (parser->num_template_parameter_lists == num_templates)
22513 return true;
22514 /* If there are more, but only one more, then we are referring to a
22515 member template. That's OK too. */
22516 if (parser->num_template_parameter_lists == num_templates + 1)
22517 return true;
22518 /* If there are more template classes than parameter lists, we have
22519 something like:
22521 template <class T> void S<T>::R<T>::f (); */
22522 if (parser->num_template_parameter_lists < num_templates)
22524 if (declarator && !current_function_decl)
22525 error_at (location, "specializing member %<%T::%E%> "
22526 "requires %<template<>%> syntax",
22527 declarator->u.id.qualifying_scope,
22528 declarator->u.id.unqualified_name);
22529 else if (declarator)
22530 error_at (location, "invalid declaration of %<%T::%E%>",
22531 declarator->u.id.qualifying_scope,
22532 declarator->u.id.unqualified_name);
22533 else
22534 error_at (location, "too few template-parameter-lists");
22535 return false;
22537 /* Otherwise, there are too many template parameter lists. We have
22538 something like:
22540 template <class T> template <class U> void S::f(); */
22541 error_at (location, "too many template-parameter-lists");
22542 return false;
22545 /* Parse an optional `::' token indicating that the following name is
22546 from the global namespace. If so, PARSER->SCOPE is set to the
22547 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22548 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22549 Returns the new value of PARSER->SCOPE, if the `::' token is
22550 present, and NULL_TREE otherwise. */
22552 static tree
22553 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22555 cp_token *token;
22557 /* Peek at the next token. */
22558 token = cp_lexer_peek_token (parser->lexer);
22559 /* If we're looking at a `::' token then we're starting from the
22560 global namespace, not our current location. */
22561 if (token->type == CPP_SCOPE)
22563 /* Consume the `::' token. */
22564 cp_lexer_consume_token (parser->lexer);
22565 /* Set the SCOPE so that we know where to start the lookup. */
22566 parser->scope = global_namespace;
22567 parser->qualifying_scope = global_namespace;
22568 parser->object_scope = NULL_TREE;
22570 return parser->scope;
22572 else if (!current_scope_valid_p)
22574 parser->scope = NULL_TREE;
22575 parser->qualifying_scope = NULL_TREE;
22576 parser->object_scope = NULL_TREE;
22579 return NULL_TREE;
22582 /* Returns TRUE if the upcoming token sequence is the start of a
22583 constructor declarator. If FRIEND_P is true, the declarator is
22584 preceded by the `friend' specifier. */
22586 static bool
22587 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22589 bool constructor_p;
22590 bool outside_class_specifier_p;
22591 tree nested_name_specifier;
22592 cp_token *next_token;
22594 /* The common case is that this is not a constructor declarator, so
22595 try to avoid doing lots of work if at all possible. It's not
22596 valid declare a constructor at function scope. */
22597 if (parser->in_function_body)
22598 return false;
22599 /* And only certain tokens can begin a constructor declarator. */
22600 next_token = cp_lexer_peek_token (parser->lexer);
22601 if (next_token->type != CPP_NAME
22602 && next_token->type != CPP_SCOPE
22603 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22604 && next_token->type != CPP_TEMPLATE_ID)
22605 return false;
22607 /* Parse tentatively; we are going to roll back all of the tokens
22608 consumed here. */
22609 cp_parser_parse_tentatively (parser);
22610 /* Assume that we are looking at a constructor declarator. */
22611 constructor_p = true;
22613 /* Look for the optional `::' operator. */
22614 cp_parser_global_scope_opt (parser,
22615 /*current_scope_valid_p=*/false);
22616 /* Look for the nested-name-specifier. */
22617 nested_name_specifier
22618 = (cp_parser_nested_name_specifier_opt (parser,
22619 /*typename_keyword_p=*/false,
22620 /*check_dependency_p=*/false,
22621 /*type_p=*/false,
22622 /*is_declaration=*/false));
22624 outside_class_specifier_p = (!at_class_scope_p ()
22625 || !TYPE_BEING_DEFINED (current_class_type)
22626 || friend_p);
22628 /* Outside of a class-specifier, there must be a
22629 nested-name-specifier. */
22630 if (!nested_name_specifier && outside_class_specifier_p)
22631 constructor_p = false;
22632 else if (nested_name_specifier == error_mark_node)
22633 constructor_p = false;
22635 /* If we have a class scope, this is easy; DR 147 says that S::S always
22636 names the constructor, and no other qualified name could. */
22637 if (constructor_p && nested_name_specifier
22638 && CLASS_TYPE_P (nested_name_specifier))
22640 tree id = cp_parser_unqualified_id (parser,
22641 /*template_keyword_p=*/false,
22642 /*check_dependency_p=*/false,
22643 /*declarator_p=*/true,
22644 /*optional_p=*/false);
22645 if (is_overloaded_fn (id))
22646 id = DECL_NAME (get_first_fn (id));
22647 if (!constructor_name_p (id, nested_name_specifier))
22648 constructor_p = false;
22650 /* If we still think that this might be a constructor-declarator,
22651 look for a class-name. */
22652 else if (constructor_p)
22654 /* If we have:
22656 template <typename T> struct S {
22657 S();
22660 we must recognize that the nested `S' names a class. */
22661 tree type_decl;
22662 type_decl = cp_parser_class_name (parser,
22663 /*typename_keyword_p=*/false,
22664 /*template_keyword_p=*/false,
22665 none_type,
22666 /*check_dependency_p=*/false,
22667 /*class_head_p=*/false,
22668 /*is_declaration=*/false);
22669 /* If there was no class-name, then this is not a constructor.
22670 Otherwise, if we are in a class-specifier and we aren't
22671 handling a friend declaration, check that its type matches
22672 current_class_type (c++/38313). Note: error_mark_node
22673 is left alone for error recovery purposes. */
22674 constructor_p = (!cp_parser_error_occurred (parser)
22675 && (outside_class_specifier_p
22676 || type_decl == error_mark_node
22677 || same_type_p (current_class_type,
22678 TREE_TYPE (type_decl))));
22680 /* If we're still considering a constructor, we have to see a `(',
22681 to begin the parameter-declaration-clause, followed by either a
22682 `)', an `...', or a decl-specifier. We need to check for a
22683 type-specifier to avoid being fooled into thinking that:
22685 S (f) (int);
22687 is a constructor. (It is actually a function named `f' that
22688 takes one parameter (of type `int') and returns a value of type
22689 `S'. */
22690 if (constructor_p
22691 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22692 constructor_p = false;
22694 if (constructor_p
22695 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22696 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22697 /* A parameter declaration begins with a decl-specifier,
22698 which is either the "attribute" keyword, a storage class
22699 specifier, or (usually) a type-specifier. */
22700 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22702 tree type;
22703 tree pushed_scope = NULL_TREE;
22704 unsigned saved_num_template_parameter_lists;
22706 /* Names appearing in the type-specifier should be looked up
22707 in the scope of the class. */
22708 if (current_class_type)
22709 type = NULL_TREE;
22710 else
22712 type = TREE_TYPE (type_decl);
22713 if (TREE_CODE (type) == TYPENAME_TYPE)
22715 type = resolve_typename_type (type,
22716 /*only_current_p=*/false);
22717 if (TREE_CODE (type) == TYPENAME_TYPE)
22719 cp_parser_abort_tentative_parse (parser);
22720 return false;
22723 pushed_scope = push_scope (type);
22726 /* Inside the constructor parameter list, surrounding
22727 template-parameter-lists do not apply. */
22728 saved_num_template_parameter_lists
22729 = parser->num_template_parameter_lists;
22730 parser->num_template_parameter_lists = 0;
22732 /* Look for the type-specifier. */
22733 cp_parser_type_specifier (parser,
22734 CP_PARSER_FLAGS_NONE,
22735 /*decl_specs=*/NULL,
22736 /*is_declarator=*/true,
22737 /*declares_class_or_enum=*/NULL,
22738 /*is_cv_qualifier=*/NULL);
22740 parser->num_template_parameter_lists
22741 = saved_num_template_parameter_lists;
22743 /* Leave the scope of the class. */
22744 if (pushed_scope)
22745 pop_scope (pushed_scope);
22747 constructor_p = !cp_parser_error_occurred (parser);
22751 /* We did not really want to consume any tokens. */
22752 cp_parser_abort_tentative_parse (parser);
22754 return constructor_p;
22757 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22758 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22759 they must be performed once we are in the scope of the function.
22761 Returns the function defined. */
22763 static tree
22764 cp_parser_function_definition_from_specifiers_and_declarator
22765 (cp_parser* parser,
22766 cp_decl_specifier_seq *decl_specifiers,
22767 tree attributes,
22768 const cp_declarator *declarator)
22770 tree fn;
22771 bool success_p;
22773 /* Begin the function-definition. */
22774 success_p = start_function (decl_specifiers, declarator, attributes);
22776 /* The things we're about to see are not directly qualified by any
22777 template headers we've seen thus far. */
22778 reset_specialization ();
22780 /* If there were names looked up in the decl-specifier-seq that we
22781 did not check, check them now. We must wait until we are in the
22782 scope of the function to perform the checks, since the function
22783 might be a friend. */
22784 perform_deferred_access_checks (tf_warning_or_error);
22786 if (success_p)
22788 cp_finalize_omp_declare_simd (parser, current_function_decl);
22789 parser->omp_declare_simd = NULL;
22792 if (!success_p)
22794 /* Skip the entire function. */
22795 cp_parser_skip_to_end_of_block_or_statement (parser);
22796 fn = error_mark_node;
22798 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22800 /* Seen already, skip it. An error message has already been output. */
22801 cp_parser_skip_to_end_of_block_or_statement (parser);
22802 fn = current_function_decl;
22803 current_function_decl = NULL_TREE;
22804 /* If this is a function from a class, pop the nested class. */
22805 if (current_class_name)
22806 pop_nested_class ();
22808 else
22810 timevar_id_t tv;
22811 if (DECL_DECLARED_INLINE_P (current_function_decl))
22812 tv = TV_PARSE_INLINE;
22813 else
22814 tv = TV_PARSE_FUNC;
22815 timevar_push (tv);
22816 fn = cp_parser_function_definition_after_declarator (parser,
22817 /*inline_p=*/false);
22818 timevar_pop (tv);
22821 return fn;
22824 /* Parse the part of a function-definition that follows the
22825 declarator. INLINE_P is TRUE iff this function is an inline
22826 function defined within a class-specifier.
22828 Returns the function defined. */
22830 static tree
22831 cp_parser_function_definition_after_declarator (cp_parser* parser,
22832 bool inline_p)
22834 tree fn;
22835 bool ctor_initializer_p = false;
22836 bool saved_in_unbraced_linkage_specification_p;
22837 bool saved_in_function_body;
22838 unsigned saved_num_template_parameter_lists;
22839 cp_token *token;
22840 bool fully_implicit_function_template_p
22841 = parser->fully_implicit_function_template_p;
22842 parser->fully_implicit_function_template_p = false;
22843 tree implicit_template_parms
22844 = parser->implicit_template_parms;
22845 parser->implicit_template_parms = 0;
22846 cp_binding_level* implicit_template_scope
22847 = parser->implicit_template_scope;
22848 parser->implicit_template_scope = 0;
22850 saved_in_function_body = parser->in_function_body;
22851 parser->in_function_body = true;
22852 /* If the next token is `return', then the code may be trying to
22853 make use of the "named return value" extension that G++ used to
22854 support. */
22855 token = cp_lexer_peek_token (parser->lexer);
22856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22858 /* Consume the `return' keyword. */
22859 cp_lexer_consume_token (parser->lexer);
22860 /* Look for the identifier that indicates what value is to be
22861 returned. */
22862 cp_parser_identifier (parser);
22863 /* Issue an error message. */
22864 error_at (token->location,
22865 "named return values are no longer supported");
22866 /* Skip tokens until we reach the start of the function body. */
22867 while (true)
22869 cp_token *token = cp_lexer_peek_token (parser->lexer);
22870 if (token->type == CPP_OPEN_BRACE
22871 || token->type == CPP_EOF
22872 || token->type == CPP_PRAGMA_EOL)
22873 break;
22874 cp_lexer_consume_token (parser->lexer);
22877 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22878 anything declared inside `f'. */
22879 saved_in_unbraced_linkage_specification_p
22880 = parser->in_unbraced_linkage_specification_p;
22881 parser->in_unbraced_linkage_specification_p = false;
22882 /* Inside the function, surrounding template-parameter-lists do not
22883 apply. */
22884 saved_num_template_parameter_lists
22885 = parser->num_template_parameter_lists;
22886 parser->num_template_parameter_lists = 0;
22888 start_lambda_scope (current_function_decl);
22890 /* If the next token is `try', `__transaction_atomic', or
22891 `__transaction_relaxed`, then we are looking at either function-try-block
22892 or function-transaction-block. Note that all of these include the
22893 function-body. */
22894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22895 ctor_initializer_p = cp_parser_function_transaction (parser,
22896 RID_TRANSACTION_ATOMIC);
22897 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22898 RID_TRANSACTION_RELAXED))
22899 ctor_initializer_p = cp_parser_function_transaction (parser,
22900 RID_TRANSACTION_RELAXED);
22901 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22902 ctor_initializer_p = cp_parser_function_try_block (parser);
22903 else
22904 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22905 (parser, /*in_function_try_block=*/false);
22907 finish_lambda_scope ();
22909 /* Finish the function. */
22910 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22911 (inline_p ? 2 : 0));
22912 /* Generate code for it, if necessary. */
22913 expand_or_defer_fn (fn);
22914 /* Restore the saved values. */
22915 parser->in_unbraced_linkage_specification_p
22916 = saved_in_unbraced_linkage_specification_p;
22917 parser->num_template_parameter_lists
22918 = saved_num_template_parameter_lists;
22919 parser->in_function_body = saved_in_function_body;
22921 parser->fully_implicit_function_template_p
22922 = fully_implicit_function_template_p;
22923 parser->implicit_template_parms
22924 = implicit_template_parms;
22925 parser->implicit_template_scope
22926 = implicit_template_scope;
22928 if (parser->fully_implicit_function_template_p)
22929 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22931 return fn;
22934 /* Parse a template-declaration, assuming that the `export' (and
22935 `extern') keywords, if present, has already been scanned. MEMBER_P
22936 is as for cp_parser_template_declaration. */
22938 static void
22939 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22941 tree decl = NULL_TREE;
22942 vec<deferred_access_check, va_gc> *checks;
22943 tree parameter_list;
22944 bool friend_p = false;
22945 bool need_lang_pop;
22946 cp_token *token;
22948 /* Look for the `template' keyword. */
22949 token = cp_lexer_peek_token (parser->lexer);
22950 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22951 return;
22953 /* And the `<'. */
22954 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22955 return;
22956 if (at_class_scope_p () && current_function_decl)
22958 /* 14.5.2.2 [temp.mem]
22960 A local class shall not have member templates. */
22961 error_at (token->location,
22962 "invalid declaration of member template in local class");
22963 cp_parser_skip_to_end_of_block_or_statement (parser);
22964 return;
22966 /* [temp]
22968 A template ... shall not have C linkage. */
22969 if (current_lang_name == lang_name_c)
22971 error_at (token->location, "template with C linkage");
22972 /* Give it C++ linkage to avoid confusing other parts of the
22973 front end. */
22974 push_lang_context (lang_name_cplusplus);
22975 need_lang_pop = true;
22977 else
22978 need_lang_pop = false;
22980 /* We cannot perform access checks on the template parameter
22981 declarations until we know what is being declared, just as we
22982 cannot check the decl-specifier list. */
22983 push_deferring_access_checks (dk_deferred);
22985 /* If the next token is `>', then we have an invalid
22986 specialization. Rather than complain about an invalid template
22987 parameter, issue an error message here. */
22988 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22990 cp_parser_error (parser, "invalid explicit specialization");
22991 begin_specialization ();
22992 parameter_list = NULL_TREE;
22994 else
22996 /* Parse the template parameters. */
22997 parameter_list = cp_parser_template_parameter_list (parser);
23000 /* Get the deferred access checks from the parameter list. These
23001 will be checked once we know what is being declared, as for a
23002 member template the checks must be performed in the scope of the
23003 class containing the member. */
23004 checks = get_deferred_access_checks ();
23006 /* Look for the `>'. */
23007 cp_parser_skip_to_end_of_template_parameter_list (parser);
23008 /* We just processed one more parameter list. */
23009 ++parser->num_template_parameter_lists;
23010 /* If the next token is `template', there are more template
23011 parameters. */
23012 if (cp_lexer_next_token_is_keyword (parser->lexer,
23013 RID_TEMPLATE))
23014 cp_parser_template_declaration_after_export (parser, member_p);
23015 else if (cxx_dialect >= cxx11
23016 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23017 decl = cp_parser_alias_declaration (parser);
23018 else
23020 /* There are no access checks when parsing a template, as we do not
23021 know if a specialization will be a friend. */
23022 push_deferring_access_checks (dk_no_check);
23023 token = cp_lexer_peek_token (parser->lexer);
23024 decl = cp_parser_single_declaration (parser,
23025 checks,
23026 member_p,
23027 /*explicit_specialization_p=*/false,
23028 &friend_p);
23029 pop_deferring_access_checks ();
23031 /* If this is a member template declaration, let the front
23032 end know. */
23033 if (member_p && !friend_p && decl)
23035 if (TREE_CODE (decl) == TYPE_DECL)
23036 cp_parser_check_access_in_redeclaration (decl, token->location);
23038 decl = finish_member_template_decl (decl);
23040 else if (friend_p && decl
23041 && DECL_DECLARES_TYPE_P (decl))
23042 make_friend_class (current_class_type, TREE_TYPE (decl),
23043 /*complain=*/true);
23045 /* We are done with the current parameter list. */
23046 --parser->num_template_parameter_lists;
23048 pop_deferring_access_checks ();
23050 /* Finish up. */
23051 finish_template_decl (parameter_list);
23053 /* Check the template arguments for a literal operator template. */
23054 if (decl
23055 && DECL_DECLARES_FUNCTION_P (decl)
23056 && UDLIT_OPER_P (DECL_NAME (decl)))
23058 bool ok = true;
23059 if (parameter_list == NULL_TREE)
23060 ok = false;
23061 else
23063 int num_parms = TREE_VEC_LENGTH (parameter_list);
23064 if (num_parms == 1)
23066 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23067 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23068 if (TREE_TYPE (parm) != char_type_node
23069 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23070 ok = false;
23072 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23074 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23075 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23076 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23077 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23078 if (TREE_TYPE (parm) != TREE_TYPE (type)
23079 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23080 ok = false;
23082 else
23083 ok = false;
23085 if (!ok)
23086 error ("literal operator template %qD has invalid parameter list."
23087 " Expected non-type template argument pack <char...>"
23088 " or <typename CharT, CharT...>",
23089 decl);
23091 /* Register member declarations. */
23092 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23093 finish_member_declaration (decl);
23094 /* For the erroneous case of a template with C linkage, we pushed an
23095 implicit C++ linkage scope; exit that scope now. */
23096 if (need_lang_pop)
23097 pop_lang_context ();
23098 /* If DECL is a function template, we must return to parse it later.
23099 (Even though there is no definition, there might be default
23100 arguments that need handling.) */
23101 if (member_p && decl
23102 && DECL_DECLARES_FUNCTION_P (decl))
23103 vec_safe_push (unparsed_funs_with_definitions, decl);
23106 /* Perform the deferred access checks from a template-parameter-list.
23107 CHECKS is a TREE_LIST of access checks, as returned by
23108 get_deferred_access_checks. */
23110 static void
23111 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23113 ++processing_template_parmlist;
23114 perform_access_checks (checks, tf_warning_or_error);
23115 --processing_template_parmlist;
23118 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23119 `function-definition' sequence that follows a template header.
23120 If MEMBER_P is true, this declaration appears in a class scope.
23122 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23123 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23125 static tree
23126 cp_parser_single_declaration (cp_parser* parser,
23127 vec<deferred_access_check, va_gc> *checks,
23128 bool member_p,
23129 bool explicit_specialization_p,
23130 bool* friend_p)
23132 int declares_class_or_enum;
23133 tree decl = NULL_TREE;
23134 cp_decl_specifier_seq decl_specifiers;
23135 bool function_definition_p = false;
23136 cp_token *decl_spec_token_start;
23138 /* This function is only used when processing a template
23139 declaration. */
23140 gcc_assert (innermost_scope_kind () == sk_template_parms
23141 || innermost_scope_kind () == sk_template_spec);
23143 /* Defer access checks until we know what is being declared. */
23144 push_deferring_access_checks (dk_deferred);
23146 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23147 alternative. */
23148 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23149 cp_parser_decl_specifier_seq (parser,
23150 CP_PARSER_FLAGS_OPTIONAL,
23151 &decl_specifiers,
23152 &declares_class_or_enum);
23153 if (friend_p)
23154 *friend_p = cp_parser_friend_p (&decl_specifiers);
23156 /* There are no template typedefs. */
23157 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23159 error_at (decl_spec_token_start->location,
23160 "template declaration of %<typedef%>");
23161 decl = error_mark_node;
23164 /* Gather up the access checks that occurred the
23165 decl-specifier-seq. */
23166 stop_deferring_access_checks ();
23168 /* Check for the declaration of a template class. */
23169 if (declares_class_or_enum)
23171 if (cp_parser_declares_only_class_p (parser))
23173 decl = shadow_tag (&decl_specifiers);
23175 /* In this case:
23177 struct C {
23178 friend template <typename T> struct A<T>::B;
23181 A<T>::B will be represented by a TYPENAME_TYPE, and
23182 therefore not recognized by shadow_tag. */
23183 if (friend_p && *friend_p
23184 && !decl
23185 && decl_specifiers.type
23186 && TYPE_P (decl_specifiers.type))
23187 decl = decl_specifiers.type;
23189 if (decl && decl != error_mark_node)
23190 decl = TYPE_NAME (decl);
23191 else
23192 decl = error_mark_node;
23194 /* Perform access checks for template parameters. */
23195 cp_parser_perform_template_parameter_access_checks (checks);
23199 /* Complain about missing 'typename' or other invalid type names. */
23200 if (!decl_specifiers.any_type_specifiers_p
23201 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23203 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23204 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23205 the rest of this declaration. */
23206 decl = error_mark_node;
23207 goto out;
23210 /* If it's not a template class, try for a template function. If
23211 the next token is a `;', then this declaration does not declare
23212 anything. But, if there were errors in the decl-specifiers, then
23213 the error might well have come from an attempted class-specifier.
23214 In that case, there's no need to warn about a missing declarator. */
23215 if (!decl
23216 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23217 || decl_specifiers.type != error_mark_node))
23219 decl = cp_parser_init_declarator (parser,
23220 &decl_specifiers,
23221 checks,
23222 /*function_definition_allowed_p=*/true,
23223 member_p,
23224 declares_class_or_enum,
23225 &function_definition_p,
23226 NULL);
23228 /* 7.1.1-1 [dcl.stc]
23230 A storage-class-specifier shall not be specified in an explicit
23231 specialization... */
23232 if (decl
23233 && explicit_specialization_p
23234 && decl_specifiers.storage_class != sc_none)
23236 error_at (decl_spec_token_start->location,
23237 "explicit template specialization cannot have a storage class");
23238 decl = error_mark_node;
23241 if (decl && VAR_P (decl))
23242 check_template_variable (decl);
23245 /* Look for a trailing `;' after the declaration. */
23246 if (!function_definition_p
23247 && (decl == error_mark_node
23248 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23249 cp_parser_skip_to_end_of_block_or_statement (parser);
23251 out:
23252 pop_deferring_access_checks ();
23254 /* Clear any current qualification; whatever comes next is the start
23255 of something new. */
23256 parser->scope = NULL_TREE;
23257 parser->qualifying_scope = NULL_TREE;
23258 parser->object_scope = NULL_TREE;
23260 return decl;
23263 /* Parse a cast-expression that is not the operand of a unary "&". */
23265 static tree
23266 cp_parser_simple_cast_expression (cp_parser *parser)
23268 return cp_parser_cast_expression (parser, /*address_p=*/false,
23269 /*cast_p=*/false, /*decltype*/false, NULL);
23272 /* Parse a functional cast to TYPE. Returns an expression
23273 representing the cast. */
23275 static tree
23276 cp_parser_functional_cast (cp_parser* parser, tree type)
23278 vec<tree, va_gc> *vec;
23279 tree expression_list;
23280 tree cast;
23281 bool nonconst_p;
23283 if (!type)
23284 type = error_mark_node;
23286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23288 cp_lexer_set_source_position (parser->lexer);
23289 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23290 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23291 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23292 if (TREE_CODE (type) == TYPE_DECL)
23293 type = TREE_TYPE (type);
23294 return finish_compound_literal (type, expression_list,
23295 tf_warning_or_error);
23299 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23300 /*cast_p=*/true,
23301 /*allow_expansion_p=*/true,
23302 /*non_constant_p=*/NULL);
23303 if (vec == NULL)
23304 expression_list = error_mark_node;
23305 else
23307 expression_list = build_tree_list_vec (vec);
23308 release_tree_vector (vec);
23311 cast = build_functional_cast (type, expression_list,
23312 tf_warning_or_error);
23313 /* [expr.const]/1: In an integral constant expression "only type
23314 conversions to integral or enumeration type can be used". */
23315 if (TREE_CODE (type) == TYPE_DECL)
23316 type = TREE_TYPE (type);
23317 if (cast != error_mark_node
23318 && !cast_valid_in_integral_constant_expression_p (type)
23319 && cp_parser_non_integral_constant_expression (parser,
23320 NIC_CONSTRUCTOR))
23321 return error_mark_node;
23322 return cast;
23325 /* Save the tokens that make up the body of a member function defined
23326 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23327 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23328 specifiers applied to the declaration. Returns the FUNCTION_DECL
23329 for the member function. */
23331 static tree
23332 cp_parser_save_member_function_body (cp_parser* parser,
23333 cp_decl_specifier_seq *decl_specifiers,
23334 cp_declarator *declarator,
23335 tree attributes)
23337 cp_token *first;
23338 cp_token *last;
23339 tree fn;
23341 /* Create the FUNCTION_DECL. */
23342 fn = grokmethod (decl_specifiers, declarator, attributes);
23343 cp_finalize_omp_declare_simd (parser, fn);
23344 /* If something went badly wrong, bail out now. */
23345 if (fn == error_mark_node)
23347 /* If there's a function-body, skip it. */
23348 if (cp_parser_token_starts_function_definition_p
23349 (cp_lexer_peek_token (parser->lexer)))
23350 cp_parser_skip_to_end_of_block_or_statement (parser);
23351 return error_mark_node;
23354 /* Remember it, if there default args to post process. */
23355 cp_parser_save_default_args (parser, fn);
23357 /* Save away the tokens that make up the body of the
23358 function. */
23359 first = parser->lexer->next_token;
23360 /* Handle function try blocks. */
23361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23362 cp_lexer_consume_token (parser->lexer);
23363 /* We can have braced-init-list mem-initializers before the fn body. */
23364 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23366 cp_lexer_consume_token (parser->lexer);
23367 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23369 /* cache_group will stop after an un-nested { } pair, too. */
23370 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23371 break;
23373 /* variadic mem-inits have ... after the ')'. */
23374 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23375 cp_lexer_consume_token (parser->lexer);
23378 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23379 /* Handle function try blocks. */
23380 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23381 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23382 last = parser->lexer->next_token;
23384 /* Save away the inline definition; we will process it when the
23385 class is complete. */
23386 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23387 DECL_PENDING_INLINE_P (fn) = 1;
23389 /* We need to know that this was defined in the class, so that
23390 friend templates are handled correctly. */
23391 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23393 /* Add FN to the queue of functions to be parsed later. */
23394 vec_safe_push (unparsed_funs_with_definitions, fn);
23396 return fn;
23399 /* Save the tokens that make up the in-class initializer for a non-static
23400 data member. Returns a DEFAULT_ARG. */
23402 static tree
23403 cp_parser_save_nsdmi (cp_parser* parser)
23405 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23408 /* Parse a template-argument-list, as well as the trailing ">" (but
23409 not the opening "<"). See cp_parser_template_argument_list for the
23410 return value. */
23412 static tree
23413 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23415 tree arguments;
23416 tree saved_scope;
23417 tree saved_qualifying_scope;
23418 tree saved_object_scope;
23419 bool saved_greater_than_is_operator_p;
23420 int saved_unevaluated_operand;
23421 int saved_inhibit_evaluation_warnings;
23423 /* [temp.names]
23425 When parsing a template-id, the first non-nested `>' is taken as
23426 the end of the template-argument-list rather than a greater-than
23427 operator. */
23428 saved_greater_than_is_operator_p
23429 = parser->greater_than_is_operator_p;
23430 parser->greater_than_is_operator_p = false;
23431 /* Parsing the argument list may modify SCOPE, so we save it
23432 here. */
23433 saved_scope = parser->scope;
23434 saved_qualifying_scope = parser->qualifying_scope;
23435 saved_object_scope = parser->object_scope;
23436 /* We need to evaluate the template arguments, even though this
23437 template-id may be nested within a "sizeof". */
23438 saved_unevaluated_operand = cp_unevaluated_operand;
23439 cp_unevaluated_operand = 0;
23440 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23441 c_inhibit_evaluation_warnings = 0;
23442 /* Parse the template-argument-list itself. */
23443 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23444 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23445 arguments = NULL_TREE;
23446 else
23447 arguments = cp_parser_template_argument_list (parser);
23448 /* Look for the `>' that ends the template-argument-list. If we find
23449 a '>>' instead, it's probably just a typo. */
23450 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23452 if (cxx_dialect != cxx98)
23454 /* In C++0x, a `>>' in a template argument list or cast
23455 expression is considered to be two separate `>'
23456 tokens. So, change the current token to a `>', but don't
23457 consume it: it will be consumed later when the outer
23458 template argument list (or cast expression) is parsed.
23459 Note that this replacement of `>' for `>>' is necessary
23460 even if we are parsing tentatively: in the tentative
23461 case, after calling
23462 cp_parser_enclosed_template_argument_list we will always
23463 throw away all of the template arguments and the first
23464 closing `>', either because the template argument list
23465 was erroneous or because we are replacing those tokens
23466 with a CPP_TEMPLATE_ID token. The second `>' (which will
23467 not have been thrown away) is needed either to close an
23468 outer template argument list or to complete a new-style
23469 cast. */
23470 cp_token *token = cp_lexer_peek_token (parser->lexer);
23471 token->type = CPP_GREATER;
23473 else if (!saved_greater_than_is_operator_p)
23475 /* If we're in a nested template argument list, the '>>' has
23476 to be a typo for '> >'. We emit the error message, but we
23477 continue parsing and we push a '>' as next token, so that
23478 the argument list will be parsed correctly. Note that the
23479 global source location is still on the token before the
23480 '>>', so we need to say explicitly where we want it. */
23481 cp_token *token = cp_lexer_peek_token (parser->lexer);
23482 error_at (token->location, "%<>>%> should be %<> >%> "
23483 "within a nested template argument list");
23485 token->type = CPP_GREATER;
23487 else
23489 /* If this is not a nested template argument list, the '>>'
23490 is a typo for '>'. Emit an error message and continue.
23491 Same deal about the token location, but here we can get it
23492 right by consuming the '>>' before issuing the diagnostic. */
23493 cp_token *token = cp_lexer_consume_token (parser->lexer);
23494 error_at (token->location,
23495 "spurious %<>>%>, use %<>%> to terminate "
23496 "a template argument list");
23499 else
23500 cp_parser_skip_to_end_of_template_parameter_list (parser);
23501 /* The `>' token might be a greater-than operator again now. */
23502 parser->greater_than_is_operator_p
23503 = saved_greater_than_is_operator_p;
23504 /* Restore the SAVED_SCOPE. */
23505 parser->scope = saved_scope;
23506 parser->qualifying_scope = saved_qualifying_scope;
23507 parser->object_scope = saved_object_scope;
23508 cp_unevaluated_operand = saved_unevaluated_operand;
23509 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23511 return arguments;
23514 /* MEMBER_FUNCTION is a member function, or a friend. If default
23515 arguments, or the body of the function have not yet been parsed,
23516 parse them now. */
23518 static void
23519 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23521 timevar_push (TV_PARSE_INMETH);
23522 /* If this member is a template, get the underlying
23523 FUNCTION_DECL. */
23524 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23525 member_function = DECL_TEMPLATE_RESULT (member_function);
23527 /* There should not be any class definitions in progress at this
23528 point; the bodies of members are only parsed outside of all class
23529 definitions. */
23530 gcc_assert (parser->num_classes_being_defined == 0);
23531 /* While we're parsing the member functions we might encounter more
23532 classes. We want to handle them right away, but we don't want
23533 them getting mixed up with functions that are currently in the
23534 queue. */
23535 push_unparsed_function_queues (parser);
23537 /* Make sure that any template parameters are in scope. */
23538 maybe_begin_member_template_processing (member_function);
23540 /* If the body of the function has not yet been parsed, parse it
23541 now. */
23542 if (DECL_PENDING_INLINE_P (member_function))
23544 tree function_scope;
23545 cp_token_cache *tokens;
23547 /* The function is no longer pending; we are processing it. */
23548 tokens = DECL_PENDING_INLINE_INFO (member_function);
23549 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23550 DECL_PENDING_INLINE_P (member_function) = 0;
23552 /* If this is a local class, enter the scope of the containing
23553 function. */
23554 function_scope = current_function_decl;
23555 if (function_scope)
23556 push_function_context ();
23558 /* Push the body of the function onto the lexer stack. */
23559 cp_parser_push_lexer_for_tokens (parser, tokens);
23561 /* Let the front end know that we going to be defining this
23562 function. */
23563 start_preparsed_function (member_function, NULL_TREE,
23564 SF_PRE_PARSED | SF_INCLASS_INLINE);
23566 /* Don't do access checking if it is a templated function. */
23567 if (processing_template_decl)
23568 push_deferring_access_checks (dk_no_check);
23570 /* #pragma omp declare reduction needs special parsing. */
23571 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23573 parser->lexer->in_pragma = true;
23574 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23575 finish_function (/*inline*/2);
23576 cp_check_omp_declare_reduction (member_function);
23578 else
23579 /* Now, parse the body of the function. */
23580 cp_parser_function_definition_after_declarator (parser,
23581 /*inline_p=*/true);
23583 if (processing_template_decl)
23584 pop_deferring_access_checks ();
23586 /* Leave the scope of the containing function. */
23587 if (function_scope)
23588 pop_function_context ();
23589 cp_parser_pop_lexer (parser);
23592 /* Remove any template parameters from the symbol table. */
23593 maybe_end_member_template_processing ();
23595 /* Restore the queue. */
23596 pop_unparsed_function_queues (parser);
23597 timevar_pop (TV_PARSE_INMETH);
23600 /* If DECL contains any default args, remember it on the unparsed
23601 functions queue. */
23603 static void
23604 cp_parser_save_default_args (cp_parser* parser, tree decl)
23606 tree probe;
23608 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23609 probe;
23610 probe = TREE_CHAIN (probe))
23611 if (TREE_PURPOSE (probe))
23613 cp_default_arg_entry entry = {current_class_type, decl};
23614 vec_safe_push (unparsed_funs_with_default_args, entry);
23615 break;
23619 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23620 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23621 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23622 from the parameter-type-list. */
23624 static tree
23625 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23626 tree default_arg, tree parmtype)
23628 cp_token_cache *tokens;
23629 tree parsed_arg;
23630 bool dummy;
23632 if (default_arg == error_mark_node)
23633 return error_mark_node;
23635 /* Push the saved tokens for the default argument onto the parser's
23636 lexer stack. */
23637 tokens = DEFARG_TOKENS (default_arg);
23638 cp_parser_push_lexer_for_tokens (parser, tokens);
23640 start_lambda_scope (decl);
23642 /* Parse the default argument. */
23643 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23644 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23647 finish_lambda_scope ();
23649 if (parsed_arg == error_mark_node)
23650 cp_parser_skip_to_end_of_statement (parser);
23652 if (!processing_template_decl)
23654 /* In a non-template class, check conversions now. In a template,
23655 we'll wait and instantiate these as needed. */
23656 if (TREE_CODE (decl) == PARM_DECL)
23657 parsed_arg = check_default_argument (parmtype, parsed_arg,
23658 tf_warning_or_error);
23659 else
23661 int flags = LOOKUP_IMPLICIT;
23662 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23663 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23664 flags = LOOKUP_NORMAL;
23665 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23666 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23667 /* This represents the whole initialization. */
23668 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23672 /* If the token stream has not been completely used up, then
23673 there was extra junk after the end of the default
23674 argument. */
23675 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23677 if (TREE_CODE (decl) == PARM_DECL)
23678 cp_parser_error (parser, "expected %<,%>");
23679 else
23680 cp_parser_error (parser, "expected %<;%>");
23683 /* Revert to the main lexer. */
23684 cp_parser_pop_lexer (parser);
23686 return parsed_arg;
23689 /* FIELD is a non-static data member with an initializer which we saved for
23690 later; parse it now. */
23692 static void
23693 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23695 tree def;
23697 maybe_begin_member_template_processing (field);
23699 push_unparsed_function_queues (parser);
23700 def = cp_parser_late_parse_one_default_arg (parser, field,
23701 DECL_INITIAL (field),
23702 NULL_TREE);
23703 pop_unparsed_function_queues (parser);
23705 maybe_end_member_template_processing ();
23707 DECL_INITIAL (field) = def;
23710 /* FN is a FUNCTION_DECL which may contains a parameter with an
23711 unparsed DEFAULT_ARG. Parse the default args now. This function
23712 assumes that the current scope is the scope in which the default
23713 argument should be processed. */
23715 static void
23716 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23718 bool saved_local_variables_forbidden_p;
23719 tree parm, parmdecl;
23721 /* While we're parsing the default args, we might (due to the
23722 statement expression extension) encounter more classes. We want
23723 to handle them right away, but we don't want them getting mixed
23724 up with default args that are currently in the queue. */
23725 push_unparsed_function_queues (parser);
23727 /* Local variable names (and the `this' keyword) may not appear
23728 in a default argument. */
23729 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23730 parser->local_variables_forbidden_p = true;
23732 push_defarg_context (fn);
23734 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23735 parmdecl = DECL_ARGUMENTS (fn);
23736 parm && parm != void_list_node;
23737 parm = TREE_CHAIN (parm),
23738 parmdecl = DECL_CHAIN (parmdecl))
23740 tree default_arg = TREE_PURPOSE (parm);
23741 tree parsed_arg;
23742 vec<tree, va_gc> *insts;
23743 tree copy;
23744 unsigned ix;
23746 if (!default_arg)
23747 continue;
23749 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23750 /* This can happen for a friend declaration for a function
23751 already declared with default arguments. */
23752 continue;
23754 parsed_arg
23755 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23756 default_arg,
23757 TREE_VALUE (parm));
23758 if (parsed_arg == error_mark_node)
23760 continue;
23763 TREE_PURPOSE (parm) = parsed_arg;
23765 /* Update any instantiations we've already created. */
23766 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23767 vec_safe_iterate (insts, ix, &copy); ix++)
23768 TREE_PURPOSE (copy) = parsed_arg;
23771 pop_defarg_context ();
23773 /* Make sure no default arg is missing. */
23774 check_default_args (fn);
23776 /* Restore the state of local_variables_forbidden_p. */
23777 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23779 /* Restore the queue. */
23780 pop_unparsed_function_queues (parser);
23783 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23785 sizeof ... ( identifier )
23787 where the 'sizeof' token has already been consumed. */
23789 static tree
23790 cp_parser_sizeof_pack (cp_parser *parser)
23792 /* Consume the `...'. */
23793 cp_lexer_consume_token (parser->lexer);
23794 maybe_warn_variadic_templates ();
23796 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23797 if (paren)
23798 cp_lexer_consume_token (parser->lexer);
23799 else
23800 permerror (cp_lexer_peek_token (parser->lexer)->location,
23801 "%<sizeof...%> argument must be surrounded by parentheses");
23803 cp_token *token = cp_lexer_peek_token (parser->lexer);
23804 tree name = cp_parser_identifier (parser);
23805 if (name == error_mark_node)
23806 return error_mark_node;
23807 /* The name is not qualified. */
23808 parser->scope = NULL_TREE;
23809 parser->qualifying_scope = NULL_TREE;
23810 parser->object_scope = NULL_TREE;
23811 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23812 if (expr == error_mark_node)
23813 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23814 token->location);
23815 if (TREE_CODE (expr) == TYPE_DECL)
23816 expr = TREE_TYPE (expr);
23817 else if (TREE_CODE (expr) == CONST_DECL)
23818 expr = DECL_INITIAL (expr);
23819 expr = make_pack_expansion (expr);
23821 if (paren)
23822 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23824 return expr;
23827 /* Parse the operand of `sizeof' (or a similar operator). Returns
23828 either a TYPE or an expression, depending on the form of the
23829 input. The KEYWORD indicates which kind of expression we have
23830 encountered. */
23832 static tree
23833 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23835 tree expr = NULL_TREE;
23836 const char *saved_message;
23837 char *tmp;
23838 bool saved_integral_constant_expression_p;
23839 bool saved_non_integral_constant_expression_p;
23841 /* If it's a `...', then we are computing the length of a parameter
23842 pack. */
23843 if (keyword == RID_SIZEOF
23844 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23845 return cp_parser_sizeof_pack (parser);
23847 /* Types cannot be defined in a `sizeof' expression. Save away the
23848 old message. */
23849 saved_message = parser->type_definition_forbidden_message;
23850 /* And create the new one. */
23851 tmp = concat ("types may not be defined in %<",
23852 IDENTIFIER_POINTER (ridpointers[keyword]),
23853 "%> expressions", NULL);
23854 parser->type_definition_forbidden_message = tmp;
23856 /* The restrictions on constant-expressions do not apply inside
23857 sizeof expressions. */
23858 saved_integral_constant_expression_p
23859 = parser->integral_constant_expression_p;
23860 saved_non_integral_constant_expression_p
23861 = parser->non_integral_constant_expression_p;
23862 parser->integral_constant_expression_p = false;
23864 /* Do not actually evaluate the expression. */
23865 ++cp_unevaluated_operand;
23866 ++c_inhibit_evaluation_warnings;
23867 /* If it's a `(', then we might be looking at the type-id
23868 construction. */
23869 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23871 tree type = NULL_TREE;
23872 bool compound_literal_p;
23874 /* We can't be sure yet whether we're looking at a type-id or an
23875 expression. */
23876 cp_parser_parse_tentatively (parser);
23877 /* Consume the `('. */
23878 cp_lexer_consume_token (parser->lexer);
23879 /* Note: as a GNU Extension, compound literals are considered
23880 postfix-expressions as they are in C99, so they are valid
23881 arguments to sizeof. See comment in cp_parser_cast_expression
23882 for details. */
23883 cp_lexer_save_tokens (parser->lexer);
23884 /* Skip tokens until the next token is a closing parenthesis.
23885 If we find the closing `)', and the next token is a `{', then
23886 we are looking at a compound-literal. */
23887 compound_literal_p
23888 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23889 /*consume_paren=*/true)
23890 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23891 /* Roll back the tokens we skipped. */
23892 cp_lexer_rollback_tokens (parser->lexer);
23893 /* If we were looking at a compound-literal, simulate an error
23894 so that the call to cp_parser_parse_definitely below will
23895 fail. */
23896 if (compound_literal_p)
23897 cp_parser_simulate_error (parser);
23898 else
23900 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23901 parser->in_type_id_in_expr_p = true;
23902 /* Look for the type-id. */
23903 type = cp_parser_type_id (parser);
23904 /* Look for the closing `)'. */
23905 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23906 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23909 /* If all went well, then we're done. */
23910 if (cp_parser_parse_definitely (parser))
23912 cp_decl_specifier_seq decl_specs;
23914 /* Build a trivial decl-specifier-seq. */
23915 clear_decl_specs (&decl_specs);
23916 decl_specs.type = type;
23918 /* Call grokdeclarator to figure out what type this is. */
23919 expr = grokdeclarator (NULL,
23920 &decl_specs,
23921 TYPENAME,
23922 /*initialized=*/0,
23923 /*attrlist=*/NULL);
23927 /* If the type-id production did not work out, then we must be
23928 looking at the unary-expression production. */
23929 if (!expr)
23930 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23931 /*cast_p=*/false, NULL);
23933 /* Go back to evaluating expressions. */
23934 --cp_unevaluated_operand;
23935 --c_inhibit_evaluation_warnings;
23937 /* Free the message we created. */
23938 free (tmp);
23939 /* And restore the old one. */
23940 parser->type_definition_forbidden_message = saved_message;
23941 parser->integral_constant_expression_p
23942 = saved_integral_constant_expression_p;
23943 parser->non_integral_constant_expression_p
23944 = saved_non_integral_constant_expression_p;
23946 return expr;
23949 /* If the current declaration has no declarator, return true. */
23951 static bool
23952 cp_parser_declares_only_class_p (cp_parser *parser)
23954 /* If the next token is a `;' or a `,' then there is no
23955 declarator. */
23956 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23957 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23960 /* Update the DECL_SPECS to reflect the storage class indicated by
23961 KEYWORD. */
23963 static void
23964 cp_parser_set_storage_class (cp_parser *parser,
23965 cp_decl_specifier_seq *decl_specs,
23966 enum rid keyword,
23967 cp_token *token)
23969 cp_storage_class storage_class;
23971 if (parser->in_unbraced_linkage_specification_p)
23973 error_at (token->location, "invalid use of %qD in linkage specification",
23974 ridpointers[keyword]);
23975 return;
23977 else if (decl_specs->storage_class != sc_none)
23979 decl_specs->conflicting_specifiers_p = true;
23980 return;
23983 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23984 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23985 && decl_specs->gnu_thread_keyword_p)
23987 pedwarn (decl_specs->locations[ds_thread], 0,
23988 "%<__thread%> before %qD", ridpointers[keyword]);
23991 switch (keyword)
23993 case RID_AUTO:
23994 storage_class = sc_auto;
23995 break;
23996 case RID_REGISTER:
23997 storage_class = sc_register;
23998 break;
23999 case RID_STATIC:
24000 storage_class = sc_static;
24001 break;
24002 case RID_EXTERN:
24003 storage_class = sc_extern;
24004 break;
24005 case RID_MUTABLE:
24006 storage_class = sc_mutable;
24007 break;
24008 default:
24009 gcc_unreachable ();
24011 decl_specs->storage_class = storage_class;
24012 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24014 /* A storage class specifier cannot be applied alongside a typedef
24015 specifier. If there is a typedef specifier present then set
24016 conflicting_specifiers_p which will trigger an error later
24017 on in grokdeclarator. */
24018 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24019 decl_specs->conflicting_specifiers_p = true;
24022 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24023 is true, the type is a class or enum definition. */
24025 static void
24026 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24027 tree type_spec,
24028 cp_token *token,
24029 bool type_definition_p)
24031 decl_specs->any_specifiers_p = true;
24033 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24034 (with, for example, in "typedef int wchar_t;") we remember that
24035 this is what happened. In system headers, we ignore these
24036 declarations so that G++ can work with system headers that are not
24037 C++-safe. */
24038 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24039 && !type_definition_p
24040 && (type_spec == boolean_type_node
24041 || type_spec == char16_type_node
24042 || type_spec == char32_type_node
24043 || type_spec == wchar_type_node)
24044 && (decl_specs->type
24045 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24046 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24047 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24048 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24050 decl_specs->redefined_builtin_type = type_spec;
24051 set_and_check_decl_spec_loc (decl_specs,
24052 ds_redefined_builtin_type_spec,
24053 token);
24054 if (!decl_specs->type)
24056 decl_specs->type = type_spec;
24057 decl_specs->type_definition_p = false;
24058 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24061 else if (decl_specs->type)
24062 decl_specs->multiple_types_p = true;
24063 else
24065 decl_specs->type = type_spec;
24066 decl_specs->type_definition_p = type_definition_p;
24067 decl_specs->redefined_builtin_type = NULL_TREE;
24068 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24072 /* True iff TOKEN is the GNU keyword __thread. */
24074 static bool
24075 token_is__thread (cp_token *token)
24077 gcc_assert (token->keyword == RID_THREAD);
24078 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24081 /* Set the location for a declarator specifier and check if it is
24082 duplicated.
24084 DECL_SPECS is the sequence of declarator specifiers onto which to
24085 set the location.
24087 DS is the single declarator specifier to set which location is to
24088 be set onto the existing sequence of declarators.
24090 LOCATION is the location for the declarator specifier to
24091 consider. */
24093 static void
24094 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24095 cp_decl_spec ds, cp_token *token)
24097 gcc_assert (ds < ds_last);
24099 if (decl_specs == NULL)
24100 return;
24102 source_location location = token->location;
24104 if (decl_specs->locations[ds] == 0)
24106 decl_specs->locations[ds] = location;
24107 if (ds == ds_thread)
24108 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24110 else
24112 if (ds == ds_long)
24114 if (decl_specs->locations[ds_long_long] != 0)
24115 error_at (location,
24116 "%<long long long%> is too long for GCC");
24117 else
24119 decl_specs->locations[ds_long_long] = location;
24120 pedwarn_cxx98 (location,
24121 OPT_Wlong_long,
24122 "ISO C++ 1998 does not support %<long long%>");
24125 else if (ds == ds_thread)
24127 bool gnu = token_is__thread (token);
24128 if (gnu != decl_specs->gnu_thread_keyword_p)
24129 error_at (location,
24130 "both %<__thread%> and %<thread_local%> specified");
24131 else
24132 error_at (location, "duplicate %qD", token->u.value);
24134 else
24136 static const char *const decl_spec_names[] = {
24137 "signed",
24138 "unsigned",
24139 "short",
24140 "long",
24141 "const",
24142 "volatile",
24143 "restrict",
24144 "inline",
24145 "virtual",
24146 "explicit",
24147 "friend",
24148 "typedef",
24149 "using",
24150 "constexpr",
24151 "__complex"
24153 error_at (location,
24154 "duplicate %qs", decl_spec_names[ds]);
24159 /* Return true iff the declarator specifier DS is present in the
24160 sequence of declarator specifiers DECL_SPECS. */
24162 bool
24163 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24164 cp_decl_spec ds)
24166 gcc_assert (ds < ds_last);
24168 if (decl_specs == NULL)
24169 return false;
24171 return decl_specs->locations[ds] != 0;
24174 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24175 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24177 static bool
24178 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24180 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24183 /* Issue an error message indicating that TOKEN_DESC was expected.
24184 If KEYWORD is true, it indicated this function is called by
24185 cp_parser_require_keword and the required token can only be
24186 a indicated keyword. */
24188 static void
24189 cp_parser_required_error (cp_parser *parser,
24190 required_token token_desc,
24191 bool keyword)
24193 switch (token_desc)
24195 case RT_NEW:
24196 cp_parser_error (parser, "expected %<new%>");
24197 return;
24198 case RT_DELETE:
24199 cp_parser_error (parser, "expected %<delete%>");
24200 return;
24201 case RT_RETURN:
24202 cp_parser_error (parser, "expected %<return%>");
24203 return;
24204 case RT_WHILE:
24205 cp_parser_error (parser, "expected %<while%>");
24206 return;
24207 case RT_EXTERN:
24208 cp_parser_error (parser, "expected %<extern%>");
24209 return;
24210 case RT_STATIC_ASSERT:
24211 cp_parser_error (parser, "expected %<static_assert%>");
24212 return;
24213 case RT_DECLTYPE:
24214 cp_parser_error (parser, "expected %<decltype%>");
24215 return;
24216 case RT_OPERATOR:
24217 cp_parser_error (parser, "expected %<operator%>");
24218 return;
24219 case RT_CLASS:
24220 cp_parser_error (parser, "expected %<class%>");
24221 return;
24222 case RT_TEMPLATE:
24223 cp_parser_error (parser, "expected %<template%>");
24224 return;
24225 case RT_NAMESPACE:
24226 cp_parser_error (parser, "expected %<namespace%>");
24227 return;
24228 case RT_USING:
24229 cp_parser_error (parser, "expected %<using%>");
24230 return;
24231 case RT_ASM:
24232 cp_parser_error (parser, "expected %<asm%>");
24233 return;
24234 case RT_TRY:
24235 cp_parser_error (parser, "expected %<try%>");
24236 return;
24237 case RT_CATCH:
24238 cp_parser_error (parser, "expected %<catch%>");
24239 return;
24240 case RT_THROW:
24241 cp_parser_error (parser, "expected %<throw%>");
24242 return;
24243 case RT_LABEL:
24244 cp_parser_error (parser, "expected %<__label__%>");
24245 return;
24246 case RT_AT_TRY:
24247 cp_parser_error (parser, "expected %<@try%>");
24248 return;
24249 case RT_AT_SYNCHRONIZED:
24250 cp_parser_error (parser, "expected %<@synchronized%>");
24251 return;
24252 case RT_AT_THROW:
24253 cp_parser_error (parser, "expected %<@throw%>");
24254 return;
24255 case RT_TRANSACTION_ATOMIC:
24256 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24257 return;
24258 case RT_TRANSACTION_RELAXED:
24259 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24260 return;
24261 default:
24262 break;
24264 if (!keyword)
24266 switch (token_desc)
24268 case RT_SEMICOLON:
24269 cp_parser_error (parser, "expected %<;%>");
24270 return;
24271 case RT_OPEN_PAREN:
24272 cp_parser_error (parser, "expected %<(%>");
24273 return;
24274 case RT_CLOSE_BRACE:
24275 cp_parser_error (parser, "expected %<}%>");
24276 return;
24277 case RT_OPEN_BRACE:
24278 cp_parser_error (parser, "expected %<{%>");
24279 return;
24280 case RT_CLOSE_SQUARE:
24281 cp_parser_error (parser, "expected %<]%>");
24282 return;
24283 case RT_OPEN_SQUARE:
24284 cp_parser_error (parser, "expected %<[%>");
24285 return;
24286 case RT_COMMA:
24287 cp_parser_error (parser, "expected %<,%>");
24288 return;
24289 case RT_SCOPE:
24290 cp_parser_error (parser, "expected %<::%>");
24291 return;
24292 case RT_LESS:
24293 cp_parser_error (parser, "expected %<<%>");
24294 return;
24295 case RT_GREATER:
24296 cp_parser_error (parser, "expected %<>%>");
24297 return;
24298 case RT_EQ:
24299 cp_parser_error (parser, "expected %<=%>");
24300 return;
24301 case RT_ELLIPSIS:
24302 cp_parser_error (parser, "expected %<...%>");
24303 return;
24304 case RT_MULT:
24305 cp_parser_error (parser, "expected %<*%>");
24306 return;
24307 case RT_COMPL:
24308 cp_parser_error (parser, "expected %<~%>");
24309 return;
24310 case RT_COLON:
24311 cp_parser_error (parser, "expected %<:%>");
24312 return;
24313 case RT_COLON_SCOPE:
24314 cp_parser_error (parser, "expected %<:%> or %<::%>");
24315 return;
24316 case RT_CLOSE_PAREN:
24317 cp_parser_error (parser, "expected %<)%>");
24318 return;
24319 case RT_COMMA_CLOSE_PAREN:
24320 cp_parser_error (parser, "expected %<,%> or %<)%>");
24321 return;
24322 case RT_PRAGMA_EOL:
24323 cp_parser_error (parser, "expected end of line");
24324 return;
24325 case RT_NAME:
24326 cp_parser_error (parser, "expected identifier");
24327 return;
24328 case RT_SELECT:
24329 cp_parser_error (parser, "expected selection-statement");
24330 return;
24331 case RT_INTERATION:
24332 cp_parser_error (parser, "expected iteration-statement");
24333 return;
24334 case RT_JUMP:
24335 cp_parser_error (parser, "expected jump-statement");
24336 return;
24337 case RT_CLASS_KEY:
24338 cp_parser_error (parser, "expected class-key");
24339 return;
24340 case RT_CLASS_TYPENAME_TEMPLATE:
24341 cp_parser_error (parser,
24342 "expected %<class%>, %<typename%>, or %<template%>");
24343 return;
24344 default:
24345 gcc_unreachable ();
24348 else
24349 gcc_unreachable ();
24354 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24355 issue an error message indicating that TOKEN_DESC was expected.
24357 Returns the token consumed, if the token had the appropriate type.
24358 Otherwise, returns NULL. */
24360 static cp_token *
24361 cp_parser_require (cp_parser* parser,
24362 enum cpp_ttype type,
24363 required_token token_desc)
24365 if (cp_lexer_next_token_is (parser->lexer, type))
24366 return cp_lexer_consume_token (parser->lexer);
24367 else
24369 /* Output the MESSAGE -- unless we're parsing tentatively. */
24370 if (!cp_parser_simulate_error (parser))
24371 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24372 return NULL;
24376 /* An error message is produced if the next token is not '>'.
24377 All further tokens are skipped until the desired token is
24378 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24380 static void
24381 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24383 /* Current level of '< ... >'. */
24384 unsigned level = 0;
24385 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24386 unsigned nesting_depth = 0;
24388 /* Are we ready, yet? If not, issue error message. */
24389 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24390 return;
24392 /* Skip tokens until the desired token is found. */
24393 while (true)
24395 /* Peek at the next token. */
24396 switch (cp_lexer_peek_token (parser->lexer)->type)
24398 case CPP_LESS:
24399 if (!nesting_depth)
24400 ++level;
24401 break;
24403 case CPP_RSHIFT:
24404 if (cxx_dialect == cxx98)
24405 /* C++0x views the `>>' operator as two `>' tokens, but
24406 C++98 does not. */
24407 break;
24408 else if (!nesting_depth && level-- == 0)
24410 /* We've hit a `>>' where the first `>' closes the
24411 template argument list, and the second `>' is
24412 spurious. Just consume the `>>' and stop; we've
24413 already produced at least one error. */
24414 cp_lexer_consume_token (parser->lexer);
24415 return;
24417 /* Fall through for C++0x, so we handle the second `>' in
24418 the `>>'. */
24420 case CPP_GREATER:
24421 if (!nesting_depth && level-- == 0)
24423 /* We've reached the token we want, consume it and stop. */
24424 cp_lexer_consume_token (parser->lexer);
24425 return;
24427 break;
24429 case CPP_OPEN_PAREN:
24430 case CPP_OPEN_SQUARE:
24431 ++nesting_depth;
24432 break;
24434 case CPP_CLOSE_PAREN:
24435 case CPP_CLOSE_SQUARE:
24436 if (nesting_depth-- == 0)
24437 return;
24438 break;
24440 case CPP_EOF:
24441 case CPP_PRAGMA_EOL:
24442 case CPP_SEMICOLON:
24443 case CPP_OPEN_BRACE:
24444 case CPP_CLOSE_BRACE:
24445 /* The '>' was probably forgotten, don't look further. */
24446 return;
24448 default:
24449 break;
24452 /* Consume this token. */
24453 cp_lexer_consume_token (parser->lexer);
24457 /* If the next token is the indicated keyword, consume it. Otherwise,
24458 issue an error message indicating that TOKEN_DESC was expected.
24460 Returns the token consumed, if the token had the appropriate type.
24461 Otherwise, returns NULL. */
24463 static cp_token *
24464 cp_parser_require_keyword (cp_parser* parser,
24465 enum rid keyword,
24466 required_token token_desc)
24468 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24470 if (token && token->keyword != keyword)
24472 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24473 return NULL;
24476 return token;
24479 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24480 function-definition. */
24482 static bool
24483 cp_parser_token_starts_function_definition_p (cp_token* token)
24485 return (/* An ordinary function-body begins with an `{'. */
24486 token->type == CPP_OPEN_BRACE
24487 /* A ctor-initializer begins with a `:'. */
24488 || token->type == CPP_COLON
24489 /* A function-try-block begins with `try'. */
24490 || token->keyword == RID_TRY
24491 /* A function-transaction-block begins with `__transaction_atomic'
24492 or `__transaction_relaxed'. */
24493 || token->keyword == RID_TRANSACTION_ATOMIC
24494 || token->keyword == RID_TRANSACTION_RELAXED
24495 /* The named return value extension begins with `return'. */
24496 || token->keyword == RID_RETURN);
24499 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24500 definition. */
24502 static bool
24503 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24505 cp_token *token;
24507 token = cp_lexer_peek_token (parser->lexer);
24508 return (token->type == CPP_OPEN_BRACE
24509 || (token->type == CPP_COLON
24510 && !parser->colon_doesnt_start_class_def_p));
24513 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24514 C++0x) ending a template-argument. */
24516 static bool
24517 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24519 cp_token *token;
24521 token = cp_lexer_peek_token (parser->lexer);
24522 return (token->type == CPP_COMMA
24523 || token->type == CPP_GREATER
24524 || token->type == CPP_ELLIPSIS
24525 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24528 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24529 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24531 static bool
24532 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24533 size_t n)
24535 cp_token *token;
24537 token = cp_lexer_peek_nth_token (parser->lexer, n);
24538 if (token->type == CPP_LESS)
24539 return true;
24540 /* Check for the sequence `<::' in the original code. It would be lexed as
24541 `[:', where `[' is a digraph, and there is no whitespace before
24542 `:'. */
24543 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24545 cp_token *token2;
24546 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24547 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24548 return true;
24550 return false;
24553 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24554 or none_type otherwise. */
24556 static enum tag_types
24557 cp_parser_token_is_class_key (cp_token* token)
24559 switch (token->keyword)
24561 case RID_CLASS:
24562 return class_type;
24563 case RID_STRUCT:
24564 return record_type;
24565 case RID_UNION:
24566 return union_type;
24568 default:
24569 return none_type;
24573 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24575 static void
24576 cp_parser_check_class_key (enum tag_types class_key, tree type)
24578 if (type == error_mark_node)
24579 return;
24580 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24582 if (permerror (input_location, "%qs tag used in naming %q#T",
24583 class_key == union_type ? "union"
24584 : class_key == record_type ? "struct" : "class",
24585 type))
24586 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24587 "%q#T was previously declared here", type);
24591 /* Issue an error message if DECL is redeclared with different
24592 access than its original declaration [class.access.spec/3].
24593 This applies to nested classes and nested class templates.
24594 [class.mem/1]. */
24596 static void
24597 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24599 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24600 return;
24602 if ((TREE_PRIVATE (decl)
24603 != (current_access_specifier == access_private_node))
24604 || (TREE_PROTECTED (decl)
24605 != (current_access_specifier == access_protected_node)))
24606 error_at (location, "%qD redeclared with different access", decl);
24609 /* Look for the `template' keyword, as a syntactic disambiguator.
24610 Return TRUE iff it is present, in which case it will be
24611 consumed. */
24613 static bool
24614 cp_parser_optional_template_keyword (cp_parser *parser)
24616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24618 /* In C++98 the `template' keyword can only be used within templates;
24619 outside templates the parser can always figure out what is a
24620 template and what is not. In C++11, per the resolution of DR 468,
24621 `template' is allowed in cases where it is not strictly necessary. */
24622 if (!processing_template_decl
24623 && pedantic && cxx_dialect == cxx98)
24625 cp_token *token = cp_lexer_peek_token (parser->lexer);
24626 pedwarn (token->location, OPT_Wpedantic,
24627 "in C++98 %<template%> (as a disambiguator) is only "
24628 "allowed within templates");
24629 /* If this part of the token stream is rescanned, the same
24630 error message would be generated. So, we purge the token
24631 from the stream. */
24632 cp_lexer_purge_token (parser->lexer);
24633 return false;
24635 else
24637 /* Consume the `template' keyword. */
24638 cp_lexer_consume_token (parser->lexer);
24639 return true;
24642 return false;
24645 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24646 set PARSER->SCOPE, and perform other related actions. */
24648 static void
24649 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24651 int i;
24652 struct tree_check *check_value;
24653 deferred_access_check *chk;
24654 vec<deferred_access_check, va_gc> *checks;
24656 /* Get the stored value. */
24657 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24658 /* Perform any access checks that were deferred. */
24659 checks = check_value->checks;
24660 if (checks)
24662 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24663 perform_or_defer_access_check (chk->binfo,
24664 chk->decl,
24665 chk->diag_decl, tf_warning_or_error);
24667 /* Set the scope from the stored value. */
24668 parser->scope = check_value->value;
24669 parser->qualifying_scope = check_value->qualifying_scope;
24670 parser->object_scope = NULL_TREE;
24673 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24674 encounter the end of a block before what we were looking for. */
24676 static bool
24677 cp_parser_cache_group (cp_parser *parser,
24678 enum cpp_ttype end,
24679 unsigned depth)
24681 while (true)
24683 cp_token *token = cp_lexer_peek_token (parser->lexer);
24685 /* Abort a parenthesized expression if we encounter a semicolon. */
24686 if ((end == CPP_CLOSE_PAREN || depth == 0)
24687 && token->type == CPP_SEMICOLON)
24688 return true;
24689 /* If we've reached the end of the file, stop. */
24690 if (token->type == CPP_EOF
24691 || (end != CPP_PRAGMA_EOL
24692 && token->type == CPP_PRAGMA_EOL))
24693 return true;
24694 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24695 /* We've hit the end of an enclosing block, so there's been some
24696 kind of syntax error. */
24697 return true;
24699 /* Consume the token. */
24700 cp_lexer_consume_token (parser->lexer);
24701 /* See if it starts a new group. */
24702 if (token->type == CPP_OPEN_BRACE)
24704 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24705 /* In theory this should probably check end == '}', but
24706 cp_parser_save_member_function_body needs it to exit
24707 after either '}' or ')' when called with ')'. */
24708 if (depth == 0)
24709 return false;
24711 else if (token->type == CPP_OPEN_PAREN)
24713 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24714 if (depth == 0 && end == CPP_CLOSE_PAREN)
24715 return false;
24717 else if (token->type == CPP_PRAGMA)
24718 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24719 else if (token->type == end)
24720 return false;
24724 /* Like above, for caching a default argument or NSDMI. Both of these are
24725 terminated by a non-nested comma, but it can be unclear whether or not a
24726 comma is nested in a template argument list unless we do more parsing.
24727 In order to handle this ambiguity, when we encounter a ',' after a '<'
24728 we try to parse what follows as a parameter-declaration-list (in the
24729 case of a default argument) or a member-declarator (in the case of an
24730 NSDMI). If that succeeds, then we stop caching. */
24732 static tree
24733 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24735 unsigned depth = 0;
24736 int maybe_template_id = 0;
24737 cp_token *first_token;
24738 cp_token *token;
24739 tree default_argument;
24741 /* Add tokens until we have processed the entire default
24742 argument. We add the range [first_token, token). */
24743 first_token = cp_lexer_peek_token (parser->lexer);
24744 if (first_token->type == CPP_OPEN_BRACE)
24746 /* For list-initialization, this is straightforward. */
24747 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24748 token = cp_lexer_peek_token (parser->lexer);
24750 else while (true)
24752 bool done = false;
24754 /* Peek at the next token. */
24755 token = cp_lexer_peek_token (parser->lexer);
24756 /* What we do depends on what token we have. */
24757 switch (token->type)
24759 /* In valid code, a default argument must be
24760 immediately followed by a `,' `)', or `...'. */
24761 case CPP_COMMA:
24762 if (depth == 0 && maybe_template_id)
24764 /* If we've seen a '<', we might be in a
24765 template-argument-list. Until Core issue 325 is
24766 resolved, we don't know how this situation ought
24767 to be handled, so try to DTRT. We check whether
24768 what comes after the comma is a valid parameter
24769 declaration list. If it is, then the comma ends
24770 the default argument; otherwise the default
24771 argument continues. */
24772 bool error = false;
24774 /* Set ITALP so cp_parser_parameter_declaration_list
24775 doesn't decide to commit to this parse. */
24776 bool saved_italp = parser->in_template_argument_list_p;
24777 parser->in_template_argument_list_p = true;
24779 cp_parser_parse_tentatively (parser);
24780 cp_lexer_consume_token (parser->lexer);
24782 if (nsdmi)
24784 int ctor_dtor_or_conv_p;
24785 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24786 &ctor_dtor_or_conv_p,
24787 /*parenthesized_p=*/NULL,
24788 /*member_p=*/true);
24790 else
24792 begin_scope (sk_function_parms, NULL_TREE);
24793 cp_parser_parameter_declaration_list (parser, &error);
24794 pop_bindings_and_leave_scope ();
24796 if (!cp_parser_error_occurred (parser) && !error)
24797 done = true;
24798 cp_parser_abort_tentative_parse (parser);
24800 parser->in_template_argument_list_p = saved_italp;
24801 break;
24803 case CPP_CLOSE_PAREN:
24804 case CPP_ELLIPSIS:
24805 /* If we run into a non-nested `;', `}', or `]',
24806 then the code is invalid -- but the default
24807 argument is certainly over. */
24808 case CPP_SEMICOLON:
24809 case CPP_CLOSE_BRACE:
24810 case CPP_CLOSE_SQUARE:
24811 if (depth == 0
24812 /* Handle correctly int n = sizeof ... ( p ); */
24813 && token->type != CPP_ELLIPSIS)
24814 done = true;
24815 /* Update DEPTH, if necessary. */
24816 else if (token->type == CPP_CLOSE_PAREN
24817 || token->type == CPP_CLOSE_BRACE
24818 || token->type == CPP_CLOSE_SQUARE)
24819 --depth;
24820 break;
24822 case CPP_OPEN_PAREN:
24823 case CPP_OPEN_SQUARE:
24824 case CPP_OPEN_BRACE:
24825 ++depth;
24826 break;
24828 case CPP_LESS:
24829 if (depth == 0)
24830 /* This might be the comparison operator, or it might
24831 start a template argument list. */
24832 ++maybe_template_id;
24833 break;
24835 case CPP_RSHIFT:
24836 if (cxx_dialect == cxx98)
24837 break;
24838 /* Fall through for C++0x, which treats the `>>'
24839 operator like two `>' tokens in certain
24840 cases. */
24842 case CPP_GREATER:
24843 if (depth == 0)
24845 /* This might be an operator, or it might close a
24846 template argument list. But if a previous '<'
24847 started a template argument list, this will have
24848 closed it, so we can't be in one anymore. */
24849 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24850 if (maybe_template_id < 0)
24851 maybe_template_id = 0;
24853 break;
24855 /* If we run out of tokens, issue an error message. */
24856 case CPP_EOF:
24857 case CPP_PRAGMA_EOL:
24858 error_at (token->location, "file ends in default argument");
24859 done = true;
24860 break;
24862 case CPP_NAME:
24863 case CPP_SCOPE:
24864 /* In these cases, we should look for template-ids.
24865 For example, if the default argument is
24866 `X<int, double>()', we need to do name lookup to
24867 figure out whether or not `X' is a template; if
24868 so, the `,' does not end the default argument.
24870 That is not yet done. */
24871 break;
24873 default:
24874 break;
24877 /* If we've reached the end, stop. */
24878 if (done)
24879 break;
24881 /* Add the token to the token block. */
24882 token = cp_lexer_consume_token (parser->lexer);
24885 /* Create a DEFAULT_ARG to represent the unparsed default
24886 argument. */
24887 default_argument = make_node (DEFAULT_ARG);
24888 DEFARG_TOKENS (default_argument)
24889 = cp_token_cache_new (first_token, token);
24890 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24892 return default_argument;
24895 /* Begin parsing tentatively. We always save tokens while parsing
24896 tentatively so that if the tentative parsing fails we can restore the
24897 tokens. */
24899 static void
24900 cp_parser_parse_tentatively (cp_parser* parser)
24902 /* Enter a new parsing context. */
24903 parser->context = cp_parser_context_new (parser->context);
24904 /* Begin saving tokens. */
24905 cp_lexer_save_tokens (parser->lexer);
24906 /* In order to avoid repetitive access control error messages,
24907 access checks are queued up until we are no longer parsing
24908 tentatively. */
24909 push_deferring_access_checks (dk_deferred);
24912 /* Commit to the currently active tentative parse. */
24914 static void
24915 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24917 cp_parser_context *context;
24918 cp_lexer *lexer;
24920 /* Mark all of the levels as committed. */
24921 lexer = parser->lexer;
24922 for (context = parser->context; context->next; context = context->next)
24924 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24925 break;
24926 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24927 while (!cp_lexer_saving_tokens (lexer))
24928 lexer = lexer->next;
24929 cp_lexer_commit_tokens (lexer);
24933 /* Commit to the topmost currently active tentative parse.
24935 Note that this function shouldn't be called when there are
24936 irreversible side-effects while in a tentative state. For
24937 example, we shouldn't create a permanent entry in the symbol
24938 table, or issue an error message that might not apply if the
24939 tentative parse is aborted. */
24941 static void
24942 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24944 cp_parser_context *context = parser->context;
24945 cp_lexer *lexer = parser->lexer;
24947 if (context)
24949 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24950 return;
24951 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24953 while (!cp_lexer_saving_tokens (lexer))
24954 lexer = lexer->next;
24955 cp_lexer_commit_tokens (lexer);
24959 /* Abort the currently active tentative parse. All consumed tokens
24960 will be rolled back, and no diagnostics will be issued. */
24962 static void
24963 cp_parser_abort_tentative_parse (cp_parser* parser)
24965 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24966 || errorcount > 0);
24967 cp_parser_simulate_error (parser);
24968 /* Now, pretend that we want to see if the construct was
24969 successfully parsed. */
24970 cp_parser_parse_definitely (parser);
24973 /* Stop parsing tentatively. If a parse error has occurred, restore the
24974 token stream. Otherwise, commit to the tokens we have consumed.
24975 Returns true if no error occurred; false otherwise. */
24977 static bool
24978 cp_parser_parse_definitely (cp_parser* parser)
24980 bool error_occurred;
24981 cp_parser_context *context;
24983 /* Remember whether or not an error occurred, since we are about to
24984 destroy that information. */
24985 error_occurred = cp_parser_error_occurred (parser);
24986 /* Remove the topmost context from the stack. */
24987 context = parser->context;
24988 parser->context = context->next;
24989 /* If no parse errors occurred, commit to the tentative parse. */
24990 if (!error_occurred)
24992 /* Commit to the tokens read tentatively, unless that was
24993 already done. */
24994 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24995 cp_lexer_commit_tokens (parser->lexer);
24997 pop_to_parent_deferring_access_checks ();
24999 /* Otherwise, if errors occurred, roll back our state so that things
25000 are just as they were before we began the tentative parse. */
25001 else
25003 cp_lexer_rollback_tokens (parser->lexer);
25004 pop_deferring_access_checks ();
25006 /* Add the context to the front of the free list. */
25007 context->next = cp_parser_context_free_list;
25008 cp_parser_context_free_list = context;
25010 return !error_occurred;
25013 /* Returns true if we are parsing tentatively and are not committed to
25014 this tentative parse. */
25016 static bool
25017 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25019 return (cp_parser_parsing_tentatively (parser)
25020 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25023 /* Returns nonzero iff an error has occurred during the most recent
25024 tentative parse. */
25026 static bool
25027 cp_parser_error_occurred (cp_parser* parser)
25029 return (cp_parser_parsing_tentatively (parser)
25030 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25033 /* Returns nonzero if GNU extensions are allowed. */
25035 static bool
25036 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25038 return parser->allow_gnu_extensions_p;
25041 /* Objective-C++ Productions */
25044 /* Parse an Objective-C expression, which feeds into a primary-expression
25045 above.
25047 objc-expression:
25048 objc-message-expression
25049 objc-string-literal
25050 objc-encode-expression
25051 objc-protocol-expression
25052 objc-selector-expression
25054 Returns a tree representation of the expression. */
25056 static tree
25057 cp_parser_objc_expression (cp_parser* parser)
25059 /* Try to figure out what kind of declaration is present. */
25060 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25062 switch (kwd->type)
25064 case CPP_OPEN_SQUARE:
25065 return cp_parser_objc_message_expression (parser);
25067 case CPP_OBJC_STRING:
25068 kwd = cp_lexer_consume_token (parser->lexer);
25069 return objc_build_string_object (kwd->u.value);
25071 case CPP_KEYWORD:
25072 switch (kwd->keyword)
25074 case RID_AT_ENCODE:
25075 return cp_parser_objc_encode_expression (parser);
25077 case RID_AT_PROTOCOL:
25078 return cp_parser_objc_protocol_expression (parser);
25080 case RID_AT_SELECTOR:
25081 return cp_parser_objc_selector_expression (parser);
25083 default:
25084 break;
25086 default:
25087 error_at (kwd->location,
25088 "misplaced %<@%D%> Objective-C++ construct",
25089 kwd->u.value);
25090 cp_parser_skip_to_end_of_block_or_statement (parser);
25093 return error_mark_node;
25096 /* Parse an Objective-C message expression.
25098 objc-message-expression:
25099 [ objc-message-receiver objc-message-args ]
25101 Returns a representation of an Objective-C message. */
25103 static tree
25104 cp_parser_objc_message_expression (cp_parser* parser)
25106 tree receiver, messageargs;
25108 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25109 receiver = cp_parser_objc_message_receiver (parser);
25110 messageargs = cp_parser_objc_message_args (parser);
25111 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25113 return objc_build_message_expr (receiver, messageargs);
25116 /* Parse an objc-message-receiver.
25118 objc-message-receiver:
25119 expression
25120 simple-type-specifier
25122 Returns a representation of the type or expression. */
25124 static tree
25125 cp_parser_objc_message_receiver (cp_parser* parser)
25127 tree rcv;
25129 /* An Objective-C message receiver may be either (1) a type
25130 or (2) an expression. */
25131 cp_parser_parse_tentatively (parser);
25132 rcv = cp_parser_expression (parser, false, NULL);
25134 if (cp_parser_parse_definitely (parser))
25135 return rcv;
25137 rcv = cp_parser_simple_type_specifier (parser,
25138 /*decl_specs=*/NULL,
25139 CP_PARSER_FLAGS_NONE);
25141 return objc_get_class_reference (rcv);
25144 /* Parse the arguments and selectors comprising an Objective-C message.
25146 objc-message-args:
25147 objc-selector
25148 objc-selector-args
25149 objc-selector-args , objc-comma-args
25151 objc-selector-args:
25152 objc-selector [opt] : assignment-expression
25153 objc-selector-args objc-selector [opt] : assignment-expression
25155 objc-comma-args:
25156 assignment-expression
25157 objc-comma-args , assignment-expression
25159 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25160 selector arguments and TREE_VALUE containing a list of comma
25161 arguments. */
25163 static tree
25164 cp_parser_objc_message_args (cp_parser* parser)
25166 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25167 bool maybe_unary_selector_p = true;
25168 cp_token *token = cp_lexer_peek_token (parser->lexer);
25170 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25172 tree selector = NULL_TREE, arg;
25174 if (token->type != CPP_COLON)
25175 selector = cp_parser_objc_selector (parser);
25177 /* Detect if we have a unary selector. */
25178 if (maybe_unary_selector_p
25179 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25180 return build_tree_list (selector, NULL_TREE);
25182 maybe_unary_selector_p = false;
25183 cp_parser_require (parser, CPP_COLON, RT_COLON);
25184 arg = cp_parser_assignment_expression (parser, false, NULL);
25186 sel_args
25187 = chainon (sel_args,
25188 build_tree_list (selector, arg));
25190 token = cp_lexer_peek_token (parser->lexer);
25193 /* Handle non-selector arguments, if any. */
25194 while (token->type == CPP_COMMA)
25196 tree arg;
25198 cp_lexer_consume_token (parser->lexer);
25199 arg = cp_parser_assignment_expression (parser, false, NULL);
25201 addl_args
25202 = chainon (addl_args,
25203 build_tree_list (NULL_TREE, arg));
25205 token = cp_lexer_peek_token (parser->lexer);
25208 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25210 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25211 return build_tree_list (error_mark_node, error_mark_node);
25214 return build_tree_list (sel_args, addl_args);
25217 /* Parse an Objective-C encode expression.
25219 objc-encode-expression:
25220 @encode objc-typename
25222 Returns an encoded representation of the type argument. */
25224 static tree
25225 cp_parser_objc_encode_expression (cp_parser* parser)
25227 tree type;
25228 cp_token *token;
25230 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25231 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25232 token = cp_lexer_peek_token (parser->lexer);
25233 type = complete_type (cp_parser_type_id (parser));
25234 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25236 if (!type)
25238 error_at (token->location,
25239 "%<@encode%> must specify a type as an argument");
25240 return error_mark_node;
25243 /* This happens if we find @encode(T) (where T is a template
25244 typename or something dependent on a template typename) when
25245 parsing a template. In that case, we can't compile it
25246 immediately, but we rather create an AT_ENCODE_EXPR which will
25247 need to be instantiated when the template is used.
25249 if (dependent_type_p (type))
25251 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25252 TREE_READONLY (value) = 1;
25253 return value;
25256 return objc_build_encode_expr (type);
25259 /* Parse an Objective-C @defs expression. */
25261 static tree
25262 cp_parser_objc_defs_expression (cp_parser *parser)
25264 tree name;
25266 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25267 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25268 name = cp_parser_identifier (parser);
25269 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25271 return objc_get_class_ivars (name);
25274 /* Parse an Objective-C protocol expression.
25276 objc-protocol-expression:
25277 @protocol ( identifier )
25279 Returns a representation of the protocol expression. */
25281 static tree
25282 cp_parser_objc_protocol_expression (cp_parser* parser)
25284 tree proto;
25286 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25287 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25288 proto = cp_parser_identifier (parser);
25289 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25291 return objc_build_protocol_expr (proto);
25294 /* Parse an Objective-C selector expression.
25296 objc-selector-expression:
25297 @selector ( objc-method-signature )
25299 objc-method-signature:
25300 objc-selector
25301 objc-selector-seq
25303 objc-selector-seq:
25304 objc-selector :
25305 objc-selector-seq objc-selector :
25307 Returns a representation of the method selector. */
25309 static tree
25310 cp_parser_objc_selector_expression (cp_parser* parser)
25312 tree sel_seq = NULL_TREE;
25313 bool maybe_unary_selector_p = true;
25314 cp_token *token;
25315 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25317 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25318 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25319 token = cp_lexer_peek_token (parser->lexer);
25321 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25322 || token->type == CPP_SCOPE)
25324 tree selector = NULL_TREE;
25326 if (token->type != CPP_COLON
25327 || token->type == CPP_SCOPE)
25328 selector = cp_parser_objc_selector (parser);
25330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25331 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25333 /* Detect if we have a unary selector. */
25334 if (maybe_unary_selector_p)
25336 sel_seq = selector;
25337 goto finish_selector;
25339 else
25341 cp_parser_error (parser, "expected %<:%>");
25344 maybe_unary_selector_p = false;
25345 token = cp_lexer_consume_token (parser->lexer);
25347 if (token->type == CPP_SCOPE)
25349 sel_seq
25350 = chainon (sel_seq,
25351 build_tree_list (selector, NULL_TREE));
25352 sel_seq
25353 = chainon (sel_seq,
25354 build_tree_list (NULL_TREE, NULL_TREE));
25356 else
25357 sel_seq
25358 = chainon (sel_seq,
25359 build_tree_list (selector, NULL_TREE));
25361 token = cp_lexer_peek_token (parser->lexer);
25364 finish_selector:
25365 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25367 return objc_build_selector_expr (loc, sel_seq);
25370 /* Parse a list of identifiers.
25372 objc-identifier-list:
25373 identifier
25374 objc-identifier-list , identifier
25376 Returns a TREE_LIST of identifier nodes. */
25378 static tree
25379 cp_parser_objc_identifier_list (cp_parser* parser)
25381 tree identifier;
25382 tree list;
25383 cp_token *sep;
25385 identifier = cp_parser_identifier (parser);
25386 if (identifier == error_mark_node)
25387 return error_mark_node;
25389 list = build_tree_list (NULL_TREE, identifier);
25390 sep = cp_lexer_peek_token (parser->lexer);
25392 while (sep->type == CPP_COMMA)
25394 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25395 identifier = cp_parser_identifier (parser);
25396 if (identifier == error_mark_node)
25397 return list;
25399 list = chainon (list, build_tree_list (NULL_TREE,
25400 identifier));
25401 sep = cp_lexer_peek_token (parser->lexer);
25404 return list;
25407 /* Parse an Objective-C alias declaration.
25409 objc-alias-declaration:
25410 @compatibility_alias identifier identifier ;
25412 This function registers the alias mapping with the Objective-C front end.
25413 It returns nothing. */
25415 static void
25416 cp_parser_objc_alias_declaration (cp_parser* parser)
25418 tree alias, orig;
25420 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25421 alias = cp_parser_identifier (parser);
25422 orig = cp_parser_identifier (parser);
25423 objc_declare_alias (alias, orig);
25424 cp_parser_consume_semicolon_at_end_of_statement (parser);
25427 /* Parse an Objective-C class forward-declaration.
25429 objc-class-declaration:
25430 @class objc-identifier-list ;
25432 The function registers the forward declarations with the Objective-C
25433 front end. It returns nothing. */
25435 static void
25436 cp_parser_objc_class_declaration (cp_parser* parser)
25438 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25439 while (true)
25441 tree id;
25443 id = cp_parser_identifier (parser);
25444 if (id == error_mark_node)
25445 break;
25447 objc_declare_class (id);
25449 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25450 cp_lexer_consume_token (parser->lexer);
25451 else
25452 break;
25454 cp_parser_consume_semicolon_at_end_of_statement (parser);
25457 /* Parse a list of Objective-C protocol references.
25459 objc-protocol-refs-opt:
25460 objc-protocol-refs [opt]
25462 objc-protocol-refs:
25463 < objc-identifier-list >
25465 Returns a TREE_LIST of identifiers, if any. */
25467 static tree
25468 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25470 tree protorefs = NULL_TREE;
25472 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25474 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25475 protorefs = cp_parser_objc_identifier_list (parser);
25476 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25479 return protorefs;
25482 /* Parse a Objective-C visibility specification. */
25484 static void
25485 cp_parser_objc_visibility_spec (cp_parser* parser)
25487 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25489 switch (vis->keyword)
25491 case RID_AT_PRIVATE:
25492 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25493 break;
25494 case RID_AT_PROTECTED:
25495 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25496 break;
25497 case RID_AT_PUBLIC:
25498 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25499 break;
25500 case RID_AT_PACKAGE:
25501 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25502 break;
25503 default:
25504 return;
25507 /* Eat '@private'/'@protected'/'@public'. */
25508 cp_lexer_consume_token (parser->lexer);
25511 /* Parse an Objective-C method type. Return 'true' if it is a class
25512 (+) method, and 'false' if it is an instance (-) method. */
25514 static inline bool
25515 cp_parser_objc_method_type (cp_parser* parser)
25517 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25518 return true;
25519 else
25520 return false;
25523 /* Parse an Objective-C protocol qualifier. */
25525 static tree
25526 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25528 tree quals = NULL_TREE, node;
25529 cp_token *token = cp_lexer_peek_token (parser->lexer);
25531 node = token->u.value;
25533 while (node && identifier_p (node)
25534 && (node == ridpointers [(int) RID_IN]
25535 || node == ridpointers [(int) RID_OUT]
25536 || node == ridpointers [(int) RID_INOUT]
25537 || node == ridpointers [(int) RID_BYCOPY]
25538 || node == ridpointers [(int) RID_BYREF]
25539 || node == ridpointers [(int) RID_ONEWAY]))
25541 quals = tree_cons (NULL_TREE, node, quals);
25542 cp_lexer_consume_token (parser->lexer);
25543 token = cp_lexer_peek_token (parser->lexer);
25544 node = token->u.value;
25547 return quals;
25550 /* Parse an Objective-C typename. */
25552 static tree
25553 cp_parser_objc_typename (cp_parser* parser)
25555 tree type_name = NULL_TREE;
25557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25559 tree proto_quals, cp_type = NULL_TREE;
25561 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25562 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25564 /* An ObjC type name may consist of just protocol qualifiers, in which
25565 case the type shall default to 'id'. */
25566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25568 cp_type = cp_parser_type_id (parser);
25570 /* If the type could not be parsed, an error has already
25571 been produced. For error recovery, behave as if it had
25572 not been specified, which will use the default type
25573 'id'. */
25574 if (cp_type == error_mark_node)
25576 cp_type = NULL_TREE;
25577 /* We need to skip to the closing parenthesis as
25578 cp_parser_type_id() does not seem to do it for
25579 us. */
25580 cp_parser_skip_to_closing_parenthesis (parser,
25581 /*recovering=*/true,
25582 /*or_comma=*/false,
25583 /*consume_paren=*/false);
25587 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25588 type_name = build_tree_list (proto_quals, cp_type);
25591 return type_name;
25594 /* Check to see if TYPE refers to an Objective-C selector name. */
25596 static bool
25597 cp_parser_objc_selector_p (enum cpp_ttype type)
25599 return (type == CPP_NAME || type == CPP_KEYWORD
25600 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25601 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25602 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25603 || type == CPP_XOR || type == CPP_XOR_EQ);
25606 /* Parse an Objective-C selector. */
25608 static tree
25609 cp_parser_objc_selector (cp_parser* parser)
25611 cp_token *token = cp_lexer_consume_token (parser->lexer);
25613 if (!cp_parser_objc_selector_p (token->type))
25615 error_at (token->location, "invalid Objective-C++ selector name");
25616 return error_mark_node;
25619 /* C++ operator names are allowed to appear in ObjC selectors. */
25620 switch (token->type)
25622 case CPP_AND_AND: return get_identifier ("and");
25623 case CPP_AND_EQ: return get_identifier ("and_eq");
25624 case CPP_AND: return get_identifier ("bitand");
25625 case CPP_OR: return get_identifier ("bitor");
25626 case CPP_COMPL: return get_identifier ("compl");
25627 case CPP_NOT: return get_identifier ("not");
25628 case CPP_NOT_EQ: return get_identifier ("not_eq");
25629 case CPP_OR_OR: return get_identifier ("or");
25630 case CPP_OR_EQ: return get_identifier ("or_eq");
25631 case CPP_XOR: return get_identifier ("xor");
25632 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25633 default: return token->u.value;
25637 /* Parse an Objective-C params list. */
25639 static tree
25640 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25642 tree params = NULL_TREE;
25643 bool maybe_unary_selector_p = true;
25644 cp_token *token = cp_lexer_peek_token (parser->lexer);
25646 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25648 tree selector = NULL_TREE, type_name, identifier;
25649 tree parm_attr = NULL_TREE;
25651 if (token->keyword == RID_ATTRIBUTE)
25652 break;
25654 if (token->type != CPP_COLON)
25655 selector = cp_parser_objc_selector (parser);
25657 /* Detect if we have a unary selector. */
25658 if (maybe_unary_selector_p
25659 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25661 params = selector; /* Might be followed by attributes. */
25662 break;
25665 maybe_unary_selector_p = false;
25666 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25668 /* Something went quite wrong. There should be a colon
25669 here, but there is not. Stop parsing parameters. */
25670 break;
25672 type_name = cp_parser_objc_typename (parser);
25673 /* New ObjC allows attributes on parameters too. */
25674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25675 parm_attr = cp_parser_attributes_opt (parser);
25676 identifier = cp_parser_identifier (parser);
25678 params
25679 = chainon (params,
25680 objc_build_keyword_decl (selector,
25681 type_name,
25682 identifier,
25683 parm_attr));
25685 token = cp_lexer_peek_token (parser->lexer);
25688 if (params == NULL_TREE)
25690 cp_parser_error (parser, "objective-c++ method declaration is expected");
25691 return error_mark_node;
25694 /* We allow tail attributes for the method. */
25695 if (token->keyword == RID_ATTRIBUTE)
25697 *attributes = cp_parser_attributes_opt (parser);
25698 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25699 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25700 return params;
25701 cp_parser_error (parser,
25702 "method attributes must be specified at the end");
25703 return error_mark_node;
25706 if (params == NULL_TREE)
25708 cp_parser_error (parser, "objective-c++ method declaration is expected");
25709 return error_mark_node;
25711 return params;
25714 /* Parse the non-keyword Objective-C params. */
25716 static tree
25717 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25718 tree* attributes)
25720 tree params = make_node (TREE_LIST);
25721 cp_token *token = cp_lexer_peek_token (parser->lexer);
25722 *ellipsisp = false; /* Initially, assume no ellipsis. */
25724 while (token->type == CPP_COMMA)
25726 cp_parameter_declarator *parmdecl;
25727 tree parm;
25729 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25730 token = cp_lexer_peek_token (parser->lexer);
25732 if (token->type == CPP_ELLIPSIS)
25734 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25735 *ellipsisp = true;
25736 token = cp_lexer_peek_token (parser->lexer);
25737 break;
25740 /* TODO: parse attributes for tail parameters. */
25741 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25742 parm = grokdeclarator (parmdecl->declarator,
25743 &parmdecl->decl_specifiers,
25744 PARM, /*initialized=*/0,
25745 /*attrlist=*/NULL);
25747 chainon (params, build_tree_list (NULL_TREE, parm));
25748 token = cp_lexer_peek_token (parser->lexer);
25751 /* We allow tail attributes for the method. */
25752 if (token->keyword == RID_ATTRIBUTE)
25754 if (*attributes == NULL_TREE)
25756 *attributes = cp_parser_attributes_opt (parser);
25757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25758 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25759 return params;
25761 else
25762 /* We have an error, but parse the attributes, so that we can
25763 carry on. */
25764 *attributes = cp_parser_attributes_opt (parser);
25766 cp_parser_error (parser,
25767 "method attributes must be specified at the end");
25768 return error_mark_node;
25771 return params;
25774 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25776 static void
25777 cp_parser_objc_interstitial_code (cp_parser* parser)
25779 cp_token *token = cp_lexer_peek_token (parser->lexer);
25781 /* If the next token is `extern' and the following token is a string
25782 literal, then we have a linkage specification. */
25783 if (token->keyword == RID_EXTERN
25784 && cp_parser_is_pure_string_literal
25785 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25786 cp_parser_linkage_specification (parser);
25787 /* Handle #pragma, if any. */
25788 else if (token->type == CPP_PRAGMA)
25789 cp_parser_pragma (parser, pragma_objc_icode);
25790 /* Allow stray semicolons. */
25791 else if (token->type == CPP_SEMICOLON)
25792 cp_lexer_consume_token (parser->lexer);
25793 /* Mark methods as optional or required, when building protocols. */
25794 else if (token->keyword == RID_AT_OPTIONAL)
25796 cp_lexer_consume_token (parser->lexer);
25797 objc_set_method_opt (true);
25799 else if (token->keyword == RID_AT_REQUIRED)
25801 cp_lexer_consume_token (parser->lexer);
25802 objc_set_method_opt (false);
25804 else if (token->keyword == RID_NAMESPACE)
25805 cp_parser_namespace_definition (parser);
25806 /* Other stray characters must generate errors. */
25807 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25809 cp_lexer_consume_token (parser->lexer);
25810 error ("stray %qs between Objective-C++ methods",
25811 token->type == CPP_OPEN_BRACE ? "{" : "}");
25813 /* Finally, try to parse a block-declaration, or a function-definition. */
25814 else
25815 cp_parser_block_declaration (parser, /*statement_p=*/false);
25818 /* Parse a method signature. */
25820 static tree
25821 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25823 tree rettype, kwdparms, optparms;
25824 bool ellipsis = false;
25825 bool is_class_method;
25827 is_class_method = cp_parser_objc_method_type (parser);
25828 rettype = cp_parser_objc_typename (parser);
25829 *attributes = NULL_TREE;
25830 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25831 if (kwdparms == error_mark_node)
25832 return error_mark_node;
25833 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25834 if (optparms == error_mark_node)
25835 return error_mark_node;
25837 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25840 static bool
25841 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25843 tree tattr;
25844 cp_lexer_save_tokens (parser->lexer);
25845 tattr = cp_parser_attributes_opt (parser);
25846 gcc_assert (tattr) ;
25848 /* If the attributes are followed by a method introducer, this is not allowed.
25849 Dump the attributes and flag the situation. */
25850 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25851 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25852 return true;
25854 /* Otherwise, the attributes introduce some interstitial code, possibly so
25855 rewind to allow that check. */
25856 cp_lexer_rollback_tokens (parser->lexer);
25857 return false;
25860 /* Parse an Objective-C method prototype list. */
25862 static void
25863 cp_parser_objc_method_prototype_list (cp_parser* parser)
25865 cp_token *token = cp_lexer_peek_token (parser->lexer);
25867 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25869 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25871 tree attributes, sig;
25872 bool is_class_method;
25873 if (token->type == CPP_PLUS)
25874 is_class_method = true;
25875 else
25876 is_class_method = false;
25877 sig = cp_parser_objc_method_signature (parser, &attributes);
25878 if (sig == error_mark_node)
25880 cp_parser_skip_to_end_of_block_or_statement (parser);
25881 token = cp_lexer_peek_token (parser->lexer);
25882 continue;
25884 objc_add_method_declaration (is_class_method, sig, attributes);
25885 cp_parser_consume_semicolon_at_end_of_statement (parser);
25887 else if (token->keyword == RID_AT_PROPERTY)
25888 cp_parser_objc_at_property_declaration (parser);
25889 else if (token->keyword == RID_ATTRIBUTE
25890 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25891 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25892 OPT_Wattributes,
25893 "prefix attributes are ignored for methods");
25894 else
25895 /* Allow for interspersed non-ObjC++ code. */
25896 cp_parser_objc_interstitial_code (parser);
25898 token = cp_lexer_peek_token (parser->lexer);
25901 if (token->type != CPP_EOF)
25902 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25903 else
25904 cp_parser_error (parser, "expected %<@end%>");
25906 objc_finish_interface ();
25909 /* Parse an Objective-C method definition list. */
25911 static void
25912 cp_parser_objc_method_definition_list (cp_parser* parser)
25914 cp_token *token = cp_lexer_peek_token (parser->lexer);
25916 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25918 tree meth;
25920 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25922 cp_token *ptk;
25923 tree sig, attribute;
25924 bool is_class_method;
25925 if (token->type == CPP_PLUS)
25926 is_class_method = true;
25927 else
25928 is_class_method = false;
25929 push_deferring_access_checks (dk_deferred);
25930 sig = cp_parser_objc_method_signature (parser, &attribute);
25931 if (sig == error_mark_node)
25933 cp_parser_skip_to_end_of_block_or_statement (parser);
25934 token = cp_lexer_peek_token (parser->lexer);
25935 continue;
25937 objc_start_method_definition (is_class_method, sig, attribute,
25938 NULL_TREE);
25940 /* For historical reasons, we accept an optional semicolon. */
25941 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25942 cp_lexer_consume_token (parser->lexer);
25944 ptk = cp_lexer_peek_token (parser->lexer);
25945 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25946 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25948 perform_deferred_access_checks (tf_warning_or_error);
25949 stop_deferring_access_checks ();
25950 meth = cp_parser_function_definition_after_declarator (parser,
25951 false);
25952 pop_deferring_access_checks ();
25953 objc_finish_method_definition (meth);
25956 /* The following case will be removed once @synthesize is
25957 completely implemented. */
25958 else if (token->keyword == RID_AT_PROPERTY)
25959 cp_parser_objc_at_property_declaration (parser);
25960 else if (token->keyword == RID_AT_SYNTHESIZE)
25961 cp_parser_objc_at_synthesize_declaration (parser);
25962 else if (token->keyword == RID_AT_DYNAMIC)
25963 cp_parser_objc_at_dynamic_declaration (parser);
25964 else if (token->keyword == RID_ATTRIBUTE
25965 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25966 warning_at (token->location, OPT_Wattributes,
25967 "prefix attributes are ignored for methods");
25968 else
25969 /* Allow for interspersed non-ObjC++ code. */
25970 cp_parser_objc_interstitial_code (parser);
25972 token = cp_lexer_peek_token (parser->lexer);
25975 if (token->type != CPP_EOF)
25976 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25977 else
25978 cp_parser_error (parser, "expected %<@end%>");
25980 objc_finish_implementation ();
25983 /* Parse Objective-C ivars. */
25985 static void
25986 cp_parser_objc_class_ivars (cp_parser* parser)
25988 cp_token *token = cp_lexer_peek_token (parser->lexer);
25990 if (token->type != CPP_OPEN_BRACE)
25991 return; /* No ivars specified. */
25993 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25994 token = cp_lexer_peek_token (parser->lexer);
25996 while (token->type != CPP_CLOSE_BRACE
25997 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25999 cp_decl_specifier_seq declspecs;
26000 int decl_class_or_enum_p;
26001 tree prefix_attributes;
26003 cp_parser_objc_visibility_spec (parser);
26005 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26006 break;
26008 cp_parser_decl_specifier_seq (parser,
26009 CP_PARSER_FLAGS_OPTIONAL,
26010 &declspecs,
26011 &decl_class_or_enum_p);
26013 /* auto, register, static, extern, mutable. */
26014 if (declspecs.storage_class != sc_none)
26016 cp_parser_error (parser, "invalid type for instance variable");
26017 declspecs.storage_class = sc_none;
26020 /* thread_local. */
26021 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26023 cp_parser_error (parser, "invalid type for instance variable");
26024 declspecs.locations[ds_thread] = 0;
26027 /* typedef. */
26028 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26030 cp_parser_error (parser, "invalid type for instance variable");
26031 declspecs.locations[ds_typedef] = 0;
26034 prefix_attributes = declspecs.attributes;
26035 declspecs.attributes = NULL_TREE;
26037 /* Keep going until we hit the `;' at the end of the
26038 declaration. */
26039 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26041 tree width = NULL_TREE, attributes, first_attribute, decl;
26042 cp_declarator *declarator = NULL;
26043 int ctor_dtor_or_conv_p;
26045 /* Check for a (possibly unnamed) bitfield declaration. */
26046 token = cp_lexer_peek_token (parser->lexer);
26047 if (token->type == CPP_COLON)
26048 goto eat_colon;
26050 if (token->type == CPP_NAME
26051 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26052 == CPP_COLON))
26054 /* Get the name of the bitfield. */
26055 declarator = make_id_declarator (NULL_TREE,
26056 cp_parser_identifier (parser),
26057 sfk_none);
26059 eat_colon:
26060 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26061 /* Get the width of the bitfield. */
26062 width
26063 = cp_parser_constant_expression (parser,
26064 /*allow_non_constant=*/false,
26065 NULL);
26067 else
26069 /* Parse the declarator. */
26070 declarator
26071 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26072 &ctor_dtor_or_conv_p,
26073 /*parenthesized_p=*/NULL,
26074 /*member_p=*/false);
26077 /* Look for attributes that apply to the ivar. */
26078 attributes = cp_parser_attributes_opt (parser);
26079 /* Remember which attributes are prefix attributes and
26080 which are not. */
26081 first_attribute = attributes;
26082 /* Combine the attributes. */
26083 attributes = chainon (prefix_attributes, attributes);
26085 if (width)
26086 /* Create the bitfield declaration. */
26087 decl = grokbitfield (declarator, &declspecs,
26088 width,
26089 attributes);
26090 else
26091 decl = grokfield (declarator, &declspecs,
26092 NULL_TREE, /*init_const_expr_p=*/false,
26093 NULL_TREE, attributes);
26095 /* Add the instance variable. */
26096 if (decl != error_mark_node && decl != NULL_TREE)
26097 objc_add_instance_variable (decl);
26099 /* Reset PREFIX_ATTRIBUTES. */
26100 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26101 attributes = TREE_CHAIN (attributes);
26102 if (attributes)
26103 TREE_CHAIN (attributes) = NULL_TREE;
26105 token = cp_lexer_peek_token (parser->lexer);
26107 if (token->type == CPP_COMMA)
26109 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26110 continue;
26112 break;
26115 cp_parser_consume_semicolon_at_end_of_statement (parser);
26116 token = cp_lexer_peek_token (parser->lexer);
26119 if (token->keyword == RID_AT_END)
26120 cp_parser_error (parser, "expected %<}%>");
26122 /* Do not consume the RID_AT_END, so it will be read again as terminating
26123 the @interface of @implementation. */
26124 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26125 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26127 /* For historical reasons, we accept an optional semicolon. */
26128 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26129 cp_lexer_consume_token (parser->lexer);
26132 /* Parse an Objective-C protocol declaration. */
26134 static void
26135 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26137 tree proto, protorefs;
26138 cp_token *tok;
26140 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26143 tok = cp_lexer_peek_token (parser->lexer);
26144 error_at (tok->location, "identifier expected after %<@protocol%>");
26145 cp_parser_consume_semicolon_at_end_of_statement (parser);
26146 return;
26149 /* See if we have a forward declaration or a definition. */
26150 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26152 /* Try a forward declaration first. */
26153 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26155 while (true)
26157 tree id;
26159 id = cp_parser_identifier (parser);
26160 if (id == error_mark_node)
26161 break;
26163 objc_declare_protocol (id, attributes);
26165 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26166 cp_lexer_consume_token (parser->lexer);
26167 else
26168 break;
26170 cp_parser_consume_semicolon_at_end_of_statement (parser);
26173 /* Ok, we got a full-fledged definition (or at least should). */
26174 else
26176 proto = cp_parser_identifier (parser);
26177 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26178 objc_start_protocol (proto, protorefs, attributes);
26179 cp_parser_objc_method_prototype_list (parser);
26183 /* Parse an Objective-C superclass or category. */
26185 static void
26186 cp_parser_objc_superclass_or_category (cp_parser *parser,
26187 bool iface_p,
26188 tree *super,
26189 tree *categ, bool *is_class_extension)
26191 cp_token *next = cp_lexer_peek_token (parser->lexer);
26193 *super = *categ = NULL_TREE;
26194 *is_class_extension = false;
26195 if (next->type == CPP_COLON)
26197 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26198 *super = cp_parser_identifier (parser);
26200 else if (next->type == CPP_OPEN_PAREN)
26202 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26204 /* If there is no category name, and this is an @interface, we
26205 have a class extension. */
26206 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26208 *categ = NULL_TREE;
26209 *is_class_extension = true;
26211 else
26212 *categ = cp_parser_identifier (parser);
26214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26218 /* Parse an Objective-C class interface. */
26220 static void
26221 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26223 tree name, super, categ, protos;
26224 bool is_class_extension;
26226 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26227 name = cp_parser_identifier (parser);
26228 if (name == error_mark_node)
26230 /* It's hard to recover because even if valid @interface stuff
26231 is to follow, we can't compile it (or validate it) if we
26232 don't even know which class it refers to. Let's assume this
26233 was a stray '@interface' token in the stream and skip it.
26235 return;
26237 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26238 &is_class_extension);
26239 protos = cp_parser_objc_protocol_refs_opt (parser);
26241 /* We have either a class or a category on our hands. */
26242 if (categ || is_class_extension)
26243 objc_start_category_interface (name, categ, protos, attributes);
26244 else
26246 objc_start_class_interface (name, super, protos, attributes);
26247 /* Handle instance variable declarations, if any. */
26248 cp_parser_objc_class_ivars (parser);
26249 objc_continue_interface ();
26252 cp_parser_objc_method_prototype_list (parser);
26255 /* Parse an Objective-C class implementation. */
26257 static void
26258 cp_parser_objc_class_implementation (cp_parser* parser)
26260 tree name, super, categ;
26261 bool is_class_extension;
26263 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26264 name = cp_parser_identifier (parser);
26265 if (name == error_mark_node)
26267 /* It's hard to recover because even if valid @implementation
26268 stuff is to follow, we can't compile it (or validate it) if
26269 we don't even know which class it refers to. Let's assume
26270 this was a stray '@implementation' token in the stream and
26271 skip it.
26273 return;
26275 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26276 &is_class_extension);
26278 /* We have either a class or a category on our hands. */
26279 if (categ)
26280 objc_start_category_implementation (name, categ);
26281 else
26283 objc_start_class_implementation (name, super);
26284 /* Handle instance variable declarations, if any. */
26285 cp_parser_objc_class_ivars (parser);
26286 objc_continue_implementation ();
26289 cp_parser_objc_method_definition_list (parser);
26292 /* Consume the @end token and finish off the implementation. */
26294 static void
26295 cp_parser_objc_end_implementation (cp_parser* parser)
26297 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26298 objc_finish_implementation ();
26301 /* Parse an Objective-C declaration. */
26303 static void
26304 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26306 /* Try to figure out what kind of declaration is present. */
26307 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26309 if (attributes)
26310 switch (kwd->keyword)
26312 case RID_AT_ALIAS:
26313 case RID_AT_CLASS:
26314 case RID_AT_END:
26315 error_at (kwd->location, "attributes may not be specified before"
26316 " the %<@%D%> Objective-C++ keyword",
26317 kwd->u.value);
26318 attributes = NULL;
26319 break;
26320 case RID_AT_IMPLEMENTATION:
26321 warning_at (kwd->location, OPT_Wattributes,
26322 "prefix attributes are ignored before %<@%D%>",
26323 kwd->u.value);
26324 attributes = NULL;
26325 default:
26326 break;
26329 switch (kwd->keyword)
26331 case RID_AT_ALIAS:
26332 cp_parser_objc_alias_declaration (parser);
26333 break;
26334 case RID_AT_CLASS:
26335 cp_parser_objc_class_declaration (parser);
26336 break;
26337 case RID_AT_PROTOCOL:
26338 cp_parser_objc_protocol_declaration (parser, attributes);
26339 break;
26340 case RID_AT_INTERFACE:
26341 cp_parser_objc_class_interface (parser, attributes);
26342 break;
26343 case RID_AT_IMPLEMENTATION:
26344 cp_parser_objc_class_implementation (parser);
26345 break;
26346 case RID_AT_END:
26347 cp_parser_objc_end_implementation (parser);
26348 break;
26349 default:
26350 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26351 kwd->u.value);
26352 cp_parser_skip_to_end_of_block_or_statement (parser);
26356 /* Parse an Objective-C try-catch-finally statement.
26358 objc-try-catch-finally-stmt:
26359 @try compound-statement objc-catch-clause-seq [opt]
26360 objc-finally-clause [opt]
26362 objc-catch-clause-seq:
26363 objc-catch-clause objc-catch-clause-seq [opt]
26365 objc-catch-clause:
26366 @catch ( objc-exception-declaration ) compound-statement
26368 objc-finally-clause:
26369 @finally compound-statement
26371 objc-exception-declaration:
26372 parameter-declaration
26373 '...'
26375 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26377 Returns NULL_TREE.
26379 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26380 for C. Keep them in sync. */
26382 static tree
26383 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26385 location_t location;
26386 tree stmt;
26388 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26389 location = cp_lexer_peek_token (parser->lexer)->location;
26390 objc_maybe_warn_exceptions (location);
26391 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26392 node, lest it get absorbed into the surrounding block. */
26393 stmt = push_stmt_list ();
26394 cp_parser_compound_statement (parser, NULL, false, false);
26395 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26397 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26399 cp_parameter_declarator *parm;
26400 tree parameter_declaration = error_mark_node;
26401 bool seen_open_paren = false;
26403 cp_lexer_consume_token (parser->lexer);
26404 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26405 seen_open_paren = true;
26406 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26408 /* We have "@catch (...)" (where the '...' are literally
26409 what is in the code). Skip the '...'.
26410 parameter_declaration is set to NULL_TREE, and
26411 objc_being_catch_clauses() knows that that means
26412 '...'. */
26413 cp_lexer_consume_token (parser->lexer);
26414 parameter_declaration = NULL_TREE;
26416 else
26418 /* We have "@catch (NSException *exception)" or something
26419 like that. Parse the parameter declaration. */
26420 parm = cp_parser_parameter_declaration (parser, false, NULL);
26421 if (parm == NULL)
26422 parameter_declaration = error_mark_node;
26423 else
26424 parameter_declaration = grokdeclarator (parm->declarator,
26425 &parm->decl_specifiers,
26426 PARM, /*initialized=*/0,
26427 /*attrlist=*/NULL);
26429 if (seen_open_paren)
26430 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26431 else
26433 /* If there was no open parenthesis, we are recovering from
26434 an error, and we are trying to figure out what mistake
26435 the user has made. */
26437 /* If there is an immediate closing parenthesis, the user
26438 probably forgot the opening one (ie, they typed "@catch
26439 NSException *e)". Parse the closing parenthesis and keep
26440 going. */
26441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26442 cp_lexer_consume_token (parser->lexer);
26444 /* If these is no immediate closing parenthesis, the user
26445 probably doesn't know that parenthesis are required at
26446 all (ie, they typed "@catch NSException *e"). So, just
26447 forget about the closing parenthesis and keep going. */
26449 objc_begin_catch_clause (parameter_declaration);
26450 cp_parser_compound_statement (parser, NULL, false, false);
26451 objc_finish_catch_clause ();
26453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26455 cp_lexer_consume_token (parser->lexer);
26456 location = cp_lexer_peek_token (parser->lexer)->location;
26457 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26458 node, lest it get absorbed into the surrounding block. */
26459 stmt = push_stmt_list ();
26460 cp_parser_compound_statement (parser, NULL, false, false);
26461 objc_build_finally_clause (location, pop_stmt_list (stmt));
26464 return objc_finish_try_stmt ();
26467 /* Parse an Objective-C synchronized statement.
26469 objc-synchronized-stmt:
26470 @synchronized ( expression ) compound-statement
26472 Returns NULL_TREE. */
26474 static tree
26475 cp_parser_objc_synchronized_statement (cp_parser *parser)
26477 location_t location;
26478 tree lock, stmt;
26480 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26482 location = cp_lexer_peek_token (parser->lexer)->location;
26483 objc_maybe_warn_exceptions (location);
26484 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26485 lock = cp_parser_expression (parser, false, NULL);
26486 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26488 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26489 node, lest it get absorbed into the surrounding block. */
26490 stmt = push_stmt_list ();
26491 cp_parser_compound_statement (parser, NULL, false, false);
26493 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26496 /* Parse an Objective-C throw statement.
26498 objc-throw-stmt:
26499 @throw assignment-expression [opt] ;
26501 Returns a constructed '@throw' statement. */
26503 static tree
26504 cp_parser_objc_throw_statement (cp_parser *parser)
26506 tree expr = NULL_TREE;
26507 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26509 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26512 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26514 cp_parser_consume_semicolon_at_end_of_statement (parser);
26516 return objc_build_throw_stmt (loc, expr);
26519 /* Parse an Objective-C statement. */
26521 static tree
26522 cp_parser_objc_statement (cp_parser * parser)
26524 /* Try to figure out what kind of declaration is present. */
26525 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26527 switch (kwd->keyword)
26529 case RID_AT_TRY:
26530 return cp_parser_objc_try_catch_finally_statement (parser);
26531 case RID_AT_SYNCHRONIZED:
26532 return cp_parser_objc_synchronized_statement (parser);
26533 case RID_AT_THROW:
26534 return cp_parser_objc_throw_statement (parser);
26535 default:
26536 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26537 kwd->u.value);
26538 cp_parser_skip_to_end_of_block_or_statement (parser);
26541 return error_mark_node;
26544 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26545 look ahead to see if an objc keyword follows the attributes. This
26546 is to detect the use of prefix attributes on ObjC @interface and
26547 @protocol. */
26549 static bool
26550 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26552 cp_lexer_save_tokens (parser->lexer);
26553 *attrib = cp_parser_attributes_opt (parser);
26554 gcc_assert (*attrib);
26555 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26557 cp_lexer_commit_tokens (parser->lexer);
26558 return true;
26560 cp_lexer_rollback_tokens (parser->lexer);
26561 return false;
26564 /* This routine is a minimal replacement for
26565 c_parser_struct_declaration () used when parsing the list of
26566 types/names or ObjC++ properties. For example, when parsing the
26567 code
26569 @property (readonly) int a, b, c;
26571 this function is responsible for parsing "int a, int b, int c" and
26572 returning the declarations as CHAIN of DECLs.
26574 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26575 similar parsing. */
26576 static tree
26577 cp_parser_objc_struct_declaration (cp_parser *parser)
26579 tree decls = NULL_TREE;
26580 cp_decl_specifier_seq declspecs;
26581 int decl_class_or_enum_p;
26582 tree prefix_attributes;
26584 cp_parser_decl_specifier_seq (parser,
26585 CP_PARSER_FLAGS_NONE,
26586 &declspecs,
26587 &decl_class_or_enum_p);
26589 if (declspecs.type == error_mark_node)
26590 return error_mark_node;
26592 /* auto, register, static, extern, mutable. */
26593 if (declspecs.storage_class != sc_none)
26595 cp_parser_error (parser, "invalid type for property");
26596 declspecs.storage_class = sc_none;
26599 /* thread_local. */
26600 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26602 cp_parser_error (parser, "invalid type for property");
26603 declspecs.locations[ds_thread] = 0;
26606 /* typedef. */
26607 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26609 cp_parser_error (parser, "invalid type for property");
26610 declspecs.locations[ds_typedef] = 0;
26613 prefix_attributes = declspecs.attributes;
26614 declspecs.attributes = NULL_TREE;
26616 /* Keep going until we hit the `;' at the end of the declaration. */
26617 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26619 tree attributes, first_attribute, decl;
26620 cp_declarator *declarator;
26621 cp_token *token;
26623 /* Parse the declarator. */
26624 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26625 NULL, NULL, false);
26627 /* Look for attributes that apply to the ivar. */
26628 attributes = cp_parser_attributes_opt (parser);
26629 /* Remember which attributes are prefix attributes and
26630 which are not. */
26631 first_attribute = attributes;
26632 /* Combine the attributes. */
26633 attributes = chainon (prefix_attributes, attributes);
26635 decl = grokfield (declarator, &declspecs,
26636 NULL_TREE, /*init_const_expr_p=*/false,
26637 NULL_TREE, attributes);
26639 if (decl == error_mark_node || decl == NULL_TREE)
26640 return error_mark_node;
26642 /* Reset PREFIX_ATTRIBUTES. */
26643 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26644 attributes = TREE_CHAIN (attributes);
26645 if (attributes)
26646 TREE_CHAIN (attributes) = NULL_TREE;
26648 DECL_CHAIN (decl) = decls;
26649 decls = decl;
26651 token = cp_lexer_peek_token (parser->lexer);
26652 if (token->type == CPP_COMMA)
26654 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26655 continue;
26657 else
26658 break;
26660 return decls;
26663 /* Parse an Objective-C @property declaration. The syntax is:
26665 objc-property-declaration:
26666 '@property' objc-property-attributes[opt] struct-declaration ;
26668 objc-property-attributes:
26669 '(' objc-property-attribute-list ')'
26671 objc-property-attribute-list:
26672 objc-property-attribute
26673 objc-property-attribute-list, objc-property-attribute
26675 objc-property-attribute
26676 'getter' = identifier
26677 'setter' = identifier
26678 'readonly'
26679 'readwrite'
26680 'assign'
26681 'retain'
26682 'copy'
26683 'nonatomic'
26685 For example:
26686 @property NSString *name;
26687 @property (readonly) id object;
26688 @property (retain, nonatomic, getter=getTheName) id name;
26689 @property int a, b, c;
26691 PS: This function is identical to
26692 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26693 static void
26694 cp_parser_objc_at_property_declaration (cp_parser *parser)
26696 /* The following variables hold the attributes of the properties as
26697 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26698 seen. When we see an attribute, we set them to 'true' (if they
26699 are boolean properties) or to the identifier (if they have an
26700 argument, ie, for getter and setter). Note that here we only
26701 parse the list of attributes, check the syntax and accumulate the
26702 attributes that we find. objc_add_property_declaration() will
26703 then process the information. */
26704 bool property_assign = false;
26705 bool property_copy = false;
26706 tree property_getter_ident = NULL_TREE;
26707 bool property_nonatomic = false;
26708 bool property_readonly = false;
26709 bool property_readwrite = false;
26710 bool property_retain = false;
26711 tree property_setter_ident = NULL_TREE;
26713 /* 'properties' is the list of properties that we read. Usually a
26714 single one, but maybe more (eg, in "@property int a, b, c;" there
26715 are three). */
26716 tree properties;
26717 location_t loc;
26719 loc = cp_lexer_peek_token (parser->lexer)->location;
26721 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26723 /* Parse the optional attribute list... */
26724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26726 /* Eat the '('. */
26727 cp_lexer_consume_token (parser->lexer);
26729 while (true)
26731 bool syntax_error = false;
26732 cp_token *token = cp_lexer_peek_token (parser->lexer);
26733 enum rid keyword;
26735 if (token->type != CPP_NAME)
26737 cp_parser_error (parser, "expected identifier");
26738 break;
26740 keyword = C_RID_CODE (token->u.value);
26741 cp_lexer_consume_token (parser->lexer);
26742 switch (keyword)
26744 case RID_ASSIGN: property_assign = true; break;
26745 case RID_COPY: property_copy = true; break;
26746 case RID_NONATOMIC: property_nonatomic = true; break;
26747 case RID_READONLY: property_readonly = true; break;
26748 case RID_READWRITE: property_readwrite = true; break;
26749 case RID_RETAIN: property_retain = true; break;
26751 case RID_GETTER:
26752 case RID_SETTER:
26753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26755 if (keyword == RID_GETTER)
26756 cp_parser_error (parser,
26757 "missing %<=%> (after %<getter%> attribute)");
26758 else
26759 cp_parser_error (parser,
26760 "missing %<=%> (after %<setter%> attribute)");
26761 syntax_error = true;
26762 break;
26764 cp_lexer_consume_token (parser->lexer); /* eat the = */
26765 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26767 cp_parser_error (parser, "expected identifier");
26768 syntax_error = true;
26769 break;
26771 if (keyword == RID_SETTER)
26773 if (property_setter_ident != NULL_TREE)
26775 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26776 cp_lexer_consume_token (parser->lexer);
26778 else
26779 property_setter_ident = cp_parser_objc_selector (parser);
26780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26781 cp_parser_error (parser, "setter name must terminate with %<:%>");
26782 else
26783 cp_lexer_consume_token (parser->lexer);
26785 else
26787 if (property_getter_ident != NULL_TREE)
26789 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26790 cp_lexer_consume_token (parser->lexer);
26792 else
26793 property_getter_ident = cp_parser_objc_selector (parser);
26795 break;
26796 default:
26797 cp_parser_error (parser, "unknown property attribute");
26798 syntax_error = true;
26799 break;
26802 if (syntax_error)
26803 break;
26805 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26806 cp_lexer_consume_token (parser->lexer);
26807 else
26808 break;
26811 /* FIXME: "@property (setter, assign);" will generate a spurious
26812 "error: expected ‘)’ before ‘,’ token". This is because
26813 cp_parser_require, unlike the C counterpart, will produce an
26814 error even if we are in error recovery. */
26815 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26817 cp_parser_skip_to_closing_parenthesis (parser,
26818 /*recovering=*/true,
26819 /*or_comma=*/false,
26820 /*consume_paren=*/true);
26824 /* ... and the property declaration(s). */
26825 properties = cp_parser_objc_struct_declaration (parser);
26827 if (properties == error_mark_node)
26829 cp_parser_skip_to_end_of_statement (parser);
26830 /* If the next token is now a `;', consume it. */
26831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26832 cp_lexer_consume_token (parser->lexer);
26833 return;
26836 if (properties == NULL_TREE)
26837 cp_parser_error (parser, "expected identifier");
26838 else
26840 /* Comma-separated properties are chained together in
26841 reverse order; add them one by one. */
26842 properties = nreverse (properties);
26844 for (; properties; properties = TREE_CHAIN (properties))
26845 objc_add_property_declaration (loc, copy_node (properties),
26846 property_readonly, property_readwrite,
26847 property_assign, property_retain,
26848 property_copy, property_nonatomic,
26849 property_getter_ident, property_setter_ident);
26852 cp_parser_consume_semicolon_at_end_of_statement (parser);
26855 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26857 objc-synthesize-declaration:
26858 @synthesize objc-synthesize-identifier-list ;
26860 objc-synthesize-identifier-list:
26861 objc-synthesize-identifier
26862 objc-synthesize-identifier-list, objc-synthesize-identifier
26864 objc-synthesize-identifier
26865 identifier
26866 identifier = identifier
26868 For example:
26869 @synthesize MyProperty;
26870 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26872 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26873 for C. Keep them in sync.
26875 static void
26876 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26878 tree list = NULL_TREE;
26879 location_t loc;
26880 loc = cp_lexer_peek_token (parser->lexer)->location;
26882 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26883 while (true)
26885 tree property, ivar;
26886 property = cp_parser_identifier (parser);
26887 if (property == error_mark_node)
26889 cp_parser_consume_semicolon_at_end_of_statement (parser);
26890 return;
26892 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26894 cp_lexer_consume_token (parser->lexer);
26895 ivar = cp_parser_identifier (parser);
26896 if (ivar == error_mark_node)
26898 cp_parser_consume_semicolon_at_end_of_statement (parser);
26899 return;
26902 else
26903 ivar = NULL_TREE;
26904 list = chainon (list, build_tree_list (ivar, property));
26905 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26906 cp_lexer_consume_token (parser->lexer);
26907 else
26908 break;
26910 cp_parser_consume_semicolon_at_end_of_statement (parser);
26911 objc_add_synthesize_declaration (loc, list);
26914 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26916 objc-dynamic-declaration:
26917 @dynamic identifier-list ;
26919 For example:
26920 @dynamic MyProperty;
26921 @dynamic MyProperty, AnotherProperty;
26923 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26924 for C. Keep them in sync.
26926 static void
26927 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26929 tree list = NULL_TREE;
26930 location_t loc;
26931 loc = cp_lexer_peek_token (parser->lexer)->location;
26933 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26934 while (true)
26936 tree property;
26937 property = cp_parser_identifier (parser);
26938 if (property == error_mark_node)
26940 cp_parser_consume_semicolon_at_end_of_statement (parser);
26941 return;
26943 list = chainon (list, build_tree_list (NULL, property));
26944 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26945 cp_lexer_consume_token (parser->lexer);
26946 else
26947 break;
26949 cp_parser_consume_semicolon_at_end_of_statement (parser);
26950 objc_add_dynamic_declaration (loc, list);
26954 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26956 /* Returns name of the next clause.
26957 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26958 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26959 returned and the token is consumed. */
26961 static pragma_omp_clause
26962 cp_parser_omp_clause_name (cp_parser *parser)
26964 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26967 result = PRAGMA_OMP_CLAUSE_IF;
26968 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26969 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26970 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26971 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26972 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26973 result = PRAGMA_OMP_CLAUSE_FOR;
26974 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26976 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26977 const char *p = IDENTIFIER_POINTER (id);
26979 switch (p[0])
26981 case 'a':
26982 if (!strcmp ("aligned", p))
26983 result = PRAGMA_OMP_CLAUSE_ALIGNED;
26984 break;
26985 case 'c':
26986 if (!strcmp ("collapse", p))
26987 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26988 else if (!strcmp ("copyin", p))
26989 result = PRAGMA_OMP_CLAUSE_COPYIN;
26990 else if (!strcmp ("copyprivate", p))
26991 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26992 break;
26993 case 'd':
26994 if (!strcmp ("depend", p))
26995 result = PRAGMA_OMP_CLAUSE_DEPEND;
26996 else if (!strcmp ("device", p))
26997 result = PRAGMA_OMP_CLAUSE_DEVICE;
26998 else if (!strcmp ("dist_schedule", p))
26999 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27000 break;
27001 case 'f':
27002 if (!strcmp ("final", p))
27003 result = PRAGMA_OMP_CLAUSE_FINAL;
27004 else if (!strcmp ("firstprivate", p))
27005 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27006 else if (!strcmp ("from", p))
27007 result = PRAGMA_OMP_CLAUSE_FROM;
27008 break;
27009 case 'i':
27010 if (!strcmp ("inbranch", p))
27011 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27012 break;
27013 case 'l':
27014 if (!strcmp ("lastprivate", p))
27015 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27016 else if (!strcmp ("linear", p))
27017 result = PRAGMA_OMP_CLAUSE_LINEAR;
27018 break;
27019 case 'm':
27020 if (!strcmp ("map", p))
27021 result = PRAGMA_OMP_CLAUSE_MAP;
27022 else if (!strcmp ("mergeable", p))
27023 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27024 else if (flag_cilkplus && !strcmp ("mask", p))
27025 result = PRAGMA_CILK_CLAUSE_MASK;
27026 break;
27027 case 'n':
27028 if (!strcmp ("notinbranch", p))
27029 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27030 else if (!strcmp ("nowait", p))
27031 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27032 else if (flag_cilkplus && !strcmp ("nomask", p))
27033 result = PRAGMA_CILK_CLAUSE_NOMASK;
27034 else if (!strcmp ("num_teams", p))
27035 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27036 else if (!strcmp ("num_threads", p))
27037 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27038 break;
27039 case 'o':
27040 if (!strcmp ("ordered", p))
27041 result = PRAGMA_OMP_CLAUSE_ORDERED;
27042 break;
27043 case 'p':
27044 if (!strcmp ("parallel", p))
27045 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27046 else if (!strcmp ("proc_bind", p))
27047 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27048 break;
27049 case 'r':
27050 if (!strcmp ("reduction", p))
27051 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27052 break;
27053 case 's':
27054 if (!strcmp ("safelen", p))
27055 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27056 else if (!strcmp ("schedule", p))
27057 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27058 else if (!strcmp ("sections", p))
27059 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27060 else if (!strcmp ("shared", p))
27061 result = PRAGMA_OMP_CLAUSE_SHARED;
27062 else if (!strcmp ("simdlen", p))
27063 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27064 break;
27065 case 't':
27066 if (!strcmp ("taskgroup", p))
27067 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27068 else if (!strcmp ("thread_limit", p))
27069 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27070 else if (!strcmp ("to", p))
27071 result = PRAGMA_OMP_CLAUSE_TO;
27072 break;
27073 case 'u':
27074 if (!strcmp ("uniform", p))
27075 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27076 else if (!strcmp ("untied", p))
27077 result = PRAGMA_OMP_CLAUSE_UNTIED;
27078 break;
27079 case 'v':
27080 if (flag_cilkplus && !strcmp ("vectorlength", p))
27081 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27082 break;
27086 if (result != PRAGMA_OMP_CLAUSE_NONE)
27087 cp_lexer_consume_token (parser->lexer);
27089 return result;
27092 /* Validate that a clause of the given type does not already exist. */
27094 static void
27095 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27096 const char *name, location_t location)
27098 tree c;
27100 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27101 if (OMP_CLAUSE_CODE (c) == code)
27103 error_at (location, "too many %qs clauses", name);
27104 break;
27108 /* OpenMP 2.5:
27109 variable-list:
27110 identifier
27111 variable-list , identifier
27113 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27114 colon). An opening parenthesis will have been consumed by the caller.
27116 If KIND is nonzero, create the appropriate node and install the decl
27117 in OMP_CLAUSE_DECL and add the node to the head of the list.
27119 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27120 return the list created.
27122 COLON can be NULL if only closing parenthesis should end the list,
27123 or pointer to bool which will receive false if the list is terminated
27124 by closing parenthesis or true if the list is terminated by colon. */
27126 static tree
27127 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27128 tree list, bool *colon)
27130 cp_token *token;
27131 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27132 if (colon)
27134 parser->colon_corrects_to_scope_p = false;
27135 *colon = false;
27137 while (1)
27139 tree name, decl;
27141 token = cp_lexer_peek_token (parser->lexer);
27142 name = cp_parser_id_expression (parser, /*template_p=*/false,
27143 /*check_dependency_p=*/true,
27144 /*template_p=*/NULL,
27145 /*declarator_p=*/false,
27146 /*optional_p=*/false);
27147 if (name == error_mark_node)
27148 goto skip_comma;
27150 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27151 if (decl == error_mark_node)
27152 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27153 token->location);
27154 else if (kind != 0)
27156 switch (kind)
27158 case OMP_CLAUSE_MAP:
27159 case OMP_CLAUSE_FROM:
27160 case OMP_CLAUSE_TO:
27161 case OMP_CLAUSE_DEPEND:
27162 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27164 tree low_bound = NULL_TREE, length = NULL_TREE;
27166 parser->colon_corrects_to_scope_p = false;
27167 cp_lexer_consume_token (parser->lexer);
27168 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27169 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27170 NULL);
27171 if (!colon)
27172 parser->colon_corrects_to_scope_p
27173 = saved_colon_corrects_to_scope_p;
27174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27175 length = integer_one_node;
27176 else
27178 /* Look for `:'. */
27179 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27180 goto skip_comma;
27181 if (!cp_lexer_next_token_is (parser->lexer,
27182 CPP_CLOSE_SQUARE))
27183 length = cp_parser_expression (parser,
27184 /*cast_p=*/false,
27185 NULL);
27187 /* Look for the closing `]'. */
27188 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27189 RT_CLOSE_SQUARE))
27190 goto skip_comma;
27191 decl = tree_cons (low_bound, length, decl);
27193 break;
27194 default:
27195 break;
27198 tree u = build_omp_clause (token->location, kind);
27199 OMP_CLAUSE_DECL (u) = decl;
27200 OMP_CLAUSE_CHAIN (u) = list;
27201 list = u;
27203 else
27204 list = tree_cons (decl, NULL_TREE, list);
27206 get_comma:
27207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27208 break;
27209 cp_lexer_consume_token (parser->lexer);
27212 if (colon)
27213 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27215 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27217 *colon = true;
27218 cp_parser_require (parser, CPP_COLON, RT_COLON);
27219 return list;
27222 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27224 int ending;
27226 /* Try to resync to an unnested comma. Copied from
27227 cp_parser_parenthesized_expression_list. */
27228 skip_comma:
27229 if (colon)
27230 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27231 ending = cp_parser_skip_to_closing_parenthesis (parser,
27232 /*recovering=*/true,
27233 /*or_comma=*/true,
27234 /*consume_paren=*/true);
27235 if (ending < 0)
27236 goto get_comma;
27239 return list;
27242 /* Similarly, but expect leading and trailing parenthesis. This is a very
27243 common case for omp clauses. */
27245 static tree
27246 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27248 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27249 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27250 return list;
27253 /* OpenMP 3.0:
27254 collapse ( constant-expression ) */
27256 static tree
27257 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27259 tree c, num;
27260 location_t loc;
27261 HOST_WIDE_INT n;
27263 loc = cp_lexer_peek_token (parser->lexer)->location;
27264 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27265 return list;
27267 num = cp_parser_constant_expression (parser, false, NULL);
27269 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27270 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27271 /*or_comma=*/false,
27272 /*consume_paren=*/true);
27274 if (num == error_mark_node)
27275 return list;
27276 num = fold_non_dependent_expr (num);
27277 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27278 || !tree_fits_shwi_p (num)
27279 || (n = tree_to_shwi (num)) <= 0
27280 || (int) n != n)
27282 error_at (loc, "collapse argument needs positive constant integer expression");
27283 return list;
27286 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27287 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27288 OMP_CLAUSE_CHAIN (c) = list;
27289 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27291 return c;
27294 /* OpenMP 2.5:
27295 default ( shared | none ) */
27297 static tree
27298 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27300 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27301 tree c;
27303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27304 return list;
27305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27307 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27308 const char *p = IDENTIFIER_POINTER (id);
27310 switch (p[0])
27312 case 'n':
27313 if (strcmp ("none", p) != 0)
27314 goto invalid_kind;
27315 kind = OMP_CLAUSE_DEFAULT_NONE;
27316 break;
27318 case 's':
27319 if (strcmp ("shared", p) != 0)
27320 goto invalid_kind;
27321 kind = OMP_CLAUSE_DEFAULT_SHARED;
27322 break;
27324 default:
27325 goto invalid_kind;
27328 cp_lexer_consume_token (parser->lexer);
27330 else
27332 invalid_kind:
27333 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27337 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27338 /*or_comma=*/false,
27339 /*consume_paren=*/true);
27341 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27342 return list;
27344 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27345 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27346 OMP_CLAUSE_CHAIN (c) = list;
27347 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27349 return c;
27352 /* OpenMP 3.1:
27353 final ( expression ) */
27355 static tree
27356 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27358 tree t, c;
27360 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27361 return list;
27363 t = cp_parser_condition (parser);
27365 if (t == error_mark_node
27366 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27368 /*or_comma=*/false,
27369 /*consume_paren=*/true);
27371 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27373 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27374 OMP_CLAUSE_FINAL_EXPR (c) = t;
27375 OMP_CLAUSE_CHAIN (c) = list;
27377 return c;
27380 /* OpenMP 2.5:
27381 if ( expression ) */
27383 static tree
27384 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27386 tree t, c;
27388 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27389 return list;
27391 t = cp_parser_condition (parser);
27393 if (t == error_mark_node
27394 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27396 /*or_comma=*/false,
27397 /*consume_paren=*/true);
27399 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27401 c = build_omp_clause (location, OMP_CLAUSE_IF);
27402 OMP_CLAUSE_IF_EXPR (c) = t;
27403 OMP_CLAUSE_CHAIN (c) = list;
27405 return c;
27408 /* OpenMP 3.1:
27409 mergeable */
27411 static tree
27412 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27413 tree list, location_t location)
27415 tree c;
27417 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27418 location);
27420 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27421 OMP_CLAUSE_CHAIN (c) = list;
27422 return c;
27425 /* OpenMP 2.5:
27426 nowait */
27428 static tree
27429 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27430 tree list, location_t location)
27432 tree c;
27434 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27436 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27437 OMP_CLAUSE_CHAIN (c) = list;
27438 return c;
27441 /* OpenMP 2.5:
27442 num_threads ( expression ) */
27444 static tree
27445 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27446 location_t location)
27448 tree t, c;
27450 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27451 return list;
27453 t = cp_parser_expression (parser, false, NULL);
27455 if (t == error_mark_node
27456 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27457 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27458 /*or_comma=*/false,
27459 /*consume_paren=*/true);
27461 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27462 "num_threads", location);
27464 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27465 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27466 OMP_CLAUSE_CHAIN (c) = list;
27468 return c;
27471 /* OpenMP 2.5:
27472 ordered */
27474 static tree
27475 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27476 tree list, location_t location)
27478 tree c;
27480 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27481 "ordered", location);
27483 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27484 OMP_CLAUSE_CHAIN (c) = list;
27485 return c;
27488 /* OpenMP 2.5:
27489 reduction ( reduction-operator : variable-list )
27491 reduction-operator:
27492 One of: + * - & ^ | && ||
27494 OpenMP 3.1:
27496 reduction-operator:
27497 One of: + * - & ^ | && || min max
27499 OpenMP 4.0:
27501 reduction-operator:
27502 One of: + * - & ^ | && ||
27503 id-expression */
27505 static tree
27506 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27508 enum tree_code code = ERROR_MARK;
27509 tree nlist, c, id = NULL_TREE;
27511 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27512 return list;
27514 switch (cp_lexer_peek_token (parser->lexer)->type)
27516 case CPP_PLUS: code = PLUS_EXPR; break;
27517 case CPP_MULT: code = MULT_EXPR; break;
27518 case CPP_MINUS: code = MINUS_EXPR; break;
27519 case CPP_AND: code = BIT_AND_EXPR; break;
27520 case CPP_XOR: code = BIT_XOR_EXPR; break;
27521 case CPP_OR: code = BIT_IOR_EXPR; break;
27522 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27523 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27524 default: break;
27527 if (code != ERROR_MARK)
27528 cp_lexer_consume_token (parser->lexer);
27529 else
27531 bool saved_colon_corrects_to_scope_p;
27532 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27533 parser->colon_corrects_to_scope_p = false;
27534 id = cp_parser_id_expression (parser, /*template_p=*/false,
27535 /*check_dependency_p=*/true,
27536 /*template_p=*/NULL,
27537 /*declarator_p=*/false,
27538 /*optional_p=*/false);
27539 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27540 if (identifier_p (id))
27542 const char *p = IDENTIFIER_POINTER (id);
27544 if (strcmp (p, "min") == 0)
27545 code = MIN_EXPR;
27546 else if (strcmp (p, "max") == 0)
27547 code = MAX_EXPR;
27548 else if (id == ansi_opname (PLUS_EXPR))
27549 code = PLUS_EXPR;
27550 else if (id == ansi_opname (MULT_EXPR))
27551 code = MULT_EXPR;
27552 else if (id == ansi_opname (MINUS_EXPR))
27553 code = MINUS_EXPR;
27554 else if (id == ansi_opname (BIT_AND_EXPR))
27555 code = BIT_AND_EXPR;
27556 else if (id == ansi_opname (BIT_IOR_EXPR))
27557 code = BIT_IOR_EXPR;
27558 else if (id == ansi_opname (BIT_XOR_EXPR))
27559 code = BIT_XOR_EXPR;
27560 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27561 code = TRUTH_ANDIF_EXPR;
27562 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27563 code = TRUTH_ORIF_EXPR;
27564 id = omp_reduction_id (code, id, NULL_TREE);
27565 tree scope = parser->scope;
27566 if (scope)
27567 id = build_qualified_name (NULL_TREE, scope, id, false);
27568 parser->scope = NULL_TREE;
27569 parser->qualifying_scope = NULL_TREE;
27570 parser->object_scope = NULL_TREE;
27572 else
27574 error ("invalid reduction-identifier");
27575 resync_fail:
27576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27577 /*or_comma=*/false,
27578 /*consume_paren=*/true);
27579 return list;
27583 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27584 goto resync_fail;
27586 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27587 NULL);
27588 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27590 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27591 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27594 return nlist;
27597 /* OpenMP 2.5:
27598 schedule ( schedule-kind )
27599 schedule ( schedule-kind , expression )
27601 schedule-kind:
27602 static | dynamic | guided | runtime | auto */
27604 static tree
27605 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27607 tree c, t;
27609 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27610 return list;
27612 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27614 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27616 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27617 const char *p = IDENTIFIER_POINTER (id);
27619 switch (p[0])
27621 case 'd':
27622 if (strcmp ("dynamic", p) != 0)
27623 goto invalid_kind;
27624 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27625 break;
27627 case 'g':
27628 if (strcmp ("guided", p) != 0)
27629 goto invalid_kind;
27630 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27631 break;
27633 case 'r':
27634 if (strcmp ("runtime", p) != 0)
27635 goto invalid_kind;
27636 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27637 break;
27639 default:
27640 goto invalid_kind;
27643 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27644 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27645 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27646 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27647 else
27648 goto invalid_kind;
27649 cp_lexer_consume_token (parser->lexer);
27651 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27653 cp_token *token;
27654 cp_lexer_consume_token (parser->lexer);
27656 token = cp_lexer_peek_token (parser->lexer);
27657 t = cp_parser_assignment_expression (parser, false, NULL);
27659 if (t == error_mark_node)
27660 goto resync_fail;
27661 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27662 error_at (token->location, "schedule %<runtime%> does not take "
27663 "a %<chunk_size%> parameter");
27664 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27665 error_at (token->location, "schedule %<auto%> does not take "
27666 "a %<chunk_size%> parameter");
27667 else
27668 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27671 goto resync_fail;
27673 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27674 goto resync_fail;
27676 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27677 OMP_CLAUSE_CHAIN (c) = list;
27678 return c;
27680 invalid_kind:
27681 cp_parser_error (parser, "invalid schedule kind");
27682 resync_fail:
27683 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27684 /*or_comma=*/false,
27685 /*consume_paren=*/true);
27686 return list;
27689 /* OpenMP 3.0:
27690 untied */
27692 static tree
27693 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27694 tree list, location_t location)
27696 tree c;
27698 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27700 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27701 OMP_CLAUSE_CHAIN (c) = list;
27702 return c;
27705 /* OpenMP 4.0:
27706 inbranch
27707 notinbranch */
27709 static tree
27710 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27711 tree list, location_t location)
27713 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27714 tree c = build_omp_clause (location, code);
27715 OMP_CLAUSE_CHAIN (c) = list;
27716 return c;
27719 /* OpenMP 4.0:
27720 parallel
27722 sections
27723 taskgroup */
27725 static tree
27726 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27727 enum omp_clause_code code,
27728 tree list, location_t location)
27730 tree c = build_omp_clause (location, code);
27731 OMP_CLAUSE_CHAIN (c) = list;
27732 return c;
27735 /* OpenMP 4.0:
27736 num_teams ( expression ) */
27738 static tree
27739 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27740 location_t location)
27742 tree t, c;
27744 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27745 return list;
27747 t = cp_parser_expression (parser, false, NULL);
27749 if (t == error_mark_node
27750 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27751 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27752 /*or_comma=*/false,
27753 /*consume_paren=*/true);
27755 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27756 "num_teams", location);
27758 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27759 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27760 OMP_CLAUSE_CHAIN (c) = list;
27762 return c;
27765 /* OpenMP 4.0:
27766 thread_limit ( expression ) */
27768 static tree
27769 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27770 location_t location)
27772 tree t, c;
27774 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27775 return list;
27777 t = cp_parser_expression (parser, false, NULL);
27779 if (t == error_mark_node
27780 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27781 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27782 /*or_comma=*/false,
27783 /*consume_paren=*/true);
27785 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27786 "thread_limit", location);
27788 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27789 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27790 OMP_CLAUSE_CHAIN (c) = list;
27792 return c;
27795 /* OpenMP 4.0:
27796 aligned ( variable-list )
27797 aligned ( variable-list : constant-expression ) */
27799 static tree
27800 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27802 tree nlist, c, alignment = NULL_TREE;
27803 bool colon;
27805 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27806 return list;
27808 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27809 &colon);
27811 if (colon)
27813 alignment = cp_parser_constant_expression (parser, false, NULL);
27815 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27816 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27817 /*or_comma=*/false,
27818 /*consume_paren=*/true);
27820 if (alignment == error_mark_node)
27821 alignment = NULL_TREE;
27824 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27825 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27827 return nlist;
27830 /* OpenMP 4.0:
27831 linear ( variable-list )
27832 linear ( variable-list : expression ) */
27834 static tree
27835 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
27836 bool is_cilk_simd_fn)
27838 tree nlist, c, step = integer_one_node;
27839 bool colon;
27841 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27842 return list;
27844 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27845 &colon);
27847 if (colon)
27849 step = cp_parser_expression (parser, false, NULL);
27851 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
27853 sorry ("using parameters for %<linear%> step is not supported yet");
27854 step = integer_one_node;
27856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27858 /*or_comma=*/false,
27859 /*consume_paren=*/true);
27861 if (step == error_mark_node)
27862 return list;
27865 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27866 OMP_CLAUSE_LINEAR_STEP (c) = step;
27868 return nlist;
27871 /* OpenMP 4.0:
27872 safelen ( constant-expression ) */
27874 static tree
27875 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27876 location_t location)
27878 tree t, c;
27880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27881 return list;
27883 t = cp_parser_constant_expression (parser, false, NULL);
27885 if (t == error_mark_node
27886 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27887 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27888 /*or_comma=*/false,
27889 /*consume_paren=*/true);
27891 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27893 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27894 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27895 OMP_CLAUSE_CHAIN (c) = list;
27897 return c;
27900 /* OpenMP 4.0:
27901 simdlen ( constant-expression ) */
27903 static tree
27904 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27905 location_t location)
27907 tree t, c;
27909 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27910 return list;
27912 t = cp_parser_constant_expression (parser, false, NULL);
27914 if (t == error_mark_node
27915 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27916 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27917 /*or_comma=*/false,
27918 /*consume_paren=*/true);
27920 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27922 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27923 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27924 OMP_CLAUSE_CHAIN (c) = list;
27926 return c;
27929 /* OpenMP 4.0:
27930 depend ( depend-kind : variable-list )
27932 depend-kind:
27933 in | out | inout */
27935 static tree
27936 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27938 tree nlist, c;
27939 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27941 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27942 return list;
27944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27946 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27947 const char *p = IDENTIFIER_POINTER (id);
27949 if (strcmp ("in", p) == 0)
27950 kind = OMP_CLAUSE_DEPEND_IN;
27951 else if (strcmp ("inout", p) == 0)
27952 kind = OMP_CLAUSE_DEPEND_INOUT;
27953 else if (strcmp ("out", p) == 0)
27954 kind = OMP_CLAUSE_DEPEND_OUT;
27955 else
27956 goto invalid_kind;
27958 else
27959 goto invalid_kind;
27961 cp_lexer_consume_token (parser->lexer);
27962 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27963 goto resync_fail;
27965 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27966 NULL);
27968 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27969 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27971 return nlist;
27973 invalid_kind:
27974 cp_parser_error (parser, "invalid depend kind");
27975 resync_fail:
27976 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27977 /*or_comma=*/false,
27978 /*consume_paren=*/true);
27979 return list;
27982 /* OpenMP 4.0:
27983 map ( map-kind : variable-list )
27984 map ( variable-list )
27986 map-kind:
27987 alloc | to | from | tofrom */
27989 static tree
27990 cp_parser_omp_clause_map (cp_parser *parser, tree list)
27992 tree nlist, c;
27993 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
27995 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27996 return list;
27998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
27999 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28001 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28002 const char *p = IDENTIFIER_POINTER (id);
28004 if (strcmp ("alloc", p) == 0)
28005 kind = OMP_CLAUSE_MAP_ALLOC;
28006 else if (strcmp ("to", p) == 0)
28007 kind = OMP_CLAUSE_MAP_TO;
28008 else if (strcmp ("from", p) == 0)
28009 kind = OMP_CLAUSE_MAP_FROM;
28010 else if (strcmp ("tofrom", p) == 0)
28011 kind = OMP_CLAUSE_MAP_TOFROM;
28012 else
28014 cp_parser_error (parser, "invalid map kind");
28015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28016 /*or_comma=*/false,
28017 /*consume_paren=*/true);
28018 return list;
28020 cp_lexer_consume_token (parser->lexer);
28021 cp_lexer_consume_token (parser->lexer);
28024 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28025 NULL);
28027 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28028 OMP_CLAUSE_MAP_KIND (c) = kind;
28030 return nlist;
28033 /* OpenMP 4.0:
28034 device ( expression ) */
28036 static tree
28037 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28038 location_t location)
28040 tree t, c;
28042 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28043 return list;
28045 t = cp_parser_expression (parser, false, NULL);
28047 if (t == error_mark_node
28048 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28049 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28050 /*or_comma=*/false,
28051 /*consume_paren=*/true);
28053 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28054 "device", location);
28056 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28057 OMP_CLAUSE_DEVICE_ID (c) = t;
28058 OMP_CLAUSE_CHAIN (c) = list;
28060 return c;
28063 /* OpenMP 4.0:
28064 dist_schedule ( static )
28065 dist_schedule ( static , expression ) */
28067 static tree
28068 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28069 location_t location)
28071 tree c, t;
28073 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28074 return list;
28076 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28078 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28079 goto invalid_kind;
28080 cp_lexer_consume_token (parser->lexer);
28082 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28084 cp_lexer_consume_token (parser->lexer);
28086 t = cp_parser_assignment_expression (parser, false, NULL);
28088 if (t == error_mark_node)
28089 goto resync_fail;
28090 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28092 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28093 goto resync_fail;
28095 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28096 goto resync_fail;
28098 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28099 location);
28100 OMP_CLAUSE_CHAIN (c) = list;
28101 return c;
28103 invalid_kind:
28104 cp_parser_error (parser, "invalid dist_schedule kind");
28105 resync_fail:
28106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28107 /*or_comma=*/false,
28108 /*consume_paren=*/true);
28109 return list;
28112 /* OpenMP 4.0:
28113 proc_bind ( proc-bind-kind )
28115 proc-bind-kind:
28116 master | close | spread */
28118 static tree
28119 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28120 location_t location)
28122 tree c;
28123 enum omp_clause_proc_bind_kind kind;
28125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28126 return list;
28128 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28130 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28131 const char *p = IDENTIFIER_POINTER (id);
28133 if (strcmp ("master", p) == 0)
28134 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28135 else if (strcmp ("close", p) == 0)
28136 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28137 else if (strcmp ("spread", p) == 0)
28138 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28139 else
28140 goto invalid_kind;
28142 else
28143 goto invalid_kind;
28145 cp_lexer_consume_token (parser->lexer);
28146 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28147 goto resync_fail;
28149 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28150 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28151 location);
28152 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28153 OMP_CLAUSE_CHAIN (c) = list;
28154 return c;
28156 invalid_kind:
28157 cp_parser_error (parser, "invalid depend kind");
28158 resync_fail:
28159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28160 /*or_comma=*/false,
28161 /*consume_paren=*/true);
28162 return list;
28165 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28166 is a bitmask in MASK. Return the list of clauses found; the result
28167 of clause default goes in *pdefault. */
28169 static tree
28170 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28171 const char *where, cp_token *pragma_tok,
28172 bool finish_p = true)
28174 tree clauses = NULL;
28175 bool first = true;
28176 cp_token *token = NULL;
28177 bool cilk_simd_fn = false;
28179 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28181 pragma_omp_clause c_kind;
28182 const char *c_name;
28183 tree prev = clauses;
28185 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28186 cp_lexer_consume_token (parser->lexer);
28188 token = cp_lexer_peek_token (parser->lexer);
28189 c_kind = cp_parser_omp_clause_name (parser);
28191 switch (c_kind)
28193 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28194 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28195 token->location);
28196 c_name = "collapse";
28197 break;
28198 case PRAGMA_OMP_CLAUSE_COPYIN:
28199 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28200 c_name = "copyin";
28201 break;
28202 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28203 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28204 clauses);
28205 c_name = "copyprivate";
28206 break;
28207 case PRAGMA_OMP_CLAUSE_DEFAULT:
28208 clauses = cp_parser_omp_clause_default (parser, clauses,
28209 token->location);
28210 c_name = "default";
28211 break;
28212 case PRAGMA_OMP_CLAUSE_FINAL:
28213 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28214 c_name = "final";
28215 break;
28216 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28217 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28218 clauses);
28219 c_name = "firstprivate";
28220 break;
28221 case PRAGMA_OMP_CLAUSE_IF:
28222 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28223 c_name = "if";
28224 break;
28225 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28227 clauses);
28228 c_name = "lastprivate";
28229 break;
28230 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28231 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28232 token->location);
28233 c_name = "mergeable";
28234 break;
28235 case PRAGMA_OMP_CLAUSE_NOWAIT:
28236 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28237 c_name = "nowait";
28238 break;
28239 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28240 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28241 token->location);
28242 c_name = "num_threads";
28243 break;
28244 case PRAGMA_OMP_CLAUSE_ORDERED:
28245 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28246 token->location);
28247 c_name = "ordered";
28248 break;
28249 case PRAGMA_OMP_CLAUSE_PRIVATE:
28250 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28251 clauses);
28252 c_name = "private";
28253 break;
28254 case PRAGMA_OMP_CLAUSE_REDUCTION:
28255 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28256 c_name = "reduction";
28257 break;
28258 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28259 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28260 token->location);
28261 c_name = "schedule";
28262 break;
28263 case PRAGMA_OMP_CLAUSE_SHARED:
28264 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28265 clauses);
28266 c_name = "shared";
28267 break;
28268 case PRAGMA_OMP_CLAUSE_UNTIED:
28269 clauses = cp_parser_omp_clause_untied (parser, clauses,
28270 token->location);
28271 c_name = "untied";
28272 break;
28273 case PRAGMA_OMP_CLAUSE_INBRANCH:
28274 case PRAGMA_CILK_CLAUSE_MASK:
28275 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28276 clauses, token->location);
28277 c_name = "inbranch";
28278 break;
28279 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28280 case PRAGMA_CILK_CLAUSE_NOMASK:
28281 clauses = cp_parser_omp_clause_branch (parser,
28282 OMP_CLAUSE_NOTINBRANCH,
28283 clauses, token->location);
28284 c_name = "notinbranch";
28285 break;
28286 case PRAGMA_OMP_CLAUSE_PARALLEL:
28287 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28288 clauses, token->location);
28289 c_name = "parallel";
28290 if (!first)
28292 clause_not_first:
28293 error_at (token->location, "%qs must be the first clause of %qs",
28294 c_name, where);
28295 clauses = prev;
28297 break;
28298 case PRAGMA_OMP_CLAUSE_FOR:
28299 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28300 clauses, token->location);
28301 c_name = "for";
28302 if (!first)
28303 goto clause_not_first;
28304 break;
28305 case PRAGMA_OMP_CLAUSE_SECTIONS:
28306 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28307 clauses, token->location);
28308 c_name = "sections";
28309 if (!first)
28310 goto clause_not_first;
28311 break;
28312 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28313 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28314 clauses, token->location);
28315 c_name = "taskgroup";
28316 if (!first)
28317 goto clause_not_first;
28318 break;
28319 case PRAGMA_OMP_CLAUSE_TO:
28320 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28321 clauses);
28322 c_name = "to";
28323 break;
28324 case PRAGMA_OMP_CLAUSE_FROM:
28325 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28326 clauses);
28327 c_name = "from";
28328 break;
28329 case PRAGMA_OMP_CLAUSE_UNIFORM:
28330 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28331 clauses);
28332 c_name = "uniform";
28333 break;
28334 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28335 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28336 token->location);
28337 c_name = "num_teams";
28338 break;
28339 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28340 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28341 token->location);
28342 c_name = "thread_limit";
28343 break;
28344 case PRAGMA_OMP_CLAUSE_ALIGNED:
28345 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28346 c_name = "aligned";
28347 break;
28348 case PRAGMA_OMP_CLAUSE_LINEAR:
28349 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28350 cilk_simd_fn = true;
28351 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28352 c_name = "linear";
28353 break;
28354 case PRAGMA_OMP_CLAUSE_DEPEND:
28355 clauses = cp_parser_omp_clause_depend (parser, clauses);
28356 c_name = "depend";
28357 break;
28358 case PRAGMA_OMP_CLAUSE_MAP:
28359 clauses = cp_parser_omp_clause_map (parser, clauses);
28360 c_name = "map";
28361 break;
28362 case PRAGMA_OMP_CLAUSE_DEVICE:
28363 clauses = cp_parser_omp_clause_device (parser, clauses,
28364 token->location);
28365 c_name = "device";
28366 break;
28367 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28368 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28369 token->location);
28370 c_name = "dist_schedule";
28371 break;
28372 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28373 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28374 token->location);
28375 c_name = "proc_bind";
28376 break;
28377 case PRAGMA_OMP_CLAUSE_SAFELEN:
28378 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28379 token->location);
28380 c_name = "safelen";
28381 break;
28382 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28383 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28384 token->location);
28385 c_name = "simdlen";
28386 break;
28387 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28388 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28389 c_name = "simdlen";
28390 break;
28391 default:
28392 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28393 goto saw_error;
28396 first = false;
28398 if (((mask >> c_kind) & 1) == 0)
28400 /* Remove the invalid clause(s) from the list to avoid
28401 confusing the rest of the compiler. */
28402 clauses = prev;
28403 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28406 saw_error:
28407 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28408 no reason to skip to the end. */
28409 if (!(flag_cilkplus && pragma_tok == NULL))
28410 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28411 if (finish_p)
28412 return finish_omp_clauses (clauses);
28413 return clauses;
28416 /* OpenMP 2.5:
28417 structured-block:
28418 statement
28420 In practice, we're also interested in adding the statement to an
28421 outer node. So it is convenient if we work around the fact that
28422 cp_parser_statement calls add_stmt. */
28424 static unsigned
28425 cp_parser_begin_omp_structured_block (cp_parser *parser)
28427 unsigned save = parser->in_statement;
28429 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28430 This preserves the "not within loop or switch" style error messages
28431 for nonsense cases like
28432 void foo() {
28433 #pragma omp single
28434 break;
28437 if (parser->in_statement)
28438 parser->in_statement = IN_OMP_BLOCK;
28440 return save;
28443 static void
28444 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28446 parser->in_statement = save;
28449 static tree
28450 cp_parser_omp_structured_block (cp_parser *parser)
28452 tree stmt = begin_omp_structured_block ();
28453 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28455 cp_parser_statement (parser, NULL_TREE, false, NULL);
28457 cp_parser_end_omp_structured_block (parser, save);
28458 return finish_omp_structured_block (stmt);
28461 /* OpenMP 2.5:
28462 # pragma omp atomic new-line
28463 expression-stmt
28465 expression-stmt:
28466 x binop= expr | x++ | ++x | x-- | --x
28467 binop:
28468 +, *, -, /, &, ^, |, <<, >>
28470 where x is an lvalue expression with scalar type.
28472 OpenMP 3.1:
28473 # pragma omp atomic new-line
28474 update-stmt
28476 # pragma omp atomic read new-line
28477 read-stmt
28479 # pragma omp atomic write new-line
28480 write-stmt
28482 # pragma omp atomic update new-line
28483 update-stmt
28485 # pragma omp atomic capture new-line
28486 capture-stmt
28488 # pragma omp atomic capture new-line
28489 capture-block
28491 read-stmt:
28492 v = x
28493 write-stmt:
28494 x = expr
28495 update-stmt:
28496 expression-stmt | x = x binop expr
28497 capture-stmt:
28498 v = expression-stmt
28499 capture-block:
28500 { v = x; update-stmt; } | { update-stmt; v = x; }
28502 OpenMP 4.0:
28503 update-stmt:
28504 expression-stmt | x = x binop expr | x = expr binop x
28505 capture-stmt:
28506 v = update-stmt
28507 capture-block:
28508 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28510 where x and v are lvalue expressions with scalar type. */
28512 static void
28513 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28515 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28516 tree rhs1 = NULL_TREE, orig_lhs;
28517 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28518 bool structured_block = false;
28519 bool seq_cst = false;
28521 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28523 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28524 const char *p = IDENTIFIER_POINTER (id);
28526 if (!strcmp (p, "read"))
28527 code = OMP_ATOMIC_READ;
28528 else if (!strcmp (p, "write"))
28529 code = NOP_EXPR;
28530 else if (!strcmp (p, "update"))
28531 code = OMP_ATOMIC;
28532 else if (!strcmp (p, "capture"))
28533 code = OMP_ATOMIC_CAPTURE_NEW;
28534 else
28535 p = NULL;
28536 if (p)
28537 cp_lexer_consume_token (parser->lexer);
28540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28543 const char *p = IDENTIFIER_POINTER (id);
28545 if (!strcmp (p, "seq_cst"))
28547 seq_cst = true;
28548 cp_lexer_consume_token (parser->lexer);
28551 cp_parser_require_pragma_eol (parser, pragma_tok);
28553 switch (code)
28555 case OMP_ATOMIC_READ:
28556 case NOP_EXPR: /* atomic write */
28557 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28558 /*cast_p=*/false, NULL);
28559 if (v == error_mark_node)
28560 goto saw_error;
28561 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28562 goto saw_error;
28563 if (code == NOP_EXPR)
28564 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28565 else
28566 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28567 /*cast_p=*/false, NULL);
28568 if (lhs == error_mark_node)
28569 goto saw_error;
28570 if (code == NOP_EXPR)
28572 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28573 opcode. */
28574 code = OMP_ATOMIC;
28575 rhs = lhs;
28576 lhs = v;
28577 v = NULL_TREE;
28579 goto done;
28580 case OMP_ATOMIC_CAPTURE_NEW:
28581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28583 cp_lexer_consume_token (parser->lexer);
28584 structured_block = true;
28586 else
28588 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28589 /*cast_p=*/false, NULL);
28590 if (v == error_mark_node)
28591 goto saw_error;
28592 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28593 goto saw_error;
28595 default:
28596 break;
28599 restart:
28600 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28601 /*cast_p=*/false, NULL);
28602 orig_lhs = lhs;
28603 switch (TREE_CODE (lhs))
28605 case ERROR_MARK:
28606 goto saw_error;
28608 case POSTINCREMENT_EXPR:
28609 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28610 code = OMP_ATOMIC_CAPTURE_OLD;
28611 /* FALLTHROUGH */
28612 case PREINCREMENT_EXPR:
28613 lhs = TREE_OPERAND (lhs, 0);
28614 opcode = PLUS_EXPR;
28615 rhs = integer_one_node;
28616 break;
28618 case POSTDECREMENT_EXPR:
28619 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28620 code = OMP_ATOMIC_CAPTURE_OLD;
28621 /* FALLTHROUGH */
28622 case PREDECREMENT_EXPR:
28623 lhs = TREE_OPERAND (lhs, 0);
28624 opcode = MINUS_EXPR;
28625 rhs = integer_one_node;
28626 break;
28628 case COMPOUND_EXPR:
28629 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28630 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28631 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28632 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28633 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28634 (TREE_OPERAND (lhs, 1), 0), 0)))
28635 == BOOLEAN_TYPE)
28636 /* Undo effects of boolean_increment for post {in,de}crement. */
28637 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28638 /* FALLTHRU */
28639 case MODIFY_EXPR:
28640 if (TREE_CODE (lhs) == MODIFY_EXPR
28641 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28643 /* Undo effects of boolean_increment. */
28644 if (integer_onep (TREE_OPERAND (lhs, 1)))
28646 /* This is pre or post increment. */
28647 rhs = TREE_OPERAND (lhs, 1);
28648 lhs = TREE_OPERAND (lhs, 0);
28649 opcode = NOP_EXPR;
28650 if (code == OMP_ATOMIC_CAPTURE_NEW
28651 && !structured_block
28652 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28653 code = OMP_ATOMIC_CAPTURE_OLD;
28654 break;
28657 /* FALLTHRU */
28658 default:
28659 switch (cp_lexer_peek_token (parser->lexer)->type)
28661 case CPP_MULT_EQ:
28662 opcode = MULT_EXPR;
28663 break;
28664 case CPP_DIV_EQ:
28665 opcode = TRUNC_DIV_EXPR;
28666 break;
28667 case CPP_PLUS_EQ:
28668 opcode = PLUS_EXPR;
28669 break;
28670 case CPP_MINUS_EQ:
28671 opcode = MINUS_EXPR;
28672 break;
28673 case CPP_LSHIFT_EQ:
28674 opcode = LSHIFT_EXPR;
28675 break;
28676 case CPP_RSHIFT_EQ:
28677 opcode = RSHIFT_EXPR;
28678 break;
28679 case CPP_AND_EQ:
28680 opcode = BIT_AND_EXPR;
28681 break;
28682 case CPP_OR_EQ:
28683 opcode = BIT_IOR_EXPR;
28684 break;
28685 case CPP_XOR_EQ:
28686 opcode = BIT_XOR_EXPR;
28687 break;
28688 case CPP_EQ:
28689 enum cp_parser_prec oprec;
28690 cp_token *token;
28691 cp_lexer_consume_token (parser->lexer);
28692 cp_parser_parse_tentatively (parser);
28693 rhs1 = cp_parser_simple_cast_expression (parser);
28694 if (rhs1 == error_mark_node)
28696 cp_parser_abort_tentative_parse (parser);
28697 cp_parser_simple_cast_expression (parser);
28698 goto saw_error;
28700 token = cp_lexer_peek_token (parser->lexer);
28701 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28703 cp_parser_abort_tentative_parse (parser);
28704 cp_parser_parse_tentatively (parser);
28705 rhs = cp_parser_binary_expression (parser, false, true,
28706 PREC_NOT_OPERATOR, NULL);
28707 if (rhs == error_mark_node)
28709 cp_parser_abort_tentative_parse (parser);
28710 cp_parser_binary_expression (parser, false, true,
28711 PREC_NOT_OPERATOR, NULL);
28712 goto saw_error;
28714 switch (TREE_CODE (rhs))
28716 case MULT_EXPR:
28717 case TRUNC_DIV_EXPR:
28718 case PLUS_EXPR:
28719 case MINUS_EXPR:
28720 case LSHIFT_EXPR:
28721 case RSHIFT_EXPR:
28722 case BIT_AND_EXPR:
28723 case BIT_IOR_EXPR:
28724 case BIT_XOR_EXPR:
28725 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28727 if (cp_parser_parse_definitely (parser))
28729 opcode = TREE_CODE (rhs);
28730 rhs1 = TREE_OPERAND (rhs, 0);
28731 rhs = TREE_OPERAND (rhs, 1);
28732 goto stmt_done;
28734 else
28735 goto saw_error;
28737 break;
28738 default:
28739 break;
28741 cp_parser_abort_tentative_parse (parser);
28742 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28744 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28745 if (rhs == error_mark_node)
28746 goto saw_error;
28747 opcode = NOP_EXPR;
28748 rhs1 = NULL_TREE;
28749 goto stmt_done;
28751 cp_parser_error (parser,
28752 "invalid form of %<#pragma omp atomic%>");
28753 goto saw_error;
28755 if (!cp_parser_parse_definitely (parser))
28756 goto saw_error;
28757 switch (token->type)
28759 case CPP_SEMICOLON:
28760 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28762 code = OMP_ATOMIC_CAPTURE_OLD;
28763 v = lhs;
28764 lhs = NULL_TREE;
28765 lhs1 = rhs1;
28766 rhs1 = NULL_TREE;
28767 cp_lexer_consume_token (parser->lexer);
28768 goto restart;
28770 else if (structured_block)
28772 opcode = NOP_EXPR;
28773 rhs = rhs1;
28774 rhs1 = NULL_TREE;
28775 goto stmt_done;
28777 cp_parser_error (parser,
28778 "invalid form of %<#pragma omp atomic%>");
28779 goto saw_error;
28780 case CPP_MULT:
28781 opcode = MULT_EXPR;
28782 break;
28783 case CPP_DIV:
28784 opcode = TRUNC_DIV_EXPR;
28785 break;
28786 case CPP_PLUS:
28787 opcode = PLUS_EXPR;
28788 break;
28789 case CPP_MINUS:
28790 opcode = MINUS_EXPR;
28791 break;
28792 case CPP_LSHIFT:
28793 opcode = LSHIFT_EXPR;
28794 break;
28795 case CPP_RSHIFT:
28796 opcode = RSHIFT_EXPR;
28797 break;
28798 case CPP_AND:
28799 opcode = BIT_AND_EXPR;
28800 break;
28801 case CPP_OR:
28802 opcode = BIT_IOR_EXPR;
28803 break;
28804 case CPP_XOR:
28805 opcode = BIT_XOR_EXPR;
28806 break;
28807 default:
28808 cp_parser_error (parser,
28809 "invalid operator for %<#pragma omp atomic%>");
28810 goto saw_error;
28812 oprec = TOKEN_PRECEDENCE (token);
28813 gcc_assert (oprec != PREC_NOT_OPERATOR);
28814 if (commutative_tree_code (opcode))
28815 oprec = (enum cp_parser_prec) (oprec - 1);
28816 cp_lexer_consume_token (parser->lexer);
28817 rhs = cp_parser_binary_expression (parser, false, false,
28818 oprec, NULL);
28819 if (rhs == error_mark_node)
28820 goto saw_error;
28821 goto stmt_done;
28822 /* FALLTHROUGH */
28823 default:
28824 cp_parser_error (parser,
28825 "invalid operator for %<#pragma omp atomic%>");
28826 goto saw_error;
28828 cp_lexer_consume_token (parser->lexer);
28830 rhs = cp_parser_expression (parser, false, NULL);
28831 if (rhs == error_mark_node)
28832 goto saw_error;
28833 break;
28835 stmt_done:
28836 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28838 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28839 goto saw_error;
28840 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28841 /*cast_p=*/false, NULL);
28842 if (v == error_mark_node)
28843 goto saw_error;
28844 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28845 goto saw_error;
28846 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28847 /*cast_p=*/false, NULL);
28848 if (lhs1 == error_mark_node)
28849 goto saw_error;
28851 if (structured_block)
28853 cp_parser_consume_semicolon_at_end_of_statement (parser);
28854 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28856 done:
28857 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28858 if (!structured_block)
28859 cp_parser_consume_semicolon_at_end_of_statement (parser);
28860 return;
28862 saw_error:
28863 cp_parser_skip_to_end_of_block_or_statement (parser);
28864 if (structured_block)
28866 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28867 cp_lexer_consume_token (parser->lexer);
28868 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28870 cp_parser_skip_to_end_of_block_or_statement (parser);
28871 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28872 cp_lexer_consume_token (parser->lexer);
28878 /* OpenMP 2.5:
28879 # pragma omp barrier new-line */
28881 static void
28882 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28884 cp_parser_require_pragma_eol (parser, pragma_tok);
28885 finish_omp_barrier ();
28888 /* OpenMP 2.5:
28889 # pragma omp critical [(name)] new-line
28890 structured-block */
28892 static tree
28893 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28895 tree stmt, name = NULL;
28897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28899 cp_lexer_consume_token (parser->lexer);
28901 name = cp_parser_identifier (parser);
28903 if (name == error_mark_node
28904 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28905 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28906 /*or_comma=*/false,
28907 /*consume_paren=*/true);
28908 if (name == error_mark_node)
28909 name = NULL;
28911 cp_parser_require_pragma_eol (parser, pragma_tok);
28913 stmt = cp_parser_omp_structured_block (parser);
28914 return c_finish_omp_critical (input_location, stmt, name);
28917 /* OpenMP 2.5:
28918 # pragma omp flush flush-vars[opt] new-line
28920 flush-vars:
28921 ( variable-list ) */
28923 static void
28924 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28927 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28928 cp_parser_require_pragma_eol (parser, pragma_tok);
28930 finish_omp_flush ();
28933 /* Helper function, to parse omp for increment expression. */
28935 static tree
28936 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28938 tree cond = cp_parser_binary_expression (parser, false, true,
28939 PREC_NOT_OPERATOR, NULL);
28940 if (cond == error_mark_node
28941 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28943 cp_parser_skip_to_end_of_statement (parser);
28944 return error_mark_node;
28947 switch (TREE_CODE (cond))
28949 case GT_EXPR:
28950 case GE_EXPR:
28951 case LT_EXPR:
28952 case LE_EXPR:
28953 break;
28954 case NE_EXPR:
28955 if (code == CILK_SIMD)
28956 break;
28957 /* Fall through: OpenMP disallows NE_EXPR. */
28958 default:
28959 return error_mark_node;
28962 /* If decl is an iterator, preserve LHS and RHS of the relational
28963 expr until finish_omp_for. */
28964 if (decl
28965 && (type_dependent_expression_p (decl)
28966 || CLASS_TYPE_P (TREE_TYPE (decl))))
28967 return cond;
28969 return build_x_binary_op (input_location, TREE_CODE (cond),
28970 TREE_OPERAND (cond, 0), ERROR_MARK,
28971 TREE_OPERAND (cond, 1), ERROR_MARK,
28972 /*overload=*/NULL, tf_warning_or_error);
28975 /* Helper function, to parse omp for increment expression. */
28977 static tree
28978 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
28980 cp_token *token = cp_lexer_peek_token (parser->lexer);
28981 enum tree_code op;
28982 tree lhs, rhs;
28983 cp_id_kind idk;
28984 bool decl_first;
28986 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28988 op = (token->type == CPP_PLUS_PLUS
28989 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
28990 cp_lexer_consume_token (parser->lexer);
28991 lhs = cp_parser_simple_cast_expression (parser);
28992 if (lhs != decl)
28993 return error_mark_node;
28994 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28997 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
28998 if (lhs != decl)
28999 return error_mark_node;
29001 token = cp_lexer_peek_token (parser->lexer);
29002 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29004 op = (token->type == CPP_PLUS_PLUS
29005 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29006 cp_lexer_consume_token (parser->lexer);
29007 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29010 op = cp_parser_assignment_operator_opt (parser);
29011 if (op == ERROR_MARK)
29012 return error_mark_node;
29014 if (op != NOP_EXPR)
29016 rhs = cp_parser_assignment_expression (parser, false, NULL);
29017 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29018 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29021 lhs = cp_parser_binary_expression (parser, false, false,
29022 PREC_ADDITIVE_EXPRESSION, NULL);
29023 token = cp_lexer_peek_token (parser->lexer);
29024 decl_first = lhs == decl;
29025 if (decl_first)
29026 lhs = NULL_TREE;
29027 if (token->type != CPP_PLUS
29028 && token->type != CPP_MINUS)
29029 return error_mark_node;
29033 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29034 cp_lexer_consume_token (parser->lexer);
29035 rhs = cp_parser_binary_expression (parser, false, false,
29036 PREC_ADDITIVE_EXPRESSION, NULL);
29037 token = cp_lexer_peek_token (parser->lexer);
29038 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29040 if (lhs == NULL_TREE)
29042 if (op == PLUS_EXPR)
29043 lhs = rhs;
29044 else
29045 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29046 tf_warning_or_error);
29048 else
29049 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29050 ERROR_MARK, NULL, tf_warning_or_error);
29053 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29055 if (!decl_first)
29057 if (rhs != decl || op == MINUS_EXPR)
29058 return error_mark_node;
29059 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29061 else
29062 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29064 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29067 /* Parse the initialization statement of either an OpenMP for loop or
29068 a Cilk Plus for loop.
29070 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29071 Plus.
29073 Return true if the resulting construct should have an
29074 OMP_CLAUSE_PRIVATE added to it. */
29076 static bool
29077 cp_parser_omp_for_loop_init (cp_parser *parser,
29078 bool parsing_openmp,
29079 tree &this_pre_body,
29080 vec<tree, va_gc> *for_block,
29081 tree &init,
29082 tree &decl,
29083 tree &real_decl)
29085 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29086 return false;
29088 bool add_private_clause = false;
29090 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29092 init-expr:
29093 var = lb
29094 integer-type var = lb
29095 random-access-iterator-type var = lb
29096 pointer-type var = lb
29098 cp_decl_specifier_seq type_specifiers;
29100 /* First, try to parse as an initialized declaration. See
29101 cp_parser_condition, from whence the bulk of this is copied. */
29103 cp_parser_parse_tentatively (parser);
29104 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29105 /*is_trailing_return=*/false,
29106 &type_specifiers);
29107 if (cp_parser_parse_definitely (parser))
29109 /* If parsing a type specifier seq succeeded, then this
29110 MUST be a initialized declaration. */
29111 tree asm_specification, attributes;
29112 cp_declarator *declarator;
29114 declarator = cp_parser_declarator (parser,
29115 CP_PARSER_DECLARATOR_NAMED,
29116 /*ctor_dtor_or_conv_p=*/NULL,
29117 /*parenthesized_p=*/NULL,
29118 /*member_p=*/false);
29119 attributes = cp_parser_attributes_opt (parser);
29120 asm_specification = cp_parser_asm_specification_opt (parser);
29122 if (declarator == cp_error_declarator)
29123 cp_parser_skip_to_end_of_statement (parser);
29125 else
29127 tree pushed_scope, auto_node;
29129 decl = start_decl (declarator, &type_specifiers,
29130 SD_INITIALIZED, attributes,
29131 /*prefix_attributes=*/NULL_TREE,
29132 &pushed_scope);
29134 auto_node = type_uses_auto (TREE_TYPE (decl));
29135 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29137 if (cp_lexer_next_token_is (parser->lexer,
29138 CPP_OPEN_PAREN))
29140 if (parsing_openmp)
29141 error ("parenthesized initialization is not allowed in "
29142 "OpenMP %<for%> loop");
29143 else
29144 error ("parenthesized initialization is "
29145 "not allowed in for-loop");
29147 else
29148 /* Trigger an error. */
29149 cp_parser_require (parser, CPP_EQ, RT_EQ);
29151 init = error_mark_node;
29152 cp_parser_skip_to_end_of_statement (parser);
29154 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29155 || type_dependent_expression_p (decl)
29156 || auto_node)
29158 bool is_direct_init, is_non_constant_init;
29160 init = cp_parser_initializer (parser,
29161 &is_direct_init,
29162 &is_non_constant_init);
29164 if (auto_node)
29166 TREE_TYPE (decl)
29167 = do_auto_deduction (TREE_TYPE (decl), init,
29168 auto_node);
29170 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29171 && !type_dependent_expression_p (decl))
29172 goto non_class;
29175 cp_finish_decl (decl, init, !is_non_constant_init,
29176 asm_specification,
29177 LOOKUP_ONLYCONVERTING);
29178 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29180 vec_safe_push (for_block, this_pre_body);
29181 init = NULL_TREE;
29183 else
29184 init = pop_stmt_list (this_pre_body);
29185 this_pre_body = NULL_TREE;
29187 else
29189 /* Consume '='. */
29190 cp_lexer_consume_token (parser->lexer);
29191 init = cp_parser_assignment_expression (parser, false, NULL);
29193 non_class:
29194 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29195 init = error_mark_node;
29196 else
29197 cp_finish_decl (decl, NULL_TREE,
29198 /*init_const_expr_p=*/false,
29199 asm_specification,
29200 LOOKUP_ONLYCONVERTING);
29203 if (pushed_scope)
29204 pop_scope (pushed_scope);
29207 else
29209 cp_id_kind idk;
29210 /* If parsing a type specifier sequence failed, then
29211 this MUST be a simple expression. */
29212 cp_parser_parse_tentatively (parser);
29213 decl = cp_parser_primary_expression (parser, false, false,
29214 false, &idk);
29215 if (!cp_parser_error_occurred (parser)
29216 && decl
29217 && DECL_P (decl)
29218 && CLASS_TYPE_P (TREE_TYPE (decl)))
29220 tree rhs;
29222 cp_parser_parse_definitely (parser);
29223 cp_parser_require (parser, CPP_EQ, RT_EQ);
29224 rhs = cp_parser_assignment_expression (parser, false, NULL);
29225 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29226 decl, NOP_EXPR,
29227 rhs,
29228 tf_warning_or_error));
29229 add_private_clause = true;
29231 else
29233 decl = NULL;
29234 cp_parser_abort_tentative_parse (parser);
29235 init = cp_parser_expression (parser, false, NULL);
29236 if (init)
29238 if (TREE_CODE (init) == MODIFY_EXPR
29239 || TREE_CODE (init) == MODOP_EXPR)
29240 real_decl = TREE_OPERAND (init, 0);
29244 return add_private_clause;
29247 /* Parse the restricted form of the for statement allowed by OpenMP. */
29249 static tree
29250 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29251 tree *cclauses)
29253 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29254 tree real_decl, initv, condv, incrv, declv;
29255 tree this_pre_body, cl;
29256 location_t loc_first;
29257 bool collapse_err = false;
29258 int i, collapse = 1, nbraces = 0;
29259 vec<tree, va_gc> *for_block = make_tree_vector ();
29261 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29262 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29263 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29265 gcc_assert (collapse >= 1);
29267 declv = make_tree_vec (collapse);
29268 initv = make_tree_vec (collapse);
29269 condv = make_tree_vec (collapse);
29270 incrv = make_tree_vec (collapse);
29272 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29274 for (i = 0; i < collapse; i++)
29276 int bracecount = 0;
29277 bool add_private_clause = false;
29278 location_t loc;
29280 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29282 cp_parser_error (parser, "for statement expected");
29283 return NULL;
29285 loc = cp_lexer_consume_token (parser->lexer)->location;
29287 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29288 return NULL;
29290 init = decl = real_decl = NULL;
29291 this_pre_body = push_stmt_list ();
29293 add_private_clause
29294 |= cp_parser_omp_for_loop_init (parser,
29295 /*parsing_openmp=*/code != CILK_SIMD,
29296 this_pre_body, for_block,
29297 init, decl, real_decl);
29299 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29300 if (this_pre_body)
29302 this_pre_body = pop_stmt_list (this_pre_body);
29303 if (pre_body)
29305 tree t = pre_body;
29306 pre_body = push_stmt_list ();
29307 add_stmt (t);
29308 add_stmt (this_pre_body);
29309 pre_body = pop_stmt_list (pre_body);
29311 else
29312 pre_body = this_pre_body;
29315 if (decl)
29316 real_decl = decl;
29317 if (cclauses != NULL
29318 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29319 && real_decl != NULL_TREE)
29321 tree *c;
29322 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29323 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29324 && OMP_CLAUSE_DECL (*c) == real_decl)
29326 error_at (loc, "iteration variable %qD"
29327 " should not be firstprivate", real_decl);
29328 *c = OMP_CLAUSE_CHAIN (*c);
29330 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29331 && OMP_CLAUSE_DECL (*c) == real_decl)
29333 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29334 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29335 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29336 OMP_CLAUSE_DECL (l) = real_decl;
29337 OMP_CLAUSE_CHAIN (l) = clauses;
29338 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29339 clauses = l;
29340 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29341 CP_OMP_CLAUSE_INFO (*c) = NULL;
29342 add_private_clause = false;
29344 else
29346 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29347 && OMP_CLAUSE_DECL (*c) == real_decl)
29348 add_private_clause = false;
29349 c = &OMP_CLAUSE_CHAIN (*c);
29353 if (add_private_clause)
29355 tree c;
29356 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29358 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29359 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29360 && OMP_CLAUSE_DECL (c) == decl)
29361 break;
29362 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29363 && OMP_CLAUSE_DECL (c) == decl)
29364 error_at (loc, "iteration variable %qD "
29365 "should not be firstprivate",
29366 decl);
29367 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29368 && OMP_CLAUSE_DECL (c) == decl)
29369 error_at (loc, "iteration variable %qD should not be reduction",
29370 decl);
29372 if (c == NULL)
29374 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29375 OMP_CLAUSE_DECL (c) = decl;
29376 c = finish_omp_clauses (c);
29377 if (c)
29379 OMP_CLAUSE_CHAIN (c) = clauses;
29380 clauses = c;
29385 cond = NULL;
29386 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29387 cond = cp_parser_omp_for_cond (parser, decl, code);
29388 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29390 incr = NULL;
29391 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29393 /* If decl is an iterator, preserve the operator on decl
29394 until finish_omp_for. */
29395 if (real_decl
29396 && ((processing_template_decl
29397 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29398 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29399 incr = cp_parser_omp_for_incr (parser, real_decl);
29400 else
29401 incr = cp_parser_expression (parser, false, NULL);
29402 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29403 SET_EXPR_LOCATION (incr, input_location);
29406 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29407 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29408 /*or_comma=*/false,
29409 /*consume_paren=*/true);
29411 TREE_VEC_ELT (declv, i) = decl;
29412 TREE_VEC_ELT (initv, i) = init;
29413 TREE_VEC_ELT (condv, i) = cond;
29414 TREE_VEC_ELT (incrv, i) = incr;
29416 if (i == collapse - 1)
29417 break;
29419 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29420 in between the collapsed for loops to be still considered perfectly
29421 nested. Hopefully the final version clarifies this.
29422 For now handle (multiple) {'s and empty statements. */
29423 cp_parser_parse_tentatively (parser);
29426 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29427 break;
29428 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29430 cp_lexer_consume_token (parser->lexer);
29431 bracecount++;
29433 else if (bracecount
29434 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29435 cp_lexer_consume_token (parser->lexer);
29436 else
29438 loc = cp_lexer_peek_token (parser->lexer)->location;
29439 error_at (loc, "not enough collapsed for loops");
29440 collapse_err = true;
29441 cp_parser_abort_tentative_parse (parser);
29442 declv = NULL_TREE;
29443 break;
29446 while (1);
29448 if (declv)
29450 cp_parser_parse_definitely (parser);
29451 nbraces += bracecount;
29455 /* Note that we saved the original contents of this flag when we entered
29456 the structured block, and so we don't need to re-save it here. */
29457 if (code == CILK_SIMD)
29458 parser->in_statement = IN_CILK_SIMD_FOR;
29459 else
29460 parser->in_statement = IN_OMP_FOR;
29462 /* Note that the grammar doesn't call for a structured block here,
29463 though the loop as a whole is a structured block. */
29464 body = push_stmt_list ();
29465 cp_parser_statement (parser, NULL_TREE, false, NULL);
29466 body = pop_stmt_list (body);
29468 if (declv == NULL_TREE)
29469 ret = NULL_TREE;
29470 else
29471 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29472 pre_body, clauses);
29474 while (nbraces)
29476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29478 cp_lexer_consume_token (parser->lexer);
29479 nbraces--;
29481 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29482 cp_lexer_consume_token (parser->lexer);
29483 else
29485 if (!collapse_err)
29487 error_at (cp_lexer_peek_token (parser->lexer)->location,
29488 "collapsed loops not perfectly nested");
29490 collapse_err = true;
29491 cp_parser_statement_seq_opt (parser, NULL);
29492 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29493 break;
29497 while (!for_block->is_empty ())
29498 add_stmt (pop_stmt_list (for_block->pop ()));
29499 release_tree_vector (for_block);
29501 return ret;
29504 /* Helper function for OpenMP parsing, split clauses and call
29505 finish_omp_clauses on each of the set of clauses afterwards. */
29507 static void
29508 cp_omp_split_clauses (location_t loc, enum tree_code code,
29509 omp_clause_mask mask, tree clauses, tree *cclauses)
29511 int i;
29512 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29513 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29514 if (cclauses[i])
29515 cclauses[i] = finish_omp_clauses (cclauses[i]);
29518 /* OpenMP 4.0:
29519 #pragma omp simd simd-clause[optseq] new-line
29520 for-loop */
29522 #define OMP_SIMD_CLAUSE_MASK \
29523 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29531 static tree
29532 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29533 char *p_name, omp_clause_mask mask, tree *cclauses)
29535 tree clauses, sb, ret;
29536 unsigned int save;
29537 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29539 strcat (p_name, " simd");
29540 mask |= OMP_SIMD_CLAUSE_MASK;
29541 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29543 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29544 cclauses == NULL);
29545 if (cclauses)
29547 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29548 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29551 sb = begin_omp_structured_block ();
29552 save = cp_parser_begin_omp_structured_block (parser);
29554 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29556 cp_parser_end_omp_structured_block (parser, save);
29557 add_stmt (finish_omp_structured_block (sb));
29559 return ret;
29562 /* OpenMP 2.5:
29563 #pragma omp for for-clause[optseq] new-line
29564 for-loop
29566 OpenMP 4.0:
29567 #pragma omp for simd for-simd-clause[optseq] new-line
29568 for-loop */
29570 #define OMP_FOR_CLAUSE_MASK \
29571 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29580 static tree
29581 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29582 char *p_name, omp_clause_mask mask, tree *cclauses)
29584 tree clauses, sb, ret;
29585 unsigned int save;
29586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29588 strcat (p_name, " for");
29589 mask |= OMP_FOR_CLAUSE_MASK;
29590 if (cclauses)
29591 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29595 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29596 const char *p = IDENTIFIER_POINTER (id);
29598 if (strcmp (p, "simd") == 0)
29600 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29601 if (cclauses == NULL)
29602 cclauses = cclauses_buf;
29604 cp_lexer_consume_token (parser->lexer);
29605 if (!flag_openmp) /* flag_openmp_simd */
29606 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29607 cclauses);
29608 sb = begin_omp_structured_block ();
29609 save = cp_parser_begin_omp_structured_block (parser);
29610 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29611 cclauses);
29612 cp_parser_end_omp_structured_block (parser, save);
29613 tree body = finish_omp_structured_block (sb);
29614 if (ret == NULL)
29615 return ret;
29616 ret = make_node (OMP_FOR);
29617 TREE_TYPE (ret) = void_type_node;
29618 OMP_FOR_BODY (ret) = body;
29619 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29620 SET_EXPR_LOCATION (ret, loc);
29621 add_stmt (ret);
29622 return ret;
29625 if (!flag_openmp) /* flag_openmp_simd */
29627 cp_parser_require_pragma_eol (parser, pragma_tok);
29628 return NULL_TREE;
29631 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29632 cclauses == NULL);
29633 if (cclauses)
29635 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29636 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29639 sb = begin_omp_structured_block ();
29640 save = cp_parser_begin_omp_structured_block (parser);
29642 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29644 cp_parser_end_omp_structured_block (parser, save);
29645 add_stmt (finish_omp_structured_block (sb));
29647 return ret;
29650 /* OpenMP 2.5:
29651 # pragma omp master new-line
29652 structured-block */
29654 static tree
29655 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29657 cp_parser_require_pragma_eol (parser, pragma_tok);
29658 return c_finish_omp_master (input_location,
29659 cp_parser_omp_structured_block (parser));
29662 /* OpenMP 2.5:
29663 # pragma omp ordered new-line
29664 structured-block */
29666 static tree
29667 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29669 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29670 cp_parser_require_pragma_eol (parser, pragma_tok);
29671 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29674 /* OpenMP 2.5:
29676 section-scope:
29677 { section-sequence }
29679 section-sequence:
29680 section-directive[opt] structured-block
29681 section-sequence section-directive structured-block */
29683 static tree
29684 cp_parser_omp_sections_scope (cp_parser *parser)
29686 tree stmt, substmt;
29687 bool error_suppress = false;
29688 cp_token *tok;
29690 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29691 return NULL_TREE;
29693 stmt = push_stmt_list ();
29695 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29697 substmt = cp_parser_omp_structured_block (parser);
29698 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29699 add_stmt (substmt);
29702 while (1)
29704 tok = cp_lexer_peek_token (parser->lexer);
29705 if (tok->type == CPP_CLOSE_BRACE)
29706 break;
29707 if (tok->type == CPP_EOF)
29708 break;
29710 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29712 cp_lexer_consume_token (parser->lexer);
29713 cp_parser_require_pragma_eol (parser, tok);
29714 error_suppress = false;
29716 else if (!error_suppress)
29718 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29719 error_suppress = true;
29722 substmt = cp_parser_omp_structured_block (parser);
29723 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29724 add_stmt (substmt);
29726 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29728 substmt = pop_stmt_list (stmt);
29730 stmt = make_node (OMP_SECTIONS);
29731 TREE_TYPE (stmt) = void_type_node;
29732 OMP_SECTIONS_BODY (stmt) = substmt;
29734 add_stmt (stmt);
29735 return stmt;
29738 /* OpenMP 2.5:
29739 # pragma omp sections sections-clause[optseq] newline
29740 sections-scope */
29742 #define OMP_SECTIONS_CLAUSE_MASK \
29743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29749 static tree
29750 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29751 char *p_name, omp_clause_mask mask, tree *cclauses)
29753 tree clauses, ret;
29754 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29756 strcat (p_name, " sections");
29757 mask |= OMP_SECTIONS_CLAUSE_MASK;
29758 if (cclauses)
29759 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29761 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29762 cclauses == NULL);
29763 if (cclauses)
29765 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29766 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29769 ret = cp_parser_omp_sections_scope (parser);
29770 if (ret)
29771 OMP_SECTIONS_CLAUSES (ret) = clauses;
29773 return ret;
29776 /* OpenMP 2.5:
29777 # pragma omp parallel parallel-clause[optseq] new-line
29778 structured-block
29779 # pragma omp parallel for parallel-for-clause[optseq] new-line
29780 structured-block
29781 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29782 structured-block
29784 OpenMP 4.0:
29785 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29786 structured-block */
29788 #define OMP_PARALLEL_CLAUSE_MASK \
29789 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29799 static tree
29800 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29801 char *p_name, omp_clause_mask mask, tree *cclauses)
29803 tree stmt, clauses, block;
29804 unsigned int save;
29805 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29807 strcat (p_name, " parallel");
29808 mask |= OMP_PARALLEL_CLAUSE_MASK;
29810 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29812 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29813 if (cclauses == NULL)
29814 cclauses = cclauses_buf;
29816 cp_lexer_consume_token (parser->lexer);
29817 if (!flag_openmp) /* flag_openmp_simd */
29818 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29819 block = begin_omp_parallel ();
29820 save = cp_parser_begin_omp_structured_block (parser);
29821 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29822 cp_parser_end_omp_structured_block (parser, save);
29823 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29824 block);
29825 OMP_PARALLEL_COMBINED (stmt) = 1;
29826 return stmt;
29828 else if (cclauses)
29830 error_at (loc, "expected %<for%> after %qs", p_name);
29831 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29832 return NULL_TREE;
29834 else if (!flag_openmp) /* flag_openmp_simd */
29836 cp_parser_require_pragma_eol (parser, pragma_tok);
29837 return NULL_TREE;
29839 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29841 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29842 const char *p = IDENTIFIER_POINTER (id);
29843 if (strcmp (p, "sections") == 0)
29845 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29846 cclauses = cclauses_buf;
29848 cp_lexer_consume_token (parser->lexer);
29849 block = begin_omp_parallel ();
29850 save = cp_parser_begin_omp_structured_block (parser);
29851 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29852 cp_parser_end_omp_structured_block (parser, save);
29853 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29854 block);
29855 OMP_PARALLEL_COMBINED (stmt) = 1;
29856 return stmt;
29860 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29862 block = begin_omp_parallel ();
29863 save = cp_parser_begin_omp_structured_block (parser);
29864 cp_parser_statement (parser, NULL_TREE, false, NULL);
29865 cp_parser_end_omp_structured_block (parser, save);
29866 stmt = finish_omp_parallel (clauses, block);
29867 return stmt;
29870 /* OpenMP 2.5:
29871 # pragma omp single single-clause[optseq] new-line
29872 structured-block */
29874 #define OMP_SINGLE_CLAUSE_MASK \
29875 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29880 static tree
29881 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29883 tree stmt = make_node (OMP_SINGLE);
29884 TREE_TYPE (stmt) = void_type_node;
29886 OMP_SINGLE_CLAUSES (stmt)
29887 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29888 "#pragma omp single", pragma_tok);
29889 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29891 return add_stmt (stmt);
29894 /* OpenMP 3.0:
29895 # pragma omp task task-clause[optseq] new-line
29896 structured-block */
29898 #define OMP_TASK_CLAUSE_MASK \
29899 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29909 static tree
29910 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29912 tree clauses, block;
29913 unsigned int save;
29915 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29916 "#pragma omp task", pragma_tok);
29917 block = begin_omp_task ();
29918 save = cp_parser_begin_omp_structured_block (parser);
29919 cp_parser_statement (parser, NULL_TREE, false, NULL);
29920 cp_parser_end_omp_structured_block (parser, save);
29921 return finish_omp_task (clauses, block);
29924 /* OpenMP 3.0:
29925 # pragma omp taskwait new-line */
29927 static void
29928 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29930 cp_parser_require_pragma_eol (parser, pragma_tok);
29931 finish_omp_taskwait ();
29934 /* OpenMP 3.1:
29935 # pragma omp taskyield new-line */
29937 static void
29938 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29940 cp_parser_require_pragma_eol (parser, pragma_tok);
29941 finish_omp_taskyield ();
29944 /* OpenMP 4.0:
29945 # pragma omp taskgroup new-line
29946 structured-block */
29948 static tree
29949 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29951 cp_parser_require_pragma_eol (parser, pragma_tok);
29952 return c_finish_omp_taskgroup (input_location,
29953 cp_parser_omp_structured_block (parser));
29957 /* OpenMP 2.5:
29958 # pragma omp threadprivate (variable-list) */
29960 static void
29961 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29963 tree vars;
29965 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29966 cp_parser_require_pragma_eol (parser, pragma_tok);
29968 finish_omp_threadprivate (vars);
29971 /* OpenMP 4.0:
29972 # pragma omp cancel cancel-clause[optseq] new-line */
29974 #define OMP_CANCEL_CLAUSE_MASK \
29975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29981 static void
29982 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
29984 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
29985 "#pragma omp cancel", pragma_tok);
29986 finish_omp_cancel (clauses);
29989 /* OpenMP 4.0:
29990 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29992 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29998 static void
29999 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30001 tree clauses;
30002 bool point_seen = false;
30004 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30006 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30007 const char *p = IDENTIFIER_POINTER (id);
30009 if (strcmp (p, "point") == 0)
30011 cp_lexer_consume_token (parser->lexer);
30012 point_seen = true;
30015 if (!point_seen)
30017 cp_parser_error (parser, "expected %<point%>");
30018 cp_parser_require_pragma_eol (parser, pragma_tok);
30019 return;
30022 clauses = cp_parser_omp_all_clauses (parser,
30023 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30024 "#pragma omp cancellation point",
30025 pragma_tok);
30026 finish_omp_cancellation_point (clauses);
30029 /* OpenMP 4.0:
30030 #pragma omp distribute distribute-clause[optseq] new-line
30031 for-loop */
30033 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30039 static tree
30040 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30041 char *p_name, omp_clause_mask mask, tree *cclauses)
30043 tree clauses, sb, ret;
30044 unsigned int save;
30045 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30047 strcat (p_name, " distribute");
30048 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30052 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30053 const char *p = IDENTIFIER_POINTER (id);
30054 bool simd = false;
30055 bool parallel = false;
30057 if (strcmp (p, "simd") == 0)
30058 simd = true;
30059 else
30060 parallel = strcmp (p, "parallel") == 0;
30061 if (parallel || simd)
30063 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30064 if (cclauses == NULL)
30065 cclauses = cclauses_buf;
30066 cp_lexer_consume_token (parser->lexer);
30067 if (!flag_openmp) /* flag_openmp_simd */
30069 if (simd)
30070 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30071 cclauses);
30072 else
30073 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30074 cclauses);
30076 sb = begin_omp_structured_block ();
30077 save = cp_parser_begin_omp_structured_block (parser);
30078 if (simd)
30079 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30080 cclauses);
30081 else
30082 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30083 cclauses);
30084 cp_parser_end_omp_structured_block (parser, save);
30085 tree body = finish_omp_structured_block (sb);
30086 if (ret == NULL)
30087 return ret;
30088 ret = make_node (OMP_DISTRIBUTE);
30089 TREE_TYPE (ret) = void_type_node;
30090 OMP_FOR_BODY (ret) = body;
30091 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30092 SET_EXPR_LOCATION (ret, loc);
30093 add_stmt (ret);
30094 return ret;
30097 if (!flag_openmp) /* flag_openmp_simd */
30099 cp_parser_require_pragma_eol (parser, pragma_tok);
30100 return NULL_TREE;
30103 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30104 cclauses == NULL);
30105 if (cclauses)
30107 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30108 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30111 sb = begin_omp_structured_block ();
30112 save = cp_parser_begin_omp_structured_block (parser);
30114 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30116 cp_parser_end_omp_structured_block (parser, save);
30117 add_stmt (finish_omp_structured_block (sb));
30119 return ret;
30122 /* OpenMP 4.0:
30123 # pragma omp teams teams-clause[optseq] new-line
30124 structured-block */
30126 #define OMP_TEAMS_CLAUSE_MASK \
30127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30135 static tree
30136 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30137 char *p_name, omp_clause_mask mask, tree *cclauses)
30139 tree clauses, sb, ret;
30140 unsigned int save;
30141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30143 strcat (p_name, " teams");
30144 mask |= OMP_TEAMS_CLAUSE_MASK;
30146 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30148 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30149 const char *p = IDENTIFIER_POINTER (id);
30150 if (strcmp (p, "distribute") == 0)
30152 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30153 if (cclauses == NULL)
30154 cclauses = cclauses_buf;
30156 cp_lexer_consume_token (parser->lexer);
30157 if (!flag_openmp) /* flag_openmp_simd */
30158 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30159 cclauses);
30160 sb = begin_omp_structured_block ();
30161 save = cp_parser_begin_omp_structured_block (parser);
30162 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30163 cclauses);
30164 cp_parser_end_omp_structured_block (parser, save);
30165 tree body = finish_omp_structured_block (sb);
30166 if (ret == NULL)
30167 return ret;
30168 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30169 ret = make_node (OMP_TEAMS);
30170 TREE_TYPE (ret) = void_type_node;
30171 OMP_TEAMS_CLAUSES (ret) = clauses;
30172 OMP_TEAMS_BODY (ret) = body;
30173 return add_stmt (ret);
30176 if (!flag_openmp) /* flag_openmp_simd */
30178 cp_parser_require_pragma_eol (parser, pragma_tok);
30179 return NULL_TREE;
30182 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30183 cclauses == NULL);
30184 if (cclauses)
30186 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30187 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30190 tree stmt = make_node (OMP_TEAMS);
30191 TREE_TYPE (stmt) = void_type_node;
30192 OMP_TEAMS_CLAUSES (stmt) = clauses;
30193 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30195 return add_stmt (stmt);
30198 /* OpenMP 4.0:
30199 # pragma omp target data target-data-clause[optseq] new-line
30200 structured-block */
30202 #define OMP_TARGET_DATA_CLAUSE_MASK \
30203 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30207 static tree
30208 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30210 tree stmt = make_node (OMP_TARGET_DATA);
30211 TREE_TYPE (stmt) = void_type_node;
30213 OMP_TARGET_DATA_CLAUSES (stmt)
30214 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30215 "#pragma omp target data", pragma_tok);
30216 keep_next_level (true);
30217 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30219 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30220 return add_stmt (stmt);
30223 /* OpenMP 4.0:
30224 # pragma omp target update target-update-clause[optseq] new-line */
30226 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30227 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30232 static bool
30233 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30234 enum pragma_context context)
30236 if (context == pragma_stmt)
30238 error_at (pragma_tok->location,
30239 "%<#pragma omp target update%> may only be "
30240 "used in compound statements");
30241 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30242 return false;
30245 tree clauses
30246 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30247 "#pragma omp target update", pragma_tok);
30248 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30249 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30251 error_at (pragma_tok->location,
30252 "%<#pragma omp target update must contain at least one "
30253 "%<from%> or %<to%> clauses");
30254 return false;
30257 tree stmt = make_node (OMP_TARGET_UPDATE);
30258 TREE_TYPE (stmt) = void_type_node;
30259 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30260 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30261 add_stmt (stmt);
30262 return false;
30265 /* OpenMP 4.0:
30266 # pragma omp target target-clause[optseq] new-line
30267 structured-block */
30269 #define OMP_TARGET_CLAUSE_MASK \
30270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30274 static bool
30275 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30276 enum pragma_context context)
30278 if (context != pragma_stmt && context != pragma_compound)
30280 cp_parser_error (parser, "expected declaration specifiers");
30281 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30282 return false;
30285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30287 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30288 const char *p = IDENTIFIER_POINTER (id);
30290 if (strcmp (p, "teams") == 0)
30292 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30293 char p_name[sizeof ("#pragma omp target teams distribute "
30294 "parallel for simd")];
30296 cp_lexer_consume_token (parser->lexer);
30297 strcpy (p_name, "#pragma omp target");
30298 if (!flag_openmp) /* flag_openmp_simd */
30299 return cp_parser_omp_teams (parser, pragma_tok, p_name,
30300 OMP_TARGET_CLAUSE_MASK, cclauses);
30301 keep_next_level (true);
30302 tree sb = begin_omp_structured_block ();
30303 unsigned save = cp_parser_begin_omp_structured_block (parser);
30304 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30305 OMP_TARGET_CLAUSE_MASK, cclauses);
30306 cp_parser_end_omp_structured_block (parser, save);
30307 tree body = finish_omp_structured_block (sb);
30308 if (ret == NULL)
30309 return ret;
30310 tree stmt = make_node (OMP_TARGET);
30311 TREE_TYPE (stmt) = void_type_node;
30312 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30313 OMP_TARGET_BODY (stmt) = body;
30314 add_stmt (stmt);
30315 return true;
30317 else if (!flag_openmp) /* flag_openmp_simd */
30319 cp_parser_require_pragma_eol (parser, pragma_tok);
30320 return NULL_TREE;
30322 else if (strcmp (p, "data") == 0)
30324 cp_lexer_consume_token (parser->lexer);
30325 cp_parser_omp_target_data (parser, pragma_tok);
30326 return true;
30328 else if (strcmp (p, "update") == 0)
30330 cp_lexer_consume_token (parser->lexer);
30331 return cp_parser_omp_target_update (parser, pragma_tok, context);
30335 tree stmt = make_node (OMP_TARGET);
30336 TREE_TYPE (stmt) = void_type_node;
30338 OMP_TARGET_CLAUSES (stmt)
30339 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30340 "#pragma omp target", pragma_tok);
30341 keep_next_level (true);
30342 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30344 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30345 add_stmt (stmt);
30346 return true;
30349 /* OpenMP 4.0:
30350 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30352 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30360 static void
30361 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30362 enum pragma_context context)
30364 bool first_p = parser->omp_declare_simd == NULL;
30365 cp_omp_declare_simd_data data;
30366 if (first_p)
30368 data.error_seen = false;
30369 data.fndecl_seen = false;
30370 data.tokens = vNULL;
30371 parser->omp_declare_simd = &data;
30373 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30374 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30375 cp_lexer_consume_token (parser->lexer);
30376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30377 parser->omp_declare_simd->error_seen = true;
30378 cp_parser_require_pragma_eol (parser, pragma_tok);
30379 struct cp_token_cache *cp
30380 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30381 parser->omp_declare_simd->tokens.safe_push (cp);
30382 if (first_p)
30384 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30385 cp_parser_pragma (parser, context);
30386 switch (context)
30388 case pragma_external:
30389 cp_parser_declaration (parser);
30390 break;
30391 case pragma_member:
30392 cp_parser_member_declaration (parser);
30393 break;
30394 case pragma_objc_icode:
30395 cp_parser_block_declaration (parser, /*statement_p=*/false);
30396 break;
30397 default:
30398 cp_parser_declaration_statement (parser);
30399 break;
30401 if (parser->omp_declare_simd
30402 && !parser->omp_declare_simd->error_seen
30403 && !parser->omp_declare_simd->fndecl_seen)
30404 error_at (pragma_tok->location,
30405 "%<#pragma omp declare simd%> not immediately followed by "
30406 "function declaration or definition");
30407 data.tokens.release ();
30408 parser->omp_declare_simd = NULL;
30412 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30413 This function is modelled similar to the late parsing of omp declare
30414 simd. */
30416 static tree
30417 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30419 struct cp_token_cache *ce;
30420 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30421 int ii = 0;
30423 if (parser->omp_declare_simd != NULL)
30425 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30426 " marked as a Cilk Plus SIMD-enabled function");
30427 XDELETE (parser->cilk_simd_fn_info);
30428 parser->cilk_simd_fn_info = NULL;
30429 return attrs;
30431 if (!info->error_seen && info->fndecl_seen)
30433 error ("vector attribute not immediately followed by a single function"
30434 " declaration or definition");
30435 info->error_seen = true;
30437 if (info->error_seen)
30438 return attrs;
30440 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30442 tree c, cl;
30444 cp_parser_push_lexer_for_tokens (parser, ce);
30445 parser->lexer->in_pragma = true;
30446 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30447 "SIMD-enabled functions attribute",
30448 NULL);
30449 cp_parser_pop_lexer (parser);
30450 if (cl)
30451 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30453 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30454 TREE_CHAIN (c) = attrs;
30455 attrs = c;
30457 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30458 TREE_CHAIN (c) = attrs;
30459 if (processing_template_decl)
30460 ATTR_IS_DEPENDENT (c) = 1;
30461 attrs = c;
30463 info->fndecl_seen = true;
30464 XDELETE (parser->cilk_simd_fn_info);
30465 parser->cilk_simd_fn_info = NULL;
30466 return attrs;
30469 /* Finalize #pragma omp declare simd clauses after direct declarator has
30470 been parsed, and put that into "omp declare simd" attribute. */
30472 static tree
30473 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30475 struct cp_token_cache *ce;
30476 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30477 int i;
30479 if (!data->error_seen && data->fndecl_seen)
30481 error ("%<#pragma omp declare simd%> not immediately followed by "
30482 "a single function declaration or definition");
30483 data->error_seen = true;
30484 return attrs;
30486 if (data->error_seen)
30487 return attrs;
30489 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30491 tree c, cl;
30493 cp_parser_push_lexer_for_tokens (parser, ce);
30494 parser->lexer->in_pragma = true;
30495 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30496 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30497 cp_lexer_consume_token (parser->lexer);
30498 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30499 "#pragma omp declare simd", pragma_tok);
30500 cp_parser_pop_lexer (parser);
30501 if (cl)
30502 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30503 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30504 TREE_CHAIN (c) = attrs;
30505 if (processing_template_decl)
30506 ATTR_IS_DEPENDENT (c) = 1;
30507 attrs = c;
30510 data->fndecl_seen = true;
30511 return attrs;
30515 /* OpenMP 4.0:
30516 # pragma omp declare target new-line
30517 declarations and definitions
30518 # pragma omp end declare target new-line */
30520 static void
30521 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30523 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30524 scope_chain->omp_declare_target_attribute++;
30527 static void
30528 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30530 const char *p = "";
30531 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30533 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30534 p = IDENTIFIER_POINTER (id);
30536 if (strcmp (p, "declare") == 0)
30538 cp_lexer_consume_token (parser->lexer);
30539 p = "";
30540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30543 p = IDENTIFIER_POINTER (id);
30545 if (strcmp (p, "target") == 0)
30546 cp_lexer_consume_token (parser->lexer);
30547 else
30549 cp_parser_error (parser, "expected %<target%>");
30550 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30551 return;
30554 else
30556 cp_parser_error (parser, "expected %<declare%>");
30557 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30558 return;
30560 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30561 if (!scope_chain->omp_declare_target_attribute)
30562 error_at (pragma_tok->location,
30563 "%<#pragma omp end declare target%> without corresponding "
30564 "%<#pragma omp declare target%>");
30565 else
30566 scope_chain->omp_declare_target_attribute--;
30569 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30570 expression and optional initializer clause of
30571 #pragma omp declare reduction. We store the expression(s) as
30572 either 3, 6 or 7 special statements inside of the artificial function's
30573 body. The first two statements are DECL_EXPRs for the artificial
30574 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30575 expression that uses those variables.
30576 If there was any INITIALIZER clause, this is followed by further statements,
30577 the fourth and fifth statements are DECL_EXPRs for the artificial
30578 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30579 constructor variant (first token after open paren is not omp_priv),
30580 then the sixth statement is a statement with the function call expression
30581 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30582 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30583 to initialize the OMP_PRIV artificial variable and there is seventh
30584 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30586 static bool
30587 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30589 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30590 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30591 type = TREE_TYPE (type);
30592 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30593 DECL_ARTIFICIAL (omp_out) = 1;
30594 pushdecl (omp_out);
30595 add_decl_expr (omp_out);
30596 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30597 DECL_ARTIFICIAL (omp_in) = 1;
30598 pushdecl (omp_in);
30599 add_decl_expr (omp_in);
30600 tree combiner;
30601 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30603 keep_next_level (true);
30604 tree block = begin_omp_structured_block ();
30605 combiner = cp_parser_expression (parser, false, NULL);
30606 finish_expr_stmt (combiner);
30607 block = finish_omp_structured_block (block);
30608 add_stmt (block);
30610 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30611 return false;
30613 const char *p = "";
30614 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30616 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30617 p = IDENTIFIER_POINTER (id);
30620 if (strcmp (p, "initializer") == 0)
30622 cp_lexer_consume_token (parser->lexer);
30623 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30624 return false;
30626 p = "";
30627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30630 p = IDENTIFIER_POINTER (id);
30633 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30634 DECL_ARTIFICIAL (omp_priv) = 1;
30635 pushdecl (omp_priv);
30636 add_decl_expr (omp_priv);
30637 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30638 DECL_ARTIFICIAL (omp_orig) = 1;
30639 pushdecl (omp_orig);
30640 add_decl_expr (omp_orig);
30642 keep_next_level (true);
30643 block = begin_omp_structured_block ();
30645 bool ctor = false;
30646 if (strcmp (p, "omp_priv") == 0)
30648 bool is_direct_init, is_non_constant_init;
30649 ctor = true;
30650 cp_lexer_consume_token (parser->lexer);
30651 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30652 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30653 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30654 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30655 == CPP_CLOSE_PAREN
30656 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30657 == CPP_CLOSE_PAREN))
30659 finish_omp_structured_block (block);
30660 error ("invalid initializer clause");
30661 return false;
30663 initializer = cp_parser_initializer (parser, &is_direct_init,
30664 &is_non_constant_init);
30665 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30666 NULL_TREE, LOOKUP_ONLYCONVERTING);
30668 else
30670 cp_parser_parse_tentatively (parser);
30671 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30672 /*check_dependency_p=*/true,
30673 /*template_p=*/NULL,
30674 /*declarator_p=*/false,
30675 /*optional_p=*/false);
30676 vec<tree, va_gc> *args;
30677 if (fn_name == error_mark_node
30678 || cp_parser_error_occurred (parser)
30679 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30680 || ((args = cp_parser_parenthesized_expression_list
30681 (parser, non_attr, /*cast_p=*/false,
30682 /*allow_expansion_p=*/true,
30683 /*non_constant_p=*/NULL)),
30684 cp_parser_error_occurred (parser)))
30686 finish_omp_structured_block (block);
30687 cp_parser_abort_tentative_parse (parser);
30688 cp_parser_error (parser, "expected id-expression (arguments)");
30689 return false;
30691 unsigned int i;
30692 tree arg;
30693 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30694 if (arg == omp_priv
30695 || (TREE_CODE (arg) == ADDR_EXPR
30696 && TREE_OPERAND (arg, 0) == omp_priv))
30697 break;
30698 cp_parser_abort_tentative_parse (parser);
30699 if (arg == NULL_TREE)
30700 error ("one of the initializer call arguments should be %<omp_priv%>"
30701 " or %<&omp_priv%>");
30702 initializer = cp_parser_postfix_expression (parser, false, false, false,
30703 false, NULL);
30704 finish_expr_stmt (initializer);
30707 block = finish_omp_structured_block (block);
30708 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30709 finish_expr_stmt (block);
30711 if (ctor)
30712 add_decl_expr (omp_orig);
30714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30715 return false;
30718 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30719 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30721 return true;
30724 /* OpenMP 4.0
30725 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30726 initializer-clause[opt] new-line
30728 initializer-clause:
30729 initializer (omp_priv initializer)
30730 initializer (function-name (argument-list)) */
30732 static void
30733 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30734 enum pragma_context)
30736 auto_vec<tree> types;
30737 enum tree_code reduc_code = ERROR_MARK;
30738 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30739 unsigned int i;
30740 cp_token *first_token;
30741 cp_token_cache *cp;
30742 int errs;
30743 void *p;
30745 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30746 p = obstack_alloc (&declarator_obstack, 0);
30748 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30749 goto fail;
30751 switch (cp_lexer_peek_token (parser->lexer)->type)
30753 case CPP_PLUS:
30754 reduc_code = PLUS_EXPR;
30755 break;
30756 case CPP_MULT:
30757 reduc_code = MULT_EXPR;
30758 break;
30759 case CPP_MINUS:
30760 reduc_code = MINUS_EXPR;
30761 break;
30762 case CPP_AND:
30763 reduc_code = BIT_AND_EXPR;
30764 break;
30765 case CPP_XOR:
30766 reduc_code = BIT_XOR_EXPR;
30767 break;
30768 case CPP_OR:
30769 reduc_code = BIT_IOR_EXPR;
30770 break;
30771 case CPP_AND_AND:
30772 reduc_code = TRUTH_ANDIF_EXPR;
30773 break;
30774 case CPP_OR_OR:
30775 reduc_code = TRUTH_ORIF_EXPR;
30776 break;
30777 case CPP_NAME:
30778 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30779 break;
30780 default:
30781 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30782 "%<|%>, %<&&%>, %<||%> or identifier");
30783 goto fail;
30786 if (reduc_code != ERROR_MARK)
30787 cp_lexer_consume_token (parser->lexer);
30789 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30790 if (reduc_id == error_mark_node)
30791 goto fail;
30793 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30794 goto fail;
30796 /* Types may not be defined in declare reduction type list. */
30797 const char *saved_message;
30798 saved_message = parser->type_definition_forbidden_message;
30799 parser->type_definition_forbidden_message
30800 = G_("types may not be defined in declare reduction type list");
30801 bool saved_colon_corrects_to_scope_p;
30802 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30803 parser->colon_corrects_to_scope_p = false;
30804 bool saved_colon_doesnt_start_class_def_p;
30805 saved_colon_doesnt_start_class_def_p
30806 = parser->colon_doesnt_start_class_def_p;
30807 parser->colon_doesnt_start_class_def_p = true;
30809 while (true)
30811 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30812 type = cp_parser_type_id (parser);
30813 if (type == error_mark_node)
30815 else if (ARITHMETIC_TYPE_P (type)
30816 && (orig_reduc_id == NULL_TREE
30817 || (TREE_CODE (type) != COMPLEX_TYPE
30818 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30819 "min") == 0
30820 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30821 "max") == 0))))
30822 error_at (loc, "predeclared arithmetic type %qT in "
30823 "%<#pragma omp declare reduction%>", type);
30824 else if (TREE_CODE (type) == FUNCTION_TYPE
30825 || TREE_CODE (type) == METHOD_TYPE
30826 || TREE_CODE (type) == ARRAY_TYPE)
30827 error_at (loc, "function or array type %qT in "
30828 "%<#pragma omp declare reduction%>", type);
30829 else if (TREE_CODE (type) == REFERENCE_TYPE)
30830 error_at (loc, "reference type %qT in "
30831 "%<#pragma omp declare reduction%>", type);
30832 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30833 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30834 "%<#pragma omp declare reduction%>", type);
30835 else
30836 types.safe_push (type);
30838 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30839 cp_lexer_consume_token (parser->lexer);
30840 else
30841 break;
30844 /* Restore the saved message. */
30845 parser->type_definition_forbidden_message = saved_message;
30846 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30847 parser->colon_doesnt_start_class_def_p
30848 = saved_colon_doesnt_start_class_def_p;
30850 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30851 || types.is_empty ())
30853 fail:
30854 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30855 goto done;
30858 first_token = cp_lexer_peek_token (parser->lexer);
30859 cp = NULL;
30860 errs = errorcount;
30861 FOR_EACH_VEC_ELT (types, i, type)
30863 tree fntype
30864 = build_function_type_list (void_type_node,
30865 cp_build_reference_type (type, false),
30866 NULL_TREE);
30867 tree this_reduc_id = reduc_id;
30868 if (!dependent_type_p (type))
30869 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30870 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30871 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30872 DECL_ARTIFICIAL (fndecl) = 1;
30873 DECL_EXTERNAL (fndecl) = 1;
30874 DECL_DECLARED_INLINE_P (fndecl) = 1;
30875 DECL_IGNORED_P (fndecl) = 1;
30876 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30877 DECL_ATTRIBUTES (fndecl)
30878 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30879 DECL_ATTRIBUTES (fndecl));
30880 if (processing_template_decl)
30881 fndecl = push_template_decl (fndecl);
30882 bool block_scope = false;
30883 tree block = NULL_TREE;
30884 if (current_function_decl)
30886 block_scope = true;
30887 DECL_CONTEXT (fndecl) = global_namespace;
30888 if (!processing_template_decl)
30889 pushdecl (fndecl);
30891 else if (current_class_type)
30893 if (cp == NULL)
30895 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30896 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30897 cp_lexer_consume_token (parser->lexer);
30898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30899 goto fail;
30900 cp = cp_token_cache_new (first_token,
30901 cp_lexer_peek_nth_token (parser->lexer,
30902 2));
30904 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30905 finish_member_declaration (fndecl);
30906 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30907 DECL_PENDING_INLINE_P (fndecl) = 1;
30908 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30909 continue;
30911 else
30913 DECL_CONTEXT (fndecl) = current_namespace;
30914 pushdecl (fndecl);
30916 if (!block_scope)
30917 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30918 else
30919 block = begin_omp_structured_block ();
30920 if (cp)
30922 cp_parser_push_lexer_for_tokens (parser, cp);
30923 parser->lexer->in_pragma = true;
30925 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30927 if (!block_scope)
30928 finish_function (0);
30929 else
30930 DECL_CONTEXT (fndecl) = current_function_decl;
30931 if (cp)
30932 cp_parser_pop_lexer (parser);
30933 goto fail;
30935 if (cp)
30936 cp_parser_pop_lexer (parser);
30937 if (!block_scope)
30938 finish_function (0);
30939 else
30941 DECL_CONTEXT (fndecl) = current_function_decl;
30942 block = finish_omp_structured_block (block);
30943 if (TREE_CODE (block) == BIND_EXPR)
30944 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30945 else if (TREE_CODE (block) == STATEMENT_LIST)
30946 DECL_SAVED_TREE (fndecl) = block;
30947 if (processing_template_decl)
30948 add_decl_expr (fndecl);
30950 cp_check_omp_declare_reduction (fndecl);
30951 if (cp == NULL && types.length () > 1)
30952 cp = cp_token_cache_new (first_token,
30953 cp_lexer_peek_nth_token (parser->lexer, 2));
30954 if (errs != errorcount)
30955 break;
30958 cp_parser_require_pragma_eol (parser, pragma_tok);
30960 done:
30961 /* Free any declarators allocated. */
30962 obstack_free (&declarator_obstack, p);
30965 /* OpenMP 4.0
30966 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30967 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30968 initializer-clause[opt] new-line
30969 #pragma omp declare target new-line */
30971 static void
30972 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
30973 enum pragma_context context)
30975 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30977 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30978 const char *p = IDENTIFIER_POINTER (id);
30980 if (strcmp (p, "simd") == 0)
30982 cp_lexer_consume_token (parser->lexer);
30983 cp_parser_omp_declare_simd (parser, pragma_tok,
30984 context);
30985 return;
30987 cp_ensure_no_omp_declare_simd (parser);
30988 if (strcmp (p, "reduction") == 0)
30990 cp_lexer_consume_token (parser->lexer);
30991 cp_parser_omp_declare_reduction (parser, pragma_tok,
30992 context);
30993 return;
30995 if (!flag_openmp) /* flag_openmp_simd */
30997 cp_parser_require_pragma_eol (parser, pragma_tok);
30998 return;
31000 if (strcmp (p, "target") == 0)
31002 cp_lexer_consume_token (parser->lexer);
31003 cp_parser_omp_declare_target (parser, pragma_tok);
31004 return;
31007 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31008 "or %<target%>");
31009 cp_parser_require_pragma_eol (parser, pragma_tok);
31012 /* Main entry point to OpenMP statement pragmas. */
31014 static void
31015 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31017 tree stmt;
31018 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31019 omp_clause_mask mask (0);
31021 switch (pragma_tok->pragma_kind)
31023 case PRAGMA_OMP_ATOMIC:
31024 cp_parser_omp_atomic (parser, pragma_tok);
31025 return;
31026 case PRAGMA_OMP_CRITICAL:
31027 stmt = cp_parser_omp_critical (parser, pragma_tok);
31028 break;
31029 case PRAGMA_OMP_DISTRIBUTE:
31030 strcpy (p_name, "#pragma omp");
31031 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31032 break;
31033 case PRAGMA_OMP_FOR:
31034 strcpy (p_name, "#pragma omp");
31035 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31036 break;
31037 case PRAGMA_OMP_MASTER:
31038 stmt = cp_parser_omp_master (parser, pragma_tok);
31039 break;
31040 case PRAGMA_OMP_ORDERED:
31041 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31042 break;
31043 case PRAGMA_OMP_PARALLEL:
31044 strcpy (p_name, "#pragma omp");
31045 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31046 break;
31047 case PRAGMA_OMP_SECTIONS:
31048 strcpy (p_name, "#pragma omp");
31049 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31050 break;
31051 case PRAGMA_OMP_SIMD:
31052 strcpy (p_name, "#pragma omp");
31053 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31054 break;
31055 case PRAGMA_OMP_SINGLE:
31056 stmt = cp_parser_omp_single (parser, pragma_tok);
31057 break;
31058 case PRAGMA_OMP_TASK:
31059 stmt = cp_parser_omp_task (parser, pragma_tok);
31060 break;
31061 case PRAGMA_OMP_TASKGROUP:
31062 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31063 break;
31064 case PRAGMA_OMP_TEAMS:
31065 strcpy (p_name, "#pragma omp");
31066 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31067 break;
31068 default:
31069 gcc_unreachable ();
31072 if (stmt)
31073 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31076 /* Transactional Memory parsing routines. */
31078 /* Parse a transaction attribute.
31080 txn-attribute:
31081 attribute
31082 [ [ identifier ] ]
31084 ??? Simplify this when C++0x bracket attributes are
31085 implemented properly. */
31087 static tree
31088 cp_parser_txn_attribute_opt (cp_parser *parser)
31090 cp_token *token;
31091 tree attr_name, attr = NULL;
31093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31094 return cp_parser_attributes_opt (parser);
31096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31097 return NULL_TREE;
31098 cp_lexer_consume_token (parser->lexer);
31099 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31100 goto error1;
31102 token = cp_lexer_peek_token (parser->lexer);
31103 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31105 token = cp_lexer_consume_token (parser->lexer);
31107 attr_name = (token->type == CPP_KEYWORD
31108 /* For keywords, use the canonical spelling,
31109 not the parsed identifier. */
31110 ? ridpointers[(int) token->keyword]
31111 : token->u.value);
31112 attr = build_tree_list (attr_name, NULL_TREE);
31114 else
31115 cp_parser_error (parser, "expected identifier");
31117 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31118 error1:
31119 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31120 return attr;
31123 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31125 transaction-statement:
31126 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31127 compound-statement
31128 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31131 static tree
31132 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31134 unsigned char old_in = parser->in_transaction;
31135 unsigned char this_in = 1, new_in;
31136 cp_token *token;
31137 tree stmt, attrs, noex;
31139 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31140 || keyword == RID_TRANSACTION_RELAXED);
31141 token = cp_parser_require_keyword (parser, keyword,
31142 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31143 : RT_TRANSACTION_RELAXED));
31144 gcc_assert (token != NULL);
31146 if (keyword == RID_TRANSACTION_RELAXED)
31147 this_in |= TM_STMT_ATTR_RELAXED;
31148 else
31150 attrs = cp_parser_txn_attribute_opt (parser);
31151 if (attrs)
31152 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31155 /* Parse a noexcept specification. */
31156 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31158 /* Keep track if we're in the lexical scope of an outer transaction. */
31159 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31161 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31163 parser->in_transaction = new_in;
31164 cp_parser_compound_statement (parser, NULL, false, false);
31165 parser->in_transaction = old_in;
31167 finish_transaction_stmt (stmt, NULL, this_in, noex);
31169 return stmt;
31172 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31174 transaction-expression:
31175 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31176 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31179 static tree
31180 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31182 unsigned char old_in = parser->in_transaction;
31183 unsigned char this_in = 1;
31184 cp_token *token;
31185 tree expr, noex;
31186 bool noex_expr;
31188 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31189 || keyword == RID_TRANSACTION_RELAXED);
31191 if (!flag_tm)
31192 error (keyword == RID_TRANSACTION_RELAXED
31193 ? G_("%<__transaction_relaxed%> without transactional memory "
31194 "support enabled")
31195 : G_("%<__transaction_atomic%> without transactional memory "
31196 "support enabled"));
31198 token = cp_parser_require_keyword (parser, keyword,
31199 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31200 : RT_TRANSACTION_RELAXED));
31201 gcc_assert (token != NULL);
31203 if (keyword == RID_TRANSACTION_RELAXED)
31204 this_in |= TM_STMT_ATTR_RELAXED;
31206 /* Set this early. This might mean that we allow transaction_cancel in
31207 an expression that we find out later actually has to be a constexpr.
31208 However, we expect that cxx_constant_value will be able to deal with
31209 this; also, if the noexcept has no constexpr, then what we parse next
31210 really is a transaction's body. */
31211 parser->in_transaction = this_in;
31213 /* Parse a noexcept specification. */
31214 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31215 true);
31217 if (!noex || !noex_expr
31218 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31220 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31222 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31223 expr = finish_parenthesized_expr (expr);
31225 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31227 else
31229 /* The only expression that is available got parsed for the noexcept
31230 already. noexcept is true then. */
31231 expr = noex;
31232 noex = boolean_true_node;
31235 expr = build_transaction_expr (token->location, expr, this_in, noex);
31236 parser->in_transaction = old_in;
31238 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31239 return error_mark_node;
31241 return (flag_tm ? expr : error_mark_node);
31244 /* Parse a function-transaction-block.
31246 function-transaction-block:
31247 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31248 function-body
31249 __transaction_atomic txn-attribute[opt] function-try-block
31250 __transaction_relaxed ctor-initializer[opt] function-body
31251 __transaction_relaxed function-try-block
31254 static bool
31255 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31257 unsigned char old_in = parser->in_transaction;
31258 unsigned char new_in = 1;
31259 tree compound_stmt, stmt, attrs;
31260 bool ctor_initializer_p;
31261 cp_token *token;
31263 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31264 || keyword == RID_TRANSACTION_RELAXED);
31265 token = cp_parser_require_keyword (parser, keyword,
31266 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31267 : RT_TRANSACTION_RELAXED));
31268 gcc_assert (token != NULL);
31270 if (keyword == RID_TRANSACTION_RELAXED)
31271 new_in |= TM_STMT_ATTR_RELAXED;
31272 else
31274 attrs = cp_parser_txn_attribute_opt (parser);
31275 if (attrs)
31276 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31279 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31281 parser->in_transaction = new_in;
31283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31284 ctor_initializer_p = cp_parser_function_try_block (parser);
31285 else
31286 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31287 (parser, /*in_function_try_block=*/false);
31289 parser->in_transaction = old_in;
31291 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31293 return ctor_initializer_p;
31296 /* Parse a __transaction_cancel statement.
31298 cancel-statement:
31299 __transaction_cancel txn-attribute[opt] ;
31300 __transaction_cancel txn-attribute[opt] throw-expression ;
31302 ??? Cancel and throw is not yet implemented. */
31304 static tree
31305 cp_parser_transaction_cancel (cp_parser *parser)
31307 cp_token *token;
31308 bool is_outer = false;
31309 tree stmt, attrs;
31311 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31312 RT_TRANSACTION_CANCEL);
31313 gcc_assert (token != NULL);
31315 attrs = cp_parser_txn_attribute_opt (parser);
31316 if (attrs)
31317 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31319 /* ??? Parse cancel-and-throw here. */
31321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31323 if (!flag_tm)
31325 error_at (token->location, "%<__transaction_cancel%> without "
31326 "transactional memory support enabled");
31327 return error_mark_node;
31329 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31331 error_at (token->location, "%<__transaction_cancel%> within a "
31332 "%<__transaction_relaxed%>");
31333 return error_mark_node;
31335 else if (is_outer)
31337 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31338 && !is_tm_may_cancel_outer (current_function_decl))
31340 error_at (token->location, "outer %<__transaction_cancel%> not "
31341 "within outer %<__transaction_atomic%>");
31342 error_at (token->location,
31343 " or a %<transaction_may_cancel_outer%> function");
31344 return error_mark_node;
31347 else if (parser->in_transaction == 0)
31349 error_at (token->location, "%<__transaction_cancel%> not within "
31350 "%<__transaction_atomic%>");
31351 return error_mark_node;
31354 stmt = build_tm_abort_call (token->location, is_outer);
31355 add_stmt (stmt);
31357 return stmt;
31360 /* The parser. */
31362 static GTY (()) cp_parser *the_parser;
31365 /* Special handling for the first token or line in the file. The first
31366 thing in the file might be #pragma GCC pch_preprocess, which loads a
31367 PCH file, which is a GC collection point. So we need to handle this
31368 first pragma without benefit of an existing lexer structure.
31370 Always returns one token to the caller in *FIRST_TOKEN. This is
31371 either the true first token of the file, or the first token after
31372 the initial pragma. */
31374 static void
31375 cp_parser_initial_pragma (cp_token *first_token)
31377 tree name = NULL;
31379 cp_lexer_get_preprocessor_token (NULL, first_token);
31380 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31381 return;
31383 cp_lexer_get_preprocessor_token (NULL, first_token);
31384 if (first_token->type == CPP_STRING)
31386 name = first_token->u.value;
31388 cp_lexer_get_preprocessor_token (NULL, first_token);
31389 if (first_token->type != CPP_PRAGMA_EOL)
31390 error_at (first_token->location,
31391 "junk at end of %<#pragma GCC pch_preprocess%>");
31393 else
31394 error_at (first_token->location, "expected string literal");
31396 /* Skip to the end of the pragma. */
31397 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31398 cp_lexer_get_preprocessor_token (NULL, first_token);
31400 /* Now actually load the PCH file. */
31401 if (name)
31402 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31404 /* Read one more token to return to our caller. We have to do this
31405 after reading the PCH file in, since its pointers have to be
31406 live. */
31407 cp_lexer_get_preprocessor_token (NULL, first_token);
31410 /* Normal parsing of a pragma token. Here we can (and must) use the
31411 regular lexer. */
31413 static bool
31414 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31416 cp_token *pragma_tok;
31417 unsigned int id;
31419 pragma_tok = cp_lexer_consume_token (parser->lexer);
31420 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31421 parser->lexer->in_pragma = true;
31423 id = pragma_tok->pragma_kind;
31424 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31425 cp_ensure_no_omp_declare_simd (parser);
31426 switch (id)
31428 case PRAGMA_GCC_PCH_PREPROCESS:
31429 error_at (pragma_tok->location,
31430 "%<#pragma GCC pch_preprocess%> must be first");
31431 break;
31433 case PRAGMA_OMP_BARRIER:
31434 switch (context)
31436 case pragma_compound:
31437 cp_parser_omp_barrier (parser, pragma_tok);
31438 return false;
31439 case pragma_stmt:
31440 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31441 "used in compound statements");
31442 break;
31443 default:
31444 goto bad_stmt;
31446 break;
31448 case PRAGMA_OMP_FLUSH:
31449 switch (context)
31451 case pragma_compound:
31452 cp_parser_omp_flush (parser, pragma_tok);
31453 return false;
31454 case pragma_stmt:
31455 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31456 "used in compound statements");
31457 break;
31458 default:
31459 goto bad_stmt;
31461 break;
31463 case PRAGMA_OMP_TASKWAIT:
31464 switch (context)
31466 case pragma_compound:
31467 cp_parser_omp_taskwait (parser, pragma_tok);
31468 return false;
31469 case pragma_stmt:
31470 error_at (pragma_tok->location,
31471 "%<#pragma omp taskwait%> may only be "
31472 "used in compound statements");
31473 break;
31474 default:
31475 goto bad_stmt;
31477 break;
31479 case PRAGMA_OMP_TASKYIELD:
31480 switch (context)
31482 case pragma_compound:
31483 cp_parser_omp_taskyield (parser, pragma_tok);
31484 return false;
31485 case pragma_stmt:
31486 error_at (pragma_tok->location,
31487 "%<#pragma omp taskyield%> may only be "
31488 "used in compound statements");
31489 break;
31490 default:
31491 goto bad_stmt;
31493 break;
31495 case PRAGMA_OMP_CANCEL:
31496 switch (context)
31498 case pragma_compound:
31499 cp_parser_omp_cancel (parser, pragma_tok);
31500 return false;
31501 case pragma_stmt:
31502 error_at (pragma_tok->location,
31503 "%<#pragma omp cancel%> may only be "
31504 "used in compound statements");
31505 break;
31506 default:
31507 goto bad_stmt;
31509 break;
31511 case PRAGMA_OMP_CANCELLATION_POINT:
31512 switch (context)
31514 case pragma_compound:
31515 cp_parser_omp_cancellation_point (parser, pragma_tok);
31516 return false;
31517 case pragma_stmt:
31518 error_at (pragma_tok->location,
31519 "%<#pragma omp cancellation point%> may only be "
31520 "used in compound statements");
31521 break;
31522 default:
31523 goto bad_stmt;
31525 break;
31527 case PRAGMA_OMP_THREADPRIVATE:
31528 cp_parser_omp_threadprivate (parser, pragma_tok);
31529 return false;
31531 case PRAGMA_OMP_DECLARE_REDUCTION:
31532 cp_parser_omp_declare (parser, pragma_tok, context);
31533 return false;
31535 case PRAGMA_OMP_ATOMIC:
31536 case PRAGMA_OMP_CRITICAL:
31537 case PRAGMA_OMP_DISTRIBUTE:
31538 case PRAGMA_OMP_FOR:
31539 case PRAGMA_OMP_MASTER:
31540 case PRAGMA_OMP_ORDERED:
31541 case PRAGMA_OMP_PARALLEL:
31542 case PRAGMA_OMP_SECTIONS:
31543 case PRAGMA_OMP_SIMD:
31544 case PRAGMA_OMP_SINGLE:
31545 case PRAGMA_OMP_TASK:
31546 case PRAGMA_OMP_TASKGROUP:
31547 case PRAGMA_OMP_TEAMS:
31548 if (context != pragma_stmt && context != pragma_compound)
31549 goto bad_stmt;
31550 cp_parser_omp_construct (parser, pragma_tok);
31551 return true;
31553 case PRAGMA_OMP_TARGET:
31554 return cp_parser_omp_target (parser, pragma_tok, context);
31556 case PRAGMA_OMP_END_DECLARE_TARGET:
31557 cp_parser_omp_end_declare_target (parser, pragma_tok);
31558 return false;
31560 case PRAGMA_OMP_SECTION:
31561 error_at (pragma_tok->location,
31562 "%<#pragma omp section%> may only be used in "
31563 "%<#pragma omp sections%> construct");
31564 break;
31566 case PRAGMA_IVDEP:
31568 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31569 cp_token *tok;
31570 tok = cp_lexer_peek_token (the_parser->lexer);
31571 if (tok->type != CPP_KEYWORD
31572 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31573 && tok->keyword != RID_DO))
31575 cp_parser_error (parser, "for, while or do statement expected");
31576 return false;
31578 cp_parser_iteration_statement (parser, true);
31579 return true;
31582 case PRAGMA_CILK_SIMD:
31583 if (context == pragma_external)
31585 error_at (pragma_tok->location,
31586 "%<#pragma simd%> must be inside a function");
31587 break;
31589 cp_parser_cilk_simd (parser, pragma_tok);
31590 return true;
31592 default:
31593 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31594 c_invoke_pragma_handler (id);
31595 break;
31597 bad_stmt:
31598 cp_parser_error (parser, "expected declaration specifiers");
31599 break;
31602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31603 return false;
31606 /* The interface the pragma parsers have to the lexer. */
31608 enum cpp_ttype
31609 pragma_lex (tree *value)
31611 cp_token *tok;
31612 enum cpp_ttype ret;
31614 tok = cp_lexer_peek_token (the_parser->lexer);
31616 ret = tok->type;
31617 *value = tok->u.value;
31619 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31620 ret = CPP_EOF;
31621 else if (ret == CPP_STRING)
31622 *value = cp_parser_string_literal (the_parser, false, false);
31623 else
31625 cp_lexer_consume_token (the_parser->lexer);
31626 if (ret == CPP_KEYWORD)
31627 ret = CPP_NAME;
31630 return ret;
31634 /* External interface. */
31636 /* Parse one entire translation unit. */
31638 void
31639 c_parse_file (void)
31641 static bool already_called = false;
31643 if (already_called)
31645 sorry ("inter-module optimizations not implemented for C++");
31646 return;
31648 already_called = true;
31650 the_parser = cp_parser_new ();
31651 push_deferring_access_checks (flag_access_control
31652 ? dk_no_deferred : dk_no_check);
31653 cp_parser_translation_unit (the_parser);
31654 the_parser = NULL;
31657 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31658 vectorlength clause:
31659 Syntax:
31660 vectorlength ( constant-expression ) */
31662 static tree
31663 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31664 bool is_simd_fn)
31666 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31667 tree expr;
31668 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31669 safelen clause. Thus, vectorlength is represented as OMP 4.0
31670 safelen. For SIMD-enabled function it is represented by OMP 4.0
31671 simdlen. */
31672 if (!is_simd_fn)
31673 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31674 loc);
31675 else
31676 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31677 loc);
31679 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31680 return error_mark_node;
31682 expr = cp_parser_constant_expression (parser, false, NULL);
31683 expr = maybe_constant_value (expr);
31685 /* If expr == error_mark_node, then don't emit any errors nor
31686 create a clause. if any of the above functions returns
31687 error mark node then they would have emitted an error message. */
31688 if (expr == error_mark_node)
31690 else if (!TREE_TYPE (expr)
31691 || !TREE_CONSTANT (expr)
31692 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31693 error_at (loc, "vectorlength must be an integer constant");
31694 else if (TREE_CONSTANT (expr)
31695 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31696 error_at (loc, "vectorlength must be a power of 2");
31697 else
31699 tree c;
31700 if (!is_simd_fn)
31702 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31703 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31704 OMP_CLAUSE_CHAIN (c) = clauses;
31705 clauses = c;
31707 else
31709 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31710 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31711 OMP_CLAUSE_CHAIN (c) = clauses;
31712 clauses = c;
31716 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31717 return error_mark_node;
31718 return clauses;
31721 /* Handles the Cilk Plus #pragma simd linear clause.
31722 Syntax:
31723 linear ( simd-linear-variable-list )
31725 simd-linear-variable-list:
31726 simd-linear-variable
31727 simd-linear-variable-list , simd-linear-variable
31729 simd-linear-variable:
31730 id-expression
31731 id-expression : simd-linear-step
31733 simd-linear-step:
31734 conditional-expression */
31736 static tree
31737 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31739 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31741 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31742 return clauses;
31743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31745 cp_parser_error (parser, "expected identifier");
31746 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31747 return error_mark_node;
31750 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31751 parser->colon_corrects_to_scope_p = false;
31752 while (1)
31754 cp_token *token = cp_lexer_peek_token (parser->lexer);
31755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31757 cp_parser_error (parser, "expected variable-name");
31758 clauses = error_mark_node;
31759 break;
31762 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31763 false, false);
31764 tree decl = cp_parser_lookup_name_simple (parser, var_name,
31765 token->location);
31766 if (decl == error_mark_node)
31768 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31769 token->location);
31770 clauses = error_mark_node;
31772 else
31774 tree e = NULL_TREE;
31775 tree step_size = integer_one_node;
31777 /* If present, parse the linear step. Otherwise, assume the default
31778 value of 1. */
31779 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31781 cp_lexer_consume_token (parser->lexer);
31783 e = cp_parser_assignment_expression (parser, false, NULL);
31784 e = maybe_constant_value (e);
31786 if (e == error_mark_node)
31788 /* If an error has occurred, then the whole pragma is
31789 considered ill-formed. Thus, no reason to keep
31790 parsing. */
31791 clauses = error_mark_node;
31792 break;
31794 else if (type_dependent_expression_p (e)
31795 || value_dependent_expression_p (e)
31796 || (TREE_TYPE (e)
31797 && INTEGRAL_TYPE_P (TREE_TYPE (e))
31798 && (TREE_CONSTANT (e)
31799 || DECL_P (e))))
31800 step_size = e;
31801 else
31802 cp_parser_error (parser,
31803 "step size must be an integer constant "
31804 "expression or an integer variable");
31807 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31808 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31809 OMP_CLAUSE_DECL (l) = decl;
31810 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31811 OMP_CLAUSE_CHAIN (l) = clauses;
31812 clauses = l;
31814 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31815 cp_lexer_consume_token (parser->lexer);
31816 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31817 break;
31818 else
31820 error_at (cp_lexer_peek_token (parser->lexer)->location,
31821 "expected %<,%> or %<)%> after %qE", decl);
31822 clauses = error_mark_node;
31823 break;
31826 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31827 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31828 return clauses;
31831 /* Returns the name of the next clause. If the clause is not
31832 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31833 token is not consumed. Otherwise, the appropriate enum from the
31834 pragma_simd_clause is returned and the token is consumed. */
31836 static pragma_omp_clause
31837 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31839 pragma_omp_clause clause_type;
31840 cp_token *token = cp_lexer_peek_token (parser->lexer);
31842 if (token->keyword == RID_PRIVATE)
31843 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31844 else if (!token->u.value || token->type != CPP_NAME)
31845 return PRAGMA_CILK_CLAUSE_NONE;
31846 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31847 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31848 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31849 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31850 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31851 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31852 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31853 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31854 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31855 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31856 else
31857 return PRAGMA_CILK_CLAUSE_NONE;
31859 cp_lexer_consume_token (parser->lexer);
31860 return clause_type;
31863 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31865 static tree
31866 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31868 tree clauses = NULL_TREE;
31870 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31871 && clauses != error_mark_node)
31873 pragma_omp_clause c_kind;
31874 c_kind = cp_parser_cilk_simd_clause_name (parser);
31875 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31876 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
31877 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31878 clauses = cp_parser_cilk_simd_linear (parser, clauses);
31879 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31880 /* Use the OpenMP 4.0 equivalent function. */
31881 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31882 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31883 /* Use the OpenMP 4.0 equivalent function. */
31884 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31885 clauses);
31886 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31887 /* Use the OMP 4.0 equivalent function. */
31888 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31889 clauses);
31890 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31891 /* Use the OMP 4.0 equivalent function. */
31892 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31893 else
31895 clauses = error_mark_node;
31896 cp_parser_error (parser, "expected %<#pragma simd%> clause");
31897 break;
31901 cp_parser_skip_to_pragma_eol (parser, pragma_token);
31903 if (clauses == error_mark_node)
31904 return error_mark_node;
31905 else
31906 return c_finish_cilk_clauses (clauses);
31909 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31911 static void
31912 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31914 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31916 if (clauses == error_mark_node)
31917 return;
31919 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31921 error_at (cp_lexer_peek_token (parser->lexer)->location,
31922 "for statement expected");
31923 return;
31926 tree sb = begin_omp_structured_block ();
31927 int save = cp_parser_begin_omp_structured_block (parser);
31928 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31929 if (ret)
31930 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31931 cp_parser_end_omp_structured_block (parser, save);
31932 add_stmt (finish_omp_structured_block (sb));
31933 return;
31936 /* Create an identifier for a generic parameter type (a synthesized
31937 template parameter implied by `auto' or a concept identifier). */
31939 static GTY(()) int generic_parm_count;
31940 static tree
31941 make_generic_type_name ()
31943 char buf[32];
31944 sprintf (buf, "auto:%d", ++generic_parm_count);
31945 return get_identifier (buf);
31948 /* Predicate that behaves as is_auto_or_concept but matches the parent
31949 node of the generic type rather than the generic type itself. This
31950 allows for type transformation in add_implicit_template_parms. */
31952 static inline bool
31953 tree_type_is_auto_or_concept (const_tree t)
31955 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31958 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
31959 (creating a new template parameter list if necessary). Returns the newly
31960 created template type parm. */
31962 tree
31963 synthesize_implicit_template_parm (cp_parser *parser)
31965 gcc_assert (current_binding_level->kind == sk_function_parms);
31967 /* We are either continuing a function template that already contains implicit
31968 template parameters, creating a new fully-implicit function template, or
31969 extending an existing explicit function template with implicit template
31970 parameters. */
31972 cp_binding_level *const entry_scope = current_binding_level;
31974 bool become_template = false;
31975 cp_binding_level *parent_scope = 0;
31977 if (parser->implicit_template_scope)
31979 gcc_assert (parser->implicit_template_parms);
31981 current_binding_level = parser->implicit_template_scope;
31983 else
31985 /* Roll back to the existing template parameter scope (in the case of
31986 extending an explicit function template) or introduce a new template
31987 parameter scope ahead of the function parameter scope (or class scope
31988 in the case of out-of-line member definitions). The function scope is
31989 added back after template parameter synthesis below. */
31991 cp_binding_level *scope = entry_scope;
31993 while (scope->kind == sk_function_parms)
31995 parent_scope = scope;
31996 scope = scope->level_chain;
31998 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32000 /* If not defining a class, then any class scope is a scope level in
32001 an out-of-line member definition. In this case simply wind back
32002 beyond the first such scope to inject the template argument list.
32003 Otherwise wind back to the class being defined. The latter can
32004 occur in class member friend declarations such as:
32006 class A {
32007 void foo (auto);
32009 class B {
32010 friend void A::foo (auto);
32013 The template argument list synthesized for the friend declaration
32014 must be injected in the scope of 'B', just beyond the scope of 'A'
32015 introduced by 'A::'. */
32017 while (scope->kind == sk_class
32018 && !TYPE_BEING_DEFINED (scope->this_entity))
32020 parent_scope = scope;
32021 scope = scope->level_chain;
32025 current_binding_level = scope;
32027 if (scope->kind != sk_template_parms
32028 || !function_being_declared_is_template_p (parser))
32030 /* Introduce a new template parameter list for implicit template
32031 parameters. */
32033 become_template = true;
32035 parser->implicit_template_scope
32036 = begin_scope (sk_template_parms, NULL);
32038 ++processing_template_decl;
32040 parser->fully_implicit_function_template_p = true;
32041 ++parser->num_template_parameter_lists;
32043 else
32045 /* Synthesize implicit template parameters at the end of the explicit
32046 template parameter list. */
32048 gcc_assert (current_template_parms);
32050 parser->implicit_template_scope = scope;
32052 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32053 parser->implicit_template_parms
32054 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32058 /* Synthesize a new template parameter and track the current template
32059 parameter chain with implicit_template_parms. */
32061 tree synth_id = make_generic_type_name ();
32062 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32063 synth_id);
32064 tree new_parm
32065 = process_template_parm (parser->implicit_template_parms,
32066 input_location,
32067 build_tree_list (NULL_TREE, synth_tmpl_parm),
32068 /*non_type=*/false,
32069 /*param_pack=*/false);
32072 if (parser->implicit_template_parms)
32073 parser->implicit_template_parms
32074 = TREE_CHAIN (parser->implicit_template_parms);
32075 else
32076 parser->implicit_template_parms = new_parm;
32078 tree new_type = TREE_TYPE (getdecls ());
32080 /* If creating a fully implicit function template, start the new implicit
32081 template parameter list with this synthesized type, otherwise grow the
32082 current template parameter list. */
32084 if (become_template)
32086 parent_scope->level_chain = current_binding_level;
32088 tree new_parms = make_tree_vec (1);
32089 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32090 current_template_parms = tree_cons (size_int (processing_template_decl),
32091 new_parms, current_template_parms);
32093 else
32095 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32096 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32097 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32098 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32101 current_binding_level = entry_scope;
32103 return new_type;
32106 /* Finish the declaration of a fully implicit function template. Such a
32107 template has no explicit template parameter list so has not been through the
32108 normal template head and tail processing. synthesize_implicit_template_parm
32109 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32110 provided if the declaration is a class member such that its template
32111 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32112 form is returned. Otherwise NULL_TREE is returned. */
32114 tree
32115 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32117 gcc_assert (parser->fully_implicit_function_template_p);
32119 if (member_decl_opt && member_decl_opt != error_mark_node
32120 && DECL_VIRTUAL_P (member_decl_opt))
32122 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32123 "implicit templates may not be %<virtual%>");
32124 DECL_VIRTUAL_P (member_decl_opt) = false;
32127 if (member_decl_opt)
32128 member_decl_opt = finish_member_template_decl (member_decl_opt);
32129 end_template_decl ();
32131 parser->fully_implicit_function_template_p = false;
32132 --parser->num_template_parameter_lists;
32134 return member_decl_opt;
32137 #include "gt-cp-parser.h"